@@ -264,6 +264,36 @@ def var(
264264) -> ndarray :
265265 return xp .var (x , axis = axis , ddof = correction , keepdims = keepdims , ** kwargs )
266266
267+ # cumulative_sum is renamed from cumsum, and adds the include_initial keyword
268+ # argument
269+
270+ def cumulative_sum (
271+ x : ndarray ,
272+ / ,
273+ xp ,
274+ * ,
275+ axis : Optional [int ] = None ,
276+ dtype : Optional [Dtype ] = None ,
277+ include_initial : bool = False ,
278+ ** kwargs
279+ ) -> ndarray :
280+ # TODO: The standard is not clear about what should happen when x.ndim == 0.
281+ if axis is None :
282+ if x .ndim > 1 :
283+ raise ValueError ("axis must be specified in cumulative_sum for more than one dimension" )
284+ axis = 0
285+
286+ res = xp .cumsum (x , axis = axis , dtype = dtype , ** kwargs )
287+
288+ # np.cumsum does not support include_initial
289+ if include_initial :
290+ initial_shape = list (x .shape )
291+ initial_shape [axis ] = 1
292+ res = xp .concatenate (
293+ [xp .zeros_like (res , shape = initial_shape ), res ],
294+ axis = axis ,
295+ )
296+ return res
267297
268298# The min and max argument names in clip are different and not optional in numpy, and type
269299# promotion behavior is different.
@@ -502,6 +532,7 @@ def unstack(x: ndarray, /, xp, *, axis: int = 0) -> Tuple[ndarray, ...]:
502532 'linspace' , 'ones' , 'ones_like' , 'zeros' , 'zeros_like' ,
503533 'UniqueAllResult' , 'UniqueCountsResult' , 'UniqueInverseResult' ,
504534 'unique_all' , 'unique_counts' , 'unique_inverse' , 'unique_values' ,
505- 'astype' , 'std' , 'var' , 'clip' , 'permute_dims' , 'reshape' ,
506- 'argsort' , 'sort' , 'nonzero' , 'ceil' , 'floor' , 'trunc' , 'matmul' ,
507- 'matrix_transpose' , 'tensordot' , 'vecdot' , 'isdtype' , 'unstack' ]
535+ 'astype' , 'std' , 'var' , 'cumulative_sum' , 'clip' , 'permute_dims' ,
536+ 'reshape' , 'argsort' , 'sort' , 'nonzero' , 'ceil' , 'floor' , 'trunc' ,
537+ 'matmul' , 'matrix_transpose' , 'tensordot' , 'vecdot' , 'isdtype' ,
538+ 'unstack' ]
0 commit comments