Dense and sparse matrices have the following attributes.
size
A tuple with the dimensions of the matrix. The size of the matrix can be changed by altering this attribute, as long as the number of elements in the matrix remains unchanged.
typecode
A char, either ’i’, ’d’, or ’z’, for integer, real, and complex matrices, respectively. A read-only attribute.
trans()
Returns the transpose of the matrix as a new matrix. One can also use A.T instead of A.trans().
ctrans()
Returns the conjugate transpose of the matrix as a new matrix. One can also use A.H instead of A.ctrans().
real()
For complex matrices, returns the real part as a real matrix. For integer and real matrices, returns a copy of the matrix.
imag()
For complex matrices, returns the imaginary part as a real matrix. For integer and real matrices, returns an integer or real zero matrix.
In addition, sparse matrices have the following attributes.
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 dense matrix with the row indices of the entries in V. A read-only attribute.
J
A single-column integer dense matrix with the column indices of the entries in V. A read-only attribute.
CCS
A triplet (column pointers, row indices, 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.
The next example below illustrates assignments to V.
>>> from cvxopt 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 ] >>> 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 ] |
The following attributes and methods are defined for dense matrices.
__array_struct__
A PyCObject implementing the NumPy array interface (see section 2.9 for details).
tofile(f)
Writes the elements of the matrix in column-major order to a binary file f.
fromfile(f)
Reads the contents of a binary file f into the matrix object.
The last two methods are illustrated in the following examples.
>>> from cvxopt import matrix, spmatrix
>>> A = matrix([[1.,2.,3.], [4.,5.,6.]]) >>> print A [ 1.00e+00 4.00e+00] [ 2.00e+00 5.00e+00] [ 3.00e+00 6.00e+00] >>> f = open(’mat.bin’,’w’) >>> A.tofile(f) >>> f.close() >>> B = matrix(0.0, (2,3)) >>> f = open(’mat.bin’,’r’) >>> B.fromfile(f) >>> f.close() >>> print B [ 1.00e+00 3.00e+00 5.00e+00] [ 2.00e+00 4.00e+00 6.00e+00] |
>>> A = spmatrix(range(5), [0,1,1,2,2], [0,0,1,1,2])
>>> f = open(’test.bin’,’w’) >>> A.V.tofile(f) >>> A.I.tofile(f) >>> A.J.tofile(f) >>> f.close() >>> 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 dump() and load() functions in the pickle module offer a convenient alternative for writing matrices to files and reading matrices from files.