Eigen教程(4)
整理下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)的更多相关文章
- Eigen教程(7)
整理下Eigen库的教程,参考:http://eigen.tuxfamily.org/dox/index.html 归约.迭代器和广播 归约 在Eigen中,有些函数可以统计matrix/array的 ...
- Eigen教程(6)
整理下Eigen库的教程,参考:http://eigen.tuxfamily.org/dox/index.html 高级初始化方法 本篇介绍几种高级的矩阵初始化方法,重点介绍逗号初始化和特殊矩阵(单位 ...
- Eigen教程(11)
整理下Eigen库的教程,参考:http://eigen.tuxfamily.org/dox/index.html 存储顺序 对于矩阵和二维数组有两种存储方式,列优先和行优先. 假设矩阵: 按行优先存 ...
- Eigen教程(9)
整理下Eigen库的教程,参考:http://eigen.tuxfamily.org/dox/index.html Eigen并没有为matrix提供直接的Reshape和Slicing的API,但是 ...
- Eigen教程(10)
整理下Eigen库的教程,参考:http://eigen.tuxfamily.org/dox/index.html 混淆 在Eigen中,当变量同时出现在左值和右值,赋值操作可能会带来混淆问题.这一篇 ...
- Eigen教程(8)
整理下Eigen库的教程,参考:http://eigen.tuxfamily.org/dox/index.html 原生缓存的接口:Map类 这篇将解释Eigen如何与原生raw C/C++ 数组混合 ...
- Eigen教程(5)
整理下Eigen库的教程,参考:http://eigen.tuxfamily.org/dox/index.html 块操作 块是matrix或array中的矩形子部分. 使用块 函数.block(), ...
- Eigen教程(3)
整理下Eigen库的教程,参考:http://eigen.tuxfamily.org/dox/index.html 矩阵和向量的运算 提供一些概述和细节:关于矩阵.向量以及标量的运算. 介绍 Eige ...
- Eigen教程(2)
整理下Eigen库的教程,参考:http://eigen.tuxfamily.org/dox/index.html Matrix类 在Eigen,所有的矩阵和向量都是Matrix模板类的对象,Vect ...
随机推荐
- java JDK JRE 1.6,1.7,1.8各个版本版本下载链接
有时想找jdk的某个版本并不太好找,在此给出所有链接及对应的api JavaSE 1.6各个版本 jdk api http://www.Oracle.com/technetwork/Java/jav ...
- solrj索引操作
添加索引 Solr添加文档至索引: http://www.cnblogs.com/dennisit/p/3621717.html 删除索引: 每天索引记录有一个唯一标识,索引的删除通过唯一标识操作,如 ...
- 企业内知识库wiki所存在的问题
相信很多公司都利用开源的wiki web app搭建了自家的内部wiki服务,比如使用media Wiki, Gollum, doku wiki, jsWiki等 但是,真正可用的企业wiki系统却没 ...
- FreeSWITCH网关参数之caller-id-in-from
1. 这个配置项两个设置值: true和false(默认) <param name="caller-id-in-from" value="true"/&g ...
- GIT 简单版
Git规范 by 程序亦非猿 2016.4.6 这又是一篇我在公司分享的,想制定一下Git的规范,有兴趣的可以看看~ 上一篇在这里 分支模型 每个项目必须要有master.develop分支. 每个开 ...
- 验证码识别 图像降噪 Python (一)
原始图片: 降噪后的图片 实现代码: # coding:utf-8 import sys, os from PIL import Image, ImageDraw # 二值数组 t2val = {} ...
- java字符串的遍历以及字符串中各类字符的统计
1.需求:获取字符串中的每一个字符 分析: A:如何能够拿到每一个字符呢? char charAt(int index) B:我怎么知道字符到底有多少个呢? int length() publi ...
- Hadoop创建/删除文件夹出错
log4j:WARN No appenders could be found for logger (org.apache.hadoop.metrics2.lib.MutableMetricsFact ...
- 使用vlc实现视频TS流的推送
鉴于Mpeg TS流播放的需求,使用 VLC作为Server来实现输出Mpeg TS 本文仅涉及如何使用VLC的Command来实现作为视频流Server通常可以使用下述四种方式来推送Mpeg ...
- 不应直接存储或返回可变成员 Mutable members should not be stored or returned directly
Mutable objects are those whose state can be changed. For instance, an array is mutable, but a Strin ...