#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;
cout << "----------------" << 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; // 一些矩阵运算
// 四则运算就不演示了,直接用+-*/即可。
cout << "----------------" << endl;
matrix_33 = Eigen::Matrix3d::Random(); // 随机数矩阵
cout << "matrix_33" << endl;
cout << matrix_33 << endl << endl; cout << "matrix_33.transpose()" << endl << matrix_33.transpose() << endl << endl; // 转置
cout << "matrix_33.sum()" << endl << matrix_33.sum() << endl << endl; // 各元素和
cout << "matrix_33.trace()" << endl << matrix_33.trace() << endl<< endl; // 迹
cout << 10*matrix_33 << endl<< endl; // 数乘
cout << "matrix_33.inverse()" << endl << matrix_33.inverse() << endl << endl; // 逆
cout << "matrix_33.determinant()" << endl << matrix_33.determinant() << endl << endl; // 行列式 // 特征值
// 实对称矩阵可以保证对角化成功
Eigen::SelfAdjointEigenSolver<Eigen::Matrix3d> eigen_solver ( matrix_33.transpose()*matrix_33 );
cout << "Eigen values = \n" << eigen_solver.eigenvalues() << endl << endl;
cout << "Eigen vectors = \n" << eigen_solver.eigenvectors() << endl << 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;
}

eigenMatrix的更多相关文章

  1. PeopleRank从社交网络中发现个体价值

    阅读导读: 1.什么是PeopleRank? 2.PeopleRank和PageRank有什么差别? 3.PR分析微博数据时,怎样对微博单个账号评分? 4.R语言怎样递归计算矩阵特征值? 5.怎样计算 ...

  2. 主成分分析(PCA)与SVD奇异值分解

      主要参考:https://www.zhihu.com/question/38417101/answer/94338598 http://blog.jobbole.com/88208/ 先说下PCA ...

  3. Eigen库笔记整理(一)

    首先熟悉Eigen库的用途,自行百度. 引入头文件: // Eigen 部分 #include <Eigen/Core> // 稠密矩阵的代数运算(逆,特征值等) #include < ...

  4. 视觉SLAM十四讲(三)——三维空间刚体运动(下)

    理论部分请看 :三维空间刚体运动 一.Eigen的使用 首先安装 Eigen: sudo apt-get install libeigen3-dev 一般都安装在 /usr/include/eigen ...

  5. 使用Eigen遇到恶心报错

    参考博客:https://www.cnblogs.com/wongyi/p/8734346.html 1. 数据类型报错 /home/wy/workdir/slambook/ch3/useEigen/ ...

  6. PageRank算法R语言实现

    PageRank算法R语言实现 Google搜索,早已成为我每天必用的工具,无数次惊叹它搜索结果的准确性.同时,我也在做Google的SEO,推广自己的博客.经过几个月尝试,我的博客PR到2了,外链也 ...

  7. WSL配置高翔vslam环境

    WSL配置高翔vslam环境 步骤: 安装 windows wls 配置 g++ cmake 环境 编译运行一下例子 1. window启用 wsl 前往 "启用或关闭 Windows 功能 ...

  8. WSL (Windows Subsystem for Linux) 的 VSLAM (Visual Simultaneous Localization and Mapping) 道路

    WSL 的 VSLAM 道路 以 Windows Subsystem for Linux 闯入 Visual Simultaneous Localization and Mapping 世界的艰难道路 ...

随机推荐

  1. Case When ELSE END语句

    一.简介.Case  When   ELSE   END共有两种用法: 说实话,这种就是数据库版的switch语句,但是只是形式上很像,实际上还是有差别的!!! Create Table Test6( ...

  2. c# Time类

    直接上代码 public KBehaviour() { //间隔时间 System.Timers.Timer t = ); t.Elapsed += new System.Timers.Elapsed ...

  3. 上传base64格式的图片到服务器

    上传base64格式的图片到服务器 /**bash64上传图片 * @param $base64 图片的base64数据 * @param $path 保存路径 */ function base64_ ...

  4. springboot使用Freemarker继承

    最近需要用到Freemarker的继承.但是发现没有关于springboot配置Freemarker的继承的.所以趁现在有时间写个博客. 1. Freemarker继承介绍 Freemarker 通过 ...

  5. 实体类的状态与Hibernate缓存

    一.Hibernate中实体类的三种状态 1.瞬时态 该状态下实体类对象的id属性没有值,该对象和session也没有关系. 实例: UserEntity user = new UserEntity( ...

  6. a 标签的跳转属性

     a 标签中调用js的几种方法   我们常用的在a标签中有点击事件:1. a href="JavaScript:js_method();" 这是我们平台上常用的方法,但是这种方法在 ...

  7. jmeter(6)——集合点与检查点

    集合点 1.概念 集合点:我们所说的并发不会是真正的并发, 集合点可以理解成,所有的用户在进行某一操作时在同一时间点一起执行,比如:抢票或者促销抢购,集合点可以帮助我们使并发更加有效可控 2.位置 位 ...

  8. HZAU 21——Arithmetic Sequence——————【暴力 or dp】

    Arithmetic Sequence Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 1810  Solved: 311[Submit][Status] ...

  9. java.lang.RuntimeException Unable to instantiate application Caused by: java

    参考:http://blog.csdn.net/xufazhong/article/details/71155528 dependencies { classpath 'com.android.too ...

  10. php根据IP获取所在省份-淘宝api接口

    这里用的file_put_contents,你也可以用别的,直接怼代码: //拼接传递的参数$ip = '175.12.53.12' $opts = array( 'http'=>array( ...