Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/blosc2/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -433,6 +433,8 @@ def _raise(exc):
full_like,
save,
stack,
cumulative_sum,
cumulative_prod,
)
from .embed_store import EmbedStore, estore_from_cframe
from .dict_store import DictStore
Expand Down
66 changes: 66 additions & 0 deletions src/blosc2/ndarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -6370,3 +6370,69 @@ def mygen(i):
for a in myarrs:
out += (broadcast_to(a, shape),)
return out


def cumulative_sum(x, /, *, axis=None, dtype=None, include_initial=False):
"""Array API cumulative_sum.

Parameters
----------
x : array-like or NDArray
Input array.
axis : int or None
Axis along which to compute the cumulative sum. For ndim > 1, this
must be provided. Default: None (flatten).
dtype : dtype or None
Accumulator/output dtype. If None, follow type-promotion rules.
include_initial : bool, default False
If True, include the additive identity (zero) as the first value along
the reduced axis (output length N+1).

Returns
-------
NDArray
Array containing the cumulative sums.
"""
# Accept blosc2.NDArray or array-like
if isinstance(x, NDArray):
arr = x[:] # read into NumPy array
else:
arr = np.asarray(x)

# Use NumPy's Array API compatible cumulative_sum function
res = np.cumulative_sum(arr, axis=axis, dtype=dtype, include_initial=include_initial)

return blosc2.asarray(res)


def cumulative_prod(x, /, *, axis=None, dtype=None, include_initial=False):
"""Array API cumulative_prod.

Parameters
----------
x : array-like or NDArray
Input array.
axis : int or None
Axis along which to compute the cumulative product. For ndim > 1, this
must be provided. Default: None (flatten).
dtype : dtype or None
Accumulator/output dtype. If None, follow type-promotion rules.
include_initial : bool, default False
If True, include the multiplicative identity (one) as the first value along
the reduced axis (output length N+1).

Returns
-------
NDArray
Array containing the cumulative products.
"""
# Accept blosc2.NDArray or array-like
if isinstance(x, NDArray):
arr = x[:] # read into NumPy array
else:
arr = np.asarray(x)

# Use NumPy's Array API compatible cumulative_prod function
res = np.cumulative_prod(arr, axis=axis, dtype=dtype, include_initial=include_initial)

return blosc2.asarray(res)
Loading