Eigen官方教程:Eigen (Main Page)

操作環境:Ubuntu 16.04


在【Eigen】從入門到放棄(一):Eigen是個什麼鬼?,鴿粗略做了一個Eigen非常淺薄的入門知識筆記。接下來這篇將做下矩陣和向量的運算問題相關的筆記。

1.簡單的四則運算

(1)加減法

I. 編寫程序

#include <iostream>
#include <Eigen/Dense>
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 =
" << a + b << endl;//矩陣加法
cout << "a - b =
" << a - b << endl;//矩陣減法
cout << "Doing a += b;" << endl;
a += b;//a = a + b,同時重新賦值a
cout << "Now a =
" << a << endl;
Vector3d v(1,2,3);
Vector3d w(1,0,0);
cout << "-v + w - v =
" << -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 <iostream>
#include <Eigen/Dense>
using namespace std;
using namespace Eigen;
int main()
{
Matrix2d a;
a << 1, 2,
3, 4;
Vector3d v(1,2,3);
cout << "a * 2.5 =
" << a * 2.5 << endl;//矩陣數乘
cout << "0.1 * v =
" << 0.1 * v << endl;//向量數乘
cout << "Doing v *= 2;" << endl;
v *= 2;//向量數乘,從新賦值v
cout << "Now v =
" << 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.轉置矩陣(A^T ),共軛矩陣( overline{A}A^H ),逆矩陣( A^{-1} )

I. 編寫程序

#include <iostream>
#include <Eigen/Dense>
using namespace std;
using namespace Eigen;
int main()
{
MatrixXcf a = MatrixXcf::Random(2,2);//定義2-by-2隨機矩陣
cout << "Here is the matrix a
" << a << endl;//矩陣a
cout << "Here is the matrix a^T
" << a.transpose() << endl;//a的轉置
cout << "Here is the matrix a^H
" << a.conjugate() << endl;//a的共軛
cout << "Here is the matrix a^{-1}
" << 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 <iostream>
#include <Eigen/Dense>
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:
" << m*m << endl;//矩陣-矩陣
cout << "Here is m*u:
" << m*u << endl;//矩陣-向量
cout << "Here is u^T*m:
" << u.transpose()*m << endl;//向量-矩陣
cout << "Here is u^T*v:
" << u.transpose()*v << endl;//向量-向量
cout << "Here is u*v^T:
" << u*v.transpose() << endl;//向量-向量
cout << "Lets multiply m by itself" << endl;
m = m*m;//矩陣-矩陣
cout << "Now m is:
" << 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
Lets multiply m by itself
Now m is:
7 10
15 22

3.點乘&叉乘

I. 編寫程序

#include <iostream>
#include <Eigen/Dense>
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:
" << v.cross(w) << endl;//向量叉乘
}

II. 運行結果

Dot product: 8
Cross product:
1
-2
1

4.其他

I. 編寫程序

#include <iostream>
#include <Eigen/Dense>
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

推薦閱讀:

相关文章