Eigen library Introduction and Getting Started

Eigen is a high-level C ++ library, effectively support linear algebra, vector and matrix operations, numerical analysis and related algorithms. Eigen is an open source library, version 3.1.1 from the beginning MPL2 license compliance.

 

Kinematic To a Matlab program into C ++ DLL written, need to use a matrix library Eigen, Eigen is a library written in C ++ source code library matrix can basically meet the operational need to use in the calculation, the following an introduction to learn some libraries.

1. First, the definition of a fixed size matrix, vector, initialization

 

#include<iostream>

#include<Eigen/Core>

 

using namespace std;

using namespace Eigen;

 

// import most commen types Own

 

int main(int, char *[])

{

         Matrix3fm3; // 3X3 single-precision matrix

         m3<< 1, 2, 3, 4, 5, 6, 7, 8, 9;

         Matrix4fm4 = Matrix4f :: Identity (); // 4X4 matrix (single precision)

         Vector4iv4 (1, 2, 3, 4); // shaping vector length 4

 

         // output

         std::cout<< "m3\n" << m3 << "\nm4:\n"

                   <<m4 << "\nv4:\n" << v4 << std::endl;

         getchar();

         return0;

}

/ * Study notes * /

/*

         Matrix representation matrix, Vector vector indicating the numbers indicate the dimension, f and i represent the final single-precision integer data type and

         When compiling a fixed size represents the number of rows and columns are fixed, then, Eigen, does not allocate dynamic memory. This is more appropriate for the smaller matrix, for example, 16 * 16

*/

 

2. Dynamic defined matrix Matrix, Vector vector

 

#include<iostream>

#include<Eigen/Core>

 

using namespace std;

using namespace Eigen;

 

int main(int, char *[])

{

         for(int size = 1; size < 4; ++size)

         {

                   MatrixXim (size, size + 1); // shaping a size (size) X (size + 1) matrix

                   for (int j = 0; j <m.cols (); ++ j) // iterate column

                            for(int i = 0; i < m.rows(); ++i)//遍历行

                                     m (i, j) = i + j * m.rows (); // Use parentheses m (i, j) elements of the matrix access

                   std :: cout << m << "\ n \ n"; // print matrix

         }

         // dynamic vector

         VectorXfv (4); // define a four-dimensional vectors of single-precision

         // Use parentheses or square brackets [] to access vector elements

         v [0] = 1; v [1] = 2; v (2) = 3; v (3) = 4;

         std::cout<< "\nv:\n" << v << endl;

         getchar();

         return0;

}

 

// learning experience

/*

         X represents a dynamic size

         #include <Eigen / Eigen> will contain all the Eigen functions. #include <Eigen / Dense> includes all common matrix function, does not contain a sparse matrix function, they will increase compile time.

*/

 

3. Zero, Ones, UnitX, Constant initialization matrix

 

#include<iostream>

#include<Eigen\Core>

 

using namespace std;

using namespace Eigen;

 

int main(int, char*[])

{

         help = 3;

         floatvalue = 3.0f;

         VectorXfx; // definition of motion vector

         x = VectorXf :: Zero (size); // all-0 vector

         cout<< x << endl << endl;

         x = VectorXf :: Ones (size); // 1 Vector Full

         // create vector-based fixed size

         Vector3fy;

         y = Vector3f :: UnitX (); // 1 0 0

         cout<< y << endl << endl;

         y= Vector3f::UnitY();       //0 1 0

         cout<< y << endl << endl;

         y= Vector3f::UnitZ();       //0 0 1

         cout << "size of basis vectors creating a dynamic" << endl;

         VectorXfz;

         z= VectorXf::Unit(4, 2);

         cout<< z << endl << endl;

         z= Vector4f(0, 1, 0, 0);

         cout<< z << endl << endl;

         z= Vector4f::UnitY();

         cout<< z << endl << endl;

         getchar();

         return0;

}

 

#include<iostream>

#include<Eigen/Core>

 

using namespace std;

using namespace Eigen;

 

int main(int, char*[])

