Eigen实现matlab中的reshape

本文详细介绍了Eigen库在3.4版本中如何进行矩阵重塑,包括2D视图的重塑和1D线性视图的创建。通过DenseBase::reshaped()函数,可以方便地将矩阵转换为不同尺寸的视图,而不改变原始数据。同时,也讨论了原地重排(resize)操作,需要注意其依赖于输入的存储顺序。示例代码展示了各种重塑和重排的方法及其效果。
摘要由CSDN通过智能技术生成

https://eigen.tuxfamily.org/dox-devel/group__TutorialReshape.html

注意,需要Eigen3.4版本


Since the version 3.4, Eigen exposes convenient methods to reshape a matrix to another matrix of different sizes or vector. All cases are handled via the DenseBase::reshaped(NRowsType,NColsType) and DenseBase::reshaped() functions. Those functions do not perform in-place reshaping, but instead return a view on the input expression.

Reshaped 2D views 2维矩阵的重排

The more general reshaping transformation is handled via: reshaped(nrows,ncols). Here is an example reshaping a 4x4 matrix to a 2x8 one:

例子1

code:注意,默认是列优先,matlab的reshape是列优先

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;

output:

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

例子2

code:注意,默认是列优先,matlab的reshape是列优先

 Eigen::VectorXd M1(9);    // Column-major storage
 M1 << 1, 2, 3,  4,  5,  6, 7, 8, 9;
cout << "Here is the matrix m:" << endl << m << endl;
cout << "Here is m.reshaped(3,3):" << endl << m.reshaped(3,3) << endl;

output:

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

By default, the input coefficients are always interpreted in column-major order regardless of the storage order of the input expression. For more control on ordering, compile-time sizes, and automatic size deduction, please see de documentation of DenseBase::reshaped(NRowsType,NColsType) that contains all the details with many examples.

1D linear views 1维矩阵的重排

A very common usage of reshaping is to create a 1D linear view over a given 2D matrix or expression. In this case, sizes can be deduced and thus omitted as in the following example:

例子3

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;

output:

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

This shortcut always returns a column vector and by default input coefficients are always interpreted in column-major order. Again, see the documentation of DenseBase::reshaped() for more control on the ordering.

TutorialReshapeInPlace 原地重排

The above examples create reshaped views, but what about reshaping inplace a given matrix? Of course this task in only conceivable for matrix and arrays having runtime dimensions. In many cases, this can be accomplished via PlainObjectBase::resize(Index,Index):

例子4

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

output:

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

However beware that unlike reshaped, the result of resize depends on the input storage order. It thus behaves similarly to reshaped:

例子5

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;

output

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

Finally, assigning a reshaped matrix to itself is currently not supported and will result to undefined-behavior because of aliasing . The following is forbidden:

A = A.reshaped(2,8); 

This is OK:

A = A.reshaped(2,8).eval(); 
Eigen是一个C++模板库,用于线性代数运算,它提供了一套高效且易于使用的矩阵和向量操作功能。然而,Eigen本身并没有直接提供像MATLAB的`poly`函数那样的多项式处理功能,该函数通常用于创建多项式的系数向量。 如果你想要在Eigen实现类似的功能,你需要手动编写代码来计算多项式表达式或处理多项式系数。这可能涉及到创建一个类来存储多项式,并实现一些基本的操作,比如加法、乘法和求值。你可以定义一个类,例如`Polynomial`,其包含一个向量来存储系数,并提供方法如`evaluate`来计算给定点的值。 举个简单的例子,你可以这样设计: ```cpp #include <Eigen/Dense> template <typename Scalar> class Polynomial { public: std::vector<Scalar> coefficients; // 添加构造函数和成员函数来设置系数和执行操作 Polynomial(const std::vector<Scalar>& coeffs) : coefficients(coeffs) {} // 求值函数 Scalar evaluate(Scalar x) const { Scalar result = 0; for (size_t i = 0; i < coefficients.size(); ++i) { result += coefficients[i] * pow(x, static_cast<int>(coefficients.size() - i - 1)); } return result; } }; // 使用示例 int main() { Eigen::VectorXd coeffs({1, 2, 3}); // 生成一个二次多项式:x^2 + 2x + 3 Polynomial<double> poly(coeffs); double x = 2.0; double value = poly.evaluate(x); // 计算x=2时的多项式值 std::cout << "Value at x=" << x << ": " << value << std::endl; return 0; } ``` 请注意,这只是一个基础的实现,并非完整的`poly`函数,实际使用时可能需要添加更多的错误检查和功能支持。如果你想处理更复杂的多项式操作,比如因式分解或高阶导数,你可能需要依赖其他库,比如SymEngine等专门用于数学符号计算的工具。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值