@@ -292,6 +292,36 @@ def cumulative_sum(
292292 )
293293 return res
294294
295+
296+ def cumulative_prod (
297+ x : ndarray ,
298+ / ,
299+ xp ,
300+ * ,
301+ axis : Optional [int ] = None ,
302+ dtype : Optional [Dtype ] = None ,
303+ include_initial : bool = False ,
304+ ** kwargs
305+ ) -> ndarray :
306+ wrapped_xp = array_namespace (x )
307+
308+ if axis is None :
309+ if x .ndim > 1 :
310+ raise ValueError ("axis must be specified in cumulative_prod for more than one dimension" )
311+ axis = 0
312+
313+ res = xp .cumprod (x , axis = axis , dtype = dtype , ** kwargs )
314+
315+ # np.cumprod does not support include_initial
316+ if include_initial :
317+ initial_shape = list (x .shape )
318+ initial_shape [axis ] = 1
319+ res = xp .concatenate (
320+ [wrapped_xp .ones (shape = initial_shape , dtype = res .dtype , device = device (res )), res ],
321+ axis = axis ,
322+ )
323+ return res
324+
295325# The min and max argument names in clip are different and not optional in numpy, and type
296326# promotion behavior is different.
297327def clip (
@@ -544,7 +574,7 @@ def sign(x: ndarray, /, xp, **kwargs) -> ndarray:
544574 'linspace' , 'ones' , 'ones_like' , 'zeros' , 'zeros_like' ,
545575 'UniqueAllResult' , 'UniqueCountsResult' , 'UniqueInverseResult' ,
546576 'unique_all' , 'unique_counts' , 'unique_inverse' , 'unique_values' ,
547- 'std' , 'var' , 'cumulative_sum' , 'clip' , 'permute_dims' ,
577+ 'astype' , ' std' , 'var' , 'cumulative_sum' , 'cumulative_prod' , 'clip' , 'permute_dims' ,
548578 'reshape' , 'argsort' , 'sort' , 'nonzero' , 'ceil' , 'floor' , 'trunc' ,
549579 'matmul' , 'matrix_transpose' , 'tensordot' , 'vecdot' , 'isdtype' ,
550580 'unstack' , 'sign' ]
0 commit comments