decent-array 0.2.4__tar.gz → 0.2.6__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.
Files changed (68) hide show
  1. {decent_array-0.2.4 → decent_array-0.2.6}/PKG-INFO +1 -1
  2. {decent_array-0.2.4 → decent_array-0.2.6}/decent_array/_array.py +1 -11
  3. {decent_array-0.2.4 → decent_array-0.2.6}/decent_array/interoperability/__init__.py +6 -0
  4. {decent_array-0.2.4 → decent_array-0.2.6}/decent_array/interoperability/_abstracts/backend.py +33 -7
  5. {decent_array-0.2.4 → decent_array-0.2.6}/decent_array/interoperability/_iop/manipulations.py +9 -2
  6. {decent_array-0.2.4 → decent_array-0.2.6}/decent_array/interoperability/_iop/math.py +21 -0
  7. {decent_array-0.2.4 → decent_array-0.2.6}/decent_array/interoperability/_iop/reductions.py +4 -4
  8. {decent_array-0.2.4 → decent_array-0.2.6}/decent_array/interoperability/_jax/jax_backend.py +20 -7
  9. {decent_array-0.2.4 → decent_array-0.2.6}/decent_array/interoperability/_numpy/numpy_backend.py +22 -9
  10. {decent_array-0.2.4 → decent_array-0.2.6}/decent_array/interoperability/_pytorch/pytorch_backend.py +20 -7
  11. {decent_array-0.2.4 → decent_array-0.2.6}/decent_array/interoperability/_tensorflow/tensorflow_backend.py +22 -9
  12. {decent_array-0.2.4 → decent_array-0.2.6}/decent_array/types/_types.py +11 -1
  13. {decent_array-0.2.4 → decent_array-0.2.6}/docs/source/conf.py +1 -0
  14. {decent_array-0.2.4 → decent_array-0.2.6}/pyproject.toml +1 -1
  15. {decent_array-0.2.4 → decent_array-0.2.6}/tests/test_array.py +27 -20
  16. {decent_array-0.2.4 → decent_array-0.2.6}/tests/test_iop_functions.py +92 -4
  17. {decent_array-0.2.4 → decent_array-0.2.6}/.github/CODEOWNERS +0 -0
  18. {decent_array-0.2.4 → decent_array-0.2.6}/.github/dependabot.yml +0 -0
  19. {decent_array-0.2.4 → decent_array-0.2.6}/.github/workflows/ci.yaml +0 -0
  20. {decent_array-0.2.4 → decent_array-0.2.6}/.gitignore +0 -0
  21. {decent_array-0.2.4 → decent_array-0.2.6}/LICENSE +0 -0
  22. {decent_array-0.2.4 → decent_array-0.2.6}/README.md +0 -0
  23. {decent_array-0.2.4 → decent_array-0.2.6}/benchmarks/bench_array.py +0 -0
  24. {decent_array-0.2.4 → decent_array-0.2.6}/benchmarks/bench_common.py +0 -0
  25. {decent_array-0.2.4 → decent_array-0.2.6}/benchmarks/bench_iop.py +0 -0
  26. {decent_array-0.2.4 → decent_array-0.2.6}/benchmarks/profile_hotpath.py +0 -0
  27. {decent_array-0.2.4 → decent_array-0.2.6}/decent_array/__init__.py +0 -0
  28. {decent_array-0.2.4 → decent_array-0.2.6}/decent_array/_constants.py +0 -0
  29. {decent_array-0.2.4 → decent_array-0.2.6}/decent_array/_errors.py +0 -0
  30. {decent_array-0.2.4 → decent_array-0.2.6}/decent_array/_utils.py +0 -0
  31. {decent_array-0.2.4 → decent_array-0.2.6}/decent_array/interoperability/_abstracts/__init__.py +0 -0
  32. {decent_array-0.2.4 → decent_array-0.2.6}/decent_array/interoperability/_backend_manager.py +0 -0
  33. {decent_array-0.2.4 → decent_array-0.2.6}/decent_array/interoperability/_iop/__init__.py +0 -0
  34. {decent_array-0.2.4 → decent_array-0.2.6}/decent_array/interoperability/_iop/bit_operators.py +0 -0
  35. {decent_array-0.2.4 → decent_array-0.2.6}/decent_array/interoperability/_iop/comparison.py +0 -0
  36. {decent_array-0.2.4 → decent_array-0.2.6}/decent_array/interoperability/_iop/creation.py +0 -0
  37. {decent_array-0.2.4 → decent_array-0.2.6}/decent_array/interoperability/_iop/linalg.py +0 -0
  38. {decent_array-0.2.4 → decent_array-0.2.6}/decent_array/interoperability/_iop/operators.py +0 -0
  39. {decent_array-0.2.4 → decent_array-0.2.6}/decent_array/interoperability/_iop/rng.py +0 -0
  40. {decent_array-0.2.4 → decent_array-0.2.6}/decent_array/interoperability/_iop/utils.py +0 -0
  41. {decent_array-0.2.4 → decent_array-0.2.6}/decent_array/interoperability/_jax/__init__.py +0 -0
  42. {decent_array-0.2.4 → decent_array-0.2.6}/decent_array/interoperability/_numpy/__init__.py +0 -0
  43. {decent_array-0.2.4 → decent_array-0.2.6}/decent_array/interoperability/_pytorch/__init__.py +0 -0
  44. {decent_array-0.2.4 → decent_array-0.2.6}/decent_array/interoperability/_tensorflow/__init__.py +0 -0
  45. {decent_array-0.2.4 → decent_array-0.2.6}/decent_array/py.typed +0 -0
  46. {decent_array-0.2.4 → decent_array-0.2.6}/decent_array/types/__init__.py +0 -0
  47. {decent_array-0.2.4 → decent_array-0.2.6}/decent_array/types/_dtypes.py +0 -0
  48. {decent_array-0.2.4 → decent_array-0.2.6}/docs/Makefile +0 -0
  49. {decent_array-0.2.4 → decent_array-0.2.6}/docs/make.bat +0 -0
  50. {decent_array-0.2.4 → decent_array-0.2.6}/docs/requirements.txt +0 -0
  51. {decent_array-0.2.4 → decent_array-0.2.6}/docs/source/_static/custom.css +0 -0
  52. {decent_array-0.2.4 → decent_array-0.2.6}/docs/source/_static/logo.png +0 -0
  53. {decent_array-0.2.4 → decent_array-0.2.6}/docs/source/_templates/module.rst.jinja +0 -0
  54. {decent_array-0.2.4 → decent_array-0.2.6}/docs/source/_templates/package.rst.jinja +0 -0
  55. {decent_array-0.2.4 → decent_array-0.2.6}/docs/source/api/decent_array.array.rst +0 -0
  56. {decent_array-0.2.4 → decent_array-0.2.6}/docs/source/api/decent_array.interoperability.rst +0 -0
  57. {decent_array-0.2.4 → decent_array-0.2.6}/docs/source/api/decent_array.rst +0 -0
  58. {decent_array-0.2.4 → decent_array-0.2.6}/docs/source/api/decent_array.types.rst +0 -0
  59. {decent_array-0.2.4 → decent_array-0.2.6}/docs/source/author.rst +0 -0
  60. {decent_array-0.2.4 → decent_array-0.2.6}/docs/source/developer.rst +0 -0
  61. {decent_array-0.2.4 → decent_array-0.2.6}/docs/source/index.rst +0 -0
  62. {decent_array-0.2.4 → decent_array-0.2.6}/docs/source/user.rst +0 -0
  63. {decent_array-0.2.4 → decent_array-0.2.6}/readthedocs.yaml +0 -0
  64. {decent_array-0.2.4 → decent_array-0.2.6}/tests/conftest.py +0 -0
  65. {decent_array-0.2.4 → decent_array-0.2.6}/tests/test_backend_manager.py +0 -0
  66. {decent_array-0.2.4 → decent_array-0.2.6}/tests/test_bindings.py +0 -0
  67. {decent_array-0.2.4 → decent_array-0.2.6}/tests/test_dtypes.py +0 -0
  68. {decent_array-0.2.4 → decent_array-0.2.6}/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.4
