x[, size[, tc]]) |
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 two-dimensional numarray array, or a list of lists of matrices and numbers.
'd'
, and from integer or
double to complex when used to create a matrix of type 'z'
).
>>> from cvxopt.base import matrix >>> A = matrix(1, (1,4)) >>> print A 1 1 1 1 >>> A = matrix(1.0, (1,4)) >>> print A 1.0000e+00 1.0000e+00 1.0000e+00 1.0000e+00 >>> A = matrix(1+1j) >>> print A 1.0000e+00+j1.0000e+00
'i'
is used).
Type conversion takes place as for scalar x.
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
'i'
,
'd'
or 'z'
, then the coefficients of x are copied, in
column-major order, to a new matrix of the given size.
The total number of elements in the new matrix (the product of
size[0] and size[1]) must be the same as the product of
the dimensions of x. If size is not specified, the
dimensions of x are used.
The default value of tc is the type of x.
Type conversion takes place when the type of x differs from
tc, in a similar way as for scalar x.
>>> A = matrix([1., 2., 3., 4., 5., 6.], (2,3)) >>> print A 1.0000e+00 3.0000e+00 5.0000e+00 2.0000e+00 4.0000e+00 6.0000e+00 >>> B = matrix(A, (3,2)) >>> print B 1.0000e+00 4.0000e+00 2.0000e+00 5.0000e+00 3.0000e+00 6.0000e+00 >>> C = matrix(B, tc='z') >>> print C 1.0000e+00-j0.0000e+00 4.0000e+00-j0.0000e+00 2.0000e+00-j0.0000e+00 5.0000e+00-j0.0000e+00 3.0000e+00-j0.0000e+00 6.0000e+00-j0.0000e+00 >>> from numarray import array >>> x = array([1., 2., 3., 4., 5., 6.], shape=(2,3)) >>> print x [[ 1. 2. 3.] [ 4. 5. 6.]] >>> y = matrix(x) >>> print y 1.0000e+00 2.0000e+00 3.0000e+00 4.0000e+00 5.0000e+00 6.0000e+00
len(x)
block-columns.
If size is specified, then the matrix with len(x)
block-columns is resized by copying its elements in column-major order
into a matrix of the dimensions given by size.
If tc is not specified, it is determined from the elements of
x (and if that is impossible, for example because x is
a list of empty lists, a value 'i'
is used).
The same rules for type conversion apply as for scalar x.
>>> A = matrix([[1., 2.], [3., 4.], [5., 6.]]) >>> print A 1.0000e+00 3.0000e+00 5.0000e+00 2.0000e+00 4.0000e+00 6.0000e+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)) >>> print matrix([[A1, 3.0, 4.0, 5.0], [B1, B2, B3]]) 1.0000e+00 6.0000e+00 8.0000e+00 1.0000e+01 2.0000e+00 7.0000e+00 9.0000e+00 1.1000e+01 3.0000e+00 1.2000e+01 1.4000e+01 1.6000e+01 4.0000e+00 1.3000e+01 1.5000e+01 1.7000e+01 5.0000e+00 1.8000e+01 1.9000e+01 2.0000e+01
A matrix with a single block-column can be represented by a single
list (i.e., when the length of x is one, it can be replaced with
x[0]
).
>>> print matrix([B1, B2, B3]) 6 8 10 7 9 11 12 14 16 13 15 17 18 19 20