简介

Eigen还没有提供resharp或者slicing的处理函数,但是,可以使用Map 类来实现这些功能。

实现resharp

操作Resharp及修改Matrix的维度大小,而其系数保持不变。Resharp时,应该返回一个对象,而保留原对象不变。

Eigen提供了示例.

    MatrixXf M1(3,3);    // Column-major storageM1 << 1, 2, 3,4, 5, 6,7, 8, 9;Map<RowVectorXf> v1(M1.data(), M1.size());cout << "v1:" << endl << v1 << endl;Matrix<float,Dynamic,Dynamic,RowMajor> M2(M1);Map<RowVectorXf> v2(M2.data(), M2.size());cout << "v2:" << endl << v2 << endl;cout << endl;MatrixXf M1(2,6);    // Column-major storageM1 << 1, 2, 3,  4,  5,  6,7, 8, 9, 10, 11, 12;Map<MatrixXf> M2(M1.data(), 6,2);cout << "M2:" << endl << M2 << endl;

执行结果:

v1:
1 4 7 2 5 8 3 6 9
v2:
1 2 3 4 5 6 7 8 9M2:1  47 102  58 113  69 12

上面示例,从一个3X3的矩阵,创建得到了1X9的矩阵。后续的操作,将列优先的2X6矩阵转换,得到6X2的矩阵,需要注意它们在内存中的系数的顺序。

实现Slicing

切片slicing大致的操作如此: 在一个矩阵内,安装一定的间隔,取得一组的行、或者列、或部分元素。

下面示例使用Map来模拟:

For instance, one can skip every P elements in a vector:

    RowVectorXf v = RowVectorXf::LinSpaced(20,0,19);cout << "Input:" << endl << v << endl;Map<RowVectorXf,0,InnerStride<2> > v2(v.data(), v.size()/2);cout << "Even:" << v2 << endl;

查看结果:

Input:0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19
Even: 0  2  4  6  8 10 12 14 16 18

也可以根据存储顺序,使用合适的outer-strider、inner-strider,来获取矩阵的某些列。
这里先看一下Stride的定义:


/** \class Stride* \ingroup Core_Module** \brief Holds strides information for Map** This class holds the strides information for mapping arrays with strides with class Map.** It holds two values: the inner stride and the outer stride.** The inner stride is the pointer increment between two consecutive entries within a given row of a* row-major matrix or within a given column of a column-major matrix.** The outer stride is the pointer increment between two consecutive rows of a row-major matrix or* between two consecutive columns of a column-major matrix.** These two values can be passed either at compile-time as template parameters, or at runtime as* arguments to the constructor.** Indeed, this class takes two template parameters:*  \tparam _OuterStrideAtCompileTime the outer stride, or Dynamic if you want to specify it at runtime.*  \tparam _InnerStrideAtCompileTime the inner stride, or Dynamic if you want to specify it at runtime.** */... ... /** \brief Convenience specialization of Stride to specify only an inner stride* See class Map for some examples */
template<int Value>
class InnerStride : public Stride<0, Value>
{typedef Stride<0, Value> Base;public:EIGEN_DEVICE_FUNC InnerStride() : Base() {}EIGEN_DEVICE_FUNC InnerStride(Index v) : Base(0, v) {} // FIXME making this explicit could break valid code
};/** \brief Convenience specialization of Stride to specify only an outer stride* See class Map for some examples */
template<int Value>
class OuterStride : public Stride<Value, 0>
{typedef Stride<Value, 0> Base;public:EIGEN_DEVICE_FUNC OuterStride() : Base() {}EIGEN_DEVICE_FUNC OuterStride(Index v) : Base(v,0) {} // FIXME making this explicit could break valid code
};

可以看到,InnerStride是第一个参数,作用于某种优先存储模式的优先级上的实体,比如行优先时,作用于行上的实体;列优先时,作用于列实体。
而OuterStride作用于行矩阵的行或者列,如行优先时作用于行,而列优先时,作用于列。

