Shorter way of using define?



The short version is

(%i2) f(x):= x^2;
(%o2) f(x):=x^2
(%i3) f1(x):= ''( diff(f(x),x) );
(%o3) f1(x):=2*x

Two single quotes!


Since this is a frequently asked question, here some background
information:

Consider this example:
f(k):= for n:0 thru k do print(n) $
Here it is not possible to evaluate the right side when entering the
expression:
(%i0) for n:0 thru k do print(n) $
Unable to evaluate predicate 0 > k
 -- an error.  To debug this try debugmode(true);

As a consequence of this the expression on the right side of a function
definition is generally not evaluated and not simplified when entered.
The right side is taken as it is. Evaluation (and simplification) is
done at runtime:
(%i1) f(k):= for n:0 thru k do print(n) $
(%i2) f(3)$
0 
1 
2 
3 
 
The following lines show this behaviour more in detail:

(%i1) a:3;
(%o1)                                  3
(%i2) %+a+x+x;
(%o2)                               2 x + 6
(%i3) 3;
(%o3)                                  3
(%i4) f(x):= %+a+x+x;
(%o4)                        f(x) := % + a + x + x
(%i5) a:7;
(%o5)                                  7
(%i6) f(x);
(%o6)                              2 x + 14
(%i7) f(x);
(%o7)                              4 x + 21


So if you type in
f(x):= x^2; 
and
f1(x):= diff(f(x),x);
then
f1(7);
is the same as 
diff(f(7),7);
(variable substitution is done before evaluation!)
which obviously runs into an error.

The double quote now enforces the evaluation of an expression: 
(example from above)

(%i2) f(x):= x^2;
(%o2) f(x):=x^2
(%i3) f1(x):= ''( diff(f(x),x) );
(%o3) f1(x):=2*x

The output in %o3 shows that the intended evaluation has been done.


The double quote is designed for interaction with Maxima in the top
level command line. In programs you can't use the double quote. Here you
use 'define'.


Volker van Nek
 

Am Samstag, den 20.06.2009, 08:44 +0100 schrieb Leo Butler:
> 
> On Fri, 19 Jun 2009, Stefan Meyer 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);
>  
> See this thread:
> http://www.math.utexas.edu/pipermail/maxima/2009/017562.html
> 
> Note that the Maxima email archive is searchable.
> 
> Leo
>