A n×n matrix is called dense if it has O(n2) non-zero entries. For example:
To store the matrix, all components are saved in row-major order. For A given above, we would store:
The dimensions of the matrix are stored separately.
A n×n matrix is called sparse if it has O(n) non-zero entries. For example:
COO (Coordinate Format) stores arrays of row indices, column indices and the corresponding non-zero data values in any order. This format provides fast methods to construct sparse matrices and convert to different sparse formats. For A the COO format is:
data=[12.09.07.05.01.02.011.03.06.04.08.010.0] row=[422100312123],col=[442303300132]CSR (Compressed Sparse Row) encodes rows offsets, column indices and the corresponding non-zero data values. This format provides fast arithmetic operations between sparse matrices, and fast matrix vector product. The row offsets are defined by the followign recursive relationship (starting with rowptr[0]=0):
where nnz(rowk) is the number of non-zero elements in the kth row. Note that the length of rowptr is nrows+1, where the last element in rowptr is the number of nonzeros in A. For A the CSR format is:
data=[1.02.03.04.05.06.07.08.09.010.011.012.0] col=[030130234234] rowptr=[02591112]The following code snippet performs CSR matrix vector product for square matrices:
import numpy as np
def csr_mat_vec(A, x):
Ax = np.zeros_like(x)
for i in range(x.shape[0]):
for k in range(A.rowptr[i], A.rowptr[i+1]):
Ax[i] += A.data[k]*x[A.col[k]]
return Ax