n-d array object for the array API namespace. See the docstring of :py:obj:`np.ndarray ` for more information. This is a wrapper around numpy.ndarray that restricts the usage to only those things that are required by the array API namespace. Note, attributes
| 43 | |
| 44 | |
| 45 | class Array: |
| 46 | """ |
| 47 | n-d array object for the array API namespace. |
| 48 | |
| 49 | See the docstring of :py:obj:`np.ndarray <numpy.ndarray>` for more |
| 50 | information. |
| 51 | |
| 52 | This is a wrapper around numpy.ndarray that restricts the usage to only |
| 53 | those things that are required by the array API namespace. Note, |
| 54 | attributes on this object that start with a single underscore are not part |
| 55 | of the API specification and should only be used internally. This object |
| 56 | should not be constructed directly. Rather, use one of the creation |
| 57 | functions, such as asarray(). |
| 58 | |
| 59 | """ |
| 60 | _array: np.ndarray[Any, Any] |
| 61 | |
| 62 | # Use a custom constructor instead of __init__, as manually initializing |
| 63 | # this class is not supported API. |
| 64 | @classmethod |
| 65 | def _new(cls, x, /): |
| 66 | """ |
| 67 | This is a private method for initializing the array API Array |
| 68 | object. |
| 69 | |
| 70 | Functions outside of the array_api submodule should not use this |
| 71 | method. Use one of the creation functions instead, such as |
| 72 | ``asarray``. |
| 73 | |
| 74 | """ |
| 75 | obj = super().__new__(cls) |
| 76 | # Note: The spec does not have array scalars, only 0-D arrays. |
| 77 | if isinstance(x, np.generic): |
| 78 | # Convert the array scalar to a 0-D array |
| 79 | x = np.asarray(x) |
| 80 | if x.dtype not in _all_dtypes: |
| 81 | raise TypeError( |
| 82 | f"The array_api namespace does not support the dtype '{x.dtype}'" |
| 83 | ) |
| 84 | obj._array = x |
| 85 | return obj |
| 86 | |
| 87 | # Prevent Array() from working |
| 88 | def __new__(cls, *args, **kwargs): |
| 89 | raise TypeError( |
| 90 | "The array_api Array object should not be instantiated directly. Use an array creation function, such as asarray(), instead." |
| 91 | ) |
| 92 | |
| 93 | # These functions are not required by the spec, but are implemented for |
| 94 | # the sake of usability. |
| 95 | |
| 96 | def __str__(self: Array, /) -> str: |
| 97 | """ |
| 98 | Performs the operation __str__. |
| 99 | """ |
| 100 | return self._array.__str__().replace("array", "Array") |
| 101 | |
| 102 | def __repr__(self: Array, /) -> str: |
no outgoing calls