diff options
author | Aaron Meurer <asmeurer@gmail.com> | 2021-09-25 17:34:22 -0500 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-09-25 16:34:22 -0600 |
commit | 2d112a98ed7597c4120b31908384ae09b0304659 (patch) | |
tree | a03fcf59a0ca9cfff10ca2b346bd4c9d37268451 /numpy/array_api/_creation_functions.py | |
parent | ac78192390943d90ebae2f4e209e194914d0bc97 (diff) | |
download | numpy-2d112a98ed7597c4120b31908384ae09b0304659.tar.gz |
ENH: Updates to numpy.array_api (#19937)
* Add __index__ to array_api and update __int__, __bool__, and __float__
The spec specifies that they should only work on arrays with corresponding
dtypes. __index__ is new in the spec since the initial PR, and works
identically to np.array.__index__.
* Add the to_device method to the array_api
This method is new since #18585. It does nothing in NumPy since NumPy does not
support non-CPU devices.
* Update transpose methods in the array_api
transpose() was renamed to matrix_transpose() and now operates on stacks of
matrices. A function to permute dimensions will be added once it is finalized
in the spec. The attribute mT was added and the T attribute was updated to
only operate on 2-dimensional arrays as per the spec.
* Restrict input dtypes in the array API statistical functions
* Add the dtype parameter to the array API sum() and prod()
* Add the function permute_dims() to the array_api namespace
permute_dims() is the replacement for transpose(), which was split into
permute_dims() and matrix_transpose().
* Add tril and triu to the array API namespace
* Fix the array_api Array.__repr__ to indent the array properly
* Make the Device type in the array_api just accept the string "cpu"
Diffstat (limited to 'numpy/array_api/_creation_functions.py')
-rw-r--r-- | numpy/array_api/_creation_functions.py | 30 |
1 files changed, 29 insertions, 1 deletions
diff --git a/numpy/array_api/_creation_functions.py b/numpy/array_api/_creation_functions.py index e9c01e7e6..9f8136267 100644 --- a/numpy/array_api/_creation_functions.py +++ b/numpy/array_api/_creation_functions.py @@ -22,7 +22,7 @@ def _check_valid_dtype(dtype): # Note: Only spelling dtypes as the dtype objects is supported. # We use this instead of "dtype in _all_dtypes" because the dtype objects - # define equality with the sorts of things we want to disallw. + # define equality with the sorts of things we want to disallow. for d in (None,) + _all_dtypes: if dtype is d: return @@ -281,6 +281,34 @@ def ones_like( return Array._new(np.ones_like(x._array, dtype=dtype)) +def tril(x: Array, /, *, k: int = 0) -> Array: + """ + Array API compatible wrapper for :py:func:`np.tril <numpy.tril>`. + + See its docstring for more information. + """ + from ._array_object import Array + + if x.ndim < 2: + # Note: Unlike np.tril, x must be at least 2-D + raise ValueError("x must be at least 2-dimensional for tril") + return Array._new(np.tril(x._array, k=k)) + + +def triu(x: Array, /, *, k: int = 0) -> Array: + """ + Array API compatible wrapper for :py:func:`np.triu <numpy.triu>`. + + See its docstring for more information. + """ + from ._array_object import Array + + if x.ndim < 2: + # Note: Unlike np.triu, x must be at least 2-D + raise ValueError("x must be at least 2-dimensional for triu") + return Array._new(np.triu(x._array, k=k)) + + def zeros( shape: Union[int, Tuple[int, ...]], *, |