数组类和系数运算
什么是数组类/行列式
与用于线性代数的Matrix类相反,Array类提供了通用数组。此外,Array类提供了一种执行逐系数运算的简便方法,该运算可能没有线性代数含义,例如,向数组中的每个系数添加一个常数或逐个系数地乘以两个数组。
数组类型
Array是具有与Matrix相同的模板参数的类模板。与Matrix一样,前三个模板参数是必需的:
Array<typename Scalar, int RowsAtCompileTime, int ColsAtCompileTime>
Eigen还为某些常见情况提供了typedef,其方式类似于Matrix typedef,但有一些细微的差异,因为单词“ array”既用于一维数组,也用于二维数组。我们采用ArrayNt形式的typedef代表一维数组的约定,其中N和t是大小和标量类型,如本页上解释的Matrix typedefs 所示。对于二维数组,我们使用ArrayNNt形式的typedef。下表显示了一些示例:
Array<float,Dynamic,1> ArrayXf
Array<float,3,1> Array3f
Array<double,Dynamic,Dynamic> ArrayXXd
Array<double,3,3> Array33d
访问数组内的值
括号运算符被重载以提供对数组系数的写和读访问,就像矩阵一样。此外,<<可以使用运算符来初始化数组(通过逗号初始化程序)或打印它们。
#include <Eigen/Dense>
#include <iostream>
using namespace Eigen;
using namespace std;
int main()
{
ArrayXXf m(2,2);
// assign some values coefficient by coefficient
m(0,0) = 1.0; m(0,1) = 2.0;
m(1,0) = 3.0; m(1,1) = m(0,1) + m(1,0);
// print values to standard output
cout << m << endl << endl;
// using the comma-initializer is also allowed
m << 1.0,2.0,
3.0,4.0;
// print values to standard output
cout << m << endl;
}
output
1 2
3 5
1 2
3 4
加减
两个数组的加减法与矩阵相同。如果两个数组的大小相同,并且该加法或减法是按系数进行的,则此操作有效。
数组还支持以下形式的表达式,该表达式array + scalar将标量添加到数组中的每个系数。这提供了不能直接用于Matrix对象的功能。
#include <Eigen/Dense>
#include <iostream>
using namespace Eigen;
using namespace std;
int main()
{
ArrayXXf a(3,3);
ArrayXXf b(3,3);
a << 1,2,3,
4,5,6,
7,8,9;
b << 1,2,3,
1,2,3,
1,2,3;
// Adding two arrays
cout << "a + b = " << endl << a + b << endl << endl;
// Subtracting a scalar from an array
cout << "a - 2 = " << endl << a - 2 << endl;
}
output
a + b =
2 4 6
5 7 9
8 10 12
a - 2 =
-1 0 1
2 3 4
5 6 7
数组乘法
首先,当然,您可以将一个数组乘以标量,这与矩阵的工作方式相同。数组与矩阵根本不同的地方是将两个矩阵相乘。矩阵将乘法解释为矩阵乘积,而数组将乘法解释为按系数乘积。因此,当且仅当两个数组具有相同的维数时,它们才能相乘。
#include <Eigen/Dense>
#include <iostream>
using namespace Eigen;
using namespace std;
int main()
{
ArrayXXf a(2,2);
ArrayXXf b(2,2);
a << 1,2,
3,4;
b << 5,6,
7,8;
cout << "a * b = " << endl << a * b << endl;
}
output
a * b =
5 12
21 32
其他系数运算
所述数组类定义其他系数为单位的运算除了加法,减法和上述乘法运算符。例如,.abs()方法获取每个系数的绝对值,而.sqrt()计算系数的平方根。如果您有两个大小相同的数组,则可以调用.min(.)来构造其系数是两个给定数组中对应系数的最小值的数组。以下示例说明了这些操作
#include <Eigen/Dense>
#include <iostream>
using namespace Eigen;
using namespace std;
int main()
{
ArrayXf a = ArrayXf::Random(5);
a *= 2;
cout << "a =" << endl
<< a << endl;
cout << "a.abs() =" << endl
<< a.abs() << endl;
cout << "a.abs().sqrt() =" << endl
<< a.abs().sqrt() << endl;
cout << "a.min(a.abs().sqrt()) =" << endl
<< a.min(a.abs().sqrt()) << endl;
}
output
a =
1.36
-0.422
1.13
1.19
1.65
a.abs() =
1.36
0.422
1.13
1.19
1.65
a.abs().sqrt() =
1.17
0.65
1.06
1.09
1.28
a.min(a.abs().sqrt()) =
1.17
-0.422
1.06
1.09
1.28
在数组和矩阵表达式之间转换
什么时候应该使用Matrix类的对象,什么时候应该使用Array类的对象?您不能对数组应用矩阵运算,也不能对矩阵应用数组运算。因此,如果您需要进行线性代数运算(例如矩阵乘法),则应使用矩阵。如果需要进行系数运算,则应使用数组。但是,有时并不是那么简单,但是您需要同时使用Matrix和Array操作。在这种情况下,您需要将矩阵转换为数组或反向转换。无论选择将对象声明为数组还是矩阵,都可以访问所有操作
矩阵表达式具有.array()方法,可以将它们“转换”为数组表达式,因此可以轻松地应用按系数进行运算。相反,数组表达式具有.matrix()方法。与所有Eigen表达式抽象一样,这没有任何运行时开销(只要您让编译器进行优化)。既.array()和.matrix()可被用作右值和作为左值。
Eigen禁止在表达式中混合矩阵和数组。例如,您不能直接添加矩阵和数组。运算+符的操作数要么都是矩阵,要么都是数组。但是,使用.array()和.matrix()可以轻松地将其转换为另一种。此规则的例外是赋值运算符:允许将矩阵表达式分配给数组变量,或将数组表达式分配给矩阵变量。
下面的示例演示如何通过使用.array()方法对Matrix对象使用数组操作。例如,语句需要两个矩阵和,它们转换到二者的阵列,用来将它们相乘系数明智和受让人结果到矩阵变量(这是合法的,因为本征允许分配阵列表达式矩阵的变量)。result = m.array() * n.array()mnresult
实际上,这种使用情况非常普遍,以至于Eigen为矩阵提供了const .cwiseProduct(。)方法来计算系数乘积。示例程序中也显示了这一点。
#include <Eigen/Dense>
#include <iostream>
using namespace Eigen;
using namespace std;
int main()
{
MatrixXf m(2,2);
MatrixXf n(2,2);
MatrixXf result(2,2);
m << 1,2,
3,4;
n << 5,6,
7,8;
result = m * n;
cout << "-- Matrix m*n: --" << endl << result << endl << endl;
result = m.array() * n.array();
cout << "-- Array m*n: --" << endl << result << endl << endl;
result = m.cwiseProduct(n);
cout << "-- With cwiseProduct: --" << endl << result << endl << endl;
result = m.array() + 4;
cout << "-- Array m + 4: --" << endl << result << endl << endl;
}
output
-- Matrix m*n: --
19 22
43 50
-- Array m*n: --
5 12
21 32
-- With cwiseProduct: --
5 12
21 32
-- Array m + 4: --
5 6
7 8
同样,如果array1和array2是数组,则表达式array1.matrix() * array2.matrix()将计算其矩阵乘积。
这是一个更高级的示例。该表达式(m.array() + 4).matrix() * m将4加到矩阵中的每个系数m,然后使用来计算结果的矩阵乘积m。类似地,表达式(m.array() * n.array()).matrix() * m计算矩阵的系数之积m和n,然后用结果的矩阵积m。
#include <Eigen/Dense>
#include <iostream>
using namespace Eigen;
using namespace std;
int main()
{
MatrixXf m(2,2);
MatrixXf n(2,2);
MatrixXf result(2,2);
m << 1,2,
3,4;
n << 5,6,
7,8;
result = (m.array() + 4).matrix() * m;
cout << "-- Combination 1: --" << endl << result << endl << endl;
result = (m.array() * n.array()).matrix() * m;
cout << "-- Combination 2: --" << endl << result << endl << endl;
}
output
-- Combination 1: --
23 34
31 46
-- Combination 2: --
41 58
117 170
本文深入探讨Eigen库中的数组类(Array),与矩阵类(Matrix)的区别,以及如何进行逐系数运算。介绍了数组类的创建、访问、基本算术运算、乘法及其它系数运算,并演示了如何在数组和矩阵表达式间转换。
1199

被折叠的 条评论
为什么被折叠?