3
+ Version: 0.2.6
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."""
@@ -50,6 +50,9 @@ from ._iop.math import (
50
50
  add,
51
51
  divide,
52
52
  floor_divide,
53
+ isfinite,
54
+ isinf,
55
+ isnan,
53
56
  multiply,
54
57
  negative,
55
58
  positive,
@@ -111,6 +114,9 @@ __all__ = [
111
114
  "get_seed",
112
115
  "greater",
113
116
  "greater_equal",
117
+ "isfinite",
118
+ "isinf",
119
+ "isnan",
114
120
  "less",
115
121
  "less_equal",
116
122
  "matmul",
@@ -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
 
@@ -88,9 +88,23 @@ class Backend(ABC):
88
88
  def from_numpy_like(self, x: NDArray[Any], like: Array) -> Array:
89
89
  """Convert a Numpy array to an :class:`Array` on this backend, matching shape and type of ``like``."""
90
90
 
91
+ def asarray(self, x: ArrayTypes | Array) -> Array:
92
+ """
93
+ Convert `x` into an :class:`~decent_array.Array` on the active backend.
94
+
95
+ `x` can be a scalar, a backend-native array/tensor, or :class:`~decent_array.Array`, in which case it acts as
96
+ the identity.
97
+
98
+ """
99
+ from decent_array._array import Array # noqa: PLC0415
100
+
101
+ if isinstance(x, Array):
102
+ return x
103
+ return Array(self.native_asarray(x))
104
+
91
105
  @abstractmethod
92
- def asarray(self, x: bool | int | float | complex) -> Array:
93
- """Convert a Python scalar to an :class:`Array` on this backend."""
106
+ def native_asarray(self, x: ArrayTypes) -> ArrayLike:
107
+ """Wrap the backend-native asarray operation."""
94
108
 
95
109
  @abstractmethod
96
110
  def to_scalar(self, x: Array) -> Any: # noqa: ANN401
@@ -191,12 +205,12 @@ class Backend(ABC):
191
205
  """Maximum of ``x`` along ``axis``."""
192
206
 
193
207
  @abstractmethod
194
- def any(self, x: Array, axis: int | tuple[int, ...] | None = None, keepdims: bool = False) -> bool:
195
- """Return True if any element of ``x`` is truthy."""
208
+ def any(self, x: Array, axis: int | tuple[int, ...] | None = None, keepdims: bool = False) -> Array:
209
+ """Test whether any array element along axis is truthy."""
196
210
 
197
211
  @abstractmethod
198
- def all(self, x: Array, axis: int | tuple[int, ...] | None = None, keepdims: bool = False) -> bool:
199
- """Return True if all elements of ``x`` are truthy."""
212
+ def all(self, x: Array, axis: int | tuple[int, ...] | None = None, keepdims: bool = False) -> Array:
213
+ """Test whether all array elements along axis are truthy."""
200
214
 
201
215
  # Math elementwise — both operands may be Array or scalar (operator dunders pass
202
216
  # either). ``Array | float`` covers both because PEP 484's numeric tower implicitly
@@ -270,6 +284,18 @@ class Backend(ABC):
270
284
  def sqrt(self, x: Array) -> Array:
271
285
  """Element-wise square root."""
272
286
 
287
+ @abstractmethod
288
+ def isfinite(self, x: Array) -> Array:
289
+ """Element-wise test for finite values."""
290
+
291
+ @abstractmethod
292
+ def isinf(self, x: Array) -> Array:
293
+ """Element-wise test for infinite values."""
294
+
295
+ @abstractmethod
296
+ def isnan(self, x: Array) -> Array:
297
+ """Element-wise test for NaN values."""
298
+
273
299
  # Comparisons — both operands may be Array or scalar.
274
300
 
275
301
  @abstractmethod
@@ -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: float | bool) -> Array:
70
- """Convert a Python scalar to an :class:`~decent_array.Array` on the active backend."""
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)
@@ -147,3 +147,24 @@ def sqrt(x: Array) -> Array:
147
147
  if _BACKEND_INSTANCE is None:
148
148
  raise no_backend_error
149
149
  return _BACKEND_INSTANCE.sqrt(x)
150
+
151
+
152
+ def isfinite(x: Array) -> Array:
153
+ """Element-wise test for finite values."""
154
+ if _BACKEND_INSTANCE is None:
155
+ raise no_backend_error
156
+ return _BACKEND_INSTANCE.isfinite(x)
157
+
158
+
159
+ def isinf(x: Array) -> Array:
160
+ """Element-wise test for infinite values."""
161
+ if _BACKEND_INSTANCE is None:
162
+ raise no_backend_error
163
+ return _BACKEND_INSTANCE.isinf(x)
164
+
165
+
166
+ def isnan(x: Array) -> Array:
167
+ """Element-wise test for NaN values."""
168
+ if _BACKEND_INSTANCE is None:
169
+ raise no_backend_error
170
+ return _BACKEND_INSTANCE.isnan(x)
@@ -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) -> bool: # noqa: A001
81
- """Return True if any element of ``x`` is truthy."""
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) -> bool: # noqa: A001
88
- """Return True if all elements of ``x`` are truthy."""
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
 
@@ -91,8 +91,9 @@ class JaxBackend(Backend):
91
91
  v = like.value
92
92
  return Array(jnp.asarray(x, dtype=v.dtype, device=v.device))
93
93
 
94
- def asarray(self, x: bool | int | float | complex) -> Array:
95
- return Array(jnp.array(x, device=self._native_device))
94
+ def native_asarray(self, x: ArrayTypes) -> ArrayLike:
95
+ """Wrap the backend-native asarray operation."""
96
+ return jnp.asarray(x, device=self._native_device)
96
97
 
97
98
  def to_scalar(self, x: Array) -> Any: # noqa: ANN401
98
99
  """
