Difference between revisions of "CPP/Eigen/LevenbergMarquardt"
From ProgrammingExamples
< CPP
Daviddoria (Talk | contribs) |
Daviddoria (Talk | contribs) |
||
Line 9: | Line 9: | ||
struct MyFunctor | struct MyFunctor | ||
{ | { | ||
− | + | int operator()(const Eigen::VectorXf &x, Eigen::VectorXf &fvec) const | |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | int operator()(const Eigen:: | + | |
{ | { | ||
// Implement y = x^2 | // Implement y = x^2 | ||
Line 22: | Line 16: | ||
} | } | ||
− | int df(const Eigen:: | + | int df(const Eigen::VectorXf &x, Eigen::MatrixXf &fjac) const |
{ | { | ||
// Implement dy/dx = 2*x | // Implement dy/dx = 2*x | ||
Line 28: | Line 22: | ||
return 0; | return 0; | ||
} | } | ||
+ | |||
+ | int inputs() const { return 1; } | ||
+ | int values() const { return 0; } | ||
}; | }; | ||
Line 35: | Line 32: | ||
Eigen::VectorXf x(1); | Eigen::VectorXf x(1); | ||
x(0) = 2; | x(0) = 2; | ||
+ | std::cout << "x: " << x << std::endl; | ||
// do the computation | // do the computation | ||
Line 41: | Line 39: | ||
lm.minimize(x); | lm.minimize(x); | ||
− | + | std::cout << "x: " << x << std::endl; | |
− | + | std::cout << "lm.fvec: " << lm.fvec << std::endl; | |
− | + | ||
return 0; | return 0; | ||
} | } |
Revision as of 18:31, 4 November 2011
ProjectPoints.cpp
#include <iostream> #include <Eigen/Dense> #include <unsupported/Eigen/NonLinearOptimization> struct MyFunctor { int operator()(const Eigen::VectorXf &x, Eigen::VectorXf &fvec) const { // Implement y = x^2 fvec(0) = x(0)*x(0); return 0; } int df(const Eigen::VectorXf &x, Eigen::MatrixXf &fjac) const { // Implement dy/dx = 2*x fjac(0) = 2.0f * x(0); return 0; } int inputs() const { return 1; } int values() const { return 0; } }; int main(int argc, char *argv[]) { Eigen::VectorXf x(1); x(0) = 2; std::cout << "x: " << x << std::endl; // do the computation MyFunctor functor; Eigen::LevenbergMarquardt<MyFunctor, float> lm(functor); lm.minimize(x); std::cout << "x: " << x << std::endl; std::cout << "lm.fvec: " << lm.fvec << std::endl; return 0; }
CMakeLists.txt
cmake_minimum_required(VERSION 2.6) PROJECT(LevenbergMarquardt) include_directories(/media/portable/src/Eigen) # FIND_PACKAGE(Eigen3 REQUIRED) # INCLUDE(${Eigen_USE_FILE}) include_directories(${EIGEN3_INCLUDE_DIR} ${EIGEN3_INCLUDE_DIR}/unsupported/) SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DEIGEN_YES_I_KNOW_SPARSE_MODULE_IS_NOT_STABLE_YET") ADD_EXECUTABLE(LevenbergMarquardt LevenbergMarquardt.cpp )