# [Maxima] R

Andrej Vodopivec andrej.vodopivec at gmail.com
Sat Feb 27 14:43:19 CST 2010

```> (%i3)
> f(x,y,n) :=
> block([i:0,ER:4,iMax:n,x0:x,y0:y],
>  while abs(x)<ER and abs(y)<ER and i<iMax
>  do (x:x^2-y^2+x0,y:2.0*x*y+y0,i:i+1),
>  min(ER,abs(x),abs(y)))\$
> compile(f);
>
>
> (%i4)
> (%o4) [f]
> (%i5)
> (dx : 2.7/70, dy : dx) \$
>
>
> (%i6)
> for i:-2.0 thru 0.7 step dx do
> for j:-1.2 thru 1.2 step dy do
> f(i,j,4) \$
>
>
> (%i7) time(%o6);
>
> (%o7) [0.79]

This can be improved even more if you declare variable:

(%i1) f(x,y,n) :=
block([i:0,ER:4,iMax:n,x0:x,y0:y],
while abs(x)<ER and abs(y)<ER and i<iMax
do (x:x^2-y^2+x0,y:2.0*x*y+y0,i:i+1),
min(ER,sqrt(x^2+y^2)))\$
(%i2) dx: dy: 2.7/70\$
(%i3) compile(f);
(%o3) [f]
(%i4) for i:-2.0 thru 0.7 step dx do
for j:-1.2 thru 1.2 step dy do
f(i,j,4)\$
(%i5) time(%);
(%o5) [0.447]
(%i6) f(x,y,n) :=
block([i:0,ER:4,iMax:n,x0:x,y0:y],
mode_declare([x,y,x0,y0], float, [i,ER,iMax,n], integer),
while abs(x)<ER and abs(y)<ER and i<iMax
do (x:x^2-y^2+x0,y:2.0*x*y+y0,i:i+1),
min(ER,sqrt(x^2+y^2)))\$
(%i7) compile(f);
WARNING-> Assigning variable iMax, whose mode is fixnum,a value of mode any.
WARNING-> Assigning variable x0, whose mode is float,a value of mode any.
WARNING-> Assigning variable y0, whose mode is float,a value of mode any.
(%o7) [f]
(%i8) for i:-2.0 thru 0.7 step dx do
for j:-1.2 thru 1.2 step dy do
f(i,j,4)\$
(%i9) time(%);
(%o9) [0.065]

Andrej
```