初始化
Matrix<float,2,3> mat_23;
//数据类型float,2行,3列
Vector3d v_3d;Matrix<float, 3, 1> vd_3d;
// v_3d,vd_3d是一样的
Matrix3d matrix_33 = Matrix3d::Zero(); //初始化为零
// Matrix3d 实质上是 Eigen::Matrix<double, 3, 3>
Matrix<float,2,3> mat_23;
matrix_23 << 1, 2, 3, 4, 5, 6;cout<<mat_23<<endl;
//长度必须一致,多少都会报错
Matrix3d matrix_33 = Matrix3d::Zero(); //初始化为零
matrix_33 = Matrix3d::Random();
MatrixXd matrix_x(matrix_33);//通过其他矩阵赋值
//通过
输出:
1 2 3
4 5 6
访问元素
cout<<mat_23(1,1)<<endl;
//(行,列)
乘法
数组乘矩阵
mat_23<<1,2,3,4,5,6;
v_3d<<3,2,1;
vd_3d<<4,5,6;
Matrix<float, 2, 1> result2 = mat_23 * vd_3d;
Matrix<double, 2, 1> result=mat_23.cast<int>()*v_3d;
//不同类型的矩阵,不能直接相乘,需要类型转换
cout<<result<<endl;
cout<<result2<<endl;
结果
1 2 3
4 5 6
1 2 3
4 5 6
其他
matrix_33 = Matrix3d::Random(); // 随机数矩阵
cout << "random matrix: \n" << matrix_33 << endl;
cout << "transpose: \n" << matrix_33.transpose() << endl; // 转置
cout << "sum: " << matrix_33.sum() << endl; // 矩阵所有元素和
cout << "trace: " << matrix_33.trace() << endl; // 迹:对角元素和
cout << "times 10: \n" << 10 * matrix_33 << endl; // 数乘
cout << "inverse: \n" << matrix_33.inverse() << endl; // 逆
cout << "det: " << matrix_33.determinant() << endl; // 转为行列式
cout<<""<<matrix_33.prod()<<endl;//平方和
cout<<""<<matrix_33.minCoeff()<<endl;//元素最小值
cout<<""<<matrix_33.maxCoeff()<<endl;//元素最大值
cout<<""<<matrix_33.norm()<<endl;//矩阵模值
特征值与特征向量
SelfAdjointEigenSolver<Matrix3d> eigen_solver(matrix_33.transpose() * matrix_33);
//构造一个实对称矩阵,SelfAdjointEigenSolver模板类,专门计算特征值和特征向量
cout << "Eigen values = \n" << eigen_solver.eigenvalues() << endl;//特征值
cout << "Eigen vectors = \n" << eigen_solver.eigenvectors() << endl;//特征向量
解方程
直接求逆:时间长
#define Max_size 50
Matrix<double, MATRIX_SIZE, MATRIX_SIZE> matrix_NN
= MatrixXd::Random(MATRIX_SIZE, MATRIX_SIZE);
matrix_NN = matrix_NN * matrix_NN.transpose(); // 保证半正定
Matrix<double, MATRIX_SIZE, 1> v_Nd = MatrixXd::Random(MATRIX_SIZE, 1);
Matrix<double, Max_size, 1> x = matrix_NN.inverse() * v_Nd;
cout << "time of normal inverse is "
<< 1000 * (clock() - time_stt) / (double) CLOCKS_PER_SEC << "ms" << endl;
cout << "x = " << x.transpose() << endl;
输出
time of normal inverse is 137.61ms
x = -55.7896 -298.793 130.113 -388.455 -159.312 160.654 -40.0416 -193.561 155.844 181.144 185.125 -62.7786 19.8333 -30.8772 -200.746 55.8385 -206.604 26.3559 -14.6789 122.719 -221.449 26.233 -318.95 -78.6931 50.1446 87.1986 -194.922 132.319 -171.78 -4.19736 11.876 -171.779 48.3047 84.1812 -104.958 -47.2103 -57.4502 -48.9477 -19.4237 28.9419 111.421 92.1237 -288.248 -23.3478 -275.22 -292.062 -92.698 5.96847 -93.6244 109.734
time_stt = clock();
x = matrix_NN.colPivHouseholderQr().solve(v_Nd);
cout << "time of Qr decomposition is "
<< 1000 * (clock() - time_stt) / (double) CLOCKS_PER_SEC << "ms" << endl;
cout << "x = " << x.transpose() << endl
输出
time of Qr decomposition is 6.3ms
x = -55.7896 -298.793 130.113 -388.455 -159.312 160.654 -40.0416 -193.561 155.844 181.144 185.125 -62.7786 19.8333 -30.8772 -200.746 55.8385 -206.604 26.3559 -14.6789 122.719 -221.449 26.233 -318.95 -78.6931 50.1446 87.1986 -194.922 132.319 -171.78 -4.19736 11.876 -171.779 48.3047 84.1812 -104.958 -47.2103 -57.4502 -48.9477 -19.4237 28.9419 111.421 92.1237 -288.248 -23.3478 -275.22 -292.062 -92.698 5.96847 -93.6244 109.734
time_stt = clock();
x = matrix_NN.ldlt().solve(v_Nd);
cout << "time of ldlt decomposition is "<< 1000 * (clock() - time_stt) / (double) CLOCKS_PER_SEC << "ms" << endl;
cout << "x = " << x.transpose() << endl;
time of ldlt decomposition is 1.673ms
x = -55.7896 -298.793 130.113 -388.455 -159.312 160.654 -40.0416 -193.561 155.844 181.144 185.125 -62.7786 19.8333 -30.8772 -200.746 55.8385 -206.604 26.3559 -14.6789 122.719 -221.449 26.233 -318.95 -78.6931 50.1446 87.1986 -194.922 132.319 -171.78 -4.19736 11.876 -171.779 48.3047 84.1812 -104.958 -47.2103 -57.4502 -48.9477 -19.4237 28.9419 111.421 92.1237 -288.248 -23.3478 -275.22 -292.062 -92.698 5.96847 -93.6244 109.734