<?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>Master Baboon &#187; AdvancedMath</title>
	<atom:link href="http://www.masterbaboon.com/tag/advancedmath/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.masterbaboon.com</link>
	<description>The sea of the simulation</description>
	<lastBuildDate>Sun, 01 Aug 2010 16:53:18 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Advanced math functions for AS3</title>
		<link>http://www.masterbaboon.com/2009/02/advanced-math-functions-for-as3/</link>
		<comments>http://www.masterbaboon.com/2009/02/advanced-math-functions-for-as3/#comments</comments>
		<pubDate>Sat, 21 Feb 2009 22:29:14 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[ActionScript 3]]></category>
		<category><![CDATA[Math]]></category>
		<category><![CDATA[AdvancedMath]]></category>

		<guid isPermaLink="false">http://www.masterbaboon.com/?p=171</guid>
		<description><![CDATA[Did you ever need to sample from a Dirichlet distribution in Actionscript 3.0? What about computing a value of the Gamma function? Probably not   In case you did, I wrote a class with a few math methods:

a function to sample from multinomial distributions; this is useful if you need to choose randomly from [...]]]></description>
			<content:encoded><![CDATA[<p>Did you ever need to sample from a Dirichlet distribution in Actionscript 3.0? What about computing a value of the Gamma function? Probably not <img src='http://www.masterbaboon.com/wp-includes/images/smilies/icon_wink.gif' alt=';-)' class='wp-smiley' />  In case you did, I wrote a class with a few math methods:</p>
<ul>
<li>a function to sample from multinomial distributions; this is useful if you need to choose randomly from a set of elements, each with a different probability;</li>
<li>functions to draw samples from the Dirichlet and Gamma distribution;</li>
<li>the Gamma function</li>
<li>a factorial function that works even for large values (using the identity n! = Gamma(n+1))</li>
<li>other utility functions, e.g. to compute mean and variance of an array, or add two arrays together</li>
</ul>
<p>You can <a href="/flashes/advancedmath.zip">download the AdvancedMath class here</a>, it includes <a href="http://www.asunit.org/" target="_blank">AsUnit tests</a>.</p>
<p>This is an example of how to use the AdvancedMath class to simulate a rigged dice: the code below throws the dice 1000 times, and keeps a count of how many times one gets the different faces.</p>
<div class="codecolorer-container actionscript3 " style="overflow:auto;white-space:nowrap;width:530px;height:300px"><table cellspacing="0" cellpadding="0"><tbody><tr><td class="line-numbers"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br /></div></td><td><div class="actionscript3 codecolorer" style="font-family:Monaco,Lucida Console,monospace"><span class="kw1">import</span> com.masterbaboon.AdvancedMath;<br />
<br />
<span class="co1">// probability of the faces; 6 is about twice more likely to appear</span><br />
<span class="kw2">var</span> p<span class="sy0">:</span><span class="kw5">Array</span> = <span class="br0">&#91;</span><span class="nu0">0.14</span>, <span class="nu0">0.14</span>, <span class="nu0">0.14</span>, <span class="nu0">0.14</span>, <span class="nu0">0.14</span>, <span class="nu0">0.3</span><span class="br0">&#93;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
<span class="co1">// count the number of times a face appears</span><br />
<span class="kw2">var</span> count<span class="sy0">:</span><span class="kw5">Array</span> = AdvancedMath.zeros<span class="br0">&#40;</span><span class="nu0">6</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
<span class="co1">// throw dice 1000 times</span><br />
<span class="kw2">var</span> face<span class="sy0">:</span><span class="kw5">int</span>;<br />
<span class="kw1">for</span> <span class="br0">&#40;</span><span class="kw2">var</span> i<span class="sy0">:</span><span class="kw5">int</span> = <span class="nu0">0</span>; i <span class="sy0">&lt;</span> <span class="nu0">1000</span>; i<span class="sy0">++</span><span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; face = AdvancedMath.sampleMultinomial<span class="br0">&#40;</span>p<span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; count<span class="br0">&#91;</span>face<span class="br0">&#93;</span><span class="sy0">++</span>;<br />
<span class="br0">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
<span class="co1">// show how many times we got which face</span><br />
<span class="kw7">trace</span><span class="br0">&#40;</span>count<span class="br0">&#41;</span>;</div></td></tr></tbody></table></div>
<p><script type="text/javascript"><!--
google_ad_client = "pub-5929969675637192";
google_ad_slot = "7572213318";
google_ad_width = 468;
google_ad_height = 15;
//--></script>
<script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
</p>
]]></content:encoded>
			<wfw:commentRss>http://www.masterbaboon.com/2009/02/advanced-math-functions-for-as3/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
	</channel>
</rss>
