Eigen基本用法
2017-03-19 14:51
232 查看
#include <iostream> using namespace std; #include <Eigen/Core> #include <Eigen/Dense> int main( ) { // 矩阵类模板的前三个参数为:数据类型,行,列 Eigen::Matrix< float, 2, 3 > matrix_23; // 填充数据 matrix_23 << 1, 2, 3, 4, 5, 6; // 输出: // 1 2 3 // 4 5 6 cout << matrix_23 << endl; // 访问元素 // 输出: // 1 // 2 for ( int i = 0; i < 1; i++ ) for ( int j = 0; j < 2; j++ ) cout << matrix_23( i, j ) << endl; // Vector3d实质上是Eigen::Matrix< double, 3, 1 > Eigen::Vector3d v_3d; v_3d << 3, 2, 1; // 矩阵和向量相乘,但不允许混合类型 Eigen::Matrix< double, 2, 1 > result = matrix_23.cast< double >( ) * v_3d; // 输出: // 10 // 28 cout << result << endl; Eigen::Matrix3d matrix_33 = Eigen::Matrix3d::Zero( ); matrix_33 = Eigen::Matrix3d::Random( ); cout << matrix_33 << 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.derterminant( ) << endl; // 特征值 Eigen::SelfAdjointEigenSolver< Eigen::Matrix3d > eigen_solver( matrix_33 ); cout << "Eigen values = " << eigen_solver.eigenvalues( ) << endl; cout << "Eigen vectors = " << eigen_solver.eigenvectors( ) << endl; // 解方程 // matrix_33 * x = v_3d // 直接求逆(运算量大) Eigen::Vector3d x = matrix_33.inverse( ) * v_3d; cout << "x = " << x << endl; // 矩阵分解法,如QR分解 x = matrix_33.colPivHouseholderQr( ).solve( v_3d ); cout << "x = " << x << endl; // 若不确定矩阵大小,可以使用动态大小的矩阵 Eigen::Matrix< double, Eigen::Dynamic, Eigen::Dynamic > matrix_dynamic; Eigen::MatrixXd matrix_x; return 0; }
相关文章推荐
- Eigen库的配置和基本用法
- NET命令的基本用法
- NET命令的基本用法
- Linux下vi的基本用法指南[zz]
- SQL语句基本的用法
- WEB标准开发中的一些基本用法
- 表单的一些基本用法与技巧
- sql时间函数的基本用法
- javascript 一些基本用法
- CheckBoxList控件基本用法
- std::string的基本用法
- gdb基本用法
- sfc的基本用法
- GDB基本用法
- rpm的基本用法
- DataGrid的用法(一)基本用法
- SQL语句基本的用法
- PL/SQL DEVELOPER 基本用法详解(转)
- 统计在线人数的基本用法
- 2006-07-28 Java的常用包,"=="和"equals"的用法,基本数据类型与引用类型,对象的克隆