Eigen教程(二)-Matrix And Vector Arithmetic


1.Addition and subtraction

如果是二元运算符,则左右两边矩阵需要有同样的行和列,因为Eigen不会进行类型转换,所以两个矩阵的数据类型(Scalar)也必须一样,这些操作符包括:

  • 二元操作符+、-,如a+b、a-b
  • 一元操作符-,如-b
  • 组合操作符+=、-=,如a+=b、a-=b
#include <isotream>
#include <Eigen/Dense>

using namespace std;
using namespace Eigen;

int main()
{
	Matrix2d a;
	a << 1, 2, 3, 4;
	MatrixXd b(2, 2);
	b << 2, 3, 1, 4;
	cout << "a + b =\n" << a + b << endl;
	cout << "a - b = \n" << a -b << endl;
	cout << "Doing a += b" << endl;
	a += b;
	cout << "Now a =\n" << a << endl;
	Vector3d v(1, 2, 3);
	Vector3d w(1, 0, 0);
	cout << "-v + w - v =\n" << -v + w - v << endl;
	
	return 0;
}

输出结果如下:

a + b =
3 5
4 8
a - b = 
-1 -1
 2  0
Doing a += b
Now a =
3 5
4 8
-v + w - v =
-1
-4
-6

2.Scalar multiplication and division

  • 二元操作符*,如Matrix * Scalar、Scalar * Matrix
  • 二元操作符/,如Matrix / Scalar
  • 组合操作符*=、/=,如Matrix *= Scalar、Matrix /= Scalar
#include <iostream>
#include <Eigen/Dense>
 
using namespace Eigen;
 
int main()
{
  Matrix2d a;
  a << 1, 2,
       3, 4;
  Vector3d v(1,2,3);
  std::cout << "a * 2.5 =\n" << a * 2.5 << std::endl;
  std::cout << "0.1 * v =\n" << 0.1 * v << std::endl;
  std::cout << "Doing v *= 2;" << std::endl;
  v *= 2;
  std::cout << "Now v =\n" << v << std::endl;
}

输出如下:

a * 2.5 =
2.5   5
7.5  10
0.1 * v =
0.1
0.2
0.3
Doing v *= 2;
Now v =
2
4
6

3.Transposition and conjugation

三个函数:

  • transpose():转置
  • conjugate():共轭
  • adjoint():共轭转置

对于实值矩阵而言,transpose()adjoint()等价:

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;

输出结果如下:

Here is the matrix a
    (-1,-0.737) (0.0655,-0.562)
(0.511,-0.0827)  (-0.906,0.358)
Here is the matrix a^T
    (-1,-0.737) (0.511,-0.0827)
(0.0655,-0.562)  (-0.906,0.358)
Here is the conjugate of a
     (-1,0.737)  (0.0655,0.562)
 (0.511,0.0827) (-0.906,-0.358)
Here is the matrix a^*
     (-1,0.737)  (0.511,0.0827)
 (0.0655,0.562) (-0.906,-0.358)
  • 需要注意一点,transpose()不支持inplace的转置,因此像a = a.transpose()这样的语句是不能够完成转置操作的。
  • 但是Eigen提供了transposeInPlcae()函数,该函数支持inplace操作,因此a = a.transposeInPlace()这样的操作是成立的。

4.Matrix-matrix and matrix-vector multiplication

上面介绍了标量与矩阵的乘除,事实上,矩阵乘法同样用到了*、*=,直接上例子:

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

输出如下:

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 -0
 2  0
Let's multiply mat by itself
Now mat is mat:
 7 10
15 22

5.Dot product and cross product

两个函数:

  • dot():点乘
  • cross():叉乘

注意叉乘只适用于3维向量,点乘适用于任何维数的向量

直接看例子:

#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 scalar
  cout << "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

6.Basic arithmetic reduction operations

  • sum():矩阵所有元素之和
  • prod():矩阵所有元素之积
  • mean():矩阵所有元素平均值,相当于A.sum() / (A.rows() * A.cols())
  • minCoeff()、maxCoeff():矩阵元素的最小值、最大值
  • trace():矩阵的迹(对角线之和)相当于A.diagonal().sum()
#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;
  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;
}

输出如下:

Here is mat.sum():       10
Here is mat.prod():      24
Here is mat.mean():      2.5
Here is mat.minCoeff():  1
Here is mat.maxCoeff():  4
Here is mat.trace():     5

minCoeff()、maxCoeff()也有参数,能够返回对应值的坐标位置:

  Matrix3f m = Matrix3f::Random();
  std::ptrdiff_t i, j;
  float minOfM = m.minCoeff(&i,&j);	// 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);	// i就是对应的元素位置
  cout << "Here is the vector v: " << v << endl;
  cout << "Its maximum coefficient (" << maxOfV 
       << ") is at position " << i << endl;

输出如下:

Here is the matrix m:
     -1 -0.0827  -0.906
 -0.737  0.0655   0.358
  0.511  -0.562   0.359
Its minimum coefficient (-1) is at position (0,0)

Here is the vector v:  9 -2  0  7
Its maximum coefficient (9) is at position 0
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值