Fractal Fern

[This article was first published on Frank Portman, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

I’m gonna share a short code snippet that I thought was interesting. This post is inspired by one of my engineering computation classes at Rice. The program initializes a pair of coordinates ‘z’ and iteratively updates z by matrix multiplication based on some random number generation criteria. After each successive coordinate update, the new ‘z’ is plotted.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
<span class="line"><span class="kn">library</span><span class="p">(</span>ggplot2<span class="p">)</span>
</span><span class="line">
</span><span class="line">z <span class="o"><-</span> <span class="kt">matrix</span><span class="p">(</span><span class="kt">c</span><span class="p">(</span><span class="m">0</span><span class="p">,</span> <span class="m">0</span><span class="p">),</span> nrow <span class="o">=</span> <span class="m">2</span><span class="p">)</span>
</span><span class="line">x <span class="o"><-</span> <span class="kt">c</span><span class="p">()</span>
</span><span class="line">y <span class="o"><-</span> <span class="kt">c</span><span class="p">()</span>
</span><span class="line">
</span><span class="line"><span class="kr">for</span> <span class="p">(</span>i <span class="kr">in</span> <span class="m">1</span><span class="o">:</span><span class="m">40000</span><span class="p">)</span> <span class="p">{</span>
</span><span class="line">
</span><span class="line">  r <span class="o"><-</span> runif<span class="p">(</span><span class="m">1</span><span class="p">)</span>
</span><span class="line">
</span><span class="line">  <span class="kr">if</span> <span class="p">(</span>r <span class="o"><</span> <span class="m">.01</span><span class="p">)</span> <span class="p">{</span>
</span><span class="line">    z <span class="o"><-</span> <span class="kt">matrix</span><span class="p">(</span><span class="kt">c</span><span class="p">(</span><span class="m">0</span><span class="p">,</span> <span class="m">0</span><span class="p">,</span> <span class="m">0</span><span class="p">,</span> <span class="m">.16</span><span class="p">),</span> nrow <span class="o">=</span> <span class="m">2</span><span class="p">,</span> byrow <span class="o">=</span> <span class="bp">T</span><span class="p">)</span> <span class="o">%*%</span> z
</span><span class="line">  <span class="p">}</span>
</span><span class="line">
</span><span class="line">  <span class="kr">else</span> <span class="kr">if</span> <span class="p">(</span>r <span class="o"><</span> <span class="m">.86</span><span class="p">)</span> <span class="p">{</span>
</span><span class="line">    z <span class="o"><-</span> <span class="kt">matrix</span><span class="p">(</span><span class="kt">c</span><span class="p">(</span><span class="m">.85</span><span class="p">,</span> <span class="m">.04</span><span class="p">,</span> <span class="m">-.04</span><span class="p">,</span> <span class="m">.85</span><span class="p">),</span> nrow <span class="o">=</span> <span class="m">2</span><span class="p">,</span> byrow <span class="o">=</span> <span class="bp">T</span><span class="p">)</span> <span class="o">%*%</span> z <span class="o">+</span> <span class="kt">c</span><span class="p">(</span><span class="m">0</span><span class="p">,</span> <span class="m">1.6</span><span class="p">)</span>
</span><span class="line">  <span class="p">}</span>
</span><span class="line">
</span><span class="line">  <span class="kr">else</span> <span class="kr">if</span> <span class="p">(</span>r <span class="o"><</span> <span class="m">.93</span><span class="p">)</span> <span class="p">{</span>
</span><span class="line">    z <span class="o"><-</span> <span class="kt">matrix</span><span class="p">(</span><span class="kt">c</span><span class="p">(</span><span class="m">0.2</span><span class="p">,</span> <span class="m">-.26</span><span class="p">,</span> <span class="m">.23</span><span class="p">,</span> <span class="m">.22</span><span class="p">),</span> nrow <span class="o">=</span> <span class="m">2</span><span class="p">,</span> byrow <span class="o">=</span> <span class="bp">T</span><span class="p">)</span> <span class="o">%*%</span> z <span class="o">+</span> <span class="kt">c</span><span class="p">(</span><span class="m">0</span><span class="p">,</span> <span class="m">1.6</span><span class="p">)</span>
</span><span class="line">  <span class="p">}</span>
</span><span class="line">
</span><span class="line">  <span class="kr">else</span> <span class="p">{</span>
</span><span class="line">    z <span class="o"><-</span> <span class="kt">matrix</span><span class="p">(</span><span class="kt">c</span><span class="p">(</span><span class="m">-.15</span><span class="p">,</span> <span class="m">.28</span><span class="p">,</span> <span class="m">.26</span><span class="p">,</span> <span class="m">.24</span><span class="p">),</span> nrow <span class="o">=</span> <span class="m">2</span><span class="p">,</span> byrow <span class="o">=</span> <span class="bp">T</span><span class="p">)</span> <span class="o">%*%</span> z <span class="o">+</span> <span class="kt">c</span><span class="p">(</span><span class="m">0</span><span class="p">,</span> <span class="m">.44</span><span class="p">)</span>
</span><span class="line">  <span class="p">}</span>
</span><span class="line">
</span><span class="line">  x<span class="p">[</span>i<span class="p">]</span> <span class="o"><-</span> z<span class="p">[</span><span class="m">1</span><span class="p">]</span>
</span><span class="line">  y<span class="p">[</span>i<span class="p">]</span> <span class="o"><-</span> z<span class="p">[</span><span class="m">2</span><span class="p">]</span>
</span><span class="line">
</span><span class="line"><span class="p">}</span>
</span><span class="line">
</span><span class="line">qplot<span class="p">(</span>x<span class="p">,</span> y<span class="p">,</span> size <span class="o">=</span> <span class="kp">I</span><span class="p">(</span><span class="m">.5</span><span class="p">))</span> <span class="o">+</span> theme_bw<span class="p">()</span> <span class="o">+</span> opts<span class="p">(</span>axis.line <span class="o">=</span> element_blank<span class="p">(),</span>
</span><span class="line">      axis.text.x <span class="o">=</span> element_blank<span class="p">(),</span> axis.text.y <span class="o">=</span> element_blank<span class="p">(),</span>
</span><span class="line">      axis.ticks <span class="o">=</span> element_blank<span class="p">(),</span> axis.title.x <span class="o">=</span> element_blank<span class="p">(),</span>
</span><span class="line">      axis.title.y <span class="o">=</span> element_blank<span class="p">())</span> <span class="o">+</span> scale_x_continuous<span class="p">(</span>breaks <span class="o">=</span> <span class="kc">NULL</span><span class="p">)</span> <span class="o">+</span>
</span><span class="line">      scale_y_continuous<span class="p">(</span>breaks <span class="o">=</span> <span class="kc">NULL</span><span class="p">)</span> <span class="o">+</span> ggtitle<span class="p">(</span><span class="s">"Fractal Fern"</span><span class="p">)</span>
</span><span class="line">	
</span>

Pretty.

To leave a comment for the author, please follow the link and comment on their blog: Frank Portman.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)