decent-array 0.2.5__tar.gz → 0.2.7__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {decent_array-0.2.5 → decent_array-0.2.7}/PKG-INFO +1 -1
- {decent_array-0.2.5 → decent_array-0.2.7}/decent_array/_array.py +1 -11
- {decent_array-0.2.5 → decent_array-0.2.7}/decent_array/interoperability/__init__.py +14 -2
- {decent_array-0.2.5 → decent_array-0.2.7}/decent_array/interoperability/_abstracts/backend.py +72 -9
- {decent_array-0.2.5 → decent_array-0.2.7}/decent_array/interoperability/_iop/creation.py +38 -3
- {decent_array-0.2.5 → decent_array-0.2.7}/decent_array/interoperability/_iop/linalg.py +14 -0
- {decent_array-0.2.5 → decent_array-0.2.7}/decent_array/interoperability/_iop/manipulations.py +9 -2
- {decent_array-0.2.5 → decent_array-0.2.7}/decent_array/interoperability/_iop/math.py +25 -0
- {decent_array-0.2.5 → decent_array-0.2.7}/decent_array/interoperability/_iop/reductions.py +4 -4
- {decent_array-0.2.5 → decent_array-0.2.7}/decent_array/interoperability/_jax/jax_backend.py +50 -9
- {decent_array-0.2.5 → decent_array-0.2.7}/decent_array/interoperability/_numpy/numpy_backend.py +52 -11
- {decent_array-0.2.5 → decent_array-0.2.7}/decent_array/interoperability/_pytorch/pytorch_backend.py +72 -9
- {decent_array-0.2.5 → decent_array-0.2.7}/decent_array/interoperability/_tensorflow/tensorflow_backend.py +59 -11
- {decent_array-0.2.5 → decent_array-0.2.7}/decent_array/types/_types.py +11 -1
- {decent_array-0.2.5 → decent_array-0.2.7}/docs/source/conf.py +1 -0
- {decent_array-0.2.5 → decent_array-0.2.7}/pyproject.toml +1 -1
- {decent_array-0.2.5 → decent_array-0.2.7}/tests/test_array.py +27 -20
- {decent_array-0.2.5 → decent_array-0.2.7}/tests/test_iop_functions.py +177 -4
- {decent_array-0.2.5 → decent_array-0.2.7}/.github/CODEOWNERS +0 -0
- {decent_array-0.2.5 → decent_array-0.2.7}/.github/dependabot.yml +0 -0
- {decent_array-0.2.5 → decent_array-0.2.7}/.github/workflows/ci.yaml +0 -0
- {decent_array-0.2.5 → decent_array-0.2.7}/.gitignore +0 -0
- {decent_array-0.2.5 → decent_array-0.2.7}/LICENSE +0 -0
- {decent_array-0.2.5 → decent_array-0.2.7}/README.md +0 -0
- {decent_array-0.2.5 → decent_array-0.2.7}/benchmarks/bench_array.py +0 -0
- {decent_array-0.2.5 → decent_array-0.2.7}/benchmarks/bench_common.py +0 -0
- {decent_array-0.2.5 → decent_array-0.2.7}/benchmarks/bench_iop.py +0 -0
- {decent_array-0.2.5 → decent_array-0.2.7}/benchmarks/profile_hotpath.py +0 -0
- {decent_array-0.2.5 → decent_array-0.2.7}/decent_array/__init__.py +0 -0
- {decent_array-0.2.5 → decent_array-0.2.7}/decent_array/_constants.py +0 -0
- {decent_array-0.2.5 → decent_array-0.2.7}/decent_array/_errors.py +0 -0
- {decent_array-0.2.5 → decent_array-0.2.7}/decent_array/_utils.py +0 -0
- {decent_array-0.2.5 → decent_array-0.2.7}/decent_array/interoperability/_abstracts/__init__.py +0 -0
- {decent_array-0.2.5 → decent_array-0.2.7}/decent_array/interoperability/_backend_manager.py +0 -0
- {decent_array-0.2.5 → decent_array-0.2.7}/decent_array/interoperability/_iop/__init__.py +0 -0
- {decent_array-0.2.5 → decent_array-0.2.7}/decent_array/interoperability/_iop/bit_operators.py +0 -0
- {decent_array-0.2.5 → decent_array-0.2.7}/decent_array/interoperability/_iop/comparison.py +0 -0
- {decent_array-0.2.5 → decent_array-0.2.7}/decent_array/interoperability/_iop/operators.py +0 -0
- {decent_array-0.2.5 → decent_array-0.2.7}/decent_array/interoperability/_iop/rng.py +0 -0
- {decent_array-0.2.5 → decent_array-0.2.7}/decent_array/interoperability/_iop/utils.py +0 -0
- {decent_array-0.2.5 → decent_array-0.2.7}/decent_array/interoperability/_jax/__init__.py +0 -0
- {decent_array-0.2.5 → decent_array-0.2.7}/decent_array/interoperability/_numpy/__init__.py +0 -0
- {decent_array-0.2.5 → decent_array-0.2.7}/decent_array/interoperability/_pytorch/__init__.py +0 -0
- {decent_array-0.2.5 → decent_array-0.2.7}/decent_array/interoperability/_tensorflow/__init__.py +0 -0
- {decent_array-0.2.5 → decent_array-0.2.7}/decent_array/py.typed +0 -0
- {decent_array-0.2.5 → decent_array-0.2.7}/decent_array/types/__init__.py +0 -0
- {decent_array-0.2.5 → decent_array-0.2.7}/decent_array/types/_dtypes.py +0 -0
- {decent_array-0.2.5 → decent_array-0.2.7}/docs/Makefile +0 -0
- {decent_array-0.2.5 → decent_array-0.2.7}/docs/make.bat +0 -0
- {decent_array-0.2.5 → decent_array-0.2.7}/docs/requirements.txt +0 -0
- {decent_array-0.2.5 → decent_array-0.2.7}/docs/source/_static/custom.css +0 -0
- {decent_array-0.2.5 → decent_array-0.2.7}/docs/source/_static/logo.png +0 -0
- {decent_array-0.2.5 → decent_array-0.2.7}/docs/source/_templates/module.rst.jinja +0 -0
- {decent_array-0.2.5 → decent_array-0.2.7}/docs/source/_templates/package.rst.jinja +0 -0
- {decent_array-0.2.5 → decent_array-0.2.7}/docs/source/api/decent_array.array.rst +0 -0
- {decent_array-0.2.5 → decent_array-0.2.7}/docs/source/api/decent_array.interoperability.rst +0 -0
- {decent_array-0.2.5 → decent_array-0.2.7}/docs/source/api/decent_array.rst +0 -0
- {decent_array-0.2.5 → decent_array-0.2.7}/docs/source/api/decent_array.types.rst +0 -0
- {decent_array-0.2.5 → decent_array-0.2.7}/docs/source/author.rst +0 -0
- {decent_array-0.2.5 → decent_array-0.2.7}/docs/source/developer.rst +0 -0
- {decent_array-0.2.5 → decent_array-0.2.7}/docs/source/index.rst +0 -0
- {decent_array-0.2.5 → decent_array-0.2.7}/docs/source/user.rst +0 -0
- {decent_array-0.2.5 → decent_array-0.2.7}/readthedocs.yaml +0 -0
- {decent_array-0.2.5 → decent_array-0.2.7}/tests/conftest.py +0 -0
- {decent_array-0.2.5 → decent_array-0.2.7}/tests/test_backend_manager.py +0 -0
- {decent_array-0.2.5 → decent_array-0.2.7}/tests/test_bindings.py +0 -0
- {decent_array-0.2.5 → decent_array-0.2.7}/tests/test_dtypes.py +0 -0
- {decent_array-0.2.5 → decent_array-0.2.7}/tests/test_iop_rng.py +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: decent-array
|
|
3
|
-
Version: 0.2.
|
|
3
|
+
Version: 0.2.7
|
|
4
4
|
Summary: A library of array operations and linear algebra primitives for interoperability across ML frameworks.
|
|
5
5
|
Project-URL: Documentation, https://decent-array.readthedocs.io/en/latest/
|
|
6
6
|
Project-URL: Source, https://github.com/team-decent/decent-array
|
|
@@ -89,7 +89,7 @@ class Array:
|
|
|
89
89
|
"Call set_backend() to initialize the interoperability layer."
|
|
90
90
|
)
|
|
91
91
|
|
|
92
|
-
self.value: Any = value
|
|
92
|
+
self.value: Any = _BACKEND_INSTANCE.native_asarray(value)
|
|
93
93
|
self._backend: Backend = _BACKEND_INSTANCE
|
|
94
94
|
|
|
95
95
|
# Binary arithmetic ----------------------------------------------------
|
|
@@ -445,16 +445,6 @@ class Array:
|
|
|
445
445
|
"""Return the matrix transpose (last two dimensions swapped)."""
|
|
446
446
|
return self._backend.matrix_transpose(self)
|
|
447
447
|
|
|
448
|
-
@property
|
|
449
|
-
def any(self) -> bool:
|
|
450
|
-
"""Return True if any element of the array is truthy."""
|
|
451
|
-
return self._backend.any(self)
|
|
452
|
-
|
|
453
|
-
@property
|
|
454
|
-
def all(self) -> bool:
|
|
455
|
-
"""Return True if all elements of the array are truthy."""
|
|
456
|
-
return self._backend.all(self)
|
|
457
|
-
|
|
458
448
|
@property
|
|
459
449
|
def device(self) -> Devices:
|
|
460
450
|
"""Return the device of the array."""
|
|
@@ -22,8 +22,8 @@ from ._iop.bit_operators import (
|
|
|
22
22
|
bitwise_xor,
|
|
23
23
|
)
|
|
24
24
|
from ._iop.comparison import equal, greater, greater_equal, less, less_equal, not_equal
|
|
25
|
-
from ._iop.creation import eye, ones, ones_like, zeros, zeros_like
|
|
26
|
-
from ._iop.linalg import dot, matmul, norm, vecdot, vector_norm
|
|
25
|
+
from ._iop.creation import arange, empty, empty_like, eye, linspace, ones, ones_like, zeros, zeros_like
|
|
26
|
+
from ._iop.linalg import dot, eigvalsh, matmul, norm, solve, vecdot, vector_norm
|
|
27
27
|
from ._iop.manipulations import (
|
|
28
28
|
asarray,
|
|
29
29
|
astype,
|
|
@@ -49,10 +49,12 @@ from ._iop.math import (
|
|
|
49
49
|
absolute,
|
|
50
50
|
add,
|
|
51
51
|
divide,
|
|
52
|
+
exp,
|
|
52
53
|
floor_divide,
|
|
53
54
|
isfinite,
|
|
54
55
|
isinf,
|
|
55
56
|
isnan,
|
|
57
|
+
logaddexp,
|
|
56
58
|
multiply,
|
|
57
59
|
negative,
|
|
58
60
|
positive,
|
|
@@ -60,6 +62,7 @@ from ._iop.math import (
|
|
|
60
62
|
remainder,
|
|
61
63
|
sqrt,
|
|
62
64
|
subtract,
|
|
65
|
+
where,
|
|
63
66
|
)
|
|
64
67
|
from ._iop.operators import argmax, argmin, maximum, sign
|
|
65
68
|
from ._iop.reductions import all, any, max, mean, min, sum # noqa: A004
|
|
@@ -84,6 +87,7 @@ __all__ = [
|
|
|
84
87
|
"add",
|
|
85
88
|
"all",
|
|
86
89
|
"any",
|
|
90
|
+
"arange",
|
|
87
91
|
"argmax",
|
|
88
92
|
"argmin",
|
|
89
93
|
"asarray",
|
|
@@ -103,7 +107,11 @@ __all__ = [
|
|
|
103
107
|
"diagonal",
|
|
104
108
|
"divide",
|
|
105
109
|
"dot",
|
|
110
|
+
"eigvalsh",
|
|
111
|
+
"empty",
|
|
112
|
+
"empty_like",
|
|
106
113
|
"equal",
|
|
114
|
+
"exp",
|
|
107
115
|
"expand_dims",
|
|
108
116
|
"eye",
|
|
109
117
|
"floor_divide",
|
|
@@ -119,6 +127,8 @@ __all__ = [
|
|
|
119
127
|
"isnan",
|
|
120
128
|
"less",
|
|
121
129
|
"less_equal",
|
|
130
|
+
"linspace",
|
|
131
|
+
"logaddexp",
|
|
122
132
|
"matmul",
|
|
123
133
|
"matrix_transpose",
|
|
124
134
|
"max",
|
|
@@ -144,6 +154,7 @@ __all__ = [
|
|
|
144
154
|
"shape",
|
|
145
155
|
"sign",
|
|
146
156
|
"size",
|
|
157
|
+
"solve",
|
|
147
158
|
"sqrt",
|
|
148
159
|
"squeeze",
|
|
149
160
|
"stack",
|
|
@@ -156,6 +167,7 @@ __all__ = [
|
|
|
156
167
|
"unsqueeze",
|
|
157
168
|
"vecdot",
|
|
158
169
|
"vector_norm",
|
|
170
|
+
"where",
|
|
159
171
|
"zeros",
|
|
160
172
|
"zeros_like",
|
|
161
173
|
]
|
{decent_array-0.2.5 → decent_array-0.2.7}/decent_array/interoperability/_abstracts/backend.py
RENAMED
|
@@ -24,7 +24,7 @@ if TYPE_CHECKING:
|
|
|
24
24
|
from numpy.typing import NDArray
|
|
25
25
|
|
|
26
26
|
from decent_array import Array
|
|
27
|
-
from decent_array.types import ArrayKey, ArrayTypes
|
|
27
|
+
from decent_array.types import ArrayKey, ArrayLike, ArrayTypes
|
|
28
28
|
from decent_array.types._dtypes import dtype
|
|
29
29
|
|
|
30
30
|
|
|
@@ -42,6 +42,14 @@ class Backend(ABC):
|
|
|
42
42
|
|
|
43
43
|
# Array creation ------------------------------------------------------
|
|
44
44
|
|
|
45
|
+
@abstractmethod
|
|
46
|
+
def empty(self, shape: int | tuple[int, ...]) -> Array:
|
|
47
|
+
"""Create an uninitialized array with the given shape."""
|
|
48
|
+
|
|
49
|
+
@abstractmethod
|
|
50
|
+
def empty_like(self, x: Array, /) -> Array:
|
|
51
|
+
"""Create an uninitialized array with the same shape as ``x``."""
|
|
52
|
+
|
|
45
53
|
@abstractmethod
|
|
46
54
|
def zeros(self, shape: int | tuple[int, ...]) -> Array:
|
|
47
55
|
"""Create an array of zeros with the given shape."""
|
|
@@ -59,8 +67,24 @@ class Backend(ABC):
|
|
|
59
67
|
"""Create an array of ones matching the shape and type of ``x``."""
|
|
60
68
|
|
|
61
69
|
@abstractmethod
|
|
62
|
-
def eye(self,
|
|
63
|
-
"""Create
|
|
70
|
+
def eye(self, n_rows: int, n_cols: int | None = None, /, *, k: int = 0) -> Array:
|
|
71
|
+
"""Create a two-dimensional array with ones on the diagonal at offset ``k``."""
|
|
72
|
+
|
|
73
|
+
@abstractmethod
|
|
74
|
+
def arange(self, start: int | float, /, stop: int | float | None = None, step: int | float = 1) -> Array:
|
|
75
|
+
"""Return evenly spaced values within a half-open interval."""
|
|
76
|
+
|
|
77
|
+
@abstractmethod
|
|
78
|
+
def linspace(
|
|
79
|
+
self,
|
|
80
|
+
start: int | float | complex,
|
|
81
|
+
stop: int | float | complex,
|
|
82
|
+
/,
|
|
83
|
+
num: int,
|
|
84
|
+
*,
|
|
85
|
+
endpoint: bool = True,
|
|
86
|
+
) -> Array:
|
|
87
|
+
"""Return evenly spaced numbers over a specified interval."""
|
|
64
88
|
|
|
65
89
|
@abstractmethod
|
|
66
90
|
def device_to_native(self, device: Devices) -> Any: # noqa: ANN401
|
|
@@ -88,9 +112,23 @@ class Backend(ABC):
|
|
|
88
112
|
def from_numpy_like(self, x: NDArray[Any], like: Array) -> Array:
|
|
89
113
|
"""Convert a Numpy array to an :class:`Array` on this backend, matching shape and type of ``like``."""
|
|
90
114
|
|
|
115
|
+
def asarray(self, x: ArrayTypes | Array) -> Array:
|
|
116
|
+
"""
|
|
117
|
+
Convert `x` into an :class:`~decent_array.Array` on the active backend.
|
|
118
|
+
|
|
119
|
+
`x` can be a scalar, a backend-native array/tensor, or :class:`~decent_array.Array`, in which case it acts as
|
|
120
|
+
the identity.
|
|
121
|
+
|
|
122
|
+
"""
|
|
123
|
+
from decent_array._array import Array # noqa: PLC0415
|
|
124
|
+
|
|
125
|
+
if isinstance(x, Array):
|
|
126
|
+
return x
|
|
127
|
+
return Array(self.native_asarray(x))
|
|
128
|
+
|
|
91
129
|
@abstractmethod
|
|
92
|
-
def
|
|
93
|
-
"""
|
|
130
|
+
def native_asarray(self, x: ArrayTypes) -> ArrayLike:
|
|
131
|
+
"""Wrap the backend-native asarray operation."""
|
|
94
132
|
|
|
95
133
|
@abstractmethod
|
|
96
134
|
def to_scalar(self, x: Array) -> Any: # noqa: ANN401
|
|
@@ -172,6 +210,14 @@ class Backend(ABC):
|
|
|
172
210
|
) -> Array:
|
|
173
211
|
"""Compute the norm of ``x``."""
|
|
174
212
|
|
|
213
|
+
@abstractmethod
|
|
214
|
+
def solve(self, x1: Array, x2: Array) -> Array:
|
|
215
|
+
"""Solve the linear system ``x1 @ solution = x2``."""
|
|
216
|
+
|
|
217
|
+
@abstractmethod
|
|
218
|
+
def eigvalsh(self, x: Array) -> Array:
|
|
219
|
+
"""Return eigenvalues of a symmetric/Hermitian matrix."""
|
|
220
|
+
|
|
175
221
|
# Math reductions -----------------------------------------------------
|
|
176
222
|
|
|
177
223
|
@abstractmethod
|
|
@@ -191,12 +237,12 @@ class Backend(ABC):
|
|
|
191
237
|
"""Maximum of ``x`` along ``axis``."""
|
|
192
238
|
|
|
193
239
|
@abstractmethod
|
|
194
|
-
def any(self, x: Array, axis: int | tuple[int, ...] | None = None, keepdims: bool = False) ->
|
|
195
|
-
"""
|
|
240
|
+
def any(self, x: Array, axis: int | tuple[int, ...] | None = None, keepdims: bool = False) -> Array:
|
|
241
|
+
"""Test whether any array element along axis is truthy."""
|
|
196
242
|
|
|
197
243
|
@abstractmethod
|
|
198
|
-
def all(self, x: Array, axis: int | tuple[int, ...] | None = None, keepdims: bool = False) ->
|
|
199
|
-
"""
|
|
244
|
+
def all(self, x: Array, axis: int | tuple[int, ...] | None = None, keepdims: bool = False) -> Array:
|
|
245
|
+
"""Test whether all array elements along axis are truthy."""
|
|
200
246
|
|
|
201
247
|
# Math elementwise — both operands may be Array or scalar (operator dunders pass
|
|
202
248
|
# either). ``Array | float`` covers both because PEP 484's numeric tower implicitly
|
|
@@ -270,6 +316,23 @@ class Backend(ABC):
|
|
|
270
316
|
def sqrt(self, x: Array) -> Array:
|
|
271
317
|
"""Element-wise square root."""
|
|
272
318
|
|
|
319
|
+
@abstractmethod
|
|
320
|
+
def exp(self, x: Array) -> Array:
|
|
321
|
+
"""Element-wise exponential."""
|
|
322
|
+
|
|
323
|
+
@abstractmethod
|
|
324
|
+
def logaddexp(self, x1: int | float | Array, x2: int | float | Array) -> Array:
|
|
325
|
+
"""Element-wise log(exp(x1) + exp(x2)) computed stably."""
|
|
326
|
+
|
|
327
|
+
@abstractmethod
|
|
328
|
+
def where(
|
|
329
|
+
self,
|
|
330
|
+
condition: Array,
|
|
331
|
+
x1: bool | int | float | complex | Array,
|
|
332
|
+
x2: bool | int | float | complex | Array,
|
|
333
|
+
) -> Array:
|
|
334
|
+
"""Select elements from ``x1`` or ``x2`` according to ``condition``."""
|
|
335
|
+
|
|
273
336
|
@abstractmethod
|
|
274
337
|
def isfinite(self, x: Array) -> Array:
|
|
275
338
|
"""Element-wise test for finite values."""
|
|
@@ -61,8 +61,43 @@ def ones_like(x: Array) -> Array:
|
|
|
61
61
|
return _BACKEND_INSTANCE.ones_like(x)
|
|
62
62
|
|
|
63
63
|
|
|
64
|
-
def
|
|
65
|
-
"""Create an
|
|
64
|
+
def empty(shape: int | tuple[int, ...]) -> Array:
|
|
65
|
+
"""Create an uninitialized array with the given shape."""
|
|
66
66
|
if _BACKEND_INSTANCE is None:
|
|
67
67
|
raise no_backend_error
|
|
68
|
-
return _BACKEND_INSTANCE.
|
|
68
|
+
return _BACKEND_INSTANCE.empty(shape)
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+
def empty_like(x: Array, /) -> Array:
|
|
72
|
+
"""Create an uninitialized array with the same shape as ``x``."""
|
|
73
|
+
if _BACKEND_INSTANCE is None:
|
|
74
|
+
raise no_backend_error
|
|
75
|
+
return _BACKEND_INSTANCE.empty_like(x)
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
def eye(n_rows: int, n_cols: int | None = None, /, *, k: int = 0) -> Array:
|
|
79
|
+
"""Create a two-dimensional array with ones on the diagonal at offset ``k``."""
|
|
80
|
+
if _BACKEND_INSTANCE is None:
|
|
81
|
+
raise no_backend_error
|
|
82
|
+
return _BACKEND_INSTANCE.eye(n_rows, n_cols, k=k)
|
|
83
|
+
|
|
84
|
+
|
|
85
|
+
def arange(start: int | float, /, stop: int | float | None = None, step: int | float = 1) -> Array:
|
|
86
|
+
"""Return evenly spaced values within a half-open interval."""
|
|
87
|
+
if _BACKEND_INSTANCE is None:
|
|
88
|
+
raise no_backend_error
|
|
89
|
+
return _BACKEND_INSTANCE.arange(start, stop, step)
|
|
90
|
+
|
|
91
|
+
|
|
92
|
+
def linspace(
|
|
93
|
+
start: int | float | complex,
|
|
94
|
+
stop: int | float | complex,
|
|
95
|
+
/,
|
|
96
|
+
num: int,
|
|
97
|
+
*,
|
|
98
|
+
endpoint: bool = True,
|
|
99
|
+
) -> Array:
|
|
100
|
+
"""Return evenly spaced numbers over a specified interval."""
|
|
101
|
+
if _BACKEND_INSTANCE is None:
|
|
102
|
+
raise no_backend_error
|
|
103
|
+
return _BACKEND_INSTANCE.linspace(start, stop, num, endpoint=endpoint)
|
|
@@ -84,3 +84,17 @@ def norm(
|
|
|
84
84
|
if _BACKEND_INSTANCE is None:
|
|
85
85
|
raise no_backend_error
|
|
86
86
|
return _BACKEND_INSTANCE.vector_norm(x, axis, keepdims, ord)
|
|
87
|
+
|
|
88
|
+
|
|
89
|
+
def solve(x1: Array, x2: Array) -> Array:
|
|
90
|
+
"""Solve the linear system ``x1 @ solution = x2``."""
|
|
91
|
+
if _BACKEND_INSTANCE is None:
|
|
92
|
+
raise no_backend_error
|
|
93
|
+
return _BACKEND_INSTANCE.solve(x1, x2)
|
|
94
|
+
|
|
95
|
+
|
|
96
|
+
def eigvalsh(x: Array) -> Array:
|
|
97
|
+
"""Return eigenvalues of a symmetric/Hermitian matrix."""
|
|
98
|
+
if _BACKEND_INSTANCE is None:
|
|
99
|
+
raise no_backend_error
|
|
100
|
+
return _BACKEND_INSTANCE.eigvalsh(x)
|
{decent_array-0.2.5 → decent_array-0.2.7}/decent_array/interoperability/_iop/manipulations.py
RENAMED
|
@@ -66,8 +66,15 @@ def from_numpy_like(x: NDArray[Any], like: Array) -> Array:
|
|
|
66
66
|
return _BACKEND_INSTANCE.from_numpy_like(x, like)
|
|
67
67
|
|
|
68
68
|
|
|
69
|
-
def asarray(x:
|
|
70
|
-
"""
|
|
69
|
+
def asarray(x: ArrayTypes | Array) -> Array:
|
|
70
|
+
"""
|
|
71
|
+
Convert `x` into an :class:`~decent_array.Array` on the active backend.
|
|
72
|
+
|
|
73
|
+
`x` can be a scalar, a native array, a nested sequence, an object supporting Python's buffer protocol. Note that `x`
|
|
74
|
+
is passed directly to the equivalent backend-native asarray function. If `x` is an :class:`~decent_array.Array`
|
|
75
|
+
instance, it is returned unchanged.
|
|
76
|
+
|
|
77
|
+
"""
|
|
71
78
|
if _BACKEND_INSTANCE is None:
|
|
72
79
|
raise no_backend_error
|
|
73
80
|
return _BACKEND_INSTANCE.asarray(x)
|
|
@@ -149,6 +149,31 @@ def sqrt(x: Array) -> Array:
|
|
|
149
149
|
return _BACKEND_INSTANCE.sqrt(x)
|
|
150
150
|
|
|
151
151
|
|
|
152
|
+
def exp(x: Array) -> Array:
|
|
153
|
+
"""Element-wise exponential."""
|
|
154
|
+
if _BACKEND_INSTANCE is None:
|
|
155
|
+
raise no_backend_error
|
|
156
|
+
return _BACKEND_INSTANCE.exp(x)
|
|
157
|
+
|
|
158
|
+
|
|
159
|
+
def logaddexp(x1: int | float | Array, x2: int | float | Array) -> Array:
|
|
160
|
+
"""Element-wise log(exp(x1) + exp(x2)) computed stably."""
|
|
161
|
+
if _BACKEND_INSTANCE is None:
|
|
162
|
+
raise no_backend_error
|
|
163
|
+
return _BACKEND_INSTANCE.logaddexp(x1, x2)
|
|
164
|
+
|
|
165
|
+
|
|
166
|
+
def where(
|
|
167
|
+
condition: Array,
|
|
168
|
+
x1: bool | int | float | complex | Array,
|
|
169
|
+
x2: bool | int | float | complex | Array,
|
|
170
|
+
) -> Array:
|
|
171
|
+
"""Select elements from ``x1`` or ``x2`` according to ``condition``."""
|
|
172
|
+
if _BACKEND_INSTANCE is None:
|
|
173
|
+
raise no_backend_error
|
|
174
|
+
return _BACKEND_INSTANCE.where(condition, x1, x2)
|
|
175
|
+
|
|
176
|
+
|
|
152
177
|
def isfinite(x: Array) -> Array:
|
|
153
178
|
"""Element-wise test for finite values."""
|
|
154
179
|
if _BACKEND_INSTANCE is None:
|
|
@@ -77,15 +77,15 @@ def max( # noqa: A001
|
|
|
77
77
|
return _BACKEND_INSTANCE.max(x, axis, keepdims)
|
|
78
78
|
|
|
79
79
|
|
|
80
|
-
def any(x: Array, axis: int | tuple[int, ...] | None = None, keepdims: bool = False) ->
|
|
81
|
-
"""
|
|
80
|
+
def any(x: Array, axis: int | tuple[int, ...] | None = None, keepdims: bool = False) -> Array: # noqa: A001
|
|
81
|
+
"""Test whether any array element along axis is truthy."""
|
|
82
82
|
if _BACKEND_INSTANCE is None:
|
|
83
83
|
raise no_backend_error
|
|
84
84
|
return _BACKEND_INSTANCE.any(x, axis, keepdims)
|
|
85
85
|
|
|
86
86
|
|
|
87
|
-
def all(x: Array, axis: int | tuple[int, ...] | None = None, keepdims: bool = False) ->
|
|
88
|
-
"""
|
|
87
|
+
def all(x: Array, axis: int | tuple[int, ...] | None = None, keepdims: bool = False) -> Array: # noqa: A001
|
|
88
|
+
"""Test whether all array elements along axis are truthy."""
|
|
89
89
|
if _BACKEND_INSTANCE is None:
|
|
90
90
|
raise no_backend_error
|
|
91
91
|
return _BACKEND_INSTANCE.all(x, axis, keepdims)
|
|
@@ -30,7 +30,7 @@ from decent_array._errors import (
|
|
|
30
30
|
from decent_array._utils import is_scalar, unwrap
|
|
31
31
|
from decent_array.interoperability._abstracts import Backend
|
|
32
32
|
from decent_array.interoperability._backend_manager import register_backend
|
|
33
|
-
from decent_array.types import ArrayKey, ArrayTypes, Devices, Frameworks
|
|
33
|
+
from decent_array.types import ArrayKey, ArrayLike, ArrayTypes, Devices, Frameworks
|
|
34
34
|
from decent_array.types._dtypes import dtype
|
|
35
35
|
|
|
36
36
|
|
|
@@ -46,6 +46,12 @@ class JaxBackend(Backend):
|
|
|
46
46
|
|
|
47
47
|
# Array creation
|
|
48
48
|
|
|
49
|
+
def empty(self, shape: int | tuple[int, ...]) -> Array:
|
|
50
|
+
return Array(jnp.empty(shape, device=self._native_device))
|
|
51
|
+
|
|
52
|
+
def empty_like(self, x: Array, /) -> Array:
|
|
53
|
+
return Array(jnp.empty_like(x.value, device=self._native_device))
|
|
54
|
+
|
|
49
55
|
def zeros(self, shape: int | tuple[int, ...]) -> Array:
|
|
50
56
|
return Array(jnp.zeros(shape, device=self._native_device))
|
|
51
57
|
|
|
@@ -58,8 +64,22 @@ class JaxBackend(Backend):
|
|
|
58
64
|
def ones_like(self, x: Array) -> Array:
|
|
59
65
|
return Array(jnp.ones_like(x.value))
|
|
60
66
|
|
|
61
|
-
def eye(self,
|
|
62
|
-
return Array(jnp.eye(
|
|
67
|
+
def eye(self, n_rows: int, n_cols: int | None = None, /, *, k: int = 0) -> Array:
|
|
68
|
+
return Array(jnp.eye(n_rows, n_cols, k=k, device=self._native_device))
|
|
69
|
+
|
|
70
|
+
def arange(self, start: int | float, /, stop: int | float | None = None, step: int | float = 1) -> Array:
|
|
71
|
+
return Array(jnp.arange(start, stop, step, device=self._native_device))
|
|
72
|
+
|
|
73
|
+
def linspace(
|
|
74
|
+
self,
|
|
75
|
+
start: int | float | complex,
|
|
76
|
+
stop: int | float | complex,
|
|
77
|
+
/,
|
|
78
|
+
num: int,
|
|
79
|
+
*,
|
|
80
|
+
endpoint: bool = True,
|
|
81
|
+
) -> Array:
|
|
82
|
+
return Array(jnp.linspace(start, stop, num, endpoint=endpoint, device=self._native_device))
|
|
63
83
|
|
|
64
84
|
def device_to_native(self, device: Devices) -> jax.Device:
|
|
65
85
|
if device == Devices.CPU:
|
|
@@ -91,8 +111,9 @@ class JaxBackend(Backend):
|
|
|
91
111
|
v = like.value
|
|
92
112
|
return Array(jnp.asarray(x, dtype=v.dtype, device=v.device))
|
|
93
113
|
|
|
94
|
-
def
|
|
95
|
-
|
|
114
|
+
def native_asarray(self, x: ArrayTypes) -> ArrayLike:
|
|
115
|
+
"""Wrap the backend-native asarray operation."""
|
|
116
|
+
return jnp.asarray(x, device=self._native_device)
|
|
96
117
|
|
|
97
118
|
def to_scalar(self, x: Array) -> Any: # noqa: ANN401
|
|
98
119
|
"""
|
|
@@ -173,6 +194,12 @@ class JaxBackend(Backend):
|
|
|
173
194
|
) -> Array:
|
|
174
195
|
return Array(jnp.linalg.norm(x.value, ord=ord, axis=axis, keepdims=keepdims))
|
|
175
196
|
|
|
197
|
+
def solve(self, x1: Array, x2: Array) -> Array:
|
|
198
|
+
return Array(jnp.linalg.solve(x1.value, x2.value))
|
|
199
|
+
|
|
200
|
+
def eigvalsh(self, x: Array) -> Array:
|
|
201
|
+
return Array(jnp.linalg.eigvalsh(x.value))
|
|
202
|
+
|
|
176
203
|
# Math reductions
|
|
177
204
|
|
|
178
205
|
def sum(self, x: Array, axis: int | tuple[int, ...] | None = None, keepdims: bool = False) -> Array:
|
|
@@ -187,11 +214,11 @@ class JaxBackend(Backend):
|
|
|
187
214
|
def max(self, x: Array, axis: int | tuple[int, ...] | None = None, keepdims: bool = False) -> Array:
|
|
188
215
|
return Array(jnp.max(x.value, axis=axis, keepdims=keepdims))
|
|
189
216
|
|
|
190
|
-
def any(self, x: Array, axis: int | tuple[int, ...] | None = None, keepdims: bool = False) ->
|
|
191
|
-
return
|
|
217
|
+
def any(self, x: Array, axis: int | tuple[int, ...] | None = None, keepdims: bool = False) -> Array:
|
|
218
|
+
return Array(jnp.any(x.value, axis=axis, keepdims=keepdims))
|
|
192
219
|
|
|
193
|
-
def all(self, x: Array, axis: int | tuple[int, ...] | None = None, keepdims: bool = False) ->
|
|
194
|
-
return
|
|
220
|
+
def all(self, x: Array, axis: int | tuple[int, ...] | None = None, keepdims: bool = False) -> Array:
|
|
221
|
+
return Array(jnp.all(x.value, axis=axis, keepdims=keepdims))
|
|
195
222
|
|
|
196
223
|
# Math elementwise — JAX arrays are immutable; "in-place" ops rebind the wrapper.
|
|
197
224
|
# Operands may be Array or scalar (operator dunders pass either); ``Array | float``
|
|
@@ -255,6 +282,20 @@ class JaxBackend(Backend):
|
|
|
255
282
|
def sqrt(self, x: Array) -> Array:
|
|
256
283
|
return Array(jnp.sqrt(x.value))
|
|
257
284
|
|
|
285
|
+
def exp(self, x: Array) -> Array:
|
|
286
|
+
return Array(jnp.exp(x.value))
|
|
287
|
+
|
|
288
|
+
def logaddexp(self, x1: int | float | Array, x2: int | float | Array) -> Array:
|
|
289
|
+
return Array(jnp.logaddexp(unwrap(x1), unwrap(x2)))
|
|
290
|
+
|
|
291
|
+
def where(
|
|
292
|
+
self,
|
|
293
|
+
condition: Array,
|
|
294
|
+
x1: bool | int | float | complex | Array,
|
|
295
|
+
x2: bool | int | float | complex | Array,
|
|
296
|
+
) -> Array:
|
|
297
|
+
return Array(jnp.where(condition.value, unwrap(x1), unwrap(x2)))
|
|
298
|
+
|
|
258
299
|
def isfinite(self, x: Array) -> Array:
|
|
259
300
|
"""Element-wise test for finite values."""
|
|
260
301
|
return Array(jnp.isfinite(x.value))
|
{decent_array-0.2.5 → decent_array-0.2.7}/decent_array/interoperability/_numpy/numpy_backend.py
RENAMED
|
@@ -25,7 +25,7 @@ from decent_array._errors import (
|
|
|
25
25
|
from decent_array._utils import is_scalar, unwrap
|
|
26
26
|
from decent_array.interoperability._abstracts import Backend
|
|
27
27
|
from decent_array.interoperability._backend_manager import register_backend
|
|
28
|
-
from decent_array.types import ArrayKey, ArrayTypes, Devices, Frameworks
|
|
28
|
+
from decent_array.types import ArrayKey, ArrayLike, ArrayTypes, Devices, Frameworks
|
|
29
29
|
from decent_array.types._dtypes import dtype
|
|
30
30
|
|
|
31
31
|
|
|
@@ -40,6 +40,12 @@ class NumpyBackend(Backend):
|
|
|
40
40
|
|
|
41
41
|
# Array creation
|
|
42
42
|
|
|
43
|
+
def empty(self, shape: int | tuple[int, ...]) -> Array:
|
|
44
|
+
return Array(np.empty(shape))
|
|
45
|
+
|
|
46
|
+
def empty_like(self, x: Array, /) -> Array:
|
|
47
|
+
return Array(np.empty_like(x.value))
|
|
48
|
+
|
|
43
49
|
def zeros(self, shape: int | tuple[int, ...]) -> Array:
|
|
44
50
|
return Array(np.zeros(shape))
|
|
45
51
|
|
|
@@ -52,8 +58,22 @@ class NumpyBackend(Backend):
|
|
|
52
58
|
def ones_like(self, x: Array) -> Array:
|
|
53
59
|
return Array(np.ones_like(x.value))
|
|
54
60
|
|
|
55
|
-
def eye(self,
|
|
56
|
-
return Array(np.eye(
|
|
61
|
+
def eye(self, n_rows: int, n_cols: int | None = None, /, *, k: int = 0) -> Array:
|
|
62
|
+
return Array(np.eye(n_rows, n_cols, k=k))
|
|
63
|
+
|
|
64
|
+
def arange(self, start: int | float, /, stop: int | float | None = None, step: int | float = 1) -> Array:
|
|
65
|
+
return Array(np.arange(start, stop, step))
|
|
66
|
+
|
|
67
|
+
def linspace(
|
|
68
|
+
self,
|
|
69
|
+
start: int | float | complex,
|
|
70
|
+
stop: int | float | complex,
|
|
71
|
+
/,
|
|
72
|
+
num: int,
|
|
73
|
+
*,
|
|
74
|
+
endpoint: bool = True,
|
|
75
|
+
) -> Array:
|
|
76
|
+
return Array(np.linspace(start, stop, num, endpoint=endpoint))
|
|
57
77
|
|
|
58
78
|
def device_to_native(self, device: Devices) -> Any: # noqa: ANN401
|
|
59
79
|
# NumPy has no explicit device management; surface the request unchanged.
|
|
@@ -84,8 +104,9 @@ class NumpyBackend(Backend):
|
|
|
84
104
|
# NumPy has no device dimension, so only the dtype of ``like`` matters.
|
|
85
105
|
return Array(np.asarray(x, dtype=like.value.dtype))
|
|
86
106
|
|
|
87
|
-
def
|
|
88
|
-
|
|
107
|
+
def native_asarray(self, x: ArrayTypes) -> ArrayLike:
|
|
108
|
+
"""Wrap the backend-native asarray operation."""
|
|
109
|
+
return np.asarray(x)
|
|
89
110
|
|
|
90
111
|
def to_scalar(self, x: Array) -> Any: # noqa: ANN401
|
|
91
112
|
"""
|
|
@@ -168,6 +189,12 @@ class NumpyBackend(Backend):
|
|
|
168
189
|
# but is still typed as int | tuple[int, int] | None, hence the ignore
|
|
169
190
|
return Array(np.linalg.vector_norm(x.value, ord=ord, axis=axis, keepdims=keepdims)) # type: ignore[arg-type]
|
|
170
191
|
|
|
192
|
+
def solve(self, x1: Array, x2: Array) -> Array:
|
|
193
|
+
return Array(np.linalg.solve(x1.value, x2.value))
|
|
194
|
+
|
|
195
|
+
def eigvalsh(self, x: Array) -> Array:
|
|
196
|
+
return Array(np.linalg.eigvalsh(x.value))
|
|
197
|
+
|
|
171
198
|
# Math reductions
|
|
172
199
|
|
|
173
200
|
def sum(self, x: Array, axis: int | tuple[int, ...] | None = None, keepdims: bool = False) -> Array:
|
|
@@ -194,17 +221,17 @@ class NumpyBackend(Backend):
|
|
|
194
221
|
return Array(np.max(v, axis=axis, keepdims=True))
|
|
195
222
|
return Array(np.max(v, axis=axis))
|
|
196
223
|
|
|
197
|
-
def any(self, x: Array, axis: int | tuple[int, ...] | None = None, keepdims: bool = False) ->
|
|
224
|
+
def any(self, x: Array, axis: int | tuple[int, ...] | None = None, keepdims: bool = False) -> Array:
|
|
198
225
|
v = cast("np.ndarray[Any, Any]", x.value)
|
|
199
226
|
if keepdims:
|
|
200
|
-
return
|
|
201
|
-
return
|
|
227
|
+
return Array(np.any(v, axis=axis, keepdims=True))
|
|
228
|
+
return Array(np.any(v, axis=axis, keepdims=False))
|
|
202
229
|
|
|
203
|
-
def all(self, x: Array, axis: int | tuple[int, ...] | None = None, keepdims: bool = False) ->
|
|
230
|
+
def all(self, x: Array, axis: int | tuple[int, ...] | None = None, keepdims: bool = False) -> Array:
|
|
204
231
|
v = cast("np.ndarray[Any, Any]", x.value)
|
|
205
232
|
if keepdims:
|
|
206
|
-
return
|
|
207
|
-
return
|
|
233
|
+
return Array(np.all(v, axis=axis, keepdims=True))
|
|
234
|
+
return Array(np.all(v, axis=axis, keepdims=False))
|
|
208
235
|
|
|
209
236
|
# Math elementwise — operands may be Array or scalar (operator dunders pass either).
|
|
210
237
|
# ``Array | float`` covers both: PEP 484's numeric tower implicitly admits ``int``.
|
|
@@ -267,6 +294,20 @@ class NumpyBackend(Backend):
|
|
|
267
294
|
def sqrt(self, x: Array) -> Array:
|
|
268
295
|
return Array(np.sqrt(x.value))
|
|
269
296
|
|
|
297
|
+
def exp(self, x: Array) -> Array:
|
|
298
|
+
return Array(np.exp(x.value))
|
|
299
|
+
|
|
300
|
+
def logaddexp(self, x1: int | float | Array, x2: int | float | Array) -> Array:
|
|
301
|
+
return Array(np.logaddexp(unwrap(x1), unwrap(x2)))
|
|
302
|
+
|
|
303
|
+
def where(
|
|
304
|
+
self,
|
|
305
|
+
condition: Array,
|
|
306
|
+
x1: bool | int | float | complex | Array,
|
|
307
|
+
x2: bool | int | float | complex | Array,
|
|
308
|
+
) -> Array:
|
|
309
|
+
return Array(np.where(condition.value, unwrap(x1), unwrap(x2)))
|
|
310
|
+
|
|
270
311
|
def isfinite(self, x: Array) -> Array:
|
|
271
312
|
"""Element-wise test for finite values."""
|
|
272
313
|
return Array(np.isfinite(x.value))
|