#### Previous topic

Eigenvalue Solver

#### Next topic

Other Sparse Matrix Packages for Python

New in version 1.0.1.

# Higher-Level Sparse Matrix Classes¶

## The pysparseMatrix module¶

This module defines a few convenience classes as wrappers around ll_mat objects. Being proper Python classes, they are subclassable. PysparseMatrix objects have hooks for all methods of ll_mat objects.

class pysparse.sparse.pysparseMatrix.PysparseMatrix(**kwargs)

Bases: pysparse.sparse.sparseMatrix.SparseMatrix

A PysparseMatrix is a class wrapper for the pysparse spmatrix sparse matrix type. This class facilitates matrix populating and allows intuitive operations on sparse matrices and vectors.

Keywords :
nrow: The number of rows of the matrix The number of columns of the matrix The common number of rows and columns, for a square matrix The bandwidth (if creating a band matrix) The starting spmatrix if there is one A guess on the number of nonzero elements of the matrix A boolean indicating whether the matrix is symmetric. A boolean indicating whether to store zero values.

Add elements of vector to the positions in the matrix corresponding to (id1,id2)

>>> L = PysparseMatrix(size = 3)
>>> L.put([3.,10.,numpy.pi,2.5], [0,0,1,2], [2,1,1,0])
>>> print L
12.300000  10.000000   3.000000
---     3.141593   2.960000
2.500000      ---     2.200000


Add the components of vector vector to the diagonal elements of the matrix.

col_scale(v)

Apply in-place column scaling. Each column is scaled by the corresponding component of v, i.e., A[:,i] *= v[i].

copy()

Returns a (deep) copy of a sparse matrix

exportMmf(filename)

Exports the matrix to a Matrix Market file of the given filename.

find()

Returns three Numpy arrays to describe the sparsity pattern of self in so-called coordinate (or triplet) format:

>>> L = PysparseMatrix(size = 3)
>>> L.put([3.,10.,numpy.pi,2.5], [0,0,1,2], [2,1,1,0])
>>> (val,irow,jcol) = L.find()
>>> val
array([ 10.        ,   3.        ,   3.14159265,   2.5       ])
>>> irow
array([0, 0, 1, 2])
>>> jcol
array([1, 2, 1, 0])

getMatrix()

Returns the underlying ll_mat sparse matrix of self

getNnz()

Returns the number of nonzero elements of self

getNumpyArray()

Convert a sparse matrix to a dense Numpy matrix.

getShape()

Returns the shape (nrow,ncol) of a sparse matrix

isSymmetric()

Returns True is self is a symmetric matrix or False otherwise

matvec(x)

This method is required for scipy solvers.

put(value, id1=None, id2=None)

Put elements of value at positions of the matrix corresponding to (id1, id2)

>>> L = PysparseMatrix(size = 3)
>>> L.put( [3.,10.,numpy.pi,2.5], [0,0,1,2], [2,1,1,0] )
>>> print L
---    10.000000   3.000000
---     3.141593      ---
2.500000      ---        ---
>>> L.put(2*numpy.pi, range(3), range(3))
>>> print L
6.283185  10.000000   3.000000
---     6.283185      ---
2.500000      ---     6.283185


If value is a scalar, it has the same effect as the vector of appropriate length with all values equal to value. If id1 is omitted, it is replaced with range(nrow). If id2 also is omitted, it is replaced with range(ncol). If id2 is omitted but id1 is present, id2 is set to id1.

putDiagonal(vector)

Put elements of vector along diagonal of matrix

>>> L = PysparseMatrix(size = 3)
>>> L.putDiagonal([3.,10.,numpy.pi])
>>> print L
3.000000      ---        ---
---    10.000000      ---
---        ---     3.141593
>>> L.putDiagonal([10.,3.])
>>> print L
10.000000      ---        ---
---     3.000000      ---
---        ---     3.141593
>>> L.putDiagonal(2.7182)
>>> print L
2.718200      ---        ---
---     2.718200      ---
---        ---     2.718200

row_scale(v)

Apply in-place row scaling. Each row is scaled by the corresponding component of v, i.e., A[i,:] *= v[i].

take(id1=None, id2=None)

Extract elements at positions (irow[i], jcol[i]) and place them in the array val. In other words:

for i in range(len(val)): val[i] = A[irow[i],jcol[i]]

takeDiagonal()

Extract the diagonal of a matrix and place it in a Numpy array.

class pysparse.sparse.pysparseMatrix.PysparseIdentityMatrix(size)

Represents a sparse identity matrix for pysparse.

>>> print PysparseIdentityMatrix(size = 3)
1.000000      ---        ---
---     1.000000      ---
---        ---     1.000000

class pysparse.sparse.pysparseMatrix.PysparseSpDiagsMatrix(size, vals, pos, **kwargs)

Represents a banded matrix with specified diagonals.

Example: Create a tridiagonal matrix with 1’s on the diagonal, 2’s above the diagonal, and -2’s below the diagonal.

>>> from numpy import ones
>>> e = ones(5)
>>> print PysparseSpDiagsMatrix(size=5, vals=(-2*e,e,2*e), pos=(-1,0,1))
1.000000   2.000000      ---        ---        ---
-2.000000   1.000000   2.000000      ---        ---
---    -2.000000   1.000000   2.000000      ---
---        ---    -2.000000   1.000000   2.000000
---        ---        ---    -2.000000   1.000000


Note that since the pos[k]-th diagonal has size-|pos[k]| elements, only that many first elements of vals[k] will be inserted.

If the banded matrix is requested to be symmetric, elements above the main diagonal are not inserted.

### Fancy Indexing¶

Fancy indexing carries over to PysparseMatrix objects and is used exactly in the same way as with ll_mat objects. Refer to Section Low-Level Sparse Matrix Types for details.