Eigen实例操作
来源:互联网 发布:矩阵的1范数,2范数 编辑:程序博客网 时间:2024/06/03 17:36
empty
前提提示:该代码涵盖了Eigen3的部分基本操作,包括矩阵常用操作,线性矩阵的分解,稀疏矩阵的基本操作及分解和空间
转换的一些操作(如:旋转向量,欧式转换矩阵,四元数,旋转矩阵)。贴出的代码并没有涵盖Eigen3的所有操作,
更加深入的学习请参考Eigen官网。
代码亲测可用。
该页面分为四个板块:矩阵的基本操作,
矩阵的分解方法实例稀疏矩阵的操作稀疏矩阵的求解几何模块(三维空间旋转)各位可以有针对性的查看1.矩阵的基本操作
#include<iostream>#include <Eigen/Dense>#include <Eigen/Core>#include <Eigen/LU>using namespace std;voidbase(){ Eigen::Matrix3d m=Eigen::Matrix3d::Random(3,3); cout<<"the matrixXd is"<<m<<endl; Eigen::Matrix3d n=(m+Eigen::Matrix3d::Constant(3,3,1.2))*50; cout<<"the matrix n is "<<n<<endl; Eigen::Vector3d v(1,1,3); v.asDiagonal(); //v<<1,2,3; Eigen::RowVector2d vv(2,4); cout<<" the vector is "<<v<<endl; cout<<" the vector vv is "<<vv<<endl; cerr<<"Matrix cols and row are "<<m.cols()<<" "<<m.rows()<<"\nthe size is "<<m.SizeAtCompileTime<<endl;// float data[] = {1,2,3,4};// Map<Vector3f> v1(data); // uses v1 as a Vector3f object// Map<ArrayXf> v2(data,3); // uses v2 as a ArrayXf object// Map<Array22f> m1(data); // uses m1 as a Array22f object// Map<MatrixXf> m2(data,2,2); // uses m2 as a MatrixXf object//cout<<v1<<endl;//cout<<v2<<endl;cout<<m1<<endl;cout<<m2<<endl;// Eigen::Matrix2cf mat;// mat<<1.00256-3.256,2+0,// 3.015-0.12,5.178-9541i;// cerr<<"mat is" <<mat<<endl; Eigen::Matrix3d m1; m1<<1,2,1,8,0,9,7,33,47; cout<<"m1 "<<m1<<endl; cout<<"m1 transpose "<<m1.transpose()<<endl; cout<<"m1 conjugate "<<m1.conjugate()<<endl; cout<<"m1 adjoint "<<m1.adjoint()<<endl; Eigen::SelfAdjointEigenSolver<Eigen::Matrix3d> eigen_solver(m1.transpose()*m1); cerr<<"eigen value "<<eigen_solver.eigenvalues()<<endl; cerr<<"eigen vector "<<eigen_solver.eigenvectors()<<endl; // Eigen::Array<float,1,2> a; Eigen::Array<float,2,2>a; a<<4, 5, 8, 1; cout<<"a is \n"<<a<<endl; cerr<<"a inverse"<<a.inverse()<<endl; // cout<<"a array max "<<a.pow()<<endl<<endl; Eigen::Matrix<float,2,2> mt,mt1; mt<<4,-5,8,1; mt1<<-4,5,8,-1; mt.col(0).swap(mt1.col(0)); cout<<"mt min coefficient "<<mt.minCoeff()<<" "<<"mt max coeffcient "<<mt.maxCoeff()<<"cols max "<<mt.colwise().maxCoeff()<<"rows min"<<mt.rowwise().minCoeff()<<endl; cout<<"mt is \n"<<mt<<endl; cout<<"mt1 is "<<mt1<<endl; cout<<"mt inverse is\n"<<mt.inverse()<<endl;; cout<<" ABS suqare \n"<<mt.cwiseAbs2()<<endl; cout<<" matrix inverse \n"<<mt.cwiseInverse()<<endl; cout<<"matrix sqrt \n"<<mt.cwiseSqrt()<<endl; cout<<"matrix square \n"<<mt.array().square()<<endl; cout<<"matrix ABS IS\n"<<mt.cwiseAbs()<<endl; cout<<" is queal \n"<<mt.cwiseEqual(mt1)<<endl; Eigen::MatrixXd matrix1=Eigen::MatrixXd::Random(8,8); cout<<"matrix1 is \n"<<matrix1<<endl; cout<<"matrix1 block is \n"<<matrix1.block<1,1>(1,1)<<endl; cout<<"matrix1 topLeftCorner is\n"<<matrix1.topLeftCorner(3,2)<<endl; cout<<"matrix1 bottomLeftCorner is \n"<<matrix1.bottomLeftCorner(2,3)<<endl; cout<<"matrix1 topRightCorner is \n"<<matrix1.topRightCorner(2,3)<<endl; cout<<"matrix1 bottomRightCorner is \n"<<matrix1.bottomRightCorner(4,3)<<endl; cout<<"matrix1 toprows is \n"<<matrix1.topRows(2)<<endl; cout<<"matrix1 bottomrows is \n"<<matrix1.bottomRows(3)<<endl; cout<<"matrix1 leftCols is \n"<<matrix1.leftCols(1)<<endl; cout<<"matrix1 rightCols is \n"<<matrix1.rightCols(2)<<endl; cout<<"replicate 1 time matrix1 \n"<<matrix1.replicate(1,2)<<endl<<endl; cout<<"replicate matrix1 rowwise is \n"<<matrix1.rowwise().replicate(1)<<endl; cout<<"matrix colwise reverse is \n"<<matrix1.colwise().reverse()<<endl; cout<<"matrix define col reverse is \n"<<matrix1.col(0).reverse()<<endl; Eigen::Matrix<int,3,3> matrix2=Eigen::Matrix<int ,3,3>::Random(); Eigen::Matrix3Xi identity(3,3); identity.col(0)<<0,0,1;identity.col(1)<<0,1,0;identity.col(2)<<1,0,0; cout<<"matrix2 is \n"<<matrix2<<endl; cout<<"identity is \n"<<identity<<endl; cout<<"change cols \n"<<matrix2*identity<<endl; cout<<"change rows is \n"<<(Eigen::Matrix<int,3,3> (3,3)<<0,0,1,0,1,0,1,0,0).finished()*matrix2<<endl;// Eigen::MatrixXf mrt(2,2); mrt<<1,2, 3,4; Eigen::MatrixXf::Index maxrow,maxcol; float max1=mrt.maxCoeff(&maxrow,&maxcol);//如果忽略该句则不能输出正确的maxCoeff()元素位置索引值;很奇怪!!! cerr<<"mrt is \n"<<mrt<<endl; cout<<"mrt maxmum coefficient is \n"<<mrt.maxCoeff(&maxrow,&maxcol)<<" and the position(x,y)is ("<<maxrow<<" ,"<<maxcol<<")"<<endl;//fail to putout right 'Index'; cout<<"mrt normal is "<<mrt.norm()<<"; mrt square normal is "<<mrt.squaredNorm()<<"; mrt lp norm is "<<mrt.lpNorm<1>()<<"; mrt lp2 norm is "<<mrt.lpNorm<2>()<<endl; cout<<"mrt lp3 norm is "<<mrt.lpNorm<3>()<<"mrt lp4 norm is "<<mrt.lpNorm<4>()<<endl;//lpNorm<n>含义是先求矩阵各个元素绝对值的n次方幂的和然后开n次根式,即求出n的范数 Eigen::MatrixXf mm(2,2); mm << 1, 2, 3, 4; //get location of maximum Eigen::MatrixXf::Index maxRow, maxCol; float max = mm.maxCoeff(&maxRow, &maxCol); //get location of minimum Eigen::MatrixXf::Index minRow, minCol; float min = mm.minCoeff(&minRow, &minCol); cout << "Max: " << max << ", at: " << maxRow << "," << maxCol << endl; cout << "Min: " << min << ", at: " << minRow << "," << minCol << endl;}2. 矩阵的分解方法实例#include <iostream>#include <Eigen/Dense>#include <Eigen/Eigen>using namespace std;void linear_algebra_decomposition(){ Eigen::Matrix2f mat,b; mat<<2,-1,-1,3; b<<1,2,3,1; Eigen::ColPivHouseholderQR<Eigen::Matrix2f> dec(mat); Eigen::MatrixXf result=dec.solve(b); Eigen::FullPivHouseholderQR<Eigen::Matrix2f> dec_full(mat); Eigen::FullPivLU<Eigen::Matrix2f>dec_lu(mat); Eigen::Matrix2f result_full=dec_full.solve(b); Eigen::LLT<Eigen::Matrix2f>dec_llt(mat); //LLT LDLT require martix 'mat' positive definite Eigen::LDLT<Eigen::Matrix2f>dec_ldlt(mat); Eigen::Matrix2f result_llt=dec_llt.solve(b); Eigen::Matrix2f result_ldlt=dec_ldlt.solve(b); Eigen::Matrix2f result_lu=dec_lu.solve(b); Eigen::Matrix2f result111=dec_full.compute(mat).solve(b);//使用‘compute()可以计算mat*x=b,尤其适合计算多个方程 //Eigen::Matrix2f result_llt_inplace=dec_llt.solveInPlace(b); Eigen::MatrixXf q;q=dec.matrixQ();//q=dec.matrixR().triangularView<Eigen::Upper>();获得R的三角阵 Eigen::Matrix2f r=dec.matrixR(); cout<<"mat decompositon by colPivHouseholder is \n"<<result<<endl;cout<<"Q IS \n"<<q<<"\n R is \n"<<r<<endl; cout<<"mat decompositon by fullpivlu is \n"<<result_lu<<endl; Eigen::Matrix2f l=dec_lu.matrixLU().triangularView<Eigen::Lower>(); Eigen::Matrix2f u=dec_lu.matrixLU().triangularView<Eigen::Upper>(); cout<<"lu is \n"<<dec_lu.matrixLU()<<endl;cout<<"L is \n"<<l<<"\n U is \n"<<u<<endl; Eigen::Matrix2f Q=dec_full.matrixQ(); cout<<"mat decompostion by fullpivhouseholder is \n"<<result_full<<endl;cout<<"Q by fullpivhouseholder is \n"<<Q<<endl; Eigen::Matrix2f L=dec_llt.matrixL(); cout<<"mat decompostion by LLT is \n"<<result_llt<<"\n LLt is \n"<<dec_llt.matrixLLT()<<"\n L by LLT is\n" <<L<<endl; cout<<"mat decompositon by LDLT is \n"<<result_ldlt<<endl; cout<<"mat decomposition by LDLT compute is \n"<<result111<<endl; cout<<"mat rank by LDLT is "<<dec_full.rank(); cout<<" mat is inversible??? "<<dec_full.isInvertible()<<endl;//通过分解的方法计算矩阵的秩rank()和判断是否可逆invertible //cout<<"mat decomposition by solveinplace is \n"<<result_llt_inplace<<endl; Eigen::MatrixXf result1=mat.colPivHouseholderQr().solve(b);//效果同上,注意‘col'和’Qr‘的大小写 Eigen::Matrix2f result1_full=mat.fullPivHouseholderQr().solve(b); Eigen::Matrix2f result1_llt=mat.llt().solve(b); Eigen::Matrix2f result1_ldlt=mat.ldlt().solve(b); cout<<"mat decomposition another result by colpivhouseholder is \n"<<result1<<endl; cout<<"mat decomposition another resslut by fullpivhouseholder is \n"<<result1_full<<endl; cout<<"mat decomposition another result by llt is \n"<<result1_llt<<endl; cout<<"mat decomposition another result bu ldlt is \n"<<result1_ldlt<<endl; float relative_error=(result*mat-b).norm()/b.norm(); cout<<"relative error is \n"<<(result*mat-b).norm()/b.norm()<<endl; cout<<"relative error is \n"<<relative_error<<endl; Eigen::Matrix2f vector; vector<<-0.851,-0.526,0.526,-0.851; Eigen::Matrix2f dialog; dialog<<-0.236,0,0,4.24; Eigen::MatrixXf origin_mat=vector*dialog*vector.inverse(); cout<<"the origin is \n"<<origin_mat<<endl; Eigen::Matrix3f det; det<<1,2,1, 2,1,0, -1,1,2; //求矩阵'det'的逆和行列式determinant(); cout<<"det determinant is \n"<<det.determinant()<<endl; cout<<"det inverse is \n"<<det.inverse()<<endl;// //关于JacobiSVD注意事项:分解出来的矩阵U和V的计算需要输入的矩阵类型是动态的,即使用// Eigen::JacobiSVD<Eigen::Matrix<float,Eigen::Dynamic,Eigen::Dynamic> >// 或者Eigen::JacobiSVD<Eigen::MatrixXf>;// 方程的求解必须用到U和V,因此JacobiSVD的对象必须有// Eigen::ComputeThinU|Eigen::ComputeThinV或Jac(entry,Eigen::ComputeFullU|Eigen::ComputeFullV)// Eigen::Matrix<float,Eigen::Dynamic,Eigen::Dynamic> entry(3,2); entry<<0.68,0.597, -0.211,0.823, 0.566,-0.605; cout<<"entry is \n"<<entry<<endl; Eigen::JacobiSVD<Eigen::Matrix<float,Eigen::Dynamic,Eigen::Dynamic> >Jac(entry,Eigen::ComputeFullU|Eigen::ComputeFullV); Eigen::Vector3f rtf(1,0,0); cout<<"vector rtf is \n"<<rtf<<endl; if(Jac.computeU()&&Jac.computeV())//判断是否计算U和V { cout<<"JacobiSVD decomposition U is \n"<<Jac.matrixU()<<endl; cout<<"JacobiSVD decomposition V is \n"<<Jac.matrixV()<<endl; } cout<<"JacobiSVD decomposition singleValue is \n"<<Jac.singularValues()<<endl; cout<<"JacobiSVD decomposition Jac*x=rtf solve is \n"<<Jac.solve(rtf)<<endl; Eigen::Matrix2f value; value<<1.19,0, 0,0.899; Eigen::Matrix<float,3,2> left; left<<0.388,0.866,0.712,-0.0634,-0.586,0.496; Eigen::Matrix2f right; right<<-0.183,0.983,0.983,0.183; Eigen::Matrix<float,3,2> origin_m=left*value*right.transpose(); cout<<"value "<<value<<endl;cout<<"right "<<right<<endl;cout<<"left "<<left<<endl;cout<<"right transpose is "<<right.transpose()<<endl;cout<<"\n\n\n"<<left*value; cout<<"origin_m ix \n"<<origin_m<<endl;}
3.稀疏矩阵的操作#include <iostream>#include <Eigen/Eigen>#include <Eigen/Sparse>using namespace std;using namespace Eigen;voidspare_matrix(){ Eigen::SparseMatrix<double,Eigen::RowMajor> mat(1000,2000); //initial sparse matrix.the first method. Eigen::SparseMatrix<double>mat1(4,4); vector<Eigen::Triplet<double> >triplet; int row[3]={0,1,2};//matrix 0 6.1 0 0 int col[3]={1,2,2};// 0 0 7.2 0 double value[3]={6.1,7.2,8.3};// 0 0 8.3 0 for(size_t t=0;t<3;t++)// 0 0 0 0 { triplet.push_back(Eigen::Triplet<double>(row[t],col[t],value[t])); } cout<<"triple row clo value\n"; for(size_t i=0;i<triplet.size();i++) { cout<<" "<<triplet[i].row()<<" "<<triplet[i].col()<<" "<<triplet[i].value()<<endl; } mat1.setFromTriplets(triplet.begin(),triplet.end()); cout<<"mat after compressed is \n"<<mat1<<endl; cout<<"mat row col and size after compressed is \n"<<mat1.rows()<<" "<<mat1.cols()<<" "<<mat1.size()<<endl;//initial sparse matrix ,THE SECOND METHOD. int row2=10; int col2=10; Eigen::SparseMatrix<double> mat2(row2,col2); vector<Eigen::Triplet<double> >triplet2; int estimate_of_entery=8; int i=1; triplet2.reserve(estimate_of_entery); for (size_t j=0;j<estimate_of_entery;++j) { int valu=j; triplet2.push_back(Eigen::Triplet<double>(j,i,valu)); } mat2.setFromTriplets(triplet2.begin(),triplet2.end()); cout<<"mat2 after compressed is \n"<<mat2; cout<<"mat2 row col and size after compressed is \n"<<mat2.rows()<<" "<<mat2.cols()<<" "<<mat2.size()<<endl; //initial sparse matrix, THE THIRD METHOD. int row3=10; int col3=10; Eigen::SparseMatrix<double> mat3(row3,col3); mat3.reserve(Eigen::VectorXf::Constant(col3,6));//为每列预留的空间“_” for(size_t i=0;i<3;i++) for(size_t j=0;j<3;j++) { int val=i+j+1; mat3.insert(i,j)=val;//直接在矩阵中插入非零元素。 } cout<<"mat3 before compressed.\n"<<mat3<<endl; mat3.makeCompressed(); cout<<"mat3 after compressed.\n"<<mat3<<endl; cout<<"mat3 innersize outersize nonzeros \n"<<" "<<mat3.innerSize()<<" "<<mat3.outerSize()<<" "<<mat3.nonZeros()<<endl;//nonZeros()非零元素个数//访问稀疏矩阵中的元素 int col4=6; int row4=6; Eigen::SparseMatrix<double>mat4(col4,row4); mat4.reserve(6); vector<Eigen::Triplet<double> >triplet4; int c[]={0,0,1,1,2,2,2,3,3,4,4,5,5,5,5}; int r[]={0,3,2,4,0,1,4,2,5,1,4,0,3,4,5}; double v[]={1,3,3,2,2,1,6,5,2,4,1,1,1,1,1}; for(size_t i=0;i<15;i++) triplet4.push_back(Eigen::Triplet<double>(r[i],c[i],v[i])); mat4.setFromTriplets(triplet4.begin(),triplet4.end()); cout<<"mat4 is \n"<<mat4; mat4.coeffRef(1,2)=9; //coeff(i,j)只能返回矩阵索引值 // coeffRef(i,j)既能返回矩阵索引值又能修改 cout<<"visit certion element mat4[i,j] "<<mat4.coeffRef(1,2)<<" "<<mat4.coeff(1,2)<<endl; for(size_t i=0;i<mat4.outerSize();i++) for(Eigen::SparseMatrix<double>::InnerIterator it(mat4,i);it;++it)//内部迭代只读,不可写入,即不可赋值;must be ++it,it is error if it++; { //只访问非零元素,速度比coeffRef()快 cout<< it.value()<<" " <<it.col()<<" " <<it.row()<<" " <<it.index()<<endl;//索引值和row()相同 } cout<<"mat4 \n"<<mat4<<endl; //稀疏矩阵的一些操作// Eigen::Matrix<double,6,6>m=Eigen::Matrix<double,6,6>::Random(6,6); Eigen::Matrix<double ,6,6>mat5; mat5=mat4*m; cout<<"m matrix is \n"<<m<<endl; cout<<"sparse mat4 product with dense m is \n"<<mat5<<endl; Eigen::Matrix<double,6,6>mat6; mat5=mat4.cwiseProduct(m); cout<<"sparse mat4 product dense m is \n"<<mat5<<endl; mat4.leftCols(2)=mat4.rightCols(2); cout<<"sparse mat4 leftCol(2) is \n"<<mat4.leftCols(2)<<endl; Eigen::Matrix<double,6,2> mat7; mat7<<1,1,1,1,1,1,2,2,2,2,2,2; cout<<"mat7 is \n"<<mat7<<endl; //mat4.leftCols(2)=mat7; //error // cout<<"sprese mat4 leftCols(2) by mat7 is \n"<<mat4.leftCol(2)<<endl; cout<<"sparse mat4 topRow(2) is \n"<<mat4.topRows(2)<<endl; mat6=mat4; //稀疏矩阵和稠密矩阵不能直接相加,但是可以把稀疏矩阵转化为稠密矩阵再相加 mat6=mat6+m; cout<<"mat6 is \n"<<mat6; mat6=mat4.adjoint(); cout<<"mat6 adjoint is \n"<<mat6<<endl; cout<<"mat6 selfadjointView with upper is \n"<<mat4.selfadjointView<Eigen::Upper>()*m<<endl;//Mat4.selfadjointView<Eigen::Upper>(),得到mat4上三角(包括对角线) //和mat4转置矩阵的下三角矩阵。实数矩阵中adjoint是原矩阵的转置。 cout<<"mat6 selfAdjiontView with lower is \n"<<mat4.selfadjointView<Eigen::Lower>()*m<<endl; Eigen::SparseMatrix<double>mat44(row4,col4); mat44=mat4.selfadjointView<Upper>(); cout<<"mat4 is \n"<<mat4<<endl; cout<<"mat44 copied by mat4 is \n"<<mat44<<endl; //mat4.leftCols(2)=m;}
4.稀疏矩阵的求解#include <iostream>#include <Eigen/Eigen>#include <Eigen/Sparse>using namespace std;using namespace Eigen;voidsolving_sparse_linear_system(){ Eigen::SparseMatrix<double>sm(6,6); vector<Eigen::Triplet<double> >triplet; sm.reserve(6); int row[]={0,0,1,1,2,2,2,3,3,4,4,5,5,5,5}; int col[]={0,3,2,4,0,1,4,2,5,1,4,0,3,4,5}; int value[]={1,3,3,2,2,1,6,5,2,4,1,1,1,1,1}; for(size_t i=0;i<15;++i) { triplet.push_back(Eigen::Triplet<double>(row[i],col[i],value[i])); } sm.setFromTriplets(triplet.begin(),triplet.end()); cout<<"sm is \n"<<sm<<endl; Eigen::Matrix<double,6,2>b=Eigen::Matrix<double,6,2>::Random(6,2); cout<<"b is \n"<<b<<endl; Eigen::SparseLU<Eigen::SparseMatrix<double>,Eigen::AMDOrdering<int> >dec(sm); //提供了3中模式AMDOrdering COLAMDOrdering NaturalOrdering Eigen::SparseQR<Eigen::SparseMatrix<double>,Eigen::NaturalOrdering<int> >dec1; dec1.compute(sm); cout<<"sm result by sparseLU is \n"<<dec.solve(b)<<endl; cout<<"sm result by sparseQR is \n"<<dec1.solve(b)<<endl; Eigen::SparseMatrix<double> S = Eigen::MatrixXd::Random(6,6).sparseView(0.5,1);//区间越小非零元素个数越小 cout<<"s is\n"<<S<<endl; //S.makeCompressed(); dec1.compute(S); dec.compute(S); Eigen::SparseMatrix<double>Q;Eigen::SparseMatrix<double>R; Q=dec1.matrixQ();R=dec1.matrixR(); cout<<"sparse matrix S decomposition is \n"<<dec1.solve(b)<<endl;cout<<"matrixQ is \n"<<Q<<endl;cout<<"matrixR is \n"<<R<<endl; cout<<dec.solve(b)<<endl;}5.几何模块(三维空间旋转)#include <iostream>#include <Eigen/Eigen>#include <Eigen/Geometry>#include <Eigen/Dense>#include <Eigen/Core>#include "geo.h"using namespace std;using namespace Eigen;voidgeo(){ Eigen::AngleAxis<double> aa=Eigen::AngleAxis<double>(0.25*M_PI,Eigen::Vector3d(0,0,1)); AngleAxis<double>aa1=AngleAxisd(0.25*M_PI, Vector3d::UnitX()); cout<<"aa with angle 0.5pi and axis UnitX is \n"<<aa.angle()<<"\n "<<aa.axis()<<endl; cout<<"aa1 with angle 0.5pi and axis UnitX is \n"<<aa1.angle()<<" \n"<<aa1.axis()<<" "<<endl;Eigen::AngleAxis<float>aaf=aa.cast<float> (); //将double类型的aa 装换成float类型的aaf Eigen::Matrix3f mat=aaf.matrix(); //matrix()和toRotationMatrix()功能一样 cout<<"matrix of Angle-axis mat is \n"<<mat<<endl; mat=aaf.toRotationMatrix();//得到转化矩阵 Eigen::AngleAxis<float>mat1; mat1=aaf.fromRotationMatrix(mat);//从旋转矩阵中获得Angle-axis cout<<"\nmatrix of Angle-axis by toRotationMrtrix is \n "<<mat<<endl; cout<<"\nmatrix of Angle-axis by fromRotationMrtrix .the angel is \n "<<mat1.angle()<<" the axis is "<<mat1.axis().transpose()<<endl; cout<<" eulerAngler of rotation matrix is \n "<<mat.eulerAngles(0,1,2)<<endl;//获得旋转矩阵的欧拉角// Eigen::Matrix<float,3,3> mat_euler; mat_euler=Eigen::AngleAxis<float>(0.1*M_PI,Eigen::Vector3f::UnitX()) *Eigen::AngleAxis<float>(0.2*M_PI,Eigen::Vector3f::UnitY()) *Eigen::AngleAxis<float>(0.25*M_PI,Eigen::Vector3f::UnitZ()); cout<<"euler angle witn initialization angle_axis \n"<<mat_euler.eulerAngles(0,1,2)<<endl;// 0,1,2 stand for x,y,zcout.precision(3);//设置输出精度// Eigen::Vector3f vr(1,0,0); cout<<"\n Rotation matrix product vector(1,0,0) mat*vr is \n"<<mat*vr<<endl;//vr绕x轴逆时针旋转90度// cout<<"\n Angel-axis product vector(0,0,1) aaf*vr is \n"<<aaf*vr<<endl; Eigen::Quaternion<float>qua(Eigen::AngleAxis<float>(0.25*M_PI,Eigen::Vector3f(0,0,1))); Eigen::Quaternion<float>qua1(mat); // Eigen::Matrix3f matf=mat Eigen::Transform<float,3,Isometry>t,t1;Transform<float,3,Isometry>t2; t=Eigen::Transform<float,3,Isometry>::Identity(); //欧式转换先用Identity()单位矩阵初始化 t1=Eigen::Transform<float,3,Isometry>::Identity(); //必须要有,不然出错// t2=Eigen::Transform<float,3,Isometry>::Identity(); cout<<"t is \n"<<t.matrix()<<"\n t1 is \n"<<t1.matrix()<<"\n t2 is \n"<<t2.matrix()<<endl;// Eigen::Isometry3f t2=Eigen::Isometry3f::Identity();// Eigen::Isometry3f t=Eigen::Isometry3f::Identity(); //必须要有,不然出错//// Eigen::Isometry3f t1=Eigen::Isometry3f::Identity(); //欧式转换的旋转矩阵的初始化分三种: //3x3旋转矩阵;Angle-axis 旋转向量;四元数quaternion t.prerotate(mat); t.pretranslate(Eigen::Vector3f(1,3,4));//prerotate() and rotate()功能相同 t1.prerotate(qua); t1.pretranslate(Eigen::Vector3f(0,0,0)); t2.rotate(Eigen::AngleAxis<float>(0.25*M_PI,Eigen::Vector3f(0,0,1))); t2.pretranslate(Eigen::Vector3f(1,3,4)); cout<<"Quaternion product vector(1,0,0) qua*vr is \n"<<qua*vr<<endl; //不同的方式计算向量vr的旋转 cout<<"Quaternion product vector(1,0,0) qua1*vr is \n"<<qua1*vr<<endl; cout<<"Transform product vector(1,0,0) t*vr is \n"<<t*vr<<endl; cout<<"Transform product vector(1,0,0) t1*vr is \n"<<t1*vr<<endl; cout<<"Transform product vector(1,0,0) t2*vr is \n"<<t2*vr<<endl; cout<<"rotation \n"<<t.rotation()<<endl; //欧式变换的旋转矩阵rotation cout<<"rotation1\n"<<t.rotation()<<endl; cout<<"rotation2\n"<<t.rotation()<<endl; cout<<"Transform matrix is \n"<<t.matrix()<<endl; cout<<"Transform matrix1 is \n"<<t.matrix()<<endl; //欧式变换的转换矩阵transform cout<<"Transform matrix2 is \n"<<t.matrix()<<endl; Eigen::Quaternion<double> qa(1,2,3,8); //参数顺序:w x y z//但是输出仍然是x y z w// cout<<"quatern x y z w \n"<<qa.x()<<" "<<qa.y()<<" "<<qa.z()<<" "<<qa.w()<<endl; Eigen::Quaternion<double> qa1(Eigen::AngleAxis<double>(0.5*M_PI,Eigen::Vector3d::UnitX())); //Eigen::Vector4d v(qa1.Quaternion()); cout<<"qa1 quatern x y z w and vector coeffs \n"<<qa1.x()<<" "<<qa1.y()<<" "<<qa1.z()<<" "<<qa1.w()<<" \n"<<qa1.vec()<<" \n "<<qa1.coeffs()<<endl; cout<<"qa1 quatern conjugate is \n"<<qa1.conjugate().coeffs()<<endl; cout<<"qa1 quatern with _transfromVector is \n"<<qa1._transformVector(Eigen::Vector3d (1,0.5,0))<<endl; cout<<"qa1 inverse is \n"<<qa1.inverse().coeffs()<<endl; Eigen::Vector3d v(1,0.5,0); cout<<"vector rotate by qa1 is \n"<<qa1*v<<endl; //和_transformVector()相同 cout<<"quatren product qa1*qa1.inverse is \n"<<(qa1*qa1.inverse()).coeffs()<<endl; cout<<"quatren product module |qa1*qa1.inverse| is \n"<<(qa1).norm()<<endl;}
阅读全文
1 0
- Eigen实例操作
- Eigen矩阵操作库入门
- eigen库的基本操作
- Eigen
- Eigen
- Eigen sparse 基本操作:构造 & 输出
- 矩阵操作比较:Armadillo,Eigen,OpenCV
- Eigen教程3 - 稀疏矩阵操作
- Armadillo,Eigen,OpenCV 矩阵操作比较(Compare Armadillo, Eigen and OpenCV)
- Ubuntu下c++调用matlab eigen的入门实例
- NDK 开发实例二(添加 Eigen库)
- Eigen中与Matlab对应的 sort操作
- Eigen C++ 模板库,用来矩阵向量的线性操作
- c++处理矩阵操作:Eigen库初步学习使用
- Eigen之矩阵、向量、标量的操作运算
- 【Eigen】VS2012配置Eigen
- eigen 初探
- eigen教程
- spring源码分析(web)--通过注解获取bean源码分析
- python实现-求int型数据在内存中存储时1的个数
- 利用 Python 练习数据挖掘(鸢尾花练习)中遇到的问题,以及解决方法。
- ImageLoader源码解析(三) 线程调度
- DiskLruCache 达到最大设置的缓存,清理的逻辑
- Eigen实例操作
- 快速排序
- Qt5.7关于Quazip的编译和使用
- 2018搜狐笔试 Kolakoski数列问题
- 2、flask第二站-请求与相应
- K:(1)算法时间复杂度的简介
- TYPEERROR: UNDEFINED IS NOT A FUNCTION解决方法
- Tomcat使用JKS格式证书配置HTTPS
- Android Activity Launch Mode