C + + Open source Matrix arithmetic tool--eigen

Source: Internet
Author: User

*************************************** Reprint Please specify the Source: Http://blog.csdn.net/lttree ********************************************



These days, has been busy to help the college a teacher to do a software function module,

Module requirements are a series of operations on matrices,

Originally was to write their own, later, found that there are ready-made tools, a lot, I finally chose the eigen,

Because it is convenient AH ~ just need to put folders in the Include folder, you can use, packaging what is also convenient.

Moreover, comparing with other tools, we find that there are some advantages in speed.


I'm looking at this stuff → this and the this

Although, its function name and MATLAB is a little different, but I will not use MATLAB, will not be used to matlab People's awkward problem exists ~ ~

Finally, its tutorials are relatively few, basically are foreign, see for themselves,


I will tidy up, send out my own learning experience, should be a more complete and clear blog. ,

Because, I also novice, make mistakes unavoidably, hope everybody can point out, common progress!

More highlights at: Http://blog.csdn.net/lttree

Ok, don't say much nonsense, practice it!


1. First of all, to add this stuff to VS, to provide us with.

Download First, unzip: Http://eigen.tuxfamily.org/index.php?title=Main_Page

My environment is win7+vs2010 downloaded in version 3.2.2.


After decompression there is this folder: Eigen-eigen-1306d75b4a21

Well, I only took the Eigen folder inside,

In the Include folder under the VC folder under the Vs_2010 folder under the VS2010 folder,

That is: drive letter: \vs2010\vs_2010\vc\include

