pythonoptimizationcvxpyconvex-optimization

How to vectorize a matrix variable in CVXPY?


I can create a symmetric matrix variable in CVXPY with X = cp.Variable((size_n, size_n), symmetric=True). Is there a way to vectorize the matrix so that I can obtain a size_n*size_n by 1 vector?

I want to do this because I want to calculate the result of a linear operator A that acts on this matrix, which is left by multiplying A with this vectorized matrix vector.

For instance, I want to minimize cvxpy.norm(A @ vec(X)). However, I cannot find such a vectorization function. I am not sure how to code it up by myself either.

It is possible to just create a new vector variable and append size_n * size_n constraints setting each entry of the matrix equal to the vector. However, this doesn't seem to be a very elegant way to do this and I'm afraid it will affect the performance.


Solution

  • For the vectorization function, it is contained in the cvxpy library. See here.

    By inspecting its code, it can be observed that it in essence uses the reshape function.