mxlib
c++ tools for analyzing astronomical data and other tasks by Jared R. Males. [git repo]
mx::math::fit::levmarInterface< fitterT > Class Template Reference

template<class fitterT>
class mx::math::fit::levmarInterface< fitterT >

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:

//This will cause the levmar_dif routine to be used
template<typename _realT>
struct dif_fitter
{
typedef _realT realT; //required
static void func(realT *p, realT *hx, int m, int n, void *adata)
{
//do stuff here . . .
}
};
int m
Parameter vector dimension (i.e. number of unknowns)
realT * p
Parameter array. On input is the initial estimates. On output has the estimated solution.
int n
I: measurement vector dimension.
void * adata
Pointer to possibly additional data, passed uninterpreted to func & jacf.

If you wish to provide your own jacobian:

//This will cause the levmar_der routine to be used
template<typename _realT>
struct der_fitter
{
typedef _realT realT; //required
typdef bool hasJacobian; //this signals that jacf exists and should be used.
static void func(realT *p, realT *hx, int m, int n, void *adata)
{
//do stuff here . . .
}
// This is the jacobian.
static void jacf(realT *p, realT *j, int m, int n, void *adata)
{
//do stuff here . . .
}
};

Note that if your fitter has a jacobian function (jacf), you must define

typedef bool hasJacobian;

for it to be used.

Template Parameters
fitterTa class with at least the minimum interface described above.

Definition at line 101 of file levmarInterface.hpp.

#include <math/fit/levmarInterface.hpp>

+ Inheritance diagram for mx::math::fit::levmarInterface< fitterT >:

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...
 

Constructor & Destructor Documentation

◆ levmarInterface() [1/2]

template<class fitterT >
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.

◆ levmarInterface() [2/2]

template<class fitterT >
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.

Parameters
[in]i_ppointer to the initial parameter guess
[in]i_xpointer to the data (can be NULL)
[in]i_mthe size of i_p
[in]i_nthe size of i_x
[in]i_adatapointer to auxiliary data (can be NULL)

Definition at line 376 of file levmarInterface.hpp.

◆ ~levmarInterface()

template<class fitterT >
mx::math::fit::levmarInterface< fitterT >::~levmarInterface

Destructor.

Frees the work and covar matrices.

Definition at line 392 of file levmarInterface.hpp.

Member Function Documentation

◆ allocate_params() [1/2]

template<class fitterT >
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().

◆ allocate_params() [2/2]

template<class fitterT >
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.

◆ allocate_work()

template<class fitterT >
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.

◆ dumpParameters() [1/2]

template<class fitterT >
template<char comment>
std::ostream & mx::math::fit::levmarInterface< fitterT >::dumpParameters

Dump the parameter vector to stdout.

Template Parameters
commentis a comment character to start each line. Can be '\0'.

Definition at line 767 of file levmarInterface.hpp.

◆ dumpParameters() [2/2]

template<class fitterT >
template<typename iosT , char comment>
iosT & mx::math::fit::levmarInterface< fitterT >::dumpParameters ( iosT &  ios)

Output current parameters to a stream.

Prints a formatted list of all current fit parameters.

Template Parameters
iosTis a std::ostream-like type.
commentis a comment character to start each line. Can be '\0'.
Parameters
[in]iosa std::ostream-like stream.

Definition at line 750 of file levmarInterface.hpp.

References mx::astro::constants::c().

◆ dumpReport() [1/2]

template<class fitterT >
template<char comment>
std::ostream & mx::math::fit::levmarInterface< fitterT >::dumpReport ( bool  dumpParams = true)

Dump a status report to stdout.

Template Parameters
commentis a comment character to start each line. Can be '\0'.
Parameters
[in]dumpParams[optional] whether or not to dump the parameters.

Definition at line 798 of file levmarInterface.hpp.

◆ dumpReport() [2/2]

template<class fitterT >
template<typename iosT , char comment>
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.

Template Parameters
iosTis a std::ostream-like type.
commentis a comment character to start each line. Can be '\0'.
Parameters
[in]iosa 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().

◆ fit()

template<class fitterT >
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().

◆ get_deltaT()

template<class fitterT >
double mx::math::fit::levmarInterface< fitterT >::get_deltaT ( )
inline

Get the elapsed time of the fit.

Definition at line 300 of file levmarInterface.hpp.

References mx::math::fit::levmarInterface< fitterT >::deltaT.

◆ get_fevals()

template<class fitterT >
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.

