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