<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Algorithm Blogs &#187; julia</title>
	<atom:link href="http://www.algorithm.co.il/blogs/index.php/tag/julia/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.algorithm.co.il/blogs</link>
	<description>Algorithms, for the heck of it</description>
	<lastBuildDate>Thu, 22 Apr 2010 21:04:32 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Fractals in 10 minutes No. 4: Mandelbrot and Julia in Ascii Art</title>
		<link>http://www.algorithm.co.il/blogs/index.php/programming/python/fractals-in-10-minutes-no-4-mandelbrot-and-julia-in-ascii-art/</link>
		<comments>http://www.algorithm.co.il/blogs/index.php/programming/python/fractals-in-10-minutes-no-4-mandelbrot-and-julia-in-ascii-art/#comments</comments>
		<pubDate>Wed, 10 Dec 2008 10:06:36 +0000</pubDate>
		<dc:creator>lorg</dc:creator>
				<category><![CDATA[Fractals]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[ascii-art]]></category>
		<category><![CDATA[fractal]]></category>
		<category><![CDATA[julia]]></category>
		<category><![CDATA[mandelbrot]]></category>

		<guid isPermaLink="false">http://www.algorithm.co.il/blogs/?p=145</guid>
		<description><![CDATA[I felt like it's about time I tackled the Mandelbrot and Julia sets, in Ascii-Art. Heck, the Mandelbrot fractal is on the logo of this Blog! However, being the most well-known fractal, this issue was tackled already, with satisfactory results.
Still, I took the ten minutes required to draw them in Python, using numpy:


def get_mandelbrot&#40;x, num_iter [...]]]></description>
			<content:encoded><![CDATA[<p>I felt like it's about time I tackled the <a href="http://en.wikipedia.org/wiki/Mandelbrot_set">Mandelbrot and Julia sets</a>, in Ascii-Art. Heck, the Mandelbrot fractal is on the logo of this Blog! However, being the most well-known fractal, this issue was <a href="http://bc.tech.coop/blog/040811.html">tackled</a> <a href="http://deems.wordpress.com/2008/11/19/howto-generate-a-mandelbrot-using-ascii-art-using-tsql/">already</a>, with satisfactory results.</p>
<p>Still, I took the ten minutes required to draw them in Python, using numpy:</p>
<div class="syntax_hilite">
<div id="python-3">
<div class="python"><span style="color: #00007f;font-weight:bold;">def</span> get_mandelbrot<span style="color: black;">&#40;</span>x, num_iter = <span style="color: #ff4500;">10</span><span style="color: black;">&#41;</span>:<br />
&nbsp; &nbsp; c = x<br />
&nbsp; &nbsp; <span style="color: #00007f;font-weight:bold;">for</span> i <span style="color: #00007f;font-weight:bold;">in</span> <span style="color: #008000;">range</span><span style="color: black;">&#40;</span>num_iter<span style="color: black;">&#41;</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; x = x**<span style="color: #ff4500;">2</span> + c<br />
&nbsp; &nbsp; <span style="color: #00007f;font-weight:bold;">return</span> <span style="color: #008000;">abs</span><span style="color: black;">&#40;</span>x<span style="color: black;">&#41;</span>&gt;<span style="color: #ff4500;">2</span></p>
<p><span style="color: #00007f;font-weight:bold;">def</span> get_julia<span style="color: black;">&#40;</span>x, c, num_iter = <span style="color: #ff4500;">10</span><span style="color: black;">&#41;</span>:<br />
&nbsp; &nbsp; <span style="color: #00007f;font-weight:bold;">for</span> i <span style="color: #00007f;font-weight:bold;">in</span> <span style="color: #008000;">range</span><span style="color: black;">&#40;</span>num_iter<span style="color: black;">&#41;</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; x = x**<span style="color: #ff4500;">2</span> + c<br />
&nbsp; &nbsp; <span style="color: #00007f;font-weight:bold;">return</span> <span style="color: #008000;">abs</span><span style="color: black;">&#40;</span>x<span style="color: black;">&#41;</span>&gt;<span style="color: #ff4500;">2</span></div>
</div>
</div>
<p></p>
<p>"Hey!" you might say, "There's no loop in here!". Indeed, with numpy loops can sometimes be avoided, when using arrays. When the expression x**2 + c is applied to an array x, it is applied element-wise, allowing for implicit loops. The actual magic happens in the following function:</p>
<div class="syntax_hilite">
<div id="python-4">
<div class="python"><span style="color: #00007f;font-weight:bold;">def</span> get_board<span style="color: black;">&#40;</span>bottom_left, top_right, num_x, num_y<span style="color: black;">&#41;</span>:<br />
&nbsp; &nbsp; x0, y0 = bottom_left<br />
&nbsp; &nbsp; x1, y1 = top_right<br />
&nbsp; &nbsp; x_values = numpy.<span style="color: #000000;">arange</span><span style="color: black;">&#40;</span>x0, x1, <span style="color: black;">&#40;</span>x1-x0<span style="color: black;">&#41;</span>/<span style="color: #008000;">float</span><span style="color: black;">&#40;</span>num_x<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; y_values = numpy.<span style="color: #000000;">arange</span><span style="color: black;">&#40;</span>y0, y1, <span style="color: black;">&#40;</span>y1-y0<span style="color: black;">&#41;</span>/<span style="color: #008000;">float</span><span style="color: black;">&#40;</span>num_y<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #00007f;font-weight:bold;">return</span> numpy.<span style="color: #dc143c;">array</span><span style="color: black;">&#40;</span><span style="color: black;">&#91;</span><span style="color: black;">&#91;</span>x+1j*y <span style="color: #00007f;font-weight:bold;">for</span> x <span style="color: #00007f;font-weight:bold;">in</span> x_values<span style="color: black;">&#93;</span> <span style="color: #00007f;font-weight:bold;">for</span> y <span style="color: #00007f;font-weight:bold;">in</span> y_values<span style="color: black;">&#93;</span><span style="color: black;">&#41;</span></div>
</div>
</div>
<p></p>
<p>The result of get_board will be used as the "x input" later on. It should be noted though that while that's a cute trick this time, it might grow unhandy for more complicated computations. For example, making each element to reflect the iteration number on which it "escaped".<br />
So, here are the results:</p>
<pre>
############################   ##############
###########################   ###############
##########################     ##############
#####################                ########
####################                #########
############ #######                 ########
############                         ########
#########                           #########
#########                           #########
############                         ########
############ #######                 ########
####################                #########
#####################                ########
##########################     ##############
###########################   ###############

############################################################
############################################################
############################################################
############################ ###############################
######################### ##   #############################
############## #######           ###########################
######## ##      ###              ##########  ##############
#########  ###        ###         ######       #############
##############       ######         ###        ###  ########
###############  ##########              ###      ## #######
############################           ####### #############
##############################   ## ########################
################################ ###########################
############################################################
############################################################
</pre>
<p>Here's the <a href="http://www.algorithm.co.il/sitecode/mandelbrot.zip">code</a>. (With the usual bsd-license slapped on top of it :)</p>
]]></content:encoded>
			<wfw:commentRss>http://www.algorithm.co.il/blogs/index.php/programming/python/fractals-in-10-minutes-no-4-mandelbrot-and-julia-in-ascii-art/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
