简介

Eigen内的Matrix和Vector提供了类似C++的运算符,如+,-,*;也提供了编程的函数方法,如点乘和叉乘的dot(), cross(),如此等等。

在Eigen的Matrix类,代表矩阵matrics和向量vector,重载的运算符仅用于支持线性代数的运算,而不支持标量计算。比如matrix1 * matrix2,表示矩阵matrix 乘以 matrix2,而matrix1 + 10则不允许。

加法和减法

如大家所知,如果2个矩阵运行运算,对2个矩阵的行数和列数是有条件要求的。另外,在Eigen内,用于计算时,矩阵的系数类型必须统一,并不会内部进行类型转换。

二元运算符 + : ma + mb;

二元运算符 - : ma - mb;

一元运算符 - : - ma;

组合运算符 +=: ma+=mb;

组合运算符 -=: ma-=mb;

示例:

// matrix_cal1.cpp
#include <iostream>
#include <Eigen/Dense>using namespace Eigen;int main()
{Matrix2d a;a << 1, 2,3, 4;MatrixXd b(2,2);b << 5, 6,7, 8;std::cout << "a + b =\n" << a + b << std::endl;std::cout << "a - b =\n" << a - b << std::endl;std::cout << "Doing a += b;" << std::endl;a += b;std::cout << "Now a =\n" << a << std::endl;std::cout << endl;Vector3d v(1,2,3);Vector3d w(1,0,0);std::cout << "-v + w - v =\n" << -v + w - v << std::endl;
}

执行后输出:

$ ./matrix_cal1
a + b =6  8
10 12
a - b =
-4 -4
-4 -4
Doing a += b;
Now a =6  8
10 12
-v + w - v =
-1
-4
-6

与标量的乘、除

与标量的乘法和除法计算是支持的,重载的操作符如下:

二元运算符 * : matrix * scalar;

二元运算符 * : scalar * matrix;

二元运算符 / : matrix / scalar;

组合运算符 *=: matrix *= scalar;

组合运算符 /=: matrix /= scalar;

示例:

// matrix_cal2.cpp
#include <iostream>
#include <Eigen/Dense>using namespace Eigen;int main()
{Matrix2d a;a << 1, 2,3, 4;std::cout << "a * 2.5 =\n" << a * 2.5 << std::endl;Vector3d v(1,2,3);std::cout << "calculating v *= 2;" << std::endl;v *= 2;std::cout << "2.0 * v =\n" << 2.0 * v << std::endl;std::cout << "Now v =\n" << v << std::endl;}

执行结果:

$ ./matrix_cal2
a * 2.5 =
2.5   5
7.5  10
calculating v *= 2;
2.0 * v =48
12
Now v =
2
4
6

转置和共轭

在线性代数中,矩阵有转置操作,共轭计算,共轭转置计算: aTa^TaT, aˉ\bar aaˉ, a∗a^*a∗, Matrix提供了对应的函数:transpose(), conjugate(), and adjoint()
说明一下: 共轭是针对复数而言的,共轭矩阵也是复数矩阵的对应的共轭矩阵。

示例程序:

// matrix_cal3.cpp
#include <iostream>
#include <Eigen/Dense>using namespace Eigen;
using namespace std;int main()
{MatrixXcf a = MatrixXcf::Random(2,2);cout << "Here is the matrix a\n" << a << endl;cout << "Here is the matrix a^T\n" << a.transpose() << endl;cout << "Here is the conjugate of a\n" << a.conjugate() << endl;cout << "Here is the matrix a^*\n" << a.adjoint() << endl;
}

执行:

$ g++   matrix_cal3.cpp -o matrix_cal3 -I /usr/local/include/eigen3
$ ./matrix_cal3
Here is the matrix a
(-0.999984,-0.736924) (0.0655345,-0.562082)
(0.511211,-0.0826997)  (-0.905911,0.357729)
Here is the matrix a^T
(-0.999984,-0.736924) (0.511211,-0.0826997)
(0.0655345,-0.562082)  (-0.905911,0.357729)
Here is the conjugate of a(-0.999984,0.736924)  (0.0655345,0.562082)(0.511211,0.0826997) (-0.905911,-0.357729)
Here is the matrix a^*(-0.999984,0.736924)  (0.511211,0.0826997)(0.0655345,0.562082) (-0.905911,-0.357729)

对实数矩阵,并没有对应的共轭矩阵,其共轭转置矩阵就是其转置矩阵。

matrix-matrix与matrix-vector的乘法

矩阵的matrix-matrix乘法,及矩阵*矩阵,是由操作符operator *完成的。而在Eigen内,向量vector也是一种特殊的矩阵,所以,matrix-vector和vector-vector的乘法也是由operator *来完成。

