整理下Eigen库的教程,参考:http://eigen.tuxfamily.org/dox/index.html

Array类和元素级操作

为什么使用Array

相对于Matrix提供的线性代数运算,Array类提供了更为一般的数组功能。Array类为元素级的操作提供了有效途径,比如点加(每个元素加值)或两个数据相应元素的点乘。

Array

Array是个类模板(类似于Matrx),前三个参数是必须指定的,后三个是可选的,这点和Matrix是相同的。

Array<typename Scalar, int RowsAtCompileTime, int ColsAtCompileTime>

Eigen也提供的一些常用类定义,Array是同时支持一维和二维的(Matrix二维,Vector一维)。

Type Tyoedef
Array<float,Dynamic,1> ArrayXf
Array<float,3,1> Array3f
Array<double,Dynamic,Dynamic> ArrayXXd
Array<double,3,3> Array33d

获取元素

读写操作重载于matrix, << 可以用于初始化array或打印。

#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;
}

加法和减法

和matrix类似,要求array的尺寸一致。同时支持array+/-scalar的操作!

#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;
}

输出

a + b =
2 4 6
5 7 9
8 10 12 a - 2 =
-1 0 1
2 3 4
5 6 7

乘法

支持array*scalar(类似于matrix),但是当执行array*array时,执行的是相应元素的乘积,因此两个array必须具有相同的尺寸。

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;
} a * b =
5 12
21 32

其他元素级操作

Function function
abs 绝对值
sqrt 平方根
min(.) 两个array相应元素的最小值
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;
}

array和matrix之间的转换

当需要线性代数类操作时,请使用Matrix;但需要元素级操作时,需要使用Array。这样就需要提供两者的转化方法。

Matrix提供了.array()函数将它们转化为Array对象。

Array提供了.matrix()函数将它们转化为Matrix对象。

在Eigen,在表达式中混合Matrix和Array操作是被禁止的,但是可以将array表达式结果赋值为matrix。

另外,Matrix提供了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;
}

输出

-- 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.matrix() * array2.matrix() 将执行矩阵乘法。

Eigen教程(4)的更多相关文章

  1. Eigen教程(7)

    整理下Eigen库的教程,参考:http://eigen.tuxfamily.org/dox/index.html 归约.迭代器和广播 归约 在Eigen中,有些函数可以统计matrix/array的 ...

  2. Eigen教程(6)

    整理下Eigen库的教程,参考:http://eigen.tuxfamily.org/dox/index.html 高级初始化方法 本篇介绍几种高级的矩阵初始化方法,重点介绍逗号初始化和特殊矩阵(单位 ...

  3. Eigen教程(11)

    整理下Eigen库的教程,参考:http://eigen.tuxfamily.org/dox/index.html 存储顺序 对于矩阵和二维数组有两种存储方式,列优先和行优先. 假设矩阵: 按行优先存 ...

  4. Eigen教程(9)

    整理下Eigen库的教程,参考:http://eigen.tuxfamily.org/dox/index.html Eigen并没有为matrix提供直接的Reshape和Slicing的API,但是 ...

  5. Eigen教程(10)

    整理下Eigen库的教程,参考:http://eigen.tuxfamily.org/dox/index.html 混淆 在Eigen中,当变量同时出现在左值和右值,赋值操作可能会带来混淆问题.这一篇 ...

  6. Eigen教程(8)

    整理下Eigen库的教程,参考:http://eigen.tuxfamily.org/dox/index.html 原生缓存的接口:Map类 这篇将解释Eigen如何与原生raw C/C++ 数组混合 ...

  7. Eigen教程(5)

    整理下Eigen库的教程,参考:http://eigen.tuxfamily.org/dox/index.html 块操作 块是matrix或array中的矩形子部分. 使用块 函数.block(), ...

  8. Eigen教程(3)

    整理下Eigen库的教程,参考:http://eigen.tuxfamily.org/dox/index.html 矩阵和向量的运算 提供一些概述和细节:关于矩阵.向量以及标量的运算. 介绍 Eige ...

  9. Eigen教程(2)

    整理下Eigen库的教程,参考:http://eigen.tuxfamily.org/dox/index.html Matrix类 在Eigen,所有的矩阵和向量都是Matrix模板类的对象,Vect ...

随机推荐

  1. 【svn】解析subversion的使用

    目录结构: contents structure [-] 安装客户端 安装服务端 创建仓库 启动仓库 创建客户端与仓库取得联系 使用svn服务 SVN密码管理 SVN的仓库布局和常规命令 分支.合并和 ...

  2. 一个web.Config或app.Config自定义段configSections的示例

    一个web.Config或app.Config自定义段configSections的示例 越来越觉得,直接用配置文件app.Config或web.Config配置应用系统的运行参数,比自己做一个xml ...

  3. Android WebView中软键盘会遮挡输入框相关问题

    要想实现这样的软键盘出现的时候会自己主动把输入框的布局顶上去的效果,须要设置输入法的属性,有下面两种设置方式:     一.在java代码中设置例如以下:      getWindow().setSo ...

  4. 题目要求:将a,b两个数的值进行交换,并且不使用任何的中间变量。

    a = a+b; b = a-b; a = a-b;

  5. Cadence 5141 下TSMC 05U工艺库安装

    以下资料摘自:<T13RF PDK簡介>-張文旭 观念与TSMC工艺库的安装 管理者安裝TSMC 0.13 MS/RF的環境下之PDK的安裝方式相當容易,首先以root的方式進入Unix/ ...

  6. C#基础课程之四集合(ArrayList、List<泛型>)

    list泛型的使用 ArrayList list = new ArrayList(); ArrayList list = ); //可变数组 list.Add("我"); //Ad ...

  7. U811.1接口EAI系列之一--通用把XML传送给EAI处理方法--PowerBuilder语言

    1.前面配置参考:http://www.cnblogs.com/spring_wang/p/3393147.html 2.pb通用调EAI方法代码如下: //===================== ...

  8. talend 将hbase中数据导入到mysql中

    首先,解决talend连接hbase的问题: 公司使用的机器是HDP2.2的机器,上面配置好Hbase服务,在集群的/etc/hbase/conf/hbase-site.xml下,有如下配置: < ...

  9. UIButton 标题靠右

    _classBtn =  [UIButton buttonWithType:UIButtonTypeCustom]; _classBtn.frame = CGRectMake(  kDeviceWid ...

  10. 关于NOIP的运行环境

    目前(2019年2月22日)仍然是 G++ , 终端运行,命令行: g++ test.cpp -o test ,而g++ 4.8.4默认标准是: -std=gnu++ . g++到了gcc6才把默认标 ...