Eigen(3)-The Array class and coefficient-wise operations(行列式)

Eigen数组类

数组类和系数运算

什么是数组类/行列式

与用于线性代数的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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Eigen3Config.cmake和eigen3-config.cmake是用于配置Eigen3库的包配置文件。这些文件包含了关于Eigen3库的信息,如库的路径、头文件路径和编译选项等。这些文件通常由Eigen3库的安装程序生成,并用于帮助CMake在编译时找到并链接Eigen3库。引用 当你在编译使用Eigen3库的项目时,如果编译器无法找到这些文件,就会出现报错信息。这可能是由于Eigen3库的版本与你的项目所需的版本不兼容,或者是库的安装位置或配置有问题。引用 如果你想解决这个问题,可以首先确保你已经正确地安装了Eigen3库,并且安装路径是正确的。然后,你可以尝试在CMakeLists.txt文件中添加以下内容,以帮助CMake找到Eigen3库的位置: ``` find_package(Eigen3 3.1 REQUIRED) ``` 这将告诉CMake在编译时查找版本为3.1的Eigen3库。如果你的Eigen3库版本不是3.1,可以根据实际情况修改这个版本号。引用 如果你仍然遇到问题,你可以尝试手动设置Eigen3库的路径,方法是使用cmake-gui或在CMakeLists.txt中添加以下内容: ``` set(Eigen3_DIR /path/to/eigen3) ``` 将`/path/to/eigen3`替换为你Eigen3库的实际路径。引用 综上所述,Eigen3Config.cmake和eigen3-config.cmake是用于配置Eigen3库的包配置文件。如果编译时无法找到这些文件,你可以尝试检查库的版本兼容性、安装路径和配置,并根据需要进行相应的修改。<span class="em">1</span><span class="em">2</span><span class="em">3</span><span class="em">4</span>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值