原文:http://blog.csdn.net/houjixin/article/details/8492841
本文主要是Eigen中矩阵和向量的算术运算,在Eigen中的这些算术运算重载了C++的+,-,*,所以使用起来非常方便。
1、矩阵的运算
Eigen提供+、-、一元操作符“-”、+=、-=,例如:
二元操作符+/-表示两矩阵相加(矩阵中对应元素相加/减,返回一个临时矩阵): B+C 或 B-C;
一元操作符-表示对矩阵取负(矩阵中对应元素取负,返回一个临时矩阵): -C;
组合操作法+=或者-=表示(对应每隔元素都做相应操作):A += B 或者 A-=B
代码段1为矩阵的加减操作,代码如下:
[cpp] view plain copy #include <iostream> #include <Eigen/Dense> using namespace Eigen; int main() { Matrix2d a; a << 1, 2, 3, 4; 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; Vector3d v(1,2,3); Vector3d w(1,0,0); std::cout << "-v + w - v =n" << -v + w - v << std::endl; } 输出结果为: a + b =3 54 8a - b =-1 -1 2 0Doing a += b;Now a =3 54 8-v + w - v =-1-4-6另外,矩阵还提供与标量(单一个数字)的乘除操作,表示每个元素都与该标量进行乘除操作。例如:
二元操作符*在:A*a中表示矩阵A中的每隔元素都与数字a相乘,结果放在一个临时矩阵中,矩阵的值不会改变。
对于a*A、A/a、A*=a、A /=a也是一样,例如下面的代码:
[cpp] view plain copy #include <iostream> #include <Eigen/Dense> using namespace Eigen; int main() { Matrix2d a; a << 1, 2, 3, 4; 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 57.5 100.1 * v =0.10.20.3Doing v *= 2;Now v =246需要注意:
在Eigen中,算术操作例如 “操作符+”并不会自己执行计算操作,他们只是返回一个“算术表达式对象”,而实际的计算则会延迟到后面的赋值时才进行。这些不影响你的使用,它只是为了方便Eigen的优化。
2、求矩阵的转置、共轭矩阵、伴随矩阵。
可以通过 成员函数transpose(), conjugate(),和 adjoint()来完成,注意这些函数返回操作后的结果,而不会对原矩阵的元素进行直接操作,如果要让原矩阵的进行转换,则需要使用响应的InPlace函数,例如:transposeInPlace() 、 adjointInPlace() 之类。
例如下面的代码所示:
矩阵的相乘,矩阵与向量的相乘也是使用操作符*,共有*和*=两种操作符,其用法可以参考如下代码:
[cpp] view plain copy #include <iostream> #include <Eigen/Dense> using namespace Eigen; int main() { Matrix2d mat; mat << 1, 2, 3, 4; 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 1015 22Here is mat*u:11Here is u^T*mat:2 2Here is u^T*v:-2Here is u*v^T:-2 -0 2 0Let's multiply mat 快三最准高手教学of an" << a.conjugate() << endl; cout << "Here is the matrix a^*n" << a.adjoint() << endl; 输出结果为:Here 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)
矩阵的相乘,矩阵与向量的相乘也是使用操作符*,共有*和*=两种操作符,其用法可以参考如下代码:
[cpp] view plain copy #include <iostream> #include <Eigen/Dense> using namespace Eigen; int main() { Matrix2d mat; mat << 1, 2, 3, 4; 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 1015 22Here is mat*u:11Here is u^T*mat:2 2Here is u^T*v:-2Here is u*v^T:-2 -0 2 0Let's multiply mat by itselfNow mat is mat: 7 1015 22免责声明:文章源自网络,版权归原作者所有,如有侵犯联系删除。