eigen 矩阵求逆_【Eigen】从入门到放弃(二):矩阵&向量的运算

本文介绍了Eigen库中矩阵和向量的基本运算,包括加减法、数乘、转置、共轭、逆矩阵、矩阵与向量乘法、点乘和叉乘等操作,并通过实例代码展示其使用方法。
摘要由CSDN通过智能技术生成

在【Eigen】从入门到放弃(一):Eigen是个什么鬼?,鸽粗略做了一个Eigen非常浅薄的入门知识笔记。接下来这篇将做下矩阵和向量的运算问题相关的笔记。

1.简单的四则运算

(1)加减法

I. 编写程序

#include #include using namespace std;

using namespace Eigen;

int main()

{

Matrix2d a;

a << 1, 2,

3, 4;

MatrixXd b(2,2);

b << 2, 3,

1, 4;

cout << "a + b =\n" << a + b << endl;//矩阵加法 cout << "a - b =\n" << a - b << endl;//矩阵减法 cout << "Doing a += b;" << endl;

a += b;//a = a + b,同时重新赋值a cout << "Now a =\n" << a << endl;

Vector3d v(1,2,3);

Vector3d w(1,0,0);

cout << "-v + w - v =\n" << -v + w - v << endl;//向量加减法}

II. 运行结果

a + b =

3 5

4 8

a - b =

-1 -1

2 0

Doing a += b;

Now a =

3 5

4 8

-v + w - v =

-1

-4

-6

(2)数乘

*在3中介绍“矩阵-矩阵乘法”和“矩阵-向量乘法”

I. 编写程序

#include #include using namespace std;

using namespace Eigen;

int main()

{

Matrix2d a;

a << 1, 2,

3, 4;

Vector3d v(1,2,3);

cout << "a * 2.5 =\n" << a * 2.5 << endl;//矩阵数乘 cout << "0.1 * v =\n" << 0.1 * v << endl;//向量数乘 cout << "Doing v *= 2;" << endl;

v *= 2;//向量数乘,从新赋值v cout << "Now v =\n" << v << endl;

}

II. 运行结果

a * 2.5 =

2.5 5

7.5 10

0.1 * v =

0.1

0.2

0.3

Doing v *= 2;

Now v =

2

4

6

2.转置矩阵(

),共轭矩阵(

),逆矩阵(

)

I. 编写程序

#include #include using namespace std;

using namespace Eigen;

int main()

{

MatrixXcf a = MatrixXcf::Random(2,2);//定义2-by-2随机矩阵 cout << "Here is the matrix a\n" << a << endl;//矩阵a cout << "Here is the matrix a^T\n" << a.transpose() << endl;//a的转置 cout << "Here is the matrix a^H\n" << a.conjugate() << endl;//a的共轭 cout << "Here is the matrix a^{-1}\n" << a.inverse() << endl;//a的逆}

II. 运行结果

Here is the matrix a

(-0.211234,0.680375) (-0.604897,0.823295)

(0.59688,0.566198) (0.536459,-0.329554)

Here is the matrix a^T

(-0.211234,0.680375) (0.59688,0.566198)

(-0.604897,0.823295) (0.536459,-0.329554)

Here is the matrix a^H

(-0.211234,-0.680375) (-0.604897,-0.823295)

(0.59688,-0.566198) (0.536459,0.329554)

Here is the matrix a^{-1}

(0.425416,-0.480856) (0.345496,-0.982836)

(-0.750469,-0.375009) (-0.0039331,0.726466)

2.矩阵&向量乘法

I. 编写程序

#include #include using namespace std;

using namespace Eigen;

int main()

{

Matrix2d m;

m << 1, 2,

3, 4;

Vector2d u(-1,1), v(2,0);

cout << "Here is m*m:\n" << m*m << endl;//矩阵-矩阵 cout << "Here is m*u:\n" << m*u << endl;//矩阵-向量 cout << "Here is u^T*m:\n" << u.transpose()*m << endl;//向量-矩阵 cout << "Here is u^T*v:\n" << u.transpose()*v << endl;//向量-向量 cout << "Here is u*v^T:\n" << u*v.transpose() << endl;//向量-向量 cout << "Let's multiply m by itself" << endl;

m = m*m;//矩阵-矩阵 cout << "Now m is:\n" << m << endl;

}

II. 运行结果

Here is m*m:

7 10

15 22

Here is m*u:

1

1

Here is u^T*m:

2 2

Here is u^T*v:

-2

Here is u*v^T:

-2 -0

2 0

Let's multiply m by itself

Now m is:

7 10

15 22

3.点乘&叉乘

I. 编写程序

#include

#include

using namespace std;

using namespace Eigen;

int main()

{

Vector3d v(1,2,3);

Vector3d w(0,1,2);

cout << "Dot product: " << v.dot(w) << endl;//向量点乘

cout << "Cross product:\n" << v.cross(w) << endl;//向量叉乘

}

II. 运行结果

Dot product: 8

Cross product:

1

-2

1

4.其他

I. 编写程序

#include #include using namespace Eigen;

using namespace std;

int main()

{

Matrix3d m;

m << 1, 2, 3,

1, 2, 1,

0, 2, 4;

cout << "Here is m.determinant(): " << m.determinant() << endl;// 行列式 cout << "Here is m.sum(): " << m.sum() << endl;//所有元素之和 cout << "Here is m.prod(): " << m.prod() << endl;//所有元素之积 cout << "Here is m.mean(): " << m.mean() << endl;//元素的平均数 cout << "Here is m.minCoeff(): " << m.minCoeff() << endl;//最小元素 cout << "Here is m.maxCoeff(): " << m.maxCoeff() << endl;//最大元素 cout << "Here is m.trace(): " << m.trace() << endl;//迹//""里的空格可以让输出结果对齐}

II. 运行结果

Here is m.determinant(): 4

Here is m.sum(): 16

Here is m.prod(): 0

Here is m.mean(): 1.77778

Here is m.minCoeff(): 0

Here is m.maxCoeff(): 4

Here is m.trace(): 7

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值