Skip to content

Commit 4bc8d6f

Browse files
committed
Add properties and functions, organise linalg tests
1 parent b6b90d1 commit 4bc8d6f

3 files changed

Lines changed: 348 additions & 240 deletions

File tree

src/blosc2/ndarray.py

Lines changed: 103 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -800,6 +800,8 @@ def all(
800800
class Operand:
801801
"""Base class for all operands in expressions."""
802802

803+
_device = "cpu"
804+
803805
def __neg__(self) -> blosc2.LazyExpr:
804806
return blosc2.LazyExpr(new_op=(0, "-", self))
805807

@@ -829,6 +831,15 @@ def __array_interface__(self):
829831
"version": 3,
830832
}
831833

834+
@property
835+
def device(self):
836+
return self._device
837+
838+
def to_device(self, device):
839+
if device != "cpu":
840+
raise ValueError(f"Unsupported device: {device}. Only 'cpu' is accepted.")
841+
return self
842+
832843
def __array_ufunc__(self, ufunc, method, *inputs, **kwargs):
833844
# Handle operations at the array level
834845
if method != "__call__":
@@ -972,6 +983,9 @@ def __rpow__(self, value: int | float | NDArray | NDField | blosc2.C2Array, /) -
972983
_check_allowed_dtypes(value)
973984
return blosc2.LazyExpr(new_op=(value, "**", self))
974985

986+
def __abs__(self) -> blosc2.LazyExpr:
987+
return abs(self)
988+
975989
def __bool__(self) -> bool:
976990
if math.prod(self.shape) != 1:
977991
raise ValueError(f"The truth value of an array of shape {self.shape} is ambiguous.")
@@ -1021,8 +1035,8 @@ def where(self, value1=None, value2=None):
10211035
out: LazyExpr
10221036
A new expression with the where condition applied.
10231037
"""
1024-
expr = blosc2.LazyExpr._new_expr("o0", {"o0": self}, guess=False).where(value1, value2)
1025-
return expr.compute()
1038+
expr = blosc2.LazyExpr._new_expr("o0", {"o0": self}, guess=False)
1039+
return expr.where(value1, value2)
10261040

10271041
@is_documented_by(sum)
10281042
def sum(self, axis=None, dtype=None, keepdims=False, **kwargs):
@@ -1501,6 +1515,19 @@ def T(self):
15011515
raise ValueError("This property only works for 2-dimensional arrays.")
15021516
return permute_dims(self)
15031517

1518+
@property
1519+
def mT(self):
1520+
"""Transpose of a matrix (or a stack of matrices)."""
1521+
if self.ndim < 2:
1522+
raise ValueError("This property only works for N-dimensional arrays with N>=2.")
1523+
axes = np.arange(self.ndim)
1524+
axes[-1] = self.ndim - 2
1525+
axes[-2] = self.ndim - 1
1526+
return permute_dims(self, axes=axes)
1527+
1528+
def __matmul__(self, other):
1529+
return matmul(self, other)
1530+
15041531
def get_fselection_numpy(self, key: list | np.ndarray) -> np.ndarray:
15051532
"""
15061533
Select a slice from the array using a fancy index.
@@ -3455,6 +3482,80 @@ def add(
34553482
return x1 + x2
34563483

34573484

3485+
def subtract(
3486+
x1: NDArray | NDField | blosc2.C2Array | blosc2.LazyExpr,
3487+
x2: NDArray | NDField | blosc2.C2Array | blosc2.LazyExpr,
3488+
) -> blosc2.LazyExpr:
3489+
"""
3490+
Computes the value of x1_i - x2_i for each element x1_i of the input array x1
3491+
with the respective element x2_i of the input array x2.
3492+
3493+
Parameters
3494+
-----------
3495+
x1: NDArray | NDField | blosc2.C2Array | blosc2.LazyExpr
3496+
First input array. May have any data type.
3497+
3498+
x2:NDArray | NDField | blosc2.C2Array | blosc2.LazyExpr
3499+
Second input array. Must be compatible with x1. May have any data type.
3500+
3501+
Returns
3502+
-------
3503+
out LazyExpr
3504+
A LazyArray containing the element-wise results.
3505+
3506+
References
3507+
----------
3508+
`np.subtract <https://numpy.org/doc/stable/reference/generated/numpy.subtract.html#numpy.subtract>`_
3509+
"""
3510+
return x1 - x2
3511+
3512+
3513+
def square(x1: NDArray | NDField | blosc2.C2Array | blosc2.LazyExpr) -> blosc2.LazyExpr:
3514+
"""
3515+
Computes the value of x1_i**2 for each element x1_i of the input array x1.
3516+
3517+
Parameters
3518+
-----------
3519+
x1: NDArray | NDField | blosc2.C2Array | blosc2.LazyExpr
3520+
First input array. May have any data type.
3521+
3522+
Returns
3523+
-------
3524+
out LazyExpr
3525+
A LazyArray containing the element-wise results.
3526+
3527+
References
3528+
----------
3529+
`np.square <https://numpy.org/doc/stable/reference/generated/numpy.square.html#numpy.square>`_
3530+
"""
3531+
return x1 * x1
3532+
3533+
3534+
def pow(
3535+
x1: NDArray | NDField | blosc2.C2Array | blosc2.LazyExpr | int | float | complex,
3536+
x2: NDArray | NDField | blosc2.C2Array | blosc2.LazyExpr | int | float | complex,
3537+
) -> blosc2.LazyExpr:
3538+
"""
3539+
Computes the value of x1_i**x2_i for each element x1_i of the input array x1 and x2_i
3540+
of x2.
3541+
3542+
Parameters
3543+
-----------
3544+
x1: NDArray | NDField | blosc2.C2Array | blosc2.LazyExpr
3545+
First input array. May have any data type.
3546+
3547+
Returns
3548+
-------
3549+
out LazyExpr
3550+
A LazyArray containing the element-wise results.
3551+
3552+
References
3553+
----------
3554+
`np.pow <https://numpy.org/doc/stable/reference/generated/numpy.pow.html#numpy.pow>`_
3555+
"""
3556+
return x1**x2
3557+
3558+
34583559
def where(
34593560
condition: blosc2.LazyExpr | NDArray,
34603561
x: NDArray | NDField | np.ndarray | int | float | complex | bool | str | bytes | None = None,

0 commit comments

Comments
 (0)