Subject: Question regarding passing functions as arguments
From: Barton Willis
Date: Thu, 31 Aug 2006 08:39:34 -0500
maxima-bounces at math.utexas.edu wrote on 08/30/2006 11:28:32 PM:
> Thanks giovanni. I had made a mistake in defining my function with
> [] rather than (). Now it works. I also tried to verify with a
> simple example
> g(f,x):=block([h:f],diff(h(x),x));
> h(t):=t^3 + t;
> and it gives me result as expected.
> (%i24) g(h,t);
> 2
> (%o24) 3 t + 1
>
> I think I can pass function as arguments rather than passing
> expression that defines the function.
Suggestion: Test the function g with some other arguments. You
might decide that it doesn't work as you expected. Also, the function
gg might be closer to the function you want.
(%i1) g(f,x):=block([h:f],diff(h(x),x))$
(%i2) h(t):=t^3 + t$
(%i3) p(t) := sqrt(5) * t^2$
(%i4) g(p,x);
(%o4) 3*x^2+1
(%i5) gg(f,t) := diff(apply('f,[t]),t)$
(%i6) gg(p,x);
(%o6) 2*sqrt(5)*x
(%i7) gg(p,t);
(%o7) 2*sqrt(5)*t
Translating 'g' changes the way it evaluates. I think this bug
has been reported.
(%i8) translate(g,gg)$
(%i9) g(p,x);
(%o9) 2*sqrt(5)*x
(%i10) gg(p,x);
(%o10) 2*sqrt(5)*x
An identifier can be given multiple values. Consider:
(%i11) f : 5$
(%i12) f(x) := %e * x^2$
(%i13) f[1] : z$
(%i14) f[k] := %pi * k$
(%i15) sum(f(f[f] + f(f) + f),f,1,f);
(%o15)
%e*(z+%e+1)^2+%e*(5*%pi+25*%e+5)^2+%e*(4*%pi+16*%e+4)^2+%e*(3*%pi+9*%e+3)^2+%e*(2*%pi+4*%e+2)^2
Barton