这是示例程序:

//matrix_sliding.cpp
#include <iostream>
#include <Eigen/Dense>using namespace std;
using namespace Eigen;int main()
{MatrixXf M1 = MatrixXf::Random(3,8);cout << "Column major input:" << endl << M1 << "\n";cout << "M1's outerStride: " << M1.outerStride() << "    -- M1.cols(): " << M1.cols() << endl;Map<MatrixXf,0,OuterStride<> > M2(M1.data(), /*3*/ M1.rows(), /*3*/ (M1.cols()+2)/3, OuterStride<>(M1.outerStride()*3));cout << "1 column over 3:" << endl << M2 << "\n";typedef Matrix<float,Dynamic,Dynamic,RowMajor> RowMajorMatrixXf;RowMajorMatrixXf M3(M1);cout << "Row major input:" << endl << M3 << "\n";cout << "M3.outerStride(): " << M3.outerStride() << endl;Map<RowMajorMatrixXf,0,Stride<Dynamic,3> > M4(M3.data(), M3.rows(), (M3.cols()+2)/3,Stride<Dynamic,3>(M3.outerStride(),3));cout << "1 column over 3:" << endl << M4 << "\n";
}

执行结果:

$ g++   -I /usr/local/include/eigen3 matrix_sliding.cpp -o matrix_sliding
$
$ ./matrix_sliding
Column major input:-0.999984 -0.0826997  -0.905911   0.869386   0.661931  0.0594004  -0.233169   0.373545-0.736924  0.0655345   0.357729  -0.232996  -0.930856   0.342299  -0.866316   0.1779530.511211  -0.562082   0.358593  0.0388328  -0.893077  -0.984604  -0.165028   0.860873
M1's outerStride: 3    -- M1.cols(): 8
1 column over 3:
-0.999984  0.869386 -0.233169
-0.736924 -0.232996 -0.8663160.511211 0.0388328 -0.165028
Row major input:-0.999984 -0.0826997  -0.905911   0.869386   0.661931  0.0594004  -0.233169   0.373545-0.736924  0.0655345   0.357729  -0.232996  -0.930856   0.342299  -0.866316   0.1779530.511211  -0.562082   0.358593  0.0388328  -0.893077  -0.984604  -0.165028   0.860873
M3.outerStride(): 8
1 column over 3:
-0.999984  0.869386 -0.233169
-0.736924 -0.232996 -0.8663160.511211 0.0388328 -0.165028

示例里使用Outstride,M1是缺省的列优先。M2映射M1得到,指定了OuterStride,取1个列,然后跳过2个列,得到新的矩阵。M1 --(Map)-->M2

M3是指定了行优先,拷贝M1构建的矩阵,系数与M1相同。从M3 映射得到M4时,指定了Stride<Dynamic,3>(M3.outerStride(),3)M3 --(Map)-->M4

从输出中,我们可以知道,在缺省未指定的情况下,Outstride的参数为其对应的行(行优先)或者列(列优先)的元素数量。如M1的OuterStride为3(每列3个元素),M3位8(每行8个列元素)。

