summaryrefslogtreecommitdiff
path: root/numpy/array_api/_statistical_functions.py
diff options
context:
space:
mode:
authorGagandeep Singh <gdp.1807@gmail.com>2021-11-02 11:28:17 +0530
committerGagandeep Singh <gdp.1807@gmail.com>2021-11-02 11:28:17 +0530
commitc04509e86e97a69a0b5fcbeebdbec66faad3dbe0 (patch)
treeb5940db3ad46e55b88d566ec058007dc3c6b7e72 /numpy/array_api/_statistical_functions.py
parent56647dd47345a7fd24b4ee8d9d52025fcdc3b9ae (diff)
parentfae6fa47a3cf9b9c64af2f5bd11a3b644b1763d2 (diff)
downloadnumpy-c04509e86e97a69a0b5fcbeebdbec66faad3dbe0.tar.gz
resolved conflicts
Diffstat (limited to 'numpy/array_api/_statistical_functions.py')
-rw-r--r--numpy/array_api/_statistical_functions.py35
1 files changed, 34 insertions, 1 deletions
diff --git a/numpy/array_api/_statistical_functions.py b/numpy/array_api/_statistical_functions.py
index 63790b447..c5abf9468 100644
--- a/numpy/array_api/_statistical_functions.py
+++ b/numpy/array_api/_statistical_functions.py
@@ -1,8 +1,17 @@
from __future__ import annotations
+from ._dtypes import (
+ _floating_dtypes,
+ _numeric_dtypes,
+)
from ._array_object import Array
+from ._creation_functions import asarray
+from ._dtypes import float32, float64
-from typing import Optional, Tuple, Union
+from typing import TYPE_CHECKING, Optional, Tuple, Union
+
+if TYPE_CHECKING:
+ from ._typing import Dtype
import numpy as np
@@ -14,6 +23,8 @@ def max(
axis: Optional[Union[int, Tuple[int, ...]]] = None,
keepdims: bool = False,
) -> Array:
+ if x.dtype not in _numeric_dtypes:
+ raise TypeError("Only numeric dtypes are allowed in max")
return Array._new(np.max(x._array, axis=axis, keepdims=keepdims))
@@ -24,6 +35,8 @@ def mean(
axis: Optional[Union[int, Tuple[int, ...]]] = None,
keepdims: bool = False,
) -> Array:
+ if x.dtype not in _floating_dtypes:
+ raise TypeError("Only floating-point dtypes are allowed in mean")
return Array._new(np.mean(x._array, axis=axis, keepdims=keepdims))
@@ -34,6 +47,8 @@ def min(
axis: Optional[Union[int, Tuple[int, ...]]] = None,
keepdims: bool = False,
) -> Array:
+ if x.dtype not in _numeric_dtypes:
+ raise TypeError("Only numeric dtypes are allowed in min")
return Array._new(np.min(x._array, axis=axis, keepdims=keepdims))
@@ -42,8 +57,15 @@ def prod(
/,
*,
axis: Optional[Union[int, Tuple[int, ...]]] = None,
+ dtype: Optional[Dtype] = None,
keepdims: bool = False,
) -> Array:
+ if x.dtype not in _numeric_dtypes:
+ raise TypeError("Only numeric dtypes are allowed in prod")
+ # Note: sum() and prod() always upcast float32 to float64 for dtype=None
+ # We need to do so here before computing the product to avoid overflow
+ if dtype is None and x.dtype == float32:
+ x = asarray(x, dtype=float64)
return Array._new(np.prod(x._array, axis=axis, keepdims=keepdims))
@@ -56,6 +78,8 @@ def std(
keepdims: bool = False,
) -> Array:
# Note: the keyword argument correction is different here
+ if x.dtype not in _floating_dtypes:
+ raise TypeError("Only floating-point dtypes are allowed in std")
return Array._new(np.std(x._array, axis=axis, ddof=correction, keepdims=keepdims))
@@ -64,8 +88,15 @@ def sum(
/,
*,
axis: Optional[Union[int, Tuple[int, ...]]] = None,
+ dtype: Optional[Dtype] = None,
keepdims: bool = False,
) -> Array:
+ if x.dtype not in _numeric_dtypes:
+ raise TypeError("Only numeric dtypes are allowed in sum")
+ # Note: sum() and prod() always upcast float32 to float64 for dtype=None
+ # We need to do so here before summing to avoid overflow
+ if dtype is None and x.dtype == float32:
+ x = asarray(x, dtype=float64)
return Array._new(np.sum(x._array, axis=axis, keepdims=keepdims))
@@ -78,4 +109,6 @@ def var(
keepdims: bool = False,
) -> Array:
# Note: the keyword argument correction is different here
+ if x.dtype not in _floating_dtypes:
+ raise TypeError("Only floating-point dtypes are allowed in var")
return Array._new(np.var(x._array, axis=axis, ddof=correction, keepdims=keepdims))