Eigen provides not only Matrix and Vector structures, but also Array structures. The difference is as follows, Matrix and Vector are matrices and vectors defined in linear algebra, and all mathematical operations are consistent with mathematics. But there is a problem that the mathematical definition does not necessarily fully meet the practical needs. For example, the addition of a matrix and a scalar is not defined mathematically. But if we want to add the same number to each element of a matrix, then this operation needs to be implemented by ourselves, which is obviously not convenient.
Array provides an Array class, which provides us with a large number of undefined operations on matrices, and it is easy to convert between Array and Matrix, so it is equivalent to providing more methods for matrices. It also provides more choices for the different needs of users.
Let's take a look at the implementation of the Array class. The Array class has the same parameters as Matrix.
Array<typename Scalar, int RowsAtCompileTime , int ColsAtCompileTime >
The meaning of the above parameters is the same as that of the parameters in Matrix.
Array also has some type definitions for common cases.
typedef Array<float ,Dynamic,1> ArrayXf;
typedef Array<float,3,1> Array3f;
typedef Array<double,Dynamic ,Dynamic > ArrayXXd;
typedef Array<double ,3,3 > Array33d;
As can be seen from the above types, there are still slight differences between Array and Matrix.
Access elements in Array
Parentheses ( ) are overloaded to access elements in an Array.
Examples are as follows:
1 #include <iostream> 2 #include <eigen3/Eigen/Dense> 3 4 using namespace Eigen; 5 using namespace std; 6 7 int main( int argc , char ** argv) 8 { 9 ArrayXXf m( 2 , 2 ) ; 10 11 // Assign a single value to the element 12 m( 0 , 0 ) = 1.0 ;m( 0 , 1 ) = 2.0 ; 13 m( 1 , 0 ) = 3.0 ;m( 1 , 1 ) = m( 0 , 1 ) + m( 1 , 0 ); 14 cout<<m<<endl<< endl; 15 16 // Comma can also be used way assignment 17 m<< 5.0 , 6.0 , 18 7.0 , 8.0 ; 19 cout<<m<< endl; 20 21 return 0 ; 22 }
The results are as follows:
Addition and Subtraction
Adding and subtracting two Arrays is the same as Matrix. This operation can be performed as long as the two Arrays have the same dimensions and the same element type. At the same time, Array also defines Matrix that does not support
Operations on an Array and a scalar. An example is as follows:
1 #include <iostream> 2 #include <eigen3/Eigen/Dense> 3 4 using namespace Eigen; 5 using namespace std; 6 7 int main(int argc ,char** argv) 8 { 9 ArrayXXf a(3,3); 10 ArrayXXf b(3,3); 11 12 a << 1,2,3, 13 4,5,6, 14 7,8,9; 15 16 b << 1,2,3, 17 1,2,3, 18 1,2,3; 19 20 cout<<"a + b = "<<endl<<a+b<<endl<<endl; 21 22 cout<<"a - 2 = "<<endl<<a-2<<endl; 23 24 return 0; 25 }
The results are as follows:
multiplication operation
The multiplication operation for an Array and a scalar is the same as that for a Matrix. At the same time, Array also defines a multiplication operation between two Arrays, which is to multiply the elements of the two Arrays.
An example is as follows:
1 using namespace Eigen; 2 using namespace std; 3 4 int main(int argc ,char** argv) 5 { 6 ArrayXXf a(2,2); 7 ArrayXXf b(2,2); 8 9 a << 1,2, 10 3,4; 11 12 b << 5,6, 13 7,8; 14 cout<<"a * b = "<<endl<<a*b<<endl<<endl; 15 16 return 0; 17 }
The results are as follows:
It can be seen that the element-wise multiplication is performed.
Other element operations
Array also defines the absolute value abs(), the square root sqrt(), and the min() operation to find the minimum value of the corresponding element;
An example is as follows:
1 #include <iostream> 2 #include <eigen3/Eigen/Dense> 3 4 using namespace Eigen; 5 using namespace std; 6 7 int main(int argc ,char** argv) 8 { 9 ArrayXf a = ArrayXf::Random(5); 10 a *= 2; 11 cout<<"a = "<<endl 12 <<a<<endl; 13 cout<<"a.abs() = "<<endl 14 <<a.abs()<<endl; 15 cout<<"a.abs().sqrt() ="<<endl 16 <<a.abs().sqrt()<<endl; 17 cout<<"a.min(a.abs().sqrt()) = "<<endl 18 <<a.min(a.abs().sqrt())<<endl; 19 return 0; 20 }
Run as follows:
Conversion between Array and Matrix is easy.
Array has a .matrix() method.
Matrix has .array() method.