◆ get_final_norm()

template<class fitterT >
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.

◆ get_initial_norm()

template<class fitterT >
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.

◆ get_iterations()

template<class fitterT >
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.

◆ get_itmax()

template<class fitterT >
int mx::math::fit::levmarInterface< fitterT >::get_itmax

Get the maximum number of iterations.

Definition at line 479 of file levmarInterface.hpp.

◆ get_jevals()

template<class fitterT >
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.

◆ get_nlinsys()

template<class fitterT >
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.

◆ get_opts()

template<class fitterT >
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

Parameters
nthe option number

Definition at line 590 of file levmarInterface.hpp.

◆ get_params()

template<class fitterT >
fitterT::realT * mx::math::fit::levmarInterface< fitterT >::get_params

Get current pointer array address.

Definition at line 485 of file levmarInterface.hpp.

◆ get_reason_code()

template<class fitterT >
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.

◆ get_reason_string()

template<class fitterT >
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.

◆ nParams() [1/2]

template<class fitterT >
int mx::math::fit::levmarInterface< fitterT >::nParams

Get the current number of parameters.

Returns
the current number of parameters (m)

Definition at line 426 of file levmarInterface.hpp.

◆ nParams() [2/2]

template<class fitterT >
void mx::math::fit::levmarInterface< fitterT >::nParams ( int  i_m)

Set number of parameters, but don't allocate.

Parameters
[in]i_mthe number of parameters

Definition at line 404 of file levmarInterface.hpp.

◆ point_params() [1/2]

template<class fitterT >
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.

◆ point_params() [2/2]

template<class fitterT >
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.

◆ set_itmax()

template<class fitterT >
void mx::math::fit::levmarInterface< fitterT >::set_itmax ( int  i_itmax)

Set the maximum number of iterations.

Sets itmax. Initialization default is itmax = 100

Parameters
i_itmaxthe new value of itmax to set

Definition at line 473 of file levmarInterface.hpp.

◆ set_opts()

template<class fitterT >
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

Parameters
nis the option number
valis the value to set

Definition at line 556 of file levmarInterface.hpp.

◆ set_opts_default()

template<class fitterT >
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.

Parameters
nthe option number. Pass -1 to set all options to defaults.

Definition at line 563 of file levmarInterface.hpp.

◆ set_params()

template<class fitterT >
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.

Member Data Documentation

◆ adata

template<class fitterT >
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.

◆ covar

template<class fitterT >
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.

◆ covar_sz

template<class fitterT >
int mx::math::fit::levmarInterface< fitterT >::covar_sz

The current size of the covar array.

Definition at line 155 of file levmarInterface.hpp.

◆ deltaT

template<class fitterT >
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().

◆ getCovar

template<class fitterT >
bool mx::math::fit::levmarInterface< fitterT >::getCovar

Controls whether the covar array is allocated.

Definition at line 158 of file levmarInterface.hpp.

◆ info

template<class fitterT >
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.

◆ init_p

template<class fitterT >
realT* mx::math::fit::levmarInterface< fitterT >::init_p
protected

Parameter array on input, saved for comparison.

Definition at line 111 of file levmarInterface.hpp.

◆ itmax

template<class fitterT >
int mx::math::fit::levmarInterface< fitterT >::itmax
protected

Maximum number of iterations, default is 100.

Definition at line 116 of file levmarInterface.hpp.

◆ m

template<class fitterT >
int mx::math::fit::levmarInterface< fitterT >::m
protected

Parameter vector dimension (i.e. number of unknowns)

Definition at line 113 of file levmarInterface.hpp.

◆ n

template<class fitterT >
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().

◆ opts

template<class fitterT >
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.

◆ own_p

template<class fitterT >
bool mx::math::fit::levmarInterface< fitterT >::own_p
protected

Flag indicating whether the p array is owned by this object (for de-allocation).

Definition at line 114 of file levmarInterface.hpp.

◆ p

template<class fitterT >
realT* mx::math::fit::levmarInterface< fitterT >::p
protected

◆ work

template<class fitterT >
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.

◆ work_sz

template<class fitterT >
int mx::math::fit::levmarInterface< fitterT >::work_sz

The current size of the work array.

Definition at line 145 of file levmarInterface.hpp.

◆ x

template<class fitterT >
realT* mx::math::fit::levmarInterface< fitterT >::x

I: measurement vector. NULL implies a zero vector.

Definition at line 119 of file levmarInterface.hpp.


The documentation for this class was generated from the following file: