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

归约、迭代器和广播

归约

在Eigen中,有些函数可以统计matrix/array的某类特征,返回一个标量。

int main()
{
Eigen::Matrix2d mat;
mat << 1, 2,
3, 4;
cout << "Here is mat.sum(): " << mat.sum() << endl;
cout << "Here is mat.prod(): " << mat.prod() << endl;
cout << "Here is mat.mean(): " << mat.mean() << endl;
cout << "Here is mat.minCoeff(): " << mat.minCoeff() << endl;
cout << "Here is mat.maxCoeff(): " << mat.maxCoeff() << endl;
cout << "Here is mat.trace(): " << mat.trace() << endl;
}

范数计算

L2范数 squareNorm(),等价于计算vector的自身点积,norm()返回squareNorm的开方根。

这些操作应用于matrix,norm() 会返回Frobenius或Hilbert-Schmidt范数。

如果你想使用其他Lp范数,可以使用lpNorm< p >()方法。p可以取Infinity,表示L∞范数。

int main()
{
VectorXf v(2);
MatrixXf m(2,2), n(2,2); v << -1,
2; m << 1,-2,
-3,4;
cout << "v.squaredNorm() = " << v.squaredNorm() << endl;
cout << "v.norm() = " << v.norm() << endl;
cout << "v.lpNorm<1>() = " << v.lpNorm<1>() << endl;
cout << "v.lpNorm<Infinity>() = " << v.lpNorm<Infinity>() << endl;
cout << endl;
cout << "m.squaredNorm() = " << m.squaredNorm() << endl;
cout << "m.norm() = " << m.norm() << endl;
cout << "m.lpNorm<1>() = " << m.lpNorm<1>() << endl;
cout << "m.lpNorm<Infinity>() = " << m.lpNorm<Infinity>() << endl;
}

输出

v.squaredNorm() = 5
v.norm() = 2.23607
v.lpNorm<1>() = 3
v.lpNorm<Infinity>() = 2 m.squaredNorm() = 30
m.norm() = 5.47723
m.lpNorm<1>() = 10
m.lpNorm<Infinity>() = 4

Operator norm: 1-norm和∞-norm可以通过其他方式得到。

int main()
{
MatrixXf m(2,2);
m << 1,-2,
-3,4;
cout << "1-norm(m) = " << m.cwiseAbs().colwise().sum().maxCoeff()
<< " == " << m.colwise().lpNorm<1>().maxCoeff() << endl;
cout << "infty-norm(m) = " << m.cwiseAbs().rowwise().sum().maxCoeff()
<< " == " << m.rowwise().lpNorm<1>().maxCoeff() << endl;
} 1-norm(m) = 6 == 6
infty-norm(m) = 7 == 7

布尔归约

all()=true matrix/array中的所有算术是true any()=true matrix/array中至少有一个元素是true count() 返回为true元素的数目

#include <Eigen/Dense>
#include <iostream>
using namespace std;
using namespace Eigen;
int main()
{
ArrayXXf a(2,2); a << 1,2,
3,4;
cout << "(a > 0).all() = " << (a > 0).all() << endl;
cout << "(a > 0).any() = " << (a > 0).any() << endl;
cout << "(a > 0).count() = " << (a > 0).count() << endl;
cout << endl;
cout << "(a > 2).all() = " << (a > 2).all() << endl;
cout << "(a > 2).any() = " << (a > 2).any() << endl;
cout << "(a > 2).count() = " << (a > 2).count() << endl;
}

输出

(a > 0).all()   = 1
(a > 0).any() = 1
(a > 0).count() = 4 (a > 2).all() = 0
(a > 2).any() = 1
(a > 2).count() = 2

迭代器(遍历)

当我们想获取某元素在Matrix或Array中的位置的时候,迭代器是必须的。常用的有:minCoeff和maxCoeff。

int main()
{
Eigen::MatrixXf m(2,2); m << 1, 2,
3, 4;
//get location of maximum
MatrixXf::Index maxRow, maxCol;
float max = m.maxCoeff(&maxRow, &maxCol);
//get location of minimum
MatrixXf::Index minRow, minCol;
float min = m.minCoeff(&minRow, &minCol);
cout << "Max: " << max << ", at: " <<
maxRow << "," << maxCol << endl;
cout << "Min: " << min << ", at: " <<
minRow << "," << minCol << endl;
} Max: 4, at: 1,1
Min: 1, at: 0,0

部分归约

Eigen中支持对Matrx或Array的行/行进行归约操作。部分归约可以使用colwise()/rowwise()函数。

int main()
{
Eigen::MatrixXf mat(2,4);
mat << 1, 2, 6, 9,
3, 1, 7, 2; std::cout << "Column's maximum: " << std::endl
<< mat.colwise().maxCoeff() << std::endl;
} Column's maximum:
3 2 7 9

类似,针对行也可以,只是返回的是列向量而已。

