SCalc
|
00001 /* 00002 function.hh, copyright (c) 2006 by Vincent Fourmond: 00003 The (public) definition of functions. 00004 00005 This program is free software; you can redistribute it and/or modify 00006 it under the terms of the GNU General Public License as published by 00007 the Free Software Foundation; either version 2 of the License, or 00008 (at your option) any later version. 00009 00010 This program is distributed in the hope that it will be useful, 00011 but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00013 GNU General Public License for more details (in the COPYING file). 00014 00015 */ 00016 00017 namespace SCalc { 00018 00019 00039 class FuncDef : public ParserResult { 00040 protected: 00041 00042 int _nb_params; 00043 00050 std::string _name; 00051 public: 00052 FuncDef(Session * s, int nb) : ParserResult(s) 00053 { _nb_params = nb; }; 00054 00056 virtual int is_func_def() { return 1;}; 00057 00059 virtual std::string pretty_print(); 00060 00062 int register_self(); 00063 00065 int nb_params() { return _nb_params;}; 00066 00070 void set_name(const char * name) { _name = std::string(name);}; 00071 00072 std::string name() { return _name;}; 00073 00074 00077 virtual double evaluate(const double * vars, const double * args) = 0; 00078 00080 static void register_common_functions(Session * sess); 00081 virtual ~FuncDef() {;}; 00082 00084 virtual FuncDef * derivative(int nb) = 0; 00085 00087 virtual void destroy_anonymous_derivatives() {;}; 00088 00091 virtual int can_delete() { return _name.empty();}; 00092 }; 00093 00095 class CFunc : public FuncDef { 00096 public: 00098 typedef double (*c_function_t)(double); 00099 00100 protected: 00102 c_function_t func; 00103 00107 FuncDef * deriv; 00108 public: 00109 CFunc(Session * s, const char * n, 00110 c_function_t func, 00111 FuncDef * derivat = NULL); 00112 00113 virtual ~CFunc() {;}; 00114 00116 virtual double evaluate(const double * vars, const double * args); 00117 00120 void set_derivative(FuncDef * d) { deriv = d;}; 00121 00123 virtual void destroy_anonymous_derivatives(); 00124 00126 virtual FuncDef * derivative(int nb) 00127 { if(nb) return NULL; return deriv; }; 00128 }; 00129 00131 class CFuncParam : public CFunc { 00132 public: 00134 typedef double (*c_function_t)(void *, double); 00135 00136 protected: 00138 c_function_t func; 00139 00143 FuncDef * deriv; 00144 00146 void * _param; 00147 public: 00148 CFuncParam(Session * s, const char * n, 00149 c_function_t func, void * param, 00150 FuncDef * derivat = NULL); 00151 00152 virtual ~CFuncParam() {;}; 00153 00155 virtual double evaluate(const double * vars, const double * args); 00156 00157 void * param() { return _param;}; 00158 void set_param(void * p) { _param = p;}; 00159 }; 00160 00162 class ExprFunc : public FuncDef { 00164 std::map<int, FuncDef *> cached_derivatives; 00165 00166 Expression * exp; 00167 public: 00169 ExprFunc(Session * s, Expression * expr, int nb_args) : 00170 FuncDef(s, nb_args) { exp = expr;}; 00171 virtual ~ExprFunc() { delete exp;}; 00172 00174 virtual void destroy_anonymous_derivatives(); 00175 00176 virtual FuncDef * derivative(int nb); 00177 00179 virtual double evaluate(const double * vars, 00180 const double * args) 00181 { return exp->evaluate(vars, args);}; 00182 00183 00184 virtual std::string pretty_print(); 00185 00186 }; 00187 00188 };