intmain() { 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; }
Output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
a + b = 35 48 a - b = -1-1 20 Doing a += b; Now a = 35 48 -v + w - v = -1 -4 -6
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
并且minCoeff()与maxCoeff()能够得到相应元素的下标,可使用如下方法实现:
int i,j;
minCoeff(&i,&j);
maxCoeff(&i,&j);
得到的i,j就是相应元素的横纵坐标。
转置,共轭,共轭转置示例代码:
MatrixXcf a = MatrixXcf::Random(2,2); //随机2x2矩阵
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;
Output:
1 2 3 4 5 6 7 8 9 10 11 12
Here is the matrix a (-1,-0.737) (0.0655,-0.562) (0.511,-0.0827) (-0.906,0.358) Here is the matrix a^T (-1,-0.737) (0.511,-0.0827) (0.0655,-0.562) (-0.906,0.358) Here is the conjugate of a (-1,0.737) (0.0655,0.562) (0.511,0.0827) (-0.906,-0.358) Here is the matrix a^* (-1,0.737) (0.511,0.0827) (0.0655,0.562) (-0.906,-0.358)
Eigen checks the validity of the operations that you perform. When possible, it checks them at compile time, producing compilation errors. These error messages can be long and ugly, but Eigen writes the important message in UPPERCASE_LETTERS_SO_IT_STANDS_OUT. For example:(Eigen会检查你所执行的操作的有效性,在可能的情况下,它会在编译时检查它们,产生编译错误。在可能的情况下,它在编译时检查它们,产生编译错误。这些错误信息可能又长又丑,但Eigen会把重要的信息用UPPERCASE_LETTERS_SO_IT_STANDS_OUT写出来,如下:)
1 2 3
Matrix3f m; Vector4f v; v = m*v; // Compile-time error: YOU_MIXED_MATRICES_OF_DIFFERENT_SIZES
Of course, in many cases, for example when checking dynamic sizes, the check cannot be performed at compile time. Eigenthen uses runtime assertions. This means that the program will abort with an error message when executing an illegal operation if it is run in "debug mode", and it will probably crash if assertions are turned off(当然,在很多情况下,比如检查动态大小时,不能在编译时进行检查。Eigen就会使用运行时断点。这意味着,如果在 "debug模式 "下运行,程序在执行非法操作时,会以错误信息中止,如果断点被关闭,程序很可能会崩溃).
1 2 3
MatrixXf m(3,3); VectorXf v(4); v = m * v; // Run-time assertion failure here: "invalid matrix product"