这里有2种运算:

  • 二元运算符 * : a * b;
  • 复合运算符 *=: a *= b;

示例:

// matrix_cal4.cpp
#include <iostream>
#include <Eigen/Dense>using namespace Eigen;int main()
{Matrix2d mat;mat << 1, 2,3, 4;Vector2d u(-1,1), v(2,0);std::cout << "Here is mat*mat:\n" << mat*mat << std::endl;std::cout << "Here is mat*u:\n" << mat*u << std::endl;std::cout << "Here is u^T*mat:\n" << u.transpose()*mat << std::endl;std::cout << "Here is u^T*v:\n" << u.transpose()*v << std::endl;std::cout << "Here is u*v^T:\n" << u*v.transpose() << std::endl;std::cout << "Let's multiply mat by itself" << std::endl;mat = mat*mat;std::cout << "Now mat is mat:\n" << mat << std::endl;
}

执行

$ g++   matrix_cal4.cpp -o matrix_cal4 -I /usr/local/include/eigen3
$
$ ./matrix_cal4
Here is mat*mat:7 10
15 22
Here is mat*u:
1
1
Here is u^T*mat:
2 2
Here is u^T*v:
-2
Here is u*v^T:
-2 -02  0
Let's multiply mat by itself
Now mat is mat:7 10
15 22

aliasing issues与运算模板

矩阵的点乘与叉乘

使用matrix类的dot(), cross()函数,来执行矩阵的点乘和叉乘。

点乘的结果可以看出是1X1的矩阵,u * v 还可以使用u.adjoint()*v进行计算.

示例:

#include <iostream>
#include <Eigen/Dense>using namespace Eigen;
using namespace std;int main()
{Vector3d v(1,2,3);Vector3d w(0,1,2);cout << "Dot product: " << v.dot(w) << endl;double dp = v.adjoint() * w; // automatic conversion of the inner product to a scalarcout << "Dot product via a matrix product: " << dp << endl;cout << "Cross product:\n" << v.cross(w) << endl;
}

执行输出:

Dot product: 8
Dot product via a matrix product: 8
Cross product:
1
-2
1

基础算数操作–arithmetic reduction

对矩阵matrix或向量vector,提供了一些算数分解操作,比如获取矩阵的系数之和,最大值,最小值,平均值、及对角线的相关算数。

  • sum() : 系数之和
  • prod() : 系数乘积
  • maxCoeff() : 最大系数
  • minCoeff() : 最小系数
  • trace() : 对角线系数和。
#include <iostream>
#include <Eigen/Dense>
using namespace std;
int main()
{Eigen::Matrix2d mat;mat << 1, 2,3, 4;cout << "Here is mat.sum():       " << mat.sum()       << endl;   // 10cout << "Here is mat.prod():      " << mat.prod()      << endl;   // 24cout << "Here is mat.mean():      " << mat.mean()      << endl;   // 2.5cout << "Here is mat.minCoeff():  " << mat.minCoeff()  << endl;   // 1cout << "Here is mat.maxCoeff():  " << mat.maxCoeff()  << endl;   // 4 cout << "Here is mat.trace():     " << mat.trace()     << endl;   // 5
}

注意这些函数的重载,还可以同时获取该系数的位置。

// matrix_cal5.cpp
#include <iostream>
#include <Eigen/Dense>using namespace Eigen;
using namespace std;int main()
{Matrix3f m = Matrix3f::Random();std::ptrdiff_t i, j;float minOfM = m.minCoeff(&i,&j);cout << "Here is the matrix m:\n" << m << endl;cout << "Its minimum coefficient (" << minOfM << ") is at position (" << i << "," << j << ")\n\n";RowVector4i v = RowVector4i::Random();int maxOfV = v.maxCoeff(&i);cout << "Here is the vector v: " << v << endl;cout << "Its maximum coefficient (" << maxOfV << ") is at position " << i << endl;}

执行结果如下:

$ g++   matrix_cal5.cpp -o matrix_cal5 -I /usr/local/include/eigen3
$ ./matrix_cal5
Here is the matrix m:-0.999984 -0.0826997  -0.905911-0.736924  0.0655345   0.3577290.511211  -0.562082   0.358593
Its minimum coefficient (-0.999984) is at position (0,0)Here is the vector v:  933495885 -250177384   41696341  710742668
Its maximum coefficient (933495885) is at position 0

