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();
};
};