{

         floatvalue = 3.0f;

         introws = 3;

         intcols = 4;

         MatrixXfx;

         x= MatrixXf::Zero(rows, cols);

         cout<< x << endl << endl;

         x= MatrixXf::Ones(rows, cols);

         cout<< x << endl << endl;

         x = MatrixXf :: Constant (rows, cols, value); // constant: constant unchanging

         cout<< x << endl << endl;

         x= MatrixXf::Identity(rows, cols);

         cout<< x << endl;

         getchar();

         return0;

}

 

4. cast by converting data types

#include<iostream>

#include<Eigen\Core>

 

using namespace std;

using namespace Eigen;

// elements by Matrix :: cast () automatic conversion

int main(int, char*[])

{

         Vector3dmd(1, 2, 3);

         Vector3fmf = md.cast<float>();

         cout<< "md = " << md << endl;

         cout<< "mf = " << mf << endl;

         getchar();

         return0;

}

 

5. Initialization matrix comma

#include<iostream>

#include<Eigen\Core>

 

using namespace std;

using namespace Eigen;

 

int main(int, char *[])

{

         Matrix3fm;

         m<< 1, 2, 3,

                   4,5, 6,

                   7,8, 9;

         cout<< m << endl;

         getchar();

         return0;

}

 

// initialize the matrix using commas

 

6. Create a fixed size matrix and vector

 

#include<iostream>

#include<Eigen/Core>

 

using namespace Eigen;

using namespace std;

 

int main(int, char *[])

{

         floatvalue = 3.0;

         Matrix3fx; // create a 3x3 matrix of single-precision

         x = Matrix3f :: Zero (); // all-zero matrix

         cout<< x << endl<<endl;

         x = Matrix3f :: Ones (); // a full matrix

         cout<< x << endl << endl;

         x = Matrix3f :: Constant (value); // full value matrix

         cout<< x << endl << endl;

         x = Matrix3f :: Identity (); // matrix

         cout<< x << endl << endl;

         x = Matrix3f :: Random (); // random matrix

         cout<< x << endl << endl;

         x.setZero (); // set all 0 x

         cout<< x << endl << endl;

         x.setOnes (); // set all 1 x

         cout<< x << endl << endl;

         x.setIdentity (); // set x is a unit matrix

         cout<< x << endl << endl;

         x.setConstant (value); // set value x for the whole matrix

         cout<< x << endl << endl;

         x.setRandom (); // set x to your random Secretary

         cout<< x << endl << endl;

         getchar();

         return0;

}

 

7. simple matrix calculation

#include<iostream>

#include<Eigen\Core>

 

using namespace std;

using namespace Eigen;

 

int main(int, char*[])

{

         MatrixXfres (10, 10); // dynamically create a 10x10 matrix

         Matrix3fa, b;

         a = Matrix3f :: Identity (); // matrix

         b = Matrix3f :: Constant (3); // full matrix 3

         res= a + b;       //res is resized to size3x3

 

         cout<< a << endl << endl;

         cout<< b << endl << endl;

         cout<< res << endl << endl;

         getchar();

         return0;

}

 

Here, the basic assignment Eigen, the initialization operation has been completely over, fought over again procedure, basically you can start writing programs, and following a few tips I record it in the preparation of kinematics algorithm will be used :

1. The (double) matrix array is converted Matirx

Here, I generally use the double (16) is converted to Matrix4d, are used as follows

 

Map<Matrix4d>(dSTOut, 4, 4) = sTargetMatrix;

 

Map used here is the statement, which is double dSTOut array of 16 elements, sTargetMatrix to Matrix4d matrix should be noted that, dSTOut here must be arranged in columns, in order to restore the matrix

2. Matrix4d convert (double) array

1 and the similar, where I generally will also be converted to Matrix4d (16) arranged in an array of columns double, are used as follows:

 

dD_Tm1 = Map<Matrix4d>(dDArmOut_Tm1, 4, 4);

 

Wherein dD_Tm1 is Matrix4d matrix, dDArmOut_Tm1 a double array elements 16 arranged in columns, the use of this statement can be converted to double Matrix4d arrays arranged in columns.

 

