Skip to content

Commit 7cb3214

Browse files
committed
Fix some issues with cumulative_sum
- The behavior for dtype=None was incorrect. - Fix an error with axis=-1, include_initial=True.
1 parent 752b706 commit 7cb3214

File tree

1 file changed

+7
-4
lines changed

1 file changed

+7
-4
lines changed

array_api_strict/_statistical_functions.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,9 @@ def cumulative_sum(
3030
) -> Array:
3131
if x.dtype not in _numeric_dtypes:
3232
raise TypeError("Only numeric dtypes are allowed in cumulative_sum")
33-
if dtype is None:
34-
dtype = x.dtype
33+
dt = x.dtype if dtype is None else dtype
34+
if dtype is not None:
35+
dtype = dtype._np_dtype
3536

3637
# TODO: The standard is not clear about what should happen when x.ndim == 0.
3738
if axis is None:
@@ -40,8 +41,10 @@ def cumulative_sum(
4041
axis = 0
4142
# np.cumsum does not support include_initial
4243
if include_initial:
43-
x = concat([zeros(x.shape[:axis] + (1,) + x.shape[axis + 1:], dtype=dtype), x], axis=axis)
44-
return Array._new(np.cumsum(x._array, axis=axis, dtype=dtype._np_dtype))
44+
if axis < 0:
45+
axis += x.ndim
46+
x = concat([zeros(x.shape[:axis] + (1,) + x.shape[axis + 1:], dtype=dt), x], axis=axis)
47+
return Array._new(np.cumsum(x._array, axis=axis, dtype=dtype))
4548

4649
def max(
4750
x: Array,

0 commit comments

Comments
 (0)