<?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>Mon, 14 Nov 2011 08:27:40 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2</generator>
		<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 a [...]]]></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="http://www.masterbaboon.com/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 default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:300px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><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="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #0033ff; font-weight: bold;">import</span> com<span style="color: #000066; font-weight: bold;">.</span>masterbaboon<span style="color: #000066; font-weight: bold;">.</span>AdvancedMath<span style="color: #000066; font-weight: bold;">;</span><br />
<br />
<span style="color: #009900; font-style: italic;">// probability of the faces; 6 is about twice more likely to appear</span><br />
<span style="color: #6699cc; font-weight: bold;">var</span> p<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">Array</span> = <span style="color: #000000;">&#91;</span><span style="color: #000000; font-weight:bold;">0.14</span><span style="color: #000066; font-weight: bold;">,</span> <span style="color: #000000; font-weight:bold;">0.14</span><span style="color: #000066; font-weight: bold;">,</span> <span style="color: #000000; font-weight:bold;">0.14</span><span style="color: #000066; font-weight: bold;">,</span> <span style="color: #000000; font-weight:bold;">0.14</span><span style="color: #000066; font-weight: bold;">,</span> <span style="color: #000000; font-weight:bold;">0.14</span><span style="color: #000066; font-weight: bold;">,</span> <span style="color: #000000; font-weight:bold;">0.3</span><span style="color: #000000;">&#93;</span><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
<span style="color: #009900; font-style: italic;">// count the number of times a face appears</span><br />
<span style="color: #6699cc; font-weight: bold;">var</span> count<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">Array</span> = AdvancedMath<span style="color: #000066; font-weight: bold;">.</span>zeros<span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">6</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
<span style="color: #009900; font-style: italic;">// throw dice 1000 times</span><br />
<span style="color: #6699cc; font-weight: bold;">var</span> face<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">int</span><span style="color: #000066; font-weight: bold;">;</span><br />
<span style="color: #0033ff; font-weight: bold;">for</span> <span style="color: #000000;">&#40;</span><span style="color: #6699cc; font-weight: bold;">var</span> i<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">int</span> = <span style="color: #000000; font-weight:bold;">0</span><span style="color: #000066; font-weight: bold;">;</span> i <span style="color: #000066; font-weight: bold;">&lt;</span> <span style="color: #000000; font-weight:bold;">1000</span><span style="color: #000066; font-weight: bold;">;</span> i<span style="color: #000066; font-weight: bold;">++</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; face = AdvancedMath<span style="color: #000066; font-weight: bold;">.</span>sampleMultinomial<span style="color: #000000;">&#40;</span>p<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; count<span style="color: #000000;">&#91;</span>face<span style="color: #000000;">&#93;</span><span style="color: #000066; font-weight: bold;">++;</span><br />
<span style="color: #000000;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
<span style="color: #009900; font-style: italic;">// show how many times we got which face</span><br />
<span style="color: #004993;">trace</span><span style="color: #000000;">&#40;</span>count<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</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>

