英文:
How to vectorize a matrix variable in CVXPY?
问题
我可以使用CVXPY创建一个对称矩阵变量,方法是X = cp.Variable((size_n, size_n), symmetric=True)
。有没有一种方法可以将这个矩阵向量化,以便我可以获得一个大小为size_n*size_n的一维向量?
我想这样做是因为我想计算线性算子A作用在这个矩阵上的结果,即通过左乘A和这个向量化的矩阵向量相乘来实现。
例如,我想要最小化cvxpy.norm(A @ vec(X))
。然而,我找不到这样的向量化函数。我也不确定如何自己编写它。
也许可以只创建一个新的向量变量,并附加size_n*size_n个约束,将矩阵的每个条目设置为向量的对应元素。然而,这似乎不是一种非常优雅的方法,我担心这会影响性能。
英文:
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.
答案1
得分: 0
关于向量化函数,它包含在cvxpy库中。
请参见此处。
通过检查它的代码,可以观察到它实质上使用了reshape函数。
英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论