CVXMOD – Matrices

Alert!

CVXMOD is discontinued!

Unfortunately CVXMOD has not been developed since 2008. It is incomplete, somewhat buggy and not supported. Please instead try the replacement package CVXPY. Or, try CVXOPT if you are prepared to work at a lower level, CVX if you are ok with Matlab, or CVXGEN if you want small (but super high speed) QP solvers.

Matrices are represented using the matrix class provided by CVXOPT. Matrices are created by specifying a list of (column-by-column) entries, followed by a tuple describing the matrix size. Omitting the size will result in a column vector.

There is currently no support for sparse or complex-valued matrices. This will be addressed at some stage.

Syntax

matrix(values, size=len(rows, 1))

Example: Simple matrices
>>> matrix((1.1, 2, 3.8, 4, 5, 6), (3, 2))

[   1.100    4.000]
[   2.000    5.000]
[   3.800    6.000]

>>> matrix((1.1, 2, 3.8, 4))

[   1.100]
[   2.000]
[   3.800]
[   4.000]

Automatic conversions to and from the NumPy type array are supported.

Example: NumPy conversions
>>> from numpy import array
>>> array(matrix((1.1, 2, 3.8, 4, 5, 6), (3, 2)))

array([[ 1.1,  4. ],
       [ 2. ,  5. ],
       [ 3.8,  6. ]])

>>> matrix(array((3.2, 4)))

[   3.200]
[   4.000]

Matrix functions

Matrices can be used in:

  • Arithmetic operations +, - or * between two matrices of appropriate sizes,

  • Elementwise scalar arithmetic operations +, -, * or /, and

  • Elementwise scalar exponentiation .

Various other functions may be applied to matrices, too.

  • transpose(mat) returns the matrix transpose of mat. (Also available as tp(mat).)

  • rows(mat) returns the number of rows of mat.

  • cols(mat) returns the number of columns of mat.

  • size(mat) returns a tuple equal to (rows(mat), cols(mat)).

  • is1x1(mat) returns True if mat is a scalar or a size (1, 1) matrix.

  • concathoriz(mat1, mat2) returns a horizontal concatenation of two matrices.

  • concatvert(mat1, mat2) returns a vertical concatenation of two matrices.

Matrix indexing and slicing

Matrices are indexed or sliced with one or two indices. Indices begin at 0.

Example: Matrix indexing
>>> A = matrix((1.1, 2, 3.8, 4, 5, 6), (3, 2))

>>> A

[   1.100    4.000]
[   2.000    5.000]
[   3.800    6.000]

>>> A[1]
2.0

>>> A[2,1]
6.0
Example: Matrix slicing
>>> A[2:4]

[   3.800]
[   4.000]

>>> A[0,:]

[   1.100    4.000]

>>> A[:,1]

[   4.000]
[   5.000]
[   6.000]

Randomly generated matrices

CVXMOD provides wrappers to CVXOPT's random number generators:

  • rand(rows, cols=rows, lowerlim=0, upperlim=1) returns a matrix with entries uniformly distributed between lowerlim and upperlim.

  • randn(rows, cols=rows, mean=0, std=1) returns a matrix with individual entries normally distributed with mean mean and standard deviation std.

  • randseed(s) seeds the random number generator. Omitting s uses a seed value from the system clock.

Example: Randomly generated matrices
>>> randseed(14)
>>> randn(3)

[   1.219   -1.939  -0.1466]
[  0.9686  -0.1017   -1.458]
[  -1.371   0.7619  -0.4962]

>>> rand(5, 1, -2, 0.5)

[  -1.008]
[-0.05621]
[  -1.704]
[  -1.442]
[  0.2537]

Utility functions

CVXMOD provides functions for producing various special matrices.

  • eye(n) returns the size (n, n) identity matrix.

  • ones(rows, cols=rows) returns an all-ones matrix.

  • zeros(rows, cols=rows) returns an all-zeros matrix.

  • zm() returns a matrix with size (0, 0), which can be useful when building a matrix with the concathoriz or concatvert functions.

  • linspace(start, end, N, includeend=True) returns a column matrix with N evenly spaced entries from start (inclusive) to stop (inclusive or exclusive).