1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
| #include "iostream" #include <ctime>
#include <Eigen/Dense> #include <Eigen/Core>
#define MATRTX_SIZE 50
using namespace std;
int main(int argc, char **argv) { Eigen::Matrix<float, 2, 3> matrix_23; Eigen::Vector3d v_3d; Eigen::Matrix3d matrix_33 = Eigen::Matrix3d::Zero();
Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic> matrix_dynamic; Eigen::MatrixXd matrix_x;
matrix_23 << 1, 2, 3, 4, 5, 6; cout << matrix_23 << endl; cout << typeid(matrix_23).name() << endl; for (int i = 0; i < 1; i++) for (int j = 0; j < 2; j++) cout << matrix_23(i, j) << endl; v_3d << 3, 2, 1; Eigen::Matrix<double, 2, 1> result = matrix_23.cast<double>() * v_3d;
matrix_33 = Eigen::Matrix3d::Random(); cout << matrix_33 << endl << endl; cout << matrix_33.transpose() << endl; cout << matrix_33.trace() << endl; cout << 10 * matrix_33 << endl; cout << matrix_33.inverse() << endl; cout << matrix_33.determinant() << endl;
Eigen::SelfAdjointEigenSolver<Eigen::Matrix3d> eigen_solver(matrix_33.transpose() * matrix_33); cout << "Eigen values = " << eigen_solver.eigenvalues() << endl; cout << "Eigen vectors = " << eigen_solver.eigenvectors() << endl;
Eigen::Matrix<double, MATRTX_SIZE, MATRTX_SIZE> matrix_NN; matrix_NN = Eigen::MatrixXd::Random(MATRTX_SIZE, MATRTX_SIZE); Eigen::Matrix<double, MATRTX_SIZE, 1> v_Nd; v_Nd = Eigen::MatrixXd::Random(MATRTX_SIZE, 1); clock_t time_start = clock(); Eigen::Matrix<double, MATRTX_SIZE, 1> x = matrix_NN.inverse() * v_Nd; cout << "time use in normal inverse is " << 1000 * (clock() - time_start)/(double) CLOCKS_PER_SEC << "ms" << endl;
time_start = clock(); x = matrix_NN.colPivHouseholderQr().solve(v_Nd); cout << "time use in normal inverse is " << 1000 * (clock() - time_start)/(double) CLOCKS_PER_SEC << "ms" << endl;
return 0; }
|