<div class="gmail_quote"><br>Hi all, <br><br>I'm new in numpy. Actually, I'm new in Python. In order to learn a bit, I want to create a program to plot the Mandelbrot set. This program is quite simple, and I have already programmed it. The problem is that I come from fortran, so I use to think in "for" loops. I know that it is not the best way to use Python and in fact the performance of the program is more than poor. <br>
<br>Here is the program:<br><br><blockquote style="border-left:1px solid rgb(204, 204, 204);margin:0pt 0pt 0pt 0.8ex;padding-left:1ex" class="gmail_quote"><i>#!/<span style="background:yellow none repeat scroll 0% 0%">usr</span>/bin/python</i><br>
<br><i>import <span style="background:yellow none repeat scroll 0% 0%">numpy</span> as <span style="background:yellow none repeat scroll 0% 0%">np</span></i><br>
<i>import <span style="background:yellow none repeat scroll 0% 0%">matplotlib</span>.<span style="background:yellow none repeat scroll 0% 0%">pyplot</span> as <span style="background:yellow none repeat scroll 0% 0%">plt</span></i><br>
<br><i># Some parameters</i><br><i><span style="background:yellow none repeat scroll 0% 0%">Xmin</span>=-1.5</i><br>
<i><span style="background:yellow none repeat scroll 0% 0%">Xmax</span>=0.5</i><br>
<i><span style="background:yellow none repeat scroll 0% 0%">Ymin</span>=-1</i><br>
<i><span style="background:yellow none repeat scroll 0% 0%">Ymax</span>=1</i><br>
<br><i><span style="background:yellow none repeat scroll 0% 0%">Ds</span> = 0.01</i><br>
<br><i># Initialization of varibles</i><br><i>X = <span style="background:yellow none repeat scroll 0% 0%">np</span>.<span style="background:yellow none repeat scroll 0% 0%">arange</span>(<span style="background:yellow none repeat scroll 0% 0%">Xmin</span>,<span style="background:yellow none repeat scroll 0% 0%">Xmax</span>,<span style="background:yellow none repeat scroll 0% 0%">Ds</span>)</i><br>
<i>Y = <span style="background:yellow none repeat scroll 0% 0%">np</span>.<span style="background:yellow none repeat scroll 0% 0%">arange</span>(<span style="background:yellow none repeat scroll 0% 0%">Ymax</span>,<span style="background:yellow none repeat scroll 0% 0%">Ymin</span>,-<span style="background:yellow none repeat scroll 0% 0%">Ds</span>)</i><br>
<br><i>N = <span style="background:yellow none repeat scroll 0% 0%">np</span>.zeros((X.shape[0],Y.shape[0]),'f')</i><br>
<br><i>############## Here are inefficient the calculations ################</i><br><i>for i in range(X.shape[0]):</i><br><i> for j in range(Y.shape[0]):</i><br><i> z= complex(0.0, 0.0)</i><br><i> c = complex(X[i], Y[j])</i><br>
<i> while N[i, j] < 30 and abs(z) < 2:</i><br>
<i> N[i, j] += 1</i><br><i> z = z**2 + c</i><br><i> if N[i, j] == 29:</i><br><i> N[i, j]=0</i><br>####################################################<br><br><i># And now, just for ploting...</i><br><i>N = N.transpose()</i><br>
<i>fig = <span style="background:yellow none repeat scroll 0% 0%">plt</span>.figure()</i><br>
<i><span style="background:yellow none repeat scroll 0% 0%">plt</span>.<span style="background:yellow none repeat scroll 0% 0%">imshow</span>(N,<span style="background:yellow none repeat scroll 0% 0%">cmap</span>=<span style="background:yellow none repeat scroll 0% 0%">plt</span>.cm.Blues)</i><br>
<i><span style="background:yellow none repeat scroll 0% 0%">plt</span>.title('Mandelbrot set')</i><br>
<i><span style="background:yellow none repeat scroll 0% 0%">plt</span>.<span style="background:yellow none repeat scroll 0% 0%">xticks</span>([]); <span style="background:yellow none repeat scroll 0% 0%">plt</span>.<span style="background:yellow none repeat scroll 0% 0%">yticks</span>([])</i><br>
<i><span style="background:yellow none repeat scroll 0% 0%">plt</span>.show()</i><br>
<i>fig.<span style="background:yellow none repeat scroll 0% 0%">savefig</span>('test.png')</i><br>
</blockquote><br clear="all"><br>As you can see, it is very simple, but it takes several seconds running just to create a 200x200 plot. Fortran takes same time to create a 2000x2000 plot, around 100 times faster... So the question is, do you know how to programme this in a Python-like fashion in order to improve seriously the performance?<br>
<br>Thanks in advance</div><br>-- <br>Juan José Gómez Navarro<br><br>Edificio CIOyN, Campus de Espinardo, 30100<br>Departamento de Física <br>Universidad de Murcia<br>Tfno. (+34) 968 398552<br><br>Email: <a href="mailto:juanjo.gomeznavarro@gmail.com">juanjo.gomeznavarro@gmail.com</a><br>
Web: <a href="http://ciclon.inf.um.es/Inicio.html">http://ciclon.inf.um.es/Inicio.html</a><br>