The following attributes and methods are defined for spmatrix objects.
V
A single-column dense matrix containing the numerical values of the nonzero entries in column-major order. Making an assignment to the attribute is an efficient way of changing the values of the sparse matrix, without changing the sparsity pattern.
When the attribute V is read, a copy of V is returned, as a new dense matrix. (This implies, for example, that an indexed assignment ”A.V[I] = B” does not work, or at least cannot be used to modify A. Instead the attribute V will be read and returned as a new matrix; then the elements of this new matrix are modified.)
I
A single-column integer matrix with the row indices of the entries in V. A read-only attribute.
J
A single-column integer matrix with the column indices of the entries in V. A read-only attribute.
size
A tuple with the dimensions of the matrix. A read-only attribute.
CCS
A triplet (colptr, rowind, values) with the compressed-column-storage representation of the matrix. A read-only attribute. This attribute can be used to export sparse matrices to other packages such as MOSEK.
trans()
Returns the transpose of a sparse matrix as a new sparse matrix. One can also use A.T instead of A.trans().
ctrans()
Returns the complex conjugate transpose of a sparse matrix as a new sparse matrix. One can also use A.H instead of A.ctrans().
In the following example we take the elementwise square root of the matrix
![]() | (6.2) |
>>> from cvxopt.base import sqrt
>>> A = spmatrix([2,1,2,2,1,3,4], [1,2,0,2,3,0,2], [0,0,1,1,2,3,3]) >>> B = spmatrix(sqrt(A.V), A.I, A.J) >>> print B [ 0 1.41e+00 0 1.73e+00] [ 1.41e+00 0 0 0 ] [ 1.00e+00 1.41e+00 0 2.00e+00] [ 0 0 1.00e+00 0 ] |
The next example below illustrates assignments to V.
>>> from cvxopt.base import spmatrix, matrix
>>> A = spmatrix(range(5), [0,1,1,2,2], [0,0,1,1,2]) >>> print A [ 0.00e+00 0 0 ] [ 1.00e+00 2.00e+00 0 ] [ 0 3.00e+00 4.00e+00] >>> B = spmatrix(A.V, A.J, A.I, (4,4)) # transpose and add a zero row and column >>> print B [ 0.00e+00 1.00e+00 0 0 ] [ 0 2.00e+00 3.00e+00 0 ] [ 0 0 4.00e+00 0 ] [ 0 0 0 0 ] >>> print matrix(B) [ 0.00e+00 1.00e+00 0.00e+00 0.00e+00] [ 0.00e+00 2.00e+00 3.00e+00 0.00e+00] [ 0.00e+00 0.00e+00 4.00e+00 0.00e+00] [ 0.00e+00 0.00e+00 0.00e+00 0.00e+00] >>> B.V = matrix([1., 7., 8., 6., 4.]) # assign new values to nonzero entries >>> print B [ 1.00e+00 7.00e+00 0 0 ] [ 0 8.00e+00 6.00e+00 0 ] [ 0 0 4.00e+00 0 ] [ 0 0 0 0 ] >>> B.V += 1.0 # add 1 to the nonzero entries >>> print B [ 2.00e+00 8.00e+00 0 0 ] [ 0 9.00e+00 7.00e+00 0 ] [ 0 0 5.00e+00 0 ] [ 0 0 0 0 ] |
The V, I and J attributes can be used for reading sparse matrices from or writing them to binary files. Suppose we want to write the matrix A defined above to a binary file.
>>> f = open(’test.bin’,’w’)
>>> A.V.tofile(f) >>> A.I.tofile(f) >>> A.J.tofile(f) >>> f.close() |
A sparse matrix can be created from this file as follows.
>>> f = open(’test.bin’,’r’)
>>> V = matrix(0.0, (5,1)); V.fromfile(f) >>> I = matrix(0, (5,1)); I.fromfile(f) >>> J = matrix(0, (5,1)); J.fromfile(f) >>> B = spmatrix(V, I, J) >>> print B [ 0.00e+00 0 0 ] [ 1.00e+00 2.00e+00 0 ] [ 0 3.00e+00 4.00e+00] |
Note that the pickle module provides a convenient alternative to this method.