c++ 的 eigen 类似于 python 的 numpy, 还有一个类似的库是 Armadillo, 当然还有 opencv.

Armadillo 与 matlab 在函数名称上更接近, 但是 TensorFlow 和 Ceres 使用了 eigen.

这里不讲究谁优谁劣, 入门阶段迅速掌握一个, 用起来就够了.

1. The Matrix Class

1) The first three template parameters of Matrix

Matrix<typename Scalar, int RowsAtCompileTime, int ColsAtCompileTime>

typedef Matrix<float, , > Matrix4f;

2) Vectors

In Eigen, vectors are just a special case of matrices.

typedef Matrix<float, , > Vector3f;

typedef Matrix<int, , > RowVector2i;

3) The special value dynamic

Matrices dimensions can be unknown at compile time in Eigen.

typedef Matrix<double, Dynamic, Dynamic> MatrixXd;

typedef Matrix<int, Dynamic, > VectorXi;

Matrix<float, , Dynamic>;

4) Constructors

Matrix3f a;

MatrixXf b;

MatrixXf a(, );

MatrixXf b();

Matrix3f a(, );

Vector2d a(5.0, 6.0);

Vector3d b(5.0, 6.0, 7.0);

Matrix3f 声明的矩阵, 大小是固定的, 不能修改. 4维之下的矩阵可以使用形如 Matrix4f 的形式, 稍微增加编译时间, 但是执行更快.

5) Coefficient accessors

int main()
{
MatrixXd m(,);
m(,) = ;
m(,) = 2.5;
m(,) = -;
m(,) = m(,) + m(,);
std::cout << "Here is the matrix m:\n" << m << std::endl;
VectorXd v();
v() = ;
v() = v() - ;
std::cout << "Here is the vector v:\n" << v << std::endl;
}

m(index) 除了可以访问 vector, 还可以访问 matrix, matrix 在 Eigen 中默认按列存储. 在上例中, m(2) == -1.

[] 也被重载了, 类似 () 的功能, 但是只能有一个 index, 因为 c++ 的特性, matrix[i, j] == matrix[j].

推测, 访问单独或批量的列或行, 应该有专有的函数, 用 () 和 [] 只能访问单个值.

6) Comma-initialization

Matrix3f m;
m << , , ,
, , ,
, , ;
cout << m;

7) Resizing

int main()
{
MatrixXd m(,);
m.resize(,);
std::cout << "The matrix m is of size "
<< m.rows() << "x" << m.cols() << std::endl;
std::cout << "It has " << m.size() << " coefficients" << std::endl;
VectorXd v();
v.resize();
std::cout << "The vector v is of size " << v.size() << std::endl;
std::cout << "As a matrix, v is of size "
<< v.rows() << "x" << v.cols() << std::endl;
}

resize() 会覆盖之前矩阵的值, 如果要保留原有未知的值, 可以使用 conservativeResize().

再次强调, 形如 Matrix4d 声明的矩阵大小不可更改.

8) Assignment and resizing

MatrixXf a(,);
std::cout << "a is of size " << a.rows() << "x" << a.cols() << std::endl;
MatrixXf b(,);
a = b;
std::cout << "a is now of size " << a.rows() << "x" << a.cols() << std::endl;

如果等号左侧是 fixed size, 则 resizing 不被允许.

9) Convenience typedefs

MatrixNt for Matrix<type, N, N>

VectorNt for Matrix<type, N, 1>

RowVectorNt for Matrix<type, 1, N>

N can be 2, 3,... or X (Dynamic)

t can be i (int), f (float), d (double), cf (complex<float>), cd (complex<double>)

2. Matrix and vector arithmetic

1) Introduction

Eigen 重载了 matrix 之间的运算符号, 但是 matrix 和 scalar 之间的运算符号未重载.

2) Addition and subtraction

Eigen 不会对 scalar 进行自动类型转换.

a + b, a - b,  -a, a += b, a -= b

int main()
{
Matrix2d a;
a << , ,
, ;
MatrixXd b(,);
b << , ,
, ;
std::cout << "a + b =\n" << a + b << std::endl;
std::cout << "a - b =\n" << a - b << std::endl;
std::cout << "Doing a += b;" << std::endl;
a += b;
std::cout << "Now a =\n" << a << std::endl;
Vector3d v(,,);
Vector3d w(,,);
std::cout << "-v + w - v =\n" << -v + w - v << std::endl;
}

