2.8 The NumPy Array Interface

The CVXOPT matrix object is compatible with the NumPy Array Interface, which allows Python objects that represent multidimensional arrays to exchange data using information stored in the attribute __array_struct__.

See Also:

NumPy Array Interface Specification

NumPy home page

As already mentioned in section 2.1, a two-dimensional array object (for example, a 2D numarray array) can be converted to a matrix object by using the matrix() constructor. Conversely, CVXOPT matrices can be used as array-like objects in numarray. The following example illustrates the compatibility of CVXOPT matrices and numarray arrays.

>>> from cvxopt import matrix
>>> a = matrix(range(6), (2,3), 'd')
>>> print a
   0.0000e+00   2.0000e+00   4.0000e+00
   1.0000e+00   3.0000e+00   5.0000e+00
>>> from numarray import array
>>> b = array(a)
>>> print b
[[ 0.  2.  4.]
 [ 1.  3.  5.]]
>>> print a*b
[[  0.   4.  16.]
 [  1.   9.  25.]]
In the last expression a*b is interpreted as a numarray multiplication (i.e., componentwise) even though one operand is a matrix object.