"define" and ":=" perform the same function, but the arguments are handled
differently.
In the case of ':=', both arguments are unevaluated. This is the normal way
function definitions work in most programming languages.
In the case of 'define', the second argument is evaluated normally, not
unevaluated. This allows you to *calculate* a definition. (The first
argument is evaluated in a peculiarly baroque way (see ? define), but let's
leave that aside.)
To see the difference vividly:
(%i1) f(x):=print(x)$
(%i2) define(g(x),print(x))$
x <<<< print(x) is evaluated at *definition* time
(%i3) dispfun(f)$ look at the definition
(%t3) f(x):=print(x) the definition includes the 'print'
(%i4) dispfun(g)$
(%t4) g(x):=x the definition includes the *value* of print(x),
namely x
(%i5) f(5);
5 <<< prints argument at runtime
(%o5) 5
(%i6) g(5);
(%o6) 5
'define' is useful for calculating function definitions which will later be
applied to arguments. For example, compare:
define(f(x), optimize(horner(taylor(tanh(x),x,0,8),x)))
f(.1) => 0.09966...
g(x) := optimize(horner(taylor(tanh(x),x,0,8),x))
g(.1) => ERROR (because it is trying to do taylor(tanh(.1),.1,0,8) )
There is a shortcut using '' (two single-quotes), but I don't recommend it.
-s
On Fri, Jun 19, 2009 at 4:46 PM, Stefan Meyer <even_steven at web.de> wrote:
> Hi,
>
> I'm new to Maxima and I wonder if I can write the
> define statement in a shorter way.
>
> This version works:
>
> (%i1) f(x):=x^2;
> (%o1) f(x):=x^2
> (%i3) define(f1(x),diff(f(x),x,1));
> (%o3) f1(x):=2*x
> (%i4) f1(1);
> (%o4) 2
>
> Instead of (%i3) I tried the following which caused
> an error:
>
> (%i10) g(x):=diff(f(x),x,1);
> (%o10) g(x):=diff(f(x),x,1)
> (%i11) g(1);
> diff: second argument must be a variable; found 1
> #0: g(x=1)
> -- an error. To debug this try debugmode(true);
>
> Is there a shorter way of defining a new function?
>
>
> Thank you
>
> Stefan
> ______________________________________________________
> GRATIS f?r alle WEB.DE-Nutzer: Die maxdome Movie-FLAT!
> Jetzt freischalten unter http://movieflat.web.de
>
>
> _______________________________________________
> Maxima mailing list
> Maxima at math.utexas.edu
> http://www.math.utexas.edu/mailman/listinfo/maxima
>