4. Block operations

1) Using block operations

Block of size(p, q), starting at (i, j)

dynamic-size block expression: matrix.block(i, j, p, q);

fixed-size block expression: matrix.block<p, q>(i, j);

  1. int main()
  2. {
  3. Eigen::MatrixXf m(,);
  4. m << , , , ,
  5. , , , ,
  6. ,,,,
  7. ,,,;
  8. cout << "Block in the middle" << endl;
  9. cout << m.block<,>(,) << endl << endl;
  10. for (int i = ; i <= ; ++i)
  11. {
  12. cout << "Block of size " << i << "x" << i << endl;
  13. cout << m.block(,,i,i) << endl << endl;
  14. }
  15. }

Output:

  1. Block in the middle
  2.  
  3. Block of size 1x1
  4.  
  5. Block of size 2x2
  6.  
  7. Block of size 3x3

.block() 的返回值可以是左值, 也可以是右值.

2) Columns and rows

matrix.row(i), matrix.col(j)

  1. int main()
  2. {
  3. Eigen::MatrixXf m(,);
  4. m << ,,,
  5. ,,,
  6. ,,;
  7. cout << "Here is the matrix m:" << endl << m << endl;
  8. cout << "2nd Row: " << m.row() << endl;
  9. m.col() += * m.col();
  10. cout << "After adding 3 times the first column into the third column, the matrix m is:\n";
  11. cout << m << endl;
  12. }

3) Corner-related operations

太复杂, 没兴趣.

4) Block operations for vectors

同上, 用时再查.

5. Advanced initialization

1) The comma initializer

  1. RowVectorXd vec1();
  2. vec1 << , , ;
  3.  
  4. RowVectorXd vec2();
  5. vec2 << , , , ;
  6.  
  7. RowVectorXd joined();
  8. joined << vec1, vec2;
  9.  
  10. MatrixXf matA(, );
  11. matA << , , , ;
  12.  
  13. MatrixXf matB(, );
  14. matB << matA, matA/, matA/, matA;
  15.  
  16. Matrix3f m;
  17. m.row() << , , ;
  18. m.block(,,,) << , , , ;
  19. m.col().tail() << , ;

2) Special matrices and arrays

Zero(), Constant(value), Constant(rows, cols, value), Random(), Identity(), LinSpaced(size, low, high),

Identity() only for matrix;

LinSpaced only for vectors and one-dimensional arrays; high也包含在数组中. 默认都是列向量.

  1. std::cout << "A fixed-size array:\n";
  2. Array33f a1 = Array33f::Zero();
  3.  
  4. std::cout << "A one-dimensional dynamic-size array:\n";
  5. ArrayXf a2 = ArrayXf::Zero();
  6.  
  7. std::cout << "A two-dimensional dynamic-size array:\n";ArrayXXf a3 = ArrayXXf::Zero(, );
  1. ArrayXXf table(, );
  2. table.col() = ArrayXf::LinSpaced(, , );
  3. table.col() = M_PI / * table.col();
  4. table.col() = table.col().sin();
  5. table.col() = table.col().cos();
  6.  
  7. Output:
  8. Degrees Radians Sine Cosine
  9.  
  10. 0.175 0.174 0.985
  11. 0.349 0.342 0.94
  12. 0.524 0.5 0.866
  13. 0.698 0.643 0.766
  14. 0.873 0.766 0.643
  15. 1.05 0.866 0.5
  16. 1.22 0.94 0.342
  17. 1.4 0.985 0.174
  18. 1.57 -4.37e-08

类似的, 有 setZero(), MatrixBase::setIdentity(), DenseBase::setLinSpaced()

3) Usage as temporary objects

  1. int main()
  2. {
  3. MatrixXd m = MatrixXd::Random(,);
  4. m = (m + MatrixXd::Constant(,,1.2)) * ;
  5. cout << "m =" << endl << m << endl;
  6. VectorXd v();
  7. v << , , ;
  8. cout << "m * v =" << endl << m * v << endl;
  9. }

comma-initializer

  1. MatrixXf mat = MatrixXf::Random(, );
  2. std::cout << mat << std::endl << std::endl;
  3. mat = (MatrixXf(,) << , , , ).finished() * mat;

注意到, .finished() 是必须的.

6. Reductions, visitors and broadcasting

1) Reducations

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

2) Norm computations

应该用不到.

3) Boolean reductions

