
/* Linear and NonLinear Model Based Least Squares Curve Fitting (of absorbance spectra) - function prototypes */


#define FITVALUES   10              // Use to dimension an array of type double
#define SALINITY    0               // Returned Salinity  Value from Fitting
#define NITRATE     1               // Returned Nitrate   Value from Fitting
#define BISULFIDE   2               // Returned BiSulfide Value from Fitting
#define BASELINE_A  3               // Returned Baseline a Coef from Fitting
#define BASELINE_B  4               // Returned Baseline b Coef from Fitting
#define BASELINE_C  5               // Returned Baseline c Coef from Fitting
#define FIT_ERROR   6               // Returned RMS ABS Err Val from Fitting
#define FIT_CHISQ   7               // Returned CHISQ Error Val from Fitting
#define FIT_GAMMQ   8               // Returned Fit Quality Val from Fitting


//
// 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);

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));

//
// 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);
