legate_sparse.dia_array#
- class legate_sparse.dia_array(self, arg, shape=None, dtype=None, copy=False)#
Sparse matrix with DIAgonal storage.
- This can be instantiated in several ways:
- dia_array(D)
where D is a 2-D ndarray or cupynumeric.ndarray
- dia_array((data, offsets), shape=(M, N))
where data is a 2-D array and offsets is a 1-D array of diagonal offsets
- dia_array((data, offset), shape=(M, N))
where data is a 1-D array and offset is a single integer
- dtype#
Data type of the array
- Type:
dtype
- shape#
Shape of the array
- Type:
2-tuple
- data#
DIA format data array of the array
- Type:
cupynumeric.ndarray
- offsets#
DIA format offset array of the array
- Type:
cupynumeric.ndarray
Notes
The DIA (Diagonal) format stores a sparse matrix by diagonals. The data array has shape (n_diagonals, max_diagonal_length) where each row represents a diagonal. The offsets array contains the diagonal offsets (k > 0 for upper diagonals, k < 0 for lower diagonals).
- Advantages of the DIA format:
efficient for matrices with few diagonals
fast matrix-vector products
simple structure
- Disadvantages of the DIA format:
inefficient for irregular sparsity patterns
not suitable for general sparse matrices
limited arithmetic operations
- Differences from SciPy:
Uses cupynumeric arrays instead of numpy arrays
Limited functionality (mainly for matrix generation in examples)
Some operations may not be fully optimized
Primarily used as an intermediate format for conversion to CSR
Examples
>>> import cupynumeric as np >>> from legate_sparse import dia_array >>> data = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) >>> offsets = np.array([-1, 0, 1]) >>> A = dia_array((data, offsets), shape=(3, 3)) >>> A.todense() array([[5, 2, 0], [4, 8, 3], [0, 7, 9]])
- __init__(arg, shape=None, dtype=None, copy=False)#
Initialize a DIA array.
- Parameters:
arg (tuple) – The input data. Must be a tuple (data, offsets) where: - data is a 2-D array containing the diagonal values - offsets is a 1-D array or integer specifying diagonal offsets
shape (tuple, optional) – Shape of the array (M, N). Required if not inferrable from input.
dtype (dtype, optional) – Data type of the array. If None, inferred from input data.
copy (bool, optional) – Whether to copy the input data. Default is False.
- Raises:
NotImplementedError – If shape is not provided (shape is required for DIA arrays).
AssertionError – If arg is not a tuple or has invalid format.
ValueError – If input data is inconsistent or invalid.
Notes
The DIA format is primarily used for matrix generation in examples and as an intermediate format for conversion to CSR. The shape parameter is required as it cannot be inferred from the diagonal data.
The offsets array specifies which diagonals are stored: - k > 0: upper diagonal (kth diagonal above main diagonal) - k = 0: main diagonal - k < 0: lower diagonal (kth diagonal below main diagonal)
Methods
__init__
(arg[, shape, dtype, copy])Initialize a DIA array.
arcsin
()Element-wise arcsin.
arcsinh
()Element-wise arcsinh.
arctan
()Element-wise arctan.
arctanh
()Element-wise arctanh.
asformat
(format[, copy])Convert the matrix to a specified format.
astype
(dtype[, casting, copy])ceil
()Element-wise ceil.
copy
()Returns a copy of this matrix.
deg2rad
()Element-wise deg2rad.
expm1
()Element-wise expm1.
floor
()Element-wise floor.
log1p
()Element-wise log1p.
nnz_to_pos
(q_nnz)Convert non-zero counts to position arrays for this instance.
nnz_to_pos_cls
(q_nnz)Convert non-zero counts to position arrays.
rad2deg
()Element-wise rad2deg.
rint
()Element-wise rint.
sign
()Element-wise sign.
sin
()Element-wise sin.
sinh
()Element-wise sinh.
sqrt
()Element-wise sqrt.
sum
([axis, dtype, out])Sum the matrix elements over a given axis.
tan
()Element-wise tan.
tanh
()Element-wise tanh.
tocsr
([copy])Convert this matrix to a CSR matrix.
transpose
([axes, copy])Reverses the dimensions of the sparse matrix.
trunc
()Element-wise trunc.
Attributes