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:
102  c_function_t func;
103 
108  public:
109  CFunc(Session * s, const char * n,
110  c_function_t func,
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:
138  c_function_t func;
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 };
Definition: functions.hh:162
FuncDef * deriv
Definition: functions.hh:143
A class representing a whole session.
Definition: session.hh:75
virtual FuncDef * derivative(int nb)
Gets the derivative.
Definition: functions.hh:126
An expression !
Definition: expression.hh:131
virtual double evaluate(const double *vars, const double *args)
The function doing the actual job... pretty easy, isn&#39;t it ?
Definition: functions.hh:179
c_function_t func
The C function to be called.
Definition: functions.hh:102
Definition: functions.hh:131
virtual int can_delete()
Definition: functions.hh:91
virtual FuncDef * derivative(int nb)=0
The derivative with regards to the argument nb.
static void register_common_functions(Session *sess)
This function registers common functions to the given session.
FuncDef * deriv
Definition: functions.hh:107
The result of an SCalc::Session::eval().
Definition: expression.hh:36
void set_name(const char *name)
Definition: functions.hh:70
virtual std::string pretty_print()
Pretty printing of the result ?
void set_derivative(FuncDef *d)
Definition: functions.hh:120
virtual double evaluate(const double *values, const double *s=NULL)=0
Evaluate the expression.
Definition: functions.hh:95
std::string _name
Definition: functions.hh:50
void * _param
The parameter !
Definition: functions.hh:146
A function definition with any number of parameters.
Definition: functions.hh:39
c_function_t func
The C function to be called.
Definition: functions.hh:138
virtual int is_func_def()
Yes, this is a function definition.
Definition: functions.hh:56
Definition: session.hh:45
ExprFunc(Session *s, Expression *expr, int nb_args)
construction of the function.
Definition: functions.hh:169
int register_self()
Register the function to the session if it has a name.
virtual void destroy_anonymous_derivatives()
Delete the derivative if anonymous.
Definition: functions.hh:87
virtual double evaluate(const double *vars, const double *args)=0
int nb_params()
The number of params the function takes.
Definition: functions.hh:65