Eigen matlab matrix operation for C + + matrix Library

Source: Internet
Author: User

Compared with other matrix libraries, the Eigen only needs to copy all of the include files to the specified location, without compiling and using the Eigen (Visit), in addition, the use of MATLAB matrix operation is simulated;

The above characteristics, so that it has a good practicality.

Attach the test code for learning and use.

Http://eigen.tuxfamily.org/dox/group__QuickRefPage.html#include <iostream> #include <Eigen/Dense> Using namespace std;using namespace Eigen;typedef matrix<float, 1, 3> rvector;int main () {int cnt = 0;//Define a matrix and assign a value Matr Ixxd m (2,2); m (0,0) = 3;m (1,0) = 2.5;m (0,1) = -1;m (All) = M (1,0) + m (0,1); cout << ' [' << cnt++ << '] ' &L  t;< ":" << "m=" << endl;cout << m << endl;cout << "m.cols () =" << m.cols () <<    ", m.rows () =" << m.rows () << ", size () =" << m.size () << Endl << endl;m << 1, 2, 3, 4; Pre-column cout << ' [' << cnt++ << '] ' << ': ' << ' comma assignment, m= ' << endl;cout << m   << endl;cout << "m first line:" << m (1) << endl;//produces a random matrix matrixxd m1 = Matrixxd::random (3,3); 3 X 3 random matrix with values between 1 and 1 cout << ' [' << cnt++ << '] ' << ': ' << ' m1 = ' << Endl << M1 << Endl << Endl; Matrixxd m2 = Matrixxd::constant (3,3,1.2);  3 x 3 matrix with a value of 1.2 cout << ' [' << cnt++ << '] ' << ': ' << ' m2 = ' << endl;cout <<  M2 << endl << endl;m1 = (m1 + m2) * 5;cout << ' [' << cnt++ << '] ' << ': ' << "M1 = (m1 + m2) * 5 =" << Endl << M1 << endl << endl;//vector assignment multiplied by matrix---comma-initializationvectorxd V (  3); v << 1, 2, 3;cout << ' [' << cnt++ << '] "<<": V "<< Endl << v << Endl << endl;cout << ' [' << cnt++ << '] ' << ' m1 * v = ' << Endl << M1 * v << E NDL << endl;//Vectorxd v1 (3) for vectors by loop, for (int i = 0; i < 3; i++) v1 (i) = I;cout << ' [' << cnt++ &LT;&L T "]" << "column vector v1=" << endl << v1 << endl << endl;//line vector rvector rv;for (int i = 0; I < 3; i++) RV (i) = I;cout << ' [' << cnt++ << '] ' << ' line vector: ' << rv << endl << Endl///Matrix resize Matrixxd m3 = matrixxd::random (3, 4); cout << ' [' << cnt++ << '] ' << ': ' << ' m3 = "<< endl;cout << m3 << endl << endl;cout <<" M3.resize (5, 5) = "<< endl;m3.resize (5   , 5); Do not maintain the original value cout << m3 << Endl << Endl; Matrixxd m4= Matrixxd::random (3, 3); cout << ' [' << cnt++ << '] ' << ': ' << ' m4 = ' <&lt ; Endl;cout << M4 << endl;//matrix transpose transpose cout << "M4 transpose =" << endl;cout << m4.transpose () < < endl;//matrix conjugate transpose cout << M4 conjugate = << endl;cout << m4.conjugate () << endl;// Adjoint transpose of the matrix cout << "m4.adjoint=" << endl;cout << m4.adjoint () << Endl << endl;//matrix with scalar +,- , x,/operation Matrixxd ma = matrixxd::random (2, 3) with the operation of the Matrix and the vector Matrixxd vb = Matrixxd::random (3, 1); cout << ' [' << cnt++ << '] ' << ': ' << ' ma = ' <&l T Endl;cout << ma << endl;cout << "VB =" << endl;cout << vb << endl;cout << "Ma * vb =" << endl;cout << ma * vb &    lt;< Endl; Dot dot multiplication and cross fork product, see reference return 0;}

Operation Result:

[0]: m=
3-1
2.5 1.5
M.cols () =2, M.rows () =2, size () =4

[1]: comma assignment, m=
1 2
3 4
First line of M: 3
[2]: m1 =
0.680375 0.59688-0.329554
-0.211234 0.823295 0.536459
0.566198-0.604897-0.444451

[3]: m2 =
1.2 1.2 1.2
1.2 1.2 1.2
1.2 1.2 1.2

[4]: M1 = (m1 + m2) * 5 =
9.40188 8.9844 4.35223
4.94383 10.1165 8.6823
8.83099 2.97551 3.77775

[5]: V
1
2
3

[6] M1 * v =
40.4274
51.2237
26.1153

[7] Column vector v1=
0
1
2

[8] Line vector: 0 1 2

[9]: m3 =
0.10794-0.270431 0.83239-0.716795
-0.0452059 0.0268018 0.271423 0.213938
0.257742 0.904459 0.434594-0.967399

M3.resize (5, 5) =
6.91676e-310 0 0) 0 0
6.91676e-310 0 0) 0 0
2.122e-314 0 0) 0 0
3.7008e-33 0 0) 0 0
7.23757e-320 0 0) 0 0

[Ten]: M4 =
-0.514226-0.686642-0.782382
-0.725537-0.198111 0.997849
0.608354-0.740419-0.563486
M4 transpose =
-0.514226-0.725537 0.608354
-0.686642-0.198111-0.740419
-0.782382 0.997849-0.563486
M4 conjugate =
-0.514226-0.686642-0.782382
-0.725537-0.198111 0.997849
0.608354-0.740419-0.563486
M4.adjoint=
-0.514226-0.725537 0.608354
-0.686642-0.198111-0.740419
-0.782382 0.997849-0.563486

[one]: MA =
0.0258648 0.22528 0.275105
0.678224-0.407937 0.0485744
VB =
-0.012834
0.94555
-0.414966
Ma * vb =
0.0985221
-0.414586


Eigen matlab matrix operation for C + + matrix Library

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.