Eigen幫助文檔的地址:http://eigen.tuxfamily.org/dox/pages.html
Eigen的論壇:http://forum.kde.org/viewforum.php?f=74
1.一些基本運算
#include <iostream>
using namespace std;
#include <ctime>
//核心部分
#include <Eigen/Core>
//稠密矩陣的運算
#include <Eigen/Dense>
using namespace Eigen;
#define MATRIX_SIZE 50
int main() {
//eigen中的所有向量和矩陣都是Eigen::Matrix,三個參數為數據類型,行,列
//聲明一個2*3的float矩陣
Matrix<float,2,3> matrix_23;
Matrix3d m1; //旋轉矩陣3*3 雙精度,也可改為f
AngleAxisd m2; //旋轉向量 3*1
Vector3d m3; //歐拉角 3*1
Quaterniond m4; //四元數 4*1
Isometry3d m5; //歐氏變換矩陣 4*4
Affine3d m6; //仿射變換4*4
Projective3d m7; //射影變換4*4
//Vector3d 本質上還是Eigen::Matrix<double,3,1>即三維向量
Vector3d v_3d;
Matrix<float,3,1> vd_3d; //類似
//本質上是Eigen::Matrix<double,3,3>
Matrix3d matrix_33 = Matrix3d::Zero(); //初始化為0
//不確定矩陣大小,使用動態矩陣
Matrix<double, Dynamic,Dynamic> matrix_dynamic;
//更簡單的:
MatrixXd matrix_x;
matrix_23 << 1,2,3,4,5,6;
cout << matrix_23 << endl;
v_3d << 3,2,1;
vd_3d << 4,5,6;
//乘法,不同類型需要顯性的轉換
Matrix<double,2,1> result = matrix_23.cast<double>() * v_3d;
cout << "[1,2,3;4,5,6]*[3,2,1]=\n" << result << endl;
/*******矩陣運算*********/
matrix_33 = Matrix3d::Random();
cout << "random matrix33:\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" << matrix_33 * 10 << endl; //數乘
cout << "inverse:\n" << matrix_33.inverse() << endl; //逆
cout << "det:" << matrix_33.determinant() << endl; //行列式
/***********************/
//特征值
//實對稱矩陣可以保證對角化成功
SelfAdjointEigenSolver<Matrix3d> eigen_solver(matrix_33.transpose() * matrix_33);
cout << "eigen values = \n" << eigen_solver.eigenvalues() << endl;
cout << "Eigen vectors = \n" << eigen_solver.eigenvectors() << endl;
//解方程
//求解matrix_nn * x = v_Nd這個方程
//直接求逆最直接,但是運算較大
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);
clock_t time_str = clock();
//直接求逆
Matrix<double,MATRIX_SIZE,1> x = matrix_NN.inverse() * v_Nd;
cout << "time is:" << 1000*(clock() - time_str) / (double) CLOCKS_PER_SEC << "ms" << endl;
cout << "x=" << x.transpose() << endl;
//QR分解,速度快很多
time_str = clock();
x = matrix_NN.colPivHouseholderQr().solve(v_Nd);
cout << "time is:" << 1000*(clock() - time_str) / (double) CLOCKS_PER_SEC << "ms" << endl;
cout << "x=" << x.transpose() << endl;
//對於正定矩陣,還可以用cholesky分解來解方程
time_str = clock();
x = matrix_NN.ldlt().solve(v_Nd);
cout << "time is:" << 1000*(clock() - time_str) / (double) CLOCKS_PER_SEC << "ms" << endl;
cout << "x=" << x.transpose() << endl;
return 0;
}
CMakeLists.txt
cmake_minimum_required(VERSION 2.8.3)
project (main)
set(CMAKE_BUILD_TYPE "Release")
set(CMAKE_CXX_FLAGS "-O3")
add_definitions(-std=c++11)
include_directories(inc)
aux_source_directory(src DIR_SRCS)
SET(SOUR_FILE ${DIR_SRCS})
include_directories("/usr/include/eigen3")
add_executable(main ${SOUR_FILE})