int main()
{
Eigen::MatrixXf mat(2,4);
mat << 1, 2, 6, 9,
3, 1, 7, 2; std::cout << "Row's maximum: " << std::endl
<< mat.rowwise().maxCoeff() << std::endl;
} Row's maximum:
9
7

结合部分归约和其他操作

例子:寻找和最大的列向量。

int main()
{
MatrixXf mat(2,4);
mat << 1, 2, 6, 9,
3, 1, 7, 2; MatrixXf::Index maxIndex;
float maxNorm = mat.colwise().sum().maxCoeff(&maxIndex); std::cout << "Maximum sum at position " << maxIndex << std::endl;
std::cout << "The corresponding vector is: " << std::endl;
std::cout << mat.col( maxIndex ) << std::endl;
std::cout << "And its sum is is: " << maxNorm << std::endl;
}

输出

Maximum sum at position 2
The corresponding vector is:
6
7
And its sum is is: 13

广播

广播是针对vector的,将vector沿行/列重复构建一个matrix,便于后期运算。

int main()
{
Eigen::MatrixXf mat(2,4);
Eigen::VectorXf v(2); mat << 1, 2, 6, 9,
3, 1, 7, 2; v << 0,
1; //add v to each column of m
mat.colwise() += v; std::cout << "Broadcasting result: " << std::endl;
std::cout << mat << std::endl;
}

输出

Broadcasting result:
1 2 6 9
4 2 8 3

注意:对Array类型,*=,/=和/这些操作可以进行行/列级的操作,但不使用与Matrix,因为会与矩阵乘混淆。

结合广播和其他操作

示例:计算矩阵中哪列与目标向量距离最近。

int main()
{
Eigen::MatrixXf m(2,4);
Eigen::VectorXf v(2); m << 1, 23, 6, 9,
3, 11, 7, 2; v << 2,
3;
MatrixXf::Index index;
// find nearest neighbour
(m.colwise() - v).colwise().squaredNorm().minCoeff(&index);
cout << "Nearest neighbour is column " << index << ":" << endl;
cout << m.col(index) << endl;
}

输出

Nearest neighbour is column 0:
1
3

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

  1. Eigen教程(6)

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

  2. Eigen教程(11)

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

  3. Eigen教程(9)

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

  4. Eigen教程(10)

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

  5. Eigen教程(8)

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

  6. Eigen教程(5)

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

  7. Eigen教程(4)

    整理下Eigen库的教程,参考:http://eigen.tuxfamily.org/dox/index.html Array类和元素级操作 为什么使用Array 相对于Matrix提供的线性代数运算 ...

  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. openkm安装过程

    # java -jar /software/OKMInstaller.jar Running in Linux: 0.9.1-CE (build: 445bef5) Linux distro: rhe ...

  2. Ilist<object>转换成I<实体> 如何转换

    ml = objects.Cast<menu>().ToList(); //需要 using System.Linq;

  3. OLTP和OLAP有何区别?

    OLTP即联机事务处理,就是我们经常说的关系数据库,意即记录即时的增.删.改.查,就是我们经常应用的东西,这是数据库的基础:OLAP即联机分析处理,是数据仓库的核心部心,所谓数据仓库是对于大量已经由O ...

  4. Sublime text —— 自定义Color theme

    网上下载,XXX.tmTheme 样式,让后放置于 C:\Users\{用户名}\AppData\Roaming\Sublime Text 2\Packages\Color Scheme - Defa ...

  5. 将多个 docx 文件使用 POI 进行合并,生成单个文档,包含图片

    1 添加 maven 依赖,需要使用 poi 的依赖项 <!-- https://mvnrepository.com/artifact/org.apache.poi/poi-scratchpad ...

  6. 硬盘 SMART 检测参数详解[转]

    一.SMART概述 硬盘的故障一般分为两种:可预测的(predictable)和不可预测的(unpredictable).后者偶而会发生,也没有办法去预防它,例如芯片突然失效,机械撞击等.但像电机轴承 ...

  7. Centos7源代码安装freeswitch和启动freeswitch

    centos7下安装freeswitch版本:1.6. 1.下载源代码: 进入目录 cd /usr/local/src 克隆原代码 git clone -b v1.6 https://freeswit ...

  8. Mysql 导入CSV数据 语句 导入时出现乱码的解决方案

    1. 登陆mysql 2. use testdb 3. 执行导入语句 LOAD DATA LOCAL INFILE 'd://exportedtest2.csv' INTO TABLE usertab ...

  9. haproxy 让后端服务器记录用户的真是IP地址(记录在header头里)

    这里我们在生产中遇到一个问题就是.我们有的用户会登录失败.但是并不是所有的用户登录失败(这里是能够正常访问网站) 所以这里想分析哪些用户登录失败,所以我们要记录他们这些登录失败的IP地址 这里我们的结 ...

  10. c语言实现xor加密

    异或运算:^ 定义:它的定义是:两个值相同时,返回false,否则返回true.也就是说,XOR可以用来判断两个值是否不同. 特点:如果对一个值连续做两次 XOR,会返回这个值本身. ^ // 第一次 ...