Eigen matrix Operations Library usage Records

Source: Internet
Author: User
Tags rand

http://blog.csdn.net/shuzfan/article/details/52367329

Recently has been doing engineering things, more than the use of the Eigen Matrix Computing Library.

Briefly describe the characteristics of Eigen:

(1) Easy to use, no need for precompilation, small call overhead

(2) The function is rich, the style is somewhat approximate matlab, easy to start;

(3) The speed is the same, faster than the OPENCV, than the MKL, Openblas slow;

Eigen3.3 Version Link Http://eigen.tuxfamily.org/index.php?title=Main_Page

Note: Most of the use of instructions and examples can be found on the official internet, so sometimes do not need to tangle Baidu to the actual discrepancy, you can directly official website or Google

———————————————————————————————————————————————

The use of the method is simple: Download the eigen after the decompression, and then include the decompression path, and finally only need to refer to the program in the header file

1 1

———————————————————————————————————————————————

The basic use method is as follows:

In-situ links for Http://eigen.tuxfamily.org/dox/AsciiQuickReference.txt

Matrix definition

#include <Eigen/Dense>

matrix<double, 3, 3> A;               Fixed rows and cols. Same as Matrix3D.
Matrix<double, 3, dynamic> B;         Fixed rows, dynamic cols.
Matrix<double, Dynamic, dynamic> C;   Full dynamic. Same as Matrixxd.
Matrix<double, 3, 3, rowmajor> E;     Row Major; The default is Column-major.
matrix3f P, Q, R;                     3x3 float matrix.
vector3f x, y, Z;                     3x1 float matrix.
Rowvector3f A, B, C;                  1x3 float matrix.
Vectorxd v;                           Dynamic column vector of doubles
1 2 3 4 5 6 7 8 9 10 11 1 2 3 4 5 6 7 8 9 10-11

Basic methods of Use

Basic usage//eigen//Matlab//Comments X.size ()//Length (x)//vector size c.ro              WS ()//Size (c,1)//number of Rows c.cols ()//Size (c,2)//Number of columns X (i)   X (i+1)//Matlab is 1-based C (i,j)//C (i+1,j+1)//A.resize (4, 4);
Runtime error if assertions are on.   B.resize (4, 9);
Runtime error if assertions are on.   A.resize (3, 3); Ok;
Size didn ' t change.   B.resize (3, 9); Ok;

Only dynamic cols changed. A << 1, 2, 3,//Initialize A.     The elements can also be 4, 5, 6,//matrices, which are stacked along-cols 7, 8, 9;
And then the rows are stacked.     B << A, a, A;
B is three horizontally stacked A ' s.       A.fill (10); Fill A with all ' s.
1, 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18

Special matrix Generation

Eigen                            //Matlab
matrixxd::identity (rows,cols)       //Eye (Rows,cols)
c.setidentity (rows,cols)            / /c = Eye (Rows,cols)
Matrixxd::zero (rows,cols)           //Zeros (Rows,cols)
C.setzero (rows,cols)                //c = ones ( Rows,cols)
matrixxd::ones (rows,cols)           //Ones (Rows,cols)
c.setones (rows,cols)                //C = ones (rows, cols)
matrixxd::random (rows,cols)         //rand (Rows,cols) *2-1        //Matrixxd::random returns uniform-Random Numbers in ( -1, 1).
C.setrandom (Rows,cols)              //C = rand (rows,cols) *2-1
vectorxd::linspaced (Size,low,high)   //Linspace ( low,high,size) '
v.setlinspaced (Size,low,high)        //V = linspace (low,high,size) '
1 2 3 4 5 6 7 8 9 10 11 1 2 3 4 5 6 7 8 9 10-11

Matrix block Operations

Matrix slicing and blocks.
All expressions listed here are read/write. Templated size versions are faster.
Note that the Matlab is 1-based (a size N//vector is X (1) ... x (n)).                        Eigen//Matlab X.head (n)//x (1:N) x.head<n> () X (1:n) x.tail (n)//x (End-n + 1:end) x.tail<n> ()//X (End-n + 1:end) x.segment (i, N)//x (i+1:i+n) x.segment<n> (i)//x (i+1:  I+n) P.block (i, J, rows, cols)//P (I+1:i+rows, J+1:j+cols) p.block<rows, cols> (i, j)//P (i+1
: I+rows, J+1:j+cols) p.row (i)//P (i+1,:)                   P.col (j)//P (:, j+1) p.leftcols<cols> ()//P (:, 1:cols) P.leftcols (cols)              P (:, 1:cols) p.middlecols<cols> (j)//P (:, J+1:j+cols) P.middlecols (J, Cols) P (:, J+1:j+cols) p.rightcols<cols> ()//P (:, End-cols+1:end) P.rightcols (cols)//P (:, End
-cols+1:end) p.toprows<rows> ()//P (1:rows,:)
P.toprows (rows)//P (1:rows,:)
P.middlerows<rows> (i)//P (i+1:i+rows,:)
P.middlerows (i, rows)//P (i+1:i+rows,:)
P.bottomrows<rows> ()//P (End-rows+1:end,:)
P.bottomrows (rows)//P (End-rows+1:end,:) P.topleftcorner (rows, cols)//P (1:rows, 1:cols) p.toprightcorner (rows, cols)//P (1:rows, End-cols+1:end) p. Bottomleftcorner (rows, cols)//P (end-rows+1:end, 1:cols) p.bottomrightcorner (rows, cols)//P (End-rows+1:end, end- Cols+1:end) p.topleftcorner<rows,cols> ()//P (1:rows, 1:cols) p.toprightcorner<rows,cols> ()//P (1: Rows, End-cols+1:end) p.bottomleftcorner<rows,cols> ()//P (End-rows+1:end, 1:cols) p.bottomrightcorner<rows ,cols> ()//P (end-rows+1:enD, End-cols+1:end) 
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18-------------19 20 21 22 23 24 25 26 27-28 29 30 31, 32 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33-34

Matrix element Exchange and transpose, etc.

The particular eigen ' s swap function which is highly optimized.
Eigen                           //Matlab
r.row (i) = P.col (j);               R (i,:) = P (:, i)
R.col (J1). Swap (Mat1.col (J2));      R (:, [J1 J2]) = R (:, [J2, J1])

/views, transpose, etc; all read-write except for. Adjoint ().
Eigen                           //Matlab
r.adjoint ()                        //R '
r.transpose ()                      //R. ' or Conj (R ')
r.diagonal ()                       Diag (R)
x.asdiagonal ()                     //Diag (x)
r.transpose (). Colwise (). reverse ();//Rot90 (R)
R.conjugate ()                      //Conj (R)
1 2

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.