2.1 Creating Matrices
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.
- If x is a number (Python integer, float or complex), a matrix
is created with the dimensions specified by size and with all the
coefficients equal to x.
The default value of size is (1,1), and the default value
of tc is the type of x.
If necessary, the type of x is converted (from integer to double
when used to create a matrix of type '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.00e+00 1.00e+00 1.00e+00 1.00e+00]
>>> A = matrix(1+1j)
>>> print A
[ 1.00e+00+j1.00e+00] - If x is a sequence of numbers (list, tuple, array
array, xrange object, one-dimensional NumPy array, ...),
then the numbers are interpreted as the coefficients of a matrix in
column-major order. The length of x must be equal to the product
of size[0] and size[1].
If size is not specified, a matrix with one column is created.
If tc is not specified, it is determined from the elements of
x (and if that is impossible, for example because x is
an empty list, a value '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] - If x is a dense or sparse matrix (a matrix or a spmatrix
object), or a two-dimensional NumPy array of type '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.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] - If x is a list of lists of matrices
(matrix or spmatrix objects) or numbers (Python integer, float or
complex), then each element of x is interpreted as a
block-column stored in column-major order.
If size is not specified, the block-columns are juxtaposed
to obtain a matrix with 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.
>>> 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., when the length of x is one, it can be replaced with x[0]).
>>> D = matrix([B1, B2, B3])
>>> print D
[ 6 8 10]
[ 7 9 11]
[ 12 14 16]
[ 13 15 17]
[ 18 19 20]
- If x is a number (Python integer, float or complex), a matrix
is created with the dimensions specified by size and with all the
coefficients equal to x.
The default value of size is (1,1), and the default value
of tc is the type of x.
If necessary, the type of x is converted (from integer to double
when used to create a matrix of type 'd', and from integer or
double to complex when used to create a matrix of type 'z').