Cannot use "diff" in a function assignment.



Daniel Carrera wrote:
> Hello,
>
> I'm pretty new to Maxima and I wonder why the following doesn't work:
>
> (%i1) f(x) := x^3 + 2*x^2 + x - 1;
> (%o1) ...
>
> (%i2) df(x) := diff( f(x), x )
> (%o2) ...
>
> (%i3) df(2);
> diff: second argument must be a variable; found 2
>
>

maxima functions are very bizarre objects, in particular evaluation is
considerably deferred. In the case above i think that df(x) remains
diff(f(x),x) unevaluated, so that when you compute df(2) you get
diff(f(2),2) producing the error message you see: the second argument of diff
is 2 and not a variable.

I think this is because maxima functions are represented internally by mdefine
which is "defined" via defmspec, a device which doesn't evaluate the
arguments. This causes all sorts of problems when you want to do anything non
trivial with maxima functions. Using expressions is far easier. For example if
you write
(%i11) y: df(x);
                                   2
(%o11)                          3 x  + 4 x + 1
(%i12) y,x=2;
(%o12)                                21


>
> I don't understand why this is a problem. It seems like a fairly natural
> thing to want to do - differentiate a function and use the result as a
> new function. Strange, because I seem to be able to use diff(f(x),x) in
> other similar contexts:
>
> (%i4) linsolve( [diff( f(x),x) = 2], [x] );
> (%o4) [ x = 1/4]
>
> (%i5) plot2d( diff(f(x),x), [x,-1,1] );
> (%o5)
>

plot passes the functions to be plotted via coerce-float-fun which does all
sorts of contortions to evaluate the maxima functions. The typical invocation
which does that in plot.lisp looks like:
 (catch 'errorsw
             (,float-fun
              (maybe-realpart (mapply ',expr (list , at gensym-args) t))))))
Besides using new arguments to the functions this runs mapply which is a
fundamental maxima evaluation procedure, takes the real part of the result if
it is complex and coerces the result to float. I suppose linsolve does
something similar i have not checked.


>
> Any ideas?
>
> Cheers,
> Daniel.

-- 
Michel Talon