文章目录
- 运算
- +,- 加减法
- * / 乘除法
- 逐元 乘法
- 逐元 除法
- 逐元综合运算
- 矩阵乘法与加减法
- 转置、共轭、伴随矩阵
- 点乘法,叉积
运算
+,- 加减法
- 逐元加减法
#include <iostream>
#include "e:/eigen/Eigen/Dense"
using namespace std;int main()
{Eigen::Matrix2d a;a << 1, 2,3, 4;Eigen::MatrixXd b(2,2);b << 10, 20,30, 40;cout << "a + b =\n" << a + b << endl;cout << "a - b =\n" << a - b << endl;cout << "Doing a += b;" << endl;a += b;cout << "Now a =\n" << a << endl;Eigen::Vector3d v(1,2,3);Eigen::Vector3d w(1,0,0);cout << "-v + w - v =\n" << -v + w - v << endl;
}
a + b =
11 22
33 44
a - b =-9 -18
-27 -36
Doing a += b;
Now a =
11 22
33 44
-v + w - v =
-1
-4
-6Process returned 0 (0x0) execution time : 0.573 s
Press any key to continue.
* / 乘除法
逐元 乘法
1、matrix*scalar
2、scalar*matrix
3、matrix*=scalar
#include <iostream>
#include <Eigen/Dense>int main()
{Eigen::Matrix2d a;a << 10, 20,30, 40;Eigen::Vector3d v(1,2,3);std::cout << "a * 0.1 =\n" << a * 0.1 << std::endl;std::cout << "0.1 * v =\n" << 10 * v << std::endl;std::cout << "Doing v *= 2;" << std::endl;v *= 2;std::cout << "Now v =\n" << v << std::endl;
}
a * 2.5 =
1 2
3 4
0.1 * v =
10
20
30
Doing v *= 2;
Now v =
2
4
6Process returned 0 (0x0) execution time : 0.534 s
Press any key to continue.
逐元 除法
1、matrix/scalar2、matrix/=scalar
#include <iostream>
#include "e:/eigen/Eigen/Dense"
using namespace std;
int main()
{Eigen::Matrix2d a;a << 10, 20,30, 40;Eigen::Vector3f v(1,2,3);cout << "a / 5 =\n" << a / 5 << endl;cout << "v / 5 =\n" << v /5 << endl;cout << "Doing a /= 10;" << endl;a /= 10;cout << "Now a =\n" << a << endl;
}
a / 5 =
2 4
6 8
v / 5 =
0.2
0.4
0.6
Doing a /= 10;
Now a =
1 2
3 4Process returned 0 (0x0) execution time : 0.477 s
Press any key to continue.
逐元综合运算
#include <iostream>
#include "e:/eigen/Eigen/Dense"
using namespace std;
int main()
{Eigen::Matrix2d a;a << 10, 20,30, 40;Eigen::Matrix2d b;b << 1, 2,3, 4;cout << "a *10+ b =\n" << a*10+b << endl;
}
a *10+ b =
101 202
303 404Process returned 0 (0x0) execution time : 0.394 s
Press any key to continue.
矩阵乘法与加减法
#include <iostream>
#include "e:/eigen/Eigen/Dense"
using namespace std;
int main()
{Eigen::Matrix2d a;a << 10, 20,30, 40;Eigen::Matrix2d b;b << 2, 4,8, 16;Eigen::Vector2d v(1,2);cout << "a * b =\n" << a *b << endl;cout << "a * v =\n" << a *v << endl;cout << "a + b =\n" << a +b << endl;cout << "a - b =\n" << a -b << endl;
}
a * b =
180 360
380 760
a * v =50
110
a + b =
12 24
38 56
a - b =8 16
22 24Process returned 0 (0x0) execution time : 0.429 s
Press any key to continue.
转置、共轭、伴随矩阵
-
共轭矩阵相关知识
-
转置矩阵
将矩阵的行列互换得到的新矩阵称为转置矩阵,转置矩阵的行列式不变。
-
复数:
template <class Type>
class complex
引用自https://learn.microsoft.com/zh-cn/cpp/standard-library/complex-class?view=msvc-170
复数 a + bi
名称 描述
imag 提取复数的虚分量。
real 提取复数的实分量。#include <complex> #include <iostream>int main( ) {using namespace std;complex<double> c1( 4.0 , 3.0 );cout << "The complex number c1 = " << c1 << endl;double dr1 = c1.real();cout << "The real part of c1 is c1.real() = "<< dr1 << "." << endl;double di1 = c1.imag();cout << "The imaginary part of c1 is c1.imag() = "<< di1 << "." << endl; }
The complex number c1 = (4,3) The real part of c1 is c1.real() = 4. The imaginary part of c1 is c1.imag() = 3.
- 复数矩阵
typedef Matrix< std::complex< float >, Dynamic, Dynamic > Eigen::MatrixXcf
- example
#include <iostream>
#include "e:/eigen/Eigen/Dense"
using namespace std;
int main()
{Eigen::Matrix2d a;a << 10, 20,30, 40;Eigen::MatrixXcf b= Eigen::MatrixXcf::Random(2,2);cout << "转置:a^T =\n" << a.transpose() << endl;cout << "共轭:a conjugate() =\n" << b.conjugate() << endl;cout << "伴随矩阵:a adjoint() =\n"<< a.adjoint() << endl;
}
转置:a^T =
10 30
20 40
共轭:a conjugate() =(0.127171,0.997497) (-0.0402539,-0.170019)(0.617481,0.613392) (0.791925,0.299417)
伴随矩阵:a adjoint() =
10 30
20 40Process returned 0 (0x0) execution time : 0.540 s
Press any key to continue.
- a = a.transpose()转置并替换使用a.transposeInPlace()
- a = a.adjoint() 共轭并替换a.adjointInPlace()
点乘法,叉积
- dot product
引用自百度百科的解释
点积在数学中,又称数量积(dot product; scalar product),是指接受在实数R上的两个向量并返回一个实数值标量的二元运算。它是欧几里得空间的标准内积。
两个向量a = [a1, a2,…, an]和b = [b1, b2,…, bn]的点积定义为:
a·b=a1b1+a2b2+……+anbn。
使用矩阵乘法并把(纵列)向量当作n×1 矩阵,点积还可以写为:
a ⋅ b = a T ∗ b ,这里的 a T 指示矩阵 a 的转置。 a·b=a^T*b,这里的a^T指示矩阵a的转置。 a⋅b=aT∗b,这里的aT指示矩阵a的转置。
- cross product
引用自百度百科知识
向量积,数学中又称外积、叉积,物理中称矢积、叉乘,是一种在向量空间中向量的二元运算。与点积不同,它的运算结果是一个向量而不是一个标量。并且两个向量的叉积与这两个向量和垂直。其应用也十分广泛,通常应用于物理学光学和计算机图形学中。
- example
#include <iostream>
#include "e:/eigen/Eigen/Dense"
using namespace std;
int main()
{Eigen::Vector3d v(1,2,3);Eigen::Vector3d w(0,1,2);cout << "v dot product w =\n" << v.dot(w) << endl;cout << "v cross product w =\n" << v.cross(w) << endl;
}
v dot product w =
8
v cross product w =1
-21Process returned 0 (0x0) execution time : 0.327 s
Press any key to continue.