Linear Algebra

The next functions can be used for computing linear algebra operations with NDArray.

matmul(x1, x2, **kwargs)

Computes the matrix product between two Blosc2 NDArrays.

matrix_transpose(arr, **kwargs)

Transposes a matrix (or a stack of matrices).

permute_dims(arr[, axes])

Permutes the axes (dimensions) of an array.

blosc2.matmul(x1: NDArray, x2: NDArray, **kwargs: Any) NDArray[source]

Computes the matrix product between two Blosc2 NDArrays.

Parameters:
  • x1 (NDArray) – The first input array.

  • x2 (NDArray) – The second input array.

  • kwargs (Any, optional) – Keyword arguments that are supported by the empty() constructor.

Returns:

out – The matrix product of the inputs. This is a scalar only when both x1, x2 are 1-d vectors.

Return type:

NDArray

Raises:

ValueError

If the last dimension of x1 is not the same size as the second-to-last dimension of x2.

If a scalar value is passed in.

References

numpy.matmul

Examples

For 2-D arrays it is the matrix product:

>>> import numpy as np
>>> import blosc2
>>> a = np.array([[1, 2],
...               [3, 4]])
>>> nd_a = blosc2.asarray(a)
>>> b = np.array([[2, 3],
...               [2, 1]])
>>> nd_b = blosc2.asarray(b)
>>> blosc2.matmul(nd_a, nd_b)
array([[ 6,  5],
       [14, 13]])

For 2-D mixed with 1-D, the result is the usual.

>>> a = np.array([[1, 3],
...               [0, 1]])
>>> nd_a = blosc2.asarray(a)
>>> v = np.array([1, 2])
>>> nd_v = blosc2.asarray(v)
>>> blosc2.matmul(nd_a, nd_v)
array([7, 2])
>>> blosc2.matmul(nd_v, nd_a)
array([1, 5])
blosc2.matrix_transpose(arr: NDArray, **kwargs: Any) NDArray[source]

Transposes a matrix (or a stack of matrices).

Parameters:

arr (NDArray) – The input NDArray having shape (..., M, N) and whose innermost two dimensions form MxN matrices.

Returns:

out – A new NDArray containing the transpose for each matrix and having shape (..., N, M).

Return type:

NDArray

blosc2.permute_dims(arr: NDArray, axes: tuple[int] | list[int] | None = None, **kwargs: Any) NDArray[source]

Permutes the axes (dimensions) of an array.

Parameters:
  • arr (NDArray) – The input array.

  • axes (tuple[int], list[int], optional) – The desired permutation of axes. If None, the axes are reversed by default. If specified, axes must be a tuple or list representing a permutation of [0, 1, ..., N-1], where N is the number of dimensions of the input array. Negative indices are also supported. The i-th axis of the result will correspond to the axis numbered axes[i] of the input.

  • kwargs (Any, optional) – Keyword arguments that are supported by the empty() constructor.

Returns:

out – A Blosc2 NDArray with axes transposed.

Return type:

NDArray

Raises:

ValueError – If axes is not a valid permutation of the dimensions of arr.

References

numpy.transpose

permute_dims

Examples

For 2-D arrays it is the matrix transposition as usual:

>>> import blosc2
>>> a = blosc2.arange(1, 10).reshape((3, 3))
>>> a[:]
array([[1, 2, 3],
       [4, 5, 6],
       [7, 8, 9]])
>>> at = blosc2.permute_dims(a)
>>> at[:]
array([[1, 4, 7],
       [2, 5, 8],
       [3, 6, 9]])

For 3-D arrays:

>>> import blosc2
>>> a = blosc2.arange(1, 25).reshape((2, 3, 4))
>>> a[:]
array([[[ 1,  2,  3,  4],
        [ 5,  6,  7,  8],
        [ 9, 10, 11, 12]],
       [[13, 14, 15, 16],
        [17, 18, 19, 20],
        [21, 22, 23, 24]]])
>>> at = blosc2.permute_dims(a, axes=(1, 0, 2))
>>> at[:]
array([[[ 1,  2,  3,  4],
        [13, 14, 15, 16]],
       [[ 5,  6,  7,  8],
        [17, 18, 19, 20]],
       [[ 9, 10, 11, 12],
        [21, 22, 23, 24]]])