3. In the calculation of the inverse matrix, the matrix inverse may not exist, in order to prevent crashes, usually write:

 

         BoolbInvertible = false;

         Matirx4dT1 = Matrix4d::zero();

         T1.computeInverseWithCheck(inverseT1,bInvertible);

         if(false == bInvertible)

         {

                   return1;

         }

         * = traMat_a1 inverseT1 dTargetMatrix;

4. Get matrix of sub-matrix, this approach often used in the matrix operation, before also checked a lot of blog and found a lot of matrix methods are said to take some questions

 

uniVec_P1 traMat_a1.block = (0, 3, 1, 3);

 

This is my approach, only the operation taken by the block sub-matrix manner, where there are four block parameters, the first two to take a position to take the first sub-matrix elements in the matrix, where 0, 3 on behalf of row 1, column 4, the latter two elements represent the elements starting from the first number of rows and columns you want to capture, where 1,3 representative for each line to obtain an element, to obtain a total of three.

 

The rows will be converted to an array of columns arranged in an array

This method is also suitable for converting an array will be arranged in an array of columns arranged in rows

 

intMatrixTran(double *dInMat)

{

double dTS[16] = {0.0};

int itran = 0;

int itrannum = 0;

 

for(int a = 0; a < 16; a ++)

{

           dTS[a] = dInMat[a];

}

 

for(int j = 0; j < 4; j++)

{

           itrannum = j;

           for(int i = 0; i < 4; i++)

           {

                    dInMat[itran] =dTS[itrannum];

                    itrannum + = 4;

                    itran ++;

           }

          

}

return 0;

}

 

6. The method according to find the pseudo-inverse matrix of the SVD

 

// find a pseudo-inverse mode using svd

template<typename_Matrix_Type_>

_Matrix_Type_pseudoInverse(const_Matrix_Type_&a,oublepsilon=std::numeric_limits<double>::epsilon())

{

Eigen :: JacobiSVD <_Matrix_Type_> SVD (a, Eigen :: ComputeThinU | Eigen :: ComputeThinV);

double tolerance = epsilon * std::max(a.cols(),a.rows()) *svd.singularValues().array().abs()(0);

Return svd.matrixV () * (svd.singularValues ​​(). Array (). abs ()> Tolerance) .Select (svd.singularValues ​​(). Array (). inverse (), 0) .matrix (). asDiagonal ( ) * svd.matrixU (). adjoint ();

}

 

In Eigen library, not as direct as in matlab seeking pseudo-inverse of pinv program, but by way of SVD obtained, the method shown above, there is no specific and detailed study of how the program is running, there is time to re-look at this in detail now, total when it is, find the pseudo-inverse kinematics can be written matlab program will convert c ++, one of the hardest place

 

to sum up

While the final may also need a lot of time on the written procedures for debugging, but the main work has been completed, the programming practice, for my side it can be characterized as simple, because I do not need to kinematics inverse solution for a lot of thinking, just exactly as written HUANG Kang matlab program, transfer program for the c language, c language can be counted, because did not involve writing class, but there are some heart urge to to convert the entire program over the style class, and I hope the Department of kinematics algorithm can be applied to multiple devices, instead of just this kind of car, found the programmer who will have to have the nature of written procedures the understanding of the characteristics of the problem can be extracted from the beginning of the program, it is necessary to consider the overall program, otherwise no end of trouble, causing very bad influence on later work, Take this, if I had carried out in accordance with written procedures Huang Kang that way, then when the docking interface and the late king's work, will be faced with a large number of programming interfaces and program content Change; secondly, discovered the importance of unified planning of the program, and the importance of the comment, the current number of rows in the program to reach more than three thousand lines, found himself in control force for the entire program has been lost in slowly at the beginning to become before must be unified interface, unified input and output parameters; in addition, for a small part of the understanding VB.Net can be said VB.Net is a very easy to use language, and do not want to c c ++ language as a whole needs a strict format feeling during compile time the compiler will do a lot of things; trying to accomplish what features need only write the appropriate function in which the latter sub can make me feel, VB.Net itself is a big tired, we just add a function to do the work required of it in class.
 

Released seven original articles · won praise 13 · views 20000 +

Guess you like

Origin blog.csdn.net/xingsongyu/article/details/103558792