(I don't know how other people put it, anyway, I used it this way = =.) )


Then you can create a new project, and after you create a new project, you can use:

#include "Eigen/eigen" using namespace Eigen;

Add in its header file, use the namespace,

And then you can use its function.


2. Recognize some of its header files.

Eigen This kind of library, save a lot of things, look at the main several head file it:



more highlights at:Http://blog.csdn.net/lttree

Huh? There is English?!!

Do not be afraid, to read more, read more, more recognition!

The upper part of the English meaning is:

The Eigen library is divided into two parts, Core module and additional module,

Each module has a header file corresponding to this module,

Eigen and dense header files are conveniently included with several header files for use.


--core

For matrices and arrays, there are basic linear algebra (which contains triangles and autocorrelation products), as well as the operation of the corresponding array.

--geometry

Class of geometry, about conversion, panning, rounding, 2D rotation, 3D rotation (four-tuple and angular axis-related)

--lu

Class of logical units, for inverse, determinant, Lu decomposition solver (FULLPIVLU,PARTIALPIVLU)

--cholesky

Contains the Cholesky factorization method for Llt and LDLT.

(Small science: Cholesky decomposition is a symmetric positive definite matrix expressed as a lower triangular matrix L and its transpose product decomposition)

--householder

Household transforms, this module is used by several linear algebra modules.

(Householder transform: Wikipedia)

--svd

Singular value decomposition, least squares solver solves singular value decomposition.

--qr

QR Decomposition solution, three ways: HOUSEHOLDERQR, COLPIVHOUSEHOLDERQR, FULLPIVHOUSEHOLDERQR

--eigenvalues

Methods of eigenvalue and eigenvector decomposition: Eigensolver, Selfadjointeigensolver, Complexeigensolver

--sparse

Sparse matrix related classes, for storage of sparse matrices and related basic linear algebra

--dense

Includes: Core, Gelometry, LU, Cholesky, SVD, QR, and eigenvalues modules (header file)

--eigen

Contains all of the above modules (header file)


For a more detailed explanation of the above, you can read the documentation: http://eigen.tuxfamily.org/dox/group__QuickRefPage.html



3. Simple operation of The matrix

Eigen provides two dense objects matrix (matrix) and vector (vector).

Both are implemented through the matrix template class and the one-dimensional or two-dimensional array template class.

There are several differences between the two:

--matrix type variable plus subtraction, if the number of rows is not equal, it cannot be added and subtracted;

The array type can be added to a constant (each element is added minus the constant).

The multiplication of the--matrix with the array type variable is also different, the matrix is multiplied, and the array is multiplied by the corresponding element.

--but the two can be converted from one to the other by. Array () and. Matrix ().


more highlights at:Http://blog.csdn.net/lttree

→① definition (Note: When a matrix is defined, it is not initialized by default and must be initialized by itself)

The matrix type of the eigen is typically represented by a type symbol behind the matrix, such as:

--' d ' represents a double, and the matrix stores a double type of data

--' F ' stands for float, The matrix stores the float type data

--' C ' stands for complex, and the number of matrices is plural type data

--' I ' represents int, the matrix stores the integer type


The corresponding relationship is:



Like what:

MATRIXXF M1 (3,4);   A dynamic matrix  matrixxf m2 (3,3) with 3 rows and 4 columns is established.  VECTOR3F v1;        

X stands for Dynamics, F for Float type


→② initialization

M1=matrixxf::zero (3,4);//Initialize the Matrix 3 row 4 column to 0m2=matrixxf::ones (3,3);//Initialize the Matrix 3 row 3 column to 1v1=vector3f::ones ();// Initialize the longitudinal amount of 3 rows to 1cout<< "m1=\n" <<m1<<endl;cout<< "m2=\n" <<m2<<endl;cout<< " V1=\n "<<v1<<endl;

Run out of Effect:



Further, test it:

MATRIXXF m3 (4,5); M3=matrixxf::zero (4,5);cout<< "m3_1=\n" <<m3<<endl;m3=matrixxf::ones (3,3); cout << "m3_2=\n" <<m3<<endl;m3=matrixxf::ones (6,6);cout<< "m3_3=\n" <<m3<<endl;


First, define a matrix of 4 rows and 5 columns.

Initialized to 0, 4 rows and 5 columns, then the output is 4 rows and 5 columns of 0

Initialized to 3 rows 3 columns of 1, Output, 3 rows 3 columns of 1

Initialized to 6 rows 6 columns of 1, output, then become 6 rows 6 columns 1,

Detailed View:



In other words, the size of the matrix is closely related to initialization, how much is initialized, and how much it is.

Who let it be dynamic?!


So, you're going to say, what do you declare when you define it?

Because the sowing method initializes, it requires a row and column value:

MATRIXXF m3 (2,3);m3<<1,2,3,4,5,6;cout<< "m3_1\n" <<endl;//for the sake of the United States, more like a matrix, you can change the line to write M3<<1.3,4,-8,    0,0.9,2;cout<< "m3_2=\n" <<m3<<endl;

Running Out is:



Isn't it very simple and rude?

more highlights at:Http://blog.csdn.net/lttree


→③ Access

This is very simple, directly on the same array of access mode,

But not square brackets, but parentheses:

MATRIXXF m3 (2,3);m3<<1,2,3,4,5,6;cout<< "m3_1\n" <<m3<<endl;//for the sake of the United States, more like a matrix, you can change the line to write m3<< 1.3,4,-8,    0,0.9,2;cout<< "m3_2=\n" <<m3<<endl;//Change the value of column 3rd of row 2nd to 99m3 =99;cout<< "m3_3= \ n "<<m3<<endl;


Of course, as with arrays, the subscript for the first column of the first row is (0,0)






4. Basic operations of matrices

The code executes the matrix:

--Place 0

--Place 1

--Random matrices

--Unit array

--Seeking inverse

--Transpose

--Multiplication matrix

<span style= "White-space:pre" ></SPAN>MATRIXXF M1 (3,3);  Matrix all elements Set 0m1.setzero ();cout<< "m1_1=\n" <<m1<<endl;//matrix all elements Set 1 (here the row value is not filled, the default definition when the row,//                     if filled out, The matrix is also changed to fill in the row and column values) M1.setones (2,2);cout<< "m1_2=\n" <<m1<<endl;//randomly generate a matrix m1.setrandom ();cout< < "m1_3=\n" <<m1<<endl;//Unit matrix m1.setidentity (3,3);cout<< "m1_4=\n" <<m1<<endl;m1 << 1, 2, 3, 5, 9, 10, 7, 0, 1;//matrix Inverse m1.inverse ();cout<< "m1_5=\n" <<m1<<endl;//matrix transpose M1.transpo SE ();cout<< "m1_6=\n" <<m1<<endl;//number * MATRIX (number/matrix) m1 = 2.6 * M1;cout<< "m1_7=\n" <<m1<< ; Endl;

Run the results as follows:



Ah, and the subtraction of the Matrix,

The amount ...

You don't have to write this, do you?

The same as the usual subtraction,

is the matrix multiplication to note that the row and column values of two matrices yo ~



5. Advanced Operations for matrices

Post-update, first come here ...




*************************************** Reprint Please specify the Source: Http://blog.csdn.net/lttree ********************************************

C + + Open source Matrix arithmetic tool--eigen

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.