@@ -187,11 +188,11 @@ class JaxBackend(Backend):
187
188
  def max(self, x: Array, axis: int | tuple[int, ...] | None = None, keepdims: bool = False) -> Array:
188
189
  return Array(jnp.max(x.value, axis=axis, keepdims=keepdims))
189
190
 
190
- def any(self, x: Array, axis: int | tuple[int, ...] | None = None, keepdims: bool = False) -> bool:
191
- return bool(jnp.any(x.value, axis=axis, keepdims=keepdims))
191
+ def any(self, x: Array, axis: int | tuple[int, ...] | None = None, keepdims: bool = False) -> Array:
192
+ return Array(jnp.any(x.value, axis=axis, keepdims=keepdims))
192
193
 
193
- def all(self, x: Array, axis: int | tuple[int, ...] | None = None, keepdims: bool = False) -> bool:
194
- return bool(jnp.all(x.value, axis=axis, keepdims=keepdims))
194
+ def all(self, x: Array, axis: int | tuple[int, ...] | None = None, keepdims: bool = False) -> Array:
195
+ return Array(jnp.all(x.value, axis=axis, keepdims=keepdims))
195
196
 
196
197
  # Math elementwise — JAX arrays are immutable; "in-place" ops rebind the wrapper.
197
198
  # Operands may be Array or scalar (operator dunders pass either); ``Array | float``
