C++精解【7】

文章目录

eigen

矩阵初始化

多维矩阵

  • 数组
cpp 复制代码
MatrixXi a {      // construct a 2x2 matrix
      {1, 2},     // first row
      {3, 4}      // second row
};
Matrix<double, 2, 3> b {
      {2, 3, 4},
      {5, 6, 7},
};
cpp 复制代码
#include <iostream>
#include "f:/learn/eigen-3.4.0/Eigen/Dense"


using Eigen::Matrix;
int main()
{
    Matrix<int,3,2> a {{1, 2}, {3, 4}, {5, 6}};
    std::cout << a<< std::endl;
}
bash 复制代码
1 2
3 4
5 6

Process returned 0 (0x0)   execution time : 0.117 s
Press any key to continue.
  • 重定向操作符
cpp 复制代码
Matrix3f m;
m << 1, 2, 3,
     4, 5, 6,
     7, 8, 9;
std::cout << m;
bash 复制代码
1 2 3
4 5 6
7 8 9

矩阵和向量size

  • rows:矩阵行
  • cols:矩阵列
  • resize:重新规划size
cpp 复制代码
#include <iostream>
#include "e:/eigen/Eigen/Dense"
using namespace std;
using namespace Eigen;
int main()
{
  MatrixXf m(2,5);
  cout << "The matrix m is of size "
            << m.rows() << "x" << m.cols() << std::endl;
  m.resize(4,3);
  cout << "The matrix m resized is of size "
            << m.rows() << "x" << m.cols() << std::endl;
  cout << "It has " << m.size() << " coefficients" << std::endl;
  VectorXf v(2);
  cout << "The vector v is of size " << v.size() << std::endl;
  v.resize(5);
  cout << "The vector v resized is of size " << v.size() << std::endl;
  cout << "As a matrix, v is of size "
            << v.rows() << "x" << v.cols() << std::endl;
}
bash 复制代码
The matrix m is of size 2x5
The matrix m resized is of size 4x3
It has 12 coefficients
The vector v is of size 2
The vector v resized is of size 5
As a matrix, v is of size 5x1

Process returned 0 (0x0)   execution time : 0.317 s
Press any key to continue.

固定大小or 动态大小

什么时候应该使用固定大小(例如Matrix4f),什么时候应该使用动态大小(例如MatrixXf)?

  • 简单的答案是:对于非常小的尺寸尽可能使用固定大小,对于较大的尺寸或必须使用动态大小。对于较小的大小,特别是小于(大约)16的大小,使用固定大小对性能非常有益,因为它允许Eigen避免动态内存分配并展开循环。
  • 当然,使用固定大小的限制是,只有在编译时知道大小时才有可能。此外,对于足够大的大小,例如大于(大约)32的大小,使用固定大小的性能优势变得可以忽略不计。
  • 更糟糕的是,尝试在函数内部使用固定大小创建一个非常大的矩阵可能会导致堆栈溢出,因为Eigen会尝试将数组自动分配为局部变量,而这通常是在堆栈上完成的。

Matrix类六个模板参数

  • 模板参数
cpp 复制代码
Matrix<typename Scalar,
       int RowsAtCompileTime,
       int ColsAtCompileTime,
       int Options = 0,
       int MaxRowsAtCompileTime = RowsAtCompileTime,
       int MaxColsAtCompileTime = ColsAtCompileTime>
  • Options是位字段。
    RowMajor。它指定这种类型的矩阵使用行为主存储顺序;默认情况下,存储顺序是以列为主的。请参阅存储订单页面。例如,此类型表示行为主的3x3矩阵:
cpp 复制代码
  Matrix<float, 3, 3, RowMajor>
  • MaxRowsAtCompileTime和MaxColsAtCompileTime
    在需要指定时非常有用,即使在编译时不知道矩阵的确切大小,但在编译时知道一个固定的上限。这样做的最大原因可能是为了避免动态内存分配。例如,下面的矩阵类型使用12个浮点数的普通数组,没有动态内存分配
cpp 复制代码
Matrix<float, Dynamic, Dynamic, 0, 3, 4>
  • 存储顺序详解
    矩阵和二维数组有两种不同的存储顺序:列为主和行为主
    当矩阵存储在内存中时,条目必须以某种方式线性排列。有两种主要的方法可以做到这一点,按行和按列。
  1. 如果一个矩阵是逐行存储的,我们就说它是按行主序存储 row-major的。首先存储整个第一行,然后是整个第二行,依此类推。以矩阵为例
cpp 复制代码
8 2 2 9 9 1 4 4 3 5 4 5 
  1. 如果一个矩阵是按列存储 column-major 的,那么它是以列为主顺序存储的,从整个第一列开始,然后是整个第二列,依此类推。若将上述矩阵按列主序存储,则其布局如下:
cpp 复制代码
8 9 3 2 1 5 2 4 4 9 4 5 

3、例

定义一个4*3的矩阵A,然后分别以按行或按列存储。
使用PlainObjectBase::data()函数,该函数返回指向矩阵第一个条目的内存位置的指针。

