SCalc
functions.hh
1 /*
2  function.hh, copyright (c) 2006 by Vincent Fourmond:
3  The (public) definition of functions.
4 
5  This program is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation; either version 2 of the License, or
8  (at your option) any later version.
9 
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU General Public License for more details (in the COPYING file).
14 
15 */
16 
17 namespace SCalc {
18 
19 
39  class FuncDef : public ParserResult {
40  protected:
41 
42  int _nb_params;
43 
50  std::string _name;
51  public:
52  FuncDef(Session * s, int nb) : ParserResult(s)
53  { _nb_params = nb; };
54 
56  virtual int is_func_def() { return 1;};
57 
59  virtual std::string pretty_print();
60 
62  int register_self();
63 
65  int nb_params() { return _nb_params;};
66 
70  void set_name(const char * name) { _name = std::string(name);};
71 
72  std::string name() { return _name;};
73 
74 
77  virtual double evaluate(const double * vars, const double * args) = 0;
78 
80  static void register_common_functions(Session * sess);
81  virtual ~FuncDef() {;};
82 
84  virtual FuncDef * derivative(int nb) = 0;
85 
87  virtual void destroy_anonymous_derivatives() {;};
88 
91  virtual int can_delete() { return _name.empty();};
92  };
93 
95  class CFunc : public FuncDef {
96  public:
98  typedef double (*c_function_t)(double);
99 
100  protected:
103 
108  public:
109  CFunc(Session * s, const char * n,
111  FuncDef * derivat = NULL);
112 
113  virtual ~CFunc() {;};
114 
116  virtual double evaluate(const double * vars, const double * args);
117 
120  void set_derivative(FuncDef * d) { deriv = d;};
121 
123  virtual void destroy_anonymous_derivatives();
124 
126  virtual FuncDef * derivative(int nb)
127  { if(nb) return NULL; return deriv; };
128  };
129 
131  class CFuncParam : public CFunc {
132  public:
134  typedef double (*c_function_t)(void *, double);
135 
136  protected:
139 
144 
146  void * _param;
147  public:
148  CFuncParam(Session * s, const char * n,
149  c_function_t func, void * param,
150  FuncDef * derivat = NULL);
151 
152  virtual ~CFuncParam() {;};
153 
155  virtual double evaluate(const double * vars, const double * args);
156 
157  void * param() { return _param;};
158  void set_param(void * p) { _param = p;};
159  };
160 
162  class ExprFunc : public FuncDef {
164  std::map<int, FuncDef *> cached_derivatives;
165 
166  Expression * exp;
167  public:
169  ExprFunc(Session * s, Expression * expr, int nb_args) :
170  FuncDef(s, nb_args) { exp = expr;};
171  virtual ~ExprFunc() { delete exp;};
172 
174  virtual void destroy_anonymous_derivatives();
175 
176  virtual FuncDef * derivative(int nb);
177 
179  virtual double evaluate(const double * vars,
180  const double * args)
181  { return exp->evaluate(vars, args);};
182 
183 
184  virtual std::string pretty_print();
185 
186  };
187 
188 };