Subject: RE : passing functions as argument one more time
From: laurent couraud
Date: Fri, 6 Oct 2006 23:01:20 +0200
> -----Message d'origine-----
> De : maxima-bounces at math.utexas.edu
> [mailto:maxima-bounces at math.utexas.edu] De la part de amli at comcast.net
> Envoy? : vendredi 6 octobre 2006 21:15
> ? : maxima at math.utexas.edu
> Objet : [Maxima] passing functions as argument one more time
>
>
> 1. How can I make sure that the functions defined in global
> space do not conflict with passing of
> function arguments? For example I do not want f(x) defined
> outside the program conflict with passing the function f.
> (In a scipt file I may have many functions defined)
>
The only way that I know for that is to make the argument name "unconflictuable"
May be something like:
F(x_%_1):=...
Note that you can use: declare (char, alphabetic) to adds char to Maxima's alphabet.
>
> x:5;
> t:3;
> ff(f,x):=block([t,g,h],g:apply('f,[t]),if x<1 then h(x):=x
> else h(x):=3*x,ev(g,t=x)*h(2*x));
> ff(sin,2); //works
>
>
> f(x):=x^2;
> ff(f,x):=block([t,g,h],g:apply('f,[t]),if x<1 then h(x):=x
> else h(x):=3*x,ev(g,t=x)*h(2*x)); ff(sin,2); //does not work
>
> I tried this
> (x):=x^2;
> ff(f,x):=block([t,g,h],g:'apply('f,[t]),if x<1 then h(x):=x
> else h(x):=3*x,ev(g,t=x)*h(2*x)); ff(sin,2);
> but how do you get the final evaluation.
>
> 2. I also do not want the functions defined inside the
> program such as h have global scope. How can I restrict them
> to be local only. I suppose I could use the kill command.
> Is that the only solution?
>
You can embed function definition in block like this:
ff(f_%, x_%) := block(
[t, g, h],
g : apply('f_%,[t]),
if x_% < 1 then (
block(
h(x_%) := x_%,
return( ev(g, t = x_%) * h(2 * x_%) )
)
) else (
block(
h(x_%) := 3 * x_%,
return( ev(g, t = x_%) * h(2 * x_%) )
)
)
);
Laurent.