Creating matrices
How to create dense and sparse matrices
CVXOPT has separate dense and sparse matrix objects. This example illustrates different ways to create dense and sparse matrices.
A dense matrix is created using the matrix() function; it can be created from a list (or iterator):
>>> from cvxopt.base import matrix
>>> A = matrix([1.0, 2.0, 3.0, 4.0, 5.0, 6.0], (2,3))
>>> print A
[ 1.00e+00 3.00e+00 5.00e+00]
[ 2.00e+00 4.00e+00 6.00e+00]
>>> A.size
(2, 3)
or from a list of lists, where each inner list represents a column of the resulting matrix:
>>> B = matrix([ [1.0, 2.0], [3.0, 4.0] ])
>>> print B
[ 1.00e+00 3.00e+00]
[ 2.00e+00 4.00e+00]
More generally, the inner lists can represent block-columns:
>>> print matrix([ [A] ,[B] ])
[ 1.00e+00 3.00e+00 5.00e+00 1.00e+00 3.00e+00]
[ 2.00e+00 4.00e+00 6.00e+00 2.00e+00 4.00e+00]
A matrix can also be created from a Numpy array:
>>> from numpy import array
>>> C = array([ [7.0, 8.0, 9.0], [10.0, 11.0, 12.0] ])
>>> print C
[[ 7. 8. 9.]
[ 10. 11. 12.]]
>>> from cvxopt.base import spmatrixSparse matrices can also be constructed by concatenating other matrices using the sparse() function:
>>> D = spmatrix([1., 2.], [0, 1], [0, 1], (4,2))
>>> print D
[ 1.00e+00 0 ]
[ 0 2.00e+00]
[ 0 0 ]
[ 0 0 ]
>>> print matrix(D)
[ 1.00e+00 0.00e+00]
[ 0.00e+00 2.00e+00]
[ 0.00e+00 0.00e+00]
[ 0.00e+00 0.00e+00]
>>> from cvxopt.base import sparse
>>> E = sparse([ [B, B], [D] ])
>>> print E
[ 1.00e+00 3.00e+00 1.00e+00 0 ]
[ 2.00e+00 4.00e+00 0 2.00e+00]
[ 1.00e+00 3.00e+00 0 0 ]
[ 2.00e+00 4.00e+00 0 0 ]
Sparse block-diagonal matrices can be constructed using the spdiag() function:
>>> from cvxopt.base import spdiag
>>> print spdiag([B, -B, 1, 2])
[ 1.00e+00 3.00e+00 0 0 0 0 ]
[ 2.00e+00 4.00e+00 0 0 0 0 ]
[ 0 0 -1.00e+00 -3.00e+00 0 0 ]
[ 0 0 -2.00e+00 -4.00e+00 0 0 ]
[ 0 0 0 0 1.00e+00 0 ]
[ 0 0 0 0 0 2.00e+00]