C++矩阵运算库Eigen使用1

官网下载:http://eigen.tuxfamily.org/index.php?title=Main_Page

1. 主要头文件:

#include 包含Matrix和Array类,基础的线性代数运算和数组操作
#include SVD分解
#include 全部包含
使用时using namespace Eigen

2. Matix类:

前三个参数:Scalar,RowsAtCompileTime,ColsAtCompileTime
分别为元素类型、行、列
已默认设置矩阵说明:
Maxtrix2cf与Matrix3d
别名定义:
typedf MaxtrixMatrix2cf
typedf Maxtrix< double,3,3>Matrix3d

其中complex表示复数
Xcf或Xcd表示动态矩阵
Matrixcol3f行向量
Matrixrow3d列向量
MatrixmatrixXd;动态矩阵
随机自定义:MatrixXcf a = MatrixXcf::Random(3,6)
矩阵大小可通过rows()、cols()、size()获取,通过resize()重新调整大小。

3. 矩阵运算:

不支持类型自动转化,因此矩阵元素类型必须相同
+、-、+=、-=、、/、=、/=等四则运算

4. 矩阵元素赋值与访问:

Matrix3f m;
m<<1,2,3
4,5,6
7,8,9
可以使用=赋值
MatrixXf n;
n=m;
访问:m(0,0)~m(2,2)
代码:

#include
#include
using namespace std;
using namespace Eigen;
int main() {
	Matrix2d a;
	a << 1.0,2.0,
		3.0, 4.0;
	MatrixXcf b = MatrixXcf::Random(3, 6);

	cout << "matrix2d a =" << endl << a << endl;
	cout << "matrixXcf::ramdom(3,6)=" << endl << b << endl;
	cout << "the size of b is " << endl << b.size() << endl;
	cout << "the cols of b is" << endl << b.cols() << endl;
	cout << "the rows of b is" << endl << b.rows() << endl;
	b.resize(4, 5);
	cout << "resize b to (4,5)" << endl << b << endl;
	cout << "a(0,1)=" << a(0, 1) << endl;
	return 0;
}

结果图:
矩阵尺寸
随机生成矩阵
矩阵访问
<<输入矩阵参数

你可能感兴趣的:(C++)