New in version 1.0.1.
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.
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 : |
|
---|
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
Add the components of vector vector to the diagonal elements of the matrix.
Apply in-place column scaling. Each column is scaled by the corresponding component of v, i.e., A[:,i] *= v[i].
Returns a (deep) copy of a sparse matrix
Exports the matrix to a Matrix Market file of the given filename.
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])
Returns the underlying ll_mat sparse matrix of self
Returns the number of nonzero elements of self
Convert a sparse matrix to a dense Numpy matrix.
Returns the shape (nrow,ncol) of a sparse matrix
Returns True is self is a symmetric matrix or False otherwise
This method is required for scipy solvers.
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.
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
Apply in-place row scaling. Each row is scaled by the corresponding component of v, i.e., A[i,:] *= v[i].
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]]
Extract the diagonal of a matrix and place it in a Numpy array.
Bases: pysparse.sparse.pysparseMatrix.PysparseMatrix
Represents a sparse identity matrix for pysparse.
>>> print PysparseIdentityMatrix(size = 3)
1.000000 --- ---
--- 1.000000 ---
--- --- 1.000000
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 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.