Eigen入门之密集矩阵 8 - resharp slicing切片相关推荐

  1. Eigen入门之密集矩阵 7 - Map class:连接Eigen与C++的数据

    简介 本文介绍一下Dense Matrix如何与c/C++的数组进行交互操作,这在引入其他的库中的vector向量和矩阵到Eigen中时要使用到的技术. 有时,你有一些定义好的数据,可能是数组,你需要 ...

  2. Eigen入门之密集矩阵 6 - Reductions, visitors and broadcasting

    简介 本文介绍一下Dense Matrix的3中操作: reduction, visitor, broadcasting. 归约计算reduction. Eigen的归约计算是这样的一类计算,它是对矩 ...

  3. Eigen入门之密集矩阵 10 - 矩阵的行优先及列优先存储

    简介 本篇介绍Eigen中矩阵及二维数组的系数存储顺序–行优先及列优先,已经如何指定优先顺序. 行优先(row-majoe). 列优先(column-majoe) 矩阵的系数条目组成了一个二维的结构, ...

  4. Eigen入门之密集矩阵 9 - 别名混乱Aliasing

    简介 别名混乱Aliasing是指在赋值表达式中,一个Eigen对象(矩阵.数组.向量)同时出现在左值和右值表达式中,比如v = v*2; m = m.transpose();; 别名混乱会引起错误, ...

  5. Eigen入门之密集矩阵 5 - 再谈Matrix初始化

    简介 这里将讨论一下高级些的矩阵初始化方法. comma-initializer 逗号初始化器 comma-initializer方法很简单,可以一下把矩阵/向量的系数全部设置完.语法很简单,使用逗号 ...

  6. Eigen入门之密集矩阵 4 - 块操作

    简介 Eigen 中Matrix/Array提供了.block()来进行block区块操作,这是面向系数提供的操作功能. 语法 Eigen中提供了2种语法,针对产生的结果是一致的.但存在性能上的不同, ...

  7. Eigen入门之密集矩阵 3 - Array操作

    简介 在Eigen内,有Matrix,vector进行线性代数的相关运算,但也需要执行对矩阵内的系数的相关操作时,这是正常的功能需求.Eigen中的Array类就是满足此需求的. Array 定义 和 ...

  8. Eigen入门之密集矩阵 2-- Matrix及Vector的计算方法

    简介 Eigen内的Matrix和Vector提供了类似C++的运算符,如+,-,*:也提供了编程的函数方法,如点乘和叉乘的dot(), cross(),如此等等. 在Eigen的Matrix类,代表 ...

  9. Eigen入门之密集矩阵 1 -- 类Matrix介绍

    简介 本篇介绍Eigen中的Matrix类.在Eigen中,矩阵和向量的类型都用Matrix来表示.向量是一种特殊的矩阵,其只有一行或者一列. Matrix构造 在Matrix.h中,定义了Matri ...

最新文章

  1. 用Auto-TensorCore代码生成优化matmul
  2. Spring如何解决循环依赖问题
  3. 使用caffe训练时Loss变为nan的原因
  4. 换个姿势为安装包重签名
  5. LeetCode LCP 34. 二叉树染色(树上DP)
  6. java描述常用的集合类_Java常用的集合类
  7. 华为Mate50渲染图曝光:经典奥利奥相机模组
  8. OpenCv之绘图(笔记03)
  9. 商业WiFi“风来了”
  10. elasticsearch5.0.1集群排错的几个思路总结
  11. SQL Server 中位数、标准差、平均数
  12. 钢铁雄心4mod星火 国策树代码-1
  13. x5maxl l android 6,Hi-Fi新纪元 4.75mm超薄vivo X5Max首测
  14. C# 随机生成名字,电话,图像
  15. Linux PCI驱动编写
  16. Chatopera 张凯:创业的信念,为了小家和大家
  17. echarts 乡镇级地图制作办法
  18. Third season seventeenth episode,Ross and Rachel can not stay at one place???
  19. CISP查询具体步骤
  20. 快讯 | 第80届金球奖揭晓

热门文章

  1. JS获取当前时间date()的用法
  2. ASP.NET MVC下使用AngularJs语言(三):ng-options
  3. 第七周作业——简单FTP
  4. 依赖注入容器 Castle windsor的使用
  5. [2]-使用busybox-1.17.2制作文件系统
  6. xstream不映射字段_这本XStream学习手册,真的不来看看?
  7. CentOS6.2安装redmine2.3
  8. 区块链和java哪个更难_java 区块链中设计合理的难度系数
  9. Codeforces Round #636 (Div. 3)(ABC)
  10. 开放下载 | 阿里妈妈技术年货来啦!