QtiPlot  0.9.8.2
muParserScripting.h
Go to the documentation of this file.
1 /***************************************************************************
2  File : muParserScripting.h
3  Project : QtiPlot
4  --------------------------------------------------------------------
5 
6  Copyright : (C) 2006 by Ion Vasilief, Knut Franke
7  Email (use @ for *) : ion_vasilief*yahoo.fr, knut.franke*gmx.de
8  Description : Evaluate mathematical expressions using muParser
9 
10  ***************************************************************************/
11 
12 /***************************************************************************
13  * *
14  * This program is free software; you can redistribute it and/or modify *
15  * it under the terms of the GNU General Public License as published by *
16  * the Free Software Foundation; either version 2 of the License, or *
17  * (at your option) any later version. *
18  * *
19  * This program is distributed in the hope that it will be useful, *
20  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
22  * GNU General Public License for more details. *
23  * *
24  * You should have received a copy of the GNU General Public License *
25  * along with this program; if not, write to the Free Software *
26  * Foundation, Inc., 51 Franklin Street, Fifth Floor, *
27  * Boston, MA 02110-1301 USA *
28  * *
29  ***************************************************************************/
30 #ifndef MUPARSER_SCRIPTING_H
31 #define MUPARSER_SCRIPTING_H
32 
33 #include "ScriptingEnv.h"
34 #include "Script.h"
35 #include "muParserScript.h"
36 
37 #include <muParser.h>
38 #include "math.h"
39 #include <gsl/gsl_sf.h>
40 #include <gsl/gsl_cdf.h>
41 #include <gsl/gsl_randist.h>
42 #include <gsl/gsl_rng.h>
43 
46 {
47  Q_OBJECT
48 
49  public:
50  static const char *langName;
52  d_initialized = true;
53  gsl_set_error_handler_off();
54  }
55  static ScriptingEnv *constructor(ApplicationWindow *parent) { return new muParserScripting(parent); }
56 
57  bool isRunning() const { return true; }
58  Script *newScript(const QString &code, QObject *context, const QString &name="<input>")
59  {
60  return new muParserScript(this, code, context, name);
61  }
62 
63  // we do not support global variables
64  bool setQObject(QObject*, const char*) { return false; }
65  bool setInt(int, const char*) { return false; }
66  bool setDouble(double, const char*) { return false; }
67 
68  const QStringList mathFunctions() const;
69  const QString mathFunctionDoc (const QString &name) const;
70 
71  const static QStringList functionsList(bool tableContext = false);
72  const static QString explainFunction(const QString &name);
73 
74  struct mathFunction
75  {
76  char *name;
77  int numargs;
78  double (*fun1)(double);
79  double (*fun2)(double,double);
80  double (*fun3)(double,double,double);
81  QString description;
82  };
83  static const mathFunction math_functions[];
84 
85  private:
86  static double rnd(double x){
87  gsl_rng_default_seed = (unsigned int)x*time(NULL);
88  const gsl_rng_type * T = gsl_rng_default;
89  gsl_rng * r = gsl_rng_alloc (T);
90  double u = gsl_rng_uniform (r);
91  gsl_rng_free (r);
92  return u;
93  }
94 
95  static double normal(double x){
96  const gsl_rng_type * T = gsl_rng_default;
97  gsl_rng * r = gsl_rng_alloc (T);
98  if (!r)
99  return 0.0;
100  gsl_rng_set(r, (unsigned int)x*time(NULL));
101  double u = gsl_ran_ugaussian(r);
102  gsl_rng_free (r);
103  return u;
104  }
105 
106  static double mod(double x, double y){ return fmod(x,y);};
107  static double bessel_I0(double x){ return gsl_sf_bessel_I0 (x);};
108  static double bessel_I1(double x){ return gsl_sf_bessel_I1 (x);};
109  static double bessel_In(double x, double n){ return gsl_sf_bessel_In ((int)n, x);};
110  static double bessel_J0(double x){ return gsl_sf_bessel_J0 (x);};
111  static double bessel_J1(double x){ return gsl_sf_bessel_J1 (x);};
112  static double bessel_Jn(double x, double n){ return gsl_sf_bessel_Jn ((int)n, x);};
113  static double bessel_Yn(double x, double n){ return gsl_sf_bessel_Yn ((int)n, x);};
114  static double bessel_Jn_zero(double n, double s){ return gsl_sf_bessel_zero_Jnu(n, (unsigned int) s);};
115  static double bessel_Y0(double x){ return gsl_sf_bessel_Y0 (x);};
116  static double bessel_Y1(double x){ return gsl_sf_bessel_Y1 (x);};
117  static double beta(double a, double b){ return gsl_sf_beta (a,b);};
118  static double erf(double x){ return gsl_sf_erf (x);};
119  static double erfc(double x){ return gsl_sf_erfc (x);};
120  static double erf_Z(double x){ return gsl_sf_erf_Z (x);};
121  static double erf_Q(double x){ return gsl_sf_erf_Q (x);};
122  static double gamma(double x){ return gsl_sf_gamma (x);};
123  static double lngamma(double x){ return gsl_sf_lngamma (x);};
124  static double hazard(double x){ return gsl_sf_hazard (x);};
125  static double lambert_W0(double x){ return gsl_sf_lambert_W0(x);};
126  static double lambert_Wm1(double x){ return gsl_sf_lambert_Wm1(x);};
127  static double invt(double x, double n){ return gsl_cdf_tdist_P(x, n);};
128  static double ttable(double x, double n){ return gsl_cdf_tdist_Pinv(x, n);};
129  static double ftable(double x, double m, double n){return gsl_cdf_fdist_Pinv(x, m, n);};
130  static double invf(double x, double m, double n){return gsl_cdf_fdist_P(x, m, n);};
131  static double gauss_pdf(double x, double sigma){return gsl_ran_gaussian_pdf (x, sigma);};
132  static double gauss_cdf(double x, double sigma){return gsl_cdf_gaussian_P (x, sigma);};
133  static double inv_gauss_cdf(double x, double sigma){return gsl_cdf_gaussian_Pinv(x, sigma);};
134  static double normcdf(double x){return gsl_cdf_ugaussian_P(x);};
135  static double norminv(double x){return gsl_cdf_ugaussian_Pinv(x);};
136  static double chi2cdf(double x, double n){return gsl_cdf_chisq_P (x, n);};
137  static double chi2inv(double x, double n){return gsl_cdf_chisq_Pinv(x, n);};
138  static double dilog(double x){return gsl_sf_dilog(x);};
139 };
140 
141 class EmptySourceError : public mu::ParserError
142 {
143  public:
145 };
146 
147 #endif