整理下Eigen库的教程,参考:http://eigen.tuxfamily.org/dox/index.html

混淆

在Eigen中,当变量同时出现在左值和右值,赋值操作可能会带来混淆问题。这一篇将解释什么是混淆,什么时候是有害的,怎么使用做。

例子

MatrixXi mat(3,3);
mat << 1, 2, 3, 4, 5, 6, 7, 8, 9;
cout << "Here is the matrix mat:\n" << mat << endl;
// This assignment shows the aliasing problem
mat.bottomRightCorner(2,2) = mat.topLeftCorner(2,2);
cout << "After the assignment, mat = \n" << mat << endl;

输出

Here is the matrix mat:
1 2 3
4 5 6
7 8 9
After the assignment, mat =
1 2 3
4 1 2
7 4 1

mat.bottomRightCorner(2,2) = mat.topLeftCorner(2,2); 赋值中展示了混淆。

mat(1,1) 在bottomRightCorner(2,2)和topLeftCorner(2,2)都存在。赋值结果中mat(2,2)本应该赋予操作前mat(1,1)的值=5。但是,最终程序结果mat(2,2)=1。原因是Eigen使用了lazy evaluation(懒惰评估),上面等价于

mat(1,1) = mat(0,0);
mat(1,2) = mat(0,1);
mat(2,1) = mat(1,0);
mat(2,2) = mat(1,1);

下面会解释如何通过eval()来解决这个问题。

混淆还会在缩小矩阵时出现,比如 vec = vec.head(n)mat = mat.block(i,j,r,c)

一般来说,混淆在编译阶段很难被检测到。比如第一个例子,如果mat再大一些可能就不会出现混淆了。但是Eigen可以在运行时检测某些混淆,如前面讲的例子。

Matrix2i a; a << 1, 2, 3, 4;
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;
Here is the matrix a:
1 2
3 4
and the result of the aliasing effect:
1 2
2 4

我们可以通过EIGEN_NO_DEBUG宏,在编译时关闭运行时的断言。

解决混淆问题

Eigen需要把右值赋值为一个临时matrix/array,然后再将临时值赋值给左值,便可以解决混淆。eval()函数实现了这个功能。

MatrixXi mat(3,3);
mat << 1, 2, 3, 4, 5, 6, 7, 8, 9;
cout << "Here is the matrix mat:\n" << mat << endl;
// The eval() solves the aliasing problem
mat.bottomRightCorner(2,2) = mat.topLeftCorner(2,2).eval();
cout << "After the assignment, mat = \n" << mat << endl;

输出

Here is the matrix mat:
1 2 3
4 5 6
7 8 9
After the assignment, mat =
1 2 3
4 1 2
7 4 5

同样: a = a.transpose().eval(); ,当然我们最好使用 transposeInPlace()。如果存在xxxInPlace函数,推荐使用这类函数,它们更加清晰地标明了你在做什么。提供的这类函数:

Origin In-place
MatrixBase::adjoint() MatrixBase::adjointInPlace()
DenseBase::reverse() DenseBase::reverseInPlace()
LDLT::solve() LDLT::solveInPlace()
LLT::solve() LLT::solveInPlace()
TriangularView::solve() TriangularView::solveInPlace()
DenseBase::transpose() DenseBase::transposeInPlace()

而针对vec = vec.head(n)这种情况,推荐使用conservativeResize()

混淆和component级的操作。

组件级是指整体的操作,比如matrix加法、scalar乘、array乘等,这类操作是安全的,不会出现混淆。

MatrixXf mat(2,2);
mat << 1, 2, 4, 7;
cout << "Here is the matrix mat:\n" << mat << endl << endl;
mat = 2 * mat;
cout << "After 'mat = 2 * mat', mat = \n" << mat << endl << endl;
mat = mat - MatrixXf::Identity(2,2);
cout << "After the subtraction, it becomes\n" << mat << endl << endl;
ArrayXXf arr = mat;
arr = arr.square();
cout << "After squaring, it becomes\n" << arr << endl << endl;

输出

Here is the matrix mat:
1 2
4 7 After 'mat = 2 * mat', mat =
2 4
8 14 After the subtraction, it becomes
1 4
8 13 After squaring, it becomes
1 16
64 169

混淆和矩阵的乘法

在Eigen中,矩阵的乘法一般都会出现混淆。除非是方阵(实质是元素级的乘)。

MatrixXf matA(2,2);
matA << 2, 0, 0, 2;
matA = matA * matA;
cout << matA; 4 0
0 4

其他的操作,Eigen默认都是存在混淆的。所以Eigen对矩阵乘法自动引入了临时变量,对的matA=matA*matA这是必须的,但是对matB=matA*matA这样便是不必要的了。我们可以使用noalias()函数来声明这里没有混淆,matA*matA的结果可以直接赋值为matB。

matB.noalias() = matA * matA;

从Eigen3.3开始,如果目标矩阵resize且结果不直接赋值给目标矩阵,默认不存在混淆。

MatrixXf A(2,2), B(3,2);
B << 2, 0, 0, 3, 1, 1;
A << 2, 0, 0, -2;
A = (B * A).cwiseAbs();//cwiseAbs()不直接赋给目标
//A = (B * A).eval().cwiseAbs()
cout << A;

当然,对于任何混淆问题,都可以通过matA=(matB*matA).eval() 来解决。

总结

