szmlb.net

tips for robotics

CMakeでEigenを使う

CMakeLists.txtの記述

下記サイトに詳細あり.
How to use Eigen through CMake? • KDE Community Forums

$ mkdir ${PROJECT_SOURCE_DIR}/cmake
$ cp /usr/local/eigen3/cmake/FindEigen3.cmake ${PROJECT_SOURCE_DIR}/cmake

CMakeLists.txtを下記のように記述.

cmake_minimum_required (VERSION 2.8)

project( main )

set(CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake)
find_package( Eigen3 REQUIRED )

include_directories(EIGEN3_INCLUDE_DIR)
add_executable( main main.cpp )

Eigenはインクルードしているだけなので, 下記のように直接記述してもOK.

cmake_minimum_required (VERSION 2.8)

project( main )

include_directories(/usr/include/eigen3)
add_executable( main main.cpp )

main.cppは下記の通り.

// Eigen test program (LinearAlgebra)
//
// Tutorial pages are follows:
// http://eigen.tuxfamily.org/dox/TutorialLinearAlgebra.html
//

#include <iostream>
#include <Eigen/Dense>

using namespace std;
using namespace Eigen;

int main()
{

	Matrix3f A; 
	Vector3f b;
	A << 1,2,3,  4,5,6,  7,8,10;
	b << 3, 3, 4;
	cout << "Here is the matrix A:\n" << A << endl;
	cout << "Here is the vector b:\n" << b << endl;

	Vector3f x1 = A.partialPivLu().solve(b);       
	Vector3f x2 = A.fullPivLu().solve(b);            
	Vector3f x3 = A.householderQr().solve(b); 

	Vector3f x4 = A.colPivHouseholderQr().solve(b);  
	cout << "The solution (colPivHouseholderQr) is:\n" << x4 << endl;
	Vector3f x5 = A.fullPivHouseholderQr().solve(b); 
	Vector3f x6 = A.llt().solve(b);		
	Vector3f x7 = A.ldlt().solve(b);		
	cout << "The solution (partialPivLu) is:\n" << x1 << endl;
	cout << "The solution (fullPivLu) is:\n" << x2 << endl;
	cout << "The solution (householderQr) is:\n" << x3 << endl;
	cout << "The solution (fullPivHouseholderQr) is:\n" << x5 << endl;
	cout << "The solution (llt) is:\n" << x6 << endl;
	cout << "The solution (ldlt) is:\n" << x7 << endl;

}

プロジェクトディレクトリで下記の通り実行.

$cmake .
$make
$./main