"Fossies" - the Fresh Open Source Software Archive

Member "fityk-1.3.1/fityk/cmpfit/mpfit.h" (13 May 2016, 7678 Bytes) of package /linux/misc/fityk-1.3.1.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "mpfit.h" see the Fossies "Dox" file reference documentation.

    1 /* 
    2  * MINPACK-1 Least Squares Fitting Library
    3  *
    4  * Original public domain version by B. Garbow, K. Hillstrom, J. More'
    5  *   (Argonne National Laboratory, MINPACK project, March 1980)
    6  * 
    7  * Tranlation to C Language by S. Moshier (moshier.net)
    8  * 
    9  * Enhancements and packaging by C. Markwardt
   10  *   (comparable to IDL fitting routine MPFIT
   11  *    see http://cow.physics.wisc.edu/~craigm/idl/idl.html)
   12  */
   13 
   14 /* Header file defining constants, data structures and functions of
   15    mpfit library 
   16    $Id: mpfit.h,v 1.15 2013/04/09 17:55:32 craigm Exp $
   17 */
   18 
   19 #ifndef MPFIT_H
   20 #define MPFIT_H
   21 
   22 /* This is a C library.  Allow compilation with a C++ compiler */
   23 #ifdef __cplusplus
   24 extern "C" {
   25 #endif
   26 
   27 /* MPFIT version string */
   28 #define MPFIT_VERSION "1.2"
   29 
   30 /* Definition of a parameter constraint structure */
   31 struct mp_par_struct {
   32   int fixed;        /* 1 = fixed; 0 = free */
   33   int limited[2];   /* 1 = low/upper limit; 0 = no limit */
   34   double limits[2]; /* lower/upper limit boundary value */
   35 
   36   char *parname;    /* Name of parameter, or 0 for none */
   37   double step;      /* Step size for finite difference */
   38   double relstep;   /* Relative step size for finite difference */
   39   int side;         /* Sidedness of finite difference derivative 
   40                 0 - one-sided derivative computed automatically
   41                 1 - one-sided derivative (f(x+h) - f(x)  )/h
   42                -1 - one-sided derivative (f(x)   - f(x-h))/h
   43                 2 - two-sided derivative (f(x+h) - f(x-h))/(2*h) 
   44             3 - user-computed analytical derivatives
   45             */
   46   int deriv_debug;  /* Derivative debug mode: 1 = Yes; 0 = No;
   47 
   48                        If yes, compute both analytical and numerical
   49                        derivatives and print them to the console for
   50                        comparison.
   51 
   52                NOTE: when debugging, do *not* set side = 3,
   53                but rather to the kind of numerical derivative
   54                you want to compare the user-analytical one to
   55                (0, 1, -1, or 2).
   56             */
   57   double deriv_reltol; /* Relative tolerance for derivative debug
   58               printout */
   59   double deriv_abstol; /* Absolute tolerance for derivative debug
   60               printout */
   61 };
   62 
   63 /* Just a placeholder - do not use!! */
   64 typedef void (*mp_iterproc)(void);
   65 
   66 /* Definition of MPFIT configuration structure */
   67 struct mp_config_struct {
   68   /* NOTE: the user may set the value explicitly; OR, if the passed
   69      value is zero, then the "Default" value will be substituted by
   70      mpfit(). */
   71   double ftol;    /* Relative chi-square convergence criterium Default: 1e-10 */
   72   double xtol;    /* Relative parameter convergence criterium  Default: 1e-10 */
   73   double gtol;    /* Orthogonality convergence criterium       Default: 1e-10 */
   74   double epsfcn;  /* Finite derivative step size               Default: MP_MACHEP0 */
   75   double stepfactor; /* Initial step bound                     Default: 100.0 */
   76   double covtol;  /* Range tolerance for covariance calculation Default: 1e-14 */
   77   int maxiter;    /* Maximum number of iterations.  If maxiter == MP_NO_ITER,
   78                      then basic error checking is done, and parameter
   79                      errors/covariances are estimated based on input
   80                      parameter values, but no fitting iterations are done. 
   81              Default: 200
   82           */
   83 #define MP_NO_ITER (-1) /* No iterations, just checking */
   84   int maxfev;     /* Maximum number of function evaluations, or 0 for no limit
   85              Default: 0 (no limit) */
   86   int nprint;     /* Default: 1 */
   87   int douserscale;/* Scale variables by user values?
   88              1 = yes, user scale values in diag;
   89              0 = no, variables scaled internally (Default) */
   90   int nofinitecheck; /* Disable check for infinite quantities from user?
   91             0 = do not perform check (Default)
   92             1 = perform check 
   93              */
   94   mp_iterproc iterproc; /* Placeholder pointer - must set to 0 */
   95 
   96 };
   97 
   98 /* Definition of results structure, for when fit completes */
   99 struct mp_result_struct {
  100   double bestnorm;     /* Final chi^2 */
  101   double orignorm;     /* Starting value of chi^2 */
  102   int niter;           /* Number of iterations */
  103   int nfev;            /* Number of function evaluations */
  104   int status;          /* Fitting status code */
  105   
  106   int npar;            /* Total number of parameters */
  107   int nfree;           /* Number of free parameters */
  108   int npegged;         /* Number of pegged parameters */
  109   int nfunc;           /* Number of residuals (= num. of data points) */
  110 
  111   double *resid;       /* Final residuals
  112               nfunc-vector, or 0 if not desired */
  113   double *xerror;      /* Final parameter uncertainties (1-sigma)
  114               npar-vector, or 0 if not desired */
  115   double *covar;       /* Final parameter covariance matrix
  116               npar x npar array, or 0 if not desired */
  117   char version[20];    /* MPFIT version string */
  118 };  
  119 
  120 /* Convenience typedefs */  
  121 typedef struct mp_par_struct mp_par;
  122 typedef struct mp_config_struct mp_config;
  123 typedef struct mp_result_struct mp_result;
  124 
  125 /* Enforce type of fitting function */
  126 typedef int (*mp_func)(int m, /* Number of functions (elts of fvec) */
  127                int n, /* Number of variables (elts of x) */
  128                double *x,      /* I - Parameters */
  129                double *fvec,   /* O - function values */
  130                double **dvec,  /* O - function derivatives (optional)*/
  131                void *private_data); /* I/O - function private data*/
  132 
  133 /* Error codes */
  134 #define MP_ERR_INPUT (0)         /* General input parameter error */
  135 #define MP_ERR_NAN (-16)         /* User function produced non-finite values */
  136 #define MP_ERR_FUNC (-17)        /* No user function was supplied */
  137 #define MP_ERR_NPOINTS (-18)     /* No user data points were supplied */
  138 #define MP_ERR_NFREE (-19)       /* No free parameters */
  139 #define MP_ERR_MEMORY (-20)      /* Memory allocation error */
  140 #define MP_ERR_INITBOUNDS (-21)  /* Initial values inconsistent w constraints*/
  141 #define MP_ERR_BOUNDS (-22)      /* Initial constraints inconsistent */
  142 #define MP_ERR_PARAM (-23)       /* General input parameter error */
  143 #define MP_ERR_DOF (-24)         /* Not enough degrees of freedom */
  144 
  145 /* Potential success status codes */
  146 #define MP_OK_CHI (1)            /* Convergence in chi-square value */
  147 #define MP_OK_PAR (2)            /* Convergence in parameter value */
  148 #define MP_OK_BOTH (3)           /* Both MP_OK_PAR and MP_OK_CHI hold */
  149 #define MP_OK_DIR (4)            /* Convergence in orthogonality */
  150 #define MP_MAXITER (5)           /* Maximum number of iterations reached */
  151 #define MP_FTOL (6)              /* ftol is too small; no further improvement*/
  152 #define MP_XTOL (7)              /* xtol is too small; no further improvement*/
  153 #define MP_GTOL (8)              /* gtol is too small; no further improvement*/
  154 
  155 /* Double precision numeric constants */
  156 #define MP_MACHEP0 2.2204460e-16
  157 #define MP_DWARF   2.2250739e-308
  158 #define MP_GIANT   1.7976931e+308
  159 
  160 #if 0
  161 /* Float precision */
  162 #define MP_MACHEP0 1.19209e-07
  163 #define MP_DWARF   1.17549e-38
  164 #define MP_GIANT   3.40282e+38
  165 #endif
  166 
  167 #define MP_RDWARF  (sqrt(MP_DWARF*1.5)*10)
  168 #define MP_RGIANT  (sqrt(MP_GIANT)*0.1)
  169 
  170 
  171 /* External function prototype declarations */
  172 extern int mpfit(mp_func funct, int m, int npar,
  173          double *xall, mp_par *pars, mp_config *config, 
  174          void *private_data, 
  175          mp_result *result);
  176 
  177 
  178 
  179 /* C99 uses isfinite() instead of finite() */
  180 #if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L
  181 #define mpfinite(x) isfinite(x)
  182 
  183 /* Microsoft C uses _finite(x) instead of finite(x) */
  184 #elif defined(_MSC_VER) && _MSC_VER
  185 #include <float.h>
  186 #define mpfinite(x) _finite(x)
  187 
  188 /* Default is to assume that compiler/library has finite() function */
  189 #else
  190 #define mpfinite(x) finite(x)
  191 
  192 #endif
  193 
  194 #ifdef __cplusplus
  195 } /* extern "C" */
  196 #endif
  197 
  198 #endif /* MPFIT_H */