[Eigen中文文档] Reshape操作

简介: 从 Eigen3.4 开始,Eigen 发布了将矩阵或向量重塑为不同大小的便捷方法。所有的操作可以通过 DenseBase::reshaped(NRowsType,NColsType) 和 DenseBase::reshaped() 两个函数完成。这些函数并不直接改变原有的变量,而是返回一个重塑后的变量副本。

文档总目录

英文原文(Reshape)

Eigen3.4 开始,Eigen 发布了将矩阵或向量重塑为不同大小的便捷方法。所有的操作可以通过 DenseBase::reshaped(NRowsType,NColsType)DenseBase::reshaped() 两个函数完成。这些函数并不直接改变原有的变量,而是返回一个重塑后的变量副本。

二维Reshape

更一般的 reshap 转换是通过 reshaped(nrows,ncols) 处理的。这是一个将 4x4 矩阵重塑为 2x8 矩阵的示例:

// 代码索引 3-8-1-1
Matrix4i m = Matrix4i::Random();
cout << "Here is the matrix m:" << endl << m << endl;
cout << "Here is m.reshaped(2, 8):" << endl << m.reshaped(2, 8) << endl;

输出:

Here is the matrix m:
 7  9 -5 -3
-2 -6  1  0
 6 -3  0  9
 6  6  3  9
Here is m.reshaped(2, 8):
 7  6  9 -3 -5  0 -3  9
-2  6 -6  6  1  3  0  9

默认情况下,无论输入表达式的存储顺序如何,输入元素始终按列优先顺序处理。有关排序、编译时大小和自动大小归约的更多信息,请参阅 DenseBase::reshaped(NRowsType,NColsType) 的文档,其中包含所有详细信息和许多示例(没找到文档,只找到源码,在Eigen/src/plugins/ReshapedMethods.h中)。

一维线性Reshape

reshape的一个非常常见的用法是将给定的二维矩阵或表达式变为一维线性的形式。在这种情况下,可以计算出维度,因此可以省略相关传参,如下例所示:

// 代码索引 3-8-1-2
Matrix4i m = Matrix4i::Random();
cout << "Here is the matrix m:" << endl << m << endl;
cout << "Here is m.reshaped().transpose():" << endl << m.reshaped().transpose() << endl;
cout << "Here is m.reshaped<RowMajor>().transpose():  " << endl << m.reshaped<RowMajor>().transpose() << endl;

输出:

Here is the matrix m:
 7  9 -5 -3
-2 -6  1  0
 6 -3  0  9
 6  6  3  9
Here is m.reshaped().transpose():
 7 -2  6  6  9 -6 -3  6 -5  1  0  3 -3  0  9  9
Here is m.reshaped<RowMajor>().transpose():  
 7  9 -5 -3 -2 -6  1  0  6 -3  0  9  6  6  3  9

.transpose() 方法始终返回列向量,默认情况下,元素始终按列优先排序。同样,请参阅 DenseBase::reshaped() 的文档以获得对排序的更多信息。

原地Reshape

上述示例都是另外创建一个reshape对象,但怎么将一个给定矩阵原地reshape呢?这个操作只适用于具有运行时维度的矩阵和数组。通常这可以通过 PlainObjectBase::resize(Index,Index) 来完成:

// 代码索引 3-8-2-1
MatrixXi m = Matrix4i::Random();
cout << "Here is the matrix m:" << endl << m << endl;
cout << "Here is m.reshaped(2, 8):" << endl << m.reshaped(2, 8) << endl;
m.resize(2,8);
cout << "Here is the matrix m after m.resize(2,8):" << endl << m << endl;

输出:

Here is the matrix m:
 7  9 -5 -3
-2 -6  1  0
 6 -3  0  9
 6  6  3  9
Here is m.reshaped(2, 8):
 7  6  9 -3 -5  0 -3  9
-2  6 -6  6  1  3  0  9
Here is the matrix m after m.resize(2,8):
 7  6  9 -3 -5  0 -3  9
-2  6 -6  6  1  3  0  9

但是请注意,与 reshaped 不同,resize 的结果取决于输入的存储顺序。因此它的行为类似于 reshaped<AutoOrder>

// 代码索引 3-8-2-2
Matrix<int,Dynamic,Dynamic,RowMajor> m = Matrix4i::Random();
cout << "Here is the matrix m:" << endl << m << endl;
cout << "Here is m.reshaped(2, 8):" << endl << m.reshaped(2, 8) << endl;
cout << "Here is m.reshaped<AutoOrder>(2, 8):" << endl << m.reshaped<AutoOrder>(2, 8) << endl;
m.resize(2,8);
cout << "Here is the matrix m after m.resize(2,8):" << endl << m << endl;

