newbie question: howto evaluate functions only once
Subject: newbie question: howto evaluate functions only once
From: andre maute
Date: Thu, 1 Oct 2009 23:03:38 +0200
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