Eigen库在VS2017下的配置与使用

参考:
Eigen 的简介和下载安装 https://www.cnblogs.com/goingupeveryday/p/5699053.html
C++矩阵处理工具——Eigen https://blog.csdn.net/abcjennifer/article/details/7781936
C++开源矩阵计算工具——Eigen 在VS2005中的下载、配置与使用 https://blog.csdn.net/houjixin/article/details/8477522

Eigen库——c++开源矩阵运算库
(本人对较多介绍Eigen库的文章的总结)

一、Eigen库的配置(VS2017)

  1. Eigen库下载: http://eigen.tuxfamily.org/index.php?title=Main_Page
    下载文件并解压:
    在这里插入图片描述
  2. 新建文件名为eigen3的文件夹,并将Eigen文件夹拷贝到其中。
    将eigen3文件夹移至常用路径下。本人的是:F:\Program Files\eigen3。
    在这里插入图片描述
  3. 打开VS(本人用的是2017版),新建项目:新建->项目->Visual C++ -> Windows桌面->Windows控制台应用程序。
    在解决方案资源管理器中,右键项目名称,选择属性。如下图。
    在这里插入图片描述
    如下图添加eigen3文件夹的路径,点击应用、确定即可。
    在这里插入图片描述
  4. 简单的代码验证
#include <iostream>
#include <Eigen/Dense>
using namespace Eigen;     // 改成这样亦可 using Eigen::MatrixXd; 
using namespace std;
int main()
{
	MatrixXd m = MatrixXd::Random(3,3);              //随机生成3*3的double型矩阵
	m = (m + MatrixXd::Constant(3,3,1.2)) * 50;      //MatrixXd::Constant(3,3,1.2)表示生成3*3的double型矩阵,该矩阵所有元素均为1.2
	cout << "m =" << endl << m << endl;
	VectorXd v(3);        // 定义v为3*1的double型向量
	v << 1, 2, 3;         // 向量赋值
	cout << "m * v =" << endl << m * v << endl;
	system("pause");
	return 0;
}

二、Eigen库的使用

在文件头中加入
#include <Eigen/Dense>

  1. 矩阵&向量的定义与赋值
    MatrixXd:定义任意维double矩阵。其中X表示维度,d表示double型。同理d可改为f或者i,分别表示float型、int型。
    Matrix2d:定义2维double矩阵(2×2)
    Matrix3d:定义3维double矩阵(3×3)

    VectorXd 定义任意维double向量
    Vector2d 定义2维double向量(2×1)
    Vector3d 定义3维double向量(3×1)

    具体使用:

//1、定义
MatrixXd temp1(3, 4); //定义3*4的矩阵
MatrixXd temp2(3, 3); //定义3*3的矩阵
VectorXd temp3(3, 1); //定义3维行向量
Vector3d temp4;       //定义3维行向量
//2、赋值
temp1 <<1,2,3,4,
	5,6,7,8,
	9,0,0,0;
temp4 <<1,
	1,
	1;

注意:矩阵的拼接与赋值方式相同!

矩阵的索引:
在这里插入图片描述

cout<< temp1 (0,0) <<endl;
cout<< temp1 (0,1) <<endl;
cout<< temp1 (1,0) <<endl;
cout<< temp1 (1,1) <<endl;
//输出结果为:
1
2
5
6

特殊矩阵/向量的赋值:

Matrix3d temp1 = MatrixXd::Identity(3, 3);   //定义3*3的单位阵
Matrix3d temp2 = MatrixXd::Zero(3, 3);       //定义3*3的零矩阵
Matrix3d temp3 = MatrixXd::Ones(3, 3);       //定义3*3的全1矩阵
Vector3d temp4 = VectorXd::Zero(3, 1);       //定义3*1的零向量
Vector3d temp5 = VectorXd::Ones(3, 1);       //定义3*1的全1向量
  1. 矩阵基本运算
    常用的 “+”,“-”,“*”, “/” 与一般的C语言操作相同。
    2.1 行列数
MatrixXd R(3, 3);
double row = R.rows();      //row为R矩阵的行数
double col = R.cols();      //col为R矩阵的列数

2.2 转置、逆、行列式

R.transpose();               //转置
R.inverse();                 //逆
double d = R.determinant();  //行列式

2.3、叉乘

// 此处的代码未给出矩阵/向量的赋值!
Vector3d temp1(3, 1), temp2(3, 1);
temp1.cross(temp2);         //temp1×temp2

2.4、svd分解

Matrix3d  H;
JacobiSVD<Eigen::MatrixXd> svd(H, ComputeThinU | ComputeThinV);
Matrix3d V = svd.matrixV(), U = svd.matrixU();
Matrix3d D = U.inverse() * H * V.transpose().inverse(); // S = U^-1 * A * VT^ -1

2.5、秩

Matrix3d D;
FullPivLU<Matrix3d> lu(D);
int r = lu.rank();  
cout << "By default, the rank of A is found to be " << lu.rank() << endl;
lu.setThreshold(1e-5);
cout << "With threshold 1e-5, the rank of A is found to be " << lu.rank() << endl;

2.6、矩阵子块的提取

MatrixXd temp(3, 2);
temp << 0, 1,
	2, 3,
	4, 5;
cout << temp.block(0,0,3,1)<<endl; //从矩阵temp(0,0)为起始点,取三行一列
cout << temp.block(0,1,3,1)<<endl; //从矩阵temp(0,1)为起始点,取三行一列

//输出结果为:
0
2
4
1
3
5

矩阵按行遍历:

MatrixXd R(3, 3);
R << 1,2,3,
     4,5,6,
     7,8,9;
double row = R.rows();      //row为R矩阵的行数
double col = R.cols();      //col为R矩阵的列数
for(int i=0;i<row;i++)
{
	cout<<R.block(i,0,1,col)<<endl;
}

矩阵按列遍历:

for(int i=0;i<col;i++)
{
	cout<<R.block(0,i,row,1)<<endl;
}

更多操作参见:http://igl.ethz.ch/projects/libigl/matlab-to-eigen.html

  • 35
    点赞
  • 163
    收藏
    觉得还不错? 一键收藏
  • 16
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值