3) Scalar multiplication and division

matrix * scalar, scalar * matrix, matrix / scalar, matrix *= scalar, matrix /= scalar

int main()
{
Matrix2d a;
a << , ,
, ;
Vector3d v(,,);
std::cout << "a * 2.5 =\n" << a * 2.5 << std::endl;
std::cout << "0.1 * v =\n" << 0.1 * v << std::endl;
std::cout << "Doing v *= 2;" << std::endl;
v *= ;
std::cout << "Now v =\n" << v << std::endl;
}

4) A note about expression templates

简单说就是 Eigen 对 arithmetic expression 有进行优化, 但这是它应该做的.

5) Transposition and conjugation

transpose() 转置

MatrixXcf a = MatrixXd::Random(,);
cout << "Here is the matrix a\n" << a << endl;
cout << "Here is the matrix a^T\n" << a.transpose() << endl;

不能使用 a = a.transpose(), a 的值会发生错乱, 要使用 a = a.transposeInPlace().

Matrix2i a; a << , , , ;
cout << "Here is the matrix a:\n" << a << endl;
a = a.transpose(); // !!! do NOT do this !!!
cout << "and the result of the aliasing effect:\n" << a << endl;
a.transposeInPlace();
cout << "and after being transposed:\n" << a << endl;

6) Matrix-matrix and matrix-vector multiplication

a * b, a *= b

对于 a *= b, 如果 size 会发生变化, 则 a 不能是 fixed size

int main()
{
Matrix2d mat;
mat << , ,
, ;
Vector2d u(-,), v(,);
std::cout << "Here is mat*mat:\n" << mat*mat << std::endl;
std::cout << "Here is mat*u:\n" << mat*u << std::endl;
std::cout << "Here is u^T*mat:\n" << u.transpose()*mat << std::endl;
std::cout << "Here is u^T*v:\n" << u.transpose()*v << std::endl;
std::cout << "Here is u*v^T:\n" << u*v.transpose() << std::endl;
std::cout << "Let's multiply mat by itself" << std::endl;
mat = mat*mat;
std::cout << "Now mat is mat:\n" << mat << std::endl;
}

7) Dot product and cross product

dot() 点积

cross() 叉积

int main()
{
Vector3d v(,,);
Vector3d w(,,);
cout << "Dot product: " << v.dot(w) << endl;
double dp = v.adjoint()*w; // automatic conversion of the inner product to a scalar
cout << "Dot product via a matrix product: " << dp << endl;
cout << "Cross product:\n" << v.cross(w) << endl;
}

8) Basic arithmetic reduction operations

sum(), prod(), mean(), minCoeff(), maxCoeff(), trace()

int main()
{
Eigen::Matrix2d mat;
mat << , ,
, ;
cout << "Here is mat.sum(): " << mat.sum() << endl;
cout << "Here is mat.prod(): " << mat.prod() << endl;
cout << "Here is mat.mean(): " << mat.mean() << endl;
cout << "Here is mat.minCoeff(): " << mat.minCoeff() << endl;
cout << "Here is mat.maxCoeff(): " << mat.maxCoeff() << endl;
cout << "Here is mat.trace(): " << mat.trace() << endl;
}

a.trace() == a.diagonal().sum()

minCoeff() 和 maxCoeff() 除了返回最小最大值, 还可以获取该值的索引

  Matrix3f m = Matrix3f::Random();
std::ptrdiff_t i, j;
float minOfM = m.minCoeff(&i,&j);
cout << "Here is the matrix m:\n" << m << endl;
cout << "Its minimum coefficient (" << minOfM
<< ") is at position (" << i << "," << j << ")\n\n";
RowVector4i v = RowVector4i::Random();
int maxOfV = v.maxCoeff(&i);
cout << "Here is the vector v: " << v << endl;
cout << "Its maximum coefficient (" << maxOfV
<< ") is at position " << i << endl;

9) Validity of operations

编译时和运行时都会检查运算的合法性

