basic functions in the Eigen
Definition of matrices in Eigen
#include <Eigen/Dense> //Basic functions only need to contain this header file
matrix<double, 3, 3> A; A matrix that has a fixed number of rows and columns is consistent with Matrix3D.
Matrix<double, 3, dynamic> B; Fixed number of rows.
Matrix<double, Dynamic, dynamic> C; Consistent with Matrixxd.
Matrix<double, 3, 3, rowmajor> E; Store by Row; Default by Column storage.
matrix3f P, Q, R; 3x3 float matrix.
vector3f x, y, Z; 3x1 float column vector.
Rowvector3f A, B, C; 1x3 float line vector.
Vectorxd v; Dynamic length type double column vector
//Eigen //Matlab //Comments
x.size () //Length (x) //Vector length
C.rows () //Size (c,1) //Matrix rows
c.cols () //Size (c,2) //Number of matrix columns
x (i) //X (i+1) Subscript 0 Start
C (i,j) //C (i+1,j+1) //
Basic use method of matrix in Eigen
A.resize (4, 4); Triggers a run-time error if out of bounds.
B.resize (4, 9); Triggers a run-time error if out of bounds.
A.resize (3, 3); Ok; No cross-border.
B.resize (3, 9); Ok; No cross-border.
A << 1, 2, 3, //Initialize A. The elements can also be
4, 5, 6, //matrices, which is stacked along cols
7, 8, 9; And then the rows is stacked.
B << A, A, A; B is three horizontally stacked A ' s. Three lines A
A.fill (ten); Fill A with all ten ' s. All 10
Eigen Special Matrix Generation
Eigen //Matlab
matrixxd::identity (rows,cols) //Eye (rows,cols) unit matrix
c.setidentity (rows,cols) //C = Eye (rows,cols) unit matrix
Matrixxd::zero (Rows,cols) //Zeros (rows,cols) 0 matrix
C.setzero (rows,cols) //C = ones (rows,cols) 0 matrix
Matrixxd::ones (Rows,cols) //Ones (Rows,cols) all one matrix
c.setones (rows,cols) //C = Ones (rows,cols) all one matrix
matrixxd::random (Rows,cols) //rand (Rows,cols) *2-1 //element randomly in -1->1
c.setrandom (rows,cols) //C = rand (rows,cols) *2-1 Ibid
. vectorxd::linspaced (Size,low,high) //Linspace (low,high,size) ' linear distribution of arrays
v.setlinspaced (Size,low,high) //V = linspace (low,high,size) ' linear distribution of arrays
Eigen Matrix chunking
Eigen//Matlab X.head (n)//x (1:N) for array extraction before n [vector] X.head<n > ()//X (1:N) similarly x.tail (n)//x (End-n + 1:end) Same as x.tail<n> ( )//X (End-n + 1:end) The same x.segment (i, N)//x (I+1:i+n) Same as x.segment<n> ( i)//x (i+1:i+n) P.block (i, J, rows, cols)//P (I+1:i+rows, J+1:j+cols) i,j start, Rows row cols Column p.block<rows, cols> (i, J)//P (I+1:i+rows, J+1:j+cols) i,j start, Rows cols column p.row (i) P (i+1,:) i row P.col (j)//P (:, j+1) J Column p.leftcols<cols> ()//P (:, 1:C OLS) left cols column p.leftcols (cols)//P (:, 1:cols) left cols column p.middlecols<cols> (j)//P (:, j+1 : J+cols) Middle from J Number cols column P.middlecols (j, cols)//P (:, J+1:j+cols) Middle from J Number cols column p.rightcols<cols> () P (:, End-cols+1:end) Right cols column p.rightcols (cols)//P (:, end-cols+1:end) Right cols column p.toprows<rows> ()//P ( 1:rows,:) same column p.toprows (rows)//P (1:rows,:) same column p.middlerows<rows> (i)//P (i+1:i+rows ,:) same column p.middlerows (i, rows)//P (i+1:i+rows,:) same column p.bottomrows<rows> ()//P (end-rows+1:e nd,:) same column p.bottomrows (rows)//P (end-rows+1:end,:) same column p.topleftcorner (rows, cols)//P (1:rows, 1:co LS) upper left corner rows row, cols column p.toprightcorner (rows, cols)//P (1:rows, end-cols+1:end) on the right corner rows row, cols column p.bottomleftcorner (rows, c OLS)//P (end-rows+1:end, 1:cols) lower left corner rows row, cols column p.bottomrightcorner (rows, cols)//P (End-rows+1:end, End-cols+1:end ) Lower right corner rows, cols column p.topleftcorner<rows,cols> ()//P (1:rows, 1:cols) ibid. p.toprightcorner<rows,cols> ()/
/P (1:rows, End-cols+1:end) ibid. p.bottomleftcorner<rows,cols> ()//P (end-rows+1:end, 1:cols) Ibid. P.bottomrightcorner<rows,cols> ()//P (End-rows+1:end, end-cols+1:end) Ibid.
Eigen Matrix Element Exchange
Eigen //Matlab
r.row (i) = P.col (j); R (i,:) = P (:, i) interchange is listed as row
r.col (J1). Swap (Mat1.col (J2)); R (:, [J1 J2]) = R (:, [J2, J1]) interchange column
Eigen Matrix Transpose
views, transpose, etc; All read-write except for. Adjoint ().
Eigen //Matlab
r.adjoint () //R ' adjoint matrix
r.transpose () //R ' or Conj (R ') transpose
r.diagonal () //Diag (R) diagonal
x.asdiagonal () //diag (x) diagonal Array (without overloading <<)
r.transpose (). Colwise (). reverse (); /Rot90 (R) All elements turn counterclockwise 90 degrees
r.conjugate () //Conj (R) Conjugate matrix
Eigen Matrix Product
Consistent with MATLAB, but Matlab does not support *= and other forms of operation.
Matrix-vector. Matrix-matrix. Matrix-scalar.
Y = m*x; R = p*q; R = p*s;
a = b*m; R = p-q; R = s*p;
A *= M; R = P + Q; R = p/s;
R *= Q; R = s*p;
R + = Q; R *= S;
R-= Q; R/= S;
Eigen Matrix single element operation
vectorized operations on each element independently//Eigen//Matlab R = p.cwiseproduct (Q); R = P. * Q corresponds to points multiplied by R = P.array () * S.array ();//R = P. * s corresponds to point multiplied by R = p.cwisequotient (q); r = P./q The corresponding point division R = P.array ()/Q.array ();//R = P./q corresponds to the Division R = P.array () + S.array ();//R = p + s corresponding points add r = P.array () -S.array ();//R = p-s corresponding point subtraction r.array () + = s; R = R + S full plus s r.array ()-= s; R = R-s Total minus s R.array () < Q.array (); R < Q The following is the operation of a single element of the Matrix R.array () <= Q.array (); R <= Q Matrix element comparison, will be placed in the corresponding position 0 or 1 r.cwiseinverse (); 1./P R.array (). Inverse (); 1./P R.array (). Sin ()//Sin (P) r.array (). cos ()//cos (P) r.array (). POW (s)//p. ^ S R.A Rray (). Square ()//p. ^ 2 R.array (). Cube ()//p. ^ 3 r.cwisesqrt ()//sqrt (P) r.array (). sqrt () sqrt (P) r.array (). exp ()//exp (P) r.array (). log ()//log (P) R.cwisemax (p)// Max (R, PThe corresponding fetch large R.array (). Max (P.array ())//MAX (R, p) corresponds to taking the large r.cwisemin (p)//min (r, p) corresponding to the small R.array (). Min (P.array ())/ /min (R, p) corresponds to take small r.cwiseabs ()//ABS (P) absolute Value R.array (). ABS ()//ABS (P) absolute Value R.CWISEABS2 () ABS (p.^2) absolute value Square r.array (). ABS2 ()//ABS (P.^2) absolute value squared (R.array () < s). Select (P,q); (R < s?) P:Q) This is also the operation of a single element
Eigen Matrix Simplification
Reductions.
int r, C;
Eigen //Matlab
R.mincoeff () //min (R (:)) min
R.maxcoeff () /MAX (R (:)) Max
s = R.mincoeff ( &r, &c) //[s, i] = min (R (:)); [R, C] = ind2sub (Size (R), i);
s = R.maxcoeff (&r, &c) //[s, i] = max (R (:)); [R, C] = ind2sub (Size (R), i);
R.sum () //SUM (R (:)) sum
r.colwise (). SUM () //SUM (r) column sum 1xN
r.rowwise (). SUM () //SUM (r, 2) or SUM (R ') ' line sum Nx1
r.prod () //prod (R (:)) all product
r.colwise (). PROD () //prod (r) column product
r.rowwise (). Prod () //prod (r, 2) or prod (r ') ' line product
r.trace () //Trace (R) trace
R.all () //All (R (:)) and Operation
R.colwise (). All () /-All (R) and Operation
R.rowwise (). All () ( R, 2) and Operation
R.any () /any (R (:)) or operation C33/>r.colwise (). any () or any (r) or Operation
R.rowwise (). any () //Any (R, 2) or operation
Eigen Matrix Point Multiplication
Dot products, norms, etc.
Eigen //Matlab
x.norm () //Norm (x). Die
X.squarednorm () //Dot (x, x) squared and
X.dot (y) //dot (x, y)
x.cross (y) /Cross (x, y) Requires #include <Eigen/Geometry>
Eigen Matrix Type Conversions
Type conversion
//Eigen //Matlab
a.cast<double> (); Double (A)
a.cast<float> (); Single (A)
a.cast<int> (); Int32 (A) downward rounding
a.real (); Real (A)
a.imag (); Imag (A)
//If the original type equals destination type, no work was done
Eigen solving linear equations Group Ax = b
Solve Ax = b. Result stored in x. matlab:x = A \ b.
x = A.LDLT (). Solve (b)); #include <eigen/cholesky>ldlt decomposition method is actually an improvement of the Cholesky decomposition method
x = A.llt (). Solve (b)); A sym. p.d. #include <Eigen/Cholesky>
x = a.lu () . Solve (b)); Stable and fast. #include <Eigen/LU>
x = A.QR () . Solve (b)); No pivoting. #include <Eigen/QR>
x = A.SVD (). Solve (b)); Stable, slowest. #include <Eigen/SVD>
/. LDLT (), MATRIXL () and. MATRIXD ()
//. LLT () . MATRIXL ()
/. Lu (), MATRIXL () and. Matrixu ()/
. QR (), MATRIXQ () and . MATRIXR ()
//. SVD () - >. Matrixu (),. Singularvalues (), and. MATRIXV ()
Eigen Matrix characteristic values
//Eigen//Matlab a.eigenvalues (); Eig (a); characteristic value eigensolver<matrix3d> Eig (a); [Vec val] = Eig (A) eig.eigenvalues (); Diag (Val) is the same result as the front eig.eigenvectors (); eigenvector corresponding to VEC eigenvalues