composite function
Chris Dams
chrisd at sci.kun.nl
Fri Dec 12 12:25:05 CET 2003
Hello,
On Fri, 12 Dec 2003, Volker Vogel wrote:
> I'am trying to define composite functions,
> for example this one:
>
> / 1 for all x > 1
> f(x) =| x*x for all 1 >= x >= -1
> \ 1 for all x <-1
>
> Later I want to evaluate this function with different x.
> I tried to do this allready using
>
> symbol x("x");
> string polystr="if (abs(x)>1) {1}else{x*x}";
> ex poly;
> poly=ex(polystr,x);
> cout << poly;
Try the following untested-so-may-contain-errors code
DECLARE_FUNCTION_1P(f)
static ex f_eval(const ex&x)
{ if(is_a<numeric>(x))
return abs(x)<1 ? x*x : 1;
return f(x).hold();
}
REGISTER_FUNCTION(f,eval_func(f_eval))
Perhaps you may later want to add more smartness to your f_eval. Perhaps
you would want it to be capable of doing f(atan(x)) -> atan(x)^2. This
kind of thing can be added.
Good luck,
Chris Dams
More information about the GiNaC-list
mailing list