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 101 of file levmarInterface.hpp.
#include <math/fit/levmarInterface.hpp>
Public Member Functions | |
levmarInterface () | |
Default constructor. More... | |
levmarInterface (realT *i_p, realT *i_x, int i_m, int i_n, void *i_adata) | |
Setup constructor. More... | |
~levmarInterface () | |
Destructor. More... | |
void | nParams (int i_m) |
Set number of parameters, but don't allocate. More... | |
int | nParams () |
Get the current number of parameters. More... | |
void | allocate_params () |
Allocate parameters array based on previous call to nParams. More... | |
void | allocate_params (int i_m) |
Set number of parameters and allocate. More... | |
void | point_params (realT *i_p) |
Point the parameter pointer at an externally allocated array. More... | |
void | point_params (realT *i_p, int i_m) |
Point the parameter pointer at an externally allocated array. More... | |
void | set_params (realT *i_p) |
Copy parameters to the parameter array. More... | |
realT * | get_params () |
Get current pointer array address. More... | |
void | set_itmax (int i_itmax) |
Set the maximum number of iterations. More... | |
int | get_itmax () |
Get the maximum number of iterations. More... | |
void | allocate_work () |
Allocate the work and covar matrices. More... | |
void | set_opts (int n, realT val) |
Set one of the minimization options to val. More... | |
void | set_opts_default (int n=-1) |
Set one or all of the minimization options to the default. More... | |
realT | get_opts (int n) |
Get the current value of an option. More... | |
int | fit () |
Perform the fit. More... | |
realT | get_initial_norm () |
Returns the L2-norm before minimization occurs. More... | |
realT | get_final_norm () |
Returns the L2-norm at the end of the minimization. More... | |
int | get_iterations () |
Get the number of iterations taken during the minimization. More... | |
int | get_reason_code () |
Get a code specifying the reason minimization terminated. More... | |
std::string | get_reason_string () |
Get the descriptive string describing the reason minimization terminated. More... | |
int | get_fevals () |
Get the number of function evaluations during the minimization. More... | |
int | get_jevals () |
Get the number of jacobian evaluations during the minimization. More... | |
int | get_nlinsys () |
Get the number of linear system solutions during the minimization. More... | |
double | get_deltaT () |
Get the elapsed time of the fit. More... | |
template<typename iosT , char comment = '#'> | |
iosT & | dumpParameters (iosT &ios) |
Output current parameters to a stream. More... | |
template<char comment = '#'> | |
std::ostream & | dumpParameters () |
Dump the parameter vector to stdout. More... | |
template<typename iosT , char comment = '#'> | |
iosT & | dumpReport (iosT &ios, bool dumpParams=true) |
Output current parameters to a stream. More... | |
template<char comment = '#'> | |
std::ostream & | dumpReport (bool dumpParams=true) |
Dump a status report to stdout. More... | |
Public Attributes | |
realT * | x |
I: measurement vector. NULL implies a zero vector. More... | |
int | n |
I: measurement vector dimension. More... | |
realT | opts [LM_OPTS_SZ] |
Options passed to the minimization routines. See set_opts for details. More... | |
realT | info [LM_INFO_SZ] |
Information regarding the minimization. More... | |
double | deltaT |
Elapsed time of the fitting procedure. More... | |
realT * | work |
Working memory passed to the levmar routines. More... | |
int | work_sz |
The current size of the work array. More... | |
realT * | covar |
Covariance matrix corresponding to LS solution; mxm. More... | |
int | covar_sz |
The current size of the covar array. More... | |
bool | getCovar |
Controls whether the covar array is allocated. More... | |
void * | adata |
Pointer to possibly additional data, passed uninterpreted to func & jacf. More... | |
Protected Attributes | |
realT * | p |
Parameter array. On input is the initial estimates. On output has the estimated solution. More... | |
realT * | init_p |
Parameter array on input, saved for comparison. More... | |
int | m |
Parameter vector dimension (i.e. number of unknowns) More... | |
bool | own_p |
Flag indicating whether the p array is owned by this object (for de-allocation). More... | |
int | itmax |
Maximum number of iterations, default is 100. More... | |
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 370 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 376 of file levmarInterface.hpp.
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 433 of file levmarInterface.hpp.
Referenced by mx::math::fit::fitMoffat2D< fitterT >::setFixed().
void mx::math::fit::levmarInterface< fitterT >::allocate_params | ( | int | i_m | ) |
Set number of parameters and allocate.
Definition at line 446 of file levmarInterface.hpp.
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 522 of file levmarInterface.hpp.
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 767 of file levmarInterface.hpp.
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 750 of file levmarInterface.hpp.
References mx::astro::constants::c().
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 798 of file levmarInterface.hpp.
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 774 of file levmarInterface.hpp.
References mx::astro::constants::c().
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 639 of file levmarInterface.hpp.
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 300 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 699 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 681 of file levmarInterface.hpp.
fitterT::realT mx::math::fit::levmarInterface< fitterT >::get_initial_norm |
Returns the L2-norm before minimization occurs.
Definition at line 675 of file levmarInterface.hpp.
int mx::math::fit::levmarInterface< fitterT >::get_iterations |
Get the number of iterations taken during the minimization.
Definition at line 687 of file levmarInterface.hpp.
int mx::math::fit::levmarInterface< fitterT >::get_itmax |
Get the maximum number of iterations.
Definition at line 479 of file levmarInterface.hpp.
int mx::math::fit::levmarInterface< fitterT >::get_jevals |
Get the number of jacobian evaluations during the minimization.
Definition at line 705 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 711 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 590 of file levmarInterface.hpp.
fitterT::realT * mx::math::fit::levmarInterface< fitterT >::get_params |
Get current pointer array address.
Definition at line 485 of file levmarInterface.hpp.
int mx::math::fit::levmarInterface< fitterT >::get_reason_code |
Get a code specifying the reason minimization terminated.
Definition at line 693 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 717 of file levmarInterface.hpp.
int mx::math::fit::levmarInterface< fitterT >::nParams |
Get the current number of parameters.
Definition at line 426 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 404 of file levmarInterface.hpp.
void mx::math::fit::levmarInterface< fitterT >::point_params | ( | realT * | i_p | ) |
Point the parameter pointer at an externally allocated array.
Definition at line 454 of file levmarInterface.hpp.
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 466 of file levmarInterface.hpp.
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 473 of file levmarInterface.hpp.
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 \)
1: \( \epsilon_1 \) Stopping threshold for ||J^T e||_inf
2: \( \epsilon_1 \) Stopping threshold for ||Dp||_2
3: \( \epsilon_1 \) Stopping threshold for ||e||_2
4: \( \Delta_{diff}\) Stepsize for finite differences
n | is the option number |
val | is the value to set |
Definition at line 556 of file levmarInterface.hpp.
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 563 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 491 of file levmarInterface.hpp.
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 163 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 allcoated and calculated set getCovar to false and NULL will be passed.
Definition at line 152 of file levmarInterface.hpp.
int mx::math::fit::levmarInterface< fitterT >::covar_sz |
The current size of the covar array.
Definition at line 155 of file levmarInterface.hpp.
double mx::math::fit::levmarInterface< fitterT >::deltaT |
Elapsed time of the fitting procedure.
Definition at line 136 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 158 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 133 of file levmarInterface.hpp.
|
protected |
Parameter array on input, saved for comparison.
Definition at line 111 of file levmarInterface.hpp.
|
protected |
Maximum number of iterations, default is 100.
Definition at line 116 of file levmarInterface.hpp.
|
protected |
Parameter vector dimension (i.e. number of unknowns)
Definition at line 113 of file levmarInterface.hpp.
int mx::math::fit::levmarInterface< fitterT >::n |
I: measurement vector dimension.
Definition at line 121 of file levmarInterface.hpp.
Referenced by 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 126 of file levmarInterface.hpp.
|
protected |
Flag indicating whether the p array is owned by this object (for de-allocation).
Definition at line 114 of file levmarInterface.hpp.
|
protected |
Parameter array. On input is the initial estimates. On output has the estimated solution.
Definition at line 110 of file levmarInterface.hpp.
Referenced by mx::math::fit::fitAiry2D< fitterT >::setGuess(), mx::math::fit::fitExpModGaussian< fitterT >::setGuess(), mx::math::fit::fitWeibull< 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 142 of file levmarInterface.hpp.
int mx::math::fit::levmarInterface< fitterT >::work_sz |
The current size of the work array.
Definition at line 145 of file levmarInterface.hpp.
realT* mx::math::fit::levmarInterface< fitterT >::x |
I: measurement vector. NULL implies a zero vector.
Definition at line 119 of file levmarInterface.hpp.