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 ncol: The number of columns of the matrix size: The common number of rows and columns, for a square matrix bandwidth: The bandwidth (if creating a band matrix) matrix: The starting spmatrix if there is one sizeHint: A guess on the number of nonzero elements of the matrix symmetric: A boolean indicating whether the matrix is symmetric. storeZeros: A boolean indicating whether to store zero values. -
addAt
(vector, id1, id2)¶ 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]) >>> L.addAt((1.73,2.2,8.4,3.9,1.23), (1,2,0,0,1), (2,2,0,0,2)) >>> print L 12.300000 10.000000 3.000000 --- 3.141593 2.960000 2.500000 --- 2.200000
-
addAtDiagonal
(vector)¶ 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 tovalue
. Ifid1
is omitted, it is replaced withrange(nrow)
. Ifid2
is omitted, it is replaced withrange(ncol)
.
-
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 arrayval
. 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)¶ Bases:
pysparse.sparse.pysparseMatrix.PysparseMatrix
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)¶ Bases:
pysparse.sparse.pysparseMatrix.PysparseMatrix
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.