It is known that Numpy basic matrix slicing will generate a view, whereas advanced slicing a copy. Is this true in cvxopt? I tried

from cvxopt import spmatrix
import numpy as np
A = spmatrix([2,-1,2,-2,1,4,3], [1,2,0,2,3,2,0], [0,0,1,1,2,3,4])
A_view = A[0:3,0:3]           # basic slicing
A_cp = A[[0,1,2],[0,1,2]]     # advanced slicing
np.may_share_memory(A,A_view) # returned false
np.may_share_memory(A,A_cp)   # returned false

That is a sparse matrix. Numpy doesn't have sparse matrices, and I think neither scipy.sparse nor cvxopt use memoryviews for sparse matrices.

Even for dense matrices it appears that cvxopt uses a low level Python API instead of something like Cython. I don't see any references to memoryview interfaces so perhaps even dense matrices in cvxopt use copying instead of views.

This might be the relevant function for sparse matrix indexing in the cvxopt source, where you will find a lot of copying. https://github.com/cvxopt/cvxopt/blob/master/src/C/sparse.c#L2791

  • $\begingroup$ Thanks, this really helps. I need a package that can retrieve sparse submatrix view and armadillo looks suitable for my purpose: arma.sourceforge.net/docs.html#SpMat. $\endgroup$ – zhh210 Dec 28 '13 at 3:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.