<?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>Coranac &#187; math</title>
	<atom:link href="http://www.coranac.com/category/math/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.coranac.com</link>
	<description>my own little world</description>
	<lastBuildDate>Sat, 19 Nov 2011 16:43:51 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1.4</generator>
		<item>
		<title>Probabilities in simple random samples</title>
		<link>http://www.coranac.com/2010/05/probabilities-in-simple-random-samples/</link>
		<comments>http://www.coranac.com/2010/05/probabilities-in-simple-random-samples/#comments</comments>
		<pubDate>Wed, 05 May 2010 17:41:51 +0000</pubDate>
		<dc:creator>cearn</dc:creator>
				<category><![CDATA[math]]></category>

		<guid isPermaLink="false">http://www.coranac.com/?p=200</guid>
		<description><![CDATA[<br />
<b>Warning</b>:  getimagesize() [<a href='function.getimagesize'>function.getimagesize</a>]: Filename cannot be empty in <b>/home/coranac/public_html/wordpress/wp-content/plugins/crnfilters.php</b> on line <b>466</b><br />
<br />
<b>Warning</b>:  getimagesize() [<a href='function.getimagesize'>function.getimagesize</a>]: Filename cannot be empty in <b>/home/coranac/public_html/wordpress/wp-content/plugins/crnfilters.php</b> on line <b>466</b><br />
<br />
<b>Warning</b>:  getimagesize() [<a href='function.getimagesize'>function.getimagesize</a>]: Filename cannot be empty in <b>/home/coranac/public_html/wordpress/wp-content/plugins/crnfilters.php</b> on line <b>466</b><br />
<br />
<b>Warning</b>:  getimagesize() [<a href='function.getimagesize'>function.getimagesize</a>]: Filename cannot be empty in <b>/home/coranac/public_html/wordpress/wp-content/plugins/crnfilters.php</b> on line <b>466</b><br />
1 Introduction 2 Probability and its rules 3 Combinatorics : counting evolved 4 Examples 5 Summary 1 Introduction When it comes to math, one of the things that people are phenomenally bad at &#8211; aside from the obvious everything &#8211; is probabilities. Specifically, calculating probabilities. People are bad enough at estimating chances, but properly calculating [...]]]></description>
			<content:encoded><![CDATA[<br />
<b>Warning</b>:  getimagesize() [<a href='function.getimagesize'>function.getimagesize</a>]: Filename cannot be empty in <b>/home/coranac/public_html/wordpress/wp-content/plugins/crnfilters.php</b> on line <b>466</b><br />
<br />
<b>Warning</b>:  getimagesize() [<a href='function.getimagesize'>function.getimagesize</a>]: Filename cannot be empty in <b>/home/coranac/public_html/wordpress/wp-content/plugins/crnfilters.php</b> on line <b>466</b><br />
<br />
<b>Warning</b>:  getimagesize() [<a href='function.getimagesize'>function.getimagesize</a>]: Filename cannot be empty in <b>/home/coranac/public_html/wordpress/wp-content/plugins/crnfilters.php</b> on line <b>466</b><br />
<br />
<b>Warning</b>:  getimagesize() [<a href='function.getimagesize'>function.getimagesize</a>]: Filename cannot be empty in <b>/home/coranac/public_html/wordpress/wp-content/plugins/crnfilters.php</b> on line <b>466</b><br />
<p></p>
<p><ul>
  <li> <a href="#sec-intro">1
Introduction
</a> </li>
  <li> <a href="#sec-prob">2
Probability and its rules
</a> </li>
  <li> <a href="#sec-cmb">3
Combinatorics : counting evolved
</a> </li>
  <li> <a href="#sec-eg">4
Examples
</a> </li>
  <li> <a href="#sec-summary">5
Summary
</a> </li>
</ul>
</p>
<p><h2 id="sec-intro">1
Introduction
</h2>
</p>
<p>
When it comes to math, one of the things that people are phenomenally<br />
bad at &ndash; aside from the obvious everything &ndash; is<br />
probabilities. Specifically, <i>calculating</i> probabilities.<br />
People are bad enough at<br />
estimating chances, but properly calculating then has its own<br />
hurdles.
</p>
<p>
In this particular case, I&#8217;d like to take a look at the probability of<br />
a 
<a href="http://en.wikipedia.org/wiki/Simple%20random%20sample">Simple random sample</a>: getting a particular distribution<br />
of results from a number of samples, like getting 3 sixes in a<br />
certain number of rolls. Specifically, I&#8217;ll look at the case<br />
<b>with replacement</b>. In this case, the sample pool remains the same<br />
after each individual sample. The case <b>without replacement</b><br />
(like picking colored marbles from a vase without putting them back)<br />
is a bit trickier so I won&#8217;t cover that now.
</p>
<p><span id="more-200"></span></p>
<h4 id="sssec-binom">The binomial distribution</h4>
<p>
One of the equations that is relevant here is the<br />

<a href="http://en.wikipedia.org/wiki/binomial%20distribution">binomial distribution</a><br />
(see Eq&nbsp;1), the scourge of math students<br />
everywhere. What I&#8217;d like to do is explain why it looks the way it does,<br />
what the terms mean and how you can generalize it to cover more than<br />
two outcomes (that&#8217;s what the &ldquo;bi-&rdquo; stands for: two).<br />
Basically, I&#8217;d like to show you how to &ldquo;read&rdquo; the equation<br />
and to interpret the terms in it, which will make it easier to deal<br />
with the subject.
</p>
<p><table class="eqtbl" id="eq-binom-std">
<tr>
<td class="eqnrcell">(1)</td>
  <td class="eqcell"><br />
<img src='http://www.coranac.com/cgi-bin/mimetex.cgi?P%28W%3Dk%29%20%3D%20%7Bn%5Cchoose%20k%7D%20p%5Ek%20%281-p%29%5E%7Bn-k%7D'<br />
	title="P(W=k) = {n\choose k} p^k (1-p)^{n-k}"<br />
	alt="P(W=k) = {n\choose k} p^k (1-p)^{n-k}" /><br />
</td>
</tr>
</table></p>
<p>
First, a few words about the Eq&nbsp;1 itself. Say you have<br />
a random event that can have two outcomes: coin-flip, true/false question,<br />
but also a die-roll if you just look at an X vs non-X result. Or, in<br />
the parlance of our times, whether you have a win (<b>W</b>) or a<br />
fail (<b>F</b>). What Eq&nbsp;1 gives you is the probability<br />
of getting exactly <i>k</i> wins in <i>n</i> trials.
</p>
<p><div>&nbsp;</div></p>
<p>
That&#8217;s all well and good, of course, but unless you actually understand<br />
how the equation is built up and what all the terms in it mean, it&#8217;ll<br />
just be yet another magic formula that someone dreamed up to torture you<br />
with. In practical terms, it also means that it&#8217;s harder to apply it<br />
correctly. So let&#8217;s look at what it means, starting with what each<br />
individual symbol means.
</p>
<div class=cblock>
<table id="tbl-binom" class="cblock"<br />
	border=1 cellpadding=2 cellspacing=0><br />
<caption>
  <b>Table&nbsp;1</b>: Explanation of symbols in<br />
  Eq&nbsp;1.<br />
</caption>
<tr>
<th>Symbol</th>
<th>Meaning</th>
</tr>
<tr>
<th>W</th>
<td>Win index, or number of wins.</td>
</tr>
<tr>
<th>F</th>
<td>Fail index, or number of fails.</td>
</tr>
<tr>
<th>n</th>
<td>Number of trials.</td>
</tr>
<tr>
<th>k</th>
<td>Selection value (usually linked to the number of<br />
	wins).</td>
</tr>
<tr>
<th>p</th>
<td>Single-trial probability (usually linked to<br />
	wins).</td>
</tr>
<tr>
<th>P(<i>cond</i>)</th>
<td>Total probability, with the condition.</td>
</tr>
</table>
</div>
<p><!--<br />
<div class=note id="id-binom-std">
<div  class=nhcare>Equation imprecise</div>
</p>
<p>
I should probably mention that while Eq&nbsp;1 is<br />
correct, it is imprecise.
</p>
<p></div>
<br />
&#8211;></p>
<p>
Here&#8217;s an example of its use. Say you have 7 dice (7d6) and you want to<br />
know the probability of throwing 3 sixes. This means you have<br />
<i>n</i>&nbsp;=&nbsp;7, <i>k</i>&nbsp;=&nbsp;3 and <i>p</i>&nbsp;=&nbsp;1/6, and the condition is<br />
<i>W</i>&nbsp;=&nbsp;<i>k</i>&nbsp;=&nbsp;3.
</p>
<h4>Divide and conquer</h4>
<p>
The next step is to split the equation into the two parts that are<br />
of true relevance, namely:
</p>
<p><div class="cptfr" style="width:px;">
  <a href="" target="_blank">  <img src="" id="img-binom-div"
    alt="" width="" /></a><br />
  <b>Fig&nbsp;1. </b>
</div>
</p>
<ol>
<li>
    The base chance of getting a specific result that satisfies your<br />
	conditions.
  </li>
<li>
    The number of results that satisfies the conditions (each with the<br />
	same base probability).
  </li>
</ol>
<p>
I&#8217;ve always found this division to make the most sense. Note that I&#8217;ve<br />
split it into two parts, not three. The terms involving <i>p</i> and<br />
(1&minus;<i>p</i>) belong in the same group, as I&#8217;ll explain later. The<br />
nice thing about this division is that the two parts neatly correspond<br />
to two concepts of mathematics: probability and combinatorics. If you<br />
understand those two, equations like Eq&nbsp;1 should<br />
start to make sense.
</p>
<div class="lblock">
</div>
<p><h2 id="sec-prob">2
Probability and its rules
</h2>
</p>
<p>
When I say you need to understand probability theory to understand<br />
the binomial distribution, what I really mean is that you understand<br />
the <i>rules</i> or probability, and how to combine single chance<br />
occurrences into larger conditions. See also<br />

<a href="http://en.wikipedia.org/wiki/Probability%23Mathematical_treatment">wiki:probability</a>.
</p>
<p><h3 id="ssec-prob-def">2.1
Probability definition
</h3>
</p>
<p>
The probability of a selected case <i>x</i> is the number of times it<br />
occurs relative to the total pool of cases. This is the fraction given in<br />
Eq&nbsp;2. All other rules are the result of this one.
</p>
<p><table class="eqtbl" id="eq-prob-def">
<tr>
<td class="eqnrcell">(2)</td>
  <td class="eqcell"><br />
<img src='http://www.coranac.com/cgi-bin/mimetex.cgi?P%28X%29%20%3D%20%7B%7B%23%20%5Ctext%7BSelected%20case%7D%7D%20%5Cover%20%7B%20%23%20%5Ctext%7BTotal%20cases%7D%7D%7D'<br />
	title="P(X) = {{# \text{Selected case}} \over { # \text{Total cases}}}"<br />
	alt="P(X) = {{# \text{Selected case}} \over { # \text{Total cases}}}" /><br />
</td>
</tr>
</table></p>
<p>
For example, we roll a d6 and we&#8217;re interested in getting a six.<br />
Since there are 6 sides and only 1 six, the probability is<br />
<i>P</i>(<i>1&nbsp;six</i>)&nbsp;=&nbsp;1/6.
</p>
<p><h3 id="ssec-prob-range">2.2
Probability range
</h3>
</p>
<p>
Since the total number of items is <i>at least</i> as large as the<br />
selection, each probability itself is at most 1. A probability can&#8217;t be<br />
lower than zero either, because counts are natural numbers.
</p>
<p>
This also holds for the sum probability of multiple items. Also, the<br />
probability to get any result at all is 1, because obviously something<br />
needs to happen. If you sum up probabilities and you get an answer<br />
greater than 1, some of the cases you&#8217;ve counted actually overlap. If<br />
you add up all probabilities and you get less than 1, you&#8217;ve missed<br />
a few cases.
</p>
<p><table class="eqtbl" id="eq-prob-range">
<tr>
<td class="eqnrcell">(3)</td>
  <td class="eqcell"><br />
<img src='http://www.coranac.com/cgi-bin/mimetex.cgi?0%20%5Cle%20P%28X%29%20%5Cle%201'<br />
	title="0 \le P(X) \le 1"<br />
	alt="0 \le P(X) \le 1" /><br />
</td>
</tr>
</table></p>
<p><h3 id="ssec-prob-not">2.3
Complementary rule : NOT
</h3>
</p>
<p>
If the probability of some event is <i>P</i>(<i>X</i>) and the sum<br />
probability of all events, then the<br />
probability of it <b>not</b> happening is 1&minus;<i>P</i>(<i>X</i>). The<br />
symbol for &ldquo;not&rdquo; is &not;.
</p>
<p><table class="eqtbl" id="eq-prob-not">
<tr>
<td class="eqnrcell">(4)</td>
  <td class="eqcell"><br />
<img src='http://www.coranac.com/cgi-bin/mimetex.cgi?P%28%5Ctext%7Bnot%7D%5C%2CX%29%20%3D%20P%28%5Cneg%20X%29%20%3D%201%20-%20P%28X%29'<br />
	title="P(\text{not}\,X) = P(\neg X) = 1 - P(X)"<br />
	alt="P(\text{not}\,X) = P(\neg X) = 1 - P(X)" /><br />
</td>
</tr>
</table></p>
<p>
For example, if the probability of a six on a d6 is 1/6, the<br />
probability of <i>not</i> getting a six is <i>P</i>(~<i>six</i>)&nbsp;=&nbsp;5/6.
</p>
<p><h3 id="ssec-prob-xor">2.4
Disjoint conditions : XOR
</h3>
</p>
<p>
If you have multiple conditions that are <b>mutually exclusive</b><br />
(disjoint), the probabilities add. This corresponds to an &ldquo;or&rdquo;<br />
case.
</p>
<p>
Please note the &ldquo;mutually&rdquo; here. It must be <i>either</i><br />
<i>X</i> <i>or</i> <i>Y</i>. For the programmers: the English word<br />
&ldquo;or&rdquo; actually mean an exclusive OR ( ^ in C), not an<br />
inclusive OR ( | ). This difference is the main reason why calculated<br />
probabilities can end up greater than 1. There are several symbols<br />
that can be used for a disjoint condition, all of which are problematic.<br />
I&#8217;ll use the boolean XOR symbol,  &oplus;, because even though it may<br />
be a little obscure for most people, at least it&#8217;s non-ambiguous.
</p>
<p><table class="eqtbl" id="eq-prob-or">
<tr>
<td class="eqnrcell">(5)</td>
  <td class="eqcell"><br />
<img src='http://www.coranac.com/cgi-bin/mimetex.cgi?P%28X%5Ctext%7B%20or%20%7DY%29%20%3D%20P%28X%20%5Coplus%20Y%29%20%3D%20P%28X%29%20%2B%20P%28Y%29'<br />
	title="P(X\text{ or }Y) = P(X \oplus Y) = P(X) + P(Y)"<br />
	alt="P(X\text{ or }Y) = P(X \oplus Y) = P(X) + P(Y)" /><br />
</td>
</tr>
</table></p>
<p>
For example, the chance of getting a 6 <b>or</b> a 5 on a single d6.<br />
is <i>P</i>(<i>six</i> or <i>5</i>)&nbsp;=&nbsp;1/6&nbsp;+&nbsp;1/6&nbsp;=&nbsp;1/3. You can also<br />
approach it from the definition of Eq&nbsp;2, where<br />
you still have a total of 6, but now you have 2 valid cases, leading<br />
directly to 2/6.
</p>
<p><div class=note id="nt-xor">
<div  class=nhcare>Language issues and silly symbols</div>
</p>
<p>
The term &#8220;or&#8221; is one of those areas where human languages, mathematics<br />
and logic are at odds. As said, the word &ldquo;or&rdquo; actually<br />
means an exclusive or (one of the two, but not both), even though in math<br />
and logic it&#8217;s used for the union (the full combination; and/or).
</p>
<p>
It gets even more vexing when one looks at the symbols used. The C<br />
equivalent for OR (the inclusive OR) is <code>|</code> or<br />
<code>||</code>. However, the <code>|</code> symbol is also used for<br />
a 
<a href="http://en.wikipedia.org/wiki/conditional%20probability">conditional probability</a>. So that&#8217;s out. I can&#8217;t use the<br />
C XOR symbol, <code>^</code>either, as that actually means AND in logic.<br />
There&#8217;s also the union symbol, <code>&cup;</code>, but I think that<br />
stands for <i>in</i>clusive OR, not exclusive. So in the end it comes<br />
down to the O-plus, &oplus;. While relatively unknown, at least it<br />
only has a single meaning.
</p>
<p>
If anyone has a better idea, I&#8217;d love to hear it.
</p>
<p></div>
</p>
<p><h3 id="ssec-prob-and">2.5
Joint conditions : AND
</h3>
</p>
<p>
When you have two independent conditions that both need to be true,<br />
the probabilities multiply. Think of it as a sub-selection, where you<br />
have to get a fraction of a fraction. This corresponds to an<br />
&ldquo;and&rdquo; case. The logical symbol for it is a cap ( &cap; ),<br />
but since it looks too much like a cup, I&#8217;ll use the actual AND-symbol<br />
( &amp; ).
</p>
<p><table class="eqtbl" id="eq-prob-and">
<tr>
<td class="eqnrcell">(6)</td>
  <td class="eqcell"><br />
<img src='http://www.coranac.com/cgi-bin/mimetex.cgi?P%28X%5Ctext%7B%20and%20%7DY%29%20%3D%20P%28X%20%5C%26%20Y%29%20%3D%20P%28X%29%20%5Ccdot%20P%28Y%29'<br />
	title="P(X\text{ and }Y) = P(X \&amp; Y) = P(X) \cdot P(Y)"<br />
	alt="P(X\text{ and }Y) = P(X \&amp; Y) = P(X) \cdot P(Y)" /><br />
</td>
</tr>
</table></p>
<p>
An example of this rule is the chance to throw two sixes with two dice.<br />
First, you need to throw a six with one for a 1/6 chance, and in the<br />
event that that happens, there&#8217;s another 1/6 chance for the other,<br />
combining into<br />
<i>P</i>(<i>six</i> and <i>six</i>)&nbsp;=&nbsp;1/6&nbsp;&middot;&nbsp;1/6&nbsp;=&nbsp;1/36. You<br />
can also get this result from the definition. With 2 dice you have 36<br />
possibilities in total, and only one of them will contain (6,6). Note<br />
that for a 5 and 6 the rules are different, since both (5,6) and (6,5)<br />
are possible here.
</p>
<p><h3 id="ssec-prob-use">2.6
Usage in general and binomials in particular
</h3>
</p>
<p>
When considering probabilities, you&#8217;ll often have multiple<br />
trials/samples and a diffent type of answer for each trial. For example,<br />
say you have three different options, <i>a</i>, <i>b</i> and <i>c</i>.<br />
Each option has a different probability,<br />
<i>p</i><sub>a</sub>, <i>p</i><sub>b</sub>, and <i>p</i><sub>c</sub>.<br />
Now I&#8217;m looking for the probability of getting 2 <i>a</i>, 3 <i>b</i>s<br />
and 4 <i>c</i>s. In other words, the number of results for each<br />
possibility is <i>k</i><sub>a</sub>&nbsp;=&nbsp;2,<br />
<i>k</i><sub>b</sub>&nbsp;=&nbsp;3 and <i>k</i><sub>c</sub>&nbsp;=&nbsp;4, for a total<br />
number of trials<br />
<i>n</i>&nbsp;=&nbsp;&Sigma;<sub>i</sub>&nbsp;<i>k</i><sub>i</sub>&nbsp;=&nbsp;10. Now look<br />
at one possible result that satisfies the condition, say,<br />
&#8220;aabbbcccc&#8221;. This is an AND case, so probabilities multiply. Each<br />
trial result gets the probability belonging to that answer, which<br />
in this case leads to
</p>
<p><table class="eqtbl">
<tr>
<td class="eqnrcell"></td>
  <td class="eqcell"><br />
<img src='http://www.coranac.com/cgi-bin/mimetex.cgi?P%28%5Ctext%7Bexactly%20%7D%20aabbbcccc%29%20%3D%20p_a%5E2%5C%2Cp_b%5E3%5C%2Cp_c%5E4.'<br />
	title="P(\text{exactly } aabbbcccc) = p_a^2\,p_b^3\,p_c^4."<br />
	alt="P(\text{exactly } aabbbcccc) = p_a^2\,p_b^3\,p_c^4." /><br />
</td>
</tr>
</table></p>
<p>
The generalized case for this has <i>m</i> classes, labelled<br />
<i>X</i><sub>j</sub>, and we&#8217;re the number of outcomes we&#8217;re looking<br />
for for each class is <i>n</i><i>j</i>. The probability for a<br />
<i>single</i> result that satisfies this condition is<br />
Eq&nbsp;7. It may look a little hairy, but all it<br />
really says is that you should multiply the probability of each of the<br />
result in the condition.
</p>
<p><table class="eqtbl" id="eq-prob-single">
<tr>
<td class="eqnrcell">(7)</td>
  <td class="eqcell"><br />
<img src='http://www.coranac.com/cgi-bin/mimetex.cgi?P%28k_1%20X_1%5C%2C%5C%26%5C%2Ck_2%20X_2%5C%2C%5C%26%20...%20%5C%26%5C%2Ck_m%20X_m%29%20%3D%20p_1%5E%7Bk_1%7D%20%5C%2C%5Ctimes%5C%2C%20p_2%5E%7Bk_2%7D%20%5C%2C%5Ctimes%5C%2C%20...%20%5C%2C%5Ctimes%5C%2C%20p_m%5E%7Bk_m%7D%20%3D%20%5Cprod_%7Bj%3D1%7D%5Em%20p_j%5E%7Bk_j%7D'<br />
	title="P(k_1 X_1\,\&amp;\,k_2 X_2\,\&amp; ... \&amp;\,k_m X_m) = p_1^{k_1} \,\times\, p_2^{k_2} \,\times\, ... \,\times\, p_m^{k_m} = \prod_{j=1}^m p_j^{k_j}"<br />
	alt="P(k_1 X_1\,\&amp;\,k_2 X_2\,\&amp; ... \&amp;\,k_m X_m) = p_1^{k_1} \,\times\, p_2^{k_2} \,\times\, ... \,\times\, p_m^{k_m} = \prod_{j=1}^m p_j^{k_j}" /><br />
</td>
</tr>
</table></p>
<p>
Now, look back at the binomial probability. In that case, you have two<br />
options, <b>W</b>in and <b>F</b>ail, with probabilities<br />
<i>p</i><sub>w</sub> and <i>p</i><i>f</i>, respectively. Let&#8217;s look at<br />
the case for <i>k</i> wins. That is<br />
<i>W</i>&nbsp;=&nbsp;<i>k</i><sub>w</sub>&nbsp;=&nbsp;<i>k</i>. Now, because there are only<br />
two options (that&#8217;s the &ldquo;bi-&rdquo; in binomial), the<br />
probabilities of the fail-case are now also set. The probability for<br />
failing is <i>p</i><sub>f</sub>&nbsp;=&nbsp;<i>p</i><sub>&not;w</sub>&nbsp;=&nbsp;1&minus;<i>p</i><sub>w</sub>.<br />
As for the number of failed cases, remember that the binomial looks<br />
for <i>exactly</i> <i>k</i> wins, meaning the rest of the cases fail,<br />
leading to <i>k</i><sub>f</sub>&nbsp;=&nbsp;<i>n</i>&minus;<i>k</i>. This little<br />
fact is never mentioned explicitly, but it shouldn&#8217;t be overlooked.
</p>
<p>
In the end, what you<br />
get is Eq&nbsp;8. The only difference with the<br />
actual binomial equation is that the subscript has been removed in the<br />
latter. Never underestimate the mathematician&#8217;s laziness when it comes<br />
to writing stuff down.
</p>
<p><table class="eqtbl" id="eq-prob-binom-single">
<tr>
<td class="eqnrcell">(8)</td>
  <td class="eqcell"><br />
<img src='http://www.coranac.com/cgi-bin/mimetex.cgi?P%28W%3Dk_w%5C%2C%5C%26%5C%2C%20F%3Dk_f%29%20%3D%20p_w%5E%7Bk_w%7D%5C%2Cp_f%5E%7Bk_f%7D%20%3D%20p_w%5Ek%5C%2C%281-p_w%29%5E%7Bn-k%7D'<br />
	title="P(W=k_w\,\&amp;\, F=k_f) = p_w^{k_w}\,p_f^{k_f} = p_w^k\,(1-p_w)^{n-k}"<br />
	alt="P(W=k_w\,\&amp;\, F=k_f) = p_w^{k_w}\,p_f^{k_f} = p_w^k\,(1-p_w)^{n-k}" /><br />
</td>
</tr>
</table></p>
<p>
There&#8217;s still one thing missing though: both<br />
Eq&nbsp;7 and Eq&nbsp;8<br />
only cover the possibility of one <i>specific</i> valid result. In<br />
most cases, there will be others as well. In the <i>a</i>,<i>b</i>,<i>c</i><br />
case, for example, I only looked at &#8220;aabbbcccc&#8221;, but &#8220;bbbaacccc&#8221;,<br />
&#8220;abcabcbcc&#8221;, or &#8220;cccbbaaa&#8221; would have worked as well, as would any<br />
other combination. You need take all of these into account, but for<br />
that you need a way to quickly find all variations first. And that&#8217;s<br />
the topic of the next section: combinatorics.
</p>
<p><h2 id="sec-cmb">3
Combinatorics : counting evolved
</h2>
</p>
<p>
Very briefly, 
<a href="http://en.wikipedia.org/wiki/combinatorics">combinatorics</a> is the study of counting<br />
discrete variations. In particular, how you can <b>quickly</b> count<br />
them all in an orderly and correct fashion.<br />
In this case, what I&#8217;ll need is a way to quickly count the different<br />
possible ways of distributing items over a number of bins. What also<br />
matters is whether the items are <b>unique</b> (that is to say, whether<br />
you can tell them apart from other items) or <b>non-unique</b>.
</p>
<p>
I&#8217;ll start with unique items and then show how to incorporate<br />
non-unique ones. This will involve factorials (<i>n</i>!). Along the<br />
way, we&#8217;ll pick up permutations (<i>n</i>P<i>r</i>) and<br />
combinations (<i>n</i>C<i>r</i>), which are merely special cases of the<br />
general formula.
</p>
<p><h3 id="ssec-cmb-unique">3.1
Arrangements with unique items
</h3>
</p>
<p>
The specific task here is to find out in how many ways you can<br />
distribute <i>n</i> unique objects over <i>n</i> different slots. This<br />
is a case of combinatorics <dfn>without replacement</dfn>, because once<br />
you&#8217;ve placed an object in a particular slot, you can&#8217;t use it anywhere<br />
else.
</p>
<p>
I&#8217;ll start by looking at an example with four objects,<br />
<i>a</i>, <i>b</i>, <i>c</i> and <i>d</i>. Naturally, there will also<br />
be four slots to put them in. To count the possibilities, we&#8217;ll make<br />
a tree (see Fig&nbsp;2). There are two ways to fill the<br />
tree: by a slot or by item. When working without replacement, it&#8217;s more<br />
convenient to fill by item. Right now it won&#8217;t matter that much,<br />
because when you start dealing with groups of non-unique items, it has<br />
a nice way of grouping all of those on the same depth.
</p>
<div class="cblock">
<div class="cpt" style="width:px;">
  <a href="" target="_blank">  <img src="" id="img-tree-abcd"
    alt="" width="" /></a><br />
  <b>Fig&nbsp;2. </b>
</div>

</div>
<p>
As you can see in Fig&nbsp;2, we start with four open<br />
slots and four items. First we place <i>a</i>, which has a choice of<br />
all four slots. For <i>b</i>, only three slots are left open; <i>c</i><br />
has only 2 and <i>d</i> takes the last remaining slot. The point here<br />
is that each next item has one fewer slot. The last column has all the<br />
different arrangements, and as you can see there are 24 in total.<br />
The really important point, though, is to note that all the branches<br />
for a given level has the same number of sub-branches. This means that<br />
you can simply multiply the number of slots left, leading to<br />
4&middot;3&middot;2&middot;1. In other words, it&#8217;s simply the<br />
factorial: 4!.
</p>
<p>
Generalizing this, given <i>n</i> unique items, the number of variations<br />
is <i>n</i>!.
</p>
<p><h3 id="ssec-cmb-non-unique">3.2
Arrangements with non-unique items
</h3>
</p>
<p>
Now lets look at what happens when there are also identical-looking<br />
items. For example, lets take one <i>a</i>, one <i>b</i> and<br />
<b>three</b> <i>c</i>s, for a total of five items<br />
(see Fig&nbsp;3). Technically, for<br />
five items you&#8217;d get 5! variations, however because the <i>c</i>s are<br />
identical some solutions are actually the same: we&#8217;ve overcounted.<br />
To resolve this, we need to know how many variations are actually the<br />
same. This is simply the number of variations within the<br />
<i>c</i>-group. With 3 <i>c</i>s, that means 3!. So to get the final<br />
answer, we divide by 3!.
</p>
<div class="cblock">
<div class="cpt" style="width:px;">
  <a href="" target="_blank">  <img src="" id="img-tree-abccc"
    alt="" width="" /></a><br />
  <b>Fig&nbsp;3. </b>
</div>
</p>
<p>
This is becoming similar to the case in section 2.6.<br />
Again, take the case that you have <i>m</i> groups,<br />
<i>k</i><sub>j</sub> identical items per group, and<br />
<i>n</i>&nbsp;=&nbsp;&Sigma;<sub>j</sub>&nbsp;<i>k</i><sub>j</sub> items in total.<br />
If all items were unique, you&#8217;d get <i>n</i>! variations, but in each<br />
group you&#8217;re counting <i>k</i><sub>j</sub>! times too many. The proper<br />
number is <i>n</i>! divided by all the overcounts. This term is called<br />
the <dfn>
<a href="http://en.wikipedia.org/wiki/Multinomial_theorem">multinomial coefficient</a></dfn>,<br />
and defined as:
</p>
<p><table class="eqtbl" id="eq-cmb-mnom">
<tr>
<td class="eqnrcell">(9)</td>
  <td class="eqcell"><br />
<img src='http://www.coranac.com/cgi-bin/mimetex.cgi?A%28k_1%20X_1%2C%20...%2C%20k_m%20X_m%29%20%3D%20%7B%20n%20%5Cchoose%20k_1%2C%5C%2C...%2C%5C%2Ck_m%20%7D%20%3D%20%7B%20%7B%20%5C%28k_1%20%2B%20...%20%2B%20k_m%5C%29%21%20%7D%20%5Cover%20%7B%20k_1%21%20%5C%2C%5Ccdot%5C%2C%20...%20%5C%2C%5Ccdot%5C%2C%20k_m%21%20%7D%20%7D%20%3D%20%7B%20%7Bn%21%7D%20%5Cover%20%7B%20%5Cprod_%7Bj%3D1%7D%5Em%5C%2C%20k_j%21%7D%20%7D'<br />
	title="A(k_1 X_1, ..., k_m X_m) = { n \choose k_1,\,...,\,k_m } = { { \(k_1 + ... + k_m\)! } \over { k_1! \,\cdot\, ... \,\cdot\, k_m! } } = { {n!} \over { \prod_{j=1}^m\, k_j!} }"<br />
	alt="A(k_1 X_1, ..., k_m X_m) = { n \choose k_1,\,...,\,k_m } = { { \(k_1 + ... + k_m\)! } \over { k_1! \,\cdot\, ... \,\cdot\, k_m! } } = { {n!} \over { \prod_{j=1}^m\, k_j!} }" /><br />
</td>
</tr>
</table></p>
<p>
As an extra example of this, consider again the case with 2 <i>a</i>s,<br />
3 <i>b</i>s and 4 <i>c</i>s. That is, <i>k</i><sub>a</sub>&nbsp;=&nbsp;2,<br />
<i>k</i><sub>b</sub>&nbsp;=&nbsp;3 and <i>k</i><sub>c</sub>&nbsp;=&nbsp;4, and<br />
<i>n</i>&nbsp;=&nbsp;9. The number of arrangements is<br />
n! / (<i>k</i><sub>a</sub>! <i>k</i><sub>b</sub>!<br />
<i>k</i><sub>c</sub>!)&nbsp;=&nbsp; 9!/(2! 3! 4!) = 1260.
</p>
<p><div>&nbsp;</div></p>
<p>
There&#8217;s also a neat little way of describing this graphically, via a<br />
table (see Table&nbsp;2). First, you take the most regular<br />
answer, where everything&#8217;s divided into groups, like<br />
<i>aabbbcccc</i>. Write the indices above each term, ultimately<br />
resulting in <i>n</i>!. Now break up the string into groups index<br />
each group, giving you  all the <i>k</i><sub>j</sub>! terms. The<br />
result is dividing the full indexed part with the group-parts.
</p>
<div class="lblock">
<table id="tbl-mnom"<br />
  border=0 cellpadding=2 cellspacing=0><br />
<caption>
  <b>Table&nbsp;2</b>. Multinomial expressed in tabular form.<br />
</caption>
<tr>
<td>
<table border=0 cellpadding=2 cellspacing=0>
<tr>
<td class="bdrL">9</td>
<td>8</td>
<td>7</td>
<td>6</td>
<td>5</td>
<td>4</td>
<td>3</td>
<td>2</td>
<td class="bdrR">1</td>
</tr>
<tr class="bdrT bdrB">
<th class="bdrL">a</th>
<th class="bdrR">a</th>
<th>b</th>
<th>b</th>
<th class="bdrR">b</th>
<th>c</th>
<th>c</th>
<th>c</th>
<th class="bdrR">c</th>
</tr>
<tr>
<td class="bdrL">2</td>
<td class="bdrR">1</td>
<td>3</td>
<td>2</td>
<td class="bdrR">1</td>
<td>4</td>
<td>3</td>
<td>2</td>
<td class="bdrR">1</td>
</tr>
</table>
</td>
<td>
<p><img src='http://www.coranac.com/cgi-bin/mimetex.cgi?%5C%3B%5C%3B%5CRightarrow%5C%3B%5C%3B%7B%209%21%20%5Cover%202%21%203%21%204%21%7D'<br />
	title="\;\;\Rightarrow\;\;{ 9! \over 2! 3! 4!}"<br />
	alt="\;\;\Rightarrow\;\;{ 9! \over 2! 3! 4!}" /></p>
</td>
</tr>
</table>
</div>
<p><h3 id="ssec-cmb-pc">3.3
Permutations and combinations
</h3>
</p>
<p>
Permutations and combinations are merely special cases of<br />
Eq&nbsp;9. This is not how it&#8217;s generally taught, but<br />
it&#8217;s still true. In  <dfn>permutation</dfn>, you have <i>r</i> <b>unique</b><br />
items and <i>n</i> slots. In other words, you have <i>n</i>&minus;<i>r</i><br />
identical, empty slots. You only have an overcount for the empty<br />
slots, so you end up with
</p>
<p><table class="eqtbl" id="eq-cmb-perm">
<tr>
<td class="eqnrcell">(10)</td>
  <td class="eqcell"><br />
<img src='http://www.coranac.com/cgi-bin/mimetex.cgi?%5EnP_r%20%3D%20%7B%20%7B%20n%21%7D%20%5Cover%20%7B%20%5Cunderbrace%7B1%21...1%21%7D_%7Br%5C%2C%5Ctext%7Btimes%7D%7D%20%28n-r%29%21%20%7D%20%7D%20%3D%20%7B%20%7B%20n%21%7D%20%5Cover%20%7B%28n-r%29%21%20%7D%20%7D.'<br />
	title="^nP_r = { { n!} \over { \underbrace{1!...1!}_{r\,\text{times}} (n-r)! } } = { { n!} \over {(n-r)! } }."<br />
	alt="^nP_r = { { n!} \over { \underbrace{1!...1!}_{r\,\text{times}} (n-r)! } } = { { n!} \over {(n-r)! } }." /><br />
</td>
</tr>
</table></p>
<p>
This also has another name: the <dfn>
<a href="http://en.wikipedia.org/wiki/falling%20factorial">falling factorial</a></dfn>,<br />
written as <i>n</i><sub>(r)</sub>. It&#8217;s called that because it&#8217;s<br />
basically the top <i>r</i> terms of a factorial. For example,<br />
<sup>7</sup>P<sub>3</sub> = 7<sub>(3)</sub> = 7&middot;6&middot;5.<br />
It&#8217;s written out in the equation below. Notice how the lower terms<br />
cancel out.
</p>
<p><table class="eqtbl">
<tr>
<td class="eqnrcell"></td>
  <td class="eqcell"><br />
<img src='http://www.coranac.com/cgi-bin/mimetex.cgi?%5E7P_3%20%3D%20%5Cfrac%7B7%21%7D%7B%287-3%29%21%7D%20%3D%20%5Cfrac%7B7%21%7D%7B4%21%7D%20%3D%20%5Cfrac%7B7%20%5Ccdot%206%20%5Ccdot%205%20%5Ccdot%7D%7B%5C%2C%7D%20%5Cfrac%7B4%20%5Ccdot%203%20%5Ccdot%202%20%5Ccdot%201%7D%7B4%20%5Ccdot%203%20%5Ccdot%202%20%5Ccdot%201%7D%3D%207%20%5Ccdot%206%20%5Ccdot%205'<br />
	title="^7P_3 = \frac{7!}{(7-3)!} = \frac{7!}{4!} = \frac{7 \cdot 6 \cdot 5 \cdot}{\,} \frac{4 \cdot 3 \cdot 2 \cdot 1}{4 \cdot 3 \cdot 2 \cdot 1}= 7 \cdot 6 \cdot 5"<br />
	alt="^7P_3 = \frac{7!}{(7-3)!} = \frac{7!}{4!} = \frac{7 \cdot 6 \cdot 5 \cdot}{\,} \frac{4 \cdot 3 \cdot 2 \cdot 1}{4 \cdot 3 \cdot 2 \cdot 1}= 7 \cdot 6 \cdot 5" /><br />
</td>
</tr>
</table></p>
<p>
For <b>combinations</b>, you have <i>r</i> <b>non-unique</b> items<br />
over <i>n</i> slots. In this case you have two groups with overcounts,<br />
one of size <i>r</i> and one of size <i>n</i>&minus;r. This becomes
</p>
<p><table class="eqtbl" id="eq-cmb-combi">
<tr>
<td class="eqnrcell">(11)</td>
  <td class="eqcell"><br />
<img src='http://www.coranac.com/cgi-bin/mimetex.cgi?%5EnC_r%20%3D%20%7B%20n%20%5Cchoose%20r%20%7D%20%3D%20%7B%20n%21%20%5Cover%20r%21%20%28n-r%29%21%20%7D%20%3D%20%7B%5EnP_r%20%5Cover%20r%21%7D.'<br />
	title="^nC_r = { n \choose r } = { n! \over r! (n-r)! } = {^nP_r \over r!}."<br />
	alt="^nC_r = { n \choose r } = { n! \over r! (n-r)! } = {^nP_r \over r!}." /><br />
</td>
</tr>
</table></p>
<p>
The combination has three interesting properties.
</p>
<ol>
<li>
    It can be interpreted as a variation from the permutation,<br />
	namely by switching from unique to non-unique items. That&#8217;s<br />
	where the division by <i>r</i>! comes from.
  </li>
<li>
    Two, it&#8217;s symmetric between <i>r</i> and <i>n</i>&minus;<i>r</i>.<br />
	That is, <sup>n</sup>C<sub>r</sub> and <sup>n</sup>C<sub>n&minus;r</sub><br />
	are identical.
  </li>
<li>
    Three, it uses two groups of identical items, which is exactly<br />
	what you have in the case of the binomial distribution: one set<br />
	of wins and one set of fails. This is why it shows up as a<br />
	multiplier in Eq&nbsp;1, and why the combination<br />
	is also called the <dfn>binomial coefficient</dfn>.
  </li>
</ol>
<p><div>&nbsp;</div></p>
<p>
Instead of looking at unique versus non-unique, you can also<br />
interpret permutation and combination by looking at whether the order<br />
of the positions matter. If it does (that is, &#8220;123&#8243; is not the same as<br />
&#8220;132&#8243;), then it&#8217;s a permutation. If the order doesn&#8217;t matter, you have<br />
a combination.
</p>
<p><div>&nbsp;</div></p>
<p>
In the above , I&#8217;ve considered the binomial coefficient as a special<br />
case of the multinomial, but you can also do the reverse: string<br />
multiple binomials together to form a multinomial. In<br />
this case, you first distribute <i>k</i><sub>1</sub> elements over the<br />
whole <i>n</i> slots. This can be done in <sup>n</sup>C<sub>k1</sub><br />
ways. At this point, you&#8217;ve got <i>n</i>&minus;<i>k</i><sub>1</sub> slots<br />
remaining, over which you distribute <i>k</i><sub>2</sub>. This<br />
leaves <i>n</i>&minus;<i>k</i><sub>1</sub>&minus;<i>k</i><sub>2</sub> for group<br />
3, and so on until you&#8217;ve got no slots and items left. This is illustrated<br />
by Eq&nbsp;12. Note how one part of the denominator of<br />
one term cancels out against the numerator of the next.
</p>
<p><table class="eqtbl" id="eq-cmb-mnom2">
<tr>
<td class="eqnrcell">(12)</td>
  <td class="eqcell"><br />
<img src='http://www.coranac.com/cgi-bin/mimetex.cgi?%5Cbegin%7Beqnarray%7D%20%7B%20n%20%5Cchoose%20k_1%20%7D%20%7B%20n-k_1%20%5Cchoose%20k_2%20%7D%20%7B%20n-k_1-k_2%20%5Cchoose%20k_3%20%7D%20%5Cdots%20%7B%20k_m%20%5Cchoose%20k_m%20%7D%20%26%3D%26%20%7B%20n%21%20%5Cover%20k_1%21%20%28n-k_1%29%21%20%7D%5C%2C%20%7B%20%28n-k_1%29%21%20%5Cover%20k_2%21%20%28n-k_1-k_2%29%21%20%7D%20%5C%2C%5Ccdots%5C%2C%20%7B%20k_m%21%20%5Cover%20k_m%21%200%21%20%7D%20%5C%5C%20%26%3D%26%20%7B%20n%21%20%5Cover%20k_1%21%5C%2Ck_2%21%5C%2C%5Ccdots%5C%2Ck_m%21%20%7D%20%5C%5C%20%26%3D%26%20%7B%20n%20%5Cchoose%20k_1%2C%5C%2C%20k2%2C%20%5C%2C...%5C%2C%20%2C%5C%2C%20k_m%20%7D%20%5Cend%7Beqnarray%7D'<br />
	title="\begin{eqnarray} { n \choose k_1 } { n-k_1 \choose k_2 } { n-k_1-k_2 \choose k_3 } \dots { k_m \choose k_m } &amp;=&amp; { n! \over k_1! (n-k_1)! }\, { (n-k_1)! \over k_2! (n-k_1-k_2)! } \,\cdots\, { k_m! \over k_m! 0! } \\ &amp;=&amp; { n! \over k_1!\,k_2!\,\cdots\,k_m! } \\ &amp;=&amp; { n \choose k_1,\, k2, \,...\, ,\, k_m } \end{eqnarray}"<br />
	alt="\begin{eqnarray} { n \choose k_1 } { n-k_1 \choose k_2 } { n-k_1-k_2 \choose k_3 } \dots { k_m \choose k_m } &amp;=&amp; { n! \over k_1! (n-k_1)! }\, { (n-k_1)! \over k_2! (n-k_1-k_2)! } \,\cdots\, { k_m! \over k_m! 0! } \\ &amp;=&amp; { n! \over k_1!\,k_2!\,\cdots\,k_m! } \\ &amp;=&amp; { n \choose k_1,\, k2, \,...\, ,\, k_m } \end{eqnarray}" /><br />
</td>
</tr>
</table></p>
<p>
The binomial and multinomial coefficients are usually written as a<br />
2-by-1 matrix between parentheses (see Eq&nbsp;13).<br />
The construct is pronounced &ldquo;<i>n</i> choose <i>k</i>&rdquo;.<br />
No, I don&#8217;t know why either.
</p>
<p><table class="eqtbl" id="eq-cmb-choose">
<tr>
<td class="eqnrcell">(13)</td>
  <td class="eqcell"><br />
<img src='http://www.coranac.com/cgi-bin/mimetex.cgi?%5Cbegin%7Beqnarray%7D%20%5Ctext%7BBinomial%3A%20%7D%26%20%7B%20n%20%5Cchoose%20k%7D%3D%20%7B%20n%21%20%5Cover%20k%21%5C%2C%28n-k%29%21%20%7D%20%5C%5C%20%5Ctext%7BMultinomial%3A%20%7D%26%20%7B%20n%20%5Cchoose%20k_1%2C%20k_2%2C%20%5C%2C...%5C%2C%2C%20k_m%20%7D%3D%20%7B%20n%21%20%5Cover%20k_1%21%5C%2Ck_2%21%5C%2C%5Ccdots%5C%2Ck_m%21%20%7D%20%5Cend%7Beqnarray%7D'<br />
	title="\begin{eqnarray} \text{Binomial: }&amp; { n \choose k}= { n! \over k!\,(n-k)! } \\ \text{Multinomial: }&amp; { n \choose k_1, k_2, \,...\,, k_m }= { n! \over k_1!\,k_2!\,\cdots\,k_m! } \end{eqnarray}"<br />
	alt="\begin{eqnarray} \text{Binomial: }&amp; { n \choose k}= { n! \over k!\,(n-k)! } \\ \text{Multinomial: }&amp; { n \choose k_1, k_2, \,...\,, k_m }= { n! \over k_1!\,k_2!\,\cdots\,k_m! } \end{eqnarray}" /><br />
</td>
</tr>
</table></p>
<p><h3>
Back to the binomial
</h3>
</p>
<p>
Now that both parts of the division has been covered, lets go back to<br />
the binomial distribution and see how they fit together. For<br />
convenience, here&#8217;s what we were talking about again.
</p>
<p><table class="eqtbl" id="eq-binom-ex">
<tr>
<td class="eqnrcell">(14)</td>
  <td class="eqcell"><br />
<img src='http://www.coranac.com/cgi-bin/mimetex.cgi?%5Cbegin%7Beqnarray%7D%20P%28W%3Dk%20%5Ctext%7B%20in%20%7D%20n%29%20%26%3D%26%20%7Bn%5Cchoose%20k%7D%20p%5Ek%20%281-p%29%5E%7Bn-k%7D%20%5C%5C%20%5Ctext%7Bor%7D%20%5C%5C%20P%28W%3Dk%2C%20F%3Dn-k%29%20%26%3D%26%20%7Bn%5Cchoose%20k%7D%20p_w%5Ek%20%5C%2C%20p_f%5E%7Bn-k%7D%20%5Cend%7Beqnarray%7D'<br />
	title="\begin{eqnarray} P(W=k \text{ in } n) &amp;=&amp; {n\choose k} p^k (1-p)^{n-k} \\ \text{or} \\ P(W=k, F=n-k) &amp;=&amp; {n\choose k} p_w^k \, p_f^{n-k} \end{eqnarray}"<br />
	alt="\begin{eqnarray} P(W=k \text{ in } n) &amp;=&amp; {n\choose k} p^k (1-p)^{n-k} \\ \text{or} \\ P(W=k, F=n-k) &amp;=&amp; {n\choose k} p_w^k \, p_f^{n-k} \end{eqnarray}" /><br />
</td>
</tr>
</table></p>
<ol>
<li>
    The base chance of getting a specific result that satisfies your<br />
	conditions.
  </li>
<li>
    The number of results that satisfies the conditions (each with the<br />
	same base probability).
  </li>
</ol>
<p>
The thing about these two parts is that they actually represent two of<br />
the rules of probability, namely those describing how probabilities<br />
combine. Part 1, the base chance is a joint condition; an AND case.<br />
The second part covers all the mutually exclusive ways that the joint<br />
condition can be satisfied.
</p>
<p>
For example with 2 <i>a</i>s and 1 <i>b</i>,<br />
the ways the cases combine are <i>aab</i>, <i>aba</i> or <i>baa</i>. If<br />
you were to write it out completely, you&#8217;d get
</p>
<p><table class="eqtbl">
<tr>
<td class="eqnrcell"></td>
  <td class="eqcell"><br />
<img src='http://www.coranac.com/cgi-bin/mimetex.cgi?P%28A%3D2%2CB%3D1%29%20%3D%20%5Cbegin%7Beqnarray%7D%20%28%5C%2CP%28A%29%20%5Ctext%7B%20and%20%7D%20P%28A%29%20%5Ctext%7B%20and%20%7D%20P%28B%29%5C%2C%29%20%26%5Ctext%7B%20or%20%7D%20%5C%5C%20%28%5C%2CP%28A%29%20%5Ctext%7B%20and%20%7D%20P%28B%29%20%5Ctext%7B%20and%20%7D%20P%28A%29%5C%2C%29%20%26%5Ctext%7B%20or%20%7D%20%5C%5C%20%28%5C%2CP%28B%29%20%5Ctext%7B%20and%20%7D%20P%28A%29%20%5Ctext%7B%20and%20%7D%20P%28A%29%5C%2C%29%20%26%20%5Cend%7Beqnarray%7D'<br />
	title="P(A=2,B=1) = \begin{eqnarray} (\,P(A) \text{ and } P(A) \text{ and } P(B)\,) &amp;\text{ or } \\ (\,P(A) \text{ and } P(B) \text{ and } P(A)\,) &amp;\text{ or } \\ (\,P(B) \text{ and } P(A) \text{ and } P(A)\,) &amp; \end{eqnarray}"<br />
	alt="P(A=2,B=1) = \begin{eqnarray} (\,P(A) \text{ and } P(A) \text{ and } P(B)\,) &amp;\text{ or } \\ (\,P(A) \text{ and } P(B) \text{ and } P(A)\,) &amp;\text{ or } \\ (\,P(B) \text{ and } P(A) \text{ and } P(A)\,) &amp; \end{eqnarray}" /><br />
</td>
</tr>
</table></p>
<p>
Obviously, you&#8217;re not going to write this out all the time,<br />
especially not when the numbers get large, but this is really what<br />
you&#8217;re looking for. The use of the base-chance and combinations are<br />
ways of shortcircuiting this. As said, the base-chance is the<br />
probability of the string of ANDs, which will be the same for case.<br />
Because of this, the addition of all the (X)ORs simply becomes a<br />
multiplication by the number of (X)ORs that you have, which is<br />
given by the binomial, or multinomial if you have more than one option.
</p>
<p><div>&nbsp;</div></p>
<p>
Another interesting point is that the binomial and multinomial<br />
coefficients themselves can be read as probabilities as well. It&#8217;s just<br />
that because we&#8217;re certain <i>k</i><sub>j</sub> items will exist if<br />
we&#8217;re just arranging items, the probability for each group is 1, so<br />
the whole probability part is unnecessary.
</p>
<p><h2 id="sec-eg">4
Examples
</h2>
</p>
<p>
Because I know all of this can be a little mesmerizing at first, I<br />
thought I&#8217;d give a few examples as well.
</p>
<p><h3 id="ssec-eg-binom">4.1
Beastly dice
</h3>
</p>
<p>
First, simple example of the binomial distribution. Say we have<br />
five dice (5d6) and we&#8217;re looking at the different ways of getting<br />
exactly three sixes. Note that this also means the remaining cases<br />
should be a non-six!
</p>
<p>
First, the probability of a six is <i>p</i><sub>6</sub>&nbsp;=&nbsp;1/6, and<br />
of a non-six <i>p</i><sub>&not;6</sub>&nbsp;=&nbsp;5/6. As for the group-counts,<br />
we have 2 groups with <i>k</i>&nbsp;=&nbsp;<i>k</i><sub>6</sub>&nbsp;=&nbsp;3 and<br />
<i>k</i><sub>&not;6</sub>&nbsp;=&nbsp;2, and <i>n</i>&nbsp;=&nbsp;5. Putting this together<br />
results in Eq&nbsp;15. Note the base chance fo getting<br />
any triple-six result, and the number of ways you can vary this<br />
over five dice.
</p>
<p><table class="eqtbl" id="eq-eg-666">
<tr>
<td class="eqnrcell">(15)</td>
  <td class="eqcell"><br />
<img src='http://www.coranac.com/cgi-bin/mimetex.cgi?P%283%20%5Ctimes%206%20%5Ctext%7B%20in%20%7D%205%29%20%3D%20P%283%20%5Ctimes%206%20%5C%2C%5C%26%5C%2C%202%20%5Ctimes%20%5Cnot6%29%20%3D%20%7B%205%20%5Cchoose%203%7D%20p_6%5E3%20p_%7B%5Cnot6%7D%5E2%20%3D%2010%5Ccdot%5C%28%5Cfrac16%5C%29%5E3%5C%28%5Cfrac56%5C%29%5E2%20%3D%203.21%25'<br />
	title="P(3 \times 6 \text{ in } 5) = P(3 \times 6 \,\&amp;\, 2 \times \not6) = { 5 \choose 3} p_6^3 p_{\not6}^2 = 10\cdot\(\frac16\)^3\(\frac56\)^2 = 3.21%"<br />
	alt="P(3 \times 6 \text{ in } 5) = P(3 \times 6 \,\&amp;\, 2 \times \not6) = { 5 \choose 3} p_6^3 p_{\not6}^2 = 10\cdot\(\frac16\)^3\(\frac56\)^2 = 3.21%" /><br />
</td>
</tr>
</table></p>
<p>
There&#8217;s also another way of getting to this result, namely by direct<br />
use of the definition of probability, Eq&nbsp;2. To do<br />
this, we need know the total number of possibilities, and how many<br />
will result in 3 sixes. With five dice, the total is<br />
<i>T</i>&nbsp;=&nbsp;6<sup>5</sup>. There are <sup>5</sup>C<sub>3</sub>&nbsp;=&nbsp;10<br />
ways of getting exactly 3 sixes in five dice and 5 non-six options<br />
for the remaining two dice, leading to a total of valid cases of<br />
10&middot;5<sup>2</sup>&nbsp;=&nbsp;250.
</p>
<p><table class="eqtbl">
<tr>
<td class="eqnrcell"></td>
  <td class="eqcell"><br />
<img src='http://www.coranac.com/cgi-bin/mimetex.cgi?P%283%20%5Ctimes%206%20%5Ctext%7B%20in%20%7D%205%29%20%3D%20%7B%20%23%283%5Ctimes%206%20%5Ctext%7B%20in%20%7D%205%29%20%5Cover%20%23%5Ctext%7Btotal%7D%20%7D%20%3D%20%7B%2010%20%5Ccdot%205%5E2%20%5Cover%205%5E6%20%7D%20%3D%20%5Cfrac%7B250%7D%7B7776%7D%20%3D%203.21%25'<br />
	title="P(3 \times 6 \text{ in } 5) = { #(3\times 6 \text{ in } 5) \over #\text{total} } = { 10 \cdot 5^2 \over 5^6 } = \frac{250}{7776} = 3.21%"<br />
	alt="P(3 \times 6 \text{ in } 5) = { #(3\times 6 \text{ in } 5) \over #\text{total} } = { 10 \cdot 5^2 \over 5^6 } = \frac{250}{7776} = 3.21%" /><br />
</td>
</tr>
</table></p>
<p><h3 id="ssec-eg-full">4.2
Yahtzee : full house
</h3>
</p>
<p>
More dice, this time looking at the yahtzee game. A full house is three<br />
of a kind plus a pair, like 66655, 11122 or 22333, and any variation<br />
thereof. In this particular case I want to look at getting a full house<br />
in one go, because to take all three turns into account is way too<br />
hairy.
</p>
<p>
The case is actually similar to the previous one in that you have<br />
3 of one group and 2 of another, but the probabilities are a little<br />
different. For a specific full house, say 11122 the base chance is<br />
1/6<sup>5</sup>. However, we&#8217;re not looking for a specific one, but any<br />
full house. This means that there will be 6 options for the first group,<br />
and 5 for the second. The final probability is
</p>
<p><table class="eqtbl" id="eq-eg-full">
<tr>
<td class="eqnrcell">(16)</td>
  <td class="eqcell"><br />
<img src='http://www.coranac.com/cgi-bin/mimetex.cgi?P%28%5Ctext%7Bany%20full%20house%7D%29%20%3D%206%20%5Ccdot%205%20%5Ctimes%20P%28%5Ctext%7Bfull%20house%7D%29%20%3D%2030%5Ccdot%7B%205%20%5Cchoose%203%20%7D%20%5Ctimes%20%5C%28%5Cfrac16%5C%29%5E5%20%3D%2030%5Ccdot10%20%5Ctimes%20%5Cfrac1%7B7776%7D%20%3D%203.86%25'<br />
	title="P(\text{any full house}) = 6 \cdot 5 \times P(\text{full house}) = 30\cdot{ 5 \choose 3 } \times \(\frac16\)^5 = 30\cdot10 \times \frac1{7776} = 3.86%"<br />
	alt="P(\text{any full house}) = 6 \cdot 5 \times P(\text{full house}) = 30\cdot{ 5 \choose 3 } \times \(\frac16\)^5 = 30\cdot10 \times \frac1{7776} = 3.86%" /><br />
</td>
</tr>
</table></p>
<p>
An alternative approach is to consider the group-determining dice to be<br />
&ldquo;free&rdquo;, giving the probability 6/6 and 5/6 for the first<br />
and second group, respectively. After that, the remaining dice have to<br />
follow suit to give (1/6)<sup>3</sup>.
</p>
<p><h3 id="ssec-eg-wheel">4.3
Multinomial : spinning wheels
</h3>
</p>
<p><div class="cptfr" style="width:px;">
  <a href="" target="_blank">  <img src="" id="img-wheel"
    alt="" width="" /></a><br />
  <b>Fig&nbsp;4. </b>
</div>
</p>
<p>
And now for an application of the multinomial. Consider the<br />
wheel from Fig&nbsp;4. It has 2 <i>a</i>s, 3 <i>b</i>s<br />
and 5 <i>c</i>s: <i>n</i><sub>a</sub>&nbsp;=&nbsp;2,<br />
<i>n</i><sub>b</sub>&nbsp;=&nbsp;3, <i>n</i><sub>c</sub>&nbsp;=&nbsp;5.<br />
The probabilities for each letter follow from the probability definition,<br />
resulting in<br />
<i>p</i><sub>a</sub>&nbsp;=&nbsp;2/10&nbsp;=&nbsp;20%,<br />
<i>p</i><sub>b</sub>&nbsp;=&nbsp;3/10&nbsp;=&nbsp;30% and<br />
<i>p</i><sub>c</sub>&nbsp;=&nbsp;5/10&nbsp;=&nbsp;50%.
</p>
<p>
The question is: what&#8217;s the chance of getting 3 <i>a</i>s, 2 <i>c</i>s<br />
in 9 spins?
</p>
<p><div>&nbsp;</div></p>
<p>
Well, first you have to note that 3+2 is less than 9, so there&#8217;s a<br />
hidden condition: the remaining 4 tries should be a <i>b</i>. This<br />
transforms the question into 3 <i>a</i>s, 4 <i>b</i>s and 2 <i>c</i>s,<br />
or<br />
<i>k</i><sub>a</sub>&nbsp;=&nbsp;3,<br />
<i>k</i><sub>b</sub>&nbsp;=&nbsp;4,<br />
<i>k</i><sub>c</sub>&nbsp;=&nbsp;2, Also note that the number of results have<br />
nothing to do with how many appear on the wheel.
</p>
<p><table class="eqtbl" id="eq-eg-wheel">
<tr>
<td class="eqnrcell">(17)</td>
  <td class="eqcell"><br />
<img src='http://www.coranac.com/cgi-bin/mimetex.cgi?%5Cbegin%7Beqnarray%7D%20P%28A%3D3%2CC%3D2%20%5Ctext%7B%20in%20%7D%209%29%20%26%3D%26%20P%28A%3D3%2CB%3D4%2CC%3D2%29%20%5C%5C%20%5Cvspace%7B10%7D%20%5C%5C%20%26%3D%26%20%7B%20%5C%28k_a%20%2B%20k_b%20%2B%20k_c%5C%29%21%20%5Cover%20k_a%21%5C%2Ck_b%21%5C%2Ck_c%21%20%7D%20p_a%5E%7Bk_a%7D%5C%2Cp_b%5E%7Bk_b%7D%5C%2Cp_c%5E%7Bk_c%7D%5C%2C%20%5C%5C%20%5Cvspace%7B10%7D%20%5C%5C%20%26%3D%26%20%7B%20%283%2B4%2B2%29%21%20%5Cover%203%21%204%21%202%21%20%7D%20%5C%28%5Cfrac2%7B10%7D%5C%29%5E3%20%5C%2C%20%5C%28%5Cfrac3%7B10%7D%5C%29%5E4%20%5C%2C%20%5C%28%5Cfrac5%7B10%7D%5C%29%5E2%5C%2C%20%5C%5C%20%5Cvspace%7B10%7D%20%5C%5C%20%26%3D%26%201260%20%5Ccdot%20%5Cfrac%7B8%20%5Ccdot%2081%20%5Ccdot%2025%20%7D%7B10%5E9%7D%20%3D%202.04%20%25%20%5Cend%7Beqnarray%7D'<br />
	title="\begin{eqnarray} P(A=3,C=2 \text{ in } 9) &amp;=&amp; P(A=3,B=4,C=2) \\ \vspace{10} \\ &amp;=&amp; { \(k_a + k_b + k_c\)! \over k_a!\,k_b!\,k_c! } p_a^{k_a}\,p_b^{k_b}\,p_c^{k_c}\, \\ \vspace{10} \\ &amp;=&amp; { (3+4+2)! \over 3! 4! 2! } \(\frac2{10}\)^3 \, \(\frac3{10}\)^4 \, \(\frac5{10}\)^2\, \\ \vspace{10} \\ &amp;=&amp; 1260 \cdot \frac{8 \cdot 81 \cdot 25 }{10^9} = 2.04 % \end{eqnarray}"<br />
	alt="\begin{eqnarray} P(A=3,C=2 \text{ in } 9) &amp;=&amp; P(A=3,B=4,C=2) \\ \vspace{10} \\ &amp;=&amp; { \(k_a + k_b + k_c\)! \over k_a!\,k_b!\,k_c! } p_a^{k_a}\,p_b^{k_b}\,p_c^{k_c}\, \\ \vspace{10} \\ &amp;=&amp; { (3+4+2)! \over 3! 4! 2! } \(\frac2{10}\)^3 \, \(\frac3{10}\)^4 \, \(\frac5{10}\)^2\, \\ \vspace{10} \\ &amp;=&amp; 1260 \cdot \frac{8 \cdot 81 \cdot 25 }{10^9} = 2.04 % \end{eqnarray}" /><br />
</td>
</tr>
</table></p>
<p><h2 id="sec-summary">5
Summary
</h2>
</p>
<p>
As this post covers quite a bit, I think a summary might be in order.
</p>
<ul>
<li>
    Rules of probability:</p>
<ol>
<li>
	    <b>Definition</b>: the probability of event <i>X</i> is the<br />
		number of occurances over the total number of events.<br/></p>
<p><img src='http://www.coranac.com/cgi-bin/mimetex.cgi?P%28X%29%20%3D%20%7B%7B%23%20%5Ctext%7BXs%7D%7D%20%5Cover%20%7B%20%5Ctext%7BAll%20events%7D%7D%7D'<br />
	title="P(X) = {{# \text{Xs}} \over { \text{All events}}}"<br />
	alt="P(X) = {{# \text{Xs}} \over { \text{All events}}}" /><br />
<br/><br />
		Do not forget this rule, as its the basis for all the others.
	  </li>
<li>
	    <b>Complementary rule (NOT)</b>. The probability of not getting<br />
		<i>X</i> is the rest of the cases.<br/></p>
<p><img src='http://www.coranac.com/cgi-bin/mimetex.cgi?P%28%5Cneg%20X%29%20%3D%201%20-%20P%28X%29'<br />
	title="P(\neg X) = 1 - P(X)"<br />
	alt="P(\neg X) = 1 - P(X)" /></p>
</li>
<li>
	    <b>Disjoint/Parallel condition ((X)OR)</b> :<br />
		intersection of cases; probabilities add.<br/></p>
<p><img src='http://www.coranac.com/cgi-bin/mimetex.cgi?P%28X%20%5Coplus%20Y%29%20%3D%20P%28X%29%20%2B%20P%28Y%29'<br />
	title="P(X \oplus Y) = P(X) + P(Y)"<br />
	alt="P(X \oplus Y) = P(X) + P(Y)" /></p>
</li>
<li>
	    <b>Joint/Serial condition (AND)</b> :<br />
		union of cases; probabilities multiply.<br/></p>
<p><img src='http://www.coranac.com/cgi-bin/mimetex.cgi?P%28X%20%5C%26%20Y%29%20%3D%20P%28X%29%20%5Ccdot%20P%28Y%29'<br />
	title="P(X \&amp; Y) = P(X) \cdot P(Y)"<br />
	alt="P(X \&amp; Y) = P(X) \cdot P(Y)" /></p>
</li>
</ol>
<p>	The NOT, XOR and AND terms have commonalities with those used in<br />
	boolean logic, and electrical engineering.
  </li>
<li>
    Compound-condition probabilities usually involve both AND and<br />
    OR conditions, making up the two parts of a<br />
	binomial/multinomial probability.</p>
<ol>
<li>The AND conditions results in the probability of<br />
	    <i>one</i> solution, which multiplies all the probabilities of<br />
	    each sub-sample.
	  </li>
<li>
	    The OR condition counts how many solutions there are,<br />
		given by the bi-/multinomial coefficient.
	  </li>
</ol>
</li>
</ul>
<p><div>&nbsp;</div></p>
<ul>
<li>
    <b>Factorial ( <i>n</i>! )</b>.<br />
	The factorial represents the number of ways you can arrange<br />
	<i>n</i> unique/distinct items.<br/></p>
<p><img src='http://www.coranac.com/cgi-bin/mimetex.cgi?n%21%20%3D%201%5Ccdot2%5Ccdot%20%5C%2C...%5C%2C%20%5Ccdot%28n-1%29%5Ccdot%20n'<br />
	title="n! = 1\cdot2\cdot \,...\, \cdot(n-1)\cdot n"<br />
	alt="n! = 1\cdot2\cdot \,...\, \cdot(n-1)\cdot n" /></p>
</li>
<li>
    <b>Permutation ( <sup>n</sup>P<sub>r</sub> )</b>.<br />
	Permutation refers to how many ways you can distribute <i>r</i><br />
	unique/distinct items over <i>n</i> slots, or when the order<br />
	of the elements matters.<br/></p>
<p><img src='http://www.coranac.com/cgi-bin/mimetex.cgi?%5EnP_r%20%3D%20n_%7B%28r%29%7D%20%3D%20%7B%20%7B%20n%21%7D%20%5Cover%20%7B%28n-r%29%21%20%7D%20%7D'<br />
	title="^nP_r = n_{(r)} = { { n!} \over {(n-r)! } }"<br />
	alt="^nP_r = n_{(r)} = { { n!} \over {(n-r)! } }" /></p>
</li>
<li>
    <b>Combination ( <sup>n</sup>C<sub>r</sub> )</b>.<br />
	Combination refers to how many ways you can distribute <i>r</i><br />
	non-unique/indistinct items over <i>n</i> slots, or when the order<br />
	of the elements does not matter.<br/></p>
<p><img src='http://www.coranac.com/cgi-bin/mimetex.cgi?%5EnC_r%20%3D%20%7B%5EnP_r%7D%2Fr%21%20%3D%20%7B%20n%21%20%5Cover%20r%21%28n-r%29%21%20%7D'<br />
	title="^nC_r = {^nP_r}/r! = { n! \over r!(n-r)! }"<br />
	alt="^nC_r = {^nP_r}/r! = { n! \over r!(n-r)! }" /></p>
</li>
<li>
    <b>Binomial / Multinomial coefficient (<br />
	<i>n</i> choose <i>k</i>(<sub>1</sub>,<br />
	<i>k</i><sub>2</sub>, &hellip; <i>k</i><sub>m</sub>) )</b>,<br />
	The binomial is the number of ways you can arrange 2 groups with<br />
	<i>k</i> and <i>n</i>&minus;<i>k</i> non-unique elements over <i>n</i><br />
	bins. This is basically the same thing as the combination.<br />
	The multinomial is the extension to <i>m</i> groups, with<br />
	<i>k</i><sub>j</sub> elements in each group. In both cases<br />
	you take the base <i>n</i>! for the unique variations, and divide<br />
	by <i>k</i><sub>j</sub>! for the overcounts for all the groups.<br/></p>
<p><img src='http://www.coranac.com/cgi-bin/mimetex.cgi?%5Cbegin%7Beqnarray%7D%20%5Ctext%7BBinomial%3A%20%7D%26%20%7B%20n%20%5Cchoose%20k%7D%3D%20%7B%20n%21%20%5Cover%20k%21%5C%2C%28n-k%29%21%20%7D%20%5C%5C%20%5Ctext%7BMultinomial%3A%20%7D%26%20%7B%20n%20%5Cchoose%20k_1%2C%20k_2%2C%20%5C%2C...%2C%5C%2C%20k_m%20%7D%3D%20%7B%20n%21%20%5Cover%20k_1%21%5C%2Ck_2%21%5C%2C%5Ccdots%5C%2Ck_m%21%20%7D%20%5Cend%7Beqnarray%7D'<br />
	title="\begin{eqnarray} \text{Binomial: }&amp; { n \choose k}= { n! \over k!\,(n-k)! } \\ \text{Multinomial: }&amp; { n \choose k_1, k_2, \,...,\, k_m }= { n! \over k_1!\,k_2!\,\cdots\,k_m! } \end{eqnarray}"<br />
	alt="\begin{eqnarray} \text{Binomial: }&amp; { n \choose k}= { n! \over k!\,(n-k)! } \\ \text{Multinomial: }&amp; { n \choose k_1, k_2, \,...,\, k_m }= { n! \over k_1!\,k_2!\,\cdots\,k_m! } \end{eqnarray}" /></p>
</li>
</ul>
<p>
And lastly,
</p>
<ul>
<li><b>Bi-/multinomial probability</b>.<br />
  When you have <i>m</i> groups with probability <i>p</i><sub>j</sub><br />
  for each group and you want to know the chance of drawing exactly<br />
  <i>k</i><i>j</i> items in<br />
  <i>n</i>&nbsp;=&nbsp;&Sigma;<sub>j</sub>&nbsp;<i>k</i><i>j</i> samples, the<br />
  probability is:<br/><br />
  <table class="eqtbl" id="eq-prob-mnom">
<tr>
<td class="eqnrcell">(18)</td>
  <td class="eqcell"><br />
<img src='http://www.coranac.com/cgi-bin/mimetex.cgi?%5Cbegin%7Beqnarray%7D%20P%28X_1%3Dk_1%5C%2C...%5C%2CX_m%3Dk_m%29%20%26%3D%26%20%7B%20n%20%5Cchoose%20k_1%2C%5C%2C...%2C%5C%2Ck_m%20%7D%20%26%5Ccdot%26%20%5Cprod_%7Bj%3D1%7D%5Em%20p_j%5Em%20%5C%5C%20%26%3D%26%20%7B%20n%21%20%5Cover%20k_1%21%5Ccdot%5C%2C...%5C%2C%20%5Ccdot%20k_m%21%20%7D%20%26%5Ccdot%26%20p_1%5E%7Bk_1%7D%5Ccdot%5C%2C...%5C%2C%5Ccdot%20p_m%5E%7Bk_m%7D%20%5Cend%7Beqnarray%7D'<br />
	title="\begin{eqnarray} P(X_1=k_1\,...\,X_m=k_m) &amp;=&amp; { n \choose k_1,\,...,\,k_m } &amp;\cdot&amp; \prod_{j=1}^m p_j^m \\ &amp;=&amp; { n! \over k_1!\cdot\,...\, \cdot k_m! } &amp;\cdot&amp; p_1^{k_1}\cdot\,...\,\cdot p_m^{k_m} \end{eqnarray}"<br />
	alt="\begin{eqnarray} P(X_1=k_1\,...\,X_m=k_m) &amp;=&amp; { n \choose k_1,\,...,\,k_m } &amp;\cdot&amp; \prod_{j=1}^m p_j^m \\ &amp;=&amp; { n! \over k_1!\cdot\,...\, \cdot k_m! } &amp;\cdot&amp; p_1^{k_1}\cdot\,...\,\cdot p_m^{k_m} \end{eqnarray}" /><br />
</td>
</tr>
</table><br/><br />
  And remember that this holds for just two groups (the binomial case)<br />
  as well, except that often they&#8217;ll just pretend that the second<br />
  group isn&#8217;t really there. Which is unfortunate, as<br />
  that&#8217;s where the (1&minus;p) and (<i>n</i>&minus;<i>k</i>) terms come from.
  </li>
</ul>
<p><!-- EOF --></p>
]]></content:encoded>
			<wfw:commentRss>http://www.coranac.com/2010/05/probabilities-in-simple-random-samples/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Another fast fixed-point sine approximation</title>
		<link>http://www.coranac.com/2009/07/sines/</link>
		<comments>http://www.coranac.com/2009/07/sines/#comments</comments>
		<pubDate>Thu, 16 Jul 2009 20:19:05 +0000</pubDate>
		<dc:creator>cearn</dc:creator>
				<category><![CDATA[code]]></category>
		<category><![CDATA[math]]></category>
		<category><![CDATA[fixed point]]></category>
		<category><![CDATA[nds]]></category>
		<category><![CDATA[research]]></category>
		<category><![CDATA[sine]]></category>

		<guid isPermaLink="false">http://www.coranac.com/?p=87</guid>
		<description><![CDATA[<br />
<b>Warning</b>:  getimagesize() [<a href='function.getimagesize'>function.getimagesize</a>]: Filename cannot be empty in <b>/home/coranac/public_html/wordpress/wp-content/plugins/crnfilters.php</b> on line <b>466</b><br />
<br />
<b>Warning</b>:  getimagesize() [<a href='function.getimagesize'>function.getimagesize</a>]: Filename cannot be empty in <b>/home/coranac/public_html/wordpress/wp-content/plugins/crnfilters.php</b> on line <b>466</b><br />
<br />
<b>Warning</b>:  getimagesize() [<a href='function.getimagesize'>function.getimagesize</a>]: Filename cannot be empty in <b>/home/coranac/public_html/wordpress/wp-content/plugins/crnfilters.php</b> on line <b>466</b><br />
<br />
<b>Warning</b>:  getimagesize() [<a href='function.getimagesize'>function.getimagesize</a>]: Filename cannot be empty in <b>/home/coranac/public_html/wordpress/wp-content/plugins/crnfilters.php</b> on line <b>466</b><br />
<br />
<b>Warning</b>:  getimagesize() [<a href='function.getimagesize'>function.getimagesize</a>]: Filename cannot be empty in <b>/home/coranac/public_html/wordpress/wp-content/plugins/crnfilters.php</b> on line <b>466</b><br />
Gaddammit! &#160; So here I am, looking forward to a nice quiet weekend; hang back, watch some telly and maybe read a bit &#8211; but NNnnneeeEEEEEUUUuuuuuuuu!! Someone had to write an interesting article about sine approximation. With a challenge at the end. And using an inefficient kind of approximation. And so now, instead of just [...]]]></description>
			<content:encoded><![CDATA[<br />
<b>Warning</b>:  getimagesize() [<a href='function.getimagesize'>function.getimagesize</a>]: Filename cannot be empty in <b>/home/coranac/public_html/wordpress/wp-content/plugins/crnfilters.php</b> on line <b>466</b><br />
<br />
<b>Warning</b>:  getimagesize() [<a href='function.getimagesize'>function.getimagesize</a>]: Filename cannot be empty in <b>/home/coranac/public_html/wordpress/wp-content/plugins/crnfilters.php</b> on line <b>466</b><br />
<br />
<b>Warning</b>:  getimagesize() [<a href='function.getimagesize'>function.getimagesize</a>]: Filename cannot be empty in <b>/home/coranac/public_html/wordpress/wp-content/plugins/crnfilters.php</b> on line <b>466</b><br />
<br />
<b>Warning</b>:  getimagesize() [<a href='function.getimagesize'>function.getimagesize</a>]: Filename cannot be empty in <b>/home/coranac/public_html/wordpress/wp-content/plugins/crnfilters.php</b> on line <b>466</b><br />
<br />
<b>Warning</b>:  getimagesize() [<a href='function.getimagesize'>function.getimagesize</a>]: Filename cannot be empty in <b>/home/coranac/public_html/wordpress/wp-content/plugins/crnfilters.php</b> on line <b>466</b><br />
<p></p>
<p>
Gad<i>dammit</i>!
</p>
<p><div>&nbsp;</div></p>
<p>
So here I am, looking forward to a nice quiet weekend; hang<br />
back, watch some telly and maybe read a bit &ndash; but<br />
<i>NNnnneeeEEEEEUUUuuuuuuuu!!</i> <i>Someone</i> had to write an interesting<br />
<a href="http://www.console-dev.de/2009/07/06/sine-approximation-with-fixed-point-math/" rel="pingback">article about sine approximation</a>.<br />
With a <i>challenge</i> at the end. <i>And</i> using an inefficient kind<br />
of approximation. And so now, instead of just relaxing, I have to spend<br />
my entire weekend <i>and</i> most of the week figuring out a better way<br />
of doing it. I hate it when this happens <kbd>&gt;_&lt;</kbd>.
</p>
<p><div>&nbsp;</div></p>
<p>
Okay, maybe not.
</p>
<p><div>&nbsp;</div></p>
<p>
Sarcasm aside, it is an interesting read. While the standard way of<br />
calculating a sine &ndash; via a look-up table &ndash; works and<br />
works well, there&#8217;s just something unsatisfying about it. The<br />
LUT-based approach is just &hellip; dull.<br />
Uninspired. Cowardly. <i>Inelegant</i>.<br />
In contrast, finding a suitable algorithm for it requires effort and a<br />
modicum of creativity, so something like that always piques my interest.
</p>
<p>
In this case it&#8217;s sine approximation. I&#8217;d been wondering about that<br />
when I did my <a href="http://www.coranac.com/documents/arctangent">arctan article</a>,<br />
but figured it would require too many terms to really be worth<br />
the effort. But looking at Mr Schraut&#8217;s post (whose site you should be<br />
visiting from time to time too; there&#8217;s good stuff there) it seems<br />
you can get a decent version quite rapidly. The article centers around<br />
the work found at<br />
<a href="http://www.devmaster.net/forums/showthread.php?t=5784">devmaster thread<br />
5784</a>, which derived the following two equations:
</p>
<p><table class="eqtbl" id="eq-lab">
<tr>
<td class="eqnrcell">(1)</td>
  <td class="eqcell"><br />
<img src='http://www.coranac.com/cgi-bin/mimetex.cgi?%5Cbegin%7Beqnarray%7D%20S_2%28x%29%20%26%3D%26%20%5Cfrac4%5Cpi%20x%20-%20%5Cfrac4%7B%5Cpi%5E2%7D%20x%5E2%20%5C%5C%20%5C%5C%20S_%7B4d%7D%28x%29%20%26%3D%26%20%281-P%29S_2%28x%29%20%2B%20P%20S_2%5E2%28x%29%20%5Cend%7Beqnarray%7D'<br />
	title="\begin{eqnarray} S_2(x) &amp;=&amp; \frac4\pi x - \frac4{\pi^2} x^2 \\ \\ S_{4d}(x) &amp;=&amp; (1-P)S_2(x) + P S_2^2(x) \end{eqnarray}"<br />
	alt="\begin{eqnarray} S_2(x) &amp;=&amp; \frac4\pi x - \frac4{\pi^2} x^2 \\ \\ S_{4d}(x) &amp;=&amp; (1-P)S_2(x) + P S_2^2(x) \end{eqnarray}" /><br />
</td>
</tr>
</table></p>
<p>
These approximations work quite well, but I feel that it actually<br />
uses the wrong starting point. There are alternative approximations<br />
that give more accurate results at nearly no extra cost in<br />
complexity. In this post, I&#8217;ll derive higher-order alternatives for<br />
both. In passing, I&#8217;ll also talk about a few of the tools that can<br />
help analyse functions and, of course, provide some source code and<br />
do some comparisons.
</p>
<p><span id="more-87"></span></p>
<p><ul>
  <li> <a href="#sec-theory">1
Theory
</a> </li>
  <li> <a href="#sec-prod">2
Derivations and implementations
</a> </li>
  <li> <a href="#sec-test">3
Testing
</a> </li>
  <li> <a href="#sec-summary">4
Summary and final thoughts
</a> </li>
</ul>
</p>
<p><h2 id="sec-theory">1
Theory
</h2>
</p>
<p><h3 id="ssec-try-symmetry">1.1
Symmetry
</h3>
</p>
<p>
The first analytical tool is symmetry. Symmetry is actually one of the<br />
most powerful concepts ever conceived. Symmetry of time leads to the<br />
conservation of energy; symmetry of space leads to conservation of<br />
momentum; in a 3D world, symmetry of direction gives rise to the<br />
inverse square law. In many cases, symmetry basically defines the kinds<br />
of functions you&#8217;re looking for.
</p>
<p>
One kind of symmetry is parity, and functions can have parity as well.<br />
Take any function <i>f</i>(<i>x</i>). A function is <dfn>even</dfn> if<br />
<i>f</i>(&minus;<i>x</i>)&nbsp;=&nbsp;<i>f</i>(<i>x</i>); it is <dfn>odd</dfn><br />
if <i>f</i>(&minus;<i>x</i>)&nbsp;=&nbsp;&minus;<i>f</i>(<i>x</i>).
</p>
<p>
This may not sound impressive, but a function&#8217;s parity can be a great<br />
source of information and a way of error checking. For example, the<br />
product of two odd or even functions is an even function, and an<br />
odd-even product is odd (compare positive/negative number products).<br />
If in a calculation you notice this doesn&#8217;t hold true, then you know<br />
there&#8217;s an error somewhere.
</p>
<p>
Symmetry can also significantly reduce the amount of work you need<br />
to do. Take the next sum, for example.
</p>
<p><table class="eqtbl" id="eq-sym">
<tr>
<td class="eqnrcell">(2)</td>
  <td class="eqcell"><br />
<img src='http://www.coranac.com/cgi-bin/mimetex.cgi?y%20%3D%20%5Cint_%7B-N%7D%5EN%20sin%5E7%28x%5E3%29%20%2B%20%5Cfrac%7Bx%5E5%7D%7Bx%5E2%2B1%7D%20-%20x%20e%5E%7B%5Cfrac%7Bx%5E2%7D%7B2%5Csigma%5E2%7D%7D%20dx'<br />
	title="y = \int_{-N}^N sin^7(x^3) + \frac{x^5}{x^2+1} - x e^{\frac{x^2}{2\sigma^2}} dx"<br />
	alt="y = \int_{-N}^N sin^7(x^3) + \frac{x^5}{x^2+1} - x e^{\frac{x^2}{2\sigma^2}} dx" /><br />
</td>
</tr>
</table></p>
<p>
If you find something like this in the wild on on a test, your first<br />
thought might be &ldquo;WTF?!?&rdquo; (assuming you don&#8217;t run away<br />
screaming). As it happens, <i>y</i>&nbsp;=&nbsp;0, for reasons of symmetry. The<br />
function is odd, so the parts left and right of <i>x</i>&nbsp;=&nbsp;0 cancel out.<br />
Instead of actually trying to do the whole calculation, you can just<br />
write down the answer in one line: &ldquo;0, cuz of symmetry&rdquo;.
</p>
<p>
Another property of symmetrical functions is that, if you break them<br />
down into series expansions, odd functions will only have odd terms,<br />
and even functions only have even terms. This becomes important in<br />
the next subsection.
</p>
<p><h3 id="ssec-try-series">1.2
Polynomial and Taylor expansions
</h3>
</p>
<p>
Every function can be broken down into a sum of more manageable<br />
functions. One fairly obvious choice for these sub-functions is<br />
increasing powers of <i>x</i>: polynomials. The most common of<br />
these is 
<a href="http://en.wikipedia.org/wiki/Taylor%20series">Taylor series</a>, which uses<br />
a reference point (<i>a</i>,&nbsp;<i>f</i>(<i>a</i>)) and extrapolates<br />
to another point some distance <i>h</i> away by using the<br />
derivatives of <i>f</i> at the reference point. In equation form,<br />
it looks like this:
</p>
<p><table class="eqtbl" id="eq-taylor-def">
<tr>
<td class="eqnrcell">(3)</td>
  <td class="eqcell"><br />
<img src='http://www.coranac.com/cgi-bin/mimetex.cgi?%5Cbegin%7Beqnarray%7D%20f%28a%2Bh%29%20%26%3D%26%20f%28a%29%20%2B%20f%27%28a%29%20h%20%2B%20%5Cfrac%7Bf%27%27%28a%29%7D%7B2%7Dh%5E2%20%2B%20%5Cfrac%7Bf%27%27%27%28a%29%7D%7B6%7D%20h%5E3%20%2B%20...%20%5C%5C%20%5C%5C%20%5C%5C%20%26%3D%26%20%5Csum_%7Bn%3D0%7D%20%5Cfrac%7Bf%5E%7B%28n%29%7D%28a%29%7D%7Bn%21%7Dh%5En%20%5Cend%7Beqnarray%7D'<br />
	title="\begin{eqnarray} f(a+h) &amp;=&amp; f(a) + f&#039;(a) h + \frac{f&#039;&#039;(a)}{2}h^2 + \frac{f&#039;&#039;&#039;(a)}{6} h^3 + ... \\ \\ \\ &amp;=&amp; \sum_{n=0} \frac{f^{(n)}(a)}{n!}h^n \end{eqnarray}"<br />
	alt="\begin{eqnarray} f(a+h) &amp;=&amp; f(a) + f&#039;(a) h + \frac{f&#039;&#039;(a)}{2}h^2 + \frac{f&#039;&#039;&#039;(a)}{6} h^3 + ... \\ \\ \\ &amp;=&amp; \sum_{n=0} \frac{f^{(n)}(a)}{n!}h^n \end{eqnarray}" /><br />
</td>
</tr>
</table></p>
<p>
Chances are you&#8217;ve actually used part of the Taylor series in game<br />
programming. On implementing movement with acceleration, you&#8217;ll<br />
often see something like Eq&nbsp;4. These are the<br />
first three terms of the Taylor expansion.
</p>
<p><table class="eqtbl" id="eq-taylor-xva">
<tr>
<td class="eqnrcell">(4)</td>
  <td class="eqcell"><br />
<img src='http://www.coranac.com/cgi-bin/mimetex.cgi?x_%7Bnew%7D%20%3D%20x_%7Bold%7D%20%5C%3A%2B%5C%3A%20v%20%5CDelta%20t%20%5C%3A%2B%5C%3A%20%5Cfrac12%20a%20%28%5CDelta%20t%29%5E2'<br />
	title="x_{new} = x_{old} \:+\: v \Delta t \:+\: \frac12 a (\Delta t)^2"<br />
	alt="x_{new} = x_{old} \:+\: v \Delta t \:+\: \frac12 a (\Delta t)^2" /><br />
</td>
</tr>
</table></p>
<p>
Ihe step-size (<i>h</i> in Eq&nbsp;3 and<br />
&Delta;<i>t</i> in Eq&nbsp;4) is small, the<br />
higher-order terms will have less effect on the end result. This<br />
allows you to cut the expansion short at some point. This leaves<br />
you with a short equation that you do the calculations with and<br />
some sort of error term, composed of the part you have removed.<br />
The error term is usually linked to the order you&#8217;ve truncated<br />
the series at; the higher the order, the more accurate the<br />
approximation.
</p>
<p><table class="eqtbl" id="eq-taylor-error">
<tr>
<td class="eqnrcell">(5)</td>
  <td class="eqcell"><br />
<img src='http://www.coranac.com/cgi-bin/mimetex.cgi?f%28a%2Bh%29%20%3D%20f%28a%29%20%2B%20f%27%28a%29%20h%20%2B%20%5Cfrac%7Bf%27%27%28a%29%7D%7B2%7Dh%5E2%20%2B%20%5Cfrac%7Bf%27%27%27%28a%29%7D%7B6%7D%20h%5E3%20%2B%20O%28h%5E4%29'<br />
	title="f(a+h) = f(a) + f&#039;(a) h + \frac{f&#039;&#039;(a)}{2}h^2 + \frac{f&#039;&#039;&#039;(a)}{6} h^3 + O(h^4)"<br />
	alt="f(a+h) = f(a) + f&#039;(a) h + \frac{f&#039;&#039;(a)}{2}h^2 + \frac{f&#039;&#039;&#039;(a)}{6} h^3 + O(h^4)" /><br />
</td>
</tr>
</table><div>&nbsp;</div></p>
<p>
If you work out the math for a sine Taylor series, with <i>a</i>&nbsp;=&nbsp;0<br />
as the reference point, you end up with Eq&nbsp;6.
</p>
<p><table class="eqtbl" id="eq-taylor-sine">
<tr>
<td class="eqnrcell">(6)</td>
  <td class="eqcell"><br />
<img src='http://www.coranac.com/cgi-bin/mimetex.cgi?sin%28h%29%20%3D%20h%20%5C%2C-%5C%2C%20%5Cfrac16%20h%5E3%20%5C%2C%2B%5C%2C%20%5Cfrac1%7B5%21%7D%20h%5E5%20%5C%2C-%5C%2C%20%5Cfrac1%7B7%21%7D%20h%5E7%20%5C%2C%2B%5C%2C%20...'<br />
	title="sin(h) = h \,-\, \frac16 h^3 \,+\, \frac1{5!} h^5 \,-\, \frac1{7!} h^7 \,+\, ..."<br />
	alt="sin(h) = h \,-\, \frac16 h^3 \,+\, \frac1{5!} h^5 \,-\, \frac1{7!} h^7 \,+\, ..." /><br />
</td>
</tr>
</table></p>
<p>
Note that all the even powers are conspicuously absent. This is what<br />
I meant by symmetry being useful: a sine function is odd, therefore<br />
only odd terms are needed in the expansion. But there&#8217;s more to it<br />
than that. The accuracy is given by the highest order in the<br />
approximating polynomial. This shows that there&#8217;s just no point in even<br />
starting with any even-powered polynomial, because you can get one extra<br />
order basically for free!
</p>
<p>
This is why using a quadratic approximation for a sine is somewhat<br />
useless; a cubic will have two terms as well, and be more accurate to<br />
boot. Just because it&#8217;s curved doesn&#8217;t mean a parabola is the most<br />
suitable approximation.
</p>
<p><h3 id="ssec-try-fit">1.3
Curve fitting (and a 3rd order example)
</h3>
</p>
<p>
Using the Taylor series as a basis for a sine approximation is nice,<br />
but it also has a problem. The series is meant to have an infinite<br />
number of terms and when you truncate the series, you will lose<br />
some accuracy. Of course, this was to be expected, but this isn&#8217;t<br />
the real problem; the real problem is that if your function<br />
has some crucial points it <i>must</i> pass through (which is<br />
certainly true for trigonometry functions), the truncation will<br />
move the curve away from those points.
</p>
<p>
To fix this, you need to use a polynomial with as-yet unknown<br />
coefficients (that is, multipliers to the powers) and a set of<br />
conditions that need to be satisfied. These conditions will determine<br />
the exact value of the coefficients. The Taylor expansion can serve<br />
as the basic for your initial approximation, and the final terms<br />
should be pretty close to the Taylor coefficients.
</p>
<p><div>&nbsp;</div></p>
<p>
Let&#8217;s try this for a third-order (cubic) sine approximation.<br />
Technically, a third-order polynomial means four unknowns, <i>but</i>,<br />
since the sine is odd, all the coefficients for the even powers<br />
are zero. That takes care of half the coefficients already. I told<br />
you symmetry was useful <kbd>:)</kbd>. The starting polynomial is<br />
reduced to Eq&nbsp;7, which has two coefficients<br />
<i>a</i> and <i>b</i> that have to be determined. For good measure<br />
I&#8217;ve also added the derivative, as that&#8217;s often useful to have as<br />
well.
</p>
<p><table class="eqtbl" id="eq-s3-base">
<tr>
<td class="eqnrcell">(7)</td>
  <td class="eqcell"><br />
<img src='http://www.coranac.com/cgi-bin/mimetex.cgi?%5Cbegin%7Beqnarray%7D%20S_3%28x%29%20%26%3D%26%20ax%20-%20b%20x%5E3%20%26%3D%26%20x%20%28a%20-%20b%20x%5E2%29%20%5C%5C%20S_3%27%28x%29%20%26%3D%26%20a%20-%203bx%5E2%20%5Cend%7Beqnarray%7D'<br />
	title="\begin{eqnarray} S_3(x) &amp;=&amp; ax - b x^3 &amp;=&amp; x (a - b x^2) \\ S_3&#039;(x) &amp;=&amp; a - 3bx^2 \end{eqnarray}"<br />
	alt="\begin{eqnarray} S_3(x) &amp;=&amp; ax - b x^3 &amp;=&amp; x (a - b x^2) \\ S_3&#039;(x) &amp;=&amp; a - 3bx^2 \end{eqnarray}" /><br />
</td>
</tr>
</table></p>
<p>
Two unknowns means we need two conditional to solve the system.<br />
The most useful conditions are usually the behaviour at the<br />
boundaries. In the case of a sine, that means look at <i>x</i>&nbsp;=&nbsp;0<br />
and/or <i>x</i>&nbsp;=&nbsp;&frac12;&pi;. The latter happens to be more<br />
useful here, so let&#8217;s look at that. First, sin(&frac12;&pi;)&nbsp;=&nbsp;1,<br />
so that&#8217;s a good one. Also, we know that at &frac12;&pi; a sine is<br />
flat (a derivative of 0). This is the second condition.
</p>
<p>
The conditions are listed in Eq&nbsp;8. Solving this<br />
system is rather straightforward and will give you values for<br />
<i>a</i> and <i>b</i>, which are also given in Eq&nbsp;8.<br />
Notice that the values are roughly 5% and 30% away from the pure<br />
Taylor coefficients.
</p>
<p><table class="eqtbl" id="eq-s3-cnd">
<tr>
<td class="eqnrcell">(8)</td>
  <td class="eqcell"><br />
<img src='http://www.coranac.com/cgi-bin/mimetex.cgi?%5Cleft.%20%5Cbegin%7Beqnarray%7D%20S_3%28%5Cfrac%7B%5Cpi%7D2%29%20%26%3D%26%201%20%26%3D%26%20%5Cfrac%7B%5Cpi%7D2%20a%20-%20%28%5Cfrac%7B%5Cpi%7D2%29%5E3%20b%20%5C%5C%20S_3%27%28%5Cfrac%7B%5Cpi%7D2%29%20%26%3D%26%200%20%26%3D%26%20a%20-%203%28%5Cfrac%7B%5Cpi%7D2%29%5E2%20b%20%5Cend%7Beqnarray%7D%20%5C%3B%20%5C%3B%20%5Crightarrow%20%5C%3B%20%5C%3B%20%5Cbegin%7Beqnarray%7D%20a%20%26%3D%26%20%5Cfrac3%5Cpi%20%26%5Capprox%26%200.955%20%5C%5C%20%5C%5C%20b%20%26%3D%26%20%5Cfrac4%7B%5Cpi%5E3%7D%20%26%5Capprox%26%200.129%20%5Cend%7Beqnarray%7D'<br />
	title="\left. \begin{eqnarray} S_3(\frac{\pi}2) &amp;=&amp; 1 &amp;=&amp; \frac{\pi}2 a - (\frac{\pi}2)^3 b \\ S_3&#039;(\frac{\pi}2) &amp;=&amp; 0 &amp;=&amp; a - 3(\frac{\pi}2)^2 b \end{eqnarray} \; \; \rightarrow \; \; \begin{eqnarray} a &amp;=&amp; \frac3\pi &amp;\approx&amp; 0.955 \\ \\ b &amp;=&amp; \frac4{\pi^3} &amp;\approx&amp; 0.129 \end{eqnarray}"<br />
	alt="\left. \begin{eqnarray} S_3(\frac{\pi}2) &amp;=&amp; 1 &amp;=&amp; \frac{\pi}2 a - (\frac{\pi}2)^3 b \\ S_3&#039;(\frac{\pi}2) &amp;=&amp; 0 &amp;=&amp; a - 3(\frac{\pi}2)^2 b \end{eqnarray} \; \; \rightarrow \; \; \begin{eqnarray} a &amp;=&amp; \frac3\pi &amp;\approx&amp; 0.955 \\ \\ b &amp;=&amp; \frac4{\pi^3} &amp;\approx&amp; 0.129 \end{eqnarray}" /><br />
</td>
</tr>
</table></p>
<p>
The final equation is then:
</p>
<p><table class="eqtbl" id="eq-s3">
<tr>
<td class="eqnrcell">(9)</td>
  <td class="eqcell"><br />
<img src='http://www.coranac.com/cgi-bin/mimetex.cgi?S_3%28x%29%20%3D%20%5Cfrac3%5Cpi%20x%20-%20%5Cfrac4%7B%5Cpi%5E3%7D%20x%5E3'<br />
	title="S_3(x) = \frac3\pi x - \frac4{\pi^3} x^3"<br />
	alt="S_3(x) = \frac3\pi x - \frac4{\pi^3} x^3" /><br />
</td>
</tr>
</table></p>
<p>
In Fig&nbsp;1 you can see a number of different<br />
approximations to the sine. Note that I&#8217;ve done a little coordinate<br />
transformation for the <i>x</i>-axis: <i>z</i>&nbsp;=&nbsp;<i>x</i>/(&frac12;&pi;),<br />
so <i>z</i>&nbsp;=&nbsp;1 means <i>x</i>&nbsp;=&nbsp;&frac12;&pi;. The benefit of this<br />
will become clear later.
</p>
<p>
As you can see, the third order Taylor expansion starts out all-right,<br />
but veers off course near the end. In contrast, the third-order fit<br />
matches the sine at both end points. There is also the second-order<br />
fit from the devmaster site. As you can see, the third-order approximation<br />
is closer.
</p>
<div class=cblock>
<div class="cpt" style="width:px;">
  <a href="" target="_blank">  <img src="" id="img-sine-t23"
    alt="" width="" /></a><br />
  <b>Fig&nbsp;1. </b>
</div>

</div>
<p><div>&nbsp;</div></p>
<p>
Now, please remember that coefficients from Eq&nbsp;8<br />
are not the only ones you can use. The conditions define what the<br />
values will be; different conditions lead to different values. For<br />
example, instead using the derivative at &frac12;&pi;, I could have<br />
used it at <i>x</i>&nbsp;=&nbsp;0. This forms the<br />
set of equations of Eq&nbsp;10 and, as you can see,<br />
the coefficients are now different. This set is actually more accurate<br />
(a 0.6% average error instead of 1.1%), but it also has some rather<br />
unsavoury characteristics of having a maximum that&#8217;s not at<br />
&frac12;&pi; and goes over 1.0; this can be <i>really</i> unsettling<br />
if you intend to use the sine in something like rotation.
</p>
<p><table class="eqtbl" id="eq-s3-cnd-alt">
<tr>
<td class="eqnrcell">(10)</td>
  <td class="eqcell"><br />
<img src='http://www.coranac.com/cgi-bin/mimetex.cgi?%5Cleft.%20%5Cbegin%7Beqnarray%7D%20S_3%28%5Cfrac%7B%5Cpi%7D2%29%20%26%3D%26%201%20%26%3D%26%20%5Cfrac%7B%5Cpi%7D2%20a%20-%20%28%5Cfrac%7B%5Cpi%7D2%29%5E3%20b%20%5C%5C%20S_3%27%280%29%20%26%3D%26%201%20%26%3D%26%20a%20%5Cend%7Beqnarray%7D%20%5C%3B%20%5C%3B%20%5Crightarrow%20%5C%3B%20%5C%3B%20%5Cbegin%7Beqnarray%7D%20a%20%26%3D%26%201%20%5C%5C%20%5C%5C%20b%20%26%3D%26%20%5Cfrac4%7B%5Cpi%5E2%7D%281-%5Cfrac2%5Cpi%29%20%5Capprox%200.147%20%5Cend%7Beqnarray%7D'<br />
	title="\left. \begin{eqnarray} S_3(\frac{\pi}2) &amp;=&amp; 1 &amp;=&amp; \frac{\pi}2 a - (\frac{\pi}2)^3 b \\ S_3&#039;(0) &amp;=&amp; 1 &amp;=&amp; a \end{eqnarray} \; \; \rightarrow \; \; \begin{eqnarray} a &amp;=&amp; 1 \\ \\ b &amp;=&amp; \frac4{\pi^2}(1-\frac2\pi) \approx 0.147 \end{eqnarray}"<br />
	alt="\left. \begin{eqnarray} S_3(\frac{\pi}2) &amp;=&amp; 1 &amp;=&amp; \frac{\pi}2 a - (\frac{\pi}2)^3 b \\ S_3&#039;(0) &amp;=&amp; 1 &amp;=&amp; a \end{eqnarray} \; \; \rightarrow \; \; \begin{eqnarray} a &amp;=&amp; 1 \\ \\ b &amp;=&amp; \frac4{\pi^2}(1-\frac2\pi) \approx 0.147 \end{eqnarray}" /><br />
</td>
</tr>
</table></p>
<p><h3 id="ssec-try-dimless">1.4
Dimensionless variables and coordinate transformations
</h3>
</p>
<p>
For higher accuracy, a higher-order polynomial should be used. Before<br />
doing that, though, I&#8217;d like to mention one more trick that can make your<br />
mathematical analysis considerably easier: dimensionless variables.
</p>
<p><div>&nbsp;</div></p>
<p>
The problem with most quantities and equations is units. Metres, feet, litres,<br />
gallons; those kinds of units. Units suck. For one, there are different<br />
units for identical quantities which can be a total pain to convert<br />
and can sometimes lead to disaster.<br />

<a href="http://en.wikipedia.org/wiki/Gimli_Glider">Literally</a>.</p>
<p>Then there&#8217;s the fact that the unit sizes are basically picked at random<br />
and have nothing to do with the physical situation they&#8217;re used for.<br />
So you have weird values for constants like <i>G</i> in<br />

<a href="http://en.wikipedia.org/wiki/Newton%26%238217%3Bs%20law%20of%20universal%20gravitation">Newton&#8217;s law of universal gravitation</a>, the speed of<br />
light <i>c</i> and the 
<a href="http://en.wikipedia.org/wiki/Planck%20constant">Planck constant</a>, <i>h</i>. Keeping<br />
track of these things in equations is annoying, especially since they<br />
tend to pile up and everybody would rather that they&#8217;d just <i>go<br />
away</i>!
</p>
<p>
Enter dimensionless variables. The idea here is that instead of using<br />
standard units, you express quantities as ratios to some meaningful<br />
size. For example, in relativity you often get <i>v</i>/<i>c</i> :<br />
velocity over speed of light. Equations become much simpler if you<br />
just denote velocities as fractions of the speed of light:<br />
&beta;&nbsp;=&nbsp;<i>v</i>/<i>c</i>. Using &beta; in the equations simplifies<br />
them immensely and has the bonus that you&#8217;re not tied to any<br />
specific speed-unit anymore.
</p>
<p>
The dimensionless variable is a type of coordinate transformation.<br />
In particular, it&#8217;s a scaling of the original variable into something<br />
more useful. Another useful transformation is translation: moving<br />
the variable to a more suitable position. We will come accross this<br />
later; but first: an example of dimensionless variables.
</p>
<p><div>&nbsp;</div></p>
<p>
A sine wave has lots of symmetry lines, all revolving around the<br />
quarter-circles. Because of this, the term that keeps showing up<br />
everywhere is &frac12;&pi;. This is the characteristic size of<br />
the wave. By using <i>z</i>&nbsp;=&nbsp;<i>x</i>/(&frac12;&pi;), all those<br />
important points are now at integral <i>z</i> values. Having ones<br />
in your equations is generally a good thing because they tend to<br />
disappear in multiplications. Look at what Eq&nbsp;9<br />
becomes when expressed in terms of <i>z</i>
</p>
<p><table class="eqtbl" id="eq-s3-dimless">
<tr>
<td class="eqnrcell">(11)</td>
  <td class="eqcell"><br />
<img src='http://www.coranac.com/cgi-bin/mimetex.cgi?%5Cbegin%7Beqnarray%7D%20S_3%28x%29%20%26%3D%26%20%5Cfrac3%5Cpi%20x%20-%20%5Cfrac4%7B%5Cpi%5E3%7D%20x%5E3%20%5C%5C%20%5C%5C%20%26%3D%26%20%5Cfrac32%20%5Cfrac%7B2x%7D%5Cpi%20-%20%5Cfrac12%20%28%5Cfrac%7B2x%7D%5Cpi%29%5E3%20%5C%5C%20%5C%5C%20%26%3D%26%20%5Cfrac12%20z%20-%20%5Cfrac12%20z%5E3%20%5C%5C%20%5C%5C%20S_3%28z%29%20%26%3D%26%20%5Cfrac12z%20%283%20-%20z%5E2%29%20%5Cend%7Beqnarray%7D'<br />
	title="\begin{eqnarray} S_3(x) &amp;=&amp; \frac3\pi x - \frac4{\pi^3} x^3 \\ \\ &amp;=&amp; \frac32 \frac{2x}\pi - \frac12 (\frac{2x}\pi)^3 \\ \\ &amp;=&amp; \frac12 z - \frac12 z^3 \\ \\ S_3(z) &amp;=&amp; \frac12z (3 - z^2) \end{eqnarray}"<br />
	alt="\begin{eqnarray} S_3(x) &amp;=&amp; \frac3\pi x - \frac4{\pi^3} x^3 \\ \\ &amp;=&amp; \frac32 \frac{2x}\pi - \frac12 (\frac{2x}\pi)^3 \\ \\ &amp;=&amp; \frac12 z - \frac12 z^3 \\ \\ S_3(z) &amp;=&amp; \frac12z (3 - z^2) \end{eqnarray}" /><br />
</td>
</tr>
</table></p>
<p>
Doesn&#8217;t that look a lot nicer? It goes deeper than that though.<br />
With dimensionless units, the units your measurements are in simply<br />
cease to matter! For angles, this means that whether you&#8217;re working in<br />
radians, degrees or brads, they&#8217;ll all result in the same circle-fraction,<br />
<i>z</i>. This makes converting algorithms to fixed-point notation<br />
considerably easier.
</p>
<p><h2 id="sec-prod">2
Derivations and implementations
</h2>
</p>
<p>
In the section above, I discussed the tools used for analysis and<br />
gave an example of a cubic approximation. In this section I&#8217;ll also<br />
derive high-accuracy fourth and fifth order approximations and<br />
show some implementations. Before that, though, there&#8217;s some<br />
terminology to go through.
</p>
<p>
Since multiple different approximations will be covered, there needs<br />
to be a way to separate all of them. In principle, the sine<br />
approximation will be named <i>S</i><sub>n</sub>, where <i>n</i> is<br />
the order of the polynomial. So that&#8217;ll give <i>S</i><sub>2</sub> to<br />
<i>S</i><sub>5</sub>. I will also use <i>S</i><sub>4d</sub> for the<br />
fourth-order approximation from devmaster. In the derivation of my<br />
own fourth-order function, I&#8217;ll use <i>C</i><sub>n</sub>, because<br />
what will actually be derived is a cosine.
</p>
<p><h3>
Third-order implementation
</h3>
</p>
<p>
Let&#8217;s start with finishing up the story of the third-order<br />
approximation. The main equation for this is Eq&nbsp;11.<br />
Because this equation is still rather simple, I&#8217;ll make this a fixed-point<br />
implementation. The main problem with turning a floating-point function<br />
into a fixed-point one is keeping track of the fixed-point during the<br />
calculations, always making sure there&#8217;s no overflow, but no underflow<br />
either. This is one of the reasons why I wrote Eq&nbsp;11<br />
like it is: by using nested parentheses you can maximize the accuracy<br />
of intermediate calculations and possibly minimize the number of<br />
of intermediate calculations and possibly minimize the number of<br />
operations to boot.
</p>
<p>
To coorectly account for the fixed-point positions, you need to<br />
be aware of the following factors:
</p>
<ul>
<li>
    The scale of the outcome (i.e., the amplitude): 2<sup>A</sup>
  </li>
<li>
    The scale on the inside the parentheses: 2<sup>p</sup>. This is<br />
    necessary to keep the multiplications from overflowing.
  </li>
<li>
    The angle-scale: 2<sup>n</sup>. This is basically the value of<br />
	&frac12;&pi; in the fixed-point system. Using <i>x</i> for the<br />
	angle, you have &nbsp;=&nbsp;<i>x</i>/2<sup>n</sup>.
  </li>
</ul>
<p>
Filling this into Eq&nbsp;11 will give the following:
</p>
<p><table class="eqtbl" id="eq-s3-fp">
<tr>
<td class="eqnrcell">(12)</td>
  <td class="eqcell"><br />
<img src='http://www.coranac.com/cgi-bin/mimetex.cgi?%5Cbegin%7Beqnarray%7D%20S_3%28y%29%20%26%3D%26%20%5Cfrac12%20z%20%283%20-%204z%5E2%29%202%5EA%20%5C%5C%20%26%3D%26%20z%20%283%5Ccdot2%5Ep%20-%20z%5E2%202%5Ep%29%202%5E%7BA-p-1%7D%20%5C%5C%20%26%3D%26%20x%20%283%5Ccdot2%5Ep%20-%20x%5E2%202%5E%7Bp-2n%7D%29%202%5E%7BA-p-n-1%7D%20%5C%5C%20%26%3D%26%20x%20%283%5Ccdot2%5Ep%20-%20x%5E2%20%2F%202%5Er%20%29%20%5Cmiddle%2F%202%5Es%2C%20%5Cend%7Beqnarray%7D'<br />
	title="\begin{eqnarray} S_3(y) &amp;=&amp; \frac12 z (3 - 4z^2) 2^A \\ &amp;=&amp; z (3\cdot2^p - z^2 2^p) 2^{A-p-1} \\ &amp;=&amp; x (3\cdot2^p - x^2 2^{p-2n}) 2^{A-p-n-1} \\ &amp;=&amp; x (3\cdot2^p - x^2 / 2^r ) \middle/ 2^s, \end{eqnarray}"<br />
	alt="\begin{eqnarray} S_3(y) &amp;=&amp; \frac12 z (3 - 4z^2) 2^A \\ &amp;=&amp; z (3\cdot2^p - z^2 2^p) 2^{A-p-1} \\ &amp;=&amp; x (3\cdot2^p - x^2 2^{p-2n}) 2^{A-p-n-1} \\ &amp;=&amp; x (3\cdot2^p - x^2 / 2^r ) \middle/ 2^s, \end{eqnarray}" /><br />
</td>
</tr>
</table></p>
<p>
with <i>r</i>&nbsp;=&nbsp;2<i>n</i>&minus;<i>p</i> and<br />
<i>s</i>&nbsp;=&nbsp;<i>n</i>+<i>p</i>+1&minus;<i>A</i>. These represent the<br />
fixed-point shifts you need to apply to keep everything on the level.<br />
With <i>p</i> as high as multiplication with <i>x</i> will allow and the<br />
standard libnds units leads to the following numbers.
</p>
<div class=lblock>
<table border=1 cellpadding=2 cellspacing=0>
<tr>
<th> A</th>
<th> n</th>
<th> p</th>
<th> r</th>
<th> s</th>
</tr>
<tr>
<td>12</td>
<td>13</td>
<td>15</td>
<td>11</td>
<td>17</td>
</tr>
</table>
</div>
<p><div class="cptfr" style="width:px;">
  <a href="" target="_blank">  <img src="" id="img-quadrants"
    alt="" width="" /></a><br />
  <b>Fig&nbsp;2. </b>
</div>
</p>
<p>
That&#8217;s the calculation necessary for the first quadrant, but the domain<br />
of a sine is infinite. To get the rest of the domain, you can use<br />
the symmetries of the sine: the 2&pi; periodicity and the<br />
&frac12;&pi; mirror symmetries. The first is taken care of by doing<br />
<i>z</i>&nbsp;%&nbsp;4. This reduces the domain to the four quadrants of a<br />
circle. The next part is somewhat tricky, so pay attention.
</p>
<p>
Look at Fig&nbsp;2. <i>S</i><sub>3</sub> works for<br />
quadrant 0. Because it&#8217;s antisymmetric, it will also correctly<br />
calculate quadrant 3, which is equivalent to quadrant &minus;1.<br />
Quadrants 1 and 2 are the problem. As you can see in<br />
Fig&nbsp;2, what needs to happen is for those<br />
quadrants to mirror onto quadrants 0 and &minus;1. A reflection<br />
of <i>x</i> at <i>D</i> is defined by Eq&nbsp;13.<br />
In this case, that means that <i>z</i>&nbsp;=&nbsp;2&nbsp;&minus;&nbsp;<i>z</i>
</p>
<p><table class="eqtbl" id="eq-reflect">
<tr>
<td class="eqnrcell">(13)</td>
  <td class="eqcell"><br />
<img src='http://www.coranac.com/cgi-bin/mimetex.cgi?x%20%3D%20D%20-%20%28x-D%29%20%3D%202D-x'<br />
	title="x = D - (x-D) = 2D-x"<br />
	alt="x = D - (x-D) = 2D-x" /><br />
</td>
</tr>
</table></p>
<p>
Some test need to be done to see when the reflection should take<br />
place. The quadrant numbers in binary are 00, 01, 10, 11. If you<br />
build a truth-table around that, you&#8217;ll see that a XOR of the<br />
two bits will do the trick. If you really want to show off,<br />
you can combine the periodicity modulo and the quadrant test by<br />
doing the arithmetic in the top bits. The implementation is<br />
now complete.
</p>
<div class="cpp">
<div class="cpp proglist" style=" ">s32 isin_S3(s32 x)<br />
{<br />
&nbsp; &nbsp; <span class="co1">// S(x) = x * ( (3&lt;&lt;p) &#8211; (x*x&gt;&gt;r) ) &gt;&gt; s</span><br />
&nbsp; &nbsp; <span class="co1">// n : Q-pos for quarter circle &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 13</span><br />
&nbsp; &nbsp; <span class="co1">// A : Q-pos for output &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 12</span><br />
&nbsp; &nbsp; <span class="co1">// p : Q-pos for parentheses intermediate &nbsp; 15</span><br />
&nbsp; &nbsp; <span class="co1">// r = 2n-p &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 11</span><br />
&nbsp; &nbsp; <span class="co1">// s = A-1-p-n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 17</span></p>
<p>&nbsp; &nbsp; <span class="kw1">static</span> <span class="kw1">const</span> <span class="kw1">int</span> qN = <span class="nu0">13</span>, qA= <span class="nu0">12</span>, qP= <span class="nu0">15</span>, qR= <span class="nu0">2</span>*qN-qP, qS= qN+qP+<span class="nu0">1</span>-qA;</p>
<p>&nbsp; &nbsp; x= x&lt;&lt;(<span class="nu0">30</span>-qN);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">// shift to full s32 range (Q13-&gt;Q30)</span></p>
<p>&nbsp; &nbsp; <span class="kw1">if</span>( (x^(x&lt;&lt;<span class="nu0">1</span>)) &lt; <span class="nu0">0</span>) &nbsp; &nbsp; <span class="co1">// test for quadrant 1 or 2</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; x= (<span class="nu0">1</span>&lt;&lt;<span class="nu0">31</span>) &#8211; x;</p>
<p>&nbsp; &nbsp; x= x&gt;&gt;(<span class="nu0">30</span>-qN);</p>
<p>&nbsp; &nbsp; <span class="kw1">return</span> x * ( (<span class="nu0">3</span>&lt;&lt;qP) &#8211; (x*x&gt;&gt;qR) ) &gt;&gt; qS;<br />
}</div>
</div>
<p>
And, of course, there&#8217;s an assembly version as well. It&#8217;s only ten<br />
instructions, which I think is actually shorter than a LUT+lerp<br />
implementation.
</p>
<div class="gccarm">
<div class="gccarm proglist" style=" "><span class="co1">@ ARM assembly version, using n=13, p=15, A=12</span><br />
<span class="co1">@ Input: gamma in Q13</span><br />
&nbsp; &nbsp; <span class="kw4">.arm</span><br />
&nbsp; &nbsp; <span class="kw4">.align</span><br />
&nbsp; &nbsp; <span class="kw4">.global</span> isin_S3a<br />
isin_S3a:<br />
&nbsp; &nbsp; <span class="re1">mov</span> &nbsp; &nbsp; <span class="kw2">r0</span>, <span class="kw2">r0</span>, <span class="kw1">lsl</span> #(<span class="nu0">30</span>-<span class="nu0">13</span>)<br />
&nbsp; &nbsp; <span class="re1">teq</span> &nbsp; &nbsp; <span class="kw2">r0</span>, <span class="kw2">r0</span>, <span class="kw1">lsl</span> #<span class="nu0">1</span><br />
&nbsp; &nbsp; <span class="re1">rsbmi</span> &nbsp; <span class="kw2">r0</span>, <span class="kw2">r0</span>, #<span class="nu0">1</span>&lt;&lt;<span class="nu0">31</span><br />
&nbsp; &nbsp; <span class="re1">mov</span> &nbsp; &nbsp; <span class="kw2">r0</span>, <span class="kw2">r0</span>, <span class="kw1">asr</span> #(<span class="nu0">30</span>-<span class="nu0">13</span>)<br />
&nbsp; &nbsp; <span class="re1">mul</span> &nbsp; &nbsp; <span class="kw2">r1</span>, <span class="kw2">r0</span>, <span class="kw2">r0</span><br />
&nbsp; &nbsp; <span class="re1">mov</span> &nbsp; &nbsp; <span class="kw2">r1</span>, <span class="kw2">r1</span>, <span class="kw1">asr</span> #<span class="nu0">11</span><br />
&nbsp; &nbsp; <span class="re1">rsb</span> &nbsp; &nbsp; <span class="kw2">r1</span>, <span class="kw2">r1</span>, #<span class="nu0">3</span>&lt;&lt;<span class="nu0">15</span><br />
&nbsp; &nbsp; <span class="re1">mul</span> &nbsp; &nbsp; <span class="kw2">r0</span>, <span class="kw2">r1</span>, <span class="kw2">r0</span><br />
&nbsp; &nbsp; <span class="re1">mov</span> &nbsp; &nbsp; <span class="kw2">r0</span>, <span class="kw2">r0</span>, <span class="kw1">asr</span> #<span class="nu0">17</span><br />
&nbsp; &nbsp; <span class="re2">bx</span>&nbsp; &nbsp; &nbsp; <span class="kw2">lr</span></div>
</div>
<h4>Radians?</h4>
<p>
Oh wait, the requirement was for the input to be in Q12 radians,<br />
right? Weeell, that&#8217;s no biggy. You just have to do the<br />
<i>x</i>&nbsp;&rarr;&nbsp;<i>z</i> conversion yourself. Take, say,<br />
2<sup>20</sup>/(2&pi;). Multiply <i>x</i> by this gives <i>z</i><br />
as a Q30 number; exactly what the first line in the C code resulted in.<br />
This means that all you have to do is change the first line to<br />
`<code>x *= 166886;</code>&#8216;.
</p>
<h4>NDS special</h4>
<p>
The assembly version given above uses standard ARM instructions, but<br />
one of the interesting things is that the NDS&#8217; ARM9 core has special<br />
multiplication instructions. In particular, there is the<br />
<code>SMULWx</code> instruction, which does a word*halfword<br />
multiplication, where the halfword can be either the top or bottom<br />
halfword of operand 2.The main result is 32&times;16&rarr;48 bits<br />
long, of which only the top 32 bits are put in the destination<br />
register. Effectively it&#8217;s like <i>a</i>*<i>b</i>&gt;&gt;16 without<br />
overflow problems. As a bonus, it&#8217;s also slightly faster than the<br />
standard <code>MUL</code>. By slightly changing the parameters,<br />
the down-shift factors <i>r</i> and <i>s</i> can be made 16, fitting<br />
perfectly with this instruction, although the internal accuracy is<br />
made slightly worse. Additionally, careful placement of each<br />
instruction can avoid the interlock cycle that happens for<br />
multiplications.
</p>
<p>
The alternate <code>isin_S3a()</code> becomes:
</p>
<div class="gccarm">
<div class="gccarm proglist" style=" "><span class="co1">@ Special ARM assembly version, using n=13 and lots of Q14</span><br />
<span class="co1">@ Input: gamma in Q13</span><br />
&nbsp; &nbsp; <span class="kw4">.arm</span><br />
&nbsp; &nbsp; <span class="kw4">.align</span><br />
&nbsp; &nbsp; <span class="kw4">.global</span> isin_S3a9<br />
isin_S3a9:<br />
&nbsp; &nbsp; <span class="re1">mov</span> &nbsp; &nbsp; <span class="kw2">r0</span>, <span class="kw2">r0</span>, <span class="kw1">lsl</span> #(<span class="nu0">30</span>-<span class="nu0">13</span>)&nbsp; &nbsp; <span class="co1">@ x &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ; Q30</span><br />
&nbsp; &nbsp; <span class="re1">teq</span> &nbsp; &nbsp; <span class="kw2">r0</span>, <span class="kw2">r0</span>, <span class="kw1">lsl</span> #<span class="nu0">1</span><br />
&nbsp; &nbsp; <span class="re1">rsbmi</span> &nbsp; <span class="kw2">r0</span>, <span class="kw2">r0</span>, #<span class="nu0">1</span>&lt;&lt;<span class="nu0">31</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span class="re2">smulwt</span>&nbsp; <span class="kw2">r1</span>, <span class="kw2">r0</span>, <span class="kw2">r0</span>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">@ y=x*x &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ; Q30*Q14/Q16 = Q28</span><br />
&nbsp; &nbsp; <span class="re1">mov</span> &nbsp; &nbsp; <span class="kw2">r2</span>, #<span class="nu0">3</span>&lt;&lt;<span class="nu0">13</span>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">@ B_14=3/2</span><br />
&nbsp; &nbsp; <span class="re1">sub</span> &nbsp; &nbsp; <span class="kw2">r1</span>, <span class="kw2">r2</span>, <span class="kw2">r1</span>, <span class="kw1">asr</span> #<span class="nu0">15</span> &nbsp; &nbsp; <span class="co1">@ 3/2-y/2 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ; Q14+Q28/Q14/2</span><br />
&nbsp; &nbsp; <span class="re2">smulwt</span>&nbsp; <span class="kw2">r0</span>, <span class="kw2">r1</span>, <span class="kw2">r0</span>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">@ &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ; Q14*Q14/Q16 = Q12</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span class="re2">bx</span>&nbsp; &nbsp; &nbsp; <span class="kw2">lr</span></div>
</div>
<p>
Technically it&#8217;s only two instruction less, but is quite a bit<br />
faster due to the difference in speed between <code>MUL</code><br />
and <code>SMULWx</code>.
</p>
<p><h3 id="ssec-prod-s5">2.1
High-precision, fifth order
</h3>
</p>
<p>
The third order approximation actually still has a substantial error,<br />
so it may be useful to use an additional term. This would be<br />
the fifth-order approximation, <i>S</i><sub>5</sub>. It and its<br />
derivative are given in Eq&nbsp;14.
</p>
<p><table class="eqtbl" id="eq-s5-base">
<tr>
<td class="eqnrcell">(14)</td>
  <td class="eqcell"><br />
<img src='http://www.coranac.com/cgi-bin/mimetex.cgi?%5Cbegin%7Beqnarray%7D%20S_5%28x%29%20%26%3D%26%20ax%20-%20b%20x%5E3%20%2B%20c%20x%5E5%20%5C%5C%20%5C%5C%20%5C%5C%20S_5%27%28x%29%20%26%3D%26%20a%20-%203b%20x%5E2%20%2B%205c%20x%5E4%20%5Cend%7Beqnarray%7D'<br />
	title="\begin{eqnarray} S_5(x) &amp;=&amp; ax - b x^3 + c x^5 \\ \\ \\ S_5&#039;(x) &amp;=&amp; a - 3b x^2 + 5c x^4 \end{eqnarray}"<br />
	alt="\begin{eqnarray} S_5(x) &amp;=&amp; ax - b x^3 + c x^5 \\ \\ \\ S_5&#039;(x) &amp;=&amp; a - 3b x^2 + 5c x^4 \end{eqnarray}" /><br />
</td>
</tr>
</table></p>
<p>
To find the terms, I will again use <i>z</i> instead of <i>x</i>.<br />
The conditions of note are the position and derivative at <i>z</i>&nbsp;=&nbsp;1<br />
and the derivative at 0. With these conditions the approximation<br />
should behave amicably at both edges.
</p>
<p><table class="eqtbl" id="eq-s5-cnd">
<tr>
<td class="eqnrcell">(15)</td>
  <td class="eqcell"><br />
<img src='http://www.coranac.com/cgi-bin/mimetex.cgi?%5Cbegin%7Beqnarray%7D%20S_5%28z%3D1%29%20%26%3D%26%201%20%26%3D%26%20a%20%26-%26%20b%20%26%2B%26%20c%20%5C%5C%20%5C%5C%20%5C%5C%20S%27_5%28z%3D1%29%20%26%3D%26%200%20%26%3D%26%20a%20%26-%26%203b%20%26%2B%26%205c%20%5C%5C%20%5C%5C%20%5C%5C%20S%27_5%28z%3D0%29%20%26%3D%26%20%5Cfrac%5Cpi2%20%26%3D%26%20a%20%5Cend%7Beqnarray%7D'<br />
	title="\begin{eqnarray} S_5(z=1) &amp;=&amp; 1 &amp;=&amp; a &amp;-&amp; b &amp;+&amp; c \\ \\ \\ S&#039;_5(z=1) &amp;=&amp; 0 &amp;=&amp; a &amp;-&amp; 3b &amp;+&amp; 5c \\ \\ \\ S&#039;_5(z=0) &amp;=&amp; \frac\pi2 &amp;=&amp; a \end{eqnarray}"<br />
	alt="\begin{eqnarray} S_5(z=1) &amp;=&amp; 1 &amp;=&amp; a &amp;-&amp; b &amp;+&amp; c \\ \\ \\ S&#039;_5(z=1) &amp;=&amp; 0 &amp;=&amp; a &amp;-&amp; 3b &amp;+&amp; 5c \\ \\ \\ S&#039;_5(z=0) &amp;=&amp; \frac\pi2 &amp;=&amp; a \end{eqnarray}" /><br />
</td>
</tr>
</table></p>
<p>
Notice that these equations are linear with respect to <i>a</i>,<br />
<i>b</i> and <i>c</i>, which means that it can be solved via matrices.<br />
Technically this system of equations forms a 3&times;3 matrix, but since<br />
<i>a</i> is already immediately known it can be reduced to a<br />
2&times;2 system. I&#8217;ll spare you the details, but it leads to the<br />
coefficients of Eq&nbsp;16. Note the complete absence of<br />
any horrid &pi;<sup>5</sup> terms that would have appeared if you had<br />
decided <i>not</i> to use dimensionless terms.
</p>
<p><table class="eqtbl" id="eq-s5-coef">
<tr>
<td class="eqnrcell">(16)</td>
  <td class="eqcell"><br />
<img src='http://www.coranac.com/cgi-bin/mimetex.cgi?%5Cbegin%7Beqnarray%7D%20a%20%26%3D%26%20%5Cpi%2F2%20%5C%5C%20%5C%5C%20b%20%26%3D%26%20%5Cpi%20-%205%2F2%20%5C%5C%20%5C%5C%20c%20%26%3D%26%20%5Cpi%2F2%20-%203%2F2%20%5Cend%7Beqnarray%7D'<br />
	title="\begin{eqnarray} a &amp;=&amp; \pi/2 \\ \\ b &amp;=&amp; \pi - 5/2 \\ \\ c &amp;=&amp; \pi/2 - 3/2 \end{eqnarray}"<br />
	alt="\begin{eqnarray} a &amp;=&amp; \pi/2 \\ \\ b &amp;=&amp; \pi - 5/2 \\ \\ c &amp;=&amp; \pi/2 - 3/2 \end{eqnarray}" /><br />
</td>
</tr>
</table></p>
<p><table class="eqtbl" id="eq-s5-final">
<tr>
<td class="eqnrcell">(17)</td>
  <td class="eqcell"><br />
<img src='http://www.coranac.com/cgi-bin/mimetex.cgi?S_5%28z%29%20%3D%20%5Cfrac12%20z%20%28%5Cpi%20-%20z%5E2%20%5B%20%282%5Cpi-5%29%20-%20z%5E2%20%28%5Cpi%20-%203%29%20%5D%20%29'<br />
	title="S_5(z) = \frac12 z (\pi - z^2 [ (2\pi-5) - z^2 (\pi - 3) ] )"<br />
	alt="S_5(z) = \frac12 z (\pi - z^2 [ (2\pi-5) - z^2 (\pi - 3) ] )" /><br />
</td>
</tr>
</table></p>
<p>
Eq&nbsp;17 is the final quintic approximation in the<br />
form that&#8217;s most accurate and easiest to implement. The implementation<br />
is basically an extension of the <i>S</i><sub>3</sub> function<br />
and left as an exercise for the reader.
</p>
<p><h3 id="ssec-prod-s4">2.2
High precision, fourth order
</h3>
</p>
<p><div class="cptfr" style="width:192px;">
  <a href="" target="_blank">  <img src="" id="img-sincos"
    alt="" width="192" /></a><br />
  <b>Fig&nbsp;3. </b>
</div>
</p>
<p>
Lastly, a fourth-order approximation. Normally, I wouldn&#8217;t even consider<br />
this for a sine (odd function == odd power series and all that), but<br />
since the devmaster post uses them and they even seem to work, there<br />
seems to be something to them after all.
</p>
<p>
The reason those approximations work is simple: they don&#8217;t actually<br />
approximate a sine at all; they approximate a <b>co</b>sine. And,<br />
because of all the symmetries and parallels with sines and cosines,<br />
one can be used to implement the other.
</p>
<p><table class="eqtbl" id="eq-sincos">
<tr>
<td class="eqnrcell">(18)</td>
  <td class="eqcell"><br />
<img src='http://www.coranac.com/cgi-bin/mimetex.cgi?%5Cbegin%7Beqnarray%7D%20sin%28x%29%20%26%3D%26%20cos%28x%20-%20%5Cpi%2F2%29%20%5C%5C%20%5C%5C%20sin%28z%29%20%26%3D%26%20cos%28z%20-%201%29%20%5Cend%7Beqnarray%7D'<br />
	title="\begin{eqnarray} sin(x) &amp;=&amp; cos(x - \pi/2) \\ \\ sin(z) &amp;=&amp; cos(z - 1) \end{eqnarray}"<br />
	alt="\begin{eqnarray} sin(x) &amp;=&amp; cos(x - \pi/2) \\ \\ sin(z) &amp;=&amp; cos(z - 1) \end{eqnarray}" /><br />
</td>
</tr>
</table></p>
<p>
Eq&nbsp;18 is<br />
the transformation you need to perform to turn a cosine into a sine<br />
wave. This can be easily done in at the start of an algorithm.<br />
What&#8217;s left is to derive a cosine approximation. Because a cosine<br />
is even, only even powers will be needed. The base form and its<br />
derivative are given in Eq&nbsp;19.
</p>
<p><table class="eqtbl" id="eq-c4-base">
<tr>
<td class="eqnrcell">(19)</td>
  <td class="eqcell"><br />
<img src='http://www.coranac.com/cgi-bin/mimetex.cgi?%5Cbegin%7Beqnarray%7D%20C_4%20%28x%29%20%26%3D%26%20a%20-%20b%20x%5E2%20%2B%20c%20x%5E4%20%5C%5C%20%5C%5C%20%5C%5C%20C_4%27%28x%29%20%26%3D%26%20-%202b%20x%20%2B%204c%20x%5E3%20%5Cend%7Beqnarray%7D'<br />
	title="\begin{eqnarray} C_4 (x) &amp;=&amp; a - b x^2 + c x^4 \\ \\ \\ C_4&#039;(x) &amp;=&amp; - 2b x + 4c x^3 \end{eqnarray}"<br />
	alt="\begin{eqnarray} C_4 (x) &amp;=&amp; a - b x^2 + c x^4 \\ \\ \\ C_4&#039;(x) &amp;=&amp; - 2b x + 4c x^3 \end{eqnarray}" /><br />
</td>
</tr>
</table></p>
<p>
For the conditions, we once again look at <i>z</i>&nbsp;=&nbsp;0 and <i>z</i>&nbsp;=&nbsp;1,<br />
which comes down to the eqt of equations in Eq&nbsp;20.<br />
One of the interesting thing about even functions is that the<br />
derivative at 0 is zero, so that&#8217;s a freebie. A very important<br />
freebie, as it means that one of the required symmetries happens<br />
automatically.
</p>
<p><table class="eqtbl" id="eq-c4-cnd">
<tr>
<td class="eqnrcell">(20)</td>
  <td class="eqcell"><br />
<img src='http://www.coranac.com/cgi-bin/mimetex.cgi?%5Cbegin%7Beqnarray%7D%20C_4%28z%3D0%29%20%26%3D%26%201%20%26%3D%26%20a%20%5C%5C%20%5C%5C%20%5C%5C%20C_4%28z%3D1%29%20%26%3D%26%200%20%26%3D%26%20a%20%26-%26%20b%20%26%2B%26%20c%20%5C%5C%20%5C%5C%20%5C%5C%20C%27_4%28z%3D1%29%20%26%3D%26%20-%5Cfrac%5Cpi2%20%26%3D%26%20%26-%26%202b%20%26%2B%26%204c%20%5Cend%7Beqnarray%7D'<br />
	title="\begin{eqnarray} C_4(z=0) &amp;=&amp; 1 &amp;=&amp; a \\ \\ \\ C_4(z=1) &amp;=&amp; 0 &amp;=&amp; a &amp;-&amp; b &amp;+&amp; c \\ \\ \\ C&#039;_4(z=1) &amp;=&amp; -\frac\pi2 &amp;=&amp; &amp;-&amp; 2b &amp;+&amp; 4c \end{eqnarray}"<br />
	alt="\begin{eqnarray} C_4(z=0) &amp;=&amp; 1 &amp;=&amp; a \\ \\ \\ C_4(z=1) &amp;=&amp; 0 &amp;=&amp; a &amp;-&amp; b &amp;+&amp; c \\ \\ \\ C&#039;_4(z=1) &amp;=&amp; -\frac\pi2 &amp;=&amp; &amp;-&amp; 2b &amp;+&amp; 4c \end{eqnarray}" /><br />
</td>
</tr>
</table></p>
<p>
The resulting set of coefficients are listed in Eq&nbsp;21.<br />
Note that <i>b</i>&nbsp;=&nbsp;<i>c</i>+1, which may be of use later. The final<br />
equation for the fourth order cosine approximation is<br />
Eq&nbsp;22. Only three MULs and two SUBs; nice.
</p>
<p><table class="eqtbl" id="eq-c4-coef">
<tr>
<td class="eqnrcell">(21)</td>
  <td class="eqcell"><br />
<img src='http://www.coranac.com/cgi-bin/mimetex.cgi?%5Cbegin%7Beqnarray%7D%20a%20%26%3D%26%201%20%5C%5C%20%5C%5C%20b%20%26%3D%26%202%20-%20%5Cpi%2F4%20%5C%5C%20%5C%5C%20c%20%26%3D%26%201%20-%20%5Cpi%2F4%20%5Cend%7Beqnarray%7D'<br />
	title="\begin{eqnarray} a &amp;=&amp; 1 \\ \\ b &amp;=&amp; 2 - \pi/4 \\ \\ c &amp;=&amp; 1 - \pi/4 \end{eqnarray}"<br />
	alt="\begin{eqnarray} a &amp;=&amp; 1 \\ \\ b &amp;=&amp; 2 - \pi/4 \\ \\ c &amp;=&amp; 1 - \pi/4 \end{eqnarray}" /><br />
</td>
</tr>
</table></p>
<p><table class="eqtbl" id="eq-c4-final">
<tr>
<td class="eqnrcell">(22)</td>
  <td class="eqcell"><br />
<img src='http://www.coranac.com/cgi-bin/mimetex.cgi?C_4%28z%29%20%3D%201%20-%20z%5E2%20%5B%20%282-%5Cpi%2F4%29%20-%20z%5E2%20%281-%5Cpi%2F4%29%20%5D'<br />
	title="C_4(z) = 1 - z^2 [ (2-\pi/4) - z^2 (1-\pi/4) ]"<br />
	alt="C_4(z) = 1 - z^2 [ (2-\pi/4) - z^2 (1-\pi/4) ]" /><br />
</td>
</tr>
</table></p>
<h4>Implementation</h4>
<p>
The floating-point implementation of Eq&nbsp;22 is<br />
again too easy to mention here, so I&#8217;ll focus on fixed-point<br />
variations. Like with <i>S</i><sub>3</sub>, you can mix and match<br />
fixed-point positions until you get something you like. In this<br />
case I&#8217;ll stick to Q14 for almost everything to keep things simple.
</p>
<p>
The real trick here is to find out what you need to do about all the<br />
other quadrants. Cutting down to four quadrants is, again, easy.<br />
For the rest, remember that the cosine approximation calculates the top<br />
quadrants and you need to flip the sign for the bottom quadrants.<br />
If you think in terms of the parameter that a sine gets, you see that<br />
only for odd semi-circles the sign needs to change. Tracing this<br />
can be done with a single bitwise AND or a clever shift.
</p>
<div class="cpp">
<div class="cpp proglist" style=" "><span class="co1">//! A sine approximation via &nbsp;a fourth-order cosine approx.</span><br />
s32 isin_S4(s32 x)<br />
{<br />
&nbsp; &nbsp; <span class="kw1">int</span> c, x2, y;<br />
&nbsp; &nbsp; <span class="kw1">static</span> <span class="kw1">const</span> <span class="kw1">int</span> qN= <span class="nu0">13</span>, qA= <span class="nu0">12</span>, B=<span class="nu0">19900</span>, C=<span class="nu0">3516</span>;</p>
<p>&nbsp; &nbsp; c= x&lt;&lt;(<span class="nu0">30</span>-qN);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">// Semi-circle info into carry.</span><br />
&nbsp; &nbsp; x -= <span class="nu0">1</span>&lt;&lt;qN; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">// sine -&gt; cosine calc</span></p>
<p>&nbsp; &nbsp; x= x&lt;&lt;(<span class="nu0">31</span>-qN);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">// Mask with PI</span><br />
&nbsp; &nbsp; x= x&gt;&gt;(<span class="nu0">31</span>-qN);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">// Note: SIGNED shift! (to qN)</span><br />
&nbsp; &nbsp; x= x*x&gt;&gt;(<span class="nu0">2</span>*qN-<span class="nu0">14</span>);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">// x=x^2 To Q14</span></p>
<p>&nbsp; &nbsp; y= B &#8211; (x*C&gt;&gt;<span class="nu0">14</span>); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">// B &#8211; x^2*C</span><br />
&nbsp; &nbsp; y= (<span class="nu0">1</span>&lt;&lt;qA)-(x*y&gt;&gt;<span class="nu0">16</span>); &nbsp; &nbsp; &nbsp; <span class="co1">// A &#8211; x^2*(B-x^2*C)</span></p>
<p>&nbsp; &nbsp; <span class="kw1">return</span> c&gt;=<span class="nu0">0</span> ? y : -y;<br />
}</div>
</div>
<p>
And an ARM9 assembly version too. As it happens, it&#8217;s only two<br />
instuctions longer than <code>isin_S3a9()</code>.
</p>
<div class="gccarm">
<div class="gccarm proglist" style=" "><span class="co1">@ ARM assembly version of S4 = C4(gamma-1), using n=13, A=12 and &#8230; miscellaneous.</span><br />
<span class="co1">@ Input: gamma in Q13</span><br />
&nbsp; &nbsp; <span class="kw4">.arm</span><br />
&nbsp; &nbsp; <span class="kw4">.align</span><br />
&nbsp; &nbsp; <span class="kw4">.global</span> isin_S4a9<br />
isin_S4a9:<br />
&nbsp; &nbsp; <span class="re1">movs</span>&nbsp; &nbsp; <span class="kw2">r0</span>, <span class="kw2">r0</span>, <span class="kw1">lsl</span> #(<span class="nu0">31</span>-<span class="nu0">13</span>)&nbsp; &nbsp; <span class="co1">@ r0=x%2 &lt;&lt;31 &nbsp; &nbsp; &nbsp; ; carry=x/2</span><br />
&nbsp; &nbsp; <span class="re1">sub</span> &nbsp; &nbsp; <span class="kw2">r0</span>, <span class="kw2">r0</span>, #<span class="nu0">1</span>&lt;&lt;<span class="nu0">31</span>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">@ r0 -= 1.0 &nbsp; &nbsp; &nbsp; &nbsp; ; sin &lt;-&gt; cos</span><br />
&nbsp; &nbsp; <span class="re2">smulwt</span>&nbsp; <span class="kw2">r1</span>, <span class="kw2">r0</span>, <span class="kw2">r0</span>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">@ r1 = x*x&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ; Q31*Q15/Q16=Q30</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span class="re2">ldr</span> &nbsp; &nbsp; <span class="kw2">r2</span>,=<span class="nu0">14016</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">@ C = (1-pi/4)&lt;&lt;16</span><br />
&nbsp; &nbsp; <span class="re2">smulwt</span>&nbsp; <span class="kw2">r0</span>, <span class="kw2">r2</span>, <span class="kw2">r1</span>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">@ C*x^2&gt;&gt;16 &nbsp; &nbsp; &nbsp; &nbsp; ; Q16*Q14/Q16 = Q14</span><br />
&nbsp; &nbsp; <span class="re1">add</span> &nbsp; &nbsp; <span class="kw2">r2</span>, <span class="kw2">r2</span>, #<span class="nu0">1</span>&lt;&lt;<span class="nu0">16</span>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">@ B = C+1</span><br />
&nbsp; &nbsp; <span class="re1">rsb</span> &nbsp; &nbsp; <span class="kw2">r0</span>, <span class="kw2">r0</span>, <span class="kw2">r2</span>, <span class="kw1">asr</span> #<span class="nu0">2</span>&nbsp; &nbsp; &nbsp; <span class="co1">@ B &#8211; C*x^2 &nbsp; &nbsp; &nbsp; &nbsp; ; Q14</span><br />
&nbsp; &nbsp; <span class="re2">smulwb</span>&nbsp; <span class="kw2">r0</span>, <span class="kw2">r1</span>, <span class="kw2">r0</span>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">@ x^2 * (B-C*x^2) &nbsp; ; Q30*Q14/Q16 = Q28</span><br />
&nbsp; &nbsp; <span class="re1">mov</span> &nbsp; &nbsp; <span class="kw2">r1</span>, #<span class="nu0">1</span>&lt;&lt;<span class="nu0">12</span><br />
&nbsp; &nbsp; <span class="re1">sub</span> &nbsp; &nbsp; <span class="kw2">r0</span>, <span class="kw2">r1</span>, <span class="kw2">r0</span>, <span class="kw1">asr</span> #<span class="nu0">16</span> &nbsp; &nbsp; <span class="co1">@ 1 &#8211; x^2 * (B-C*x^2)</span><br />
&nbsp; &nbsp; <span class="re1">rsbcs</span> &nbsp; <span class="kw2">r0</span>, <span class="kw2">r0</span>, #<span class="nu0">0</span>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">@ Flip sign for odd semi-circles.</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span class="re2">bx</span>&nbsp; &nbsp; &nbsp; <span class="kw2">lr</span></div>
</div>
<p><h2 id="sec-test">3
Testing
</h2>
</p>
<p>
Deriving approximations is nice and all, but there&#8217;s really no point<br />
unless you do some sort of test to see how well they perform. I&#8217;ll<br />
look at two things: accuracy and some speed-tests. For the speed-test,<br />
I&#8217;ll only consider the functions given here along with some traditional<br />
ones. The accuracy test is done only for the first quadrant and in<br />
floating-point, but the results should carry over well to a fixed-point<br />
case. Finally, I&#8217;ll show how you can optimize the functions for accuracy.
</p>
<p><h3 id="ssec-test-speed">3.1
Third and fourth-order speed
</h3>
</p>
<p>
For the speed test I calculated the sine at 256 points for<br />
<i>x</i>&nbsp;&isin;&nbsp;[0,&nbsp;2&pi;). There will be some loop-overhead<br />
in the numbers, but it should be small. Tests were performed on the<br />
NDS.
</p>
<p>
Functions under investigation are the three <i>S</i><sub>3</sub> and<br />
two <i>S</i><sub>4</sub> functions given earlier. I&#8217;ve also tested<br />
the standard floating-point <code>sin()</code> library function,<br />
the libnds <code>sinLerp()</code> and my own <code>isin()</code><br />
function that you can find in<br />
<a href="http://www.coranac.com/documents/arctangent#ssec-atan-sin">arctan:sine</a>.<br />
The cumulative and average times can be found in<br />
Table&nbsp;1.
</p>
<div class=lblock>
<table id="tbl-isin-speed"<br />
  border=1 cellpadding=3 cellspacing=0><br />
<caption align=bottom>
  <b>Table&nbsp;1</b>: sine cycle-times (roughly).<br />
</caption>
<tr>
<th>Function (thumb/ARM) </th>
<th>Total cycles</th>
<th>average cycles</th>
<tr class=rnum>
<th>sin (F)</th>
<td>300321</td>
<td>1175.1</td>
</tr>
<tr class=rnum>
<th>sinLerp (T)</th>
<td>10051</td>
<td>39.2</td>
</tr>
<tr class=rnum>
<th>isin (T)</th>
<td>7401</td>
<td>28.9</td>
</tr>
<tr class=rnum>
<th>isin_S3 (T)</th>
<td>5267</td>
<td>20.5</td>
</tr>
<tr class=rnum>
<th>isin_S4 (T)</th>
<td>6456</td>
<td>25.2</td>
</tr>
<tr class=rnum>
<th>isin_S3a (A)</th>
<td>3438</td>
<td>13.4</td>
</tr>
<tr class=rnum>
<th>isin_S3a9 (A)</th>
<td>2591</td>
<td>10.1</td>
</tr>
<tr class=rnum>
<th>isin_S4a9 (A)</th>
<td>3123</td>
<td>12.1</td>
</tr>
</table>
</div>
<p>
The first thing that should be clear is just why we don&#8217;t use the<br />
floating-point sine. I mean, seriously. There is also a clear difference<br />
between the Thumb-compiled and ARM assembly versions, the latter being<br />
significantly faster.
</p>
<p>
Within the compiled versions, I find it interesting to see that the<br />
algorithmic calculations are actually faster than the LUT+lerp-based<br />
implementations. I guess loading all those numbers from memory<br />
really does suck.
</p>
<p>
And <i>then</i> there&#8217;s the assembly versions. Wow. Compared to the<br />
compiled version they&#8217;re twice as fast, and up to four times as fast<br />
as the LUT-based functions.
</p>
<p><div class=note>
<div  class=nhcare>NDS timers measure half-cycles</div>
</p>
<p>
The cycle-times from Table&nbsp;1 do not make sense<br />
if you count instruction cycles. For example, for <code>isin_S3a</code><br />
the function overhead alone should already be around 10 cycles. The<br />
thing here is that the numbers are taken from the hardware timers,<br />
which use the bus-frequency (33 MHz) rather than the ARM9 cpu (66 MHz).<br />
As such, it measures in half-cycles. For details, see<br />
<a href="http://nocash.emubase.de/gbatek.htm/#dsmemorytimings">gbatek:nds-timings</a>.
</p>
<p></div>
</p>
<p><h3 id="ssec-test-acc">3.2
Accuracy
</h3>
</p>
<p>
Fig&nbsp;4 shows all the approximations in one graph.<br />
It only shows one quadrant because the rest can be retrieved by<br />
symmetry. I&#8217;ve also scaled the sine and its approximations by<br />
2<sup>12</sup> because that&#8217;s the scale that usual fixed-point scale<br />
right now. And to be sure, yes, this is a different chart than<br />
Fig&nbsp;1; it&#8217;s just hard to tell because the<br />
fourth and fifth order functions are virtually identical to the<br />
real sine line.
</p>
<div class=cblock>
<div class="cpt" style="width:px;">
  <a href="" target="_blank">  <img src="" id="img-sine-all"
    alt="" width="" /></a><br />
  <b>Fig&nbsp;4. </b>
</div>

</div>
<p>
For the high-accuracy approximations, it&#8217;s better to look at<br />
Fig&nbsp;5, which shows the errors. Here you can<br />
clearly see a difference between <i>S</i><sub>4d</sub> and<br />
<i>S</i><sub>5</sub>, the latter is roughly 3 times better.
</p>
<p>
There&#8217;s also a large difference between the devmaster fourth-order<br />
sine and my own. The reason behind this is a difference in conditions.<br />
In my case, I&#8217;ve fixed the derivatives at both end-points, which<br />
always results in an over- or underestimate. The devmaster&#8217;s<br />
<i>S</i><sub>4d</sub> let go of those conditions and minimized the<br />
error. I&#8217;ll also do this in the next sub-section.
</p>
<div class=cblock>
<div class="cpt" style="width:px;">
  <a href="" target="_blank">  <img src="" id="img-sine-all-err"
    alt="" width="" /></a><br />
  <b>Fig&nbsp;5. </b>
</div>

</div>
<p><div>&nbsp;</div></p>
<p>
Table&nbsp;2 and Table&nbsp;3<br />
list some interesting statistics about<br />
the various approximations, namely the minimum, average and maximum<br />
errors. It also contains a 
<a href="http://en.wikipedia.org/wiki/Root%20Mean%20Square%20Deviation">Root Mean Square Deviation</a> (RMSD), which is a<br />
special kind of distance. If you consider the data-points as a<br />
vector, the RMSD is the average Pythagorean length for each point.<br />
Table&nbsp;2 is normed to 2<sup>12</sup>, whereas<br />
Table&nbsp;3 is table for the traditional floating-point<br />
sine scale.
</p>
<p>
The RMSD values are probably the most useful to look at. From them<br />
you can see that there is a huge gap between the low-accuracy and<br />
high-accuracy functions of about a factor 60. And if you do your<br />
math right, all it costs is one multiplication and one addition,<br />
and maybe some extra shifts in the fixed-point case. That&#8217;s quite<br />
a bargain. Compared to that, the difference between the odd and<br />
even functions is somewhat meager: only a factor three or so.<br />
Still, it is something.
</p>
<p>
If you look at the fixed-point table, you can see that the error<br />
you make with <i>S</i><sub>4d</sub> and<br />
<i>S</i><sub>5</sub> is in the single digits. This means<br />
that this is probably accurate enough for practical purposes.<br />
Combined with the fact that even fifth order polynomials can be<br />
made pretty fast, this makes them worth considering over LUTs.
</p>
<div class=cblock>
<table width=80%>
<tr>
<td>
<table id="tbl-stats" border=1 cellpadding=3 cellspacing=0>
<caption align=bottom>
  <b>Table&nbsp;2</b>: error statistics for 2<sup>12</sup>sin(x) approx.<br />
</caption>
<tr class=top>
<th></th>
<th>min</th>
<th>avg</th>
<th>max</th>
<th>rms</th>
</tr>
<tr class=rnum>
<th>Taylor3</th>
<td>-302.1</td>
<td>-51.5</td>
<td>0</td>
<td>92.7</td>
</tr>
<tr class=rnum>
<th>S2</th>
<td>0</td>
<td>123.1</td>
<td>229.4</td>
<td>146.8</td>
</tr>
<tr class=rnum>
<th>S3</th>
<td>-82.0</td>
<td>-47.6</td>
<td>0</td>
<td>55.0</td>
</tr>
<tr class=rnum>
<th>S4d</th>
<td>-4.47</td>
<td>0.19</td>
<td>3.11</td>
<td>2.44</td>
</tr>
<tr class=rnum>
<th>S4</th>
<td>0</td>
<td>5.87</td>
<td>11.4</td>
<td>7.11</td>
</tr>
<tr class=rnum>
<th>S5</th>
<td>0</td>
<td>0.74</td>
<td>1.62</td>
<td>0.94</td>
</tr>
</table>
</td>
<td>
<table id="tbl-statsp" border=1 cellpadding=3 cellspacing=0>
<caption align=bottom>
  <b>Table&nbsp;3</b>: error statistics in percentages.<br />
</caption>
<tr class=top>
<th></th>
<th>min%</th>
<th>avg%</th>
<th>max%</th>
<th>rms%</th>
</tr>
<tr class=rnum>
<th>Taylor3</th>
<td>-7.37</td>
<td>-1.26</td>
<td>0</td>
<td>2.26</td>
</tr>
<tr class=rnum>
<th>S2</th>
<td>0</td>
<td>3</td>
<td>5.6</td>
<td>3.58</td>
</tr>
<tr class=rnum>
<th>S3</th>
<td>-2</td>
<td>-1.16</td>
<td>0</td>
<td>1.34</td>
</tr>
<tr class=rnum>
<th>S4d</th>
<td>-0.11</td>
<td>0.0047</td>
<td>0.076</td>
<td>0.06</td>
</tr>
<tr class=rnum>
<th>S4</th>
<td>0</td>
<td>0.143</td>
<td>0.278</td>
<td>0.174</td>
</tr>
<tr class=rnum>
<th>S5</th>
<td>0</td>
<td>0.018</td>
<td>0.039</td>
<td>0.023</td>
</tr>
</table>
</td>
</table>
</div>
<p><h3 id="ssec-test-opt">3.3
Optimizing higher-order approximations
</h3>
</p>
<p>
From the charts, you can see that <i>S</i><sub>4</sub> and<br />
<i>S</i><sub>5</sub> all err on the same side of the sine line. You<br />
can increase the accuracy of the approximation by tweaking the<br />
coefficients in such a way that the errors are redistributed in<br />
a preferable way. Two methods are possible here: shoot for a zero<br />
error average, or minimize the RMSD. Technically minimizing the<br />
RMSD is standard (it comes down to least-squares optimization), but<br />
because a zero-average allows for an analytical solution, I&#8217;ll use<br />
that. In any case, the differences in outcomes will be small.
</p>
<p><div>&nbsp;</div></p>
<p>
First, think of what an average of a function means. The average<br />
of a set of numbers is the sum divided by the size of the set. For<br />
functions, it&#8217;s the integral of that function divided by the<br />
interval. When you want a zero-average for an approximation, the<br />
integral of the function and that of the approximation should<br />
be equal. With a polynomial approximation to a sine, we get:
</p>
<p><table class="eqtbl" id="eq-cnd-avg0">
<tr>
<td class="eqnrcell">(23)</td>
  <td class="eqcell"><br />
<img src='http://www.coranac.com/cgi-bin/mimetex.cgi?%5Cbegin%7Beqnarray%7D%20%5Cint_0%5E1%20%5Csum_n%20a_n%20x%5En%20dx%20%26%3D%26%20%5Cint_0%5E1%20sin%28x%5Cpi%2F2%29%20dx%20%5C%3B%5C%3B%20%5Crightarrow%20%5C%5C%20%5C%5C%20%5Csum_n%20%5Cfrac%7Ba_n%7D%7Bn%2B1%7D%20%26%3D%26%202%2F%5Cpi%20%2C%20%5Cend%7Beqnarray%7D'<br />
	title="\begin{eqnarray} \int_0^1 \sum_n a_n x^n dx &amp;=&amp; \int_0^1 sin(x\pi/2) dx \;\; \rightarrow \\ \\ \sum_n \frac{a_n}{n+1} &amp;=&amp; 2/\pi , \end{eqnarray}"<br />
	alt="\begin{eqnarray} \int_0^1 \sum_n a_n x^n dx &amp;=&amp; \int_0^1 sin(x\pi/2) dx \;\; \rightarrow \\ \\ \sum_n \frac{a_n}{n+1} &amp;=&amp; 2/\pi , \end{eqnarray}" /><br />
</td>
</tr>
</table></p>
<p>
with <i>a</i><sub>n</sub> reducing to the coefficients of the<br />
polynomials we had before. This can be used as an alternate condition<br />
to the derivative at 0. For <i>S</i><sub>4</sub> and<br />
<i>S</i><sub>5</sub>, you&#8217;ll end up with the following coefficients.
</p>
<p><!--</p>
<p>
The simplest way to do this is to just dump the data into Excel<br />
and let the Solver do its magic on all the coefficients. A better<br />
way is first see what actually needs to change. We still have some<br />
conditions that need to be satisfied: 0 and 1 at the boundaries<br />
and a maximum at <i>z</i>&nbsp;=&nbsp;1. Only the derivative at 0 is flexible,<br />
and this leads to some restrictions in the search. In fact, it<br />
turns out that only one coefficient needs to be minimized for, and<br />
the rest follow from its value.
</p>
<p>
Taking all that into account, you get the following<br />
coefficients for <i>S</i><sub>4</sub> and<br />
<i>S</i><sub>5</sub>.
</p>
<p>&#8211;></p>
<p><table class="eqtbl" id="eq-s4-opt-coef">
<tr>
<td class="eqnrcell">(24)</td>
  <td class="eqcell"><br />
<img src='http://www.coranac.com/cgi-bin/mimetex.cgi?%5Cbegin%7Beqnarray%7D%20a_4%20%26%3D%26%201%20%5C%5C%20b_4%20%26%3D%26%20c_4%2B1%20%5C%5C%20c_4%20%26%3D%26%205%281-%5Cfrac3%5Cpi%29%20%5Capprox%200.225351707%20%5Cend%7Beqnarray%7D'<br />
	title="\begin{eqnarray} a_4 &amp;=&amp; 1 \\ b_4 &amp;=&amp; c_4+1 \\ c_4 &amp;=&amp; 5(1-\frac3\pi) \approx 0.225351707 \end{eqnarray}"<br />
	alt="\begin{eqnarray} a_4 &amp;=&amp; 1 \\ b_4 &amp;=&amp; c_4+1 \\ c_4 &amp;=&amp; 5(1-\frac3\pi) \approx 0.225351707 \end{eqnarray}" /><br />
</td>
</tr>
</table></p>
<p><table class="eqtbl" id="eq-s5-opt-coef">
<tr>
<td class="eqnrcell">(25)</td>
  <td class="eqcell"><br />
<img src='http://www.coranac.com/cgi-bin/mimetex.cgi?%5Cbegin%7Beqnarray%7D%20a_5%20%26%3D%26%204%28%5Cfrac3%5Cpi%20-%20%5Cfrac9%7B16%7D%29%20%5Capprox%201.569718634%20%5C%5C%20b_5%20%26%3D%26%202%20a_5%20-%205%2F2%20%5C%5C%20c_5%20%26%3D%26%20a_5%20-%203%2F2%20%5Cend%7Beqnarray%7D'<br />
	title="\begin{eqnarray} a_5 &amp;=&amp; 4(\frac3\pi - \frac9{16}) \approx 1.569718634 \\ b_5 &amp;=&amp; 2 a_5 - 5/2 \\ c_5 &amp;=&amp; a_5 - 3/2 \end{eqnarray}"<br />
	alt="\begin{eqnarray} a_5 &amp;=&amp; 4(\frac3\pi - \frac9{16}) \approx 1.569718634 \\ b_5 &amp;=&amp; 2 a_5 - 5/2 \\ c_5 &amp;=&amp; a_5 - 3/2 \end{eqnarray}" /><br />
</td>
</tr>
</table></p>
<p>
If you&#8217;re still awake and remember the devmaster <i>S</i><sub>4d</sub><br />
coefficients, there should be something familiar about<br />
<i>a</i><sub>4</sub>. Yes, they&#8217;re practically identical. If you<br />
optimize <i>S</i><sub>4</sub> for the RMSD, you actually get the exact<br />
same function as <i>S</i><sub>4d</sub>.
</p>
<p>
Table&nbsp;4 shows the statistics for the original<br />
approximations and the new optimized versions, <i>S</i><sub>4o</sub><br />
and <i>S</i><sub>5o</sub>. The numbers for <i>S</i><sub>4o</sub><br />
are basically those from <i>S</i><sub>4d</sub> seen earlier. More<br />
interesting are the details for <i>S</i><sub>5o</sub>. The maximum<br />
and minimum errors are now within &plusmn;1. That is to say,<br />
this approximation gives values that are at most 1 off from the<br />
proper Q12 sine. This is about as good as any Q12<br />
approximation is able to get.
</p>
<div class=lblock>
<table id="tbl-stats-opt" border=1 cellpadding=3 cellspacing=0>
<caption align=bottom>
  <b>Table&nbsp;4</b>: Optimized Q12 <i>S</i><sub>4</sub> and <i>S</i><sub>5</sub>.<br />
</caption>
<tr>
<th class=top></th>
<th class=top>min</th>
<th class=top>avg</th>
<th class=top>max</th>
<th class=top>rmsd</th>
</tr>
<tr class=rnum>
<th>S4</th>
<td>0</td>
<td>5.87</td>
<td>11.4</td>
<td>7.11</td>
</tr>
<tr class=rnum>
<th>S5</th>
<td>0</td>
<td>0.74</td>
<td>1.616</td>
<td>0.94</td>
</tr>
<tr class=rnum>
<th>S4o</th>
<td>-4.72</td>
<td>0</td>
<td>2.89</td>
<td>2.47</td>
</tr>
<tr class=rnum>
<th>S5o</th>
<td>-0.73</td>
<td>0</td>
<td>0.79</td>
<td>0.52</td>
</tr>
</table>
</div>
<div class=cblock>
<div class="cpt" style="width:px;">
  <a href="" target="_blank">  <img src="" id="img-sine-err45-opt"
    alt="" width="" /></a><br />
  <b>Fig&nbsp;6. </b>
</div>

</div>
<p><h2 id="sec-summary">4
Summary and final thoughts
</h2>
</p>
<p>
Here&#8217;s a few things to take from all this.
</p>
<ul>
<li>Symmetry is your friend.</li>
<li>
    When constructing a polynomial approximation, more terms mean<br />
    higher accuracy. Symmetry properties of the function approximated<br />
    allow you to remove terms from consideration, simplifying the<br />
    equation.
  </li>
<li>
    Coordinate transformations are your friends too. Sometimes<br />
    it&#8217;s much easier to work on a scaled or moved version of the<br />
    original problem.<br />
    If your situation has a characteristic length (or time,<br />
	velocity, whatever) consider using dimensionless variables:<br />
	expressing parameters as ratios of the characteristic length.<br />
	This makes the initial units pretty much irrelevant. For<br />
	angles, think circle-fractions.
  </li>
<li>
    Zero and one (0 and 1) are the best values to have in your<br />
	equations, as they tend to vanish to easily.
  </li>
<li>
    Any approximation formula will have coefficients to be determined.<br />
    In general, the Taylor series terms are <i>not</i> the best<br />
    set; values slightly offset from these terms will be better as<br />
    they can correct for the truncation. To determine the values of<br />
    the coefficients, define some conditions that need to be<br />
	satisfied. Examples of conditions are values of the function and<br />
	its derivative at the boundaries, or its integrals. Or you can<br />
	wuss out and just dump the thing in the Excel Solver.
  </li>
<li>
    When converting to fixed-point, accuracy and overflow comes into<br />
    the fray. If you know the domain of the function beforehand, you can<br />
    optimize for accuracy. Also, it helps if you construct the<br />
	algorithm in a sort of recursive form instead of a pure<br />
	polynomial: not <i>a</i><i>x</i>&nbsp;+&nbsp;<i>b</i><i>x</i><sup>2</sup><br />
	but	<i>x</i>(<i>a</i>&nbsp;+&nbsp;<i>x</i><i>b</i>). Ordered like this,<br />
	each new additional term only requires one multiplication and<br />
	one addition extra.
  </li>
<li>
    For fixed-point work, <code>SMULWx</code> is teh awesome.
  </li>
<li>
    Even a fourth order (and presumably fifth order as well) polynomial<br />
    implementation in C is faster than the LUT-based sines on the NDS.<br />
	And specialized assembly versions are considerably faster still.
  </li>
<li>
    The difference in accuracy of<br />
    <i>S</i><sub>4</sub> vs <i>S</i><sub>2</sub> or<br />
    <i>S</i><sub>5</sub> vs <i>S</i><sub>3</sub> is huge: a factor of<br />
    60. Going from an even to the next odd approximation only gains you<br />
	a factor 3. Shame; I&#8217;d hoped it&#8217;d be more.
  </li>
<li>
    Unlike I initially thought, the even-powered polynomials work<br />
	out quite well. This is because they&#8217;re actually modified cosine<br />
	approximations.
  </li>
</ul>
<h4>Exercises for the reader</h4>
<ol>
<li>
    Express the parabolic approximation <i>S</i><sub>2</sub>(<i>x</i>)<br />
	of Eq&nbsp;1 in terms of <i>z</i>. &#8216;s Not hard, I promise.
  </li>
<li>
    Implement the fixed-point version of the fifth-order sine<br />
	approximation, <i>S</i><sub>5</sub>(<i>x</i>).
  </li>
<li>
    For the masochists: derive the coefficients for <i>S</i><sub>5</sub>(<i>x</i>)<br />
	<i>without</i> dimensionless variables. That is to say, with<br />
	the conditions at <i>x</i>&nbsp;=&nbsp;&frac12;&pi; instead of <i>z</i>&nbsp;=&nbsp;1.
  </li>
<li>
    Solve Eq&nbsp;24 and Eq&nbsp;25 for<br />
    minimal RMDS. Also, try to derive an analytical form for minimal RMDS;<br />
    I think it&#8217;s exists, but it may be tricky to come up with the right form.
  </li>
</ol>
]]></content:encoded>
			<wfw:commentRss>http://www.coranac.com/2009/07/sines/feed/</wfw:commentRss>
		<slash:comments>23</slash:comments>
		</item>
		<item>
		<title>On arctangent.</title>
		<link>http://www.coranac.com/2009/02/on-arctangent/</link>
		<comments>http://www.coranac.com/2009/02/on-arctangent/#comments</comments>
		<pubDate>Wed, 25 Feb 2009 10:43:17 +0000</pubDate>
		<dc:creator>cearn</dc:creator>
				<category><![CDATA[documents]]></category>
		<category><![CDATA[math]]></category>
		<category><![CDATA[arctan]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[docs]]></category>
		<category><![CDATA[research]]></category>
		<category><![CDATA[trig]]></category>

		<guid isPermaLink="false">http://www.coranac.com/?p=65</guid>
		<description><![CDATA[The arctangent is one of the more interesting trigonometry functions &#8211; and by &#8220;interesting&#8221; I of course mean a bitch to get right. I&#8217;ve been meaning to write something about the various methods of calculating it for a while now and finally got round to it recently. I, ah, uhm, may have gotten a little [...]]]></description>
			<content:encoded><![CDATA[<p>
The arctangent is one of the more interesting trigonometry functions &ndash; and by &ldquo;interesting&rdquo; I of course mean a bitch to get right. I&#8217;ve been meaning to write something about the various methods of calculating it for a while now and finally got round to it recently.
</p>
<p>
I, ah, uhm, may have gotten a little carried away with it though &#8230; but that&#8217;s okay,! At least now I while one to recommend.
</p>
<ul>
<li>The document itself: <a href="/documents/arctangent/">Off on a tangent : a look at arctangent implementations</a>
  </li>
<li>
    Related source code: <a href="/files/misc/trig-src.zip">trig-src.zip</a>. Also contains decent <code>sin()</code>, <code>cos()</code> and <code>tan()</code> implementations.
  </li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://www.coranac.com/2009/02/on-arctangent/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>New doc: matrices from a geometry perspective</title>
		<link>http://www.coranac.com/2008/06/matrices-for-geometry/</link>
		<comments>http://www.coranac.com/2008/06/matrices-for-geometry/#comments</comments>
		<pubDate>Wed, 25 Jun 2008 14:19:55 +0000</pubDate>
		<dc:creator>cearn</dc:creator>
				<category><![CDATA[documents]]></category>
		<category><![CDATA[math]]></category>
		<category><![CDATA[docs]]></category>
		<category><![CDATA[matrix]]></category>
		<category><![CDATA[trig]]></category>
		<category><![CDATA[vector]]></category>

		<guid isPermaLink="false">http://www.coranac.com/?p=53</guid>
		<description><![CDATA[&#8220;Matrices from a geometry perspective&#8221; is now out. Vector and matrix math is used heavily in computer graphics because it involved geometry and coordinate transformations. While this is widely known, I&#8217;ve seen many people struggle with the basic concepts of how it&#8217;s supposed to work. this is especially true in the GBA/NDS community, where erroneous [...]]]></description>
			<content:encoded><![CDATA[<p>
&ldquo;<a href="http://www.coranac.com/documents/geomatrix/">Matrices from a geometry perspective</a>&rdquo; is now out. Vector and matrix math is used heavily in computer graphics because it involved geometry and coordinate transformations. While this is widely known, I&#8217;ve seen many people struggle with the basic concepts of how it&#8217;s supposed to work. this is especially true in the GBA/NDS community, where erroneous information about <a href="http://www.coranac.com/tonc/text/affine.htm">the affine matrix</a> is still found in many demos and even library code even today. And that just <i>kills</i> me, because the fundamentals are really quite simple to grasp once you look at it from the right direction<span class="fnote"><a href="#ft-nr1" title="I&#8217;m not saying it&#8217;s not easy to make mistakes here (it&#8217;s still math we&#8217;re talking about), but the basic concepts are not that complex.">(1)</a></span>
</p>
<p>
I&#8217;ve wanted to write something on the subject for quite some time. Yeah, there were a few sections in Tonc that covered it, but not in the amount of detail I wanted it. A recent email (thanks Ian) finally spurred me to write something down.
</p>
<p>
In the document, I describe what points, vectors and coordinates really are in geometrical terms. This may seem obvious, but it&#8217;s always good to go over the basics again because overlooking those is often the reason for misunderstandings. Then it describes how coordinate transformations work and how matrices fit into the subject.
</p>
<p>
I&#8217;ve tried to keep the hardcore math to a minimum in order to keep it understandable. I&#8217;d really like to get some feedback on this to know if it <i>is</i> comprehensible to people who don&#8217;t already know how all this stuff works. If there are other items that anyone feels I should add, I&#8217;ll consider that as well.<br />
<span class=sm>I should probably also note that many parts of it have already been rewritten three times already, so if sentences don&#8217;t make sense, let me know so I can fix it.</span>
</p>
<p></p>
<p>
linky: <a href="http://www.coranac.com/documents/geomatrix/">Matrices from a geometry perspective</a>.</p>
<hr /><div class="footnotes">
<h5>Notes:</h5>
<ol>
<li id="ft-nr1"> 
  I&#8217;m not saying it&#8217;s not easy to make mistakes here (it&#8217;s still math we&#8217;re talking about), but the basic concepts are not that complex.
</li>
</ol>
</div
<hr />
]]></content:encoded>
			<wfw:commentRss>http://www.coranac.com/2008/06/matrices-for-geometry/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Testing latex</title>
		<link>http://www.coranac.com/2008/02/testing-latex/</link>
		<comments>http://www.coranac.com/2008/02/testing-latex/#comments</comments>
		<pubDate>Fri, 22 Feb 2008 17:00:02 +0000</pubDate>
		<dc:creator>cearn</dc:creator>
				<category><![CDATA[math]]></category>

		<guid isPermaLink="false">http://www.coranac.com/2008/02/22/testing-latex/</guid>
		<description><![CDATA[No, not the rubber stuff; the math stuff. According to the WP codex, it should be as simple as this: $latex i\hbar\frac{\partial}{\partial t}\left&#124;\Psi(t)\right>=H\left&#124;\Psi(t)\right>$ OK, so that doesn&#8217;t work :\. Ah right, that&#8217;s just for when you have the blog under the wordpress.org itself. Right &#8230; mimetex then. * finds wp-latexrender.zip here. * looks for install [...]]]></description>
			<content:encoded><![CDATA[<p>
No, not the rubber stuff; the math stuff. According to the WP codex, it<br />
should be as simple as this:
</p>
<p>$latex i\hbar\frac{\partial}{\partial t}\left|\Psi(t)\right>=H\left|\Psi(t)\right>$</p>
<p>
OK, so that doesn&#8217;t work <kbd>:\</kbd>. Ah right, that&#8217;s just for when you<br />
have the blog under the wordpress.org itself. Right &hellip; mimetex then.
</p>
<p>* finds <tt>wp-latexrender.zip</tt> <a href="http://sixthform.info/steve/wordpress/">here</a>.</p>
<p>* looks for install procedure.</p>
<p>* looks for <i>clear</i> install procedure.</p>
<p>* tries the install anyway.</p>
<p>* reads the <tt>mimetex.php</tt> in full because while everyone says<br />
  to update the path directories, it&#8217;s unclear what to set them <i>to</i><br />
  especially as they seem to be double.</p>
<p>* sighs and rewrites the whole function because the variables and how<br />
  they are used are &hellip; silly.</p>
<p></p>
<p>
Right. How about this then: &#8220;&#91;tex]x^2+y^2[/tex]&#8220;?
</p>
<p><img src='/img/mimetex/8dfaf8281769c217b7e78b27a4747285.gif'<br />
	title="x^2+y^2"<br />
	alt="x^2+y^2" /></p>
<p>
Yaaay! <kbd>:D</kbd>. And now for some more interesting forms:
</p>
<p><img src='/img/mimetex/8208e6112240f26cbedf99d7a4d76e3a.gif'<br />
	title="E = mc^2 \label{clever}"<br />
	alt="E = mc^2 \label{clever}" /></p>
<p>
OK, so apparently I can&#8217;t put the labels into mimetex. Let&#8217;s see<br />
what else will and will not work &#8230;
</p>
<p><img src='/img/mimetex/508c4a61c6922d5aacbf6249fc921a06.gif'<br />
	title="\lim_{n \to \infty}\sum_{k=1}^n \frac{1}{k^2} = \frac{\pi^2}{6}"<br />
	alt="\lim_{n \to \infty}\sum_{k=1}^n \frac{1}{k^2} = \frac{\pi^2}{6}" /></p>
<p>
Okay &#8230; so the default version doesn&#8217;t like newlines either. So let&#8217;s<br />
tweak the formula so that all whitespace (and repeated white) it<br />
replaced by a single space &#8230; &#8216;<code>/\s+/ /g</code>&#8216; should do<br />
the trick.
</p>
<p><img src='/img/mimetex/3cffd92b165de34bb521c56d883d09ed.gif'<br />
	title="\LARGE\tilde y=\left\{ {\ddot x\text{ if $\vec x$ odd}\atop\hat{\,\bar x+1}\text{ if even}}\right."<br />
	alt="\LARGE\tilde y=\left\{ {\ddot x\text{ if $\vec x$ odd}\atop\hat{\,\bar x+1}\text{ if even}}\right." /></p>
<p></p>
<p><img src='/img/mimetex/76d85a25bad6f624b275a0dafef2da22.gif'<br />
	title="\small\hspace{10}\unitlength{.75} \picture(120,220){~(60,200){\circle(120,40)}~(0,20){\line(0,180)} (5,189;0,-30){\pict(110,20){(c20,10;70;2){ \pict(40,20){(20,10){\circle(40,20)}(c10,10)+(c30,10)-}}~}~} (119,20){\line(0,180)}~(60,20){\circle(120,40;34)}}"<br />
	alt="\small\hspace{10}\unitlength{.75} \picture(120,220){~(60,200){\circle(120,40)}~(0,20){\line(0,180)} (5,189;0,-30){\pict(110,20){(c20,10;70;2){ \pict(40,20){(20,10){\circle(40,20)}(c10,10)+(c30,10)-}}~}~} (119,20){\line(0,180)}~(60,20){\circle(120,40;34)}}" /></p>
<p></p>
<p><img src='/img/mimetex/3140aa57b009e4d9608eb144b41d4bb7.gif'<br />
	title="\bold C( \theta, \phi) = \bold R_y(\phi) \cdot \bold R_x(\theta) = \begin{bmatrix} \cos (\phi) &amp;amp; \sin(\phi) \sin(\theta) &amp;amp; -\sin(\phi) \cos(\theta)\\ 0 &amp;amp; \cos(\theta) &amp;amp; \sin(\theta) \\ \sin(\phi) &amp;amp; -\cos(\phi)\sin(\theta) &amp;amp; \cos(\phi)\sin(\theta) \end{bmatrix}"<br />
	alt="\bold C( \theta, \phi) = \bold R_y(\phi) \cdot \bold R_x(\theta) = \begin{bmatrix} \cos (\phi) &amp;amp; \sin(\phi) \sin(\theta) &amp;amp; -\sin(\phi) \cos(\theta)\\ 0 &amp;amp; \cos(\theta) &amp;amp; \sin(\theta) \\ \sin(\phi) &amp;amp; -\cos(\phi)\sin(\theta) &amp;amp; \cos(\phi)\sin(\theta) \end{bmatrix}" /></p>
<p>
And to try some inline stuff:<br />
<img src='/img/mimetex/24f616b8e39379ee9c11851860560bde.gif'<br />
	title="m \vec{a} = -k \vec{x}"<br />
	alt="m \vec{a} = -k \vec{x}" /><br />
. Or maybe<br />
<img src='/img/mimetex/ef00dea0f93c4b877c835c2ac20d1fb0.gif'<br />
	title="m \vec{a} = -k \vec{x}"<br />
	alt="m \vec{a} = -k \vec{x}" /><br />
 for something a little smaller. Yay, that works as well.
</p>
<p>
Anyway, for anyone who also wants math on their site, you can get mimetex from from <a href="http://www.forkosh.com/mimetex.html">http://www.forkosh.com/mimetex.html</a>.<br />
Although the site starts with the source, there are precompiled binaries in<br />
the back somewhere.<br />
I would recommend rewriting <tt>mimetex.php</tt>, though: I found it<br />
somewhat tricky to get it working. It relies on 4 paths, but it doesn&#8217;t exactly<br />
say what they were supposed to represent or what they should be set to;<br />
when I tried it locally the only way to get them to work was to do stuff that<br />
had little relation to the fill-in-the-blanks parts of the paths.
</p>
<p>It doesn&#8217;t like whitespace either in or around the formula, so removing that<br />
can be useful; so is feeding the through<br />
<code>rawurlencode()</code> if you want to put it into the url: HTMLTidy<br />
really doesn&#8217;t like all the backslashes and stuff in the url.<br />
Adding options to the &#91;tex] tag is also a good idea (like size; see above).<br />
And what&#8217;s <i>especially</i> useful is a preview mode that doesn&#8217;t render<br />
to a file. That way you won&#8217;t be left with hundreds of little temporary bitmaps<br />
for all the failed variants. <span class=mini>(I suppose I could give you mine,<br />
but it&#8217;s not finished yet.)</span>
</p>
<p>
Lastly, if you&#8217;re new to TeX, consider using <a href="http://www.codecogs.com/components/equationeditor/equationeditor.php">this site</a> as an editor. There is a larger manual on the mimetex maintainer&#8217;s site, but this little tool is very useful.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.coranac.com/2008/02/testing-latex/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

<!--
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML//EN">
<html>
<head>
</head>
<body>
<p>
My database has called in sick. Please imagine some 
annoying elevator tune till he gets back.
</p>
<p>
<small>[[Doo-di-doo tooo. Dum-di-dum-di-doo-dooo.]]</small>
</p>
</body>
</html>

-->
