妈妈再也不用担心我的矩阵运算了!Mosek学习笔记5,矩阵。

Mosek学习笔记系列就要接近尾声了。

(Mosek学习笔记系列之前的链接:https://blog.csdn.net/aliexken/article/details/104443873

之所以把矩阵放的比较靠后,是因为其重要性。作为优化过程中承载数据的载体,矩阵的相关运算直接决定了建立优化问题模型的方式。经过研究后,非常遗憾,我还是没能找到能够舍弃Eigen的Mosek矩阵运算方法。Expr提供的矩阵运算形式,更多的是一种方程约束形式的表示,而不是直接的矩阵运算。即你不能通过Expr直接算出两个矩阵的和,但是却可以表示两个矩阵的和,并放入约束中,使用优化来获取值。感觉很绕,我猜想这和mosek在设计之初,以优化为第一目的的设计理念有关。

尽管如此,我还是非常认真的做了mosek学习笔记的原因,该工具还是能够将矩阵与线性代数运算和优化系统结合在一起,提供了完备的解决方案,而且最重要的是,提供了良好的C++接口!对基于VS开发C++算法的我来说简直就是福音!

闲话少说,上干货。

1. 矩阵初始化

矩阵初始化可以分为稠密矩阵和稀疏矩阵两种

预定了shape形态的矩阵。

auto A = new_array_ptr<double,2>({ {1,2,3,4}, {5,6,7,8} });
auto Ad= Matrix::dense(A);

基于一维向量模式。

auto Af = new_array_ptr<double,1>({ 1,2,3,4,5,6,7,8 });
auto Aff= Matrix::dense(2,4,Af);

稀疏矩阵

 auto rows   = new_array_ptr<int,1>({ 0, 0, 1, 1 });
 auto cols   = new_array_ptr<int,1>({ 0, 3, 1, 3 });
 auto values = new_array_ptr<double,1>({ 1.0, 2.0, 3.0, 4.0 });    
 auto ms = Matrix::sparse(rows->size(), cols->size(), rows, cols, values);
Members:
 

Matrix.get – Get a single entry 获得唯一入口。

Matrix.getDataAsArray – Return a dense array of values 返回一个稠密数组的值。

Matrix.getDataAsTriplets – Return the matrix data in sparse triplet format 返回矩阵数据的稀疏三重格式。

Matrix.isSparse – Returns true if the matrix is sparse. 判断是否稀疏。

Matrix.numColumns – Returns the number of columns in the matrix 返回矩阵中的列。

Matrix.numNonzeros – Returns the number of non-zeros in the matrix 返回矩阵中非0的数目。

Matrix.numRows – Returns the number of rows in the matrix 返回矩阵中的行。

Matrix.toString – Get a string representation of the matrix 获得。

Matrix.transpose – Transpose the matrix 转置.

矩阵转置的方法有点怪,给一个实例

//vector<vector<double>> LX

auto LX_Ma = monty::new_array_ptr<double>(LX);         
auto LX_M = mosek::fusion::Matrix::dense(LX_Ma);
auto LX_M_T = mosek::fusion::Matrix::t(LX_M)->transpose();

Static members:
 

Matrix.antidiag – Create a sparse square matrix with a given vector as anti-diagonal

用给定的矢量创建一个稀疏的正方形矩阵作为对角线。

Matrix.dense – Create a dense matrix from the given data.

从提供的数据中创建一个稠密矩阵。

Matrix.diag – Create a sparse square matrix with a given vector as diagonal.

用给定的矢量创建一个稀疏的正方形矩阵作为对角线

Matrix.eye – Create the identity matrix.

创建单位矩阵

Matrix.ones – Create a matrix filled with all ones.

创建一个全是1的矩阵

Matrix.sparse – Create a sparse matrix from the given data.

利用给定数据创建一个稀疏矩阵

 

总结起来,也就是一个转置矩阵操作。

2. 结合线性代数的运算

Expr.addElement-wise addition of two matrices 加法
Expr.subElement-wise subtraction of two matrices 减法
Expr.mulMatrix or matrix-scalar multiplication 乘法
Expr.negSign inversion 符号反转
Expr.outerVector outer-product  适量外积
Expr.dotDot product 点积
Expr.sumSum over a given dimension 给定维度的总和
Expr.mulElmElement-wise multiplication 逐元素乘法
Expr.mulDiag

Sum over the diagonal of a matrix which is the result of a matrix multiplication 

矩阵对角线的总和,这是矩阵相乘的结果

Expr.constTermReturn a constant term 返回常数项
Expr::add(Expr::mul(A,x), Expr::mul(B,y));
Expr::add(new_array_ptr<Variable::t,1>({x, y, z, w}));

3. 约束域Domain的运算

*Domain.equalsTo – Defines the domain consisting of a fixed point. 创建一个等式

*Domain.greaterThan – Defines the domain specified by a lower bound in each dimension. 创建一个大于不等式

*Domain.lessThan – Defines the domain specified by an upper bound in each dimension. 建一个小于不等式

Domain.axis – Set the dimension along which the cones are created. 在建立锥优化时设置维度

Domain.binary – Creates a domain of binary variables. 创建一个二进制变量域

Domain.inDExpCone – Defines the dual exponential cone. 定义双指数锥

Domain.inDPowerCone – Defines the dual power cone.  定义双幂锥

Domain.inPExpCone – Defines the primal exponential cone. 定义原始指数锥

Domain.inPPowerCone – Defines the primal power cone. 定义原始幂锥

Domain.inPSDCone – Creates a domain of Positive Semidefinite matrices. 创建一个正半定矩阵的域

Domain.inQCone – Defines the domain of quadratic cones. 定义二次锥的域

Domain.inRange – Creates a domain specified by a range in each dimension. 创建一个由每个维度中的范围指定的域

Domain.inRotatedQCone – Defines the domain of rotated quadratic cones.

Domain.integral – Creates a domain of integral variables.

Domain.isLinPSD – Creates a domain of Positive Semidefinite matrices.

Domain.isTrilPSD – Creates a domain of Positive Semidefinite matrices.

Domain.sparse – Use a sparse representation.

Domain.symmetric – Impose symmetry on a given linear domain.

Domain.unbounded – Creates a domain in which variables are unbounded.

有了矩阵的初始化,线性代数计算以及约束域方法,我们就可以建立相应的约束规则。

在实际编程的时候,发现这个矩阵计算还是有点问题,Expr这个线性代数的计算不是直接针对矩阵的,看起来像是针对优化问题的,所以还是不能像eigen一样那么方便,除非是所有的矩阵都求好了才能够列在限制条件里,这个确实有点坑。如果有空的话,在补一期mosek与eigen配合使用的说明教程。

上面所有的Mosek介绍全部都是基于Fusion Model来进行的,也就是:MOSEK Fusion API for C++ 9.1.13。当我真正使用这套工具来做多元优化问题的时候,我傻了!使用几何规划实在是非常麻烦,在经过一天半的努力尝试后,我几乎要放弃了,都准备要转qpOASES学习。所谓踏破铁鞋无觅处,我找到了MOSEK Optimizer API for C 9.1.13。我原来以为Mosek的功能实现是相同的,只不过是基于C++或C的编译器,做不同的接口罢了,但是!我看了MOSEK Optimizer API for C 9.1.13,这里分明有多元优化问题的成熟解决方案!好吧,哥们今天就跟你干上了,新开一个mosek学习笔记! 

 

 

 

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

程序猿老甘

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值