Eigen教程1 - 基础
Eigen教程1 - 基础
参考:https://blog.csdn.net/xuezhisdc/article/details/54619853
固定大小的矩阵和向量
/** 参考链接:http://eigen.tuxfamily.org/dox-2.0/TutorialCore.html#TutorialCoreGettingStarted
*/#include <iostream>
#include <Eigen/Core>using namespace Eigen;
using namespace std; // import most common Eigen types
int main(int, char *[])
{Matrix3f m3; //3x3单精度矩阵m3 << 1, 2, 3, 4, 5, 6, 7, 8, 9;Matrix4f m4 = Matrix4f::Identity(); //4x4单位矩阵(单精度)Vector4i v4(1, 2, 3, 4); // 长度为4的整型向量// 输出结果std::cout << "m3\n" << m3 << "\nm4:\n"<< m4 << "\nv4:\n" << v4 << std::endl;
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- Matrix表示矩阵,Vector表示向量,数字表示维度,最后的f和i分别表示单精度和整型数据类型。
- 固定大小表示编译时,行数和列数是固定的。这时,Eigen不会分配动态内存。这对于比较小的尺寸比较适合,比如16x16。
动态大小的矩阵和向量
/** 参考链接:http://eigen.tuxfamily.org/dox-2.0/TutorialCore.html#TutorialCoreGettingStarted
*/#include <iostream>
#include <Eigen/Core>using namespace Eigen;
using namespace std; int main(int, char *[])
{// 动态矩阵for (int size=1; size<=4; ++size){MatrixXi m(size,size+1); // 一个整型的大小为 (size)x(size+1) 的矩阵for (int j=0; j<m.cols(); ++j) // 遍历列for (int i=0; i<m.rows(); ++i) // 遍历行m(i,j) = i+j*m.rows(); // 使用圆括号m(i,j)访问矩阵的元素std::cout << m << "\n\n"; //打印矩阵}// 动态向量VectorXf v(4); // 定义一个4维单精度向量// 使用圆括号()或方括号[]访问向量元素v[0] = 1; v[1] = 2; v(2) = 3; v(3) = 4;std::cout << "\nv:\n" << v << std::endl;
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 小结:X表示动态大小。
#include <Eigen/Eigen>
将包含所有的Eigen函数。#include <Eigen/Dense>
包含所有普通矩阵函数,不包括稀疏矩阵函数。它们会增加编译时间。
矩阵和向量类型
- Eigen中的所有密集矩阵和向量都是通过Matrix类来表示的。Matrix通过一系列的模板参数来生成具体的类别。
Matrix<Scalar, RowsAtCompileTime, ColsAtCompileTime>
中,Scalar表示数据类型,RowsAtCompileTime和ColsAtCompileTime分别表示编译时的行数和列数。Vector3d
定义为Matrix<double, 3, 1>
- 对于动态大小的类型,在编译时不指定行数和列数,使用Eigen::Dynamic。比如,
VectorXd
定义为Matrix<double, Dynamic, 1>
。
访问元素
- Eigen支持以下的读/写元素语法:
matrix(i,j);
vector(i)
vector[i]
vector.x() // first coefficient
vector.y() // second coefficient
vector.z() // third coefficient
vector.w() // fourth coefficient
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 矩阵只能通过圆括号()访问;
- 向量可以通过圆括号()和方括号[]访问。
- 上述的元素访问方法都通过断言检查范围,代价比较大。
- 通过定义EIGEN_NO_DEBUG 或 NDEBUG,取消断言。
- 通过使用
coeff()
和coeffRef()
,来取消检查。比如,MatrixBase::coeff(int,int) const, MatrixBase::coeffRef(int,int)等。
创建和初始化矩阵和向量
通过预定义矩阵初始化
创建固定大小的矩阵和向量
/** 参考链接:http://eigen.tuxfamily.org/dox-2.0/TutorialCore.html#TutorialCoreGettingStarted
*/#include <iostream>
#include <Eigen/Core>using namespace Eigen;
using namespace std; int main(int, char *[])
{float value = 3.0;Matrix3f x; // 创建一个3x3的单精度矩阵x = Matrix3f::Zero(); //全零矩阵cout << x << endl << endl;x = Matrix3f::Ones(); //全一矩阵cout << x << endl << endl;x = Matrix3f::Constant(value); //全value矩阵cout << x << endl << endl;x = Matrix3f::Identity(); //单位矩阵cout << x << endl << endl;x = Matrix3f::Random(); // 随机矩阵cout << x << endl << endl;x.setZero();cout << x << endl << endl;x.setOnes();cout << x << endl << endl;x.setIdentity();cout << x << endl << endl;x.setConstant(value);cout << x << endl << endl;x.setRandom();cout << x << endl << endl;
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
创建动态大小的矩阵
/** 参考链接:http://eigen.tuxfamily.org/dox-2.0/TutorialCore.html#TutorialCoreGettingStarted
*/#include <iostream>
#include <Eigen/Core>using namespace Eigen;
using namespace std; int main(int, char *[])
{float value = 3.0f;int rows = 3;int cols = 4;MatrixXf x;x = MatrixXf::Zero(rows, cols);cout << x << endl << endl;x = MatrixXf::Ones(rows, cols);cout << x << endl << endl;x = MatrixXf::Constant(rows, cols, value);cout << x << endl << endl;x = MatrixXf::Identity(rows, cols);cout << x << endl << endl;x = MatrixXf::Random(rows, cols);cout << x << endl << endl;x.setZero(rows, cols);cout << x << endl << endl;x.setOnes(rows, cols);cout << x << endl << endl;x.setConstant(rows, cols, value);cout << x << endl << endl;x.setIdentity(rows, cols);cout << x << endl << endl;x.setRandom(rows, cols);cout << x << endl << endl;return 0;
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
创建动态大小的向量
/** 参考链接:http://eigen.tuxfamily.org/dox-2.0/TutorialCore.html#TutorialCoreGettingStarted
*/#include <iostream>
#include <Eigen/Core>using namespace Eigen;
using namespace std; int main(int, char *[])
{int size = 3;float value = 3.0f;VectorXf x; // 定义动态向量x = VectorXf::Zero(size); //全0向量cout << x << endl << endl;x = VectorXf::Ones(size); //全1向量cout << x << endl << endl;x = VectorXf::Constant(size, value);//全value向量cout << x << endl << endl;//x = VectorXf::Identity(size);//报错x = VectorXf::Random(size);cout << x << endl << endl;x.setZero(size);cout << x << endl << endl;x.setOnes(size);cout << x << endl << endl;x.setConstant(size, value);cout << x << endl << endl;//x.setIdentity(size);x.setRandom(size);cout << x << endl << endl;return 0;
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
创建固定大小的基向量
/** 参考链接:http://eigen.tuxfamily.org/dox-2.0/TutorialCore.html#TutorialCoreGettingStarted
*/#include <iostream>
#include <Eigen/Core>using namespace Eigen;
using namespace std; int main(int, char *[])
{Vector3f x;x = Vector3f::UnitX(); // 1 0 0cout << x << endl << endl;x = Vector3f::UnitY(); // 0 1 0cout << x << endl << endl;x = Vector3f::UnitZ(); // 0 0 1cout << x << endl << endl;return 0;
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
创建动态大小的基向量
/** 参考链接:http://eigen.tuxfamily.org/dox-2.0/TutorialCore.html#TutorialCoreGettingStarted
*/#include <iostream>
#include <Eigen/Core>using namespace Eigen;
using namespace std; int main(int, char *[])
{VectorXf x;x = VectorXf::Unit(4,1);cout << x << endl << endl;x = Vector4f(0,1,0,0);cout << x << endl << endl;x = Vector4f::UnitY();cout << x << endl << endl;return 0;
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
例子
/** 参考链接:http://eigen.tuxfamily.org/dox-2.0/TutorialCore.html#TutorialCoreGettingStarted
*/#include <iostream>
#include <Eigen/Core>using namespace Eigen;
using namespace std; int main(int, char *[])
{cout << MatrixXf::Constant(2, 3, sqrt(2)) << endl; // 2x3的单精度矩阵RowVector3i v; //3维行向量v.setConstant(6);cout << "v = " << v << endl;return 0;
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
通过Cast的方式初始化
相同尺寸的矩阵兼容
- 元素类型通过
MatrixBase::cast()
自动转换。
/** 参考链接:http://eigen.tuxfamily.org/dox-2.0/TutorialCore.html#TutorialCoreGettingStarted
*/#include <iostream>
#include <Eigen/Core>
#include <Eigen/Eigen>using namespace Eigen;
using namespace std; int main(int, char *[])
{Vector3d md(1,2,3);Vector3f mf = md.cast<float>();cout << "md = " << md << endl;cout << "mf = " << mf << endl;return 0;
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
相同类型的矩阵兼容
/** 参考链接:http://eigen.tuxfamily.org/dox-2.0/TutorialCore.html#TutorialCoreGettingStarted
*/#include <iostream>
#include <Eigen/Core>using namespace Eigen;
using namespace std; int main(int, char *[])
{MatrixXf res(10,10);Matrix3f a, b;a = Matrix3f::Identity();b = Matrix3f::Constant(3);res = a+b; // OK: res is resized to size 3x3cout << a << endl << endl;cout << b << endl << endl;cout << res << endl << endl;return 0;
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
通过Map方式初始化
/** 参考链接:http://eigen.tuxfamily.org/dox-2.0/TutorialCore.html#TutorialCoreGettingStarted
*/#include <iostream>
#include <vector>
#include <Eigen/Core>using namespace Eigen;
using namespace std; int main(int, char *[])
{std::vector<float> stlarray(10);VectorXf::Map(&stlarray[0], stlarray.size()).squaredNorm();return 0;
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 下面的代码没有完全调通
/** 参考链接:http://eigen.tuxfamily.org/dox-2.0/TutorialCore.html#TutorialCoreGettingStarted
*/#include <iostream>
#include <vector>
#include <Eigen/Core>using namespace Eigen;
using namespace std; int main(int, char *[])
{const int rows = 3;const int cols = 4;float array[rows*cols];Map<MatrixXf> m(array,rows,cols);Matrix3f othermatrix1 = Matrix3f::Identity();//单位矩阵MatrixXf othermatrix2(3,4);othermatrix2 = MatrixXf::Constant(3,4,5);//3x4的常量矩阵,值都为5m = othermatrix1 * othermatrix2;//m.eigenvalues();std::vector<float> stlarray(10);VectorXf::Map(&stlarray[0], stlarray.size()).squaredNorm();cout << m << endl;return 0;
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
通过逗号初始化
/** 参考链接:http://eigen.tuxfamily.org/dox-2.0/TutorialCore.html#TutorialCoreGettingStarted
*/#include <iostream>
#include <vector>
#include <Eigen/Core>using namespace Eigen;
using namespace std; int main(int, char *[])
{Matrix3f m;m << 1, 2, 3,4, 5, 6,7, 8, 9;cout << m << endl;return 0;
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 使用逗号和子矩阵,初始化矩阵。
/** 参考链接:http://eigen.tuxfamily.org/dox-2.0/TutorialCore.html#TutorialCoreGettingStarted
*/#include <iostream>
#include <vector>
#include <Eigen/Core>using namespace Eigen;
using namespace std; int main(int, char *[])
{int rows=5, cols=5;MatrixXf m(rows,cols);m << (Matrix3f() << 1, 2, 3, 4, 5, 6, 7, 8, 9).finished(),//左上角3x3MatrixXf::Zero(3,cols-3), //右上角3x2MatrixXf::Zero(rows-3,3), //左下角2x3MatrixXf::Identity(rows-3,cols-3); //右下角2x2cout << m << endl;return 0;
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
.finished()
用于当临时矩阵初始化完成时,获取实际的矩阵对象。尽管看起来很复杂,但实际上编译时已经优化。
算术操作
传统的数学运算
- 矩阵/向量乘法:
col2 = mat1 * col1; //矩阵x列向量
row2 = row1 * mat1; // 行向量x矩阵
row1 *= mat1;
mat3 = mat1 * mat2;
mat3 *= mat1;
- 1
- 2
- 3
- 4
- 5
- 矩阵/向量加法/减法:
mat3 = mat1 + mat2;
mat3 += mat1;
mat3 = mat1 - mat2;
mat3 -= mat1;
- 1
- 2
- 3
- 4
- 标量加法/减法:
mat3 = mat1 * s1;
mat3 = s1 * mat1;
mat3 *= s1;
mat3 = mat1 / s1;
mat3 /= s1;
- 1
- 2
- 3
- 4
- 5
逐元素的操作
逐元素的操作,请查阅
.cwise()
。逐元素乘法
mat3 = mat1.cwise() * mat2;
- 1
- 加/减标量
//需要Array模块 #include <Eigen/Array>
mat3 = mat1.cwise() + scalar;
mat3.cwise() += scalar;
mat3.cwise() -= scalar;
- 1
- 2
- 3
- 4
- 逐元素除法
//需要Array模块 #include <Eigen/Array>
mat3 = mat1.cwise() / mat2;
- 1
- 2
- 逐元素取倒数
//需要Array模块 #include <Eigen/Array>
mat3 = mat1.cwise().inverse();
- 1
- 2
- 逐元素比较运算
//需要Array模块 #include <Eigen/Array>
mat3 = mat1.cwise() < mat2;
mat3 = mat1.cwise() <= mat2;
mat3 = mat1.cwise() > mat2;
//等
- 1
- 2
- 3
- 4
- 5
- 三角余弦
- sin(), cos()等。
//需要Array模块 #include <Eigen/Array>
mat3 = mat1.cwise().sin();
// 等
- 1
- 2
- 3
- 指数
- pow(), square(), cube(), sqrt(), exp(), log()等。
//需要Array模块 #include <Eigen/Array>
mat3 = mat1.cwise().square();
mat3 = mat1.cwise().pow(5);
mat3 = mat1.cwise().log();
//等
- 1
- 2
- 3
- 4
- 5
- 最小值,最大值,绝对值
mat3 = mat1.cwise().min(mat2);
mat3 = mat1.cwise().max(mat2);
mat3 = mat1.cwise().abs();
mat3 = mat1.cwise().abs2();
- 1
- 2
- 3
- 4
各种乘法运算
- 矩阵乘法:
m1*m2
- 逐元素乘法:
mat1.cwise()*mat2
- 点积:
scalar = vec1.dot(vec2);
- 外积:
mat = vec1 * vec2.transpose();
- 交叉积:
#include <Eigen/Geometry> vec3 = vec1.cross(vec2);
- 矩阵乘法:
逐元素操作示例:
/** 参考链接:http://eigen.tuxfamily.org/dox-2.0/TutorialCore.html#TutorialCoreGettingStarted
*/#include <iostream>
#include <vector>
#include <Eigen/Eigen>using namespace Eigen;
using namespace std; int main(int, char *[])
{Matrix3f x, y;x << 5,3,1,2,-7,8,9,-4,6;y << 5,3,1,2,-7,8,9,4,7;cout << x << endl << endl;cout << x.cwiseAbs() << endl << endl;//绝对值cout << x.cwiseAbs2() << endl << endl; //平方cout << x.cwiseEqual(y) << endl << endl; //是否相等cout << x.cwiseMax(y) << endl << endl; //逐元素最大值cout << x.cwiseMin(y) << endl << endl;cout << x.cwiseInverse() << endl << endl; //倒数cout << x.cwiseNotEqual(y) << endl << endl; //不相等cout << x.cwiseProduct(y) << endl << endl; //逐元素乘法cout << x.cwiseQuotient(y) << endl << endl; //除法cout << x.cwiseSqrt() << endl << endl; //return 0;
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
Reductions
- Eigen提供了一些reduction方法: minCoeff() , maxCoeff() , sum() , trace() , norm() , squaredNorm() , all() , 和 any()。
- 上述这些方法都可以逐列或逐行的执行。如下所示:
/** 参考链接:http://eigen.tuxfamily.org/dox-2.0/TutorialCore.html#TutorialCoreGettingStarted
*/#include <iostream>
#include <vector>
#include <Eigen/Core>using namespace Eigen;
using namespace std; int main(int, char *[])
{Matrix3f x;x << 5,3,1,2,7,8,9,4,6;cout << x.minCoeff() << endl;cout << x.colwise().minCoeff() << endl;cout << x.rowwise().minCoeff() << endl;return 0;
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- maxCoeff()和minCoeff()函数可以通过设置可选参数,返回最大/小值的位置:maxCoeff(int* i, int* j) , minCoeff(int* i, int* j) 。
- all() 和 any()在使用逐元素操作时,非常有用。
参考
- http://eigen.tuxfamily.org/dox-2.0/TutorialCore.html#TutorialCoreGettingStarted
- 备注:该链接的内容过时了。。。
Eigen教程1 - 基础相关推荐
- python向量计算库教程_NumPy库入门教程:基础知识总结
原标题:NumPy库入门教程:基础知识总结 视学算法 | 作者 知乎专栏 | 来源 numpy可以说是 Python运用于人工智能和科学计算的一个重要基础,近段时间恰好学习了numpy,pandas, ...
- Nmap扫描教程之基础扫描详解
Nmap扫描教程之基础扫描详解 Nmap扫描基础扫描 当用户对Nmap工具了解后,即可使用该工具实施扫描.通过上一章的介绍,用户可知Nmap工具可以分别对主机.端口.版本.操作系统等实施扫描.但是,在 ...
- [译]Vulkan教程(04)基础代码
[译]Vulkan教程(04)基础代码 General structure 通用结构 In the previous chapter you've created a Vulkan project w ...
- 软件测试python基础_软件测试学习教程——python基础
原标题:软件测试学习教程--python基础 一,print()函数 print 作用:把" "内部的内容,输出到屏幕. print("hello world" ...
- c语言语言教程0基础_C语言基础
c语言语言教程0基础 Hey, Folks here I am back with my second article on C language. Hope you are through with ...
- html5教学文档笔记,4.HTML 教程- (HTML5 基础)
HTML 教程- (HTML5 基础) 1.HTML 标题 HTML 标题(Heading)是通过 - 标签来定义的. 2.HTML 段落 HTML 段落是通过标签 来定义的. 3.HTML 链接 H ...
- 计算机键盘输入法基础知识,教程计算机基础知识-:认识输入法
教程计算机基础知识-:认识输入法 认识输入法 在任务栏的右边有一个小键盘图标,这就是输入法,默认输入的是英文字母,点击可以选择汉字输入法: 1.切换输入法 1)瞄准任务栏上的小键盘点左键,在出来的菜单 ...
- java基础教程知识点,[Java教程]js基础知识点总结
[Java教程]js基础知识点总结 0 2016-11-01 21:00:04 如何在一个网站或者一个页面,去书写你的js代码: 1.js的分层(功能):jquery(tool) 组件(ui) 应用( ...
- style 字体加粗_第9篇 Qt Quick入门教程之基础(九)文本显示和字体
导语 文本显示是界面开发必不可少的内容,在Qt Quick模块中提供了 Text 项目来进行文本的显示,其中可以使用 font 属性组对文本字体进行设置.这一篇我们来看看它们的具体使用. 使用字体 就 ...
最新文章
- 升级gcc后glibc报错
- golang go-sql-driver 数据库报错 bad connection
- 【深入Java虚拟机JVM 10】回收方法区
- servlet多重映射_关于多重映射问题,很奇怪
- android studio创建第一个安卓程序加载html5页面(一)
- 2021年中国一氧化碳传感器市场趋势报告、技术动态创新及2027年市场预测
- 苹果 5G 芯片“难产”!
- 戴尔企业级技术社区达人积分等级制度
- 为什么你的数据库经常会被破防呢?原因原来是这——Sql注入问题(源码+文字深度解析)
- 装系统缺少硬盘驱动_缺少操作系统-向我学习,请在今年备份您的硬盘!
- Android 控件获取焦点
- GBIT51231-2016装配式混凝土结构建筑技术标准
- 超简单方法搭建Eclipse下的Android NDK
- 能同时模拟键盘及鼠标的神器--51单片机可控制
- 历时三年“鸽王”Filecoin主网上线,分布式存储市场将迎来最强劲敌?
- maximo数据库配置
- 一些学习网络安全的平台介绍
- PPTP(Point to Point Tunneling Protocol),即点对点隧道协议。
- 过去一周区块链投融资事件回顾
- 数据库系统原理--第2章作业2--习题答案
热门文章
- 特性开关框架 java_关于Mosfet你应当知道的开关特性
- Ubuntu中安装docker-compose
- java 国际化例子_JavaSE 国际化 简单例子
- cs6 数据库mysql_能mysql内容
- 计算机利用公式计算实发工资怎么弄,2019新个税Excel计算器公式 助你轻松算出工资...
- java hasfocus_Android·Focus机制解析和常见问题
- Keycloak宣布不再适配Spring Boot和Spring Security
- 尽快卸载这两款恶意浏览器插件!已有近50万用户安装
- 星巴克是如何处理订单的?
- LinkedList 真的是查找慢增删快?