@@ -255,6 +256,18 @@ class JaxBackend(Backend):
255
256
  def sqrt(self, x: Array) -> Array:
256
257
  return Array(jnp.sqrt(x.value))
257
258
 
259
+ def isfinite(self, x: Array) -> Array:
260
+ """Element-wise test for finite values."""
261
+ return Array(jnp.isfinite(x.value))
262
+
263
+ def isinf(self, x: Array) -> Array:
264
+ """Element-wise test for infinite values."""
265
+ return Array(jnp.isinf(x.value))
266
+
267
+ def isnan(self, x: Array) -> Array:
268
+ """Element-wise test for NaN values."""
269
+ return Array(jnp.isnan(x.value))
270
+
258
271
  # Comparisons
259
272
 
260
273
  def equal(self, x1: int | float | complex | Array, x2: int | float | complex | Array) -> Array:
@@ -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
 
@@ -84,8 +84,9 @@ class NumpyBackend(Backend):
84
84
  # NumPy has no device dimension, so only the dtype of ``like`` matters.
85
85
  return Array(np.asarray(x, dtype=like.value.dtype))
86
86
 
87
- def asarray(self, x: bool | int | float | complex) -> Array:
88
- return Array(np.array(x))
87
+ def native_asarray(self, x: ArrayTypes) -> ArrayLike:
88
+ """Wrap the backend-native asarray operation."""
89
+ return np.asarray(x)
89
90
 
90
91
  def to_scalar(self, x: Array) -> Any: # noqa: ANN401
91
92
  """
@@ -194,17 +195,17 @@ class NumpyBackend(Backend):
194
195
  return Array(np.max(v, axis=axis, keepdims=True))
195
196
  return Array(np.max(v, axis=axis))
196
197
 
197
- def any(self, x: Array, axis: int | tuple[int, ...] | None = None, keepdims: bool = False) -> bool:
198
+ def any(self, x: Array, axis: int | tuple[int, ...] | None = None, keepdims: bool = False) -> Array:
198
199
  v = cast("np.ndarray[Any, Any]", x.value)
199
200
  if keepdims:
200
- return bool(np.any(v, axis=axis, keepdims=True))
201
- return bool(np.any(v, axis=axis))
201
+ return Array(np.any(v, axis=axis, keepdims=True))
202
+ return Array(np.any(v, axis=axis, keepdims=False))
202
203
 
203
- def all(self, x: Array, axis: int | tuple[int, ...] | None = None, keepdims: bool = False) -> bool:
204
+ def all(self, x: Array, axis: int | tuple[int, ...] | None = None, keepdims: bool = False) -> Array:
204
205
  v = cast("np.ndarray[Any, Any]", x.value)
205
206
  if keepdims:
206
- return bool(np.all(v, axis=axis, keepdims=True))
207
- return bool(np.all(v, axis=axis))
207
+ return Array(np.all(v, axis=axis, keepdims=True))
208
+ return Array(np.all(v, axis=axis, keepdims=False))
208
209
 
209
210
  # Math elementwise — operands may be Array or scalar (operator dunders pass either).
210
211
  # ``Array | float`` covers both: PEP 484's numeric tower implicitly admits ``int``.
@@ -267,6 +268,18 @@ class NumpyBackend(Backend):
267
268
  def sqrt(self, x: Array) -> Array:
268
269
  return Array(np.sqrt(x.value))
269
270
 
271
+ def isfinite(self, x: Array) -> Array:
272
+ """Element-wise test for finite values."""
273
+ return Array(np.isfinite(x.value))
274
+
275
+ def isinf(self, x: Array) -> Array:
276
+ """Element-wise test for infinite values."""
277
+ return Array(np.isinf(x.value))
278
+
279
+ def isnan(self, x: Array) -> Array:
280
+ """Element-wise test for NaN values."""
281
+ return Array(np.isnan(x.value))
282
+
270
283
  # Comparisons
271
284
 
272
285
  def equal(self, x1: int | float | complex | Array, x2: int | float | complex | Array) -> Array:
@@ -19,7 +19,7 @@ from decent_array._errors import MatrixTransposeError, NDimError, UnsupportedDTy
19
19
  from decent_array._utils import is_scalar, unwrap
20
20
  from decent_array.interoperability._abstracts import Backend
21
21
  from decent_array.interoperability._backend_manager import register_backend
22
- from decent_array.types import ArrayKey, ArrayTypes, Devices, Frameworks
22
+ from decent_array.types import ArrayKey, ArrayLike, ArrayTypes, Devices, Frameworks
23
23
  from decent_array.types._dtypes import dtype
24
24
 
25
25
 
@@ -88,8 +88,9 @@ class PyTorchBackend(Backend):
88
88
  v = like.value
89
89
  return Array(torch.from_numpy(x).to(dtype=v.dtype, device=v.device))
90
90
 
91
- def asarray(self, x: bool | int | float | complex) -> Array:
92
- return Array(torch.tensor(x, device=self._native_device))
91
+ def native_asarray(self, x: ArrayTypes) -> ArrayLike:
92
+ """Wrap the backend-native asarray operation."""
93
+ return torch.as_tensor(x, device=self._native_device)
93
94
 
94
95
  def to_scalar(self, x: Array) -> Any: # noqa: ANN401
95
96
  """
