An spmatrix object corresponds to a particular triplet description of a sparse matrix. We will refer to the entries in the triplet description as the nonzero entries of the object, even though they may have a numerical value zero.
Two functions are provided to create sparse matrices. The first, spmatrix(), constructs a sparse matrix from a triplet description.
x, I, J[, size[, tc]]) |
I and J are sequences of integers (lists, tuples,
array arrays, xrange objects, ...) or integer matrices
(matrix objects with typecode 'i'
), containing the row and column
indices of the nonzero entries.
The lengths of I and J must be equal. If they
are matrices, they are treated as lists of indices stored in
column-major order, i.e., as lists list(I)
, respectively,
list(J)
.
size is a tuple of nonnegative integers with the row and column
dimensions of the matrix.
The size argument is only needed when creating a matrix with
a zero last row or last column. If size is not specified, it
is determined from I and J:
the default value for size[0]
is max(I)+1
if I is nonempty and zero otherwise.
The default value for size[1]
is
max(J)+1
if J is nonempty and zero otherwise.
tc is the typecode, 'd'
or 'z'
, for double and complex
matrices, respectively. Integer sparse matrices are not implemented.
x can be a number, a sequence of numbers, or a dense matrix. This argument specifies the numerical values of the nonzero entries.
'd'
if x is integer or float,
and 'z'
if x is complex.
The following code creates a 4 by 4 sparse identity matrix.
>>> from cvxopt.base import spmatrix >>> A = spmatrix(1.0, range(4), range(4)) >>> print A SIZE: (4,4) (0, 0) 1.0000e+00 (1, 1) 1.0000e+00 (2, 2) 1.0000e+00 (3, 3) 1.0000e+00
'd'
if x contains integers and floating-point numbers or
if x is an empty list,
and 'z'
if x contains at least one complex number.
As an example, the matrix (6.1) can be created as follows.
>>> A = spmatrix([2,-1,2,-2,1,4,3], [1,2,0,2,3,2,0], [0,0,1,1,2,3,4]) >>> print A SIZE: (4,5) (1, 0) 2.0000e+00 (2, 0) -1.0000e+00 (0, 1) 2.0000e+00 (2, 1) -2.0000e+00 (3, 2) 1.0000e+00 (2, 3) 4.0000e+00 (0, 4) 3.0000e+00
'd'
if x is an 'i'
or 'd'
matrix, and 'z'
otherwise.
If I and J contain repeated entries, the corresponding values of the coefficients are added.
The function sparse() constructs a sparse matrix from a block-matrix description.
x[, tc]) |
'd'
or 'z'
, for double and complex
matrices, respectively.
x can be a matrix, spmatrix, or a list of lists of matrices (matrix or spmatrix objects) and numbers (Python integer, float or complex).
len(x)
block-columns
(as in matrix(), see section 2.1).
Numerical zeros are removed from the triplet description of the new
matrix.
The following example shows how to construct a sparse block-matrix.
>>> from cvxopt.base import matrix, spmatrix, sparse >>> A = matrix([[1, 2, 0], [2, 1, 2], [0, 2, 1]]) >>> B = spmatrix([], [], [], (3,3)) >>> C = spmatrix([3, 4, 5], [0, 1, 2], [0, 1, 2]) >>> print sparse([[A, B], [B, C]]) SIZE: (6,6) (0, 0) 1.0000e+00 (1, 0) 2.0000e+00 (0, 1) 2.0000e+00 (1, 1) 1.0000e+00 (2, 1) 2.0000e+00 (1, 2) 2.0000e+00 (2, 2) 1.0000e+00 (3, 3) 3.0000e+00 (4, 4) 4.0000e+00 (5, 5) 5.0000e+00
A matrix with a single block-column can be represented by a single list.
>>> print sparse([A, C]) SIZE: (6,3) (0, 0) 1.0000e+00 (1, 0) 2.0000e+00 (3, 0) 3.0000e+00 (0, 1) 2.0000e+00 (1, 1) 1.0000e+00 (2, 1) 2.0000e+00 (4, 1) 4.0000e+00 (1, 2) 2.0000e+00 (2, 2) 1.0000e+00 (5, 2) 5.0000e+00