all(), any(), count()

  1. int main()
  2. {
  3. ArrayXXf a(,);
  4.  
  5. a << ,,
  6. ,;
  7. cout << "(a > 0).all() = " << (a > ).all() << endl;
  8. cout << "(a > 0).any() = " << (a > ).any() << endl;
  9. cout << "(a > 0).count() = " << (a > ).count() << endl;
  10. cout << endl;
  11. cout << "(a > 2).all() = " << (a > ).all() << endl;
  12. cout << "(a > 2).any() = " << (a > ).any() << endl;
  13. cout << "(a > 2).count() = " << (a > ).count() << endl;
  14. }
  15.  
  16. Output:
  17. (a > ).all() =
  18. (a > ).any() =
  19. (a > ).count() =
  20.  
  21. (a > ).all() =
  22. (a > ).any() =
  23. (a > ).count() =

4) Visitors

maxCoeff(&x, &y), minCoeff(&x, &y)

x, y 即为行索引和列索引, MatrixXf::Index, 注意, 不是 int

  1. //get location of maximum
  2. MatrixXf::Index maxRow, maxCol;
  3. float max = m.maxCoeff(&maxRow, &maxCol);
  4.  
  5. //get location of minimum
  6. MatrixXf::Index minRow, minCol;
  7. float min = m.minCoeff(&minRow, &minCol);

5) Partial reductions

colwise(), rowwise()

  1. int main() {
  2. Eigen::MatrixXf mat(, );
  3. mat << , , , ,
  4. , , , ;
  5. cout << "Column's maximum: " << endl
  6. << mat.colwise().maxCoeff() << endl;
  7. cout << "Row's maximum: " << endl
  8. << mat.rowwise().maxCoeff() << endl;
  9. }
  10.  
  11. Output:
  12. Column's maximum:
  13.  
  14. Row's maximum:

combining partial reductions with other operations

  1. int main() {
  2. MatrixXf mat(, );
  3. mat << , , , ,
  4. , , , ;
  5. MatrixXf::Index maxIndex;
  6. float maxNorm = mat.colwise().sum().maxCoeff(&maxIndex);
  7.  
  8. cout << "Maximum sum at position: " << maxIndex << endl;
  9. cout << "The corresponding vector is: " << endl;
  10. cout << mat.col(maxIndex) << endl;
  11. cout << "And its sum is: " << maxNorm << endl;
  12. }
  13.  
  14. Output:
  15. Maximum sum at position
  16. The corresponding vector is:
  17.  
  18. And its sum is is:

6) Broadcasting

  1. int main() {
  2. MatrixXf mat(, );
  3. VectorXf v();
  4.  
  5. mat << , , , ,
  6. , , , ;
  7. v << , ;
  8. w << , , , ;
  9.  
  10. mat.colwise() += v;
  11.  
  12. cout << "Broadcasting result: " << endl;
  13. cout << mat << endl;
  14.  
  15. mat.rowwise() += w.transpose();
  16. cout << "Broadcasting result: " << endl;
  17. cout << mat << endl;
  18. }
  19.  
  20. Output:
  1. Broadcasting result:
  2. 1 2 6 9
  3. 4 2 8 3
  1. Broadcasting result:

7) Combining broadcasting with other operations

略.

7. Interfacing with raw buffers: the Map class

1) Map types and declaring Map variables

Map<Matrix<typename Scalar, int RowsAtCompileTime, int ColsAtCompileTime> >

RowsAtCompileTime 和 ColsAtCompileTime 是生成矩阵的行数和列数

Map<MatrixXf> mf(pf, rows, columns);

pf 指向一个定义 coefficient array 的 region 的起始, 这里注意 pf 是一个指针, 指向的是一个 coefficient, rows 和 columns 是该 mf 的行和列, 默认全部.

这里有一个问题, Map 模板参数指定的 RowsAtCompileTime 和 ColsAtCompileTime 与括号中的 rows 和 columns 有什么关系?

a. Matrix

Dynamic

Map<Matrix<int, Dynamic, Dynamic> > 即 Map<MatrixXi>, 此时生成 matrix 的行和列由 rows 和 columns 指定

这里还要注意一下, pf 的属性, 如果 pf 是指向一个内存区域, 内存区域的数字怎么获取, 要看这个区域存储的是什么.

存储 array, 假设 array 长度为 15, 要转换成3行4列矩阵, 那么取前12个数

存储 matrix, 注意 matrix 默认是按列存储的, 所以会取按列排布的前12个数, 也就是前4列

fixed-size

Map<Matrix<int, 3, 4> > , 此时生成 Matrix 的行和列已定, rows 和 columns 要与 3, 4 保持一致, 或者省略否则报错.

b. Vector

Dynamic

Map<Matrix<int, 1, Dynamic> > or Map<Matrix<int, Dynamic, 1> >

Vector 有一个特别地方, rows 和 columns 可以只写一个, 比如 Map<VectorXi> m2map(p, m2.size()), 也可以全写, 但是维度要与 Map 中的一致.

fixed-size

注意维度一致.

Map<const Vector4i> mi(pi);

声明一个 fixed-size read-only vector.

Map 还有几个可选的模板参数

