文章目录
- Eigen
-
- 1.使用Map函数,c++数组与 eigen矩阵的转换
- 2.eigen矩阵与opencv互转
- 3.eigen使用总结
- 4.eigen visual studio 和 ubuntu上
-
- 1)
- 2)
- 3)初始化的方法
- 4)元素获取
- 5)resize
- 6)赋值操作
- 7)存储顺序
- 8)算术操作
- 9)算术表达式
- 10) 转置
- 11) 矩阵相乘
- 12)内积
- 13)外积
- 14)eigen中内积和外积分别是 dot, cross
- 15)reduction 操作
- 16)array
- 17) array 和matrix
- 18)matrix : block(), row(), col() 和其他
- 19)vector: head(), tail(), segment()
- 20)slicing和indexing 操作
- 21)根据索引提取列
- 22)一些初始化方法
- 23) 其他示例
- 24)最小二乘法解线性方程组
Eigen
1.使用Map函数,c++数组与 eigen矩阵的转换

2.eigen矩阵与opencv互转

3.eigen使用总结
https://zxl19.github.io/eigen-note/
https://eigen.tuxfamily.org/dox/group__QuickRefPage.html
4.eigen visual studio 和 ubuntu上
a.ubuntu上只需要CMakeLists.txt中添加
include_directories(“/home/xx/software/eigen-3.4.0”)
b.eigen 库在visual studio中的使用:
只需要添加包含目录即可

1)
在eigen中, matrices 和 vectors都是 Matrix template class的对象。
Vectors 是matrices的特殊case:行数为1或者列数为1
模板类一共有六个参数,一般使用前三个参数就足够了。比如

2)
Dynamic关键字 指示动态矩阵动态向量。 动态是运行时才知道size大小。
静态时编译时就知道size大小。一般比较小的矩阵可以使用静态。
静态和动态类似于:
int a[16];
int *a = new int[1000*1000];
3)初始化的方法

4)元素获取
用括号
m(1,2) //第2行第3列
5)resize
m.rows() m.cols() m.size()
resize 只能操作固定size的矩阵,如果resize后数量不变则无操作,数量变化会删除原来的数据
6)赋值操作
a = b //相当于把b copy到 a, 如果ab大小不同,会有resize的操作。
7)存储顺序
column major or row major
如果没有指定,默认是 ColMajor。
且两者可以互相转换,转换方法如下:

=======================================================================
8)算术操作
加减操作。必须类型(Scalar)相同,size相同。
矩阵与scalar的乘法,除法操作。
9)算术表达式
下面是可行的,而且可以更好的被优化
a = 3*b + 4*c - x/2;
10) 转置

11) 矩阵相乘
c = a * b -> [m,n] @ [n,m] = [m, m]
12)内积

13)外积

14)eigen中内积和外积分别是 dot, cross
dot适用于任意长度的向量
cross只适用于长度为3的vector
15)reduction 操作

===============================================================================
array class
16)array
matrix目的是为了线性集合操作,比如矩阵相乘。
Array 可以更好的执行 element-wise 操作。
比如对矩阵+上一个数,或者两个矩阵对应元素相乘。
定义

对应元素 加,减,乘的操作略
对应元素 abs, sqrt, min, max

17) array 和matrix
两个类的操作是不能互通的。因此如果你既想要矩阵相乘,又想要对应元素相乘,这个时候要首先进行转换。
.array() 和 .matrix() 可以是左值也可以是右值。而且基本上没有耗时

=======================================================================================
对于matrix
18)matrix : block(), row(), col() 和其他
block 输入 起始坐标(row,col)和对应高度宽度(height, width),输出矩形数据

m.row(i)
m.col(j)
其他取矩阵的操作

19)vector: head(), tail(), segment()

20)slicing和indexing 操作

21)根据索引提取列

22)一些初始化方法

23) 其他示例
// case1 reduction
// case2 范数
// case3, 按照行或者列进行reduction
// case4: 布尔 all, any, count 操作
// case5: partial recuctions
// case6: broadcasting
// case7: index:A({2, 1}, { 4,2,5,5,3 }) 先索引行,再选列
// case8: reshape 特点是默认按照col优先的储存顺序
#include <iostream>
#include <Eigen/Dense>
#include <algorithm>
using namespace std;
using namespace Eigen;
int main()
{
// case1 reduction
Eigen::Matrix2d mat;
mat << 1, 2,
3, 4;
cout << "Here is mat.sum(): " << mat.sum() << endl;
cout << "Here is mat.prod(): "


1万+

被折叠的 条评论
为什么被折叠?



