1.安装
内有安装Eigen的安装教程https://editor.csdn.net/md/?articleId=106730686
2.定义Eigen
#include <iostream>
#include <eigen3/Eigen/Dense>//引用Eigen库
using namespace Eigen;
using namespace std;
template <typename T> using Mat2 = Matrix<T, 2 , 2>;//创建一个矩阵定义方式1
int main(int argc, char *argv[]){
Mat2<int> mat1;
mat1 << 1,1,
1,1;//矩阵赋值
cout << mat1 << endl;//输出矩阵的值
Vector2d vec1;//创建一个矩阵定义方式2
vec1 << 1 ,2 ;
cout << vec1 << endl;
MatrixXd mat(2,2);//创建一个矩阵定义方式3
for (int i = 0; i < 2; ++i) {
for (int j = 0; j < 2; ++j) {
mat(i , j) = 1;
cout << mat(i,j) << " ";
}
cout << endl;
}
return 0;
}
3.全零阵,全一阵,单位阵和随机阵的定义方式
#include <iostream>
#include <eigen3/Eigen/Dense>//引用Eigen库
using namespace Eigen;
using namespace std;
int main(int argc, char *argv[]){
//全零阵,全一阵,单位阵和随机阵的定义方式
MatrixXd matrix1 = MatrixXd::Zero(2,2);
MatrixXd matrix2 = MatrixXd::Ones(2,2);
MatrixXd matrix3 = MatrixXd::Identity(2,2);
MatrixXd matrix4 = MatrixXd::Random(2,2);
cout << matrix1 << endl;
cout << matrix2 << endl;
cout << matrix3 << endl;
cout << matrix4 << endl;
return 0;
}
4.矩阵行列数赋值时的变化
#include <iostream>
#include <eigen3/Eigen/Dense>//引用Eigen库
using namespace Eigen;
using namespace std;
int main(int argc, char *argv[]){
//矩阵行列数赋值时的变化
MatrixXd a(2,2);
MatrixXd b(3,3);
//cout << a.rows() << " " << a.cols() << endl;
//a = b;
//cout << a.rows() << " " << a.cols() << endl;
cout << b.rows() << " " << b.cols() << endl;
b = a;
cout << b.rows() << " " << b.cols() << endl;
return 0;
}
5.矩阵的四则运算
#include <iostream>
#include <eigen3/Eigen/Dense>//引用Eigen库
using namespace Eigen;
using namespace std;
int main(int argc, char *argv[]){
//矩阵的四则运算
Matrix2d mat1;
mat1 << 5,6,
7,8;
Matrix2d mat2;
mat2 << 1,2,
3,4;
cout << mat1 + mat2 << endl;
cout << mat1 - mat2 << endl;
cout << mat1 * mat2 << endl;
cout << mat1 / 2 << endl;
cout << mat1.setZero() << endl;//置0
return 0;
}
6.特殊矩阵
#include <iostream>
#include <eigen3/Eigen/Dense>//引用Eigen库
using namespace Eigen;
using namespace std;
int main(int argc, char *argv[]){
//矩阵的特殊矩阵
Matrix2d mat1;
mat1 << 5,6,
7,8;
//mat1.transposeInPlace();
cout << "转置矩阵" << endl << mat1.transpose()<< endl;//转置矩阵
cout << "共轭矩阵" << endl << mat1.conjugate() << endl;//共轭矩阵
cout << "逆矩阵" << endl << mat1.inverse() << endl;//逆矩阵
cout << "伴随矩阵" << endl << mat1.adjoint() << endl;//伴随矩阵
return 0;
}
7.矩阵的快速取元素操作
7.1 矩阵的快速取元素操作1
#include <iostream>
#include <eigen3/Eigen/Dense>//引用Eigen库
using namespace Eigen;
using namespace std;
int main(int argc, char *argv[]){
//矩阵的快速取元素操作
ArrayXd vec(10);
vec << 1,2,3,4,5,6,7,8,9,0;
cout << vec <<endl;
cout << vec.head(3) << endl;//前三个
cout << vec.tail(4 ) << endl;//后几个
cout << vec.segment(3,8) << endl;//中间的几个
return 0;
}
7.2矩阵的快速取元素操作2
#include <iostream>
#include <eigen3/Eigen/Dense>//引用Eigen库
using namespace Eigen;
using namespace std;
int main(int argc, char *argv[]){
//矩阵的快速取元素操作
MatrixXd mat(4,4);
mat << 1,2,3,4,
5,6,7,8,
9,10,11,12,
13,14,15,16;
cout << mat.col(2) << endl;
cout << mat.row(2) << endl;
//取矩阵中的某块
cout << mat.block(1,1,2,2) << endl;//参考点行数,参考点列数,取行数的个数,取列数的个数
cout << mat.block<2,2>(1,1) << endl;
return 0;
}