<?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>Dragons in the Algorithm &#187; Math</title>
	<atom:link href="http://mcherm.com/permalinks/1/category/math/feed" rel="self" type="application/rss+xml" />
	<link>http://mcherm.com</link>
	<description>Adventures in Programming</description>
	<lastBuildDate>Tue, 08 Jun 2010 18:54:48 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>A random selection algorithm</title>
		<link>http://mcherm.com/permalinks/1/a-random-selection-algorithm</link>
		<comments>http://mcherm.com/permalinks/1/a-random-selection-algorithm#comments</comments>
		<pubDate>Fri, 27 Jun 2008 11:19:29 +0000</pubDate>
		<dc:creator>mcherm</dc:creator>
				<category><![CDATA[Math]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://mcherm.com/?p=79</guid>
		<description><![CDATA[Suppose you want to select k things randomly from a list of n items, with no duplicates. Of course, if k &#62; n, then it&#8217;s hopeless, and if k = n it&#8217;s trivial, so the interesting case is when k &#60; n. A naive approach might pick an item randomly, and try again if it&#8217;s [...]]]></description>
			<content:encoded><![CDATA[<p>Suppose you want to select k things randomly from a list of n items, with no duplicates.<span id="more-79"></span> Of course, if k &gt; n, then it&#8217;s hopeless, and if k = n it&#8217;s trivial, so the interesting case is when k &lt; n. A naive approach might pick an item randomly, and try again if it&#8217;s a duplicate.</p>
<p><code> </code></p>
<pre>var selected = new List();
while( len(selected) &lt; k ) {
    var newIndex = floor(random() * n);
    if ( items[newIndex] not in selected ) {
        selected.append( items[newIndex] );
    }
}</pre>
<p><a title="Dice" href="http://www.flickr.com/photos/wilhei/109404009/"><img src="http://mcherm.com/blog/wp-content/uploads/2008/06/dice.jpg" border="0" alt="Some Dice" hspace="2" vspace="2" width="240" height="179" align="right" /></a>This may work fine if k is much smaller than n, but when k is large and close to n, this can be disastrously slow. Near the end, most of the items are already selected, so we keep picking items and having to &#8220;throw them back&#8221;. The alternative of selecting which items to omit (instead of selecting the ones to keep) fails in the same way for small values of k.</p>
<p>Instead, there&#8217;s a simple trick that is guaranteed to never require more than n random numbers. As we get to each item, we select it or not with the &#8220;correct&#8221; probability. Here is the pseudocode:</p>
<p><code> </code></p>
<pre>var selected = new List();
var needed = k;
var available = n;
var position = 0;
while( len(selected) &lt; k ) {
    if( random() &lt; needed / available ) {
        selected.append( items[position] )
        needed -= 1;
    }
    available -= 1;
}</pre>
<p>Handy trick&#8230; I&#8217;ve needed this algorithm quite a few times.</p>
]]></content:encoded>
			<wfw:commentRss>http://mcherm.com/permalinks/1/a-random-selection-algorithm/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>The Butterfly Bug</title>
		<link>http://mcherm.com/permalinks/1/the-butterfly-effect</link>
		<comments>http://mcherm.com/permalinks/1/the-butterfly-effect#comments</comments>
		<pubDate>Fri, 18 Apr 2008 02:16:41 +0000</pubDate>
		<dc:creator>mcherm</dc:creator>
				<category><![CDATA[Math]]></category>

		<guid isPermaLink="false">http://mcherm.com/permalinks/1/the-butterfly-effect</guid>
		<description><![CDATA[Edward Lorenz died this past Thursday at age 90. This is as good an excuse as I will ever have to recount the story of his discovery (which I first learned from the book Chaos by James Gleick). It is a tale of math and the proper use of computers.
Edward Lorenz had majored in math, [...]]]></description>
			<content:encoded><![CDATA[<p>Edward Lorenz died this past Thursday at age 90. This is as good an excuse as I will ever have to recount the story of his discovery (which I first learned from the book <a href="http://www.amazon.com/Chaos-Making-Science-James-Gleick/dp/0140092501/" title="Book: Chaos">Chaos by James Gleick</a>). It is a tale of math and the proper use of computers.<span id="more-68"></span></p>
<p><img src="http://mcherm.com/blog/wp-content/uploads/2008/04/edwardlorentz.JPG" alt="Edward Lorenz" align="right" height="204" hspace="2" vspace="2" width="200" />Edward Lorenz had majored in math, but after working as an Air Corp weather forecaster during WWII he found himself working as a meteorologist. In 1961 he was one of the first to seriously apply computer modeling to the prediction of weather. He ran simulations which took hours to calculate, and printed their output every so often. One day he wanted to continue an interrupted run and to save time didn&#8217;t want to start the simulation over again from the beginning, so he started it by typing in the numbers from one of the printouts near the end of the previous run.</p>
<p>He didn&#8217;t start from the FINAL printout because, of course, there was a danger of typing in a value incorrectly, and he wanted to check against the next couple of printouts in case he&#8217;d made a typo. Curiously, the numbers on the subsequent printouts were close, <em>but not exactly right!</em> A normal person would have shrugged their shoulders and ignored it, but Lorenz investigated further.</p>
<p>After some experimentation, he discovered that the discrepancy was due to rounding: he was typing in 0.506 when the computer was storing 0.506127. The result was that the output diverged, slowly at first but eventually giving a completely different result. Here is a sample from his output:</p>
<p align="center"><img src="http://mcherm.com/blog/wp-content/uploads/2008/04/butterfly_graph.png" alt="Diverging graph from Lorenz’s 1961 printouts" height="259" hspace="2" vspace="2" width="474" /></p>
<p align="left">At this point a normal person would have shrugged and just gone back to start from the beginning again, but Lorenz realized there was something deeply wrong about this.  The weather measurements that he was working from were not accurate to 6 decimal places — if accuracy at this level mattered, then perhaps all the research he was doing was meaningless! Further investigation showed that he could reproduce the problem, and that it wasn&#8217;t due to a error in his program: if there was a bug it lay, not in his program, but in the mathematics behind it!</p>
<p align="left">He tried simplifying his equations, seeking to discover the simplest case that still exhibited the behavior (this is a standard debugging technique used by programmers everywhere). Eventually he got a <a href="http://www.cmp.caltech.edu/~mcc/Chaos_Course/Lesson1/Demo8.html" title="demo of chaos">simple system of just 3 equations</a> that exhibited this property, dubbed &#8220;Sensitive Dependence on Initial Conditions&#8221;. Under certain circumstances (which apply to weather predictions), tiny variations in input values will grow over time until they completely alter the results of the formula. This discovery was popularized as the &#8220;butterfly effect&#8221;: a butterfly choosing whether or not to flap its wings could alter <a href="http://www.hurricane-katrina.org/" title="Big weather patterns.">weather patterns</a> a few months hence half the world away. This is the reason that computers have gotten hundreds of times faster and bigger, yet <a href="http://www.weather.gov/">weather forecasters</a> still only predict about a week in advance.</p>
<p align="left"><a href="http://www.flickr.com/photos/santarosa/261923723/"><img src="http://mcherm.com/blog/wp-content/uploads/2008/04/fractal_1.jpg" alt="Fractal 1" align="right" height="100" hspace="3" vspace="2" width="100" /></a><a href="http://www.flickr.com/photos/allegra_ricci/2296417761/"><img src="http://mcherm.com/blog/wp-content/uploads/2008/04/fractal_2.thumbnail.jpg" alt="Fractal 2" align="left" height="75" hspace="3" vspace="2" width="100" /></a> Of course, it goes far beyond the weather. Lorenz&#8217;s discovery proved to be at the root of an entire field of mathematics (now called &#8220;Chaos Theory&#8221;) which encompassed many oddities from strange attractors to <a href="http://nedbatchelder.com/code/aptus/" title="A fractal generator">fractals</a>.</p>
<p align="left">There are several lessons in this story. You have to be willing to question your fundamental assumptions: a tiny difference in the last decimal point turned out to contain a discovery which undermined his entire field of study&#8230; and opened up a new one. There&#8217;s also a lesson about computers: they can be very helpful, but too much trust can lead you astray. Invalid input, invalid assumptions, invalid equations, odd mathematical chaos&#8230; there are many pitfalls. And finally, an experiment that <em>fails</em> may in the end be even more useful than one that succeeds.</p>
]]></content:encoded>
			<wfw:commentRss>http://mcherm.com/permalinks/1/the-butterfly-effect/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>When Spheres Collide</title>
		<link>http://mcherm.com/permalinks/1/when-spheres-collide</link>
		<comments>http://mcherm.com/permalinks/1/when-spheres-collide#comments</comments>
		<pubDate>Thu, 15 Nov 2007 21:05:18 +0000</pubDate>
		<dc:creator>mcherm</dc:creator>
				<category><![CDATA[Math]]></category>

		<guid isPermaLink="false">http://mcherm.com/blog/?p=15</guid>
		<description><![CDATA[If I can&#8217;t use my own blog to post nicely worked out math problems, then what good is it?

So the other day, I had a problem to solve. I had two circles (the initial problem was in 2D, but the solution works for spheres in any number of dimensions). The circles were moving in what [...]]]></description>
			<content:encoded><![CDATA[<p align="left">If I can&#8217;t use my own blog to post nicely worked out math problems, then what good is it?</p>
<p><span id="more-15"></span></p>
<p align="left">So the other day, I had a problem to solve. I had two circles (the initial problem was in 2D, but the solution works for spheres in any number of dimensions). The circles were moving in what I can assume are straight lines, and I want to know when (if ever) they will intersect.</p>
<p align="left">Start with some definitions. I&#8217;ll specify the circles with initial position and velocity (vectors, as you can tell from the little arrows over them) and radius. Then each one&#8217;s position (position of the center) can be expressed in a simple vector equation as a function of time:</p>
<p align="center"><img src="http://mcherm.com/blog/wp-content/uploads/2007/11/initialdefinitions.png" alt="Initial Definitions" /></p>
<p align="left">Now here&#8217;s where it gets clever. I make a change of frame (a trick I learned from many Physics problems). I&#8217;ll consider it from the point of view of circle 1. From that point of view, circle 1 is sitting at the origin, not moving, while the position and velocity of circle 2 can be expressed as a new moving circle which (for lack of imagination) I will call circle 3. While I&#8217;m at it, I&#8217;ll give circle 3 a radius equal to the sum of that of circles 1 and 2:</p>
<p align="center"><img src="http://mcherm.com/blog/wp-content/uploads/2007/11/changeofframe.png" alt="Change of Frame" /></p>
<p align="left">Now the problem is reduced to a SINGLE moving circle and we want to know when (if ever) it will touch the origin. Express distance (of the edge of the circle) from the origin and start solving. Double vertical bars is length (of a vector) and other places use the dot product of vectors.</p>
<p align="center"><img src="http://mcherm.com/blog/wp-content/uploads/2007/11/expandingdoft.png" alt="Expanding d(t)" /></p>
<p align="left">Okay, I&#8217;ve simplified that some. Now let&#8217;s solve for when it touches. That would be when <code>d(t) = 0.</code></p>
<p align="center"><img src="http://mcherm.com/blog/wp-content/uploads/2007/11/setdtozero.png" alt="Set d to 0" /></p>
<p align="left">It&#8217;s actually not very messy, but we can make it incredibly neat by defining a few terms:</p>
<p align="center"><img src="http://mcherm.com/blog/wp-content/uploads/2007/11/defineabc.png" alt="Define A, B, and C" /></p>
<p align="left">Giving us this form:</p>
<p align="center"><img src="http://mcherm.com/blog/wp-content/uploads/2007/11/quadraticform.png" alt="Quadratic Form" /></p>
<p align="left">Oh, I think I recognize that one. In fact, I have the solution memorized:</p>
<p align="center"><img src="http://mcherm.com/blog/wp-content/uploads/2007/11/quadraticsolution.png" alt="Quadratic Solution" /></p>
<p>Q.E.D.</p>
<hr />[Okay, so this was a problem solved, not a proof so I shouldn't write "Q.E.D.". But like I said: it's <em>my </em>blog, if I can't have fun, what good is it?]</p>
]]></content:encoded>
			<wfw:commentRss>http://mcherm.com/permalinks/1/when-spheres-collide/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>The Quadratic Equation</title>
		<link>http://mcherm.com/permalinks/1/the-quadratic-equation</link>
		<comments>http://mcherm.com/permalinks/1/the-quadratic-equation#comments</comments>
		<pubDate>Thu, 15 Nov 2007 21:05:04 +0000</pubDate>
		<dc:creator>mcherm</dc:creator>
				<category><![CDATA[Math]]></category>

		<guid isPermaLink="false">http://mcherm.com/blog/?p=6</guid>
		<description><![CDATA[I was 13 at the time, and wise to the ways of the world and of math...]]></description>
			<content:encoded><![CDATA[<p>I was in middle school taking Algebra I when someone, probably another student, said I should open the textbook to a page about 3/4 of the way into the book. They pointed to a <em>terribly</em> complicated equation and said &#8220;you&#8217;ll have to learn THAT&#8221;.<span id="more-6"></span> Here is the equation:</p>
<p align="center"><img src="http://mcherm.com/blog/wp-content/uploads/2007/11/quadraticequation.png" alt="Quadratic Equation" /></p>
<p>I was 13 at the time, and wise to the ways of the world and of math, so I knew immediately that this person was trying to put something over on me. Oh sure, this messy-looking equation might have been the solution to some particular (complicated) homework problem, or it may even have been an intermediate step on the way to a more elegant solution, but there was NO WAY that something this &#8220;messy-looking&#8221; was actually a meaningful and important piece of mathematics. I&#8217;d been going to school for over 8 years now, and every piece of mathematical insight I had seen had been strikingly simple and elegant, never a complicated mess like this.</p>
<p>Well, live and learn I suppose.</p>
<p>The <a href="http://mathworld.wolfram.com/QuadraticEquation.html">quadratic equation</a> is, of course, the solution to a fundamental question, and it has no other form I know of which is much simpler. The world is just a bit messier than I had believed in those sheltered days.</p>
<p>Since then I have come to believe the following:</p>
<p><strong>(1) The &#8220;messy&#8221; is far more common than the &#8220;simple&#8221; or &#8220;elegant&#8221;.</strong> Just as nearly-all functions are not expressible in closed form and nearly-all real numbers are irrational, nearly all problems have very difficult or messy solutions. I&#8217;d love to be able to state this precisely, perhaps even prove it, but I&#8217;m not quite sure how to go about it.</p>
<p><strong>(2) An astonishingly large portion of the really important and interesting problems have simple and elegant answers.</strong> The <a href="http://www.claymath.org/millennium/Navier-Stokes_Equations/">Navier-Stokes equation</a> is the exception &#8211; most things we care about CAN be solved simply. Why this should be is a <em>truly</em> deep question.</p>
]]></content:encoded>
			<wfw:commentRss>http://mcherm.com/permalinks/1/the-quadratic-equation/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

