|
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 functionsMatrices can be used in:
Various other functions may be applied to matrices, too.
Matrix indexing and slicingMatrices 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 matricesCVXMOD provides wrappers to CVXOPT’s random number generators:
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 functionsCVXMOD provides functions for producing various special matrices.
|