Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python3 conversion between cvxopt.matrix and numpy.array

python: python3.2 cvxopt: 1.1.5 numpy: 1.6.1

I read http://abel.ee.ucla.edu/cvxopt/examples/tutorial/numpy.html

import cvxopt
import numpy as np
cvxopt.matrix(np.array([[7, 8, 9], [10, 11, 12]]))

I got

Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: non-numeric element in list

By np.array(cvxopt.matrix([[7, 8, 9], [10, 11, 12]])), I got

array([[b'\x07', b'\n'],
   [b'\x08', b'\x0b'],
   [b'\t', b'\x0c']], 
  dtype='|S8')
like image 710
updogliu Avatar asked Feb 20 '23 02:02

updogliu


1 Answers

As of cvxopt == 1.2.6 and numpy == 1.21.2:

import cvxopt
import numpy as np

matrix = cvxopt.matrix(np.array([[7, 8, 9], [10, 11, 12]]))
print(matrix)

produces the output:

[  7   8   9]
[ 10  11  12]

and print(repr(matrix)) says:

<2x3 matrix, tc='i'>

and print(type(matrix)) says:

<class 'cvxopt.base.matrix'>

The resulting matrix has integer type (the 'i') because the starting numpy array contained integers. Starting with double results in a 'd' type.

like image 142
Ioannis Filippidis Avatar answered Feb 24 '23 00:02

Ioannis Filippidis