/* Steven Andrews, 9/98 */
/* See documentation called BasisFn doc */
/* Copyright 2003 by Steven Andrews.  Permission is granted
   for non-commercial use of and modifications to the code. */

#ifndef __BasisFn_h
#define __BasisFn_h

#include "string2.h"
#include "Spectra.h"
#include "Set.h"

typedef struct modeltype	{
	char name[STRCHAR];
	char file[STRCHAR];
	char color[STRCHAR];
	sptr spec;
	sptr uncert;
	set basis;
	int np;
	float sigma;
	float xmin;
	float xmax;
	float dx;
	float *covar; }* modelptr;

typedef struct basisfn {
	char name[STRCHAR];
	char proc[STRCHAR];
	char color[STRCHAR];
	char desc[STRCHAR];
	float (*addr)(float x,float *param,sptr spec,float *deriv);
	modelptr model;
	int n;
	float *param;
	float *pold;
	float *paramlo;
	float *paramhi;
	char **eqn;
	char **pname;
	int *freeze;
	float *scratch;
	int isspec;
	sptr spec; }* basisptr;

basisptr BasisAlloc(int n);
void BasisFree(basisptr b);
modelptr ModelAlloc(sptr s);
void ModelFree(modelptr m);
int CheckBasis(basisptr b,modelptr m);
int CheckMSpec(modelptr m);
int CheckUncert(modelptr m);
int setupbasis();
void PlotBasis(basisptr b);
void PlotModel(modelptr m);
void BasisRange(basisptr b,float *xa,float *xb,float *ya,float *yb,int fn);
void ModelRange(modelptr m,float *xa,float *xb,float *ya,float *yb,int fn);
int CountFreParam(modelptr m,int **frzptr);
int FindBasisParam(basisptr *bptr,modelptr m,char* str);
int FindBasisParam2(basisptr *bptr,modelptr m,float* fltptr);
float ModelValue(float x,modelptr m);
float ModelValueD(float x,modelptr m,float *d);
void TypeBasis(basisptr b);
void TypeAllBasis();
void TypeModel(modelptr m);
basisptr BasisCopy(basisptr b);
modelptr ModelCopy(modelptr m);
basisptr AddBasis(basisptr b,char *bproc,sptr s,modelptr m);
void RemoveBasis(basisptr b);
int SaveModel(modelptr m);
int LoadModel(modelptr *mptr,char *name,sptr s);
int BasisMath(basisptr b,void *v,sptr *ansptr,float k,char *fn);
int ModelMath(modelptr m,void *v,sptr *ansptr,float k,char *fn);
void endbasis();
float rmserror(modelptr m);
float chisq(modelptr m,float ch2max);
float ChiSqD(modelptr m,float ch2max,float *alpha,float *beta,int np);
int prefit(modelptr m,float ***pptr,float **ploptr,float **phiptr);
int ModelCovar(modelptr m);
void unfit(modelptr m);
int RandomFit(modelptr m);
int LinearFit(modelptr m);
int LMFit(modelptr m);
int RandMultiFit(set s);

#endif