Eigen入门之密集矩阵 2-- Matrix及Vector的计算方法相关推荐

  1. Eigen入门之密集矩阵 7 - Map class:连接Eigen与C++的数据

    简介 本文介绍一下Dense Matrix如何与c/C++的数组进行交互操作,这在引入其他的库中的vector向量和矩阵到Eigen中时要使用到的技术. 有时,你有一些定义好的数据,可能是数组,你需要 ...

  2. Eigen入门之密集矩阵 1 -- 类Matrix介绍

    简介 本篇介绍Eigen中的Matrix类.在Eigen中,矩阵和向量的类型都用Matrix来表示.向量是一种特殊的矩阵,其只有一行或者一列. Matrix构造 在Matrix.h中,定义了Matri ...

  3. Eigen入门之密集矩阵 9 - 别名混乱Aliasing

    简介 别名混乱Aliasing是指在赋值表达式中,一个Eigen对象(矩阵.数组.向量)同时出现在左值和右值表达式中,比如v = v*2; m = m.transpose();; 别名混乱会引起错误, ...

  4. Eigen入门之密集矩阵 6 - Reductions, visitors and broadcasting

    简介 本文介绍一下Dense Matrix的3中操作: reduction, visitor, broadcasting. 归约计算reduction. Eigen的归约计算是这样的一类计算,它是对矩 ...

  5. Eigen入门之密集矩阵 4 - 块操作

    简介 Eigen 中Matrix/Array提供了.block()来进行block区块操作,这是面向系数提供的操作功能. 语法 Eigen中提供了2种语法,针对产生的结果是一致的.但存在性能上的不同, ...

  6. Eigen入门之密集矩阵 3 - Array操作

    简介 在Eigen内,有Matrix,vector进行线性代数的相关运算,但也需要执行对矩阵内的系数的相关操作时,这是正常的功能需求.Eigen中的Array类就是满足此需求的. Array 定义 和 ...

  7. Eigen入门之密集矩阵 5 - 再谈Matrix初始化

    简介 这里将讨论一下高级些的矩阵初始化方法. comma-initializer 逗号初始化器 comma-initializer方法很简单,可以一下把矩阵/向量的系数全部设置完.语法很简单,使用逗号 ...

  8. Eigen入门之密集矩阵 10 - 矩阵的行优先及列优先存储

    简介 本篇介绍Eigen中矩阵及二维数组的系数存储顺序–行优先及列优先,已经如何指定优先顺序. 行优先(row-majoe). 列优先(column-majoe) 矩阵的系数条目组成了一个二维的结构, ...

  9. Eigen入门之密集矩阵 8 - resharp slicing切片

    简介 Eigen还没有提供resharp或者slicing的处理函数,但是,可以使用Map 类来实现这些功能. 实现resharp 操作Resharp及修改Matrix的维度大小,而其系数保持不变.R ...

最新文章

  1. 求求你了,配个GC日志呗,不然咋分析故障原因
  2. C#学习小记12实现一个接口
  3. python pipeline框架_Python爬虫从入门到放弃(十六)之 Scrapy框架中Item Pipeline用法...
  4. 安装SQL SERVER 2000时提示:以前的某个程序安装已在安装计算机上创建挂起的文件操作。...
  5. python的flask找不到网页_掌握这几个网页制作小技巧,让你快速提高你的网站转化率...
  6. Codevs 1010 过河卒 2002年NOIP全国联赛普及组
  7. SAP S4CRM 1811 服务订单API介绍
  8. 新版本ISR 为啥 移除replica.lag.max.messages这个参数
  9. .Net Mvc Automated Migration 数据迁移
  10. 使人无条件相信你的5个策略
  11. 阿里巴巴开源项目汇总-(JAVA)
  12. k8s集群中 spark访问hbase中数据
  13. Sentiment Analysis情感分析——珍藏版
  14. as3 socket 跨域连接
  15. Centos7配置 SNMP服务(防火墙配置注意)
  16. 国内首个!阿里云发布容器ATTCK攻防矩阵 | 凌云时刻
  17. linux c 读写 ini 配置文件
  18. CentOS 7.6 安装 Sentaurus 2017.09 记录
  19. 模拟信号和数字信号的区别
  20. 初谈证券交易系统开发核心

热门文章

  1. MVC Scaffolding SmartCode-Engine 更新
  2. 键盘enter按钮出发登陆事件
  3. 【安全牛学习笔记】思路、身份认证方法、密码破解方法、字典
  4. 27.思科防火墙(ASA)
  5. iOS随机页面NSClassFromString
  6. json格式数据,将数据库中查询的结果转换为json, 然后调用接口的方式返回json(方式一)...
  7. Infobright 数据仓库心得总结
  8. ffmpeg文档2:输出到屏幕
  9. 酷冷至尊官方psu计算工具_一款精致的电源,轻松应付高端配置、酷冷至尊MWE750金牌全模组电源 体验...
  10. linux tar.gz指定目录,tar.gz包内提取某个文件在指定目录下。