# [Maxima] displaying a function

Stavros Macrakis macrakis at alum.mit.edu
Wed Jan 9 09:40:27 CST 2008

```By the way, you should keep in mind that in Maxima, functions defined with a
subscript, e.g.

f[i](x) :=  ...

or

g[i] := ...

are memoized, that is, they save any calculated values for future use (to
avoid recalculation).

This is very useful to avoid exponentially many recalculations in double
recursions etc. (see example below).  BUT!!! You must be careful.  If you
change the definition of g, or if the value of g depends on parameters which
may change, the memoized values will no longer be correct: you need to
remarray(g) and redefine g in that case.

-s

Example of memoized function

fib(x):=(print(x),if x<1 then 1 else fib(x-1)+fib(x-2))\$   <<< not memoized
fb[x] := (print(x),if x<1 then 1 else fb[x-1]+fb[x-2])\$     <<< memoized

(%i15) fib(3);
3
2
1
0
- 1
0
1
0
- 1           <<<<< 9 calls to fib
(%o15)                                          5
(%i16) fib(3);
3
2
1
0
- 1
0
1
0
- 1          <<<<< still 9 calls
(%o16)                                          5
(%i17) fb[3];
3
2
1
0
- 1          <<<<  only 5 calls to fb, since some values are needed more
than once
(%o17)                                          5
(%i18) fb[3];
<<<<  no calls to fb because fb[3] is saved
(%o18)                                          5
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.math.utexas.edu/pipermail/maxima/attachments/20080109/d837c5f3/attachment.htm
```