New in version 1.0.1.
Bases: 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.
| Currently accepted keywords include: | |
|---|---|
| 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. |
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
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])
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 is omitted, it is replaced with range(ncol).
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
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]]
Bases: pysparseMatrix.PysparseMatrix
Represents a sparse identity matrix for pysparse.
>>> print PysparseIdentityMatrix(size = 3)
1.000000 --- ---
--- 1.000000 ---
--- --- 1.000000
Bases: 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.