0
votes

I am trying to create a cvxopt.spmatrix object (see cvxopt doc) in Julia using PyCall. However, if I run the following code:

using PyCall
@pyimport cvxopt as cvx
I = [0.0 1 3 1 5 2 6 3 4 5 4 5 6 5 6]
J = [0.0 0 0 1 1 2 2 3 3 3 4 4 4 5 6]
B = cvx.spmatrix(0.1,I,J)

I get the following error message:

ERROR: LoadError: PyError (ccall(@pysym(:PyObject_Call), PyPtr, (PyPtr, PyPtr, PyPtr), o, arg, C_NULL)) TypeError('invalid array type',)

I believe this is because the PyCall Wrapper converts the Julia arrays I,J to a Python array that is incompatible with the spmatrix constructor. I think it wants a Python-list.

I know there is a Julia interface for cvx, but I need the spmatrix for a different purpose. Any ideas how to solve this? Thank you very much for your help!

1
What should this code achive (floats in indexing arrays!)? And what is your special purpose? - sascha
I essentially want to use the chompack (Chordal Matrix Package) library to test some code that I wrote in Julia. However that library requires me to create cvxopt.spmatrix objects. - miga89
I and J are index-arrays. There should be no float in it! If that's your problem i don't know. But that surely is problematic. - sascha

1 Answers

2
votes

There are two issues here:

  1. As @sascha pointed out I & J are indexes so need to be lists of Integers (as specified in the linked manual).
  2. The second issue I don't fully understand but there is an easy (if potentially not performant workaround).

B = cvx.spmatrix(0.1,Int.(I), Int.(J)) will give:

ERROR: PyError (ccall(@pysym(:PyObject_Call), PyPtr, (PyPtr, PyPtr, PyPtr), o, arg, C_NULL)) TypeError('buffer format not supported',)

This is likely something to do with the fact that PyCall doen't copy these arrays but passes them directly (usually a good thing). A hacky workaround would be to do this (assuming I and J are now Integer Arrays):

B = cvx.spmatrix(0.1, (I...), (J...))

PyObject <7x7 sparse matrix, tc='d', nnz=15>

There might be a better way though.