如何在CVXPY中对矩阵变量进行向量化?

huangapple go评论79阅读模式
英文:

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.

huangapple
  • 本文由 发表于 2023年6月22日 03:58:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/76526736.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定