当相同的矩阵或array在等式左右都出现时,很容易出现混淆。

  1. compnent级别的操作不用考虑混淆。
  2. 矩阵相乘,Eigen默认会解决混淆问题,如果你确定不会出现混淆,可以使用noalias()来提效。
  3. 混淆出现时,可以用eval()和xxxInPlace()函数解决。

Eigen教程(10)的更多相关文章

  1. node-webkit教程(10)Platform Service之File dialogs

    node-webkit教程(10)Platform Service之File dialogs 文/玄魂 目录 node-webkit教程(10)Platform Service之File dialog ...

  2. 【译】ASP.NET MVC 5 教程 - 10:添加验证

    原文:[译]ASP.NET MVC 5 教程 - 10:添加验证 在本节中,我们将为Movie模型添加验证逻辑,并确认验证规则在用户试图使用程序创建和编辑电影时有效. DRY 原则 ASP.NET M ...

  3. Linux pwn入门教程(10)——针对函数重定位流程的几种攻击

    作者:Tangerine@SAINTSEC 本系列的最后一篇 感谢各位看客的支持 感谢原作者的付出一直以来都有读者向笔者咨询教程系列问题,奈何该系列并非笔者所写[笔者仅为代发]且笔者功底薄弱,故无法解 ...

  4. [译]Vulkan教程(10)交换链

    [译]Vulkan教程(10)交换链 Vulkan does not have the concept of a "default framebuffer", hence it r ...

  5. 黑马lavarel教程---10、lavarel模型关联

    黑马lavarel教程---10.lavarel模型关联 一.总结 一句话总结: 1.模型关联比较方便,一次定义,后面都可以使用 2.关联关系 使用动态属性进行调用 1.一对多,多对多实例? 一对多: ...

  6. Directx11教程(10) 画一个简易坐标轴

    原文:Directx11教程(10) 画一个简易坐标轴       本篇教程中,我们将在三维场景中,画一个简易的坐标轴,分别用红.绿.蓝三种颜色表示x,y,z轴的正向坐标轴. 为此,我们要先建立一个A ...

  7. 深度学习与CV教程(10) | 轻量化CNN架构 (SqueezeNet,ShuffleNet,MobileNet等)

    作者:韩信子@ShowMeAI 教程地址:http://www.showmeai.tech/tutorials/37 本文地址:http://www.showmeai.tech/article-det ...

  8. Eigen教程(7)

    整理下Eigen库的教程,参考:http://eigen.tuxfamily.org/dox/index.html 归约.迭代器和广播 归约 在Eigen中,有些函数可以统计matrix/array的 ...

  9. Eigen教程(6)

    整理下Eigen库的教程,参考:http://eigen.tuxfamily.org/dox/index.html 高级初始化方法 本篇介绍几种高级的矩阵初始化方法,重点介绍逗号初始化和特殊矩阵(单位 ...

随机推荐

  1. 【MAVEN】搜索错误“Index downloads are disabled,search results may be incomplete”

    出现上面这个错误,需要将Maven的索引下载到本地. 应用后,在Window -> Show View -> Other -> Maven -> Maven Repositor ...

  2. SpringMVC整合Mongodb开发,高级操作

    开发环境: 操作系统:windows xpMongodb:2.0.6依 赖 包:Spring3.2.2 + spring-data-mongodb-1.3.0 + Spring-data-1.5 +  ...

  3. 转 mongodb win7 32位系统安装以及配置

    mongodb win7 32位系统安装以及配置   今天安装 win7 32位系统 mongodb 费了好大劲..记录一下,希望相同的同学可以少踩点坑. 1.安装 我安装的是3.2.4  地址:ht ...

  4. [转]Spring注解-@Configuration注解、@Bean注解以及配置自动扫描、bean作用域

    1.@Configuration标注在类上,相当于把该类作为spring的xml配置文件中的<beans>,作用为:配置spring容器(应用上下文) package com.test.s ...

  5. Android新特性--ConstraintLayout完全解析

    Android新特性--ConstraintLayout完全解析 本篇文章的主题是ConstraintLayout.其实ConstraintLayout是Android Studio 2.2中主要的新 ...

  6. 【转】在一个Job中同时写入多个HBase的table

    在进行Map/Reduce时,有的业务需要在一个job中将数据写入到多个HBase的表中,下面是实现方式. 原文地址:http://lookfirst.com/2011/07/hbase-multit ...

  7. haproxy 日志配置

    haproxy日志配置 haproxy在默认情况不会记录日志,除了在haproxy.conf中的global段指定日志的输出外,还需要配置系统日志的配置文件.下面以centos6.4为例,haprox ...

  8. 复习下C 链表操作(单向循环链表、查找循环节点)

    循环链表 稍复杂点. 肯能会有0 或 6 字型的单向循环链表.  接下来创建 单向循环链表 并 查找单向循环链表中的循环节点. 这里已6字型单向循环链表为例. //创建 循环链表 Student * ...

  9. 实现一个简单的虚拟demo算法

    假如现在你需要写一个像下面一样的表格的应用程序,这个表格可以根据不同的字段进行升序或者降序的展示. 这个应用程序看起来很简单,你可以想出好几种不同的方式来写.最容易想到的可能是,在你的 JavaScr ...

  10. Burpsuite如何抓取使用了SSL或TLS传输的 IOS App流量

    之前一篇文章介绍了Burpsuite如何抓取使用了SSL或TLS传输的Android App流量,那么IOS中APP如何抓取HTTPS流量呢, 套路基本上与android相同,唯一不同的是将证书导入i ...