/*
 * apf_nr.h
 *
 *  Created on: Mar 13, 2025
 *      Author: tm
 */

#ifndef APF_NUMBR_H_
#define APF_NUMBR_H_



//
// Computes a least squares fit to a given linear model function as published in Numerical Recipes R in C, chapter 15.4.
//
//int     LinFit(double **CoefVals, double *LambdaVals, double *AbsVals, double *AbsFitVals, double *ResVals, double *FitVals, ushort fit_pixels, ushort fit_concs, ushort bsl_model);

void    LinModelFuncs(double x[], double afunc[], double **Ecoefs, int i, ushort fitconcs, ushort bslmodel);

#ifdef MOVEDTOMATH
int    _lfit(double x[], double y[], double yfit[], double sig[], double **Ecoefs, double a[],
        int ia[], int ma, double **covar, double *chisq, ushort fitpixels, ushort fitconcs, ushort bslmodel,
        void (*funcs)(double [], double [], double **, int, ushort, ushort));
#endif


//
// Computes a non-linear least squares fit to a given model using Levenberg-Marquardt as published in Numerical Recipes in C, chapter 15.5.
//
int     NLinFit(double **CoefVals, double *LambdaVals, double *AbsVals, double *AbsFitVals, double *ResVals, double *FitVals, ushort fit_pixels, ushort fit_concs, ushort bsl_model);

void    NLinModelFuncs(double x[], double a[], double yfit[], double dyda[], double **Ecoefs, int i, ushort fitconcs, ushort bslmodel);

int     _mrqmin(double x[], double y[], double yfit[], double sig[], double **Ecoefs, double a[],
        int ia[], int ma, double **covar, double *chisq, double *alamda, double **alpha, ushort fitpixels, ushort fitconcs, ushort bslmodel,
        void (*funcs)(double [], double [], double [], double [], double **, int, ushort, ushort) );

void    _mrqcof(double x[], double y[], double yfit[], double sig[], double **Ecoefs, double a[],
        int ia[], int ma, double **alpha, double beta[], double *chisq, ushort fitpixels, ushort fitconcs, ushort bslmodel,
        void (*funcs)(double [], double [], double [], double [], double **, int, ushort, ushort) );

//
// Numerical Recipes in C support functions.
//
void    _covsrt(double **covar, int ma, int ia[], int mfit);
void    _gcf(double *gammcf, double a, double x, double *gln);
void    _gser(double *gamser, double a, double x, double *gln);
int     _gaussj(double **a, int n, double **b, int m);
double  _gammq(double a, double x);
double  _gammp(double a, double x);
double  _gammln(double xx);

void nrerror(char error_text[]);
double **dmatrix(long nrl, long nrh, long ncl, long nch);
int *ivector(long nl, long nh);
double *dvector(long nl, long nh);
void free_ivector(int *v, long nl, long nh);
void free_dvector(double *v, long nl, long nh);
void free_dmatrix(double **m, long nrl, long nrh, long ncl, long nch);

static float sqrarg;
#define SQR(a) ((sqrarg=(a)) == 0.0 ? 0.0 : sqrarg*sqrarg)

static double dsqrarg;
#define DSQR(a) ((dsqrarg=(a)) == 0.0 ? 0.0 : dsqrarg*dsqrarg)

static double dmaxarg1,dmaxarg2;
#define DMAX(a,b) (dmaxarg1=(a),dmaxarg2=(b),(dmaxarg1) > (dmaxarg2) ?\
        (dmaxarg1) : (dmaxarg2))

static double dminarg1,dminarg2;
#define DMIN(a,b) (dminarg1=(a),dminarg2=(b),(dminarg1) < (dminarg2) ?\
        (dminarg1) : (dminarg2))

static float maxarg1,maxarg2;
#define FMAX(a,b) (maxarg1=(a),maxarg2=(b),(maxarg1) > (maxarg2) ?\
        (maxarg1) : (maxarg2))

static float minarg1,minarg2;
#define FMIN(a,b) (minarg1=(a),minarg2=(b),(minarg1) < (minarg2) ?\
        (minarg1) : (minarg2))

static long lmaxarg1,lmaxarg2;
#define LMAX(a,b) (lmaxarg1=(a),lmaxarg2=(b),(lmaxarg1) > (lmaxarg2) ?\
        (lmaxarg1) : (lmaxarg2))

static long lminarg1,lminarg2;
#define LMIN(a,b) (lminarg1=(a),lminarg2=(b),(lminarg1) < (lminarg2) ?\
        (lminarg1) : (lminarg2))

static int imaxarg1,imaxarg2;
#define IMAX(a,b) (imaxarg1=(a),imaxarg2=(b),(imaxarg1) > (imaxarg2) ?\
        (imaxarg1) : (imaxarg2))

static int iminarg1,iminarg2;
#define IMIN(a,b) (iminarg1=(a),iminarg2=(b),(iminarg1) < (iminarg2) ?\
        (iminarg1) : (iminarg2))

#define SIGN(a,b) ((b) >= 0.0 ? fabs(a) : -fabs(a))

#endif /* APF_NUMBR_H_ */
