On Fri, Aug 18, 2023 at 4:59 AM Ronald van Elburg <r.a.j.van.elburg@hetnet.nl> wrote:
I was trying to get a feel for how often the work around occurs. I found three clear examples in Scipy and one unclear case. One case in holoviews. Two in numpy. One from soundappraisal's code base.

Anyone interested in this enhancement is encouraged to review the discussion in that pull request (https://github.com/numpy/numpy/pull/14542), and an earlier issue from 2015: https://github.com/numpy/numpy/issues/6044

Warren

Next to prepending to the output, I also see prepending to the input as a workaround.

Some examples of workarounds:

scipy: (prepending to the output)

scipy/scipy/sparse/construct.py:

'''Python
row_offsets = np.append(0, np.cumsum(brow_lengths))
col_offsets = np.append(0, np.cumsum(bcol_lengths))
'''

scipy/scipy/sparse/dia.py:

'''Python
indptr = np.zeros(num_cols + 1, dtype=idx_dtype)
'''

scipy/scipy/sparse/csgraph/_tools.pyx:

'''Python
indptr = np.zeros(N + 1, dtype=ITYPE)
'''

Not sure whether this is also an example:

scipy/scipy/stats/_hypotests_pythran.py
'''Python
# Now fill in the values. We cannot use cumsum, unfortunately.
val = 0.0 if minj == 0 else 1.0
for jj in range(maxj - minj):
j = jj + minj
val = (A[jj + minj - lastminj] * i + val * j) / (i + j)
A[jj] = val
'''

holoviews: (prepending to the input)

'''Python
# We add a zero in the begging for the cumulative sum
points = points.cumsum()
'''

numpy (prepending to the input):

numpy/numpy/lib/_iotools.py :

'''Python
idx = np.cumsum([0] + list(delimiter))
'''

numpy/numpy/lib/histograms.py

'''Python
cw = np.concatenate((zero, sw.cumsum()))
'''

soundappraisal own code: (prepending to the output)

'''Python
def get_cumulativepixelareas(whiteboard):
whiteboard['cumulativepixelareas'] = \
np.concatenate((np.array([0, ]), np.cumsum(whiteboard['pixelareas'])))
return True
'''
_______________________________________________
NumPy-Discussion mailing list -- numpy-discussion@python.org
To unsubscribe send an email to numpy-discussion-leave@python.org
https://mail.python.org/mailman3/lists/numpy-discussion.python.org/