cpp 复制代码
#include <iostream>
#include "e:/eigen/Eigen/Dense"
using namespace std;
using namespace Eigen;
int main()
{
    Matrix<int, 4, 3, ColMajor> Acolmajor;
    Acolmajor << 1,2,3,4,
                 5,6,7,8,
                 9,10,11,12;
    cout << "The matrix A:" << endl;
    cout << Acolmajor << endl << endl;

    cout << "In memory (column-major):" << endl;
    for (int i = 0; i < Acolmajor.size(); i++)
      cout << *(Acolmajor.data() + i) << "  ";
    cout << endl << endl;

    Matrix<int, 4, 3, RowMajor> Arowmajor = Acolmajor;
    cout << "In memory (row-major):" << endl;
    for (int i = 0; i < Arowmajor.size(); i++)
      cout << *(Arowmajor.data() + i) << "  ";
    cout << endl;
}
bash 复制代码
The matrix A:
 1  2  3
 4  5  6
 7  8  9
10 11 12

In memory (column-major):
1  4  7  10  2  5  8  11  3  6  9  12

In memory (row-major):
1  2  3  4  5  6  7  8  9  10  11  12

Process returned 0 (0x0)   execution time : 0.404 s
Press any key to continue.
  • Matrix类模板有六个模板参数,其中三个是强制性的(Scalar, RowsAtCompileTime和ColsAtCompileTime),另外三个是可选的(Options, MaxRowsAtCompileTime和MaxColsAtCompileTime)。如果Options参数设置为RowMajor,则矩阵或数组按行主要顺序存储;如果设置为ColMajor,则按列主顺序存储。该机制在上述特征程序中用于指定存储顺序。

  • 如果未指定存储顺序,则Eigen默认以列为主的方式存储条目。如果使用方便类型(Matrix3f、ArrayXXd等)之一,也会出现这种情况。

  • 使用一种存储顺序的矩阵和数组可以分配给使用另一种存储顺序的矩阵和数组,就像上面的程序中使用Acolmajor初始化Arowmajor时发生的那样。Eigen将自动重新排序条目。更一般地说,行为主矩阵和列为主矩阵可以在表达式中混合使用。

  • 您应该在程序中使用哪种存储顺序呢?这个问题没有简单的答案;这取决于您的应用程序。以下是一些需要牢记的要点:

1.您的用户可能希望您使用特定的存储顺序。或者,您可以使用Eigen以外的其他库,这些库可能需要特定的存储顺序。在这些情况下,在整个程序中使用这种存储顺序可能是最简单和最快的。

2.当矩阵以行为主的顺序存储时,由于更好的数据局部性,逐行遍历矩阵的算法将运行得更快。类似地,对于列主矩阵,逐列遍历更快。为了找出适合您的特定应用程序的更快的方法,进行一些实验可能是值得的。

3.缺省情况下,Eigen是列为主的。自然地,大多数特征库的开发和测试都是用列主矩阵完成的。这意味着,尽管我们的目标是透明地支持列为主和行为主的存储顺序,但Eigen库可能最适合列为主的矩阵。

初始化向量

cpp 复制代码
#include <iostream>
#include "f:/learn/eigen-3.4.0/Eigen/Dense"


using Eigen::VectorXd;
using Eigen::RowVectorXd;
int main()
{
    VectorXd a {{1.5, 2.5, 3.5}};
    RowVectorXd b {{1.0, 2.0, 3.0, 4.0}};
    std::cout << a<< std::endl;
    std::cout << b<< std::endl;
}
bash 复制代码
1.5
2.5
3.5
1 2 3 4

Process returned 0 (0x0)   execution time : 0.110 s
Press any key to continue.

元素类型

cpp 复制代码
MatrixNt for Matrix<type, N, N>. For example, MatrixXi for Matrix<int, Dynamic, Dynamic>.
MatrixXNt for Matrix<type, Dynamic, N>. For example, MatrixX3i for Matrix<int, Dynamic, 3>.
MatrixNXt for Matrix<type, N, Dynamic>. For example, Matrix4Xd for Matrix<d, 4, Dynamic>.
VectorNt for Matrix<type, N, 1>. For example, Vector2f for Matrix<float, 2, 1>.
RowVectorNt for Matrix<type, 1, N>. For example, RowVector3d for Matrix<double, 1, 3>.
  • N可以是2、3、4或X中的任意一个(表示动态)。
  • T可以是I (int)、f (float)、d (double)、cf (complex)或CD (complex)中的任意一个。虽然只为这五种类型定义了类型定义,但这并不意味着它们是唯一受支持的标量类型

参考文献

  1. Eigen
相关推荐
lxp1997411 分钟前
php函数积累
开发语言·php
科技资讯早知道5 分钟前
java计算机毕设课设—坦克大战游戏
java·开发语言·游戏·毕业设计·课程设计·毕设
꧁༺❀氯ྀൢ躅ྀൢ❀༻꧂6 分钟前
实验4 循环结构
c语言·算法·基础题
白拾16 分钟前
使用Conda管理python环境的指南
开发语言·python·conda
新晓·故知31 分钟前
<基于递归实现线索二叉树的构造及遍历算法探讨>
数据结构·经验分享·笔记·算法·链表
咖啡里的茶i34 分钟前
C++之继承
c++
从0至142 分钟前
力扣刷题 | 两数之和
c语言·开发语言
总裁余(余登武)42 分钟前
算法竞赛(Python)-万变中的不变“随机算法”
开发语言·python·算法
NormalConfidence_Man43 分钟前
C++新特性汇总
开发语言·c++
一个闪现必杀技1 小时前
Python练习2
开发语言·python