<?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>nklein software &#187; Math</title>
	<atom:link href="http://nklein.com/math/feed/" rel="self" type="application/rss+xml" />
	<link>http://nklein.com</link>
	<description>software development and consulting</description>
	<lastBuildDate>Tue, 16 Feb 2010 21:29:16 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.1</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Finding the Perfect Hyperbola</title>
		<link>http://nklein.com/2010/02/finding-the-perfect-hyperbola/</link>
		<comments>http://nklein.com/2010/02/finding-the-perfect-hyperbola/#comments</comments>
		<pubDate>Mon, 15 Feb 2010 20:53:18 +0000</pubDate>
		<dc:creator>pat</dc:creator>
				<category><![CDATA[Articles]]></category>
		<category><![CDATA[Math]]></category>
		<category><![CDATA[algebra]]></category>
		<category><![CDATA[geometry]]></category>
		<category><![CDATA[vecto]]></category>

		<guid isPermaLink="false">http://nklein.com/?p=1189</guid>
		<description><![CDATA[For an application that I&#8217;m working on, I needed a way to scale quantities that range (theoretically) over the real numbers (though practically are probably between plus and minus three) into positive numbers.  I wanted the function to be everywhere increasing, I wanted , and I wanted control of the derivative at .
The easy [...]]]></description>
			<content:encoded><![CDATA[<p>For an application that I&#8217;m working on, I needed a way to scale quantities that range (theoretically) over the real numbers (though practically are probably between plus and minus three) into positive numbers.  I wanted the function to be everywhere increasing, I wanted <a href="http://wordpress.org/extend/plugins/easy-latex/" target="_blank" title="f(0) = 1"><img src="http://l.wordpress.com/latex.php?latex=f%280%29%20%3D%201&#038;bg=FFFFCC&#038;fg=000000&#038;s=0" style="vertical-align:-20%;" class="tex" alt="f(0) = 1" /></a>, and I wanted control of the derivative at <a href="http://www.Thulasidas.com/latex" target="_blank" title="x = 0"><img src="http://l.wordpress.com/latex.php?latex=x%20%3D%200&#038;bg=FFFFCC&#038;fg=000000&#038;s=0" style="vertical-align:-20%;" class="tex" alt="x = 0" /></a>.</p>
<p>The easy choice is: <a ><img src="http://l.wordpress.com/latex.php?latex=f%28x%29%20%3D%20e%5E%7B%5Calpha%20x%7D&#038;bg=FFFFCC&#038;fg=000000&#038;s=0" style="vertical-align:-20%;" class="tex" alt="f(x) = e^{\alpha x}" /></a>.  This is monotonically increasing.  <img src="http://l.wordpress.com/latex.php?latex=f%280%29%20%3D%201&#038;bg=FFFFCC&#038;fg=000000&#038;s=0" title="f(0) = 1" style="vertical-align:-20%;" class="tex" alt="f(0) = 1" /> and <img src="http://l.wordpress.com/latex.php?latex=f%5E%5Cprime%280%29%20%3D%20%5Calpha&#038;bg=FFFFCC&#038;fg=000000&#038;s=0" title="f^\prime(0) = \alpha" style="vertical-align:-20%;" class="tex" alt="f^\prime(0) = \alpha" />.</p>
<p>I needed to scale three such quantities and mush them together.  I thought it&#8217;d be spiffy then to have three different functions that satisfy my criteria.  The next logical choice was <img src="http://l.wordpress.com/latex.php?latex=f%28x%29%20%3D%201%20%2B%20%5Cmathrm%7Btanh%7D%20%28%5Calpha%20x%29&#038;bg=FFFFCC&#038;fg=000000&#038;s=0" title="f(x) = 1 + \mathrm{tanh} (\alpha x)" style="vertical-align:-20%;" class="tex" alt="f(x) = 1 + \mathrm{tanh} (\alpha x)" />.  It is everywhere positive and increasing.  And, it has <img src="http://l.wordpress.com/latex.php?latex=f%5E%5Cprime%280%29%20%3D%20%5Calpha&#038;bg=FFFFCC&#038;fg=000000&#038;s=0" title="f^\prime(0) = \alpha" style="vertical-align:-20%;" class="tex" alt="f^\prime(0) = \alpha" />.</p>
<p>Now, I needed third function that was always positive, always increasing, had <img src="http://l.wordpress.com/latex.php?latex=f%280%29%20%3D%201&#038;bg=FFFFCC&#038;fg=000000&#038;s=0" title="f(0) = 1" style="vertical-align:-20%;" class="tex" alt="f(0) = 1" /> and <img src="http://l.wordpress.com/latex.php?latex=f%5E%5Cprime%280%29%20%3D%20%5Calpha&#038;bg=FFFFCC&#038;fg=000000&#038;s=0" title="f^\prime(0) = \alpha" style="vertical-align:-20%;" class="tex" alt="f^\prime(0) = \alpha" />.  One choice was:  <img src="http://l.wordpress.com/latex.php?latex=f%28x%29%20%3D%20e%5E%7Be%5E%7B%5Calpha%20x%7D%20-%201%7D&#038;bg=FFFFCC&#038;fg=000000&#038;s=0" title="f(x) = e^{e^{\alpha x} - 1}" style="vertical-align:-20%;" class="tex" alt="f(x) = e^{e^{\alpha x} - 1}" />.  But, that seemed like overkill.  It also meant that I really had to keep my <img src="http://l.wordpress.com/latex.php?latex=%5Calpha&#038;bg=FFFFCC&#038;fg=000000&#038;s=0" title="\alpha" style="vertical-align:-20%;" class="tex" alt="\alpha" /> tiny if I didn&#8217;t want to scale things into the stratosphere.</p>
<h3>Playing with hyperbolas</h3>
<p><a href="http://nklein.com/wp-content/uploads/2010/02/hyperbola.png"><img src="http://nklein.com/wp-content/uploads/2010/02/hyperbola-300x300.png" alt="" title="hyperbola" width="300" height="300" class="alignright size-medium wp-image-1191" /></a>So, I thought&#8230; why don&#8217;t I make a hyperbola, rotate it, and shift it so that the apex of one side of the hyperbola is at <img src="http://l.wordpress.com/latex.php?latex=%280%2C1%29&#038;bg=FFFFCC&#038;fg=000000&#038;s=0" title="(0,1)" style="vertical-align:-20%;" class="tex" alt="(0,1)" />.  And, I can adjust the parameters of the hyperbola so that <img src="http://l.wordpress.com/latex.php?latex=f%27%280%29%20%3D%20%5Calpha&#038;bg=FFFFCC&#038;fg=000000&#038;s=0" title="f'(0) = \alpha" style="vertical-align:-20%;" class="tex" alt="f'(0) = \alpha" />.  After a variety of false starts where I tried to keep the hyperbola general until the very end (<img src="http://l.wordpress.com/latex.php?latex=%5Cfrac%7Bx%5E2%7D%7Ba%5E2%7D%20-%20%5Cfrac%7By%5E2%7D%7Bb%5E2%7D%20%3D%20r%5E2&#038;bg=FFFFCC&#038;fg=000000&#038;s=0" title="\frac{x^2}{a^2} - \frac{y^2}{b^2} = r^2" style="vertical-align:-20%;" class="tex" alt="\frac{x^2}{a^2} - \frac{y^2}{b^2} = r^2" />, rotated by <img src="http://l.wordpress.com/latex.php?latex=%5Ctheta&#038;bg=FFFFCC&#038;fg=000000&#038;s=0" title="\theta" style="vertical-align:-20%;" class="tex" alt="\theta" /> degrees, and shifted by <img src="http://l.wordpress.com/latex.php?latex=%5Cbeta&#038;bg=FFFFCC&#038;fg=000000&#038;s=0" title="\beta" style="vertical-align:-20%;" class="tex" alt="\beta" />), I quickly got bogged down in six or seven incredibly ugly equations in eight or nine variables.</p>
<p>So, it was time to start trying to make it easy from the beginning.   I noticed that if was going to rotate it by an angle <img src="http://l.wordpress.com/latex.php?latex=%5Ctheta&#038;bg=FFFFCC&#038;fg=000000&#038;s=0" title="\theta" style="vertical-align:-20%;" class="tex" alt="\theta" /> in the clockwise direction, then I needed <img src="http://l.wordpress.com/latex.php?latex=%5Cphi%20%3D%20%5Cfrac%7B%5Cpi%7D%7B2%7D%20-%20%5Ctheta&#038;bg=FFFFCC&#038;fg=000000&#038;s=0" title="\phi = \frac{\pi}{2} - \theta" style="vertical-align:-20%;" class="tex" alt="\phi = \frac{\pi}{2} - \theta" /> to be such that <img src="http://l.wordpress.com/latex.php?latex=%5Ctan%20%5Cphi%20%3D%20%5Calpha&#038;bg=FFFFCC&#038;fg=000000&#038;s=0" title="\tan \phi = \alpha" style="vertical-align:-20%;" class="tex" alt="\tan \phi = \alpha" /> if my slope was going to work out right in the end.<a href="http://nklein.com/wp-content/uploads/2010/02/triangle.png"><img src="http://nklein.com/wp-content/uploads/2010/02/triangle-150x150.png" alt="" title="triangle" width="150" height="150"  style="clear: right;" class="alignright size-thumbnail wp-image-1192" /></a>   So, I&#8217;m looking at the basic triangle on the right then to determine all of my sines and cosines and such.</p>
<p>Based on that triangle, it was also obvious that the asymptote for my starting hyperbola had to have <img src="http://l.wordpress.com/latex.php?latex=%5Cfrac%7Bb%7D%7Ba%7D%20%3D%20%5Cfrac%7B1%7D%7B%5Calpha%7D&#038;bg=FFFFCC&#038;fg=000000&#038;s=0" title="\frac{b}{a} = \frac{1}{\alpha}" style="vertical-align:-20%;" class="tex" alt="\frac{b}{a} = \frac{1}{\alpha}" />.  I played around a bit then with making <img src="http://l.wordpress.com/latex.php?latex=a%20%3D%20%5Calpha&#038;bg=FFFFCC&#038;fg=000000&#038;s=0" title="a = \alpha" style="vertical-align:-20%;" class="tex" alt="a = \alpha" /> and <img src="http://l.wordpress.com/latex.php?latex=b%20%3D%201&#038;bg=FFFFCC&#038;fg=000000&#038;s=0" title="b = 1" style="vertical-align:-20%;" class="tex" alt="b = 1" />.  In the end, I found things simplified sooner if I started with <img src="http://l.wordpress.com/latex.php?latex=a%20%3D%201&#038;bg=FFFFCC&#038;fg=000000&#038;s=0" title="a = 1" style="vertical-align:-20%;" class="tex" alt="a = 1" /> and <img src="http://l.wordpress.com/latex.php?latex=b%20%3D%20%5Cfrac%7B1%7D%7B%5Calpha%7D&#038;bg=FFFFCC&#038;fg=000000&#038;s=0" title="b = \frac{1}{\alpha}" style="vertical-align:-20%;" class="tex" alt="b = \frac{1}{\alpha}" />.</p>
<p>I also needed <img src="http://l.wordpress.com/latex.php?latex=r&#038;bg=FFFFCC&#038;fg=000000&#038;s=0" title="r" style="vertical-align:-20%;" class="tex" alt="r" /> to be such that the point <img src="http://l.wordpress.com/latex.php?latex=%28-r%2C0%29&#038;bg=FFFFCC&#038;fg=000000&#038;s=0" title="(-r,0)" style="vertical-align:-20%;" class="tex" alt="(-r,0)" /> rotate up so that its <img src="http://l.wordpress.com/latex.php?latex=y&#038;bg=FFFFCC&#038;fg=000000&#038;s=0" title="y" style="vertical-align:-20%;" class="tex" alt="y" />-coordinate was <img src="http://l.wordpress.com/latex.php?latex=1&#038;bg=FFFFCC&#038;fg=000000&#038;s=0" title="1" style="vertical-align:-20%;" class="tex" alt="1" />.  This meant that <img src="http://l.wordpress.com/latex.php?latex=r%20%5Csin%20%5Ctheta%20%3D%201&#038;bg=FFFFCC&#038;fg=000000&#038;s=0" title="r \sin \theta = 1" style="vertical-align:-20%;" class="tex" alt="r \sin \theta = 1" /> or <img src="http://l.wordpress.com/latex.php?latex=r%20%3D%20%5Cfrac%7B1%7D%7B%5Csin%20%5Ctheta%7D%20%3D%20%5Csqrt%7B1%20%2B%20%5Calpha%5E2%7D&#038;bg=FFFFCC&#038;fg=000000&#038;s=0" title="r = \frac{1}{\sin \theta} = \sqrt{1 + \alpha^2}" style="vertical-align:-20%;" class="tex" alt="r = \frac{1}{\sin \theta} = \sqrt{1 + \alpha^2}" />.</p>
<p>So, my starting hyperbola then was: <img src="http://l.wordpress.com/latex.php?latex=x%5E2%20-%20%5Calpha%5E2%20y%5E2%20%3D%201%20%2B%20%5Calpha%5E2&#038;bg=FFFFCC&#038;fg=000000&#038;s=0" title="x^2 - \alpha^2 y^2 = 1 + \alpha^2" style="vertical-align:-20%;" class="tex" alt="x^2 - \alpha^2 y^2 = 1 + \alpha^2" />.</p>
<p>From there, I had to rotate the <img src="http://l.wordpress.com/latex.php?latex=x&#038;bg=FFFFCC&#038;fg=000000&#038;s=0" title="x" style="vertical-align:-20%;" class="tex" alt="x" /> and <img src="http://l.wordpress.com/latex.php?latex=y&#038;bg=FFFFCC&#038;fg=000000&#038;s=0" title="y" style="vertical-align:-20%;" class="tex" alt="y" /> by <img src="http://l.wordpress.com/latex.php?latex=%5Ctheta&#038;bg=FFFFCC&#038;fg=000000&#038;s=0" title="\theta" style="vertical-align:-20%;" class="tex" alt="\theta" /> in the clockwise direction.  This gave me:<br />
<center><img src="http://l.wordpress.com/latex.php?latex=%28x%5Ccos%5Ctheta%20-%20y%5Csin%5Ctheta%29%5E2%20-%20%5Calpha%5E2%20%28x%5Csin%5Ctheta%20%2B%20y%5Ccos%5Ctheta%29%5E2%20%3D%201%20%2B%20%5Calpha%5E2&#038;bg=FFFFCC&#038;fg=000000&#038;s=0" title="(x\cos\theta - y\sin\theta)^2 - \alpha^2 (x\sin\theta + y\cos\theta)^2 = 1 + \alpha^2" style="vertical-align:-20%;" class="tex" alt="(x\cos\theta - y\sin\theta)^2 - \alpha^2 (x\sin\theta + y\cos\theta)^2 = 1 + \alpha^2" /></center></p>
<p>A little multiplying out leads to:<br />
<center><img src="http://l.wordpress.com/latex.php?latex=%28x%5E2%5Ccos%5E2%5Ctheta%20-%202xy%5Csin%5Ctheta%5Ccos%5Ctheta%20%2B%20y%5E2%5Csin%5E2%5Ctheta%29%20%5C%5C%20-%20%5Calpha%5E2%28x%5E2%5Csin%5E2%5Ctheta%20%2B%202xy%5Csin%5Ctheta%5Ccos%5Ctheta%20%2B%20y%5E2%5Ccos%5E2%5Ctheta%29%20%3D%201%20%2B%20%5Calpha%5E2&#038;bg=FFFFCC&#038;fg=000000&#038;s=0" title="(x^2\cos^2\theta - 2xy\sin\theta\cos\theta + y^2\sin^2\theta) \\ - \alpha^2(x^2\sin^2\theta + 2xy\sin\theta\cos\theta + y^2\cos^2\theta) = 1 + \alpha^2" style="vertical-align:-20%;" class="tex" alt="(x^2\cos^2\theta - 2xy\sin\theta\cos\theta + y^2\sin^2\theta) \\ - \alpha^2(x^2\sin^2\theta + 2xy\sin\theta\cos\theta + y^2\cos^2\theta) = 1 + \alpha^2" /></center></p>
<p>From there, using <img src="http://l.wordpress.com/latex.php?latex=cos%5E2%5Ctheta%20%3D%20%5Cfrac%7B%5Calpha%5E2%7D%7B1%20%2B%20%5Calpha%5E2%7D&#038;bg=FFFFCC&#038;fg=000000&#038;s=0" title="cos^2\theta = \frac{\alpha^2}{1 + \alpha^2}" style="vertical-align:-20%;" class="tex" alt="cos^2\theta = \frac{\alpha^2}{1 + \alpha^2}" />, <img src="http://l.wordpress.com/latex.php?latex=sin%5E2%5Ctheta%20%3D%20%5Cfrac%7B1%7D%7B1%20%2B%20%5Calpha%5E2%7D&#038;bg=FFFFCC&#038;fg=000000&#038;s=0" title="sin^2\theta = \frac{1}{1 + \alpha^2}" style="vertical-align:-20%;" class="tex" alt="sin^2\theta = \frac{1}{1 + \alpha^2}" />, and <img src="http://l.wordpress.com/latex.php?latex=%5Csin%5Ctheta%5Ccos%5Ctheta%20%3D%20%5Cfrac%7B%5Calpha%7D%7B1%20%2B%20%5Calpha%5E2%7D&#038;bg=FFFFCC&#038;fg=000000&#038;s=0" title="\sin\theta\cos\theta = \frac{\alpha}{1 + \alpha^2}" style="vertical-align:-20%;" class="tex" alt="\sin\theta\cos\theta = \frac{\alpha}{1 + \alpha^2}" />, we come to:</p>
<p><center><img src="http://l.wordpress.com/latex.php?latex=%5Cfrac%7B-2%5Calpha%281%20%2B%20%5Calpha%5E2%29xy%20%2B%20%28%201%20-%20%5Calpha%5E4%20%29y%5E2%7D%7B1%20%2B%20%5Calpha%5E2%7D%20%3D%20-2%5Calpha%7B%7Dxy%20%2B%20%20%281%20-%20%5Calpha%5E2%29y%5E2%20%3D%201%20%2B%20%5Calpha%5E2&#038;bg=FFFFCC&#038;fg=000000&#038;s=0" title="\frac{-2\alpha(1 + \alpha^2)xy + ( 1 - \alpha^4 )y^2}{1 + \alpha^2} = -2\alpha{}xy +  (1 - \alpha^2)y^2 = 1 + \alpha^2" style="vertical-align:-20%;" class="tex" alt="\frac{-2\alpha(1 + \alpha^2)xy + ( 1 - \alpha^4 )y^2}{1 + \alpha^2} = -2\alpha{}xy +  (1 - \alpha^2)y^2 = 1 + \alpha^2" /></center></p>
<p>The only step remaining was to shift it all over so that when <img src="http://l.wordpress.com/latex.php?latex=y%20%3D%201&#038;bg=FFFFCC&#038;fg=000000&#038;s=0" title="y = 1" style="vertical-align:-20%;" class="tex" alt="y = 1" />, we end up with <img src="http://l.wordpress.com/latex.php?latex=x%20%3D%200&#038;bg=FFFFCC&#038;fg=000000&#038;s=0" title="x = 0" style="vertical-align:-20%;" class="tex" alt="x = 0" />.  Plugging in <img src="http://l.wordpress.com/latex.php?latex=y%20%3D%201&#038;bg=FFFFCC&#038;fg=000000&#038;s=0" title="y = 1" style="vertical-align:-20%;" class="tex" alt="y = 1" />, we see that <img src="http://l.wordpress.com/latex.php?latex=-2x%5Calpha%20%2B%201%20-%20%5Calpha%5E2%20%3D%201%20%2B%20%5Calpha&#038;bg=FFFFCC&#038;fg=000000&#038;s=0" title="-2x\alpha + 1 - \alpha^2 = 1 + \alpha" style="vertical-align:-20%;" class="tex" alt="-2x\alpha + 1 - \alpha^2 = 1 + \alpha" />.  That equation balances when <img src="http://l.wordpress.com/latex.php?latex=x%20%3D%20-%5Calpha&#038;bg=FFFFCC&#038;fg=000000&#038;s=0" title="x = -\alpha" style="vertical-align:-20%;" class="tex" alt="x = -\alpha" />.  We want it to balance when <img src="http://l.wordpress.com/latex.php?latex=x%20%3D%200&#038;bg=FFFFCC&#038;fg=000000&#038;s=0" title="x = 0" style="vertical-align:-20%;" class="tex" alt="x = 0" />, so we&#8217;re going to substitute <img src="http://l.wordpress.com/latex.php?latex=%28x%20-%20%5Calpha%29&#038;bg=FFFFCC&#038;fg=000000&#038;s=0" title="(x - \alpha)" style="vertical-align:-20%;" class="tex" alt="(x - \alpha)" /> in place of the <img src="http://l.wordpress.com/latex.php?latex=x&#038;bg=FFFFCC&#038;fg=000000&#038;s=0" title="x" style="vertical-align:-20%;" class="tex" alt="x" /> in the above equation to get:<br />
<center><img src="http://l.wordpress.com/latex.php?latex=-2%5Calpha%28x%20-%20%5Calpha%29y%20%2B%20%281%20-%20%5Calpha%5E2%29y%5E2%20%3D%201%20%2B%20%5Calpha%5E2&#038;bg=FFFFCC&#038;fg=000000&#038;s=0" title="-2\alpha(x - \alpha)y + (1 - \alpha^2)y^2 = 1 + \alpha^2" style="vertical-align:-20%;" class="tex" alt="-2\alpha(x - \alpha)y + (1 - \alpha^2)y^2 = 1 + \alpha^2" /></center></p>
<p>We can easily verify that the point <img src="http://l.wordpress.com/latex.php?latex=%280%2C1%29&#038;bg=FFFFCC&#038;fg=000000&#038;s=0" title="(0,1)" style="vertical-align:-20%;" class="tex" alt="(0,1)" /> is on the curve.  And, we can implicitly differentiate the above to get:<br />
<center><img src="http://l.wordpress.com/latex.php?latex=-2%5Calpha%28x%20-%20%5Calpha%29%5Cfrac%7Bdy%7D%7Bdx%7D%20-%202%5Calpha%7B%7Dy%20%2B%202%281%20-%20%5Calpha%5E2%29y%5Cfrac%7Bdy%7D%7Bdx%7D%20%3D%200&#038;bg=FFFFCC&#038;fg=000000&#038;s=0" title="-2\alpha(x - \alpha)\frac{dy}{dx} - 2\alpha{}y + 2(1 - \alpha^2)y\frac{dy}{dx} = 0" style="vertical-align:-20%;" class="tex" alt="-2\alpha(x - \alpha)\frac{dy}{dx} - 2\alpha{}y + 2(1 - \alpha^2)y\frac{dy}{dx} = 0" /></center><br />
Plopping <img src="http://l.wordpress.com/latex.php?latex=x%20%3D%200&#038;bg=FFFFCC&#038;fg=000000&#038;s=0" title="x = 0" style="vertical-align:-20%;" class="tex" alt="x = 0" /> and <img src="http://l.wordpress.com/latex.php?latex=y%20%3D%201&#038;bg=FFFFCC&#038;fg=000000&#038;s=0" title="y = 1" style="vertical-align:-20%;" class="tex" alt="y = 1" /> in there, we find that <img src="http://l.wordpress.com/latex.php?latex=%5Cfrac%7Bdy%7D%7Bdx%7D%20%3D%20%5Calpha&#038;bg=FFFFCC&#038;fg=000000&#038;s=0" title="\frac{dy}{dx} = \alpha" style="vertical-align:-20%;" class="tex" alt="\frac{dy}{dx} = \alpha" />.</p>
<p>This is pretty good as it goes.  The only step is to complete the square to find a nicer expression for <img src="http://l.wordpress.com/latex.php?latex=y&#038;bg=FFFFCC&#038;fg=000000&#038;s=0" title="y" style="vertical-align:-20%;" class="tex" alt="y" /> in terms of <img src="http://l.wordpress.com/latex.php?latex=x&#038;bg=FFFFCC&#038;fg=000000&#038;s=0" title="x" style="vertical-align:-20%;" class="tex" alt="x" />.  We start by adding <img src="http://l.wordpress.com/latex.php?latex=%5Cleft%5B%20%5Cfrac%7B%5Calpha%7D%7B%5Csqrt%7B1%20-%20%5Calpha%5E2%7D%7D%20%28x%20-%20%5Calpha%29%20%5Cright%5D%5E2&#038;bg=FFFFCC&#038;fg=000000&#038;s=0" title="\left[ \frac{\alpha}{\sqrt{1 - \alpha^2}} (x - \alpha) \right]^2" style="vertical-align:-20%;" class="tex" alt="\left[ \frac{\alpha}{\sqrt{1 - \alpha^2}} (x - \alpha) \right]^2" /> to both sides to get:<br />
<center><img src="http://l.wordpress.com/latex.php?latex=%5Cleft%5B%20%5Csqrt%281%20-%20%5Calpha%5E2%29y%20-%20%5Cfrac%7B%5Calpha%7D%7B%5Csqrt%7B1%20-%20%5Calpha%5E2%7D%28x%20-%20%5Calpha%29%7D%20%5Cright%5D%5E2%20%3D%201%20%2B%20%5Calpha%5E2%20%2B%20%5Cfrac%7B%5Calpha%7D%7B1%20-%20%5Calpha%5E2%7D%20%28x-%5Calpha%29%5E2&#038;bg=FFFFCC&#038;fg=000000&#038;s=0" title="\left[ \sqrt(1 - \alpha^2)y - \frac{\alpha}{\sqrt{1 - \alpha^2}(x - \alpha)} \right]^2 = 1 + \alpha^2 + \frac{\alpha}{1 - \alpha^2} (x-\alpha)^2" style="vertical-align:-20%;" class="tex" alt="\left[ \sqrt(1 - \alpha^2)y - \frac{\alpha}{\sqrt{1 - \alpha^2}(x - \alpha)} \right]^2 = 1 + \alpha^2 + \frac{\alpha}{1 - \alpha^2} (x-\alpha)^2" /></center></p>
<p>This is easy enough to solve for <img src="http://l.wordpress.com/latex.php?latex=y&#038;bg=FFFFCC&#038;fg=000000&#038;s=0" title="y" style="vertical-align:-20%;" class="tex" alt="y" /> by taking the square root of both sides and shuffling some things about:<br />
<center><img src="http://l.wordpress.com/latex.php?latex=y%20%3D%20%5Cfrac%7B%5Calpha%7D%7B1%20-%20%5Calpha%5E2%7D%28x%20-%20%5Calpha%29%20%2B%20%5Cfrac%7B1%7D%7B%5Csqrt%7B1%20-%20%5Calpha%5E2%7D%7D%20%5Csqrt%7B1%20%2B%20%5Calpha%5E2%20%2B%20%5Cfrac%7B%5Calpha%5E2%7D%7B1%20-%20%5Calpha%5E2%7D%28x%20-%20%5Calpha%29%5E2%7D&#038;bg=FFFFCC&#038;fg=000000&#038;s=0" title="y = \frac{\alpha}{1 - \alpha^2}(x - \alpha) + \frac{1}{\sqrt{1 - \alpha^2}} \sqrt{1 + \alpha^2 + \frac{\alpha^2}{1 - \alpha^2}(x - \alpha)^2}" style="vertical-align:-20%;" class="tex" alt="y = \frac{\alpha}{1 - \alpha^2}(x - \alpha) + \frac{1}{\sqrt{1 - \alpha^2}} \sqrt{1 + \alpha^2 + \frac{\alpha^2}{1 - \alpha^2}(x - \alpha)^2}" /></center></p>
<p>Here are all three curves with <img src="http://l.wordpress.com/latex.php?latex=%5Calpha%20%3D%20%5Cfrac%7B1%7D%7B4%7D&#038;bg=FFFFCC&#038;fg=000000&#038;s=0" title="\alpha = \frac{1}{4}" style="vertical-align:-20%;" class="tex" alt="\alpha = \frac{1}{4}" />.  The exponential is in black, the hyperbolic tangent is in red, and the hyperbola is in blue:<br />
<center><a href="http://nklein.com/wp-content/uploads/2010/02/final.png"><img src="http://nklein.com/wp-content/uploads/2010/02/final.png" alt="" title="final" width="522" height="322" class="alignnone size-full wp-image-1218" /></a></center></p>
<p>The first image on the page here was made with Zach&#8217;s <a href="http://www.xach.com/lisp/vecto/">Vecto</a> library with some post-processing in the GIMP.  (Here is the source file: <a href="http://nklein.com/wp-content/uploads/2010/02/hyperbola.lisp">hyperbola.lisp</a>.) The second image was made entirely within the GIMP.  And, the last image was made using <a href="http://fooplot.com/">Foo Plot</a> and the GIMP.</p>
]]></content:encoded>
			<wfw:commentRss>http://nklein.com/2010/02/finding-the-perfect-hyperbola/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Image Approximation with Genetically Selected Cosines</title>
		<link>http://nklein.com/2009/10/image-approximation-with-genetically-selected-cosines/</link>
		<comments>http://nklein.com/2009/10/image-approximation-with-genetically-selected-cosines/#comments</comments>
		<pubDate>Fri, 02 Oct 2009 17:52:17 +0000</pubDate>
		<dc:creator>pat</dc:creator>
				<category><![CDATA[Articles]]></category>
		<category><![CDATA[Math]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[lisp]]></category>

		<guid isPermaLink="false">http://nklein.com/?p=907</guid>
		<description><![CDATA[I started with the target image here on the right.  I then used a genetic algorithm to try to find an approximation of this image as a sum of cosine waves.  
Each cosine wave has the following formThe genetic algorithm can tweak the amplitude , the orientation in the plane , the frequency [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://nklein.com/wp-content/uploads/2009/10/in.png"><img src="http://nklein.com/wp-content/uploads/2009/10/in.png" alt="input image" title="input image" width="64" height="64" class="alignright size-full wp-image-914" /></a>I started with the target image here on the right.  I then used a genetic algorithm to try to find an approximation of this image as a sum of cosine waves.  </p>
<p>Each cosine wave has the following form<center><a href="http://wordpress.org/extend/plugins/easy-latex/" target="_blank" title="f_i(x,y) = A_i \cos \left( 2 \pi \left( x \cos \theta_i + y \cos \theta_i \right) \omega_i + 2 \pi \phi_i \right)"><img src="http://l.wordpress.com/latex.php?latex=f_i%28x%2Cy%29%20%3D%20A_i%20%5Ccos%20%5Cleft%28%202%20%5Cpi%20%5Cleft%28%20x%20%5Ccos%20%5Ctheta_i%20%2B%20y%20%5Ccos%20%5Ctheta_i%20%5Cright%29%20%5Comega_i%20%2B%202%20%5Cpi%20%5Cphi_i%20%5Cright%29&#038;bg=FFFFCC&#038;fg=000000&#038;s=0" style="vertical-align:-20%;" class="tex" alt="f_i(x,y) = A_i \cos \left( 2 \pi \left( x \cos \theta_i + y \cos \theta_i \right) \omega_i + 2 \pi \phi_i \right)" /></a></center>The genetic algorithm can tweak the amplitude <a href="http://www.Thulasidas.com/latex" target="_blank" title="A_i"><img src="http://l.wordpress.com/latex.php?latex=A_i&#038;bg=FFFFCC&#038;fg=000000&#038;s=0" style="vertical-align:-20%;" class="tex" alt="A_i" /></a>, the orientation in the plane <a ><img src="http://l.wordpress.com/latex.php?latex=%5Ctheta_i&#038;bg=FFFFCC&#038;fg=000000&#038;s=0" style="vertical-align:-20%;" class="tex" alt="\theta_i" /></a>, the frequency <img src="http://l.wordpress.com/latex.php?latex=%5Comega_i&#038;bg=FFFFCC&#038;fg=000000&#038;s=0" title="\omega_i" style="vertical-align:-20%;" class="tex" alt="\omega_i" />, and the phase <img src="http://l.wordpress.com/latex.php?latex=%5Cphi_i&#038;bg=FFFFCC&#038;fg=000000&#038;s=0" title="\phi_i" style="vertical-align:-20%;" class="tex" alt="\phi_i" /> of each wave.</p>
<p><a href="http://nklein.com/wp-content/uploads/2009/10/out-0442.png"><img src="http://nklein.com/wp-content/uploads/2009/10/out-0442.png" alt="out-0442" title="out-0442" width="64" height="64" class="alignright size-full wp-image-915" /></a>  The image at the right here, is from the 443rd generation of a population of 200 where each gene represents 100 cosine waves.  For the video below, I used a population of 100 genes, each representing the sum of 50 cosine waves.</p>
<p class="clear"><object width="640" height="448"><param name="allowfullscreen" value="true" /><param name="allowscriptaccess" value="always" /><param name="movie" value="http://vimeo.com/moogaloop.swf?clip_id=6867189&amp;server=vimeo.com&amp;show_title=1&amp;show_byline=0&amp;show_portrait=0&amp;color=00ADEF&amp;fullscreen=1" /><embed src="http://vimeo.com/moogaloop.swf?clip_id=6867189&amp;server=vimeo.com&amp;show_title=1&amp;show_byline=0&amp;show_portrait=0&amp;color=00ADEF&amp;fullscreen=1" type="application/x-shockwave-flash" allowfullscreen="true" allowscriptaccess="always" width="640" height="448"></embed></object></p>
<p><a href="http://vimeo.com/6867189">Image Approximation with Genetically Selected Cosines</a> from <a href="http://vimeo.com/nklein">Patrick Stein</a> on <a href="http://vimeo.com">Vimeo</a>.</p>
<h3>Nitty Gritty Details</h3>
<p>The genes are represented internally as an array of fixed-point numbers.  Each number is five-bits integer, ten-bits fractional, and a sign bit.  I scale the amplitudes down by a factor of 50 and scale the frequency and offset up by 50% when I convert them back to floating point from the fixed point representation just to get them into a more useful ballpark.  During each generation, the fittest two quartiles of the population are cross-bred (in randomly selected pairs with two-point crossover) to replace the third quartile of the population, and the fourth quartile is replaced by mutations of randomly selected members of the top quartile.  I also run through and remove any duplicate genes and replace them with randomly generated ones so that I don&#8217;t breed myself into a total corner.</p>
<p>I toyed around with two different fitness functions:  <a href="http://en.wikipedia.org/wiki/Root_mean_square">RMS</a> and <a href="http://en.wikipedia.org/wiki/Entropy_(information_theory)">entropy</a>.  So, I would calculate the image represented by the gene and subtract that from the original image.  Then, I would calculate the RMS (<img src="http://l.wordpress.com/latex.php?latex=%5Cfrac%7B%5Csum_%7Bi%3D1%7D%5E%7Bn%7D%20v_i%5E2%7D%7Bn%7D&#038;bg=FFFFCC&#038;fg=000000&#038;s=0" title="\frac{\sum_{i=1}^{n} v_i^2}{n}" style="vertical-align:-20%;" class="tex" alt="\frac{\sum_{i=1}^{n} v_i^2}{n}" />) or the sum of the entropies of the possible values (<img src="http://l.wordpress.com/latex.php?latex=%5Csum_%7Bv%3D0%7D%5E%7B255%7D%20-%20p_v%20%5Clog%20p_v&#038;bg=FFFFCC&#038;fg=000000&#038;s=0" title="\sum_{v=0}^{255} - p_v \log p_v" style="vertical-align:-20%;" class="tex" alt="\sum_{v=0}^{255} - p_v \log p_v" />, where <img src="http://l.wordpress.com/latex.php?latex=p_v&#038;bg=FFFFCC&#038;fg=000000&#038;s=0" title="p_v" style="vertical-align:-20%;" class="tex" alt="p_v" /> is the proportion of the pixels in the resulting image that had value <img src="http://l.wordpress.com/latex.php?latex=v&#038;bg=FFFFCC&#038;fg=000000&#038;s=0" title="v" style="vertical-align:-20%;" class="tex" alt="v" />).</p>
<p>I bothered with entropy because I was hoping that if I could encode a gene in just a hundred or two hundred bytes that I might be able to compress the remainder (losslessly) and save bytes over just compressing the original image.  The big problem with the entropy calculation though is that I get the same answer when every pixel value is off by 100 as I get when every pixel value is right on target.  So, for a time, I was multiplying the RMS and the entropy for the fitness function.  For the video and images above, I just used the RMS since it reached very close to the same solution as the multiplying method, and I saved myself fifty histograms per generation.</p>
<h3>Source Code</h3>
<p>Here is the <a href="http://nklein.com/wp-content/uploads/2009/10/approx-ft.lisp">Common Lisp source code for the genetic algorithm</a>.  It depends on the <a href="http://www.ljosa.com/~ljosa/software/cl-png/">CL-PNG library</a> for image input and output.  As it appears here, it will create an output image from the fittest member of each generation, an image of the fittest member of the current generation, an image of the original with the fittest member of the current generation subtracted, a Lisp file containing the fittest gene of the current generation, and the current state of the random number generator when the run ends.</p>
<p>For the video above, I quickly put together some code to demonstrate the different parameters that the genetic algorithm can tweak and to show incrementally adding in more of the cosine waves.  Here is some <a href="http://nklein.com/wp-content/uploads/2009/10/about.lisp">general utility code that is display-independent</a>.  And, here is the <a href="http://nklein.com/wp-content/uploads/2009/10/gl.lisp">OpenGL display code</a> which depends on the <a href="http://common-lisp.net/project/cl-opengl/">CL-OpenGL libraries</a>.  I included the source code here because I had trouble finding an example on the net of using Lisp-created textures with CL-OpenGL.  I render the cosine waves into a buffer, copy that buffer into an OpenGL texture, and draw a quad with that texture.  Note: it looks a little sluggish when run, but that is because I put a big <em>(SLEEP&nbsp;&#8230;)</em> in there to keep things from flying by too quickly.</p>
]]></content:encoded>
			<wfw:commentRss>http://nklein.com/2009/10/image-approximation-with-genetically-selected-cosines/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Fourier Transform of Swarm Data with HUD</title>
		<link>http://nklein.com/2009/09/fourier-transform-of-swarm-data-with-hud/</link>
		<comments>http://nklein.com/2009/09/fourier-transform-of-swarm-data-with-hud/#comments</comments>
		<pubDate>Fri, 25 Sep 2009 21:01:56 +0000</pubDate>
		<dc:creator>pat</dc:creator>
				<category><![CDATA[Articles]]></category>
		<category><![CDATA[Math]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[lisp]]></category>

		<guid isPermaLink="false">http://nklein.com/?p=893</guid>
		<description><![CDATA[I added a heads-up display to the code from the previous article.  This display shows you the coordinates of each of the bees.
The leader-bee is thick white string with green beads.  The follower-bees are the thin black strings with red beads.  Going clockwise from the top, the beads on each string represent [...]]]></description>
			<content:encoded><![CDATA[<p>I added a heads-up display to the code from the <a href="http://nklein.com/2009/09/inverse-fourier-transform-of-swarm-data/">previous article</a>.  This display shows you the coordinates of each of the bees.</p>
<p>The leader-bee is thick white string with green beads.  The follower-bees are the thin black strings with red beads.  Going clockwise from the top, the beads on each string represent amplitude, x-frequency, y-frequency, x-phase, and y-phase.  The outside of the HUD represents positive infinity.  The inner circle of the HUD represents negative infinity.  The light yellow circle between the infinities represents zero.  Note: the scaling is extreme.  Two is almost 90% of the way from zero to infinity on this scale.</p>
<p><object width="640" height="352"><param name="allowfullscreen" value="true" /><param name="allowscriptaccess" value="always" /><param name="movie" value="http://vimeo.com/moogaloop.swf?clip_id=6758874&amp;server=vimeo.com&amp;show_title=1&amp;show_byline=0&amp;show_portrait=0&amp;color=00ADEF&amp;fullscreen=1" /><embed src="http://vimeo.com/moogaloop.swf?clip_id=6758874&amp;server=vimeo.com&amp;show_title=1&amp;show_byline=0&amp;show_portrait=0&amp;color=00ADEF&amp;fullscreen=1" type="application/x-shockwave-flash" allowfullscreen="true" allowscriptaccess="always" width="640" height="352"></embed></object>
<p><a href="http://vimeo.com/6758874">Fourier Swarm with Bee Display</a> from <a href="http://vimeo.com/nklein">Patrick Stein</a> on <a href="http://vimeo.com">Vimeo</a>.</p>
<h3>Technical details of the HUD scaling</h3>
<p>Say you have a number <a href="http://wordpress.org/extend/plugins/easy-latex/" target="_blank" title="m"><img src="http://l.wordpress.com/latex.php?latex=m&#038;bg=FFFFCC&#038;fg=000000&#038;s=0" style="vertical-align:-20%;" class="tex" alt="m" /></a> that you want to map into the interval <a href="http://www.Thulasidas.com/latex" target="_blank" title="[-1,1]"><img src="http://l.wordpress.com/latex.php?latex=%5B-1%2C1%5D&#038;bg=FFFFCC&#038;fg=000000&#038;s=0" style="vertical-align:-20%;" class="tex" alt="[-1,1]" /></a>.  Draw the upper half of the unit circle centered at the origin.  Start moving from left to right along the circle until you find the spot with a tangent of slope <a ><img src="http://l.wordpress.com/latex.php?latex=m&#038;bg=FFFFCC&#038;fg=000000&#038;s=0" style="vertical-align:-20%;" class="tex" alt="m" /></a>.  Use the <img src="http://l.wordpress.com/latex.php?latex=x&#038;bg=FFFFCC&#038;fg=000000&#038;s=0" title="x" style="vertical-align:-20%;" class="tex" alt="x" />-coordinate of this point.  To map that interval onto my HUD, subtract your calculated <img src="http://l.wordpress.com/latex.php?latex=x&#038;bg=FFFFCC&#038;fg=000000&#038;s=0" title="x" style="vertical-align:-20%;" class="tex" alt="x" />-coordinate from two and use the result as the radius from the center of the HUD.</p>
<p>Here is the <a href="http://nklein.com/wp-content/uploads/2009/09/swarm-fft2.lisp">Lisp source code</a>.  It depends on <a href="http://www.xach.com/lisp/vecto/">Zach&#8217;s Vecto library</a>.</p>
<p>Edit: Oops&#8230; there is a bug in the HUD code.  The leader and first follower are not shown.  The second follower is the thick white line.  Ooops.  Okay&#8230; I have replaced the video and the source code now.</p>
]]></content:encoded>
			<wfw:commentRss>http://nklein.com/2009/09/fourier-transform-of-swarm-data-with-hud/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Beautiful Random FFTs</title>
		<link>http://nklein.com/2009/09/beautiful-random-ffts/</link>
		<comments>http://nklein.com/2009/09/beautiful-random-ffts/#comments</comments>
		<pubDate>Tue, 22 Sep 2009 17:07:32 +0000</pubDate>
		<dc:creator>pat</dc:creator>
				<category><![CDATA[Articles]]></category>
		<category><![CDATA[Math]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://nklein.com/?p=875</guid>
		<description><![CDATA[Neil Baylis emailed me a program he had put together that puts a few random numbers in a grid and does the inverse FFT of them.  His program cycles the values placed in the grid between -1 and 1 to animate some different effects.  I like just regenerating random ones over and over [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://pixpopuli.com/about">Neil Baylis</a> emailed me <a href="http://www.pixpopuli.com/labs/blob_thing.html">a program he had put together</a> that puts a few random numbers in a grid and does the inverse FFT of them.  His program cycles the values placed in the grid between -1 and 1 to animate some different effects.  I like just regenerating random ones over and over again or tweaking which cells of the grid are in use.  [You may already be on the same page as I am here, but I've got an idea kicking around now that I just have to try.... watch for it in a day or two.]</p>
<p><a href="http://nklein.com/wp-content/uploads/2009/09/blob_thing.png"><img src="http://nklein.com/wp-content/uploads/2009/09/blob_thing-300x192.png" alt="blob_thing" title="blob_thing" width="300" height="192" class="alignright size-medium wp-image-876" /></a>  Here is one of the nifty designs that I generated with his program.  I encourage you to <a href="http://www.pixpopuli.com/labs/blob_thing.html">download it</a> and play around a little bit.  It is really great to see in real-time how activating certain frequencies affects the output.</p>
<p>For reference, <a href="http://nklein.com/wp-content/uploads/2009/09/blob_thing_grid.png"><img src="http://nklein.com/wp-content/uploads/2009/09/blob_thing_grid-150x146.png" alt="blob_thing_grid" title="blob_thing_grid" width="150" height="146" class="alignright size-thumbnail wp-image-877" /></a> here are the frequencies that I had activated for the above picture.</p>
<p>There are gorgeous specular features and shading in his output images.  He could have just it left it flat-colored, but he went the extra distance to add a lighting model.  The results are stunning.  You should click on the above image to see it full-size.  And, you should check out the <a href="http://pixpopuli.com/labs/blob_ps_demo.jpg">the example on Neil&#8217;s page</a>.</p>
<p class="clear">&nbsp;</p>
]]></content:encoded>
			<wfw:commentRss>http://nklein.com/2009/09/beautiful-random-ffts/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Blur and Edge Detect your Fourier Transforms</title>
		<link>http://nklein.com/2009/09/blur-and-edge-detect-your-fourier-transforms/</link>
		<comments>http://nklein.com/2009/09/blur-and-edge-detect-your-fourier-transforms/#comments</comments>
		<pubDate>Mon, 21 Sep 2009 23:04:10 +0000</pubDate>
		<dc:creator>pat</dc:creator>
				<category><![CDATA[Articles]]></category>
		<category><![CDATA[Math]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[javascript]]></category>

		<guid isPermaLink="false">http://nklein.com/?p=862</guid>
		<description><![CDATA[I have now added convolutions to my FFT Paint application.  Here, I started with the following image (again, magnitude on the left and phase on the right):
&#160;&#160;
From there, I took the Fourier Transform:
&#160;&#160;
Then, I did a horizontal Sobel operator on it to get this:
&#160;&#160;
From there, I did the Inverse Fourier Transform to obtain this:
&#160;&#160;
Enjoy!
]]></description>
			<content:encoded><![CDATA[<p>I have now added convolutions to <a href="http://nklein.com/mas-961/fft-paint/">my FFT Paint application</a>.  Here, I started with the following image (again, magnitude on the left and phase on the right):<br />
<center><a href="http://nklein.com/wp-content/uploads/2009/09/conv_orig_m.png"><img src="http://nklein.com/wp-content/uploads/2009/09/conv_orig_m-300x300.png" alt="conv_orig_m" title="conv_orig_m" width="300" height="300" class="alignnone size-medium wp-image-863" /></a>&nbsp;&nbsp;<a href="http://nklein.com/wp-content/uploads/2009/09/conv_orig_p.png"><img src="http://nklein.com/wp-content/uploads/2009/09/conv_orig_p-300x300.png" alt="conv_orig_p" title="conv_orig_p" width="300" height="300" class="alignnone size-medium wp-image-864" /></a></center></p>
<p>From there, I took the Fourier Transform:<br />
<center><a href="http://nklein.com/wp-content/uploads/2009/09/conv_pre_m.png"><img src="http://nklein.com/wp-content/uploads/2009/09/conv_pre_m-300x300.png" alt="conv_pre_m" title="conv_pre_m" width="300" height="300" class="alignnone size-medium wp-image-865" /></a>&nbsp;&nbsp;<a href="http://nklein.com/wp-content/uploads/2009/09/conv_pre_p.png"><img src="http://nklein.com/wp-content/uploads/2009/09/conv_pre_p-300x300.png" alt="conv_pre_p" title="conv_pre_p" width="300" height="300" class="alignnone size-medium wp-image-866" /></a></center></p>
<p>Then, I did a horizontal <a href="http://en.wikipedia.org/wiki/Sobel_operator">Sobel operator</a> on it to get this:<br />
<center><a href="http://nklein.com/wp-content/uploads/2009/09/conv_post_m.png"><img src="http://nklein.com/wp-content/uploads/2009/09/conv_post_m-300x300.png" alt="conv_post_m" title="conv_post_m" width="300" height="300" class="alignnone size-medium wp-image-867" /></a>&nbsp;&nbsp;<a href="http://nklein.com/wp-content/uploads/2009/09/conv_post_p.png"><img src="http://nklein.com/wp-content/uploads/2009/09/conv_post_p-300x300.png" alt="conv_post_p" title="conv_post_p" width="300" height="300" class="alignnone size-medium wp-image-868" /></a></center></p>
<p>From there, I did the Inverse Fourier Transform to obtain this:<br />
<center><a href="http://nklein.com/wp-content/uploads/2009/09/conv_final_m.png"><img src="http://nklein.com/wp-content/uploads/2009/09/conv_final_m-300x300.png" alt="conv_final_m" title="conv_final_m" width="300" height="300" class="alignnone size-medium wp-image-869" /></a>&nbsp;&nbsp;<a href="http://nklein.com/wp-content/uploads/2009/09/conv_final_p.png"><img src="http://nklein.com/wp-content/uploads/2009/09/conv_final_p-300x300.png" alt="conv_final_p" title="conv_final_p" width="300" height="300" class="alignnone size-medium wp-image-870" /></a></center></p>
<p>Enjoy!</p>
]]></content:encoded>
			<wfw:commentRss>http://nklein.com/2009/09/blur-and-edge-detect-your-fourier-transforms/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Do Radial Gradients on your FFT</title>
		<link>http://nklein.com/2009/09/do-radial-gradients-on-your-fft/</link>
		<comments>http://nklein.com/2009/09/do-radial-gradients-on-your-fft/#comments</comments>
		<pubDate>Wed, 16 Sep 2009 19:10:09 +0000</pubDate>
		<dc:creator>pat</dc:creator>
				<category><![CDATA[Articles]]></category>
		<category><![CDATA[Math]]></category>

		<guid isPermaLink="false">http://nklein.com/?p=849</guid>
		<description><![CDATA[I have now added radial gradients to my FFT Paint application.  Here, I wiped out an image entirely and painted my own data using only the new gradient tool to come up with this starting point:
&#160;&#160;
This is what it looks like if I do the inverse FFT of that data.  It&#8217;s not an [...]]]></description>
			<content:encoded><![CDATA[<p>I have now added radial gradients to my <a href="http://nklein.com/mas-961/fft-paint/">FFT Paint application</a>.  Here, I wiped out an image entirely and painted my own data using only the new gradient tool to come up with this starting point:<br />
<center><a href="http://nklein.com/wp-content/uploads/2009/09/mag_grad.png"><img src="http://nklein.com/wp-content/uploads/2009/09/mag_grad-300x300.png" alt="mag_grad" title="mag_grad" width="300" height="300" class="alignnone size-medium wp-image-850" /></a>&nbsp;&nbsp;<a href="http://nklein.com/wp-content/uploads/2009/09/phase_grad.png"><img src="http://nklein.com/wp-content/uploads/2009/09/phase_grad-300x300.png" alt="phase_grad" title="phase_grad" width="300" height="300" class="alignnone size-medium wp-image-851" /></a></center></p>
<p>This is what it looks like if I do the inverse FFT of that data.  It&#8217;s not an incredibly interesting result, but it does show the power.<br />
<center><a href="http://nklein.com/wp-content/uploads/2009/09/mag_inv.png"><img src="http://nklein.com/wp-content/uploads/2009/09/mag_inv-300x300.png" alt="mag_inv" title="mag_inv" width="300" height="300" class="alignnone size-medium wp-image-852" /></a>&nbsp;&nbsp;<a href="http://nklein.com/wp-content/uploads/2009/09/phase_inv.png"><img src="http://nklein.com/wp-content/uploads/2009/09/phase_inv-300x300.png" alt="phase_inv" title="phase_inv" width="300" height="300" class="alignnone size-medium wp-image-853" /></a></center></p>
<p>Here is another example of a starting point made entirely with the gradient paint tool:<br />
<center><a href="http://nklein.com/wp-content/uploads/2009/09/conc_tm.png"><img src="http://nklein.com/wp-content/uploads/2009/09/conc_tm-300x300.png" alt="conc_tm" title="conc_tm" width="300" height="300" class="alignnone size-medium wp-image-857" /></a>&nbsp;&nbsp;<a href="http://nklein.com/wp-content/uploads/2009/09/conc_tp.png"><img src="http://nklein.com/wp-content/uploads/2009/09/conc_tp-300x300.png" alt="conc_tp" title="conc_tp" width="300" height="300" class="alignnone size-medium wp-image-858" /></a></center></p>
<p>And, its resulting image when I do the inverse transform:<br />
<center><a href="http://nklein.com/wp-content/uploads/2009/09/conc_rm.png"><img src="http://nklein.com/wp-content/uploads/2009/09/conc_rm-300x300.png" alt="conc_rm" title="conc_rm" width="300" height="300" class="alignnone size-medium wp-image-859" /></a>&nbsp;&nbsp;<a href="http://nklein.com/wp-content/uploads/2009/09/conc_rp.png"><img src="http://nklein.com/wp-content/uploads/2009/09/conc_rp-300x300.png" alt="conc_rp" title="conc_rp" width="300" height="300" class="alignnone size-medium wp-image-860" /></a></center></p>
]]></content:encoded>
			<wfw:commentRss>http://nklein.com/2009/09/do-radial-gradients-on-your-fft/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Simple Function Plotting in Javascript</title>
		<link>http://nklein.com/2009/09/simple-function-plotting-in-javascript/</link>
		<comments>http://nklein.com/2009/09/simple-function-plotting-in-javascript/#comments</comments>
		<pubDate>Tue, 15 Sep 2009 15:02:59 +0000</pubDate>
		<dc:creator>pat</dc:creator>
				<category><![CDATA[Articles]]></category>
		<category><![CDATA[Math]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[javascript]]></category>

		<guid isPermaLink="false">http://nklein.com/?p=843</guid>
		<description><![CDATA[Rather than work more on my FFT Paint program yesterday, I took a break to make a simple function plotter.  There are lots of useful Java applets out there that do more than what I&#8217;ve done here.  And, certainly, gnuplot is much more complete and useful.
On the other hand, if you just want [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://nklein.com/wp-content/uploads/2009/09/graph.png"><img src="http://nklein.com/wp-content/uploads/2009/09/graph-300x200.png" alt="graph" title="graph" width="300" height="200" class="alignright size-medium wp-image-846" style="margin-top: 4mm;"/></a>Rather than work more on my <a href="http://nklein.com/mas-961/fft-paint/">FFT Paint</a> program yesterday, I took a break to make a <a href="http://nklein.com/math-scripts/simple-graph/">simple function plotter</a>.  There are lots of useful Java applets out there that do more than what I&#8217;ve done here.  And, certainly, <a href="http://www.gnuplot.info/">gnuplot</a> is much more complete and useful.</p>
<p>On the other hand, if you just want to quickly plot something that you can save as an image, you either have to re-learn gnuplot (every time I use it, I have to sift through the help to figure out how to set up my tics and labels and output size and output filename) or you have to do screen grabs from one of the Java applets.</p>
<p>With the <a href="http://nklein.com/math-scripts/simple-graph/">plotter that I wrote yesterday</a>, you click on the <q>Snapshot</q> button, then right-click to save the image or copy it to your clipboard.</p>
<p>Enjoy.</p>
]]></content:encoded>
			<wfw:commentRss>http://nklein.com/2009/09/simple-function-plotting-in-javascript/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Trapdoor Puzzles</title>
		<link>http://nklein.com/2009/08/trapdoor-puzzles/</link>
		<comments>http://nklein.com/2009/08/trapdoor-puzzles/#comments</comments>
		<pubDate>Fri, 28 Aug 2009 22:21:46 +0000</pubDate>
		<dc:creator>pat</dc:creator>
				<category><![CDATA[Articles]]></category>
		<category><![CDATA[Math]]></category>

		<guid isPermaLink="false">http://nklein.com/?p=783</guid>
		<description><![CDATA[With most logic or word puzzles, your goal is to find a solution.  To make those puzzles interesting, the puzzle maker has to assure that there is a solution and that the solution is unique.  This is true for crossword puzzles, sudoku puzzles, cryptograms, rebuses (rebi?), mazes, Lights Out games, and binary logic [...]]]></description>
			<content:encoded><![CDATA[<p>With most logic or word puzzles, your goal is to find a solution.  To make those puzzles interesting, the puzzle maker has to assure that there is a solution and that the solution is unique.  This is true for crossword puzzles, sudoku puzzles, cryptograms, rebuses (rebi?), mazes, <a href="http://en.wikipedia.org/wiki/Lights_Out_(game)">Lights Out games</a>, and binary logic puzzles of this form: <q>Alice is wearing red.  John is older than Jimmy.  The tallest person is not wearing green.  Clue. Clue. Clue.  Who is standing next to Alice?</q>  <a href="http://www.puzzles.com/products/RushHour/RushHourApp.htm">Rush Hour games</a> and <a href="http://nlvm.usu.edu/en/nav/frames_asid_112_g_2_t_4.html?open=activities">tanagrams</a> are also usually crafted so that there is a unique solution.</p>
<h3>Uniqueness</h3>
<p>For some of these games, we mean a unique solution amongst all solutions without backtracking.  For example, in solving a maze, we would not consider solutions that involve running down a corridor only to come back along the same corridor.  For Lights Out games, the concept is simple but describing it takes work.  Imagine that every square on the board, in addition to being lit or not, also has a letter &#8216;A&#8217; on it at the start of the game.  Whenever you press a button labelled &#8216;A&#8217;, it gets relabeled &#8216;B&#8217;.  Every time you press a button labelled &#8216;B&#8217;, it gets relabeled &#8216;A&#8217;.  Two solutions are identical if the board has the same configuration of &#8216;A&#8217;s and &#8216;B&#8217;s after both moves.  Thus, hitting any button more than once was just a waste of time.  However, the order that you hit the buttons makes no difference at all.</p>
<p>There is a <a href="http://mazes.com/election-day-puzzle-1996.html">notable crossword puzzle</a> with two solutions.  There are some notorious Sudoku puzzles with two solutions.  I don&#8217;t know of any cryptograms with two solutions, but I am going to go asearchin&#8217; some time.</p>
<p>Technically, a Sudoku puzzle with two solutions cannot be solved.  You had to make a guess somewhere and it turned out you had twice the odds of being right that you should have.  It is easy to accidentally take too many numbers out of the Sudoku grid and end up with an ambiguous puzzle.  For a crossword puzzle, I doubt most people bother to check to see if the solution is unique.  It probably is unique by sheer combinatorics.  The same may be true cryptograms, as well.  You&#8217;d have to specifically be trying to make them ambiguous to achieve it.</p>
<h3>Trapdoors</h3>
<p>Where are the trapdoors?  Well, the puzzle creator has a much harder job than the puzzle solver for most games.  It is easy to construct mazes and Lights Out games with unique solutions (in fact, it&#8217;s easy to show that if a Lights Out game is solvable, the solution is unique).  It is not so easy to construct other puzzles that have provably unique solutions.</p>
<p>This brings us to the <a href="http://en.wikipedia.org/wiki/Discrete_logarithm">Discrete Logarithm Problem</a>.  Imagine that I told you that <a href="http://wordpress.org/extend/plugins/easy-latex/" target="_blank" title="3^x \equiv 16 \pmod{29}"><img src="http://l.wordpress.com/latex.php?latex=3%5Ex%20%5Cequiv%2016%20%5Cpmod%7B29%7D&#038;bg=FFFFCC&#038;fg=000000&#038;s=0" style="vertical-align:-20%;" class="tex" alt="3^x \equiv 16 \pmod{29}" /></a> and asked you what <a href="http://www.Thulasidas.com/latex" target="_blank" title="x"><img src="http://l.wordpress.com/latex.php?latex=x&#038;bg=FFFFCC&#038;fg=000000&#038;s=0" style="vertical-align:-20%;" class="tex" alt="x" /></a> is modulo 28.  There is no really easy way to solve this problem, yet it is easy to formulate such problems.  I checked that <a ><img src="http://l.wordpress.com/latex.php?latex=3%5E%7B14%7D%20%5Cneq%201%20%5Cpmod%7B29%7D&#038;bg=FFFFCC&#038;fg=000000&#038;s=0" style="vertical-align:-20%;" class="tex" alt="3^{14} \neq 1 \pmod{29}" /></a>.  Then, I picked <img src="http://l.wordpress.com/latex.php?latex=x%20%3D%2012&#038;bg=FFFFCC&#038;fg=000000&#038;s=0" title="x = 12" style="vertical-align:-20%;" class="tex" alt="x = 12" /> and plugged it in to see that <img src="http://l.wordpress.com/latex.php?latex=3%5E%7B12%7D%20%5Cequiv%2016%20%5Cpmod%7B29%7D&#038;bg=FFFFCC&#038;fg=000000&#038;s=0" title="3^{12} \equiv 16 \pmod{29}" style="vertical-align:-20%;" class="tex" alt="3^{12} \equiv 16 \pmod{29}" />.  I started from the answer and was quickly able to generate a problem that I can guarantee has a unique solution.</p>
<p>This is called a trapdoor function.  It is easy to calculate, but hard to invert the calculation.  It&#8217;s also not a very fun problem to do.  You will never open up the comics page of your newspaper and find five of these problems below the bridge column and above the horoscopes.</p>
<p>I can easily imagine a whole variety of Lights Out type games where lights have more than one state.  It would be easy to prove, for a wide class of such games, that when a solution exists, it is unique.  These would all be solvable by inverting a matrix whose coefficients come from a finite field.</p>
<p>What other puzzles could take advantage of trapdoor functions to make the game easier on the puzzle maker than on the puzzle solver?</p>
]]></content:encoded>
			<wfw:commentRss>http://nklein.com/2009/08/trapdoor-puzzles/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Taking the Long Way Home</title>
		<link>http://nklein.com/2009/08/taking-the-long-way-home/</link>
		<comments>http://nklein.com/2009/08/taking-the-long-way-home/#comments</comments>
		<pubDate>Fri, 14 Aug 2009 18:07:52 +0000</pubDate>
		<dc:creator>pat</dc:creator>
				<category><![CDATA[Articles]]></category>
		<category><![CDATA[Math]]></category>
		<category><![CDATA[probability]]></category>

		<guid isPermaLink="false">http://nklein.com/?p=759</guid>
		<description><![CDATA[I was pondering non-deterministic, shareware nagging.  Suppose that every time you performed a particular function, I wanted to randomly decide whether to nag you this time or not.  Suppose further that I&#8217;d prefer not to have to keep track of how long it&#8217;s been since I last nagged you, but I&#8217;d still like [...]]]></description>
			<content:encoded><![CDATA[<p>I was pondering non-deterministic, shareware nagging.  Suppose that every time you performed a particular function, I wanted to randomly decide whether to nag you this time or not.  Suppose further that I&#8217;d prefer not to have to keep track of how long it&#8217;s been since I last nagged you, but I&#8217;d still like you to be nagged about once every <a href="http://wordpress.org/extend/plugins/easy-latex/" target="_blank" title="n"><img src="http://l.wordpress.com/latex.php?latex=n&#038;bg=FFFFCC&#038;fg=000000&#038;s=0" style="vertical-align:-20%;" class="tex" alt="n" /></a> times.  Naïvely, one would expect that if I just chose a random number uniformly from the range zero to one and nag if the number is less than <a href="http://www.Thulasidas.com/latex" target="_blank" title="\frac{1}{n}"><img src="http://l.wordpress.com/latex.php?latex=%5Cfrac%7B1%7D%7Bn%7D&#038;bg=FFFFCC&#038;fg=000000&#038;s=0" style="vertical-align:-20%;" class="tex" alt="\frac{1}{n}" /></a>, this would do the trick.  You would expect that if you rolled an <a ><img src="http://l.wordpress.com/latex.php?latex=n&#038;bg=FFFFCC&#038;fg=000000&#038;s=0" style="vertical-align:-20%;" class="tex" alt="n" /></a>-sided die each time, that you&#8217;d roll a one about every <img src="http://l.wordpress.com/latex.php?latex=n&#038;bg=FFFFCC&#038;fg=000000&#038;s=0" title="n" style="vertical-align:-20%;" class="tex" alt="n" />-th time.</p>
<p>Me, I&#8217;ve learned to never trust my naïve intuition when it comes to probability questions.  Well, it turns out that the naïve answer is perfectly correct in this case.  Here is the epic journey I took to get there though.</p>
<p>Here&#8217;s what I wanted.  I wanted to know the probability <img src="http://l.wordpress.com/latex.php?latex=p&#038;bg=FFFFCC&#038;fg=000000&#038;s=0" title="p" style="vertical-align:-20%;" class="tex" alt="p" /> needed so the expected number of trials until the first success is some number <img src="http://l.wordpress.com/latex.php?latex=n&#038;bg=FFFFCC&#038;fg=000000&#038;s=0" title="n" style="vertical-align:-20%;" class="tex" alt="n" /> given that each trial is independent and is successful <img src="http://l.wordpress.com/latex.php?latex=p&#038;bg=FFFFCC&#038;fg=000000&#038;s=0" title="p" style="vertical-align:-20%;" class="tex" alt="p" /> proportion of the time.  Suppose for a moment that I decided I am going to roll a six-sided die until I get a one.  What is the expected number of times that I need to roll that die to get my first one?  What I want here is the inverse problem.  How many sides should my die be if I want it to take <img src="http://l.wordpress.com/latex.php?latex=n&#038;bg=FFFFCC&#038;fg=000000&#038;s=0" title="n" style="vertical-align:-20%;" class="tex" alt="n" /> tries (on average) to get my first one?</p>
<p><a href="http://nklein.com/wp-content/uploads/2009/08/tree.png"><img src="http://nklein.com/wp-content/uploads/2009/08/tree.png" alt="tree" title="tree" width="256" height="256" class="alignright size-full wp-image-760" /></a> In the picture at the right, each circle represents a trial.  Each square represents a success.  What I am looking for is what value should I pick for <img src="http://l.wordpress.com/latex.php?latex=p&#038;bg=FFFFCC&#038;fg=000000&#038;s=0" title="p" style="vertical-align:-20%;" class="tex" alt="p" /> so that if you go left <img src="http://l.wordpress.com/latex.php?latex=p&#038;bg=FFFFCC&#038;fg=000000&#038;s=0" title="p" style="vertical-align:-20%;" class="tex" alt="p" /> proportion of the time and go right the other <img src="http://l.wordpress.com/latex.php?latex=%281-p%29&#038;bg=FFFFCC&#038;fg=000000&#038;s=0" title="(1-p)" style="vertical-align:-20%;" class="tex" alt="(1-p)" /> proportion of the time, you will expect to hit <img src="http://l.wordpress.com/latex.php?latex=n&#038;bg=FFFFCC&#038;fg=000000&#038;s=0" title="n" style="vertical-align:-20%;" class="tex" alt="n" /> circles along your descent.  You could, if very unlucky (or lucky in this case since I&#8217;m going to nag you) go forever to the right and never get to a rectangle.  So, already, we&#8217;re expecting an infinite sum.</p>
<p>Indeed, the expected number of circles you cross in a random descent of this tree is <img src="http://l.wordpress.com/latex.php?latex=E%28p%29%20%3D%20%5Csum_%7Bk%3D1%7D%5E%7B%5Cinfty%7D%20k%20%5Ccdot%20p%20%5Ccdot%20%281%20-%20p%29%5E%7Bk-1%7D&#038;bg=FFFFCC&#038;fg=000000&#038;s=0" title="E(p) = \sum_{k=1}^{\infty} k \cdot p \cdot (1 - p)^{k-1}" style="vertical-align:-20%;" class="tex" alt="E(p) = \sum_{k=1}^{\infty} k \cdot p \cdot (1 - p)^{k-1}" />.  Massaging this a little, we can get it into a simpler form:  <img src="http://l.wordpress.com/latex.php?latex=E%28p%29%20%3D%20%5Cfrac%7Bp%7D%7B1-p%7D%20%5Csum_%7Bk%3D1%7D%5E%7B%5Cinfty%7D%20k%20%5Ccdot%20%281%20-%20p%29%5Ek&#038;bg=FFFFCC&#038;fg=000000&#038;s=0" title="E(p) = \frac{p}{1-p} \sum_{k=1}^{\infty} k \cdot (1 - p)^k" style="vertical-align:-20%;" class="tex" alt="E(p) = \frac{p}{1-p} \sum_{k=1}^{\infty} k \cdot (1 - p)^k" />.</p>
<p>That infinite series is pretty nasty looking though.  So, we whip out <a href="http://pi.physik.uni-bonn.de/~dieckman/InfProd/InfProd.html#q-Series">a handy table of infinite series</a> and see that:<br />
<center><img src="http://l.wordpress.com/latex.php?latex=%5Csum_%7Bk%3D1%7D%5E%7B%5Cinfty%7D%20k%5Em%20q%5Ek%20%3D%20%5Cfrac%7B1%7D%7B%281-q%29%5E%7Bm%2B1%7D%7D%20%5Csum_%7Bk%3D0%7D%5Em%20%5Cleft%5B%20%5Csum_%7Bj%3D0%7D%5E%7Bk%2B1%7D%20%28-1%29%5Ej%20%7B%20%7Bm%2B1%7D%20%5Cchoose%20j%20%7D%20%28k%20-%20j%20%2B%201%29%5Em%20%5Cright%5D%20q%5E%7Bm-k%7D&#038;bg=FFFFCC&#038;fg=000000&#038;s=0" title="\sum_{k=1}^{\infty} k^m q^k = \frac{1}{(1-q)^{m+1}} \sum_{k=0}^m \left[ \sum_{j=0}^{k+1} (-1)^j { {m+1} \choose j } (k - j + 1)^m \right] q^{m-k}" style="vertical-align:-20%;" class="tex" alt="\sum_{k=1}^{\infty} k^m q^k = \frac{1}{(1-q)^{m+1}} \sum_{k=0}^m \left[ \sum_{j=0}^{k+1} (-1)^j { {m+1} \choose j } (k - j + 1)^m \right] q^{m-k}" /></center><br />
Fortunately, in our case, <img src="http://l.wordpress.com/latex.php?latex=m%20%3D%201&#038;bg=FFFFCC&#038;fg=000000&#038;s=0" title="m = 1" style="vertical-align:-20%;" class="tex" alt="m = 1" />.  As such, we can reduce this significantly:<br />
<center><img src="http://l.wordpress.com/latex.php?latex=%5Cfrac%7B1%7D%7B%281-q%29%5E2%7D%20%5Csum_%7Bk%3D0%7D%5E1%20%5Cleft%5B%20%5Csum_%7Bj%3D0%7D%5E%7Bk%2B1%7D%20%28-1%29%5Ej%20%7B%202%20%5Cchoose%20j%20%7D%20%28k%20-%20j%20%2B%201%29%20%5Cright%5D%20q%5E%7B1-k%7D&#038;bg=FFFFCC&#038;fg=000000&#038;s=0" title="\frac{1}{(1-q)^2} \sum_{k=0}^1 \left[ \sum_{j=0}^{k+1} (-1)^j { 2 \choose j } (k - j + 1) \right] q^{1-k}" style="vertical-align:-20%;" class="tex" alt="\frac{1}{(1-q)^2} \sum_{k=0}^1 \left[ \sum_{j=0}^{k+1} (-1)^j { 2 \choose j } (k - j + 1) \right] q^{1-k}" /></center><br />
<center><img src="http://l.wordpress.com/latex.php?latex=%5Cfrac%7B1%7D%7B%281-q%29%5E2%7D%20%5Cleft%5C%7B%20%5Cleft%5B%20%5Csum_%7Bj%3D0%7D%5E1%20%28-1%29%5Ej%20%7B%202%20%5Cchoose%20j%20%7D%20%281%20-%20j%29%20%5Cright%5D%20q%20%2B%20%5Cleft%5B%20%5Csum_%7Bj%3D0%7D%5E2%20%28-1%29%5Ej%20%7B%202%20%5Cchoose%20j%20%7D%20%282%20-%20j%29%20%5Cright%5D%20%5Cright%5C%7D&#038;bg=FFFFCC&#038;fg=000000&#038;s=0" title="\frac{1}{(1-q)^2} \left\{ \left[ \sum_{j=0}^1 (-1)^j { 2 \choose j } (1 - j) \right] q + \left[ \sum_{j=0}^2 (-1)^j { 2 \choose j } (2 - j) \right] \right\}" style="vertical-align:-20%;" class="tex" alt="\frac{1}{(1-q)^2} \left\{ \left[ \sum_{j=0}^1 (-1)^j { 2 \choose j } (1 - j) \right] q + \left[ \sum_{j=0}^2 (-1)^j { 2 \choose j } (2 - j) \right] \right\}" /></center><br />
Now, we note that when <img src="http://l.wordpress.com/latex.php?latex=j%20%3D%202&#038;bg=FFFFCC&#038;fg=000000&#038;s=0" title="j = 2" style="vertical-align:-20%;" class="tex" alt="j = 2" />, the last summand is zero.  So, <img src="http://l.wordpress.com/latex.php?latex=%5Csum_%7Bj%3D0%7D%5E2%20%28-1%29%5Ej%20%7B%202%20%5Cchoose%20j%20%7D%20%282%20-%20j%29%20%3D%20%5Csum_%7Bj%3D0%7D%5E1%20%28-1%29%5Ej%20%7B2%20%5Cchoose%20j%20%7D%20%282%20-%20j%29&#038;bg=FFFFCC&#038;fg=000000&#038;s=0" title="\sum_{j=0}^2 (-1)^j { 2 \choose j } (2 - j) = \sum_{j=0}^1 (-1)^j {2 \choose j } (2 - j)" style="vertical-align:-20%;" class="tex" alt="\sum_{j=0}^2 (-1)^j { 2 \choose j } (2 - j) = \sum_{j=0}^1 (-1)^j {2 \choose j } (2 - j)" />.  As such, we can combine our two summations into one:<br />
<center><img src="http://l.wordpress.com/latex.php?latex=%5Cfrac%7B1%7D%7B%281-q%29%5E2%7D%20%5Csum_%7Bj%3D0%7D%5E1%20%28-1%29%5Ej%20%7B%202%20%5Cchoose%20j%20%7D%20%5Cleft%5B%20%281%20-%20j%29%20q%20%2B%20%282%20-%20j%29%20%5Cright%5D&#038;bg=FFFFCC&#038;fg=000000&#038;s=0" title="\frac{1}{(1-q)^2} \sum_{j=0}^1 (-1)^j { 2 \choose j } \left[ (1 - j) q + (2 - j) \right]" style="vertical-align:-20%;" class="tex" alt="\frac{1}{(1-q)^2} \sum_{j=0}^1 (-1)^j { 2 \choose j } \left[ (1 - j) q + (2 - j) \right]" /></center><br />
And, since <img src="http://l.wordpress.com/latex.php?latex=j&#038;bg=FFFFCC&#038;fg=000000&#038;s=0" title="j" style="vertical-align:-20%;" class="tex" alt="j" /> is only going from zero to one, it&#8217;s easy enough to write out both terms of the summation:<br />
<center><img src="http://l.wordpress.com/latex.php?latex=%5Cfrac%7B1%7D%7B%281-q%29%5E2%7D%20%5Cleft%5C%7B%20%7B%202%20%5Cchoose%200%20%7D%20%5Cleft%5B%20q%20%2B%202%20%5Cright%5D%20-%20%7B%202%20%5Cchoose%201%20%7D%20%5Cright%5C%7D&#038;bg=FFFFCC&#038;fg=000000&#038;s=0" title="\frac{1}{(1-q)^2} \left\{ { 2 \choose 0 } \left[ q + 2 \right] - { 2 \choose 1 } \right\}" style="vertical-align:-20%;" class="tex" alt="\frac{1}{(1-q)^2} \left\{ { 2 \choose 0 } \left[ q + 2 \right] - { 2 \choose 1 } \right\}" /></center><br />
<center><img src="http://l.wordpress.com/latex.php?latex=%5Cfrac%7B1%7D%7B%281-q%29%5E2%7D%20%5Cleft%5C%7B%20q%20%2B%202%20-%202%20%5Cright%5C%7D&#038;bg=FFFFCC&#038;fg=000000&#038;s=0" title="\frac{1}{(1-q)^2} \left\{ q + 2 - 2 \right\}" style="vertical-align:-20%;" class="tex" alt="\frac{1}{(1-q)^2} \left\{ q + 2 - 2 \right\}" /></center><br />
<center><img src="http://l.wordpress.com/latex.php?latex=%5Csum_%7Bk%3D1%7D%5E%7B%5Cinfty%7D%20k%20q%5Ek%20%3D%20%5Cfrac%7Bq%7D%7B%281-q%29%5E2%7D&#038;bg=FFFFCC&#038;fg=000000&#038;s=0" title="\sum_{k=1}^{\infty} k q^k = \frac{q}{(1-q)^2}" style="vertical-align:-20%;" class="tex" alt="\sum_{k=1}^{\infty} k q^k = \frac{q}{(1-q)^2}" /></center></p>
<p>Whew!  Big sigh.  We&#8217;re through the ugly parts.  Now, in our case, <img src="http://l.wordpress.com/latex.php?latex=q%20%3D%20%281-p%29&#038;bg=FFFFCC&#038;fg=000000&#038;s=0" title="q = (1-p)" style="vertical-align:-20%;" class="tex" alt="q = (1-p)" />, and the equation we were interested in was <img src="http://l.wordpress.com/latex.php?latex=E%28p%29%20%3D%20%5Cfrac%7Bp%7D%7B1-p%7D%20%5Csum_%7Bk%3D1%7D%5E%7B%5Cinfty%7D%20k%20%5Ccdot%20%281%20-%20p%29%5Ek&#038;bg=FFFFCC&#038;fg=000000&#038;s=0" title="E(p) = \frac{p}{1-p} \sum_{k=1}^{\infty} k \cdot (1 - p)^k" style="vertical-align:-20%;" class="tex" alt="E(p) = \frac{p}{1-p} \sum_{k=1}^{\infty} k \cdot (1 - p)^k" />.  Plugging in <img src="http://l.wordpress.com/latex.php?latex=%281-p%29&#038;bg=FFFFCC&#038;fg=000000&#038;s=0" title="(1-p)" style="vertical-align:-20%;" class="tex" alt="(1-p)" /> for the <img src="http://l.wordpress.com/latex.php?latex=q&#038;bg=FFFFCC&#038;fg=000000&#038;s=0" title="q" style="vertical-align:-20%;" class="tex" alt="q" /> in our infinite series solution, we find that<br />
<center><img src="http://l.wordpress.com/latex.php?latex=E%28p%29%20%3D%20%5Cfrac%7Bp%7D%7B1-p%7D%20%5Ccdot%20%5Cfrac%7B1-p%7D%7Bp%5E2%7D%20%3D%20%5Cfrac%7B1%7D%7Bp%7D&#038;bg=FFFFCC&#038;fg=000000&#038;s=0" title="E(p) = \frac{p}{1-p} \cdot \frac{1-p}{p^2} = \frac{1}{p}" style="vertical-align:-20%;" class="tex" alt="E(p) = \frac{p}{1-p} \cdot \frac{1-p}{p^2} = \frac{1}{p}" /></center></p>
<p>Two pages of ugly, ugly algebra bears out the simple guess.  Wheee!  If we want <img src="http://l.wordpress.com/latex.php?latex=E%28p%29%20%3D%20n&#038;bg=FFFFCC&#038;fg=000000&#038;s=0" title="E(p) = n" style="vertical-align:-20%;" class="tex" alt="E(p) = n" />, then we have to pick <img src="http://l.wordpress.com/latex.php?latex=p%20%3D%20%5Cfrac%7B1%7D%7Bn%7D&#038;bg=FFFFCC&#038;fg=000000&#038;s=0" title="p = \frac{1}{n}" style="vertical-align:-20%;" class="tex" alt="p = \frac{1}{n}" />.  Hooray!</p>
]]></content:encoded>
			<wfw:commentRss>http://nklein.com/2009/08/taking-the-long-way-home/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Ask a simple question&#8230;</title>
		<link>http://nklein.com/2009/08/ask-a-simple-question/</link>
		<comments>http://nklein.com/2009/08/ask-a-simple-question/#comments</comments>
		<pubDate>Sat, 08 Aug 2009 05:55:45 +0000</pubDate>
		<dc:creator>pat</dc:creator>
				<category><![CDATA[Articles]]></category>
		<category><![CDATA[Math]]></category>
		<category><![CDATA[integrals]]></category>
		<category><![CDATA[probability density functions]]></category>
		<category><![CDATA[vecto]]></category>

		<guid isPermaLink="false">http://nklein.com/?p=735</guid>
		<description><![CDATA[Ask a simple question, spend the next forty minutes sifting through integral tables.  Earlier, I took some code that was uniformly selecting points from a square centered at the origin and converted it to code using points from a normal distribution.  For my purposes, I used a standard-deviation for the normal distribution that [...]]]></description>
			<content:encoded><![CDATA[<p>Ask a simple question, spend the next forty minutes sifting through integral tables.  Earlier, I took some code that was uniformly selecting points from a square centered at the origin and converted it to <a href="http://nklein.com/2009/08/casting-to-integers-considered-harmful/">code using points from a normal distribution</a>.  For my purposes, I used a standard-deviation for the normal distribution that was half of the in-radius of the box.  It wasn&#8217;t at all exact in any way.</p>
<p>What if I wanted to be exact?  Suppose I wanted to switch from points uniformly distributed in a box to points from a normal distribution while maintaining the <em>expected distance from the selected point to the origin</em>.</p>
<p>What exactly is the expected distance from a selected point to the origin if one picks each coordinate for the point from a uniform distribution on the range [-1,+1]?  Let&#8217;s start with the one-dimensional case.  The probability density is <a href="http://wordpress.org/extend/plugins/easy-latex/" target="_blank" title="\frac{1}{2}"><img src="http://l.wordpress.com/latex.php?latex=%5Cfrac%7B1%7D%7B2%7D&#038;bg=FFFFCC&#038;fg=000000&#038;s=0" style="vertical-align:-20%;" class="tex" alt="\frac{1}{2}" /></a> everywhere.  So, the answer is <a href="http://www.Thulasidas.com/latex" target="_blank" title="2 \int_{r=0}^1 \frac{1}{2} r dr = \frac{1}{2}"><img src="http://l.wordpress.com/latex.php?latex=2%20%5Cint_%7Br%3D0%7D%5E1%20%5Cfrac%7B1%7D%7B2%7D%20r%20dr%20%3D%20%5Cfrac%7B1%7D%7B2%7D&#038;bg=FFFFCC&#038;fg=000000&#038;s=0" style="vertical-align:-20%;" class="tex" alt="2 \int_{r=0}^1 \frac{1}{2} r dr = \frac{1}{2}" /></a> as we would expect.</p>
<p><img src="http://nklein.com/wp-content/uploads/2009/08/integrate.png" alt="(image created in Lisp using Zach&#039;s Vecto library)" title="integrate" width="256" height="256" class="alignright size-full wp-image-737" />How about two dimensions?  This is immediately more awkward.  The one-dimensional disk looks exactly like the one-dimensional square.  In two dimensions, the disk and square are quite different.  We either have to integrate over the square and calculate the radius of each point, or we have to integrate over increasing radii and be careful once we get to the in-radius of the square. <span style="font-size: 75%;">(image created in Lisp using <a href="http://xach.livejournal.com/">Zach</a>&#039;s <a href="http://www.xach.com/lisp/vecto/">Vecto</a> library)</span></p>
<p>I originally chose to try to integrate over the square.  The probability density is <a ><img src="http://l.wordpress.com/latex.php?latex=%5Cfrac%7B1%7D%7B4%7D&#038;bg=FFFFCC&#038;fg=000000&#038;s=0" style="vertical-align:-20%;" class="tex" alt="\frac{1}{4}" /></a> everywhere.  This means the expected radius is  <img src="http://l.wordpress.com/latex.php?latex=%5Cint_%7By%3D-1%7D%5E1%20%5Cint_%7Bx%3D-1%7D%5E1%20%5Csqrt%7Bx%5E2%2By%5E2%7D%20dx%20dy&#038;bg=FFFFCC&#038;fg=000000&#038;s=0" title="\int_{y=-1}^1 \int_{x=-1}^1 \sqrt{x^2+y^2} dx dy" style="vertical-align:-20%;" class="tex" alt="\int_{y=-1}^1 \int_{x=-1}^1 \sqrt{x^2+y^2} dx dy" />.  This gets to be no fun very fast since the inner integral comes out with a logarithm in it.  I mean, do you want to finish this integral?<br />
<center><img src="http://l.wordpress.com/latex.php?latex=%5Cint_%7By%3D-1%7D%5E1%20%5Cleft.%5Cleft%28%20%5Cfrac%7B1%7D%7B2%7D%20x%20%5Csqrt%7Bx%5E2%20%2B%20y%5E2%7D%20%2B%20%5Cfrac%7B1%7D%7B2%7D%20y%5E2%20%5Cln%20%5Cleft%28%20x%20%2B%20%5Csqrt%7Bx%5E2%20%2B%20y%5E2%7D%20%5Cright%29%5Cright%29%5Cright%7C_%7Bx%3D-1%7D%5E1%20dy&#038;bg=FFFFCC&#038;fg=000000&#038;s=0" title="\int_{y=-1}^1 \left.\left( \frac{1}{2} x \sqrt{x^2 + y^2} + \frac{1}{2} y^2 \ln \left( x + \sqrt{x^2 + y^2} \right)\right)\right|_{x=-1}^1 dy" style="vertical-align:-20%;" class="tex" alt="\int_{y=-1}^1 \left.\left( \frac{1}{2} x \sqrt{x^2 + y^2} + \frac{1}{2} y^2 \ln \left( x + \sqrt{x^2 + y^2} \right)\right)\right|_{x=-1}^1 dy" /></center><br />
It may be easy.  I didn&#8217;t want to mess with it.  I suppose once you evaluate the <img src="http://l.wordpress.com/latex.php?latex=x&#038;bg=FFFFCC&#038;fg=000000&#038;s=0" title="x" style="vertical-align:-20%;" class="tex" alt="x" /> terms, it reduces a fair bit (if I did that all right):<br />
<center><img src="http://l.wordpress.com/latex.php?latex=%5Cint_%7By%3D-1%7D%5E1%20%5Cleft%28%20%5Csqrt%7B1%20%2B%20y%5E2%7D%20%2B%20%5Cfrac%7B1%7D%7B2%7D%20y%5E2%20%5Cln%20%5Cleft%28%20%5Cfrac%7B1%20%2B%20%5Csqrt%7B1%20%2B%20y%5E2%7D%7D%7B-1%20%2B%20%5Csqrt%7B1%20%2B%20y%5E2%7D%7D%20%5Cright%29%20%5Cright%29%20dy&#038;bg=FFFFCC&#038;fg=000000&#038;s=0" title="\int_{y=-1}^1 \left( \sqrt{1 + y^2} + \frac{1}{2} y^2 \ln \left( \frac{1 + \sqrt{1 + y^2}}{-1 + \sqrt{1 + y^2}} \right) \right) dy" style="vertical-align:-20%;" class="tex" alt="\int_{y=-1}^1 \left( \sqrt{1 + y^2} + \frac{1}{2} y^2 \ln \left( \frac{1 + \sqrt{1 + y^2}}{-1 + \sqrt{1 + y^2}} \right) \right) dy" /></center><br />
I still don&#8217;t want to touch it.</p>
<p>So, how about integrating over the radii?  Again, the probability density is <img src="http://l.wordpress.com/latex.php?latex=%5Cfrac%7B1%7D%7B4%7D&#038;bg=FFFFCC&#038;fg=000000&#038;s=0" title="\frac{1}{4}" style="vertical-align:-20%;" class="tex" alt="\frac{1}{4}" /> everywhere.  This makes the expected radius:<br />
<center><img src="http://l.wordpress.com/latex.php?latex=%5Cint_%7Br%3D0%7D%5E1%20%5Cfrac%7B1%7D%7B4%7D%20%5Ccdot%202%20%5Cpi%20r%20dr%20%2B%20%5Cint_%7Br%3D1%7D%5E%7B%5Csqrt%7B2%7D%7D%20%5Cfrac%7B1%7D%7B4%7D%20%5Ccdot%208%20%5Cleft%28%20%5Cfrac%7B%5Cpi%7D%7B4%7D%20-%20%5Carccos%20%5Cfrac%7B1%7D%7Br%7D%20%5Cright%29%20dr&#038;bg=FFFFCC&#038;fg=000000&#038;s=0" title="\int_{r=0}^1 \frac{1}{4} \cdot 2 \pi r dr + \int_{r=1}^{\sqrt{2}} \frac{1}{4} \cdot 8 \left( \frac{\pi}{4} - \arccos \frac{1}{r} \right) dr" style="vertical-align:-20%;" class="tex" alt="\int_{r=0}^1 \frac{1}{4} \cdot 2 \pi r dr + \int_{r=1}^{\sqrt{2}} \frac{1}{4} \cdot 8 \left( \frac{\pi}{4} - \arccos \frac{1}{r} \right) dr" /></center><br />
With a little massaging, you can move the <img src="http://l.wordpress.com/latex.php?latex=%5Cfrac%7B%5Cpi%7D%7B4%7D&#038;bg=FFFFCC&#038;fg=000000&#038;s=0" title="\frac{\pi}{4}" style="vertical-align:-20%;" class="tex" alt="\frac{\pi}{4}" /> from the second integral into the first instead:<br />
<center><img src="http://l.wordpress.com/latex.php?latex=%5Cint_%7Br%3D0%7D%5E%7B%5Csqrt%7B2%7D%7D%20%5Cfrac%7B1%7D%7B4%7D%20%5Ccdot%202%20%5Cpi%20r%20dr%20-%20%5Cint_%7Br%3D1%7D%5E%7B%5Csqrt%7B2%7D%7D%20%5Cfrac%7B1%7D%7B4%7D%20%5Ccdot%208%20%5Carccos%20%5Cfrac%7B1%7D%7Br%7D%20dr&#038;bg=FFFFCC&#038;fg=000000&#038;s=0" title="\int_{r=0}^{\sqrt{2}} \frac{1}{4} \cdot 2 \pi r dr - \int_{r=1}^{\sqrt{2}} \frac{1}{4} \cdot 8 \arccos \frac{1}{r} dr" style="vertical-align:-20%;" class="tex" alt="\int_{r=0}^{\sqrt{2}} \frac{1}{4} \cdot 2 \pi r dr - \int_{r=1}^{\sqrt{2}} \frac{1}{4} \cdot 8 \arccos \frac{1}{r} dr" /></center><br />
This is hardly consolation though.  Here is where we get lost in our table of integrals for the better part of an hour.</p>
<p>And, this is only the two-dimensional case!  In my original problem, the ambient space was 32-dimensional.  My, oh my.</p>
]]></content:encoded>
			<wfw:commentRss>http://nklein.com/2009/08/ask-a-simple-question/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
