94
94
"cross" ,
95
95
"cumprod" ,
96
96
"cumsum" ,
97
+ "cumulative_prod" ,
98
+ "cumulative_sum" ,
97
99
"diff" ,
98
100
"divide" ,
99
101
"ediff1d" ,
@@ -1062,6 +1064,8 @@ def cumprod(a, axis=None, dtype=None, out=None):
1062
1064
1063
1065
See Also
1064
1066
--------
1067
+ :obj:`dpnp.cumulative_prod` : Array API compatible alternative for
1068
+ :obj:`dpnp.cumprod`.
1065
1069
:obj:`dpnp.prod` : Product array elements.
1066
1070
1067
1071
Examples
@@ -1143,6 +1147,8 @@ def cumsum(a, axis=None, dtype=None, out=None):
1143
1147
1144
1148
See Also
1145
1149
--------
1150
+ :obj:`dpnp.cumulative_sum` : Array API compatible alternative for
1151
+ :obj:`dpnp.cumsum`.
1146
1152
:obj:`dpnp.sum` : Sum array elements.
1147
1153
:obj:`dpnp.trapezoid` : Integration of array values using composite
1148
1154
trapezoidal rule.
@@ -1157,8 +1163,8 @@ def cumsum(a, axis=None, dtype=None, out=None):
1157
1163
[4, 5, 6]])
1158
1164
>>> np.cumsum(a)
1159
1165
array([ 1, 3, 6, 10, 15, 21])
1160
- >>> np.cumsum(a, dtype=float ) # specifies type of output value(s)
1161
- array([ 1., 3., 6., 10., 15., 21.])
1166
+ >>> np.cumsum(a, dtype=np.float32 ) # specifies type of output value(s)
1167
+ array([ 1., 3., 6., 10., 15., 21.], dtype=np.float32 )
1162
1168
1163
1169
>>> np.cumsum(a, axis=0) # sum over rows for each of the 3 columns
1164
1170
array([[1, 2, 3],
@@ -1169,8 +1175,8 @@ def cumsum(a, axis=None, dtype=None, out=None):
1169
1175
1170
1176
``cumsum(b)[-1]`` may not be equal to ``sum(b)``
1171
1177
1172
- >>> b = np.array([1, 2e-9 , 3e-9 ] * 10000 )
1173
- >>> b.cumsum().dtype == b.sum().dtype == np.float64
1178
+ >>> b = np.array([1, 2e-7 , 3e-7 ] * 100000, dtype=np.float32 )
1179
+ >>> b.cumsum().dtype == b.sum().dtype == np.float32
1174
1180
True
1175
1181
>>> b.cumsum()[-1] == b.sum()
1176
1182
array(False)
@@ -1194,6 +1200,184 @@ def cumsum(a, axis=None, dtype=None, out=None):
1194
1200
)
1195
1201
1196
1202
1203
+ def cumulative_prod (
1204
+ x , / , * , axis = None , dtype = None , out = None , include_initial = False
1205
+ ):
1206
+ """
1207
+ Return the cumulative product of elements along a given axis.
1208
+
1209
+ This function is an Array API compatible alternative to :obj:`dpnp.cumprod`.
1210
+
1211
+ For full documentation refer to :obj:`numpy.cumulative_prod`.
1212
+
1213
+ Parameters
1214
+ ----------
1215
+ x : {dpnp.ndarray, usm_ndarray}
1216
+ Input array.
1217
+ axis : {None, int}, optional
1218
+ Axis along which the cumulative product is computed. The default value
1219
+ is only allowed for one-dimensional arrays. For arrays with more than
1220
+ one dimension `axis` is required.
1221
+ Default: ``None``.
1222
+ dtype : {None, dtype}, optional
1223
+ Type of the returned array and of the accumulator in which the elements
1224
+ are summed. If `dtype` is not specified, it defaults to the dtype of
1225
+ `x`, unless `x` has an integer dtype with a precision less than that of
1226
+ the default platform integer. In that case, the default platform
1227
+ integer is used.
1228
+ Default: ``None``.
1229
+ out : {None, dpnp.ndarray, usm_ndarray}, optional
1230
+ Alternative output array in which to place the result. It must have the
1231
+ same shape and buffer length as the expected output but the type will
1232
+ be cast if necessary.
1233
+ Default: ``None``.
1234
+ include_initial : bool, optional
1235
+ Boolean indicating whether to include the initial value (ones) as
1236
+ the first value in the output. With ``include_initial=True``
1237
+ the shape of the output is different than the shape of the input.
1238
+ Default: ``False``.
1239
+
1240
+ Returns
1241
+ -------
1242
+ out : dpnp.ndarray
1243
+ A new array holding the result is returned unless `out` is specified,
1244
+ in which case a reference to `out` is returned. The
1245
+ result has the same shape as `x` if ``include_initial=False``.
1246
+
1247
+ See Also
1248
+ --------
1249
+ :obj:`dpnp.prod` : Product array elements.
1250
+
1251
+ Examples
1252
+ --------
1253
+ >>> import dpnp as np
1254
+ >>> a = np.array([1, 2, 3])
1255
+ >>> np.cumulative_prod(a) # intermediate results 1, 1*2
1256
+ ... # total product 1*2*3 = 6
1257
+ array([1, 2, 6])
1258
+ >>> a = np.array([1, 2, 3, 4, 5, 6])
1259
+ >>> np.cumulative_prod(a, dtype=np.float32) # specify type of output
1260
+ array([ 1., 2., 6., 24., 120., 720.], dtype=float32)
1261
+
1262
+ The cumulative product for each column (i.e., over the rows) of `b`:
1263
+
1264
+ >>> b = np.array([[1, 2, 3], [4, 5, 6]])
1265
+ >>> np.cumulative_prod(b, axis=0)
1266
+ array([[ 1, 2, 3],
1267
+ [ 4, 10, 18]])
1268
+
1269
+ The cumulative product for each row (i.e. over the columns) of `b`:
1270
+
1271
+ >>> np.cumulative_prod(b, axis=1)
1272
+ array([[ 1, 2, 6],
1273
+ [ 4, 20, 120]])
1274
+
1275
+ """
1276
+
1277
+ return dpnp_wrap_reduction_call (
1278
+ x ,
1279
+ out ,
1280
+ dpt .cumulative_prod ,
1281
+ _get_reduction_res_dt ,
1282
+ dpnp .get_usm_ndarray (x ),
1283
+ axis = axis ,
1284
+ dtype = dtype ,
1285
+ include_initial = include_initial ,
1286
+ )
1287
+
1288
+
1289
+ def cumulative_sum (
1290
+ x , / , * , axis = None , dtype = None , out = None , include_initial = False
1291
+ ):
1292
+ """
1293
+ Return the cumulative sum of the elements along a given axis.
1294
+
1295
+ This function is an Array API compatible alternative to :obj:`dpnp.cumsum`.
1296
+
1297
+ For full documentation refer to :obj:`numpy.cumulative_sum`.
1298
+
1299
+ Parameters
1300
+ ----------
1301
+ x : {dpnp.ndarray, usm_ndarray}
1302
+ Input array.
1303
+ axis : {None, int}, optional
1304
+ Axis along which the cumulative sum is computed. The default value
1305
+ is only allowed for one-dimensional arrays. For arrays with more than
1306
+ one dimension `axis` is required.
1307
+ Default: ``None``.
1308
+ dtype : {None, dtype}, optional
1309
+ Type of the returned array and of the accumulator in which the elements
1310
+ are summed. If `dtype` is not specified, it defaults to the dtype of
1311
+ `x`, unless `x` has an integer dtype with a precision less than that of
1312
+ the default platform integer. In that case, the default platform
1313
+ integer is used.
1314
+ Default: ``None``.
1315
+ out : {None, dpnp.ndarray, usm_ndarray}, optional
1316
+ Alternative output array in which to place the result. It must have the
1317
+ same shape and buffer length as the expected output but the type will
1318
+ be cast if necessary.
1319
+ Default: ``None``.
1320
+ include_initial : bool, optional
1321
+ Boolean indicating whether to include the initial value (ones) as
1322
+ the first value in the output. With ``include_initial=True``
1323
+ the shape of the output is different than the shape of the input.
1324
+ Default: ``False``.
1325
+
1326
+ Returns
1327
+ -------
1328
+ out : dpnp.ndarray
1329
+ A new array holding the result is returned unless `out` is specified,
1330
+ in which case a reference to `out` is returned. The
1331
+ result has the same shape as `x` if ``include_initial=False``.
1332
+
1333
+ See Also
1334
+ --------
1335
+ :obj:`dpnp.sum` : Sum array elements.
1336
+ :obj:`dpnp.trapezoid` : Integration of array values using composite
1337
+ trapezoidal rule.
1338
+ :obj:`dpnp.diff` : Calculate the n-th discrete difference along given axis.
1339
+
1340
+ Examples
1341
+ --------
1342
+ >>> import dpnp as np
1343
+ >>> a = np.array([1, 2, 3, 4, 5, 6])
1344
+ >>> a
1345
+ array([1, 2, 3, 4, 5, 6])
1346
+ >>> np.cumulative_sum(a)
1347
+ array([ 1, 3, 6, 10, 15, 21])
1348
+ >>> np.cumulative_sum(a, dtype=np.float32) # specifies output dtype
1349
+ array([ 1., 3., 6., 10., 15., 21.], dtype=np.float32)
1350
+
1351
+ >>> b = np.array([[1, 2, 3], [4, 5, 6]])
1352
+ >>> np.cumulative_sum(b, axis=0) # sum over rows for each of the 3 columns
1353
+ array([[1, 2, 3],
1354
+ [5, 7, 9]])
1355
+ >>> np.cumulative_sum(b, axis=1) # sum over columns for each of the 2 rows
1356
+ array([[ 1, 3, 6],
1357
+ [ 4, 9, 15]])
1358
+
1359
+ ``cumulative_sum(c)[-1]`` may not be equal to ``sum(c)``
1360
+
1361
+ >>> c = np.array([1, 2e-7, 3e-7] * 100000, dtype=np.float32)
1362
+ >>> np.cumulative_sum(c).dtype == c.sum().dtype == np.float32
1363
+ True
1364
+ >>> np.cumulative_sum(c)[-1] == c.sum()
1365
+ array(False)
1366
+
1367
+ """
1368
+
1369
+ return dpnp_wrap_reduction_call (
1370
+ x ,
1371
+ out ,
1372
+ dpt .cumulative_sum ,
1373
+ _get_reduction_res_dt ,
1374
+ dpnp .get_usm_ndarray (x ),
1375
+ axis = axis ,
1376
+ dtype = dtype ,
1377
+ include_initial = include_initial ,
1378
+ )
1379
+
1380
+
1197
1381
def diff (a , n = 1 , axis = - 1 , prepend = None , append = None ):
1198
1382
"""
1199
1383
Calculate the n-th discrete difference along the given axis.
0 commit comments