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:

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

>>> from cvxopt.base import matrix  
>>> a = matrix(range(6), (2,3), ’d’)  
>>> print a  
[ 0.00e+00  2.00e+00  4.00e+00]  
[ 1.00e+00  3.00e+00  5.00e+00]  
>>> from numpy import array  
>>> b = array(a)  
>>> b  
array([[ 0.  2.  4.]  
       [ 1.  3.  5.]])  
>>> a*b  
array([[  0.   4.  16.]  
       [  1.   9.  25.]])  
>>> from numpy import mat  
>>> c = mat(a)  
>>> c  
matrix([[ 0.  2.  4.]  
        [ 1.  3.  5.]])  
>>> a.T * c  
matrix([[  1.,   3.,   5.],  
        [  3.,  13.,  23.],  
        [  5.,  23.,  41.]])

In the first product, a*b is interpreted as NumPy array multiplication, i.e., componentwise multiplication. The second product a.T*c is interpreted as NumPy matrix multiplication, i.e., standard matrix multiplication.