|
40 | 40 | """
|
41 | 41 |
|
42 | 42 |
|
| 43 | +import dpctl.tensor as dpt |
43 | 44 | import numpy
|
44 | 45 |
|
45 | 46 | import dpnp
|
46 | 47 | from dpnp.dpnp_algo import *
|
| 48 | +from dpnp.dpnp_array import dpnp_array |
47 | 49 | from dpnp.dpnp_utils import *
|
48 | 50 |
|
49 | 51 | from .dpnp_algo.dpnp_elementwise_common import (
|
|
98 | 100 | "log1p",
|
99 | 101 | "log2",
|
100 | 102 | "logaddexp",
|
| 103 | + "logsumexp", |
101 | 104 | "rad2deg",
|
102 | 105 | "radians",
|
103 | 106 | "reciprocal",
|
| 107 | + "reduce_hypot", |
104 | 108 | "rsqrt",
|
105 | 109 | "sin",
|
106 | 110 | "sinh",
|
@@ -989,6 +993,10 @@ def hypot(
|
989 | 993 | Otherwise the function will be executed sequentially on CPU.
|
990 | 994 | Input array data types are limited by supported real-valued data types.
|
991 | 995 |
|
| 996 | + See Also |
| 997 | + -------- |
| 998 | + :obj:`dpnp.reduce_hypot` : The square root of the sum of squares of elements in the input array. |
| 999 | +
|
992 | 1000 | Examples
|
993 | 1001 | --------
|
994 | 1002 | >>> import dpnp as np
|
@@ -1303,6 +1311,7 @@ def logaddexp(
|
1303 | 1311 | --------
|
1304 | 1312 | :obj:`dpnp.log` : Natural logarithm, element-wise.
|
1305 | 1313 | :obj:`dpnp.exp` : Exponential, element-wise.
|
| 1314 | + :obj:`dpnp.logsumdexp` : Logarithm of the sum of exponentials of elements in the input array. |
1306 | 1315 |
|
1307 | 1316 | Examples
|
1308 | 1317 | --------
|
@@ -1331,6 +1340,81 @@ def logaddexp(
|
1331 | 1340 | )
|
1332 | 1341 |
|
1333 | 1342 |
|
| 1343 | +def logsumexp(x, axis=None, out=None, dtype=None, keepdims=False): |
| 1344 | + """ |
| 1345 | + Calculates the logarithm of the sum of exponentials of elements in the input array. |
| 1346 | +
|
| 1347 | + Parameters |
| 1348 | + ---------- |
| 1349 | + x : {dpnp_array, usm_ndarray} |
| 1350 | + Input array, expected to have a real-valued data type. |
| 1351 | + axis : int or tuple of ints, optional |
| 1352 | + Axis or axes along which values must be computed. If a tuple |
| 1353 | + of unique integers, values are computed over multiple axes. |
| 1354 | + If ``None``, the result is computed over the entire array. |
| 1355 | + Default: ``None``. |
| 1356 | + out : {dpnp_array, usm_ndarray}, optional |
| 1357 | + If provided, the result will be inserted into this array. It should |
| 1358 | + be of the appropriate shape and dtype. |
| 1359 | + dtype : data type, optional |
| 1360 | + Data type of the returned array. If ``None``, the default data |
| 1361 | + type is inferred from the "kind" of the input array data type. |
| 1362 | + * If `x` has a real-valued floating-point data type, |
| 1363 | + the returned array will have the default real-valued |
| 1364 | + floating-point data type for the device where input |
| 1365 | + array `x` is allocated. |
| 1366 | + * If `x` has a boolean or integral data type, the returned array |
| 1367 | + will have the default floating point data type for the device |
| 1368 | + where input array `x` is allocated. |
| 1369 | + * If `x` has a complex-valued floating-point data type, |
| 1370 | + an error is raised. |
| 1371 | + If the data type (either specified or resolved) differs from the |
| 1372 | + data type of `x`, the input array elements are cast to the |
| 1373 | + specified data type before computing the result. Default: ``None``. |
| 1374 | + keepdims : bool |
| 1375 | + If ``True``, the reduced axes (dimensions) are included in the result |
| 1376 | + as singleton dimensions, so that the returned array remains |
| 1377 | + compatible with the input arrays according to Array Broadcasting |
| 1378 | + rules. Otherwise, if ``False``, the reduced axes are not included in |
| 1379 | + the returned array. Default: ``False``. |
| 1380 | +
|
| 1381 | + Returns |
| 1382 | + ------- |
| 1383 | + out : dpnp.ndarray |
| 1384 | + An array containing the results. If the result was computed over |
| 1385 | + the entire array, a zero-dimensional array is returned. The returned |
| 1386 | + array has the data type as described in the `dtype` parameter |
| 1387 | + description above. |
| 1388 | +
|
| 1389 | + Note |
| 1390 | + ---- |
| 1391 | + This function is equivalent of `numpy.logaddexp.reduce`. |
| 1392 | +
|
| 1393 | + See Also |
| 1394 | + -------- |
| 1395 | + :obj:`dpnp.log` : Natural logarithm, element-wise. |
| 1396 | + :obj:`dpnp.exp` : Exponential, element-wise. |
| 1397 | + :obj:`dpnp.logaddexp` : Logarithm of the sum of exponentiations of the inputs, element-wise. |
| 1398 | +
|
| 1399 | + Examples |
| 1400 | + -------- |
| 1401 | + >>> import dpnp as np |
| 1402 | + >>> a = np.ones(10) |
| 1403 | + >>> np.logsumexp(a) |
| 1404 | + array(3.30258509) |
| 1405 | + >>> np.log(np.sum(np.exp(a))) |
| 1406 | + array(3.30258509) |
| 1407 | +
|
| 1408 | + """ |
| 1409 | + |
| 1410 | + dpt_array = dpnp.get_usm_ndarray(x) |
| 1411 | + result = dpnp_array._create_from_usm_ndarray( |
| 1412 | + dpt.logsumexp(dpt_array, axis=axis, dtype=dtype, keepdims=keepdims) |
| 1413 | + ) |
| 1414 | + |
| 1415 | + return dpnp.get_result_array(result, out, casting="same_kind") |
| 1416 | + |
| 1417 | + |
1334 | 1418 | def reciprocal(x1, **kwargs):
|
1335 | 1419 | """
|
1336 | 1420 | Return the reciprocal of the argument, element-wise.
|
@@ -1363,6 +1447,79 @@ def reciprocal(x1, **kwargs):
|
1363 | 1447 | return call_origin(numpy.reciprocal, x1, **kwargs)
|
1364 | 1448 |
|
1365 | 1449 |
|
| 1450 | +def reduce_hypot(x, axis=None, out=None, dtype=None, keepdims=False): |
| 1451 | + """ |
| 1452 | + Calculates the square root of the sum of squares of elements in the input array. |
| 1453 | +
|
| 1454 | + Parameters |
| 1455 | + ---------- |
| 1456 | + x : {dpnp_array, usm_ndarray} |
| 1457 | + Input array, expected to have a real-valued data type. |
| 1458 | + axis : int or tuple of ints, optional |
| 1459 | + Axis or axes along which values must be computed. If a tuple |
| 1460 | + of unique integers, values are computed over multiple axes. |
| 1461 | + If ``None``, the result is computed over the entire array. |
| 1462 | + Default: ``None``. |
| 1463 | + out : {dpnp_array, usm_ndarray}, optional |
| 1464 | + If provided, the result will be inserted into this array. It should |
| 1465 | + be of the appropriate shape and dtype. |
| 1466 | + dtype : data type, optional |
| 1467 | + Data type of the returned array. If ``None``, the default data |
| 1468 | + type is inferred from the "kind" of the input array data type. |
| 1469 | + * If `x` has a real-valued floating-point data type, |
| 1470 | + the returned array will have the default real-valued |
| 1471 | + floating-point data type for the device where input |
| 1472 | + array `x` is allocated. |
| 1473 | + * If `x` has a boolean or integral data type, the returned array |
| 1474 | + will have the default floating point data type for the device |
| 1475 | + where input array `x` is allocated. |
| 1476 | + * If `x` has a complex-valued floating-point data type, |
| 1477 | + an error is raised. |
| 1478 | + If the data type (either specified or resolved) differs from the |
| 1479 | + data type of `x`, the input array elements are cast to the |
| 1480 | + specified data type before computing the result. Default: ``None``. |
| 1481 | + keepdims : bool |
| 1482 | + If ``True``, the reduced axes (dimensions) are included in the result |
| 1483 | + as singleton dimensions, so that the returned array remains |
| 1484 | + compatible with the input arrays according to Array Broadcasting |
| 1485 | + rules. Otherwise, if ``False``, the reduced axes are not included in |
| 1486 | + the returned array. Default: ``False``. |
| 1487 | +
|
| 1488 | + Returns |
| 1489 | + ------- |
| 1490 | + out : dpnp.ndarray |
| 1491 | + An array containing the results. If the result was computed over |
| 1492 | + the entire array, a zero-dimensional array is returned. The returned |
| 1493 | + array has the data type as described in the `dtype` parameter |
| 1494 | + description above. |
| 1495 | +
|
| 1496 | + Note |
| 1497 | + ---- |
| 1498 | + This function is equivalent of `numpy.hypot.reduce`. |
| 1499 | +
|
| 1500 | + See Also |
| 1501 | + -------- |
| 1502 | + :obj:`dpnp.hypot` : Given the "legs" of a right triangle, return its hypotenuse. |
| 1503 | +
|
| 1504 | + Examples |
| 1505 | + -------- |
| 1506 | + >>> import dpnp as np |
| 1507 | + >>> a = np.ones(10) |
| 1508 | + >>> np.reduce_hypot(a) |
| 1509 | + array(3.16227766) |
| 1510 | + >>> np.sqrt(np.sum(np.square(a))) |
| 1511 | + array(3.16227766) |
| 1512 | +
|
| 1513 | + """ |
| 1514 | + |
| 1515 | + dpt_array = dpnp.get_usm_ndarray(x) |
| 1516 | + result = dpnp_array._create_from_usm_ndarray( |
| 1517 | + dpt.reduce_hypot(dpt_array, axis=axis, dtype=dtype, keepdims=keepdims) |
| 1518 | + ) |
| 1519 | + |
| 1520 | + return dpnp.get_result_array(result, out, casting="same_kind") |
| 1521 | + |
| 1522 | + |
1366 | 1523 | def rsqrt(
|
1367 | 1524 | x,
|
1368 | 1525 | /,
|
|
0 commit comments