cobra-array 0.2.0__tar.gz → 0.2.1__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.
- {cobra_array-0.2.0/src/cobra_array.egg-info → cobra_array-0.2.1}/PKG-INFO +1 -1
- {cobra_array-0.2.0 → cobra_array-0.2.1}/src/cobra_array/__init__.py +1 -1
- {cobra_array-0.2.0 → cobra_array-0.2.1}/src/cobra_array/compat/_array.py +2 -2
- {cobra_array-0.2.0 → cobra_array-0.2.1}/src/cobra_array/compat/_array.pyi +3 -2
- {cobra_array-0.2.0 → cobra_array-0.2.1}/src/cobra_array/compat/_namespace.py +131 -0
- {cobra_array-0.2.0 → cobra_array-0.2.1}/src/cobra_array/compat/_namespace.pyi +25 -2
- {cobra_array-0.2.0 → cobra_array-0.2.1}/src/cobra_array/convert.py +4 -4
- {cobra_array-0.2.0 → cobra_array-0.2.1}/src/cobra_array/types.py +3 -2
- {cobra_array-0.2.0 → cobra_array-0.2.1/src/cobra_array.egg-info}/PKG-INFO +1 -1
- {cobra_array-0.2.0 → cobra_array-0.2.1}/tests/test_compat.py +47 -6
- {cobra_array-0.2.0 → cobra_array-0.2.1}/LICENSE +0 -0
- {cobra_array-0.2.0 → cobra_array-0.2.1}/README.md +0 -0
- {cobra_array-0.2.0 → cobra_array-0.2.1}/pyproject.toml +0 -0
- {cobra_array-0.2.0 → cobra_array-0.2.1}/setup.cfg +0 -0
- {cobra_array-0.2.0 → cobra_array-0.2.1}/src/cobra_array/_core.py +0 -0
- {cobra_array-0.2.0 → cobra_array-0.2.1}/src/cobra_array/_utils.py +0 -0
- {cobra_array-0.2.0 → cobra_array-0.2.1}/src/cobra_array/array_api.py +0 -0
- {cobra_array-0.2.0 → cobra_array-0.2.1}/src/cobra_array/compat/__init__.py +0 -0
- {cobra_array-0.2.0 → cobra_array-0.2.1}/src/cobra_array/compat/_base.py +0 -0
- {cobra_array-0.2.0 → cobra_array-0.2.1}/src/cobra_array/convert.pyi +0 -0
- {cobra_array-0.2.0 → cobra_array-0.2.1}/src/cobra_array/default.py +0 -0
- {cobra_array-0.2.0 → cobra_array-0.2.1}/src/cobra_array/exceptions.py +0 -0
- {cobra_array-0.2.0 → cobra_array-0.2.1}/src/cobra_array.egg-info/SOURCES.txt +0 -0
- {cobra_array-0.2.0 → cobra_array-0.2.1}/src/cobra_array.egg-info/dependency_links.txt +0 -0
- {cobra_array-0.2.0 → cobra_array-0.2.1}/src/cobra_array.egg-info/requires.txt +0 -0
- {cobra_array-0.2.0 → cobra_array-0.2.1}/src/cobra_array.egg-info/top_level.txt +0 -0
- {cobra_array-0.2.0 → cobra_array-0.2.1}/tests/test_backend.py +0 -0
- {cobra_array-0.2.0 → cobra_array-0.2.1}/tests/test_compat_namespace.py +0 -0
- {cobra_array-0.2.0 → cobra_array-0.2.1}/tests/test_convert.py +0 -0
- {cobra_array-0.2.0 → cobra_array-0.2.1}/tests/test_default.py +0 -0
- {cobra_array-0.2.0 → cobra_array-0.2.1}/tests/test_wrap.py +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: cobra-array
|
|
3
|
-
Version: 0.2.
|
|
3
|
+
Version: 0.2.1
|
|
4
4
|
Summary: A backend-agnostic array utility library that unifies array conversion, context control, and cross-library operations across `NumPy`/`PyTorch`-style ecosystems.
|
|
5
5
|
Author-email: Zhen Tian <zhen.tian.cs@gmail.com>
|
|
6
6
|
Project-URL: Homepage, https://github.com/tinchen777/cobra-array.git
|
|
@@ -339,7 +339,7 @@ class CompatArray(Compat):
|
|
|
339
339
|
@property
|
|
340
340
|
def device(self):
|
|
341
341
|
"""
|
|
342
|
-
|
|
342
|
+
Device on which `self` is stored.
|
|
343
343
|
"""
|
|
344
344
|
return api.device(self._arr)
|
|
345
345
|
|
|
@@ -402,7 +402,7 @@ class CompatArray(Compat):
|
|
|
402
402
|
"""Allow implicit NumPy conversion."""
|
|
403
403
|
return self.to_numpy()
|
|
404
404
|
|
|
405
|
-
def __getattr__(self, name
|
|
405
|
+
def __getattr__(self, name):
|
|
406
406
|
attr = self._get_xp_attr(name)
|
|
407
407
|
|
|
408
408
|
if callable(attr) and not isinstance(attr, type):
|
|
@@ -10,7 +10,7 @@ from typing import (Union, List, Tuple, Optional, Any, Sequence, Generic, TypeVa
|
|
|
10
10
|
from ._base import Compat
|
|
11
11
|
from ._namespace import CompatNamespace
|
|
12
12
|
from ..types import (
|
|
13
|
-
T,
|
|
13
|
+
T, dtypeT, DTypeT, deviceT, DeviceT, DType, AnyDevice,
|
|
14
14
|
ArrayLike, ArrayLibraryName,
|
|
15
15
|
ArrayOrAny, ArrayOrScalar, ArrayOrReal, ArrayOrIntLike, ArrayOrInt, ArrayOrbool,
|
|
16
16
|
UniqueAllResult, UniqueCountsResult, UniqueInverseResult
|
|
@@ -64,7 +64,6 @@ class CompatArray(Compat, Generic[TT, DT]):
|
|
|
64
64
|
def to_device(self, device: DeviceT, /, *, stream: Optional[Any] = None) -> CompatArray[TT, DeviceT]: ...
|
|
65
65
|
@overload
|
|
66
66
|
def to_device(self, device: AnyDevice, /, *, stream: Optional[Any] = None) -> CompatArray[TT, AnyDevice]: ...
|
|
67
|
-
|
|
68
67
|
def to_device(self, device: AnyDevice, /, *, stream: Optional[Any] = None) -> CompatArray[Any, AnyDevice]: ...
|
|
69
68
|
|
|
70
69
|
# === Data type functions ===
|
|
@@ -1771,6 +1770,8 @@ class CompatArray(Compat, Generic[TT, DT]):
|
|
|
1771
1770
|
|
|
1772
1771
|
def __array__(self) -> NDArray[TT]: ...
|
|
1773
1772
|
|
|
1773
|
+
def __getattr__(self, name: str) -> Any: ...
|
|
1774
|
+
|
|
1774
1775
|
def __len__(self) -> int: ...
|
|
1775
1776
|
def __abs__(self) -> CompatArray[TT, DT]: ...
|
|
1776
1777
|
def __add__(self, other: ArrayOrScalar, /) -> CompatArray[Any, DT]: ...
|
|
@@ -236,6 +236,137 @@ class CompatNamespace(Compat):
|
|
|
236
236
|
result = self._get_xp_attr("broadcast_arrays")(*[unwrap(arr) for arr in arrays])
|
|
237
237
|
return [CompatArray(arr, xp=self) for arr in result]
|
|
238
238
|
|
|
239
|
+
# === Linear Algebra Extension ===
|
|
240
|
+
def vector_norm(self, x, /, *, axis=None, keepdims=False, ord=2):
|
|
241
|
+
"""
|
|
242
|
+
Computes the vector norm of a vector (or batch of vectors) :param:`x`.
|
|
243
|
+
|
|
244
|
+
Parameters
|
|
245
|
+
----------
|
|
246
|
+
x : ArrayLike[Any]
|
|
247
|
+
The input array. Should have a floating-point data type.
|
|
248
|
+
|
|
249
|
+
axis : Optional[Union[int, Tuple[int, ...]]], default to `None`
|
|
250
|
+
- _int_: :param:`axis` specifies the axis (dimension) along which to compute vector norms;
|
|
251
|
+
- _tuple_: :param:`axis` specifies the axes (dimensions) along which to compute batched vector norms;
|
|
252
|
+
- `None`: The vector norm must be computed over all array values (i.e., equivalent to computing the vector norm of a flattened array).
|
|
253
|
+
|
|
254
|
+
Negative indices must be supported.
|
|
255
|
+
|
|
256
|
+
keepdims : bool, default to `False`
|
|
257
|
+
- `True`: The axes (dimensions) specified by :param:`axis` must be included in the result as singleton dimensions, and, accordingly, the result must be compatible with the input array (see Broadcasting);
|
|
258
|
+
- `False`: The axes (dimensions) specified by :param:`axis` must not be included in the result.
|
|
259
|
+
|
|
260
|
+
ord : Union[int, float, Literal['inf', '-inf']], default to `2`
|
|
261
|
+
Order of the norm.
|
|
262
|
+
The following mathematical norms must be supported:
|
|
263
|
+
- `1`: L1-norm (Manhattan);
|
|
264
|
+
- `2`: L2-norm (Euclidean);
|
|
265
|
+
- `"inf"`: infinity norm;
|
|
266
|
+
- _int_ or _float_ (>=1): p-norm.
|
|
267
|
+
|
|
268
|
+
The following non-mathematical “norms” must be supported:
|
|
269
|
+
- `0`: sum(a != 0);
|
|
270
|
+
- `-1`: 1./sum(1./abs(a));
|
|
271
|
+
- `-2`: 1./sqrt(sum(1./a**2));
|
|
272
|
+
- `"-inf"`: min(abs(a));
|
|
273
|
+
- _int_ or _float_ (<1): sum(abs(a)**ord)**(1./ord).
|
|
274
|
+
|
|
275
|
+
Returns
|
|
276
|
+
-------
|
|
277
|
+
CompatArray
|
|
278
|
+
A :class:`CompatArray` array containing the vector norms.
|
|
279
|
+
- :param:`axis` is `None`: The returned array must be a zero-dimensional array containing a vector norm;
|
|
280
|
+
- :param:`axis` is a scalar value (_int_ or _float_): The returned array must have a rank which is one less than the rank of :param:`x`;
|
|
281
|
+
- :param:`axis` is _tuple_ (`n` elements): The returned array must have a rank which is `n` less than the rank of :param:`x`;
|
|
282
|
+
|
|
283
|
+
- :param:`x` is real-valued data type: The returned array must have a real-valued floating-point data type determined by Type Promotion Rules;
|
|
284
|
+
- :param:`x` is complex-valued data type: The returned array must have a real-valued floating-point data type whose precision matches the precision of :param:`x` (e.g., if :param:`x` is complex128, then the returned array must have a float64 data type).
|
|
285
|
+
"""
|
|
286
|
+
if ord == "inf":
|
|
287
|
+
ord = float("inf")
|
|
288
|
+
elif ord == "-inf":
|
|
289
|
+
ord = float("-inf")
|
|
290
|
+
|
|
291
|
+
result = getattr(self.linalg, "vector_norm")(unwrap(x), axis=axis, keepdims=keepdims, ord=ord)
|
|
292
|
+
return CompatArray(result, xp=self)
|
|
293
|
+
|
|
294
|
+
def matrix_norm(self, x, /, *, keepdims=False, ord="fro"):
|
|
295
|
+
"""
|
|
296
|
+
Computes the matrix norm of a matrix (or a stack of matrices) :param:`x`.
|
|
297
|
+
|
|
298
|
+
Parameters
|
|
299
|
+
----------
|
|
300
|
+
x : ArrayLike[Any]
|
|
301
|
+
Input array having shape (..., `M`, `N`) and whose innermost two dimensions form `MxN` matrices. Should have a floating-point data type.
|
|
302
|
+
|
|
303
|
+
keepdims : bool, default to `False`
|
|
304
|
+
- `True`: The last two axes (dimensions) must be included in the result as singleton dimensions, and, accordingly, the result must be compatible with the input array (see Broadcasting);
|
|
305
|
+
- `False`: The last two axes (dimensions) must not be included in the result.
|
|
306
|
+
|
|
307
|
+
ord : Optional[Union[int, float, Literal['inf', '-inf', 'fro', 'nuc']]], default to `"fro"`
|
|
308
|
+
order of the norm.
|
|
309
|
+
The following mathematical norms must be supported:
|
|
310
|
+
- `"fro"`: Frobenius norm;
|
|
311
|
+
- `"nuc"`: nuclear norm;
|
|
312
|
+
- `1`: max(sum(abs(x), axis=0)). The norm corresponds to the induced matrix norm where `p=1` (i.e., the maximum absolute value column sum);
|
|
313
|
+
- `2`: largest singular value. The norm corresponds to the induced matrix norm where `p=inf` (i.e., the maximum absolute value row sum);
|
|
314
|
+
- `"inf"`: max(sum(abs(x), axis=1)). The norm corresponds to the induced matrix norm where `p=2` (i.e., the largest singular value).
|
|
315
|
+
|
|
316
|
+
The following non-mathematical “norms” must be supported:
|
|
317
|
+
- `-1`: min(sum(abs(x), axis=0));
|
|
318
|
+
- `-2`: smallest singular value;
|
|
319
|
+
- `"-inf"`: min(sum(abs(x), axis=1)).
|
|
320
|
+
|
|
321
|
+
Returns
|
|
322
|
+
-------
|
|
323
|
+
CompatArray
|
|
324
|
+
A :class:`CompatArray` array containing the norms for each `MxN` matrix.
|
|
325
|
+
- :param:`keepdims` is `False`: The returned array must have a rank which is two less than the rank of :param:`x`;
|
|
326
|
+
|
|
327
|
+
- :param:`x` is real-valued data type: The returned array must have a real-valued floating-point data type determined by Type Promotion Rules;
|
|
328
|
+
- :param:`x` is complex-valued data type: The returned array must have a real-valued floating-point data type whose precision matches the precision of :param:`x` (e.g., if :param:`x` is complex128, then the returned array must have a float64 data type).
|
|
329
|
+
|
|
330
|
+
"""
|
|
331
|
+
if ord == "inf":
|
|
332
|
+
ord = float("inf")
|
|
333
|
+
elif ord == "-inf":
|
|
334
|
+
ord = float("-inf")
|
|
335
|
+
|
|
336
|
+
result = getattr(self.linalg, "matrix_norm")(unwrap(x), keepdims=keepdims, ord=ord)
|
|
337
|
+
return CompatArray(result, xp=self)
|
|
338
|
+
|
|
339
|
+
@property
|
|
340
|
+
def linalg(self):
|
|
341
|
+
"""
|
|
342
|
+
The `linalg` namespace for linear algebra functions.
|
|
343
|
+
The following functions must be supported in the `linalg` namespace:
|
|
344
|
+
- `cholesky`(x, /, *, upper=False): Returns the lower (upper) Cholesky decomposition of a complex Hermitian or real symmetric positive-definite matrix x.
|
|
345
|
+
- `cross`(x1, x2, /, *, axis=-1): Returns the cross product of 3-element vectors.
|
|
346
|
+
- `det`(x, /): Returns the determinant of a square matrix (or a stack of square matrices) x.
|
|
347
|
+
- `diagonal`(x, /, *, offset=0): Returns the specified diagonals of a matrix (or a stack of matrices) x.
|
|
348
|
+
- `eigh`(x, /): Returns an eigenvalue decomposition of a complex Hermitian or real symmetric matrix (or a stack of matrices) x.
|
|
349
|
+
- `eigvalsh`(x, /): Returns the eigenvalues of a complex Hermitian or real symmetric matrix (or a stack of matrices) x.
|
|
350
|
+
- `inv`(x, /): Returns the multiplicative inverse of a square matrix (or a stack of square matrices) x.
|
|
351
|
+
- `matmul`(x1, x2, /): Alias for matmul().
|
|
352
|
+
- `matrix_norm`(x, /, *, keepdims=False, ord='fro'): Computes the matrix norm of a matrix (or a stack of matrices) x.
|
|
353
|
+
- `matrix_power`(x, n, /): Raises a square matrix (or a stack of square matrices) x to an integer power n.
|
|
354
|
+
- `matrix_rank`(x, /, *, rtol=None): Returns the rank (i.e., number of non-zero singular values) of a matrix (or a stack of matrices).
|
|
355
|
+
- `matrix_transpose`(x, /): Alias for matrix_transpose().
|
|
356
|
+
- `outer`(x1, x2, /): Returns the outer product of two vectors x1 and x2.
|
|
357
|
+
- `pinv`(x, /, *, rtol=None): Returns the (Moore-Penrose) pseudo-inverse of a matrix (or a stack of matrices) x.
|
|
358
|
+
- `qr`(x, /, *, mode='reduced'): Returns the QR decomposition of a full column rank matrix (or a stack of matrices).
|
|
359
|
+
- `slogdet`(x, /): Returns the sign and the natural logarithm of the absolute value of the determinant of a square matrix (or a stack of square matrices) x.
|
|
360
|
+
- `solve`(x1, x2, /): Returns the solution of a square system of linear equations with a unique solution.
|
|
361
|
+
- `svd`(x, /, *, full_matrices=True): Returns a singular value decomposition (SVD) of a matrix (or a stack of matrices) x.
|
|
362
|
+
- `svdvals`(x, /): Returns the singular values of a matrix (or a stack of matrices) x.
|
|
363
|
+
- `tensordot`(x1, x2, /, *, axes=2): Alias for tensordot().
|
|
364
|
+
- `trace`(x, /, *, offset=0, dtype=None): Returns the sum along the specified diagonals of a matrix (or a stack of matrices) x.
|
|
365
|
+
- `vecdot`(x1, x2, /, *, axis=-1): Alias for vecdot().
|
|
366
|
+
- `vector_norm`(x, /, *, axis=None, keepdims=False, ord=2): Computes the vector norm of a vector (or batch of vectors) x.
|
|
367
|
+
"""
|
|
368
|
+
return self._get_xp_attr("linalg")
|
|
369
|
+
|
|
239
370
|
# === Constants ===
|
|
240
371
|
@property
|
|
241
372
|
def e(self):
|
|
@@ -4,12 +4,13 @@
|
|
|
4
4
|
|
|
5
5
|
from __future__ import annotations
|
|
6
6
|
from numpy.typing import NDArray
|
|
7
|
+
from types import ModuleType
|
|
7
8
|
from typing import (Union, List, Tuple, Optional, Any, Literal, overload)
|
|
8
9
|
|
|
9
10
|
from ._base import Compat
|
|
10
11
|
from ._array import CompatArray
|
|
11
12
|
from ..types import (
|
|
12
|
-
DTypeT,
|
|
13
|
+
dtypeT, DTypeT, deviceT, DeviceT, DType, AnyDevice,
|
|
13
14
|
ValueT, Value, ArrayLike, ArrayOrAny
|
|
14
15
|
)
|
|
15
16
|
|
|
@@ -802,7 +803,7 @@ class CompatNamespace(Compat):
|
|
|
802
803
|
Returns
|
|
803
804
|
-------
|
|
804
805
|
CompatArray
|
|
805
|
-
A :class:`CompatArray` output array containing the concatenated values.
|
|
806
|
+
A :class:`CompatArray` output array containing the concatenated values.
|
|
806
807
|
"""
|
|
807
808
|
...
|
|
808
809
|
|
|
@@ -841,6 +842,26 @@ class CompatNamespace(Compat):
|
|
|
841
842
|
"""
|
|
842
843
|
...
|
|
843
844
|
|
|
845
|
+
# === Linear Algebra Extension ===
|
|
846
|
+
@overload
|
|
847
|
+
def vector_norm(self, x: NDArray[Any], /, *, axis: Optional[Union[int, Tuple[int, ...]]] = ..., keepdims: bool = ..., ord: Union[int, float, Literal["inf", "-inf"]] = ...) -> CompatArray[float, Literal["cpu"]]: ...
|
|
848
|
+
@overload
|
|
849
|
+
def vector_norm(self, x: CompatArray[Any, deviceT], /, *, axis: Optional[Union[int, Tuple[int, ...]]] = ..., keepdims: bool = ..., ord: Union[int, float, Literal["inf", "-inf"]] = ...) -> CompatArray[float, deviceT]: ...
|
|
850
|
+
@overload
|
|
851
|
+
def vector_norm(self, x: ArrayLike[Any], /, *, axis: Optional[Union[int, Tuple[int, ...]]] = ..., keepdims: bool = ..., ord: Union[int, float, Literal["inf", "-inf"]] = ...) -> CompatArray[float, AnyDevice]: ...
|
|
852
|
+
def vector_norm(self, x: ArrayLike[Any], /, *, axis: Optional[Union[int, Tuple[int, ...]]] = None, keepdims: bool = False, ord: Union[int, float, Literal["inf", "-inf"]] = 2) -> CompatArray[float, AnyDevice]: ...
|
|
853
|
+
|
|
854
|
+
@overload
|
|
855
|
+
def matrix_norm(self, x: NDArray[Any], /, *, keepdims: bool = ..., ord: Optional[Union[int, float, Literal["inf", "-inf", "fro", "nuc"]]] = ...) -> CompatArray[float, Literal["cpu"]]: ...
|
|
856
|
+
@overload
|
|
857
|
+
def matrix_norm(self, x: CompatArray[Any, deviceT], /, *, keepdims: bool = ..., ord: Optional[Union[int, float, Literal["inf", "-inf", "fro", "nuc"]]] = ...) -> CompatArray[float, deviceT]: ...
|
|
858
|
+
@overload
|
|
859
|
+
def matrix_norm(self, x: ArrayLike[Any], /, *, keepdims: bool = ..., ord: Optional[Union[int, float, Literal["inf", "-inf", "fro", "nuc"]]] = ...) -> CompatArray[float, AnyDevice]: ...
|
|
860
|
+
def matrix_norm(self, x: ArrayLike[Any], /, *, keepdims: bool = False, ord: Optional[Union[int, float, Literal["inf", "-inf", "fro", "nuc"]]] = "fro") -> CompatArray[float, AnyDevice]: ...
|
|
861
|
+
|
|
862
|
+
@property
|
|
863
|
+
def linalg(self) -> ModuleType: ...
|
|
864
|
+
|
|
844
865
|
# === Constants ===
|
|
845
866
|
@property
|
|
846
867
|
def e(self) -> float: ...
|
|
@@ -883,3 +904,5 @@ class CompatNamespace(Compat):
|
|
|
883
904
|
|
|
884
905
|
@property
|
|
885
906
|
def __name__(self) -> str: ...
|
|
907
|
+
|
|
908
|
+
def __getattr__(self, name: str) -> Any: ...
|
|
@@ -122,11 +122,11 @@ def to_tensor(obj, /, *, dtype=None, device=None, copy=True):
|
|
|
122
122
|
- `None`: Raises `ConvertNoneTypeError`;
|
|
123
123
|
- _others_: Converted to a `PyTorch tensor` directly.
|
|
124
124
|
|
|
125
|
-
dtype : Optional[
|
|
125
|
+
dtype : Optional[DType], default to `None`
|
|
126
126
|
The data type of the resulting `PyTorch tensor`.
|
|
127
127
|
- `None`: Use the default data type of the object.
|
|
128
128
|
|
|
129
|
-
device : Optional[
|
|
129
|
+
device : Optional[AnyDevice], default to `None`
|
|
130
130
|
The device on which the resulting `PyTorch tensor` will be allocated.
|
|
131
131
|
- `None`: Use the default device (usually `"cpu"`).
|
|
132
132
|
|
|
@@ -294,11 +294,11 @@ def as_array(obj, xp, /, *, dtype=None, device=None, copy=False, arraylike_only=
|
|
|
294
294
|
- _ArrayLibraryName_ (`"numpy"` or `"torch"`): Converted to a `NumPy array` or `PyTorch tensor` respectively using the corresponding conversion functions;
|
|
295
295
|
- _Namespace_ or _CompatNamespace_: Converted to an array using the `asarray()` function provided by the namespace module, which must be compatible with the array API standard.
|
|
296
296
|
|
|
297
|
-
dtype : Optional[
|
|
297
|
+
dtype : Optional[DType], default to `None`
|
|
298
298
|
The data type of the resulting array.
|
|
299
299
|
- `None`: Use the default data type of the object.
|
|
300
300
|
|
|
301
|
-
device : Optional[
|
|
301
|
+
device : Optional[AnyDevice], default to `None`
|
|
302
302
|
The device on which the resulting array will be allocated (only if `array namespace` supports it).
|
|
303
303
|
|
|
304
304
|
copy : bool, default to `False`
|
|
@@ -24,8 +24,9 @@ Device = Union[DeviceLiteral, torch.device]
|
|
|
24
24
|
AnyDevice = Union[Device, str]
|
|
25
25
|
|
|
26
26
|
dtypeT = TypeVar("dtypeT", bound=DType)
|
|
27
|
-
deviceT = TypeVar("deviceT", bound=
|
|
28
|
-
anydeviceT = TypeVar("anydeviceT", bound=AnyDevice)
|
|
27
|
+
deviceT = TypeVar("deviceT", bound=AnyDevice)
|
|
28
|
+
# anydeviceT = TypeVar("anydeviceT", bound=AnyDevice)
|
|
29
|
+
|
|
29
30
|
DTypeT = TypeVar("DTypeT", bound=DType)
|
|
30
31
|
DeviceT = TypeVar("DeviceT", bound=Device)
|
|
31
32
|
AnyDeviceT = TypeVar("AnyDeviceT", bound=AnyDevice)
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: cobra-array
|
|
3
|
-
Version: 0.2.
|
|
3
|
+
Version: 0.2.1
|
|
4
4
|
Summary: A backend-agnostic array utility library that unifies array conversion, context control, and cross-library operations across `NumPy`/`PyTorch`-style ecosystems.
|
|
5
5
|
Author-email: Zhen Tian <zhen.tian.cs@gmail.com>
|
|
6
6
|
Project-URL: Homepage, https://github.com/tinchen777/cobra-array.git
|
|
@@ -67,10 +67,17 @@ def test_to_device_on_numpy_backend():
|
|
|
67
67
|
@pytest.mark.skipif(torch_xp is None, reason="PyTorch not available")
|
|
68
68
|
def test_to_tensor_available_backend():
|
|
69
69
|
a = _arr_1d()
|
|
70
|
+
|
|
71
|
+
aa = a.to_device("cpu")
|
|
72
|
+
|
|
73
|
+
aa = CompatArray(a)
|
|
74
|
+
|
|
70
75
|
t = a.to_tensor(device="cpu")
|
|
71
76
|
|
|
72
77
|
assert isinstance(t, torch_xp.Tensor)
|
|
73
78
|
assert tuple(t.shape) == (3,)
|
|
79
|
+
|
|
80
|
+
aa = wrap_arraylike(a)
|
|
74
81
|
|
|
75
82
|
|
|
76
83
|
def test_unstack_and_nonzero():
|
|
@@ -470,13 +477,47 @@ def test_cxp_of_compatarray_matches_array_namespace():
|
|
|
470
477
|
assert cxp.xp_name == a.xp_name
|
|
471
478
|
|
|
472
479
|
|
|
480
|
+
def test_linalg():
|
|
481
|
+
a = CompatArray(np.array([[1.0, 2.0], [3.0, 4.0]], dtype=np.float32))
|
|
482
|
+
cxp = a.cxp
|
|
483
|
+
print(a)
|
|
484
|
+
|
|
485
|
+
f = cxp.vector_norm(a, axis=0, ord="-inf")
|
|
486
|
+
|
|
487
|
+
print(f)
|
|
488
|
+
|
|
489
|
+
f = cxp.matrix_norm(a, ord="nuc")
|
|
490
|
+
|
|
491
|
+
print(f)
|
|
492
|
+
|
|
493
|
+
# def test_linalg2():
|
|
494
|
+
# import torch
|
|
495
|
+
# a = CompatArray(torch.tensor([[1.0, 2.0], [3.0, 4.0]], device="cuda:0"))
|
|
496
|
+
# cxp = a.cxp
|
|
497
|
+
# print(a)
|
|
498
|
+
|
|
499
|
+
# f = cxp.vector_norm(a, axis=0, ord="-inf")
|
|
500
|
+
|
|
501
|
+
# print(f.device)
|
|
502
|
+
|
|
503
|
+
# print(f)
|
|
504
|
+
|
|
505
|
+
# f = cxp.matrix_norm(a, ord="nuc")
|
|
506
|
+
|
|
507
|
+
# print(f)
|
|
508
|
+
# print(f.device)
|
|
509
|
+
|
|
510
|
+
|
|
473
511
|
if __name__ == "__main__":
|
|
474
|
-
test_unique_all_unique_counts_unique_inverse()
|
|
475
|
-
print("=" * 40)
|
|
512
|
+
# test_unique_all_unique_counts_unique_inverse()
|
|
513
|
+
# print("=" * 40)
|
|
476
514
|
|
|
477
|
-
test_numpy_operator_overloads()
|
|
478
|
-
print("=" * 40)
|
|
515
|
+
# test_numpy_operator_overloads()
|
|
516
|
+
# print("=" * 40)
|
|
479
517
|
# test_torch_operator_overloads()
|
|
480
|
-
print("=" * 40)
|
|
518
|
+
# print("=" * 40)
|
|
481
519
|
|
|
482
|
-
test_add()
|
|
520
|
+
# test_add()
|
|
521
|
+
|
|
522
|
+
# test_linalg2()
|
|
523
|
+
pass
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|