c++ Eigen矩阵-基础知识

照抄书籍《视觉slam十四讲》
c++中的Eigen线性代数库:
求解:
1、转置矩阵
2、逆矩阵
3、行列式
4、迹
5、特征值、特征向量
6、求解方程

#include <iostream>
#include <ctime>
using namespace std;

// Eigen部分
#include <Eigen/Core>
// 稠密矩阵的代数运算(逆,特征值等)
#include <Eigen/Dense>

#define MATRIX_SIZE 50

// 本程序演示Eigen基本类型的使用
int main(int argc, char* argv[])
{
    // Eigen以矩阵为基本数据单元,它是一个模板类。
    // 它的前三个参数:数据类型,行,列。
    // 下一行声明一个2*3的float矩阵
    Eigen::Matrix<float, 2, 3> matrix_23;
    // Eigen通过typedef提供了许多内置类型,不过底层仍是Eigen::Matrix
    // 例如 Vector3d 实质上是Eigen::Matrix<double,3,1>
    Eigen::Vector3d v_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;
    
    // 下面是对矩阵的操作
    // 输入数据
    matrix_23 << 1,2,3,4,5,6;
    // 输出
    cout << "matrix_23: " << matrix_23 << endl;


    // 用( )访问矩阵中的元素
    for (int i=0; i<1; ++i)
    {
        for (int j=0; j<2; ++j)
        {
            cout << matrix_23(i,j) << endl;
        }
    }

    v_3d << 3,2,1;
    // 矩阵和向量相乘,实际上仍是矩阵和矩阵
    // 但是不能混合两种数值类型不同的矩阵,下一行错误示范
    // 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 << result << endl;
    // 同样不能搞错矩阵的维度
    // Eigen::Matrix<double, 2 ,3> result_wrong_dimension = matrix_23.cast<double>()* v_3d;
    // 四则运算就不演示了,直接用对应的运算符

    cout << "matrix_33_zero: " << endl << matrix_33 << endl;
    matrix_33 = Eigen::Matrix3d::Random();
    cout << "matrix_33_random: " << endl << matrix_33 << endl;
    cout << "转置矩阵matrix_33.transpose(): " << endl << matrix_33.transpose() << endl;
    cout << "逆矩阵matrix_33.inverse(): " << endl << matrix_33.inverse() << endl;
    cout << "各元素和matrix_33.sum(): " << endl <<  matrix_33.sum() << endl;
    cout << "迹matrix_33.trace(): " << endl <<  matrix_33.trace() << endl;
    cout << "行列式matrix_33.determinant(): " << endl <<  matrix_33.determinant() << endl;

    // 特征值
    // 实对称矩阵可以保证对角化成功
    Eigen::SelfAdjointEigenSolver<Eigen::Matrix3d> eigen_solver (matrix_33.transpose()*matrix_33);
    cout << "Eigen values = " << endl << eigen_solver.eigenvalues() << endl;
    cout << "Eigen vectors = " << endl << 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(); //计时
    cout << "time_stt: " << time_stt << endl;
    cout << "(double)CLOCKS_PER_SEC: " << (double)CLOCKS_PER_SEC << endl;
    //直接求逆矩阵
    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 composition is: " << 1000 * (clock() - time_stt)/(double)CLOCKS_PER_SEC<<"ms"<<endl;


    return 0;
}
  • 0
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值