Map<typename MatrixType, int MapOptions, typename StrideType>

MapOptions: Aligned or Unaligned, 默认 Unaligned

StrideType: 决定 array 的排布, 使用 Stride class

  1. int array[];
  2. for(int i = ; i < ; ++i) array[i] = i;
  3. cout << "Column-major:\n" << Map<Matrix<int,,> >(array) << endl;
  4. cout << "Row-major:\n" << Map<Matrix<int,,,RowMajor> >(array) << endl;
  5. cout << "Row-major using stride:\n" <<
  6. Map<Matrix<int,,>, Unaligned, Stride<,> >(array) << endl;
  7.  
  8. Output:
  9.  
  10. Column-major:
  11.  
  12. Row-major:
  13.  
  14. Row-major using stride:

2) Using Map variables

注意: a Map type is not identical to its Dense equivalent.

  1. typedef Matrix<float,,Dynamic> MatrixType;
  2. typedef Map<MatrixType> MapType;
  3. typedef Map<const MatrixType> MapTypeConst; // a read-only map
  4. const int n_dims = ;
  5.  
  6. MatrixType m1(n_dims), m2(n_dims);
  7. m1.setRandom();
  8. m2.setRandom();
  9. float *p = &m2(); // get the address storing the data for m2
  10. MapType m2map(p,m2.size()); // m2map shares data with m2
  11. MapTypeConst m2mapconst(p,m2.size()); // a read-only accessor for m2
  12. cout << "m1: " << m1 << endl;
  13. cout << "m2: " << m2 << endl;
  14. cout << "Squared euclidean distance: " << (m1-m2).squaredNorm() << endl;
  15. cout << "Squared euclidean distance, using map: " <<
  16. (m1-m2map).squaredNorm() << endl;
  17. m2map() = ; // this will change m2, since they share the same array
  18. cout << "Updated m2: " << m2 << endl;
  19. cout << "m2 coefficient 2, constant accessor: " << m2mapconst() << endl;
  20. /* m2mapconst(2) = 5; */ // this yields a compile-time error
  21.  
  22. Output:
  23.  
  24. m1: 0.68 -0.211 0.566 0.597 0.823
  25. m2: -0.605 -0.33 0.536 -0.444 0.108
  26. Squared euclidean distance: 3.26
  27. Squared euclidean distance, using map: 3.26
  28. Updated m2: -0.605 -0.33 0.536 0.108
  29. m2 coefficient , constant accessor: 0.536

这里注意一下, m2map, m2mapconst 与 m2 共享内存空间, 所以对 m2map 的修改, 会同步到 m2 和 m2mapconst.

但是, 如果如果 *p 指向的是 array 中的地址, 那么 map 转化出的 matrix 不会与 array 共享内存空间.

3) Changing the mapped array

用 new 可以对声明后的 Map object 进行修改

  1. int data[] = {,,,,,,,,};
  2. Map<RowVectorXi> v(data,);
  3. cout << "The mapped vector v is: " << v << "\n";
  4. new (&v) Map<RowVectorXi>(data+,);
  5. cout << "Now v is: " << v << "\n";
  6.  
  7. Output:
  8. The mapped vector v is:
  9. Now v is:

所以也可以声明未知 array 的 map object

  1. Map<Matrix3f> A(NULL); // don't try to use this matrix yet!
  2. VectorXf b(n_matrices);
  3. for (int i = ; i < n_matrices; i++)
  4. {
  5. new (&A) Map<Matrix3f>(get_matrix_pointer(i));
  6. b(i) = A.trace();
  7. }

8. Reshape and Slicing

1) Reshape

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

注意这里, 存储方式的不同, 会导致输出不同.

