c++ Eigen矩阵运算

算法中总会存在大量的矩阵运算,eigen提供便捷又强大的矩阵运算库,将理论可在C++中得以实现。

一下内容为基于官方教程进行的总结,官方教程地址:

http://eigen.tuxfamily.org/dox/GettingStarted.html

一、定义与赋值

在这里说明一下在VS中eigen库的安装方式,我的版本是VS2015

如图,点击项目->管理NuGet程序包,在 浏览 中搜索 eigen,安装即可

1.1、矩阵的定义及赋值

#include <iostream>
#include <Eigen/Dense>
 
using Eigen::MatrixXd;
 
int main()
{
  MatrixXd m(2,2);
  m(0,0) = 3;
  m(1,0) = 2.5;
  m(0,1) = -1;
  m(1,1) = m(1,0) + m(0,1);
  std::cout << m << std::endl;
  VectorXd v(3); //Vector3d v;
  v << 1, 2, 3;
  cout << "m * v =" << endl << m * v << endl;
}

其中,MatrixXd为定义一个矩阵。对于方阵也可使用Matrixnd定义,其中n为阶数,如定义三阶矩阵可通过:

Matrix3d m;

m(0,0)=3等为对定义矩阵各元素赋值,也可通过以下方式赋值:

m<< 3, -1,
2.5, 1.5;

对于向量同样可使用VectorXd(列向量)/RowVectorXd(行向量)或Vectornd/RowVectornd来定义,赋值方式与矩阵赋值相同。

#include <iostream>
#include <Eigen/Dense>
 
using namespace Eigen;
 
int main()
{
  VectorXd v(2);
  v(0) = 4;
  v(1) = v(0) - 1;
  std::cout << "Here is the vector v:\n" << v << std::endl;
}

结果为:

Here is the vector v:
4
3

定义中末尾的d指double类型,也可改为i(int)、f(float)。

这里推荐使用Matrixnd和Vectornd定义,使用该定义则固定了矩阵/向量大小,编译器可产生更好更快的代码;同时指定大小,编译时容许更严格的检查,如4阶矩阵与三维向量相乘报错。

1.2、对定义矩阵进行随机赋值

#include <iostream>
#include <Eigen/Dense>
 
using namespace Eigen;
using namespace std;
 
int main()
{
  MatrixXd m = MatrixXd::Random(3,3);       //Matrix3d m = Matrix3d::Random();
  m = (m + MatrixXd::Constant(3,3,1.2)) * 50; //m = (m + Matrix3d::Constant(1.2)) * 50;
  cout << "m =" << endl << m << endl;
  VectorXd v(3);
  v << 1, 2, 3;
  cout << "m * v =" << endl << m * v << endl;
}

可使用Random()方法进行随机赋值,该方法产生的随机值范围在-1至1之间。

1.3、矩阵大小、调整及分配大小

(1)获取行数、列数、元素个数及重新分配大小:

可通过rows()cols() and size()方法获得矩阵的行数、列数及元素个数。可通过resize()重新调整矩阵大小:

#include <iostream>
#include <Eigen/Dense>
 
using namespace Eigen;
 
int main()
{
  MatrixXd m(2,5);
  m.resize(4,3);
  std::cout << "The matrix m is of size "
            << m.rows() << "x" << m.cols() << std::endl;
  std::cout << "It has " << m.size() << " coefficients" << std::endl;
  VectorXd v(2);
  v.resize(5);
  std::cout << "The vector v is of size " << v.size() << std::endl;
  std::cout << "As a matrix, v is of size "
            << v.rows() << "x" << v.cols() << std::endl;
}

输出结果:

The matrix m is of size 4x3
It has 12 coefficients
The vector v is of size 5
As a matrix, v is of size 5x1

(2)分配

可通过分配将右边的矩阵复制到左边矩阵

MatrixXf a(2,2);
std::cout << "a is of size " << a.rows() << "x" << a.cols() << std::endl;
MatrixXf b(3,3);
a = b;
std::cout << "a is now of size " << a.rows() << "x" << a.cols() << std::endl;

输出结果:

a is of size 2x2
a is now of size 3x3

二、矩阵向量运算

2.1、矩阵的加减

加减前提,当然是保证左右两边的矩阵有着相同的行和列,也必须是相同的类型。

加减操作分别有:

二元运算:a+b、a-b

一元运算:-a

复合运算:a+=b、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;
}

运算结果:

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.2、矩阵的数乘和数除

二元运算:矩阵*标量、标量*矩阵、矩阵/标量

复合运算:矩阵*=标量、矩阵/=标量

#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

 

 

 

 

 

已标记关键词 清除标记
©️2020 CSDN 皮肤主题: 编程工作室 设计师:CSDN官方博客 返回首页