functions.hh
/*
function.hh, copyright (c) 2006 by Vincent Fourmond:
The (public) definition of functions.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details (in the COPYING file).
*/
namespace SCalc {
class FuncDef : public ParserResult {
protected:
int _nb_params;
std::string _name;
public:
FuncDef(Session * s, int nb) : ParserResult(s)
{ _nb_params = nb; };
virtual int is_func_def() { return 1;};
virtual std::string pretty_print();
int register_self();
int nb_params() { return _nb_params;};
void set_name(const char * name) { _name = std::string(name);};
std::string name() { return _name;};
virtual double evaluate(const double * vars, const double * args) = 0;
static void register_common_functions(Session * sess);
virtual ~FuncDef() {;};
virtual FuncDef * derivative(int nb) = 0;
virtual void destroy_anonymous_derivatives() {;};
virtual int can_delete() { return _name.empty();};
};
class CFunc : public FuncDef {
public:
typedef double (*c_function_t)(double);
protected:
c_function_t func;
FuncDef * deriv;
public:
CFunc(Session * s, const char * n,
c_function_t func,
FuncDef * derivat = NULL);
virtual ~CFunc() {;};
virtual double evaluate(const double * vars, const double * args);
void set_derivative(FuncDef * d) { deriv = d;};
virtual void destroy_anonymous_derivatives();
virtual FuncDef * derivative(int nb)
{ if(nb) return NULL; return deriv; };
};
class CFuncParam : public CFunc {
public:
typedef double (*c_function_t)(void *, double);
protected:
c_function_t func;
FuncDef * deriv;
void * _param;
public:
CFuncParam(Session * s, const char * n,
c_function_t func, void * param,
FuncDef * derivat = NULL);
virtual ~CFuncParam() {;};
virtual double evaluate(const double * vars, const double * args);
void * param() { return _param;};
void set_param(void * p) { _param = p;};
};
class ExprFunc : public FuncDef {
std::map<int, FuncDef *> cached_derivatives;
Expression * exp;
public:
ExprFunc(Session * s, Expression * expr, int nb_args) :
FuncDef(s, nb_args) { exp = expr;};
virtual ~ExprFunc() { delete exp;};
virtual void destroy_anonymous_derivatives();
virtual FuncDef * derivative(int nb);
virtual double evaluate(const double * vars,
const double * args)
{ return exp->evaluate(vars, args);};
virtual std::string pretty_print();
};
};
SCalc::ExprFunc
Definition: functions.hh:162
SCalc::CFuncParam::deriv
FuncDef * deriv
Definition: functions.hh:143
SCalc::Session
A class representing a whole session.
Definition: session.hh:75
SCalc::CFunc::derivative
virtual FuncDef * derivative(int nb)
Gets the derivative.
Definition: functions.hh:126
SCalc::Expression
An expression !
Definition: expression.hh:131
SCalc::ExprFunc::evaluate
virtual double evaluate(const double *vars, const double *args)
The function doing the actual job... pretty easy, isn't it ?
Definition: functions.hh:179
SCalc::CFunc::func
c_function_t func
The C function to be called.
Definition: functions.hh:102
SCalc::CFuncParam
Definition: functions.hh:131
SCalc::FuncDef::can_delete
virtual int can_delete()
Definition: functions.hh:91
SCalc::FuncDef::derivative
virtual FuncDef * derivative(int nb)=0
The derivative with regards to the argument nb.
SCalc::FuncDef::register_common_functions
static void register_common_functions(Session *sess)
This function registers common functions to the given session.
SCalc::CFunc::evaluate
virtual double evaluate(const double *vars, const double *args)
The function doing the actual job...
SCalc::CFunc::deriv
FuncDef * deriv
Definition: functions.hh:107
SCalc::ParserResult
The result of an SCalc::Session::eval().
Definition: expression.hh:36
SCalc::FuncDef::set_name
void set_name(const char *name)
Definition: functions.hh:70
SCalc::ExprFunc::destroy_anonymous_derivatives
virtual void destroy_anonymous_derivatives()
Delete the derivative if anonymous.
SCalc::ExprFunc::derivative
virtual FuncDef * derivative(int nb)
The derivative with regards to the argument nb.
SCalc::CFunc::destroy_anonymous_derivatives
virtual void destroy_anonymous_derivatives()
Delete the derivative if anonymous.
SCalc::ExprFunc::pretty_print
virtual std::string pretty_print()
Pretty printing of the result ?
SCalc::FuncDef::pretty_print
virtual std::string pretty_print()
Pretty printing of the result ?
SCalc::CFuncParam::evaluate
virtual double evaluate(const double *vars, const double *args)
The function doing the actual job...
SCalc::CFunc::set_derivative
void set_derivative(FuncDef *d)
Definition: functions.hh:120
SCalc::Expression::evaluate
virtual double evaluate(const double *values, const double *s=NULL)=0
Evaluate the expression.
SCalc::CFunc
Definition: functions.hh:95
SCalc::FuncDef::_name
std::string _name
Definition: functions.hh:50
SCalc::CFuncParam::_param
void * _param
The parameter !
Definition: functions.hh:146
SCalc::CFunc::c_function_t
double(* c_function_t)(double)
The type of arguments it accepts.
Definition: functions.hh:98
SCalc::FuncDef
A function definition with any number of parameters.
Definition: functions.hh:39
SCalc::CFuncParam::func
c_function_t func
The C function to be called.
Definition: functions.hh:138
SCalc::FuncDef::is_func_def
virtual int is_func_def()
Yes, this is a function definition.
Definition: functions.hh:56
SCalc
Definition: session.hh:45
SCalc::ExprFunc::ExprFunc
ExprFunc(Session *s, Expression *expr, int nb_args)
construction of the function.
Definition: functions.hh:169
SCalc::CFuncParam::c_function_t
double(* c_function_t)(void *, double)
The type of arguments it accepts.
Definition: functions.hh:134
SCalc::FuncDef::register_self
int register_self()
Register the function to the session if it has a name.
SCalc::FuncDef::destroy_anonymous_derivatives
virtual void destroy_anonymous_derivatives()
Delete the derivative if anonymous.
Definition: functions.hh:87
SCalc::FuncDef::evaluate
virtual double evaluate(const double *vars, const double *args)=0
SCalc::FuncDef::nb_params
int nb_params()
The number of params the function takes.
Definition: functions.hh:65