Eigen库的基本使用说明(二)
之前的文章中,简单的介绍了一些基本的操作,回归之前的内容可以参考一下链接:
zEigen库的基本使用说明_每日亿学的博客-CSDN博客_eigen库
本章内容主要就是继续延伸Eigen库的使用内容也会实时进行更新,Eigen库在SLAM中使用广泛,需要对这个库有一定的熟悉。
一、赋值操作
首先最简单的如何给申请好的矩阵对象进行赋值?
1、定义对象时
//这里以动态的矩阵模板为例子
MatrixXi a { // 创建 2x2 matrix
{1, 2}, // first row
{3, 4} // second row
};
Matrix c = {1, 2, 3, 4, 5};
2、<<操作
类似输入流的方式
Matrix3f m;
m << 1, 2, 3,
4, 5, 6,
7, 8, 9;
3、(,)赋值
注意这里和数组不一样它使用的是[]索引
Eigen::MatrixXd m(2,2); m(0,0) = 3; m(1,0) = 2.5; m(0,1) = -1; m(1,1) = m(1,0) + m(0,1);
这里主要使用的多维2和3这两种。
二、矩阵和向量运算
Eigen通过重载常见的 C++ 算术运算符(如 +、-、*)或通过特殊方法(如 dot()、cross() 等)提供矩阵/向量算术运算。对于Matrix类(矩阵和向量),运算符仅重载以支持线性代数运算。例如,matrix1 * matrix2意味着矩阵矩阵乘积,并且vector + scalar(一个数)是不允许的。
1、加减
要进行加减操作,首先矩阵类型相同,矩阵大小也得相同才能进行操作。
- 二元运算符 + 如a+b
- 二元运算符 – 如a-b
- 一元运算符 – 如-a
- 复合运算符 += 如a+=b
- 复合运算符 -= 如a-=b
#include
#include
int main()
{
Eigen::Matrix2d a;
a << 1, 2,
3, 4;
Eigen::MatrixXd b(2,2);
b << 2, 3,
1, 4;
std::cout << "a + b =\n" << a + b << std::endl;
std::cout << "a - b =\n" << a - b << std::endl;
std::cout << "Doing a += b;" << std::endl;
a += b;
std::cout << "Now a =\n" << a << std::endl;
Eigen::Vector3d v(1,2,3);
Eigen::Vector3d w(1,0,0);
std::cout << "-v + w - v =\n" << -v + w - v << std::endl;
}
输出:
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、标量的乘法和除法
- 二元运算符 * 如matrix*scalar
- 二元运算符 * 如scalar*matrix
- 二元运算符 / 如matrix/scalar
- 复合运算符 *= 如matrix*=scalar
- 复合运算符 /= 如matrix/=scalar
#include
#include
int main()
{
Eigen::Matrix2d a;
a << 1, 2,
3, 4;
Eigen::Vector3d v(1,2,3);
std::cout << "a * 2.5 =\n" << a * 2.5 << std::endl;
std::cout << "0.1 * v =\n" << 0.1 * v << std::endl;
std::cout << "Doing v *= 2;" << std::endl;
v *= 2;
std::cout << "Now v =\n" << v << std::endl;
}
输出:
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
3、转置和共轭
矩阵a的转置、共轭和伴随(即共轭的转置),分别成员函数为:transpose(),conjugate()和adjoint()。对于实数矩阵共轭共轭操作实际是空操作伴随的其实就是转置。
MatrixXcf a = MatrixXcf::Random(2,2); cout << "Here is the matrix a\n" << a << endl; cout << "Here is the matrix a^T\n" << a.transpose() << endl; cout << "Here is the conjugate of a\n" << a.conjugate() << endl; cout << "Here is the matrix a^*\n" << a.adjoint() << endl; //输出 ere is the matrix a (-0.211,0.68) (-0.605,0.823) (0.597,0.566) (0.536,-0.33) Here is the matrix a^T (-0.211,0.68) (0.597,0.566) (-0.605,0.823) (0.536,-0.33) Here is the conjugate of a (-0.211,-0.68) (-0.605,-0.823) (0.597,-0.566) (0.536,0.33) Here is the matrix a^* (-0.211,-0.68) (0.597,-0.566) (-0.605,-0.823) (0.536,0.33)
这个官方提升禁止使用a=a.transpose()会出现一个别名问题!问题描述如下:
Matrix2i a; a << 1, 2, 3, 4; cout << "Here is the matrix a:\n" << a << endl; a = a.transpose(); // !!! do NOT do this !!! cout << "and the result of the aliasing effect:\n" << a << endl; //输出 Here is the matrix a: 1 2 3 4 and the result of the aliasing effect: 1 2 2 4
在实际的编写代码时不小心就会这样编写,Eigen给出了一个原地转置的函数:transposePlace()
这样我们就不需要为了转置再赋值给原变量了。
MatrixXf a(2,3); a << 1, 2, 3, 4, 5, 6; cout << "Here is the initial matrix a:\n" << a << endl; a.transposeInPlace(); cout << "and after being transposed:\n" << a << endl; //输出 Here is the initial matrix a: 1 2 3 4 5 6 and after being transposed: 1 4 2 5 3 6
4、矩阵-矩阵和矩阵*向量乘法
矩阵和矩阵之间可以使用*符号完成,向量其实也是矩阵属性所以也是相对于矩阵隐式处理。一般有以下两个情况:
- 二元运算符 * 如a*b
- 复合运算符 *= 如a*=b(这在右边相乘:a*=b等同于a = a*b)
#include
#include
int main()
{
Eigen::Matrix2d mat;
mat << 1, 2,
3, 4;
Eigen::Vector2d u(-1,1), v(2,0);
std::cout << "Here is mat*mat:\n" << mat*mat << std::endl;
std::cout << "Here is mat*u:\n" << mat*u << std::endl;
std::cout << "Here is u^T*mat:\n" << u.transpose()*mat << std::endl;
std::cout << "Here is u^T*v:\n" << u.transpose()*v << std::endl;
std::cout << "Here is u*v^T:\n" << u*v.transpose() << std::endl;
std::cout << "Let's multiply mat by itself" << std::endl;
mat = mat*mat;
std::cout << "Now mat is mat:\n" << mat << std::endl;
//输出
Here is mat*mat:
7 10
15 22
Here is mat*u:
1
1
Here is u^T*mat:
2 2
Here is u^T*v:
-2
Here is u*v^T:
-2 -0
2 0
Let's multiply mat by itself
Now mat is mat:
7 10
15 22
注意官网提示我们使用m=m*m运算,不会出现别名问题Eigen帮我们修改成:
tmp=m*m;
m=tmp;
5、点积和叉积
对于点积和叉积,您需要dot()和cross()方法。当然,点积也可以像u.adjoint()*v一样得到一个1×1的矩阵。
#include
#include
int main()
{
Eigen::Vector3d v(1,2,3);
Eigen::Vector3d w(0,1,2);
std::cout << "Dot product: " << v.dot(w) << std::endl;
double dp = v.adjoint()*w; // automatic conversion of the inner product to a scalar
std::cout << "Dot product via a matrix product: " << dp << std::endl;
std::cout << "Cross product:\n" << v.cross(w) << std::endl;
}
//输出
Dot product: 8
Dot product via a matrix product: 8
Cross product:
1
-2
1
官网提示叉积只能试用大小为3的向量。
6、基本算术归纳运算
Eigen还提供了求总和(sum())、求总积(prod()),最大值(maxCoeff())和最小值(minCoeff())
#include
#include
using namespace std;
int main()
{
Eigen::Matrix2d mat;
mat << 1, 2,
3, 4;
cout << "Here is mat.sum(): " << mat.sum() << endl;
cout << "Here is mat.prod(): " << mat.prod() << endl;
cout << "Here is mat.mean(): " << mat.mean() << endl;
cout << "Here is mat.minCoeff(): " << mat.minCoeff() << endl;
cout << "Here is mat.maxCoeff(): " << mat.maxCoeff() << endl;
cout << "Here is mat.trace(): " << mat.trace() << endl;
}
//输出
Here is mat.sum(): 10
Here is mat.prod(): 24
Here is mat.mean(): 2.5
Here is mat.minCoeff(): 1
Here is mat.maxCoeff(): 4
Here is mat.trace(): 5
我们求取最大值和最小值还可以求取它的坐标:
std::ptrdiff_t i, j; float minOfM = m.minCoeff(&i,&j); RowVector4i v = RowVector4i::Random(); int maxOfV = v.maxCoeff(&i);
本文来自网络,不代表协通编程立场,如若转载,请注明出处:https://net2asp.com/0d221aa22e.html
