Eigen的基础使用
2017-07-20 00:00
288 查看
#Eigen的安装
下载Eigen以后直接引用头文件即可,需要的头文件如下
Eigen支持的编译器类型
GCC, version 4.4 and newer.
MSVC (Visual Studio), 2010 and newer. (the old 3.1 version of Eigen supports MSVC 2008).
Intel C++ compiler. Enabling the -inline-forceinline option is highly recommended.
LLVM/CLang++, version 3.4 and newer. (The 2.8 version used to work fine, but it is not tested with up-to-date versions of Eigen)
XCode 4 and newer. Based on LLVM/CLang.
MinGW, recent versions. Based on GCC.
QNX's QCC compiler.
接下来把文件解压,将解压后的文件放到你的工程项目目录下
填写cmake
VS下的配置
接下来测试代码:
即可测试成功
下载Eigen以后直接引用头文件即可,需要的头文件如下
Eigen支持的编译器类型
GCC, version 4.4 and newer.
MSVC (Visual Studio), 2010 and newer. (the old 3.1 version of Eigen supports MSVC 2008).
Intel C++ compiler. Enabling the -inline-forceinline option is highly recommended.
LLVM/CLang++, version 3.4 and newer. (The 2.8 version used to work fine, but it is not tested with up-to-date versions of Eigen)
XCode 4 and newer. Based on LLVM/CLang.
MinGW, recent versions. Based on GCC.
QNX's QCC compiler.
接下来把文件解压,将解压后的文件放到你的工程项目目录下
填写cmake
cmake_minimum_required( VERSION 2.8 ) project( useEigen ) set( CMAKE_BUILD_TYPE "Release" ) set( CMAKE_CXX_FLAGS "-O3" ) # 添加Eigen头文件 include_directories( "/usr/include/eigen3" ) # in osx and brew install # include_directories( /usr/local/Cellar/eigen/3.3.3/include/eigen3 ) add_executable( eigenMatrix eigenMatrix.cpp )
VS下的配置
接下来测试代码:
#include <iostream> using namespace std; #include <ctime> // Eigen 部分 #include <Eigen/Core> // 稠密矩阵的代数运算(逆,特征值等) #include <Eigen/Dense> #define MATRIX_SIZE 50 /**************************** * 本程序演示了 Eigen 基本类型的使用 ****************************/ int main( int argc, char** argv ) { // Eigen 中所有向量和矩阵都是Eigen::Matrix,它是一个模板类。它的前三个参数为:数据类型,行,列 // 声明一个2*3的float矩阵 Eigen::Matrix<float, 2, 3> matrix_23; // 同时,Eigen 通过 typedef 提供了许多内置类型,不过底层仍是Eigen::Matrix // 例如 Vector3d 实质上是 Eigen::Matrix<double, 3, 1>,即三维向量 Eigen::Vector3d v_3d; // 这是一样的 Eigen::Matrix<float,3,1> vd_3d; // Matrix3d 实质上是 Eigen::Matrix<double, 3, 3> Eigen::Matrix3d matrix_33 = Eigen::Matrix3d::Zero(); //初始化为零 // 如果不确定矩阵大小,可以使用动态大小的矩阵 Eigen::Matrix< double, Eigen::Dynamic, Eigen::Dynamic > matrix_dynamic; // 更简单的 Eigen::MatrixXd matrix_x; // 这种类型还有很多,我们不一一列举 // 下面是对Eigen阵的操作 // 输入数据(初始化) matrix_23 << 1, 2, 3, 4, 5, 6; // 输出 cout << matrix_23 << endl; // 用()访问矩阵中的元素 for (int i=0; i<2; i++) { for (int j=0; j<3; j++) cout<<matrix_23(i,j)<<"\t"; cout<<endl; } // 矩阵和向量相乘(实际上仍是矩阵和矩阵) v_3d << 3, 2, 1; vd_3d << 4,5,6; // 但是在Eigen里你不能混合两种不同类型的矩阵,像这样是错的 // Eigen::Matrix<double, 2, 1> result_wrong_type = matrix_23 * v_3d; // 应该显式转换 Eigen::Matrix<double, 2, 1> result = matrix_23.cast<double>() * v_3d; cout << result << endl; Eigen::Matrix<float, 2, 1> result2 = matrix_23 * vd_3d; cout << result2 << endl; // 同样你不能搞错矩阵的维度 // 试着取消下面的注释,看看Eigen会报什么错 // Eigen::Matrix<double, 2, 3> result_wrong_dimension = matrix_23.cast<double>() * v_3d; // 一些矩阵运算 // 四则运算就不演示了,直接用+-*/即可。 matrix_33 = Eigen::Matrix3d::Random(); // 随机数矩阵 cout << matrix_33 << endl << endl; cout << matrix_33.transpose() << endl; // 转置 cout << matrix_33.sum() << endl; // 各元素和 cout << matrix_33.trace() << endl; // 迹 cout << 10*matrix_33 << endl; // 数乘 cout << matrix_33.inverse() << endl; // 逆 cout << matrix_33.determinant() << endl; // 行列式 // 特征值 // 实对称矩阵可以保证对角化成功 Eigen::SelfAdjointEigenSolver<Eigen::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 这个方程 // N的大小在前边的宏里定义,它由随机数生成 // 直接求逆自然是最直接的,但是求逆运算量大 Eigen::Matrix< double, MATRIX_SIZE, MATRIX_SIZE > matrix_NN; matrix_NN = Eigen::MatrixXd::Random( MATRIX_SIZE, MATRIX_SIZE ); Eigen::Matrix< double, MATRIX_SIZE, 1> v_Nd; v_Nd = Eigen::MatrixXd::Random( MATRIX_SIZE,1 ); clock_t time_stt = clock(); // 计时 // 直接求逆 Eigen::Matrix<double,MATRIX_SIZE,1> x = matrix_NN.inverse()*v_Nd; cout <<"time use in normal inverse is " << 1000* (clock() - time_stt)/(double)CLOCKS_PER_SEC << "ms"<< endl; // 通常用矩阵分解来求,例如QR分解,速度会快很多 time_stt = clock(); x = matrix_NN.colPivHouseholderQr().solve(v_Nd); cout <<"time use in Qr decomposition is " <<1000* (clock() - time_stt)/(double)CLOCKS_PER_SEC <<"ms" << endl; return 0; }
即可测试成功
相关文章推荐
- 使用MIDP2.0开发游戏 (1) GameCanvas基础
- Rational Robot 基础使用手册(一)——概述
- Rational Robot 基础使用手册(四)
- Rational Robot 基础使用手册(七)
- Rational Robot 基础使用手册(九)
- Rational Robot 基础使用手册(十一)
- Rational Robot 基础使用手册(十二)
- Delphi基础:回调函数及其使用
- Eclipse基础--使用links方式安装Eclipse插件
- Eclipse基础--使用links方式安装Eclipse插件
- Unix基础之 TCP/IP管理及使用
- [软件架构训练基础教程-10]使用标准
- NHibernate0.7 发布,软件基础开发平台以及在软件基础开发平台中使用O/R Mapping
- 在C#中使用异步Socket编程实现TCP网络服务的C/S的通讯构架(一)----基础类库部分
- Eclipse基础--使用links方式安装Eclipse插件
- Cookie使用基础
- Eclipse基础--使用links方式安装Eclipse插件
- 企业信息化系统基础——AD:使用C#批量创建帐号
- Rational Robot 基础使用手册(十三)
- Eclipse基础--使用links方式安装Eclipse插件