Reputation: 6255
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')
Upvotes: 4
Views: 6192
Reputation: 11484
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.
Upvotes: 4
Reputation: 2805
While it is not fixed, a simple workaround for
cvxopt.matrix(nparray)
is
cvxopt.matrix(nparray.T.tolist())
It is more tough for the opposite direction. If you expect int array,
np.vectorize(lambda x: int.from_bytes(x, 'big'))(np.array(cvxoptmat).T)
For the double array:
import struct
np.vectorize(lambda x: struct.unpack('d', x))(np.array(cvxoptmat).T)
Upvotes: 2
Reputation: 928
Check the patched dense.c that I put up on the cvxopt discussion forum (https://groups.google.com/forum/?fromgroups=#!topic/cvxopt/9jWnkbJvk54). Recompile with this, and you will be able to convert np arrays to dense matrices. I assume the same kind of edits will be necessary for sparse matrices, but as I do not need them I will leave that up to the devs.
Upvotes: 2