sfepy.linalg.sparse module

Some sparse matrix utilities missing in scipy.

sfepy.linalg.sparse.compose_sparse(blocks, row_sizes=None, col_sizes=None)[source]

Compose sparse matrices into a global sparse matrix.

Parameters:
blockssequence of sequences

The sequence of sequences of equal lengths - the individual sparse matrix blocks. The integer 0 can be used to mark an all-zero block, if its size can be determined from the other blocks.

row_sizessequence, optional

The required row sizes of the blocks. It can be either a sequence of non-negative integers, or a sequence of slices with non-negative limits. In any case the sizes have to be compatible with the true block sizes. This allows to extend the matrix shape as needed and to specify sizes of all-zero blocks.

col_sizessequence, optional

The required column sizes of the blocks. See row_sizes.

Returns:
mtxcoo_matrix

The sparse matrix (COO format) composed from the given blocks.

Examples

Stokes-like problem matrix.

>>> import scipy.sparse as sp
>>> A = sp.csr_matrix([[1, 0], [0, 1]])
>>> B = sp.coo_matrix([[1, 1]])
>>> K = compose_sparse([[A, B.T], [B, 0]])
>>> print K.todense()
[[1 0 1]
 [0 1 1]
 [1 1 0]]
sfepy.linalg.sparse.infinity_norm(mtx)[source]

Infinity norm of a sparse matrix (maximum absolute row sum).

Parameters:
mtxspmatrix or array

The sparse matrix.

Returns:
normfloat

Infinity norm of the matrix.

See also

scipy.linalg.norm

dense matrix norms

Notes

  • This serves as an upper bound on spectral radius.

  • CSR and CSC avoid copying indices and indptr arrays.

  • inspired by PyAMG

sfepy.linalg.sparse.insert_sparse_to_csr(mtx1, mtx2, irs, ics)[source]

Insert a sparse matrix mtx2 into a CSR sparse matrix mtx1 at rows irs and columns ics. The submatrix mtx1[irs,ics] must already be preallocated and have the same structure as mtx2.

sfepy.linalg.sparse.save_sparse_txt(filename, mtx, fmt='%d %d %f\n')[source]

Save a CSR/CSC sparse matrix into a text file