看到“矩阵计算”这几个字,大家肯定首先想到的是大名鼎鼎的美国MathWorks公司开发的MATLAB矩阵实验室,MATLAB功能的确非常强大。但有时由于一些特殊需求,我们只能用C++/C来编程实现矩阵运算。下面通过一些简单的例子来了解Eigen
#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;
}
#include <Eigen/Dense>
Eigen/dense头文件定义了MatrixXd type 和相关类型的成员函数。
读完这个程序,能看出MatrixXd 只是声明了一个2x2的矩阵,并没有对其进行初始化。但为什么要用MatrixXd,X和d分别有什么含义呢?
X:代表一个任意大小的矩阵。d:表示输入元素的类型为double。如果想要输入浮点型元素,改为MatrixXf就可以了,同理,想输入整型元素,改为MatrixXi即可。
m(0,0) = 3;
这条语句将矩阵的第一个元素赋值为3,
注意这里在进行赋值时,元素索引从0开始,要与数学表达进行区分,第一行和第一列下标为(0,0)Eigen提供了两种表示dense objects,用矩阵类的模板函数来表示矩阵和向量。用类数组类的模板函数来表示一维和二维数组,原型如下所示:
typedef Matrix<Scalar, RowsAtCompileTime, ColsAtCompileTime, Options> MyMatrixType;
typedef Array<Scalar, RowsAtCompileTime, ColsAtCompileTime, Options> MyArrayType;
Scalar:数据类型(int,double,float);
RowsAtCompileTime:矩阵行数,也可以动态确定
ColsAtCompileTime:矩阵列数,也可以动态确定
Matrix<double,Dynamic,Dynamic> <=> MatrixXd
Matrix<float,Dynamic,Dynamic> <=> MatrixXf
Eigen也提供了很多新的类型定义:
Matrix2d is a 2x2 square matrix of doubles (Matrix<double, 2, 2>)
Vector4f is a vector of 4 floats (Matrix<float, 4, 1>)
RowVector3i is a row-vector of 3 ints (Matrix<int, 1, 3>)
MatrixXf is a dynamic-size matrix of floats (Matrix<float, Dynamic, Dynamic>)
VectorXf is a dynamic-size vector of floats (Matrix<float, Dynamic, 1>)
Matrix2Xf is a partially fixed-size (dynamic-size) matrix of floats (Matrix<float, 2, Dynamic>)
MatrixX3d is a partially dynamic-size (fixed-size) matrix of double (Matrix<double, Dynamic, 3>)
下面开始介绍矩阵的一些基本运算
一:矩阵相加
#include <iostream>
#include <stdlib.h>
#include <Eigen/Dense>
using namespace Eigen;
int main()
{
MatrixXd m(2, 2);
m(0, 0) = 3;
m(1, 0) = 2.5;
m(0, 1) = -1;
m(1, 1) = 5;
std::cout << "Here is the matrix m:\n" << m << std::endl;
MatrixXd n(2, 2);
n(0, 0) = 4;
n(1, 0) = 1.5;
n(0, 1) = 3;
n(1, 1) = -2.5;
std::cout << "Here is the matrix n:\n" << n << std::endl;
MatrixXd A(2, 2);
A = m + n;
std::cout << "Here is the matrix A:\n" << A << std::endl;
system("pause");
}
二矩阵相减
#include <iostream>
#include <stdlib.h>
#include <Eigen/Dense>
using namespace Eigen;
int main()
{
Matrix3d m;
m << 1, 2, 3,
4, 5, 6,
7, 8, 9;
std::cout << "Here is the matrix m:\n" << m << std::endl;
Matrix3d n;
n << 1.5, 3.4, 2.6,
4.2, 5.5, 6.2,
7.1, 8.5, 5.5;
std::cout << "Here is the matrix n:\n" << n << std::endl;
Matrix3d B;
B = n - m;
std::cout << "Here is the matrix B:\n" << B << std::endl;
system("pause");
}
运行结果如图所示
这段程序中使用了一种新的方法来对矩阵进行初始化,重载了“《”来输入矩阵中的元素。
三矩阵相乘
#include <iostream>
#include <stdlib.h>
#include <Eigen/Dense>
using namespace Eigen;
int main()
{
Matrix3d m;
m << 1, 1, 1,
1, 1, 1,
1, 1, 1;
std::cout << "Here is the matrix m:\n" << m << std::endl;
Matrix3d n;
n << 2, 2, 2,
2, 2, 2,
2, 2, 2;
std::cout << "Here is the matrix n:\n" << n << std::endl;
Matrix3d B;
B = n * m;
std::cout << "Here is the matrix B:\n" << B << std::endl;
system("pause");
}
运行结果如图所示
详细的教程请访问官网:
http://eigen.tuxfamily.org/dox/index.html