R




On Sat, 27 Feb 2010, Mario Rodriguez wrote:

< 
< > Thx. It works good.
< > When I try to make image for n from 1 to 20 then it takes houres.
< > In R code runs faster ( few minutes).
< > What is the reason ?
< > 
< > Adam
< 
< Adam, 
< 
< 
< (%i4) f(x,y,n) :=
<  block([i:0, c:x+y*%i,ER:4,iMax:n,z:0],
<  while abs(z)<ER and i<iMax
<     do (z:z*z + c,i:i+1),
<  min(ER,abs(z)))$
< 
< (%i5) dx : 2.7/70 $
< 
< (%i6) dy : 2.4/70 $
< 
< (%i7) for i:-2 thru 0.7 step dx do
<   for j:-1.2 thru 1.2 step dy do
<     f(i,j,4) $
< 
< (%i8) time(%o7);
< (%o8)                             [84.30927]
< 
< It takes about 84 sec to calculate all the points for only one frame.
< 
< 
< 
< If you load the following lisp function, things look certainly better:
< 
< (defun $g (x y n)
<   (let ((er 4.0)
<         (c (complex ($float x) ($float y)))
<         (z (complex 0.0 0.0)))
<     (dotimes (i n)
<       (setf z (+ (* z z) c)))
<     (min er (abs z)) ))
< 
< 
< 
< 
< (%i13) for i:-2 thru 0.7 step dx do
<   for j:-1.2 thru 1.2 step dy do
<     g(i,j,4) $
< (%i14) time(%o13);
< (%o14)                             [0.46803]
 

I don't think users should need to learn lisp in order to do
quick computations. Indeed, Maxima does a better job if you
do the following:

-change the computation z:z*z+c into two computations of the
real and imaginary parts
-compile the function

See %o7 & %o10 below. The compiled Maxima function runs faster.

Leo


(%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]

------------
(%i8)
to_lisp();
(defun $f (x y n)
  (let ((er 4.0)
      (c (complex ($float x) ($float y)))
      (z (complex 0.0 0.0)))
    (dotimes (i n)
      (setf z (+ (* z z) c)))
    (min er (abs z)) ))
(to-maxima)

Type (to-maxima) to restart, ($quit) to quit Maxima.

MAXIMA>
$F
MAXIMA> Returning to Maxima
(%o8) true
(%i9)
for i:-2.0 thru 0.7 step dx do
for j:-1.2 thru 1.2 step dy do
f(i,j,4) $


(%i10) time(%o9);

(%o10) [0.84]

-- 
The University of Edinburgh is a charitable body, registered in
Scotland, with registration number SC005336.