Sparse Matrix

class fastmat.Sparse

Bases: fastmat.Matrix.Matrix

\[x \mapsto S x,\]

where \(S\) is a scipy.sparse matrix. To provide a high level of generality, the user has to make use of the standard scipy.sparse matrix constructors and pass them to fastmat during construction. After that a Sparse matrix can be used like every other type in fastmat

>>> # import the package
>>> import fastmat as fm
>>>
>>> # import scipy to get
>>> # the constructor
>>> import scipy.sparse.rand as r
>>>
>>> # set the matrix size
>>> n = 100
>>>
>>> # construct the sparse matrix
>>> S = fm.Sparse(
>>>         r(
>>>             n,
>>>             n,
>>>             0.01,
>>>             format='csr'
>>>         ))

This yields a random sparse matrix with 1% of its entries occupied drawn from a random distribution.

It is also possible to directly cast SciPy sparse matrices into the fastmat` sparse matrix format as follows.

>>> # import the package
>>> import fastmat as fm
>>>
>>> # import scipy to get
>>> # the constructor
>>> import scipy.sparse as ss
>>>
>>> # construct the SciPy sparse matrix
>>> S_scipy = ss.csr_matrix(
>>>         [
>>>             [1, 0, 0],
>>>             [1, 0, 0],
>>>             [0, 0, 1]
>>>         ]
>>>     )
>>>
>>> # construct the fastmat sparse matrix
>>> S = fm.Sparse(S_scipy)

Note

The format specifier drastically influences performance during multiplication of these matrices. From our experience 'csr' works best in these cases.

For this matrix class we used the already tried and tested routines of SciPy [1], so we merely provide a convenient wrapper to integrate nicely into fastmat.

__init__

Initialize a Sparse matrix instance.

Parameters:
matSparse : scipy.sparse.spmatrix

A 2d scipy sparse matrix to be cast as a fastmat matrix.

**options : optional

Additional optional keyworded arguments. Supports all optional arguments supported by fastmat.Matrix.

spArray

Return the scipy sparse matrix .

(read only)

spArrayH

Return the scipy sparse matrix’ hermitian transpose.

(read only)