A dense matrix is created by calling the function matrix(). The arguments specify the values of the coefficients, the dimensions, and the type (integer, double or complex) of the matrix.
matrix(x[, size[, tc]])
size is a tuple of length two with the matrix dimensions. The number of rows and/or the number of columns can be zero.
tc stands for typecode. The possible values are ’i’, ’d’ and ’z’, for integer, real (double) and complex matrices, respectively.
x can be a number, a sequence of numbers, a dense or sparse matrix, a one- or two-dimensional NumPy array, or a list of lists of matrices and numbers.
>>> from cvxopt import matrix
>>> A = matrix(1, (1,4)) >>> print A [ 1 1 1 1] >>> A = matrix(1.0, (1,4)) >>> print A [ 1.00e+00 1.00e+00 1.00e+00 1.00e+00] >>> A = matrix(1+1j) >>> print A [ 1.00e+00+j1.00e+00] |
The following example shows several ways to define the same integer matrix.
>>> A = matrix([0, 1, 2, 3], (2,2))
>>> A = matrix((0, 1, 2, 3), (2,2)) >>> A = matrix(xrange(4), (2,2)) >>> from array import array >>> A = matrix(array(’i’, [0,1,2,3]), (2,2)) >>> print A [ 0 2] [ 1 3] |
>>> A = matrix([1., 2., 3., 4., 5., 6.], (2,3))
>>> print A [ 1.00e+00 3.00e+00 5.00e+00] [ 2.00e+00 4.00e+00 6.00e+00] >>> B = matrix(A, (3,2)) >>> print B [ 1.00e+00 4.00e+00] [ 2.00e+00 5.00e+00] [ 3.00e+00 6.00e+00] >>> C = matrix(B, tc=’z’) >>> print C [ 1.00e+00-j0.00e+00 4.00e+00-j0.00e+00] [ 2.00e+00-j0.00e+00 5.00e+00-j0.00e+00] [ 3.00e+00-j0.00e+00 6.00e+00-j0.00e+00] >>> from numpy import array >>> x = array([[1., 2., 3.], [4., 5., 6.]]) >>> x array([[ 1. 2. 3.] [ 4. 5. 6.]]) >>> print matrix(x) [ 1.00e+00 2.00e+00 3.00e+00] [ 4.00e+00 5.00e+00 6.00e+00] |
>>> print matrix([[1., 2.], [3., 4.], [5., 6.]])
[ 1.00e+00 3.00e+00 5.00e+00] [ 2.00e+00 4.00e+00 6.00e+00] >>> A1 = matrix([1, 2], (2,1)) >>> B1 = matrix([6, 7, 8, 9, 10, 11], (2,3)) >>> B2 = matrix([12, 13, 14, 15, 16, 17], (2,3)) >>> B3 = matrix([18, 19, 20], (1,3)) >>> C = matrix([[A1, 3.0, 4.0, 5.0], [B1, B2, B3]]) >>> print C [ 1.00e+00 6.00e+00 8.00e+00 1.00e+01] [ 2.00e+00 7.00e+00 9.00e+00 1.10e+01] [ 3.00e+00 1.20e+01 1.40e+01 1.60e+01] [ 4.00e+00 1.30e+01 1.50e+01 1.70e+01] [ 5.00e+00 1.80e+01 1.90e+01 2.00e+01] |
A matrix with a single block-column can be represented by a single list (i.e., if x is a list of lists, and has length one, then the argument x can be replaced by x[0]).
>>> D = matrix([B1, B2, B3])
>>> print D [ 6 8 10] [ 7 9 11] [ 12 14 16] [ 13 15 17] [ 18 19 20] |