@@ -201,11 +202,11 @@ class PyTorchBackend(Backend):
201
202
  return Array(torch.max(v))
202
203
  return Array(torch.amax(v, dim=axis, keepdim=keepdims))
203
204
 
204
- def any(self, x: Array, axis: int | tuple[int, ...] | None = None, keepdims: bool = False) -> bool:
205
- return bool(torch.any(x.value, dim=axis, keepdim=keepdims).item())
205
+ def any(self, x: Array, axis: int | tuple[int, ...] | None = None, keepdims: bool = False) -> Array:
206
+ return Array(torch.any(x.value, dim=axis, keepdim=keepdims))
206
207
 
207
- def all(self, x: Array, axis: int | tuple[int, ...] | None = None, keepdims: bool = False) -> bool:
208
- return bool(torch.all(x.value, dim=axis, keepdim=keepdims).item())
208
+ def all(self, x: Array, axis: int | tuple[int, ...] | None = None, keepdims: bool = False) -> Array:
209
+ return Array(torch.all(x.value, dim=axis, keepdim=keepdims))
209
210
 
210
211
  # Math elementwise — operands may be Array or scalar (operator dunders pass either).
211
212
  # ``Array | float`` covers both: PEP 484's numeric tower implicitly admits ``int``.
@@ -268,6 +269,18 @@ class PyTorchBackend(Backend):
268
269
  def sqrt(self, x: Array) -> Array:
269
270
  return Array(torch.sqrt(x.value))
270
271
 
272
+ def isfinite(self, x: Array) -> Array:
273
+ """Element-wise test for finite values."""
274
+ return Array(torch.isfinite(x.value))
275
+
276
+ def isinf(self, x: Array) -> Array:
277
+ """Element-wise test for infinite values."""
278
+ return Array(torch.isinf(x.value))
279
+
280
+ def isnan(self, x: Array) -> Array:
281
+ """Element-wise test for NaN values."""
282
+ return Array(torch.isnan(x.value))
283
+
271
284
  # Comparisons
272
285
 
273
286
  def equal(self, x1: int | float | complex | Array, x2: int | float | complex | Array) -> Array:
