QtiPlot 0.9.8.2
muParserScripting.h
Go to the documentation of this file.
00001 /***************************************************************************
00002     File                 : muParserScripting.h
00003     Project              : QtiPlot
00004     --------------------------------------------------------------------
00005 
00006     Copyright            : (C) 2006 by Ion Vasilief, Knut Franke
00007     Email (use @ for *)  : ion_vasilief*yahoo.fr, knut.franke*gmx.de
00008     Description          : Evaluate mathematical expressions using muParser
00009 
00010  ***************************************************************************/
00011 
00012 /***************************************************************************
00013  *                                                                         *
00014  *  This program is free software; you can redistribute it and/or modify   *
00015  *  it under the terms of the GNU General Public License as published by   *
00016  *  the Free Software Foundation; either version 2 of the License, or      *
00017  *  (at your option) any later version.                                    *
00018  *                                                                         *
00019  *  This program is distributed in the hope that it will be useful,        *
00020  *  but WITHOUT ANY WARRANTY; without even the implied warranty of         *
00021  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the          *
00022  *  GNU General Public License for more details.                           *
00023  *                                                                         *
00024  *   You should have received a copy of the GNU General Public License     *
00025  *   along with this program; if not, write to the Free Software           *
00026  *   Foundation, Inc., 51 Franklin Street, Fifth Floor,                    *
00027  *   Boston, MA  02110-1301  USA                                           *
00028  *                                                                         *
00029  ***************************************************************************/
00030 #ifndef MUPARSER_SCRIPTING_H
00031 #define MUPARSER_SCRIPTING_H
00032 
00033 #include "ScriptingEnv.h"
00034 #include "Script.h"
00035 #include "muParserScript.h"
00036 
00037 #include <muParser.h>
00038 #include "math.h"
00039 #include <gsl/gsl_sf.h>
00040 #include <gsl/gsl_cdf.h>
00041 #include <gsl/gsl_randist.h>
00042 #include <gsl/gsl_rng.h>
00043 
00045 class muParserScripting: public ScriptingEnv
00046 {
00047   Q_OBJECT
00048 
00049   public:
00050     static const char *langName;
00051     muParserScripting(ApplicationWindow *parent) : ScriptingEnv(parent, langName){
00052         d_initialized = true;
00053         gsl_set_error_handler_off();
00054     }
00055     static ScriptingEnv *constructor(ApplicationWindow *parent) { return new muParserScripting(parent); }
00056 
00057     bool isRunning() const { return true; }
00058     Script *newScript(const QString &code, QObject *context, const QString &name="<input>")
00059     {
00060       return new muParserScript(this, code, context, name);
00061     }
00062 
00063     // we do not support global variables
00064     bool setQObject(QObject*, const char*) { return false; }
00065     bool setInt(int, const char*) { return false; }
00066     bool setDouble(double, const char*) { return false; }
00067 
00068     const QStringList mathFunctions() const;
00069     const QString mathFunctionDoc (const QString &name) const;
00070 
00071     const static QStringList functionsList(bool tableContext = false);
00072     const static QString explainFunction(const QString &name);
00073 
00074     struct mathFunction
00075     {
00076       char *name;
00077       int numargs;
00078       double (*fun1)(double);
00079       double (*fun2)(double,double);
00080       double (*fun3)(double,double,double);
00081       QString description;
00082     };
00083     static const mathFunction math_functions[];
00084 
00085   private:
00086     static double rnd(double x){
00087         gsl_rng_default_seed = (unsigned int)x*time(NULL);
00088         const gsl_rng_type * T = gsl_rng_default;
00089         gsl_rng * r = gsl_rng_alloc (T);
00090         double u = gsl_rng_uniform (r);
00091         gsl_rng_free (r);
00092         return u;
00093     }
00094 
00095     static double normal(double x){
00096         const gsl_rng_type * T = gsl_rng_default;
00097         gsl_rng * r = gsl_rng_alloc (T);
00098         if (!r)
00099             return 0.0;
00100         gsl_rng_set(r, (unsigned int)x*time(NULL));
00101         double u = gsl_ran_ugaussian(r);
00102         gsl_rng_free (r);
00103         return u;
00104     }
00105 
00106     static double mod(double x, double y){ return fmod(x,y);};
00107     static double bessel_J0(double x){ return gsl_sf_bessel_J0 (x);};
00108     static double bessel_J1(double x){ return gsl_sf_bessel_J1 (x);};
00109     static double bessel_Jn(double x, double n){ return gsl_sf_bessel_Jn ((int)n, x);};
00110     static double bessel_Yn(double x, double n){ return gsl_sf_bessel_Yn ((int)n, x);};
00111     static double bessel_Jn_zero(double n, double s){ return gsl_sf_bessel_zero_Jnu(n, (unsigned int) s);};
00112     static double bessel_Y0(double x){ return gsl_sf_bessel_Y0 (x);};
00113     static double bessel_Y1(double x){ return gsl_sf_bessel_Y1 (x);};
00114     static double beta(double a, double b){ return gsl_sf_beta (a,b);};
00115     static double erf(double x){ return gsl_sf_erf (x);};
00116     static double erfc(double x){ return gsl_sf_erfc (x);};
00117     static double erf_Z(double x){ return gsl_sf_erf_Z (x);};
00118     static double erf_Q(double x){ return gsl_sf_erf_Q (x);};
00119     static double gamma(double x){ return gsl_sf_gamma (x);};
00120     static double lngamma(double x){ return gsl_sf_lngamma (x);};
00121     static double hazard(double x){ return gsl_sf_hazard (x);};
00122     static double lambert_W0(double x){ return gsl_sf_lambert_W0(x);};
00123     static double lambert_Wm1(double x){ return gsl_sf_lambert_Wm1(x);};
00124     static double invt(double x, double n){ return gsl_cdf_tdist_P(x, n);};
00125     static double ttable(double x, double n){ return gsl_cdf_tdist_Pinv(x, n);};
00126     static double ftable(double x, double m, double n){return gsl_cdf_fdist_Pinv(x, m, n);};
00127     static double invf(double x, double m, double n){return gsl_cdf_fdist_P(x, m, n);};
00128     static double gauss_pdf(double x, double sigma){return gsl_ran_gaussian_pdf (x, sigma);};
00129     static double gauss_cdf(double x, double sigma){return gsl_cdf_gaussian_P (x, sigma);};
00130     static double inv_gauss_cdf(double x, double sigma){return gsl_cdf_gaussian_Pinv(x, sigma);};
00131     static double normcdf(double x){return gsl_cdf_ugaussian_P(x);};
00132     static double norminv(double x){return gsl_cdf_ugaussian_Pinv(x);};
00133     static double chi2cdf(double x, double n){return gsl_cdf_chisq_P (x, n);};
00134     static double chi2inv(double x, double n){return gsl_cdf_chisq_Pinv(x, n);};
00135     static double dilog(double x){return gsl_sf_dilog(x);};
00136 };
00137 
00138 class EmptySourceError : public mu::ParserError
00139 {
00140     public:
00141         EmptySourceError() {}
00142 };
00143 
00144 #endif