Matrix3f m;
Vector4f v;
v = m*v; // Compile-time error: YOU_MIXED_MATRICES_OF_DIFFERENT_SIZES MatrixXf m(,);
VectorXf v();
v = m * v; // Run-time assertion failure here: "invalid matrix product"

3. The array class and coefficient-wise operations

1) Array types

Array<typename Scalar, int RowsAtCompileTime, int ColsAtCompileTime>

可以有多维数组

Array<float, Dynamic, 1> ArrayXf

Array<float, 3, 1> Array3f

Array<double, Dynamic, Dynamic> ArrayXXd

Array<double, 3, 3> Array33d

2) Accessing values inside an Array

int main()
{
ArrayXXf m(,); // assign some values coefficient by coefficient
m(,) = 1.0; m(,) = 2.0;
m(,) = 3.0; m(,) = m(,) + m(,); // print values to standard output
cout << m << endl << endl; // using the comma-initializer is also allowed
m << 1.0,2.0,
3.0,4.0; // print values to standard output
cout << m << endl;
}

3) Addition and subtraction

array + array, array - scalar

int main()
{
ArrayXXf a(,);
ArrayXXf b(,);
a << ,,,
,,,
,,;
b << ,,,
,,,
,,; // Adding two arrays
cout << "a + b = " << endl << a + b << endl << endl;
// Subtracting a scalar from an array
cout << "a - 2 = " << endl << a - << endl;
}

4) Array multiplicaiton

区分于矩阵乘法, 对应位置参数相乘.

int main()
{
ArrayXXf a(,);
ArrayXXf b(,);
a << ,,
,;
b << ,,
,;
cout << "a * b = " << endl << a * b << endl;
}

5) Other coefficient-wise operations

abs(), sqrt(), min() 两个 array 对应位置取最小

int main()
{
ArrayXf a = ArrayXf::Random();
a *= ;
cout << "a =" << endl
<< a << endl;
cout << "a.abs() =" << endl
<< a.abs() << endl;
cout << "a.abs().sqrt() =" << endl
<< a.abs().sqrt() << endl;
cout << "a.min(a.abs().sqrt()) =" << endl
<< a.min(a.abs().sqrt()) << endl;
}

6) Converting between array and matrix expressions

matrix 有一个 array() 方法, array 有一个 matrix 方法, .array() 和 .matrix() 的返回值可以用作左值或右值.

matrix 有一个 cwiseProduct() 方法, 用于两个 matrix 对应参数相乘.

Eigen 允许将 array expression 赋值给 matrix 变量.

int main()
{
MatrixXf m(,);
MatrixXf n(,);
MatrixXf result(,);
m << ,,
,;
n << ,,
,;
result = m * n;
cout << "-- Matrix m*n: --" << endl << result << endl << endl;
result = m.array() * n.array();
cout << "-- Array m*n: --" << endl << result << endl << endl;
result = m.cwiseProduct(n);
cout << "-- With cwiseProduct: --" << endl << result << endl << endl;
result = m.array() + ;
cout << "-- Array m + 4: --" << endl << result << endl << endl;
}

稍微复杂点的栗子

int main()
{
MatrixXf m(,);
MatrixXf n(,);
MatrixXf result(,);
m << ,,
,;
n << ,,
,; result = (m.array() + ).matrix() * m;
cout << "-- Combination 1: --" << endl << result << endl << endl;
result = (m.array() * n.array()).matrix() * m;
cout << "-- Combination 2: --" << endl << result << endl << endl;
}

