eigen的简单⽤法汇总1.⼀些基本运算
#include <iostream>
using namespace std;
#include <ctime>
//核⼼部分
#include <Eigen/Core>
//稠密矩阵的运算
#include <Eigen/Dense>
using namespace Eigen;
#define MATRIX_SIZE 50
int main() {
/
/eigen中的所有向量和矩阵都是Eigen::Matrix,三个参数为数据类型,⾏,列
//声明⼀个2*3的float矩阵
Matrix<float,2,3> matrix_23;
Matrix3d m1; //旋转矩阵3*3 双精度,也可改为f
AngleAxisd m2; //旋转向量 3*1
Vector3d m3; //欧拉⾓ 3*1
Quaterniond m4; //四元数 4*1
Isometry3d m5; //欧⽒变换矩阵 4*4
Affine3d m6; //仿射变换4*4
Projective3d m7; //射影变换4*4
//Vector3d 本质上还是Eigen::Matrix<double,3,1>即三维向量
Vector3d v_3d;
Matrix<float,3,1> vd_3d; //类似
define的基本用法//本质上是Eigen::Matrix<double,3,3>
Matrix3d matrix_33 = Matrix3d::Zero();  //初始化为0
//不确定矩阵⼤⼩,使⽤动态矩阵
Matrix<double, Dynamic,Dynamic> matrix_dynamic;
//更简单的:
MatrixXd matrix_x;
matrix_23 << 1,2,3,4,5,6;
cout << matrix_23 << endl;
v_3d << 3,2,1;
vd_3d << 4,5,6;
//乘法,不同类型需要显性的转换
Matrix<double,2,1> result = matrix_23.cast<double>() * v_3d;
cout << "[1,2,3;4,5,6]*[3,2,1]=\n" << result << endl;
/*******矩阵运算*********/
matrix_33 = Matrix3d::Random();
cout << "random matrix33:\n" << matrix_33 << endl;
cout << "transpose:\n" << anspose() << endl; //转置
cout << "sum:" << matrix_33.sum() << endl; //各元素求和
cout << "trace:" << ace() << endl; //迹
cout << "times 10:\n" << matrix_33 * 10 << endl; //数乘
cout << "inverse:\n" << matrix_33.inverse() << endl; //逆
cout << "det:" << matrix_33.determinant() << endl; //⾏列式
/***********************/
//特征值
//实对称矩阵可以保证对⾓化成功
SelfAdjointEigenSolver<Matrix3d> eigen_solver(anspose() * matrix_33);
cout << "eigen values = \n" << eigen_solver.eigenvalues() << endl;
cout << "Eigen vectors = \n" << eigen_solver.eigenvectors() << endl;
//解⽅程
//求解matrix_nn * x = v_Nd这个⽅程
//直接求逆最直接,但是运算较⼤
Matrix<double,MATRIX_SIZE,MATRIX_SIZE> matrix_NN
= MatrixXd::Random(MATRIX_SIZE,MATRIX_SIZE);
matrix_NN = matrix_NN * anspose(); //保证半正定
Matrix<double,MATRIX_SIZE,1> v_Nd = MatrixXd::Random(MATRIX_SIZE,1);
clock_t time_str = clock();
//直接求逆
Matrix<double,MATRIX_SIZE,1> x = matrix_NN.inverse() * v_Nd;
cout << "time is:" << 1000*(clock() - time_str) / (double) CLOCKS_PER_SEC << "ms" << endl;    cout << "x=" << x.transpose() << endl;
//QR分解,速度快很多
time_str = clock();
x = lPivHouseholderQr().solve(v_Nd);
cout << "time is:" << 1000*(clock() - time_str) / (double) CLOCKS_PER_SEC << "ms" << endl;    cout << "x=" << x.transpose() << endl;
//对于正定矩阵,还可以⽤cholesky分解来解⽅程
time_str = clock();
x = matrix_NN.ldlt().solve(v_Nd);
cout << "time is:" << 1000*(clock() - time_str) / (double) CLOCKS_PER_SEC << "ms" << endl;    cout << "x=" << x.transpose() << endl;
//旋转
double theta = n * 2 * M_PI / (poseNums * 4); // 1/4 圆
R = Eigen::AngleAxisd(theta, Eigen::Vector3d::UnitZ());
return 0;
}
<
cmake_minimum_required(VERSION 2.8.3)
project (main)
set(CMAKE_BUILD_TYPE "Release")
set(CMAKE_CXX_FLAGS "-O3")
add_definitions(-std=c++11)
include_directories(inc)
aux_source_directory(src DIR_SRCS)
SET(SOUR_FILE ${DIR_SRCS})
include_directories("/usr/include/eigen3")
add_executable(main ${SOUR_FILE})

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。