### RE: using functions as arguments

Viktor T. Toth <vttoth <at> vttoth.com>

2004-12-02 13:51:43 GMT

You want to make sure that the differential operator is executed AFTER f is
substituted but BEFORE the substitution for x has been made. I.e., when you
call Nf(sin,3), you want to make sure that at one point, diff(sin(x),x) (and
NOT diff(sin(8),8) or diff(f(8),8)) gets evaluated.
I'm sure there's a more elegant way, but the following does the trick and is
not terribly ugly:
(%i1)
Nf(f,a):=block([u,v],fd(x):=''(diff(f(x),x)),u:ev(fd(v),diff),ev(u,v=a+5))$
(%i2) Nf(sin,3);
(%o2) cos(8)
(%i3) Nf(sin,x);
(%o3) cos(x + 5)
The idea is to evaluate fd(x) first with a dummy (non-numeric) argument that
causes the differentiation to be carried out, and only then substitute the
actual argument that could be a number or expression that cannot serve as
the second argument to diff().
Come to think of it, there is a more elegant way, one that doesn't even use
a "local" function:
(%i4) Nf(f,a):=block([x,fd],fd:diff(f(x),x),ev(fd,x=a+5))$
(%i5) Nf(sin,3);
(%o5) cos(8)
The reason why this works is that fd being an assignment gets evaluated with
f having its proper value, but x being a dummy parameter, so the
differentiation is carried out with respect to x BEFORE the call is made to

(Continue reading)