2、Matrix and vector arithmetic

Matrix and vector arithmetic

Eigen通过常见的C ++算术运算符(例如+,-,*)的重载,或通过诸如dot(),cross()等特殊方法的方式提供矩阵/向量算术运算。对于Matrix类(矩阵和向量),运算符 仅重载以支持线性代数运算。 例如,matrix1 * matrix2表示矩阵-矩阵乘积,而向量+标量是不允许的。 如果要执行各种数组运算而不是线性代数,请参见下一页。

  • binary operator + as in a+b
  • binary operator - as in a-b
  • unary operator - as in -a
  • compound operator += as in a+=b
  • compound operator -= as in a-=b

例如

#include <iostream>
#include <Eigen/Dense>
 
using namespace Eigen;
 
int main()
{
  Matrix2d a;
  a << 1, 2,
       3, 4;
  MatrixXd b(2,2);
  b << 2, 3,
       1, 4;
  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;
  Vector3d v(1,2,3);
  Vector3d w(1,0,0);
  std::cout << "-v + w - v =\n" << -v + w - v << std::endl;
}

Output:

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

  • binary operator * as in matrix*scalar
  • binary operator * as in scalar*matrix
  • binary operator / as in matrix/scalar
  • compound operator = as in matrix=scalar
  • compound operator /= as in 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;
}

Output:
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

Note:

VectorXf a(50), b(50), c(50), d(50);
...
a = 3*b + 4*c + 5*d;

Eigen compiles it to just one for loop, so that the arrays are traversed only once. Simplifying (e.g. ignoring SIMD optimizations), this loop looks like this:

for(int i = 0; i < 50; ++i)
  a[i] = 3*b[i] + 4*c[i] + 5*d[i];

Transposition and conjugation

The transpose aT, conjugate a¯, and adjoint (i.e., conjugate transpose) a∗ of a matrix or vector a are obtained by the member functions transpose(), conjugate(), and adjoint(), respectively.

Example:

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;
 

Output:
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)

Matrix2i a; a << 1, 2, 3, 4;
cout << "Here is the matrix a:\n" << a << endl;
 
a = a.transpose(); // !!! do NOT do this !!!
cout << "and the result of the aliasing effect:\n" << a << endl;

这样做不对,替换成下面代码:

MatrixXf a(2,3); a << 1, 2, 3, 4, 5, 6;
cout << "Here is the initial matrix a:\n" << a << endl;

a.transposeInPlace();
cout << "and after being transposed:\n" << a << endl;

There is also the adjointInPlace() function for complex matrices.

Matrix-matrix and matrix-vector multiplication

  • binary operator * as in a*b
  • compound operator = as in a=b (this multiplies on the right: a*=b
    is equivalent to a = a*b)
#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;
}

Output:
Here is matmat:
7 10
15 22
Here is mat
u:
1
1
Here is u^Tmat:
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


Note:
Eigen treats matrix multiplication as a special case and takes care of introducing a temporary here, so it will compile m=m*m as:

tmp = m*m;
m = tmp;

如果您知道您的矩阵可以安全地评估到目标矩阵中而不会出现混叠问题,那么您可以使用noalias()函数来避免出现临时情况,例如:

c.noalias()+ = a * b;

注意:
对于担心性能的BLAS用户,可以使用c.noalias()-= 2 * a.adjoint()* b等表达式; 经过全面优化,并触发了一个类似于gemm的函数调用。

Dot product and cross product

Example:

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

Output:

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

记住:
叉积仅适用于大小为3的向量。点积适用于任何大小的向量。

基本算术归约运算

Eigen还提供了一些归约运算,以将给定的矩阵或向量归约为单个值,例如总和(由sum()计算),乘积(prod())或最大值(maxCoeff())和最小值(minCoeff() )的所有系数。

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

Output:
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

找最大最小:

  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;

Output:
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

小结:

Matrix and vector arithmetic:
a + b
a - b
-a
a += b
a -= b
matrix * scalar
scalar * matrix
matrix / scalar
matrix *= scalar
matrix /= scalar

转置:a.transpose()
共轭:a.conjugate()
伴随:a.adjoint()

Note:
对于基本的算术运算符,transpose()和adjoint()只是返回一个代理对象,而不进行实际的转置。 如果执行b = a.transpose(),则在将结果写入b的同时评估转置。 但是,这里有一个复杂之处。 如果执行a = a.transpose(),则Eigen将在转置运算完成之前开始将结果写入a中。 因此,指令a = a.transpose()不会用其转置代替a。
as for instance in a = a.transpose(), simply use the transposeInPlace() function:

Vector3d v(1,2,3);
Vector3d w(0,1,2);

点乘:v.dot(w)
叉乘:v.cross(w)

矩阵所有系数的
总和:mat.sum()
乘积:mat.prod()
均值:mat.mean()
最小值:mat.minCoeff()
最小值:mat.maxCoeff()
迹:mat.trace()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值