输出:

Here is the matrix m:
 7 -2  6  6
 9 -6 -3  6
-5  1  0  3
-3  0  9  9
Here is m.reshaped(2, 8):
 7 -5 -2  1  6  0  6  3
 9 -3 -6  0 -3  9  6  9
Here is m.reshaped<AutoOrder>(2, 8):
 7 -2  6  6  9 -6 -3  6
-5  1  0  3 -3  0  9  9
Here is the matrix m after m.resize(2,8):
 7 -2  6  6  9 -6 -3  6
-5  1  0  3 -3  0  9  9

最后,目前不支持将reshape处理后的矩阵分配给自身,也不支持由于别名而导致未定义的行为。禁止以下行为:

A = A.reshaped(2,8);

但这样是可以的:

A = A.reshaped(2,8).eval();
相关文章
|
5月前
|
Python
NumPy 教程 之 NumPy 矩阵库(Matrix) 3
NumPy 矩阵库教程,介绍 numpy.matlib 模块,该模块提供专门的矩阵操作函数。矩阵是由行列构成的矩形数组,元素可为数字、符号或表达式。教程展示如何使用 `numpy.matlib.zeros()` 创建全零矩阵,并演示了转置矩阵的实现方法,即通过 `T` 属性或 `transpose` 函数将 m×n 矩阵转换为 n×m 矩阵。
49 4
|
5月前
|
Python
NumPy 教程 之 NumPy 矩阵库(Matrix) 4
矩阵是由行和列构成的矩形数组,其元素可以是数字、符号或数学表达式。
43 4
|
5月前
|
Python
NumPy 教程 之 NumPy 矩阵库(Matrix) 2
不同于ndarray,matlib函数生成的是矩阵形式。教程中详细解释了矩阵的概念,并介绍了转置矩阵的实现方式,使用T属性或函数实现。此外,还展示了如何利用`matlib.empty()`创建指定形状的新矩阵,并可选择数据类型及顺序。最后通过示例演示了矩阵填充随机数据的方法。
52 3
|
5月前
|
Python
NumPy 教程 之 NumPy 矩阵库(Matrix) 8
矩阵是由行和列构成的矩形数组,其元素可以是数字、符号或表达式。教程中讲解了如何使用`numpy.matlib.rand()`创建指定大小且元素随机填充的矩阵,并演示了矩阵与ndarray之间的转换方法。此外,还介绍了如何使用T属性进行矩阵转置。示例代码展示了创建矩阵、将其转换为ndarray以及再转回矩阵的过程。
60 9
|
5月前
|
Python
NumPy 教程 之 NumPy 矩阵库(Matrix) 7
矩阵是由行和列构成的矩形数组,可包含数字、符号或表达式。教程还介绍了如何使用T属性或numpy.transpose进行矩阵转置,并演示了如何利用numpy.matlib.rand()生成指定大小的随机矩阵。示例代码展示了3x3随机矩阵的创建过程及其输出结果。
44 4
|
5月前
|
Python
NumPy 教程 之 NumPy 矩阵库(Matrix) 1
NumPy的`numpy.matlib`模块提供了一系列生成矩阵的函数。矩阵是由行和列构成的矩形数组,其元素可以是数字、符号或表达式。使用`.T`属性或`numpy.transpose`函数可实现矩阵转置,将m行n列的矩阵转换为n行m列。示例代码展示了如何通过`np.arange`和`reshape`创建矩阵,并使用`.T`进行转置。
67 2
|
5月前
|
索引 Python
NumPy 教程 之 NumPy 矩阵库(Matrix) 5
内容涵盖矩阵概念、转置操作及`numpy.matlib.eye()`函数的使用方法,示例展示了如何创建一个具有指定行列数和浮点型数据的单位矩阵。
79 0
|
5月前
|
Python
NumPy 教程 之 NumPy 矩阵库(Matrix) 6
主要内容包括矩阵的概念、转置操作及单位矩阵生成。使用numpy.matlib提供的工具,如`numpy.matlib.identity()`可创建指定大小的单位矩阵,示例中创建了一个5x5的浮点型单位矩阵,并展示了其输出结果。
51 0
|
存储 算法 NoSQL
[Eigen中文文档] 稀疏矩阵操作
在许多应用中(例如,有限元方法),通常要处理非常大的矩阵,其中只有少数系数不为零。在这种情况下,可以通过使用仅存储非零系数的特殊表示来减少内存消耗并提高性能。这样的矩阵称为稀疏矩阵。
532 0
|
存储 编译器