1 | // This may look like C code, but it is really -*- C++ -*-
|
---|
2 | #ifndef GENERALFIT_SEEN
|
---|
3 | #define GENERALFIT_SEEN
|
---|
4 |
|
---|
5 | #include "exceptions.h"
|
---|
6 | #include "matrix.h"
|
---|
7 | #include "cvector.h"
|
---|
8 | #include "generaldata.h"
|
---|
9 |
|
---|
10 | //================================================================
|
---|
11 | // GeneralFunction
|
---|
12 | //================================================================
|
---|
13 |
|
---|
14 | class GeneralFunction EXC_AWARE {
|
---|
15 | public:
|
---|
16 | GeneralFunction(unsigned int nVar, unsigned int nPar);
|
---|
17 | virtual ~GeneralFunction();
|
---|
18 |
|
---|
19 | virtual double Value(double const xp[], double const* parm)=0;
|
---|
20 | virtual double Val_Der(double const xp[], double const* parm
|
---|
21 | , double* DgDpar);
|
---|
22 |
|
---|
23 | void SetDeltaParm(int numPar, double delta=0.);
|
---|
24 | void SetDeltaParm(double const* dparam);
|
---|
25 |
|
---|
26 | inline int NVar() const {return mNVar;}
|
---|
27 | inline int NPar() const {return mNPar;}
|
---|
28 |
|
---|
29 | protected:
|
---|
30 | const int mNVar; // nombre de variables f(x,y,z,...)
|
---|
31 | const int mNPar; // nombre de parametres
|
---|
32 |
|
---|
33 | double *deltaParm;
|
---|
34 | double *tmpParm;
|
---|
35 | };
|
---|
36 |
|
---|
37 | //================================================================
|
---|
38 | // GeneralFunc
|
---|
39 | //================================================================
|
---|
40 |
|
---|
41 | class GeneralFunc : public GeneralFunction {
|
---|
42 | public:
|
---|
43 | GeneralFunc(unsigned int nvar, unsigned int npar
|
---|
44 | ,double (*fun)(double const*,double const*)
|
---|
45 | ,double (*funder)(double const*, double const*, double*)=NULL);
|
---|
46 | virtual ~GeneralFunc();
|
---|
47 |
|
---|
48 | virtual double Value(double const xp[], double const* Par);
|
---|
49 | virtual double Val_Der(double const xp[],double const* parm
|
---|
50 | , double* DgDpar);
|
---|
51 |
|
---|
52 | protected:
|
---|
53 | double (*tmpFun) (double const*, double const*);
|
---|
54 | double (*tmpFunDer)(double const*, double const*, double*);
|
---|
55 | };
|
---|
56 |
|
---|
57 | //================================================================
|
---|
58 | // GeneralXi2
|
---|
59 | //================================================================
|
---|
60 |
|
---|
61 | class GeneralFitData;
|
---|
62 |
|
---|
63 | class GeneralXi2 EXC_AWARE {
|
---|
64 | public:
|
---|
65 | GeneralXi2(unsigned int nPar);
|
---|
66 | virtual ~GeneralXi2();
|
---|
67 |
|
---|
68 | virtual double Value(GeneralFitData& data, double* parm, int& ndataused)=0;
|
---|
69 | virtual double Derivee(GeneralFitData& data, int i, double* parm);
|
---|
70 | virtual double Derivee2(GeneralFitData& data, int i,int j, double* parm);
|
---|
71 |
|
---|
72 | void SetDeltaParm(int numPar, double delta=0.);
|
---|
73 | void SetDeltaParm(double const* dparam);
|
---|
74 |
|
---|
75 | inline int NPar() const {return mNPar;}
|
---|
76 |
|
---|
77 | protected:
|
---|
78 | const int mNPar; // nombre de parametres
|
---|
79 |
|
---|
80 | double *deltaParm;
|
---|
81 | };
|
---|
82 |
|
---|
83 | //================================================================
|
---|
84 | // GENERALFIT
|
---|
85 | //================================================================
|
---|
86 |
|
---|
87 | class GeneralFit EXC_AWARE {
|
---|
88 | public:
|
---|
89 | GeneralFit(GeneralFunction* f);
|
---|
90 | GeneralFit(GeneralXi2* f);
|
---|
91 | ~GeneralFit();
|
---|
92 |
|
---|
93 | void WriteStep(char *filename = NULL);
|
---|
94 | void SetDebug(int level = 0);
|
---|
95 | void SetMaxStep(int n = 100);
|
---|
96 | void SetLambda_Fac(double fac = 10.);
|
---|
97 | void SetStopChi2(double s = 0.01);
|
---|
98 | void SetEps(double ep = 1.e-8);
|
---|
99 | void SetEps(int n,double ep = 1.e-8);
|
---|
100 | void SetStopMx(int nstopmx = 3, double stopchi2 = -1.);
|
---|
101 | void SetStopLent(int nstoplent = 3);
|
---|
102 | void SetFunction(GeneralFunction*);
|
---|
103 | void SetFuncXi2(GeneralXi2*);
|
---|
104 | void SetData(GeneralFitData*);
|
---|
105 | void SetParam(int n,double value, double step
|
---|
106 | ,double min=1., double max=-1.);
|
---|
107 | void SetParam(int n,string const&
|
---|
108 | ,double value, double step
|
---|
109 | ,double min=1., double max=-1.);
|
---|
110 | void SetParam(int n,double value);
|
---|
111 | void SetStep(int n,double value);
|
---|
112 | void SetMinStepDeriv(int i,double val = 0.);
|
---|
113 | void SetMinStepDeriv(double val = 0.);
|
---|
114 | void SetBound(int n,double min,double max);
|
---|
115 | void SetBound(int n);
|
---|
116 | void SetUnBound(int n);
|
---|
117 | void SetUnBound();
|
---|
118 | void SetFix(int n,double v);
|
---|
119 | void SetFix(int n);
|
---|
120 | void SetFree(int n);
|
---|
121 | void SetFree();
|
---|
122 |
|
---|
123 | double GetParm(int);
|
---|
124 | Vector GetParm();
|
---|
125 | double GetParmErr(int);
|
---|
126 | double GetCoVar(int,int);
|
---|
127 | double GetStep(int n);
|
---|
128 | double GetMax(int n);
|
---|
129 | double GetMin(int n);
|
---|
130 | inline double GetChi2() const {return Chi2;};
|
---|
131 | inline double GetChi2Red() const {
|
---|
132 | if(mNddl<=0) return (double) mNddl;
|
---|
133 | return Chi2/(double) mNddl;
|
---|
134 | };
|
---|
135 | inline double GetEps(int i) const {return Eps(i);};
|
---|
136 | inline int GetNddl() const {return mNddl;};
|
---|
137 | inline int GetNStep() const {return nStep;};
|
---|
138 | inline int GetNVar() const {return mNVar;};
|
---|
139 | inline int GetNPar() const {return mNPar;};
|
---|
140 | inline int GetNFree() const {return mNParFree;};
|
---|
141 | inline int GetNBound() const {return mNParBound;};
|
---|
142 | inline int GetNStop() const {return nStop;};
|
---|
143 | inline int GetNStopLent() const {return nStopLent;};
|
---|
144 | inline GeneralFunction* GetFunction() const {return mFunction;};
|
---|
145 | inline GeneralFitData* GetGData() const {return mData;};
|
---|
146 |
|
---|
147 | void PrintStatus();
|
---|
148 | void PrintFit();
|
---|
149 | void PrintParm(int n);
|
---|
150 | void PrintParm();
|
---|
151 |
|
---|
152 | int Fit();
|
---|
153 | double ReCalChi2(int& nddl, double* par = NULL);
|
---|
154 | GeneralFitData* DataResidus(bool clean=true);
|
---|
155 | GeneralFitData* DataFunction(bool clean=true);
|
---|
156 | void PrintFitErr(int rc);
|
---|
157 |
|
---|
158 | protected:
|
---|
159 | int mNtry; // numero d'appel de la routine de fit.
|
---|
160 | int mNVar; // nombre de variables f(x,y,z,...)
|
---|
161 | int mNPar; // nombre de parametres
|
---|
162 | int mNParFree; // nombre de parametres libres
|
---|
163 | int mNParBound; // nombre de parametres bornes
|
---|
164 | GeneralFunction* mFunction;
|
---|
165 | GeneralXi2* mFuncXi2;
|
---|
166 | GeneralFitData* mData;
|
---|
167 |
|
---|
168 | Vector Param;
|
---|
169 | Vector errParam;
|
---|
170 | Vector stepParam;
|
---|
171 | Vector minParam;
|
---|
172 | Vector maxParam;
|
---|
173 | Vector minStepDeriv;
|
---|
174 | Vector Eps;
|
---|
175 | unsigned short int* fixParam;
|
---|
176 | unsigned short int* boundParam;
|
---|
177 | string* nameParam;
|
---|
178 |
|
---|
179 | double Lambda_Fac;
|
---|
180 | double stopChi2;
|
---|
181 | int maxStep;
|
---|
182 | int nStopMx;
|
---|
183 | double stopChi2SMx;
|
---|
184 | int nStopLent;
|
---|
185 | int debugLevel;
|
---|
186 | FILE *FileStep;
|
---|
187 |
|
---|
188 | Matrix ATGA;
|
---|
189 | Vector BETA;
|
---|
190 | Matrix ATGA_Try;
|
---|
191 | Vector BETA_Try;
|
---|
192 | Vector C;
|
---|
193 | Vector D;
|
---|
194 |
|
---|
195 | double Chi2;
|
---|
196 | int mNddl;
|
---|
197 | int nStep;
|
---|
198 | int nStop, nStopL;
|
---|
199 | double Lambda;
|
---|
200 |
|
---|
201 | // Fonctions privees
|
---|
202 | void write_in_step(double ci2,Vector& par);
|
---|
203 | void General_Init(void);
|
---|
204 | void TryFunc(Vector& par,Vector& par_tr);
|
---|
205 | void TryXi2(Vector& par,Vector& par_tr);
|
---|
206 | void CheckSanity();
|
---|
207 | void Set_Bound_C_D(int i);
|
---|
208 | void Set_Bound_C_D();
|
---|
209 | double p_vers_tr(int i,double p);
|
---|
210 | Vector p_vers_tr(Vector const& p);
|
---|
211 | void p_vers_tr(Vector const& p,Vector& tr);
|
---|
212 | double tr_vers_p(int i,double tr);
|
---|
213 | Vector tr_vers_p(Vector const& tr);
|
---|
214 | void tr_vers_p(Vector const& tr,Vector& p);
|
---|
215 | double c_dp_vers_dtr(int i,double tr);
|
---|
216 | Vector dp_vers_dtr(Vector const& dp,Vector const& tr);
|
---|
217 | void dp_vers_dtr(Vector const& dp,Vector const& tr,Vector& dtr);
|
---|
218 | double c_dtr_vers_dp(int i,double tr);
|
---|
219 | Vector dtr_vers_dp(Vector const& dtr,Vector const& tr);
|
---|
220 | int put_in_limits_for_deriv(Vector const& p,Vector& dp,double dist=0.66);
|
---|
221 | inline void dtr_vers_dp(Vector const& dtr,Vector const& tr,Vector& dp)
|
---|
222 | { for(int i=0;i<mNPar;i++)
|
---|
223 | { if( fixParam[i] ) continue;
|
---|
224 | if( ! boundParam[i] ) dp(i) = dtr(i);
|
---|
225 | else dp(i) = D(i)/(1.+tr(i)*tr(i)) * dtr(i); }
|
---|
226 | };
|
---|
227 | };
|
---|
228 |
|
---|
229 | #endif
|
---|