@@ -28,7 +28,7 @@ from decent_array._errors import (
28
28
  from decent_array._utils import is_scalar, unwrap
29
29
  from decent_array.interoperability._abstracts import Backend
30
30
  from decent_array.interoperability._backend_manager import register_backend
31
- from decent_array.types import ArrayKey, ArrayTypes, Devices, Frameworks
31
+ from decent_array.types import ArrayKey, ArrayLike, ArrayTypes, Devices, Frameworks
32
32
  from decent_array.types._dtypes import dtype
33
33
 
34
34
 
@@ -98,11 +98,12 @@ class TensorflowBackend(Backend):
98
98
  with tf.device(v.device):
99
99
  return Array(tf.convert_to_tensor(x, dtype=v.dtype))
100
100
 
101
- def asarray(self, x: bool | int | float | complex) -> Array:
102
- """Convert a Python scalar to an :class:`Array` on this backend."""
101
+ def native_asarray(self, x: ArrayTypes) -> ArrayLike:
102
+ """Wrap the backend-native asarray operation."""
103
+ # TensorFlow's type stubs do not cover all inputs accepted by
104
+ # convert_to_tensor at runtime.
103
105
  with tf.device(self._native_device):
104
- # Its not a tf tensor but mypyc doesn't import tf so it complains about unsude type-ignores
105
- return Array(tf.convert_to_tensor(cast("tf.Tensor", x)))
106
+ return tf.convert_to_tensor(cast("Any", x))
106
107
 
107
108
  def to_scalar(self, x: Array) -> Any: # noqa: ANN401
108
109
  """
@@ -210,11 +211,11 @@ class TensorflowBackend(Backend):
210
211
  def max(self, x: Array, axis: int | tuple[int, ...] | None = None, keepdims: bool = False) -> Array:
211
212
  return Array(tf.reduce_max(x.value, axis=axis, keepdims=keepdims))
212
213
 
213
- def any(self, x: Array, axis: int | tuple[int, ...] | None = None, keepdims: bool = False) -> bool:
214
- return bool(tf.reduce_any(tf.cast(x.value, tf.bool), axis=axis, keepdims=keepdims).numpy())
214
+ def any(self, x: Array, axis: int | tuple[int, ...] | None = None, keepdims: bool = False) -> Array:
215
+ return Array(tf.reduce_any(tf.cast(x.value, tf.bool), axis=axis, keepdims=keepdims))
215
216
 
216
- def all(self, x: Array, axis: int | tuple[int, ...] | None = None, keepdims: bool = False) -> bool:
217
- return bool(tf.reduce_all(tf.cast(x.value, tf.bool), axis=axis, keepdims=keepdims).numpy())
217
+ def all(self, x: Array, axis: int | tuple[int, ...] | None = None, keepdims: bool = False) -> Array:
218
+ return Array(tf.reduce_all(tf.cast(x.value, tf.bool), axis=axis, keepdims=keepdims))
218
219
 
219
220
  # Math elementwise — TF Tensors are immutable; "in-place" ops rebind the wrapper.
220
221
  # Operands may be Array or scalar (operator dunders pass either); ``Array | float``
@@ -278,6 +279,18 @@ class TensorflowBackend(Backend):
278
279
  def sqrt(self, x: Array) -> Array:
279
280
  return Array(tf.sqrt(x.value))
280
281
 
282
+ def isfinite(self, x: Array) -> Array:
283
+ """Element-wise test for finite values."""
284
+ return Array(tf.math.is_finite(x.value))
285
+
286
+ def isinf(self, x: Array) -> Array:
287
+ """Element-wise test for infinite values."""
288
+ return Array(tf.math.is_inf(x.value))
289
+
290
+ def isnan(self, x: Array) -> Array:
291
+ """Element-wise test for NaN values."""
292
+ return Array(tf.math.is_nan(x.value))
293
+
281
294
  # Comparisons
282
295
 
283
296
  def equal(self, x1: int | float | complex | Array, x2: int | float | complex | Array) -> Array:
@@ -2,6 +2,7 @@
2
2
 
3
3
  from __future__ import annotations
4
4
 
5
+ from collections.abc import Buffer, Sequence
5
6
  from enum import Enum
6
7
  from typing import TYPE_CHECKING, SupportsIndex, TypeAlias, Union
7
8
 
@@ -14,6 +15,10 @@ if TYPE_CHECKING:
14
15
 
15
16
  from decent_array._array import Array
16
17
 
18
+ Scalar: TypeAlias = bool | int | float | complex | numpy.generic # noqa: UP040
19
+ """
20
+ Type alias for scalar types supported in decent-array.
21
+ """
17
22
 
18
23
  ArrayLike: TypeAlias = Union["numpy.ndarray", "torch.Tensor", "tf.Tensor", "jax.Array"] # noqa: UP040
19
24
  """
@@ -21,7 +26,12 @@ Type alias for array-like types supported in decent-array, including NumPy array
21
26
  PyTorch tensors, TensorFlow tensors, and JAX arrays.
22
27
  """
23
28
 
24
- ArrayTypes: TypeAlias = bool | int | float | complex | numpy.generic | ArrayLike # noqa: UP040
29
+ NestedSequence: TypeAlias = "Sequence[Scalar | NestedSequence]" # noqa: UP040
30
+ """
31
+ Type alias for nested sequences supported in decent-array.
32
+ """
33
+
34
+ ArrayTypes: TypeAlias = Scalar | ArrayLike | NestedSequence | Buffer # noqa: UP040
25
35
  """
26
36
  Type alias for supported scalar/array types in decent-array.
27
37
  """
@@ -38,6 +38,7 @@ nitpick_ignore = [
38
38
  ("py:class", "float64"),
39
39
  ("py:class", "numpy._typing._array_like._SupportsArray"),
40
40
  ("py:class", "numpy._typing._nested_sequence._NestedSequence"),
41
+ ("py:class", "Sequence[Scalar | NestedSequence]"),
41
42
  ("py:class", "T"),
42
43
  ]
43
44
 
@@ -1,6 +1,6 @@
1
1
  [project]
2
2
  name = "decent-array"
3
- version = "0.2.4"
3
+ version = "0.2.6"
4
4
  authors = [{name = "Simon Granström"}, {name = "Nicola Bastianello"}]
5
5
  maintainers = [{name = "Team Decent"}]
6
6
  description = "A library of array operations and linear algebra primitives for interoperability across ML frameworks."
@@ -37,6 +37,33 @@ def test_init_records_active_backend(backend: tuple) -> None:
37
37
  assert isinstance(arr, Array)
38
38
 
39
39
 
40
+ @pytest.mark.parametrize(
41
+ "value",
42
+ [
43
+ True,
44
+ 1,
45
+ 1.0,
46
+ 1.0 + 2.0j,
47
+ np.int32(1),
48
+ np.float32(1.0),
49
+ np.complex64(1.0 + 2.0j),
50
+ ],
51
+ )
52
+ def test_init_converts_to_backend_array(value, backend: tuple) -> None:
53
+ arr = Array(value)
54
+
55
+ assert isinstance(arr, Array)
56
+ assert arr.ndim == 0
57
+ assert arr.size == 1
58
+ assert arr.shape == ()
59
+
60
+
61
+ def test_init_scalar_value(backend: tuple) -> None:
62
+ arr = Array(4)
63
+
64
+ np.testing.assert_array_equal(iop.to_numpy(arr), np.array(4))
65
+
66
+
40
67
  # Binary arithmetic -------------------------------------------------------
41
68
 
42
69
 
@@ -528,26 +555,6 @@ def test_mT_raises_for_rank_lt_2(backend: tuple) -> None:
528
555
  _ = a.mT
529
556
 
530
557
 
531
- def test_any_true(backend: tuple) -> None:
532
- a = _create_array([0.0, 0.0, 1.0])
533
- assert a.any is True
534
-
535
-
536
- def test_any_false(backend: tuple) -> None:
537
- a = _create_array([0.0, 0.0, 0.0])
538
- assert a.any is False
539
-
540
-
541
- def test_all_true(backend: tuple) -> None:
542
- a = _create_array([1.0, 2.0, 3.0])
543
- assert a.all is True
544
-
545
-
546
- def test_all_false(backend: tuple) -> None:
547
- a = _create_array([1.0, 0.0, 3.0])
548
- assert a.all is False
549
-
550
-
551
558
  def test_device_property(backend: tuple) -> None:
552
559
  _framework, device = backend
553
560
  a = iop.zeros((3,))
@@ -62,6 +62,59 @@ def test_device_of(backend: tuple) -> None:
62
62
 
63
63
  # Array manipulation -----------------------------------------------------
64
64
 
65
+ def test_asarray_existing_array_is_identity(backend: tuple) -> None:
66
+ arr = Array(4)
67
+
68
+ result = iop.asarray(arr)
69
+
70
+ assert result is arr
71
+
72
+
73
+ def test_asarray_scalar(backend: tuple) -> None:
74
+ result = iop.asarray(4)
75
+
76
+ assert isinstance(result, Array)
77
+ assert result.shape == ()
78
+ assert result.size == 1
79
+ assert result.ndim == 0
80
+
81
+
82
+ def test_asarray_native_array(backend: tuple) -> None:
83
+ arr = iop.from_numpy(np.array([1.0, 2.0, 3.0], dtype=np.float32))
84
+
85
+ result = iop.asarray(arr.value)
86
+
87
+ assert isinstance(result, Array)
88
+ np.testing.assert_array_equal(_np(result), _np(arr))
89
+
90
+
91
+ def test_asarray_numpy_array(backend: tuple) -> None:
92
+ raw = np.array([1.0, 2.0, 3.0], dtype=np.float32)
93
+
94
+ result = iop.asarray(raw)
95
+
96
+ assert isinstance(result, Array)
97
+ np.testing.assert_array_equal(_np(result), raw)
98
+
99
+
100
+ def test_asarray_nested_sequence(backend: tuple) -> None:
101
+ raw = [[1, 2, 3], [4, 5, 6]]
102
+
103
+ result = iop.asarray(raw)
104
+
105
+ assert isinstance(result, Array)
106
+ np.testing.assert_array_equal(_np(result), raw)
107
+
108
+
109
+ def test_asarray_buffer(backend: tuple) -> None:
110
+ raw = np.array([1, 2, 3], dtype=np.int32)
111
+ buffer = memoryview(raw)
112
+
113
+ result = iop.asarray(buffer)
114
+
115
+ assert isinstance(result, Array)
116
+ np.testing.assert_array_equal(_np(result), raw)
117
+
65
118
 
66
119
  def test_copy_independent(backend: tuple) -> None:
67
120
  src = iop.from_numpy(np.array([1.0, 2.0, 3.0], dtype=np.float32))
@@ -331,22 +384,42 @@ def test_max_dim(backend: tuple) -> None:
331
384
 
332
385
  def test_any_true(backend: tuple) -> None:
333
386
  arr = iop.from_numpy(np.array([0.0, 1.0, 0.0], dtype=np.float32))
334
- assert iop.any(arr) is True
387
+ np.testing.assert_array_equal(_np(iop.any(arr)), True)
335
388
 
336
389
 
337
390
  def test_any_false(backend: tuple) -> None:
338
391
  arr = iop.from_numpy(np.array([0.0, 0.0, 0.0], dtype=np.float32))
339
- assert iop.any(arr) is False
392
+ np.testing.assert_array_equal(_np(iop.any(arr)), False)
340
393
 
341
394
 
342
395
  def test_all_true(backend: tuple) -> None:
343
396
  arr = iop.from_numpy(np.array([1.0, 2.0, 3.0], dtype=np.float32))
344
- assert iop.all(arr) is True
397
+ np.testing.assert_array_equal(_np(iop.all(arr)), True)
345
398
 
346
399
 
347
400
  def test_all_false(backend: tuple) -> None:
348
401
  arr = iop.from_numpy(np.array([1.0, 0.0, 3.0], dtype=np.float32))
349
- assert iop.all(arr) is False
402
+ np.testing.assert_array_equal(_np(iop.all(arr)), False)
403
+
404
+
405
+ def test_any_dim(backend: tuple) -> None:
406
+ arr = iop.from_numpy(np.array([[0.0, 1.0], [0.0, 0.0]], dtype=np.float32))
407
+ np.testing.assert_array_equal(_np(iop.any(arr, axis=0)), [False, True])
408
+
409
+
410
+ def test_all_dim(backend: tuple) -> None:
411
+ arr = iop.from_numpy(np.array([[1.0, 1.0], [1.0, 0.0]], dtype=np.float32))
412
+ np.testing.assert_array_equal(_np(iop.all(arr, axis=0)), [True, False])
413
+
414
+
415
+ def test_any_dim_keepdims(backend: tuple) -> None:
416
+ arr = iop.from_numpy(np.array([[0.0, 1.0], [0.0, 0.0]], dtype=np.float32))
417
+ np.testing.assert_array_equal(_np(iop.any(arr, axis=0, keepdims=True)), [[False, True]])
418
+
419
+
420
+ def test_all_dim_keepdims(backend: tuple) -> None:
421
+ arr = iop.from_numpy(np.array([[1.0, 1.0], [1.0, 0.0]], dtype=np.float32))
422
+ np.testing.assert_array_equal(_np(iop.all(arr, axis=0, keepdims=True)), [[True, False]])
350
423
 
351
424
 
352
425
  # Math elementwise -------------------------------------------------------
@@ -478,6 +551,21 @@ def test_sqrt(backend: tuple) -> None:
478
551
  np.testing.assert_allclose(_np(iop.sqrt(arr)), [1.0, 2.0, 3.0])
479
552
 
480
553
 
554
+ def test_isfinite(backend: tuple) -> None:
555
+ arr = iop.from_numpy(np.array([1.0, np.inf, -np.inf, np.nan], dtype=np.float32))
556
+ np.testing.assert_array_equal(_np(iop.isfinite(arr)), [True, False, False, False])
557
+
558
+
559
+ def test_isinf(backend: tuple) -> None:
560
+ arr = iop.from_numpy(np.array([1.0, np.inf, -np.inf, np.nan], dtype=np.float32))
561
+ np.testing.assert_array_equal(_np(iop.isinf(arr)), [False, True, True, False])
562
+
563
+
564
+ def test_isnan(backend: tuple) -> None:
565
+ arr = iop.from_numpy(np.array([1.0, np.inf, -np.inf, np.nan], dtype=np.float32))
566
+ np.testing.assert_array_equal(_np(iop.isnan(arr)), [False, False, False, True])
567
+
568
+
481
569
  # Operators --------------------------------------------------------------
482
570
 
483
571
 
File without changes
File without changes
File without changes
File without changes
File without changes