fips.Operator.sparse#

Operator.sparse()[source]#

Return the matrix as a new SciPy CSR array, rows and columns in the spaces’ order.

A sparse or diagonal operator (or a block diagonal one of those) gives its stored entries without forming the dense matrix. Any other form (a dense array, Kronecker factors) forms the full matrix, as dense() does, and keeps its nonzero entries.

Returns:

A writable copy of the matrix, by position: the labels are in rows and cols.

Return type:

csr_array

See also

Operator.dense

The matrix as a dense array.

Examples

>>> import numpy as np
>>> import pandas as pd
>>> import scipy.sparse as sp
>>> from fips import Operator, Space
>>> obs = Space({"co2": pd.Index([1, 2], name="obs_id")})
>>> state = Space({"flux": pd.Index(["a", "b", "c"], name="cell")})
>>> H = Operator(sp.csr_array([[1.0, 0.0, 0.0], [0.0, 0.0, 2.0]]), obs, state)
>>> H.sparse().nnz
2
>>> abs(H.sparse()).sum(axis=0)
array([1., 0., 2.])