eigen 笔记1的更多相关文章

  1. eigen 笔记2

    4. Block operations 1) Using block operations Block of size(p, q), starting at (i, j) dynamic-size b ...

  2. Eigen学习笔记2-Matrix类

    在Eigen中,所有的矩阵Matrix和向量Vector都是由Matrix类构造的.向量只不过是矩阵的特殊形式,只有一列(列向量)或者一行. Matrix模板类有6个参数,其中前三个参数是必须的.前三 ...

  3. Eigen学习笔记2:C++矩阵运算库Eigen介绍

    Eigen常规矩阵定义 1.使用 Eigen的使用在官网上有详细的介绍,这里对我学习过程中用到的基本操作进行介绍.首先是矩阵的定义.在矩阵类的模板参数共有6个.一般情况下我们只需要关注前三个参数即可. ...

  4. Eigen学习笔记1:在VS2015下Eigen(矩阵变换)的配置

    一.Eigen简介 Eigen是一个高层次的C ++库,有效支持线性代数,矩阵和矢量运算,数值分析及其相关的算法. Eigen适用范围广,支持包括固定大小.任意大小的所有矩阵操作,甚至是稀疏矩阵:支持 ...

  5. 从零开始编写深度学习库(五)Eigen Tensor学习笔记2.0

    1.extract_image_patches函数的使用: 假设Eigen::Tensor形状为(3,8,8,9),现在要对第二维.第三维根据size大小为(2,2),stride=(2,2),那么如 ...

  6. Eigen库笔记整理(二)

    Eigen/Geometry 模块提供了各种旋转和平移的表示 旋转矩阵直接使用 Matrix3d 或 Matrix3f Eigen::Matrix3d rotation_matrix = Eigen: ...

  7. Eigen库笔记整理(一)

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

  8. Eigen 矩阵库学习笔记

    最近为了在C++中使用矩阵运算,简单学习了一下Eigen矩阵库.Eigen比Armadillo相对底层一点,但是只需要添加头文库即可使用,不使用额外的编译和安装过程. 基本定义 Matrix3f是3* ...

  9. Eigen 学习笔记

    1.  初始化 //外部指针初始化 ]={...}; ] = ...; kernels[].mu = Vector3d(_mu0); kernels[].sigma_inv = Matrix3d(_s ...

随机推荐

  1. 原生js--客户端存储的种类

    客户端存储遵循同源策略,不同的站点页面之间不可以相互读取对方的数据,但同一站点的不同页面之间可以共享存储的数据 客户端存储的种类: 1.web存储 localStorage.sessionStorag ...

  2. java(9)并发编程

    整理自<java 并发编程的艺术> 1. 上下文切换 即使是单核处理器也支持多线程执行代码,CPU通过给每个线程分配CPU时间片来实现这个机制.时间片是CPU分配给各个线程的时间,因为时间 ...

  3. sencha touch 简单的倒计时插件

    效果如图: 代码: Ext.define('ux.label.Countdown', { alternateClassName: 'labelCountdown', extend: 'Ext.Comp ...

  4. 生产环境的gitlab大版本升级思路(从7.x升级到8.x)

    之前在生产环境部署的gitlab是7.x版本的,提供给公司内部的员工来使用,大概有350个用户左右,gitlab从8.x版本之后内置了CI和CD的集成,所以就考虑到升级版本的问题 通过参考和总结git ...

  5. 【咸鱼教程】Egret可长按识别二维码(精确位置和大小)

    教程目录一 实现原理二 实现过程三 Demo下载 本教程是在Egret中实现长按识别的二维码,并可以精确定位二维码的位置和大小,支持横屏和竖屏. 一 实现原理 微信中长按识别二维码,需要长按jpg或p ...

  6. 【BZOJ5090】组题 分数规划

    [BZOJ5090]组题 Description 著名出题人小Q的备忘录上共有n道可以出的题目,按照顺序依次编号为1到n,其中第i道题目的难度系数被小Q估计为a_i,难度系数越高,题目越难,负数表示这 ...

  7. MAC SVN 基本设置 终端命令

    extends:http://www.cnblogs.com/heiniuhaha/archive/2012/07/31/2616493.html 安装XCode后Mac OS X 系统已经内置了sv ...

  8. Unity3D 面试ABC

    最先执行的方法是: 1.(激活时的初始化代码)Awake,2.Start.3.Update[FixUpdate.LateUpdate].4.(渲染模块)OnGUI.5.再向后,就是卸载模块(TearD ...

  9. Unity3D笔记十六 输入输出-键盘事件、鼠标事件

    输入与控制操作Unity为开发者提供了Input类库,其中包括键盘事件.鼠标事件和触摸事件等一切跨平台所需要的控制事件. 一.键盘事件 1.按下事件 Input.GetKeyDown():如果按键被按 ...

  10. vue之cli脚手架项目中组件的使用

    在webpack-simple模板中,包括webpck模板.一个.vue文件就是一个组件. 为什么会这样呢?因为webpack干活了!webpack的将我们所有的资源文件进行打包.同时webpack还 ...