Eigen帮助文档的地址:http://eigen.tuxfamily.org/dox/pages.html

Eigen的论坛:http://forum.kde.org/viewforum.php?f=74

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; //类似 //本质上是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; //基本操作
cout << matrix_23.row(2); //取第二行的元素
cout << matrix_23.col(1); //取第一列的元素
matrix_23.dot(matrix_23); //点积,对应元素相乘后相加,结果为一个数
matrix_23.cross(matrix_23); //叉乘,结果为一个矩阵 //乘法,不同类型需要显性的转换
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" << matrix_33.transpose() << endl; //转置
cout << "sum:" << matrix_33.sum() << endl; //各元素求和
cout << "trace:" << matrix_33.trace() << 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(matrix_33.transpose() * 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 * matrix_NN.transpose(); //保证半正定 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 = matrix_NN.colPivHouseholderQr().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;
}

CMakeLists.txt

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})

最新文章

  1. Goodbye 2016 总结与展望
  2. MVC实用架构设计(三)——EF-Code First(5):二级缓存
  3. angular路由
  4. 接触LLBL Gen Pro 对象关系映射框架后 前途变的一片光明
  5. 为Autodesk Viewer添加自定义工具条
  6. 【活动】不用买书,不用花钱,可以免费看HTML5入门连载了
  7. android学习笔记14——GridView、ImageSwitcher
  8. (Loadrunner)Abnormal termination, caused by mdrv process termination.(转)
  9. PHP的接口(interface)
  10. 垃圾回收 GC
  11. [ES6] 21. ESNext, ES6-Shim &amp; Node
  12. OSG事件回调
  13. 让 collabtive-11 支持中文
  14. 37条常用Linux Shell命令组合
  15. 救援模式(Rescue Mode)、单用户模式(Single-User Mode)、紧急模式(Emergency Mode)的区别与联系
  16. ctf writeup之程序员密码
  17. Dubbo架构设计及原理详解
  18. python一个命令开启http服务器
  19. Android抓包方法(转)
  20. vue-cli脚手架之webpack.base.conf.js

热门文章

  1. i春秋破译
  2. 【Devexpress】gridcontorl设置某个特定单元格不可编辑
  3. Python3 Scrapy 框架学习
  4. (Java)设计模式:行为型
  5. K8s安装乐维5.0应用部署文档
  6. CompletionService 使用小结
  7. 【Java SE】Day01 前言、入门程序、常量、变量
  8. 五年经验的前端社招被问:CPU 和 GPU 到底有啥区别?
  9. java定时任务Quartz整理
  10. Django框架:13、csrf跨站请求伪造、auth认证模块及相关用法