newbie question: howto evaluate functions only once



On Friday 02 October 2009, Barton Willis wrote:
> I think you want an array function:
>
> (%i1) f[a,b] := (print("computing"), a + b);
> (%o1) f[a,b]:=(print(computing),a+b)
>
> (%i2) f[3,4];
> computing
> (%o2) 7
>
> (%i3) f[3,4];
> (%o3) 7
>
> For more information, enter: ? functions;

Thanks Barton, that did work.

Andre

>
> Barton
>
> -----maxima-bounces at math.utexas.edu wrote: -----
>
> >To: maxima at math.utexas.edu
> >From: andre maute <andre.maute at gmx.de>
> >Sent by: maxima-bounces at math.utexas.edu
> >Date: 10/01/2009 04:03PM
> >Subject: newbie question: howto evaluate functions only once
> >
> >What is the standard maxima way to avoid additional function evaluation,
> >
> >if the set of the function parameters is apriori known to be small?
> >
> >
> >
> >For numbers I use the following method
> >
> >
> >
> >--------------------------------------------------
> >
> >~$ maxima -b evalonce.max
> >
> >Maxima 5.19.1 http://maxima.sourceforge.net
> >
> >Using Lisp SBCL 1.0.18.debian
> >
> >Distributed under the GNU Public License. See the file COPYING.
> >
> >Dedicated to the memory of William Schelter.
> >
> >The function bug_report() provides bug reporting information.
> >
> >(%i1)                         batch(evalonce.max)
> >
> >
> >
> >batching /home/user/evalonce.max
> >
> >(%i2)                          display2d : false
> >
> >(%o2) false
> >
> >(%i3) f(x):=block(print("hello"),return(x))
> >
> >(%i4) if not constantp(h[2]) then block(h[2]:f(2))
> >
> >hello
> >
> >(%o4) 2
> >
> >(%i5) if not constantp(h[2]) then block(h[2]:f(2))
> >
> >(%o5) false
> >
> >(%i6) if not constantp(h[3]) then block(h[3]:f(3))
> >
> >hello
> >
> >(%o6) 3
> >
> >(%i7) if not constantp(h[3]) then block(h[3]:f(3))
> >
> >(%o7) false
> >
> >(%i8) arrayinfo(h)
> >
> >(%o8) [hashed,1,[2],[3]]
> >
> >(%i9) listarray(h)
> >
> >(%o9) [2,3]
> >
> >--------------------------------------------------
> >
> >
> >
> >As one can see "hello" is printed only two and not four times.
> >
> >But what should one do if the output of f is more complicated,
> >
> >e.g. expressions with sin and cos functions?
> >
> >
> >
> >For me it would be enough,
> >
> >if I could decide if something has already been assigned to e.g. h[2].
> >
> >With constantp I can do this only for constants.
> >
> >
> >
> >Andre
> >
> >
> >
> >
> >
> >
> >
> >_______________________________________________
> >Maxima mailing list
> >Maxima at math.utexas.edu
> >http://www.math.utexas.edu/mailman/listinfo/maxima