2) Slicing

  1. RowVectorXf v = RowVectorXf::LinSpaced(,,);
  2. cout << "Input:" << endl << v << endl;
  3. Map<RowVectorXf,,InnerStride<> > v2(v.data(), v.size()/);
  4. cout << "Even:" << v2 << endl;
  5.  
  6. Output:
  7. Input:
  8.  
  9. Even:
  1. MatrixXf M1 = MatrixXf::Random(,);
  2. cout << "Column major input:" << endl << M1 << "\n";
  3. Map<MatrixXf,,OuterStride<> > M2(M1.data(), M1.rows(), (M1.cols()+)/, OuterStride<>(M1.outerStride()*));
  4. cout << "1 column over 3:" << endl << M2 << "\n";
  5. typedef Matrix<float,Dynamic,Dynamic,RowMajor> RowMajorMatrixXf;
  6. RowMajorMatrixXf M3(M1);
  7. cout << "Row major input:" << endl << M3 << "\n";
  8. Map<RowMajorMatrixXf,,Stride<Dynamic,> > M4(M3.data(), M3.rows(), (M3.cols()+)/,
  9. Stride<Dynamic,>(M3.outerStride(),));
  10. cout << "1 column over 3:" << endl << M4 << "\n";
  11.  
  12. Output:
  13. Column major input:
  14. 0.68 0.597 -0.33 0.108 -0.27 0.832 -0.717 -0.514
  15. -0.211 0.823 0.536 -0.0452 0.0268 0.271 0.214 -0.726
  16. 0.566 -0.605 -0.444 0.258 0.904 0.435 -0.967 0.608
  17. column over :
  18. 0.68 0.108 -0.717
  19. -0.211 -0.0452 0.214
  20. 0.566 0.258 -0.967
  21. Row major input:
  22. 0.68 0.597 -0.33 0.108 -0.27 0.832 -0.717 -0.514
  23. -0.211 0.823 0.536 -0.0452 0.0268 0.271 0.214 -0.726
  24. 0.566 -0.605 -0.444 0.258 0.904 0.435 -0.967 0.608
  25. column over :
  26. 0.68 0.108 -0.717
  27. -0.211 -0.0452 0.214
  28. 0.566 0.258 -0.967

注意 M1.data(), 返回一个指针.

9. Aliasing

m = m.transpose() 会造成 aliasing

a) m = m.transpose().eval();

b) m = m.transposeInPlace();

component-wise operations is safe.

mat = 2 * mat;

mat = mat - MatrixXf::Identity(2, 2);

matB = matA * matA; // Simple bu not quite as efficient

matB.noalias() = matA * matA; // Use this

10. Storage orders

默认 column-major, 也可以选择 row-major

11. Quick reference guide

Eigen/LU    Inverse, determinant, LU decompositions with solver (FullPivLU, PartialPivLU)

Eigen/SVD    SVD decompositions with least-squares solver (JacobiSVD, BDCSVD)

Eigen/QR    decomposition with solver (HouseholderQR, ColPivHouseholderQR, FullPivHouseholderQR)

...

Finished.

eigen 笔记2的更多相关文章

  1. eigen 笔记1

    c++ 的 eigen 类似于 python 的 numpy, 还有一个类似的库是 Armadillo, 当然还有 opencv. Armadillo 与 matlab 在函数名称上更接近, 但是 T ...

  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. 斐讯K2刷不死breed与第三方固件教程

    本文主要就是简单的斐讯 K2 刷机教程,方便大家了解一下 K2 怎样刷固件.斐讯 K2 是一款 1200M AC 双频无线路由器,支持 5G 和 2.4G WiFi 信号,虽然缺少 USB 且只有百兆 ...

  2. LeetCode 12 Integer to Roman (整数转罗马数字)

    题目链接: https://leetcode.com/problems/integer-to-roman/?tab=Description   String M[] = {"", ...

  3. 从Java代码到字节码(1)

    理解Java代码是如何被编译为字节码并在Java虚拟机(JVM)上执行是非常重要的,这将帮助理解你的程序是如何执行的.这样的理解不仅仅能够让你在逻辑上更好的掌握语言特性,而且能够有机会理解在做出重要决 ...

  4. [工具] Snipaste

    https://zh.snipaste.com/ Snipaste 是一个简单但强大的截图工具,也可以让你将截图贴回到屏幕上! 下载并打开 Snipaste,按下 F1 来开始截图, 选择“复制到剪贴 ...

  5. [HTML5]移动平台的HTML5开发框架

    jQuery Mobile http://jquerymobile.com/ jQTouch http://jqtouch.com/ DHTMLX Touch http://dhtmlx.com/to ...

  6. Office2007 每次打开斗需要检查 【配置进度】

    打开C:\Program Files\Common Files\microsoft shared\OFFICE12\Office Setup Controller目录下将SETUP.EXE 重命名为 ...

  7. Linux 常用命令标记

    1.linux 服务器之间拷贝文件 scp 本地用户名@IP地址:文件名1 远程用户名@IP地址:文件名2 该命令可以变型为目的服务器204上输入如下命令:红色部分是需要接受目的地,-r是递归复制该文 ...

  8. 为什么用VUE,而不用Jquery了?

    在没有任何前端框架之前,我们写代码,只能用原生的JS,进行数据的处理,DOM的操作,譬如对一个id 为txtName 的文本框进行赋值,我们是这样的 document.getElementById(' ...

  9. iOS钱包卡券开发(往钱包里面加自己的卡券)

    参考文章 https://blog.csdn.net/sz_vcp2007/article/details/60762349 https://blog.csdn.net/eqera/article/d ...

  10. python3学习笔记(3)_dict-set

    # !/usr/bin/env python3 # -*- coding:utf8 -*- #dict 和 set #dict dictionary 用于存放 键值对的, 无序,key 不可变 #姓名 ...