~showard314/ubuntu/natty/qtiplot/Python2.7_fix

« back to all changes in this revision

Viewing changes to qtiplot/src/Fit.h

  • Committer: Bazaar Package Importer
  • Author(s): Gudjon I. Gudjonsson
  • Date: 2007-03-25 12:06:27 UTC
  • Revision ID: james.westby@ubuntu.com-20070325120627-5pvdufddr7i0r74x
Tags: upstream-0.9~rc2
ImportĀ upstreamĀ versionĀ 0.9~rc2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/***************************************************************************
 
2
    File                 : Fit.h
 
3
    Project              : QtiPlot
 
4
    --------------------------------------------------------------------
 
5
    Copyright            : (C) 2006 by Ion Vasilief, Tilman Hoener zu Siederdissen
 
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 "ApplicationWindow.h"
 
35
#include "Filter.h"
 
36
 
 
37
#include <gsl/gsl_multifit_nlin.h>
 
38
#include <gsl/gsl_multimin.h>
 
39
 
 
40
class Table;
 
41
class Matrix;
 
42
 
 
43
//! Fit base class
 
44
class Fit : public Filter
 
45
{
 
46
        Q_OBJECT
 
47
 
 
48
        public:
 
49
 
 
50
                typedef double (*fit_function_simplex)(const gsl_vector *, void *);
 
51
                typedef int (*fit_function)(const gsl_vector *, void *, gsl_vector *);
 
52
                typedef int (*fit_function_df)(const gsl_vector *, void *, gsl_matrix *);
 
53
                typedef int (*fit_function_fdf)(const gsl_vector *, void *, gsl_vector *, gsl_matrix *);
 
54
 
 
55
                enum Algorithm{ScaledLevenbergMarquardt, UnscaledLevenbergMarquardt, NelderMeadSimplex};
 
56
                enum WeightingMethod{NoWeighting, Instrumental, Statistical, Dataset};
 
57
 
 
58
                Fit(ApplicationWindow *parent, Graph *g = 0, const char * name = 0);
 
59
                ~Fit();
 
60
 
 
61
                //! Actually does the fit. Should be reimplemented in derived classes.
 
62
                virtual void fit();
 
63
        virtual bool run(){return false;};
 
64
 
 
65
                //! Sets the data set to be used for weighting
 
66
                bool setWeightingData(WeightingMethod w, const QString& colName = QString::null);
 
67
 
 
68
                void setDataCurve(int curve, double start, double end);
 
69
 
 
70
                QString formula(){return d_formula;};
 
71
                int numParameters() {return d_p;}
 
72
 
 
73
                void setInitialGuess(int parIndex, double val){gsl_vector_set(d_param_init, parIndex, val);};
 
74
                void setInitialGuesses(double *x_init);
 
75
 
 
76
                virtual void guessInitialValues(){};
 
77
 
 
78
                void setAlgorithm(Algorithm s){d_solver = s;};
 
79
 
 
80
                //! Specifies weather the result of the fit is a function curve
 
81
                void generateFunction(bool yes, int points = 100);
 
82
 
 
83
                //! Output string added to the plot as a new legend
 
84
                virtual QString legendInfo();
 
85
 
 
86
                //! Returns a vector with the fit results
 
87
                double* results(){return d_results;};
 
88
 
 
89
                //! Returns a vector with the standard deviations of the results
 
90
                double* errors();
 
91
 
 
92
                //! Returns the sum of squares of the residuals from the best-fit line
 
93
                double chiSquare() {return chi_2;};
 
94
 
 
95
                //! Returns R^2
 
96
                double rSquare();
 
97
 
 
98
                //! Specifies wheather the errors must be scaled with sqrt(chi_2/dof)
 
99
                void scaleErrors(bool yes = true){d_scale_errors = yes;};
 
100
 
 
101
                Table* parametersTable(const QString& tableName);
 
102
                Matrix* covarianceMatrix(const QString& matrixName);
 
103
 
 
104
        private:
 
105
                //! Pointer to the GSL multifit minimizer (for simplex algorithm)
 
106
                gsl_multimin_fminimizer * fitSimplex(gsl_multimin_function f, int &iterations, int &status);
 
107
 
 
108
                //! Pointer to the GSL multifit solver
 
109
                gsl_multifit_fdfsolver * fitGSL(gsl_multifit_function_fdf f, int &iterations, int &status);
 
110
 
 
111
                //! Customs and stores the fit results according to the derived class specifications. Used by exponential fits.
 
112
                virtual void storeCustomFitResults(double *par);
 
113
 
 
114
        protected:
 
115
                //! Adds the result curve as a FunctionCurve to the plot, if d_gen_function = true
 
116
                void insertFitFunctionCurve(const QString& name, double *x, double *y, int penWidth = 1);
 
117
 
 
118
                //! Adds the result curve to the plot
 
119
                virtual void generateFitCurve(double *par);
 
120
 
 
121
                //! Calculates the data for the output fit curve and store itin the X an Y vectors
 
122
                virtual void calculateFitCurveData(double *par, double *X, double *Y) { Q_UNUSED(par) Q_UNUSED(X) Q_UNUSED(Y)   };
 
123
 
 
124
                //! Output string added to the result log
 
125
                virtual QString logFitInfo(double *par, int iterations, int status, const QString& plotName);
 
126
 
 
127
                fit_function d_f;
 
128
                fit_function_df d_df;
 
129
                fit_function_fdf d_fdf;
 
130
                fit_function_simplex d_fsimplex;
 
131
 
 
132
                //! Number of fit parameters
 
133
                int d_p;
 
134
 
 
135
                //! Initial guesses for the fit parameters 
 
136
                gsl_vector *d_param_init;
 
137
 
 
138
                /*! \brief Tells whether the fitter uses non-linear/simplex fitting 
 
139
                 * with an initial parameters set, that must be freed in the destructor.
 
140
                 */
 
141
                bool is_non_linear;
 
142
 
 
143
                //! weighting data set used for the fit
 
144
                double *d_w;
 
145
 
 
146
                //! Names of the fit parameters
 
147
                QStringList d_param_names;
 
148
 
 
149
                //! Stores a list of short explanations for the significance of the fit parameters
 
150
                QStringList d_param_explain;
 
151
 
 
152
                //! Specifies weather the result curve is a FunctionCurve or a normal curve with the same x values as the fit data
 
153
                bool d_gen_function;
 
154
 
 
155
                //! Algorithm type
 
156
                Algorithm d_solver;
 
157
 
 
158
                //! The fit formula
 
159
                QString d_formula;
 
160
 
 
161
                //! Covariance matrix
 
162
                gsl_matrix *covar;
 
163
 
 
164
                //! The kind of weighting to be performed on the data
 
165
                WeightingMethod d_weihting;
 
166
 
 
167
                //! The name of the weighting dataset
 
168
                QString weighting_dataset;
 
169
 
 
170
                //! Stores the result parameters
 
171
                double *d_results;
 
172
 
 
173
                //! Stores standard deviations of the result parameters
 
174
                double *d_errors;
 
175
 
 
176
                //! The sum of squares of the residuals from the best-fit line
 
177
                double chi_2;
 
178
 
 
179
                //! Specifies wheather the errors must be scaled with sqrt(chi_2/dof)
 
180
                bool d_scale_errors;
 
181
};
 
182
 
 
183
#endif