SciDAVis  1.D4
Fit.h
Go to the documentation of this file.
1 /***************************************************************************
2  File : Fit.h
3  Project : SciDAVis
4  --------------------------------------------------------------------
5  Copyright : (C) 2006 by Ion Vasilief, Tilman Benkert
6  Email (use @ for *) : ion_vasilief*yahoo.fr, thzs*gmx.net
7  Description : Fit base class
8 
9  ***************************************************************************/
10 
11 /***************************************************************************
12  * *
13  * This program is free software; you can redistribute it and/or modify *
14  * it under the terms of the GNU General Public License as published by *
15  * the Free Software Foundation; either version 2 of the License, or *
16  * (at your option) any later version. *
17  * *
18  * This program is distributed in the hope that it will be useful, *
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
21  * GNU General Public License for more details. *
22  * *
23  * You should have received a copy of the GNU General Public License *
24  * along with this program; if not, write to the Free Software *
25  * Foundation, Inc., 51 Franklin Street, Fifth Floor, *
26  * Boston, MA 02110-1301 USA *
27  * *
28  ***************************************************************************/
29 #ifndef FIT_H
30 #define FIT_H
31 
32 #include <QObject>
33 
34 #include "Filter.h"
35 #include "Script.h"
36 
37 #include <gsl/gsl_multifit_nlin.h>
38 #include <gsl/gsl_multimin.h>
39 
40 class Table;
41 class Matrix;
42 class ApplicationWindow;
43 class Script;
44 
46 class Fit : public Filter, public scripted
47 {
48  Q_OBJECT
49 
50  public:
51 
52  typedef double (*fit_function_simplex)(const gsl_vector *, void *);
53  typedef int (*fit_function)(const gsl_vector *, void *, gsl_vector *);
54  typedef int (*fit_function_df)(const gsl_vector *, void *, gsl_matrix *);
55  typedef int (*fit_function_fdf)(const gsl_vector *, void *, gsl_vector *, gsl_matrix *);
56 
59 
60  Fit(ApplicationWindow *parent, Graph *g = 0, const char * name = 0);
61  ~Fit();
62 
64  virtual void fit();
65 
67  bool setYErrorSource(ErrorSource err, const QString& colName = QString::null, bool fail_silently=false);
68 
69  void setDataCurve(int curve, double start, double end);
70 
71  QString formula(){return d_formula;};
72  int numParameters() {return d_p;}
73 
74  void setInitialGuess(int parIndex, double val){gsl_vector_set(d_param_init, parIndex, val);};
75  void setInitialGuesses(double *x_init);
76 
77  virtual void guessInitialValues(){};
78 
80 
82  void generateFunction(bool yes, int points = 100);
83 
85  virtual QString legendInfo();
86 
88  double* results(){return d_results;};
89 
91  double* errors();
92 
94  double chiSquare() {return chi_2;};
95 
97  double rSquare();
98 
100  void scaleErrors(bool yes = true){d_scale_errors = yes;};
101 
102  Table* parametersTable(const QString& tableName);
103  Matrix* covarianceMatrix(const QString& matrixName);
104 
105  int evaluate_f(const gsl_vector * x, gsl_vector * f);
106  double evaluate_d(const gsl_vector * x);
107  int evaluate_df(const gsl_vector *x, gsl_matrix *J);
108  static double evaluate_df_helper(double x, void * param);
109 
110  protected slots:
111  void scriptError(const QString& message,const QString& script_name,int line_number);
112 
113  private:
115  double * fitGslMultimin(int &iterations, int &status);
116 
118  double * fitGslMultifit(int &iterations, int &status);
119 
121  virtual void storeCustomFitResults(double *par);
122 
123  protected:
125  void insertFitFunctionCurve(const QString& name, double *x, double *y, int penWidth = 1);
126 
128  virtual void generateFitCurve(double *par);
129 
131  virtual void calculateFitCurveData(double *par, double *X, double *Y) { Q_UNUSED(par) Q_UNUSED(X) Q_UNUSED(Y) };
132 
134  virtual QString logFitInfo(double *par, int iterations, int status, const QString& plotName);
135 
140 
142  int d_p;
143 
145  gsl_vector *d_param_init;
146 
151 
153  double *d_y_errors;
154 
156  QStringList d_param_names;
157 
159  QStringList d_param_explain;
160 
163 
166 
168  QString d_formula;
169 
171  gsl_matrix *covar;
172 
175 
178 
180  double *d_results;
181 
184 
186  double chi_2;
187 
190 
193 };
194 
195 #endif