@@ -297,6 +297,36 @@ def cumulative_sum(
297297 )
298298 return res
299299
300+
301+ def cumulative_prod (
302+ x : ndarray ,
303+ / ,
304+ xp ,
305+ * ,
306+ axis : Optional [int ] = None ,
307+ dtype : Optional [Dtype ] = None ,
308+ include_initial : bool = False ,
309+ ** kwargs
310+ ) -> ndarray :
311+ wrapped_xp = array_namespace (x )
312+
313+ if axis is None :
314+ if x .ndim > 1 :
315+ raise ValueError ("axis must be specified in cumulative_prod for more than one dimension" )
316+ axis = 0
317+
318+ res = xp .cumprod (x , axis = axis , dtype = dtype , ** kwargs )
319+
320+ # np.cumprod does not support include_initial
321+ if include_initial :
322+ initial_shape = list (x .shape )
323+ initial_shape [axis ] = 1
324+ res = xp .concatenate (
325+ [wrapped_xp .ones (shape = initial_shape , dtype = res .dtype , device = device (res )), res ],
326+ axis = axis ,
327+ )
328+ return res
329+
300330# The min and max argument names in clip are different and not optional in numpy, and type
301331# promotion behavior is different.
302332def clip (
@@ -549,7 +579,7 @@ def sign(x: ndarray, /, xp, **kwargs) -> ndarray:
549579 'linspace' , 'ones' , 'ones_like' , 'zeros' , 'zeros_like' ,
550580 'UniqueAllResult' , 'UniqueCountsResult' , 'UniqueInverseResult' ,
551581 'unique_all' , 'unique_counts' , 'unique_inverse' , 'unique_values' ,
552- 'astype' , 'std' , 'var' , 'cumulative_sum' , 'clip' , 'permute_dims' ,
582+ 'astype' , 'std' , 'var' , 'cumulative_sum' , 'cumulative_prod' , ' clip' , 'permute_dims' ,
553583 'reshape' , 'argsort' , 'sort' , 'nonzero' , 'ceil' , 'floor' , 'trunc' ,
554584 'matmul' , 'matrix_transpose' , 'tensordot' , 'vecdot' , 'isdtype' ,
555585 'unstack' , 'sign' ]
0 commit comments