|
mxlib
c++ tools for analyzing astronomical data and other tasks by Jared R. Males. [git repo]
|
A templatized interface to the levmar package.
Requires a fitter class, which conforms to one of the following minimum specifications. To use the finite difference jacobian calculation:
If you wish to provide your own jacobian:
Note that if your fitter has a jacobian function (jacf), you must define
for it to be used.
| fitterT | a class with at least the minimum interface described above. |
Definition at line 115 of file levmarInterface.hpp.
#include <math/fit/levmarInterface.hpp>
Inheritance diagram for mx::math::fit::levmarInterface< fitterT >:Public Member Functions | |
| levmarInterface () | |
| Default constructor. | |
| levmarInterface (realT *i_p, realT *i_x, int i_m, int i_n, void *i_adata) | |
| Setup constructor. | |
| ~levmarInterface () | |
| Destructor. | |
| void | nParams (int i_m) |
| Set number of parameters, but don't allocate. | |
| int | nParams () |
| Get the current number of parameters. | |
| void | allocate_params () |
| Allocate parameters array based on previous call to nParams. | |
| void | allocate_params (int i_m) |
| Set number of parameters and allocate. | |
| void | point_params (realT *i_p) |
| Point the parameter pointer at an externally allocated array. | |
| void | point_params (realT *i_p, int i_m) |
| Point the parameter pointer at an externally allocated array. | |
| void | set_params (realT *i_p) |
| Copy parameters to the parameter array. | |
| realT * | get_params () |
| Get current pointer array address. | |
| void | set_itmax (int i_itmax) |
| Set the maximum number of iterations. | |
| int | get_itmax () |
| Get the maximum number of iterations. | |
| void | allocate_work () |
| Allocate the work and covar matrices. | |
| void | set_opts (int n, realT val) |
| Set one of the minimization options to val. | |
| void | set_opts_default (int n=-1) |
| Set one or all of the minimization options to the default. | |
| realT | get_opts (int n) |
| Get the current value of an option. | |
| int | fit () |
| Perform the fit. | |
| realT | get_initial_norm () |
| Returns the L2-norm before minimization occurs. | |
| realT | get_final_norm () |
| Returns the L2-norm at the end of the minimization. | |
| int | get_iterations () |
| Get the number of iterations taken during the minimization. | |
| int | get_reason_code () |
| Get a code specifying the reason minimization terminated. | |
| std::string | get_reason_string () |
| Get the descriptive string describing the reason minimization terminated. | |
| int | get_fevals () |
| Get the number of function evaluations during the minimization. | |
| int | get_jevals () |
| Get the number of jacobian evaluations during the minimization. | |
| int | get_nlinsys () |
| Get the number of linear system solutions during the minimization. | |
| double | get_deltaT () |
| Get the elapsed time of the fit. | |
| template<typename iosT , char comment = '#'> | |
| iosT & | dumpParameters (iosT &ios) |
| Output current parameters to a stream. | |
| template<char comment = '#'> | |
| std::ostream & | dumpParameters () |
| Dump the parameter vector to stdout. | |
| template<typename iosT , char comment = '#'> | |
| iosT & | dumpReport (iosT &ios, bool dumpParams=true) |
| Output current parameters to a stream. | |
| template<char comment = '#'> | |
| std::ostream & | dumpReport (bool dumpParams=true) |
| Dump a status report to stdout. | |
Public Attributes | |
| realT * | x |
| I: measurement vector. NULL implies a zero vector. | |
| int | n |
| I: measurement vector dimension. | |
| realT | opts [LM_OPTS_SZ] |
| Options passed to the minimization routines. See set_opts for details. | |
| realT | info [LM_INFO_SZ] |
| Information regarding the minimization. | |
| double | deltaT |
| Elapsed time of the fitting procedure. | |
| realT * | work |
| Working memory passed to the levmar routines. | |
| int | work_sz |
| The current size of the work array. | |
| realT * | covar |
| Covariance matrix corresponding to LS solution; mxm. | |
| int | covar_sz |
| The current size of the covar array. | |
| bool | getCovar |
| Controls whether the covar array is allocated. | |
| void * | adata |
| Pointer to possibly additional data, passed uninterpreted to func & jacf. | |
Protected Attributes | |
| realT * | p |
| Parameter array. On input is the initial estimates. On output has the estimated solution. | |
| realT * | init_p |
| Parameter array on input, saved for comparison. | |
| int | m |
| Parameter vector dimension (i.e. number of unknowns) | |
| bool | own_p |
| Flag indicating whether the p array is owned by this object (for de-allocation). | |
| int | itmax |
| Maximum number of iterations, default is 100. | |
| mx::math::fit::levmarInterface< fitterT >::levmarInterface | ( | ) |
Default constructor.
With this constructor, you must set the parameter, data, and adata before calling fit().
Definition at line 378 of file levmarInterface.hpp.
| mx::math::fit::levmarInterface< fitterT >::levmarInterface | ( | typename fitterT::realT * | i_p, |
| typename fitterT::realT * | i_x, | ||
| int | i_m, | ||
| int | i_n, | ||
| void * | i_adata | ||
| ) |
Setup constructor.
with this constructor fit() will work immediately.
| [in] | i_p | pointer to the initial parameter guess |
| [in] | i_x | pointer to the data (can be NULL) |
| [in] | i_m | the size of i_p |
| [in] | i_n | the size of i_x |
| [in] | i_adata | pointer to auxiliary data (can be NULL) |
Definition at line 384 of file levmarInterface.hpp.
References mx::math::six_fifths().
| mx::math::fit::levmarInterface< fitterT >::~levmarInterface | ( | ) |
| void mx::math::fit::levmarInterface< fitterT >::allocate_params | ( | ) |
Allocate parameters array based on previous call to nParams.
Definition at line 440 of file levmarInterface.hpp.
References mx::math::six_fifths().
Referenced by mx::math::fit::fitMoffat2D< fitterT >::setFixed(), and mx::math::fit::fitEmpirical2DGen< fitterT >::setFixed().
| void mx::math::fit::levmarInterface< fitterT >::allocate_params | ( | int | i_m | ) |
Set number of parameters and allocate.
Definition at line 453 of file levmarInterface.hpp.
References mx::math::six_fifths().
| void mx::math::fit::levmarInterface< fitterT >::allocate_work | ( | ) |
Allocate the work and covar matrices.
Uses a function object specialized for whether or not there is a jacobian to determine the size of work.
Definition at line 528 of file levmarInterface.hpp.
References mx::math::six_fifths().
| std::ostream & mx::math::fit::levmarInterface< fitterT >::dumpParameters | ( | ) |
Dump the parameter vector to stdout.
| comment | is a comment character to start each line. Can be '\0'. |
Definition at line 758 of file levmarInterface.hpp.
References mx::math::six_fifths().
| iosT & mx::math::fit::levmarInterface< fitterT >::dumpParameters | ( | iosT & | ios | ) |
Output current parameters to a stream.
Prints a formatted list of all current fit parameters.
| iosT | is a std::ostream-like type. |
| comment | is a comment character to start each line. Can be '\0'. |
| [in] | ios | a std::ostream-like stream. |
Definition at line 741 of file levmarInterface.hpp.
References mx::math::six_fifths().
| std::ostream & mx::math::fit::levmarInterface< fitterT >::dumpReport | ( | bool | dumpParams = true | ) |
Dump a status report to stdout.
| comment | is a comment character to start each line. Can be '\0'. |
| [in] | dumpParams | [optional] whether or not to dump the parameters. |
Definition at line 787 of file levmarInterface.hpp.
References mx::math::six_fifths().
| iosT & mx::math::fit::levmarInterface< fitterT >::dumpReport | ( | iosT & | ios, |
| bool | dumpParams = true |
||
| ) |
Output current parameters to a stream.
Prints a formatted list of all current fit parameters.
| iosT | is a std::ostream-like type. |
| comment | is a comment character to start each line. Can be '\0'. |
| [in] | ios | a std::ostream-like stream. |
| [in] | dumpParams | [optional] whether or not to dump the parameters. |
Definition at line 765 of file levmarInterface.hpp.
References mx::math::six_fifths().
| int mx::math::fit::levmarInterface< fitterT >::fit | ( | ) |
Perform the fit.
This calls allocate_work, and then dispatches the levmar routine appropriate for fitterT.
Definition at line 629 of file levmarInterface.hpp.
References mx::sys::get_curr_time(), and mx::math::six_fifths().
Referenced by mx::math::fit::fitGaussian1D< _realT >::fit(), and mx::math::fit::fitGaussian2D< fitterT >::fit().
|
inline |
Get the elapsed time of the fit.
Definition at line 308 of file levmarInterface.hpp.
References mx::math::fit::levmarInterface< fitterT >::deltaT.
| int mx::math::fit::levmarInterface< fitterT >::get_fevals | ( | ) |
Get the number of function evaluations during the minimization.
Definition at line 691 of file levmarInterface.hpp.
| fitterT::realT mx::math::fit::levmarInterface< fitterT >::get_final_norm | ( | ) |
Returns the L2-norm at the end of the minimization.
Definition at line 673 of file levmarInterface.hpp.
| fitterT::realT mx::math::fit::levmarInterface< fitterT >::get_initial_norm | ( | ) |
Returns the L2-norm before minimization occurs.
Definition at line 667 of file levmarInterface.hpp.
| int mx::math::fit::levmarInterface< fitterT >::get_iterations | ( | ) |
Get the number of iterations taken during the minimization.
Definition at line 679 of file levmarInterface.hpp.
| int mx::math::fit::levmarInterface< fitterT >::get_itmax | ( | ) |
Get the maximum number of iterations.
Definition at line 485 of file levmarInterface.hpp.
| int mx::math::fit::levmarInterface< fitterT >::get_jevals | ( | ) |
Get the number of jacobian evaluations during the minimization.
Definition at line 697 of file levmarInterface.hpp.
| int mx::math::fit::levmarInterface< fitterT >::get_nlinsys | ( | ) |
Get the number of linear system solutions during the minimization.
Definition at line 703 of file levmarInterface.hpp.
| fitterT::realT mx::math::fit::levmarInterface< fitterT >::get_opts | ( | int | n | ) |
Get the current value of an option.
See set_opts for a description of the options
| n | the option number |
Definition at line 597 of file levmarInterface.hpp.
| fitterT::realT * mx::math::fit::levmarInterface< fitterT >::get_params | ( | ) |
Get current pointer array address.
Definition at line 491 of file levmarInterface.hpp.
| int mx::math::fit::levmarInterface< fitterT >::get_reason_code | ( | ) |
Get a code specifying the reason minimization terminated.
Definition at line 685 of file levmarInterface.hpp.
| std::string mx::math::fit::levmarInterface< fitterT >::get_reason_string | ( | ) |
Get the descriptive string describing the reason minimization terminated.
Definition at line 709 of file levmarInterface.hpp.
| int mx::math::fit::levmarInterface< fitterT >::nParams | ( | ) |
Get the current number of parameters.
Definition at line 434 of file levmarInterface.hpp.
| void mx::math::fit::levmarInterface< fitterT >::nParams | ( | int | i_m | ) |
Set number of parameters, but don't allocate.
| [in] | i_m | the number of parameters |
Definition at line 413 of file levmarInterface.hpp.
References mx::math::six_fifths().
| void mx::math::fit::levmarInterface< fitterT >::point_params | ( | realT * | i_p | ) |
Point the parameter pointer at an externally allocated array.
Definition at line 461 of file levmarInterface.hpp.
References mx::math::six_fifths().
| void mx::math::fit::levmarInterface< fitterT >::point_params | ( | realT * | i_p, |
| int | i_m | ||
| ) |
Point the parameter pointer at an externally allocated array.
Definition at line 472 of file levmarInterface.hpp.
References mx::math::six_fifths().
| void mx::math::fit::levmarInterface< fitterT >::set_itmax | ( | int | i_itmax | ) |
Set the maximum number of iterations.
Sets itmax. Initialization default is itmax = 100
| i_itmax | the new value of itmax to set |
Definition at line 479 of file levmarInterface.hpp.
References mx::math::six_fifths().
| void mx::math::fit::levmarInterface< fitterT >::set_opts | ( | int | n, |
| realT | val | ||
| ) |
Set one of the minimization options to val.
The options correspond to:
0: the scale factor of the initial \( \mu \), default is 1e-3.
1: \( \epsilon_1 \) Stopping threshold for ||J^T e||_inf, default is 1e-17.
2: \( \epsilon_2 \) Stopping threshold for ||Dp||_2, default is 1e-17.
3: \( \epsilon_3 \) Stopping threshold for ||e||_2, default is 1e-17.
4: \( \Delta_{diff}\) Stepsize for finite differences, default is 1e-6.
| n | is the option number |
| val | is the value to set |
Definition at line 564 of file levmarInterface.hpp.
References mx::math::six_fifths().
| void mx::math::fit::levmarInterface< fitterT >::set_opts_default | ( | int | n = -1 | ) |
Set one or all of the minimization options to the default.
See set_opts for discription of the options.
| n | the option number. Pass -1 to set all options to defaults. |
Definition at line 570 of file levmarInterface.hpp.
| void mx::math::fit::levmarInterface< fitterT >::set_params | ( | realT * | i_p | ) |
Copy parameters to the parameter array.
This assumes that either the array was allocated (i.e. with allocate_params) or that point_params has been called
Definition at line 497 of file levmarInterface.hpp.
References mx::math::six_fifths().
| void* mx::math::fit::levmarInterface< fitterT >::adata |
Pointer to possibly additional data, passed uninterpreted to func & jacf.
Set to NULL if not needed.
Definition at line 173 of file levmarInterface.hpp.
| realT* mx::math::fit::levmarInterface< fitterT >::covar |
Covariance matrix corresponding to LS solution; mxm.
Here this is allocated to size m-x-m by allocate_work, but if you don't want it allocated and calculated set getCovar to false and NULL will be passed.
Definition at line 162 of file levmarInterface.hpp.
| int mx::math::fit::levmarInterface< fitterT >::covar_sz |
The current size of the covar array.
Definition at line 165 of file levmarInterface.hpp.
| double mx::math::fit::levmarInterface< fitterT >::deltaT |
Elapsed time of the fitting procedure.
Definition at line 146 of file levmarInterface.hpp.
Referenced by mx::math::fit::levmarInterface< fitterT >::get_deltaT().
| bool mx::math::fit::levmarInterface< fitterT >::getCovar |
Controls whether the covar array is allocated.
Definition at line 168 of file levmarInterface.hpp.
| realT mx::math::fit::levmarInterface< fitterT >::info[LM_INFO_SZ] |
Information regarding the minimization.
See the levmar source code for documentation. These fields are accessed by get_initial_norm, get_final_norm, get_iterations, get_reason_code, get_reason_string, get_fevals, get_jevals, get_nlinsys
Definition at line 143 of file levmarInterface.hpp.
|
protected |
Parameter array on input, saved for comparison.
Definition at line 123 of file levmarInterface.hpp.
|
protected |
Maximum number of iterations, default is 100.
Definition at line 128 of file levmarInterface.hpp.
|
protected |
Parameter vector dimension (i.e. number of unknowns)
Definition at line 125 of file levmarInterface.hpp.
| int mx::math::fit::levmarInterface< fitterT >::n |
I: measurement vector dimension.
Definition at line 133 of file levmarInterface.hpp.
Referenced by mx::math::fit::fitEmpirical2DGen< fitterT >::setArray(), and mx::math::fit::fitMoffat2D< fitterT >::setArray().
| realT mx::math::fit::levmarInterface< fitterT >::opts[LM_OPTS_SZ] |
Options passed to the minimization routines. See set_opts for details.
Definition at line 136 of file levmarInterface.hpp.
|
protected |
Flag indicating whether the p array is owned by this object (for de-allocation).
Definition at line 126 of file levmarInterface.hpp.
|
protected |
Parameter array. On input is the initial estimates. On output has the estimated solution.
Definition at line 122 of file levmarInterface.hpp.
Referenced by mx::math::fit::fitEmpirical2DGen< fitterT >::dx(), mx::math::fit::fitEmpirical2DGen< fitterT >::dy(), mx::math::fit::fitEmpirical2DGen< fitterT >::scale(), mx::math::fit::fitAiry2D< fitterT >::setGuess(), mx::math::fit::fitAiry2D< fitterT >::setGuess(), mx::math::fit::fitAiry2D< fitterT >::setGuess(), mx::math::fit::fitExpModGaussian< fitterT >::setGuess(), mx::math::fit::fitWeibull< fitterT >::setGuess(), mx::math::fit::fitGammaDistribution< fitterT >::setGuess(), mx::math::fit::fitExpModGaussian< fitterT >::setGuess(), mx::math::fit::fitEmpirical2DGen< fitterT >::setGuess(), mx::math::fit::fitWeibull< fitterT >::setGuess(), mx::math::fit::fitGammaDistribution< fitterT >::setGuess(), and mx::math::fit::fitGammaDistribution< fitterT >::setGuess().
| realT* mx::math::fit::levmarInterface< fitterT >::work |
Working memory passed to the levmar routines.
From the levmar documentation: at least LM_DER/DIF_WORKSZ() reals large, allocated if NULL Here this is always allocated by a call to allocate_work.
Definition at line 152 of file levmarInterface.hpp.
| int mx::math::fit::levmarInterface< fitterT >::work_sz |
The current size of the work array.
Definition at line 155 of file levmarInterface.hpp.
| realT* mx::math::fit::levmarInterface< fitterT >::x |
I: measurement vector. NULL implies a zero vector.
Definition at line 131 of file levmarInterface.hpp.