<?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; polynomials</title>
	<atom:link href="http://nklein.com/tags/polynomials/feed/" rel="self" type="application/rss+xml" />
	<link>http://nklein.com</link>
	<description>software development and consulting</description>
	<lastBuildDate>Tue, 22 May 2012 03:48:05 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
		<item>
		<title>The Anti-Cons</title>
		<link>http://nklein.com/2012/02/the-anti-cons/</link>
		<comments>http://nklein.com/2012/02/the-anti-cons/#comments</comments>
		<pubDate>Tue, 28 Feb 2012 03:51:44 +0000</pubDate>
		<dc:creator>pat</dc:creator>
				<category><![CDATA[Articles]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[anti-cons]]></category>
		<category><![CDATA[lisp]]></category>
		<category><![CDATA[polynomials]]></category>
		<category><![CDATA[recursion]]></category>

		<guid isPermaLink="false">http://nklein.com/?p=1801</guid>
		<description><![CDATA[Motivation I no longer remember what led me to this page of synthetic division implemented in various languages. The author provides a common lisp implementation for taking a list representing the coefficients of a polynomial in one variable and a number and returning the result of dividing the polynomial by . The author states: I&#8217;m [...]]]></description>
			<content:encoded><![CDATA[<h3>Motivation</h3>
<p>I no longer remember what led me to this page of <a href="http://kourge.net/node/98">synthetic division</a> implemented in various languages.  The author provides a common lisp implementation for taking a list representing the coefficients of a polynomial in one variable <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_1a507c5494969dc6de305770cadc6630.png" title="x" style="vertical-align:-20%;" class="tex" alt="x" /> and a number <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_a7175a60e504c64ad51e7119b44ee9aa.png" title="\alpha" style="vertical-align:-20%;" class="tex" alt="\alpha" /> and returning the result of dividing the polynomial by <img src="http://l.wordpress.com/latex.php?latex=%28x-%5Calpha%29&#038;bg=FFFFCC&#038;fg=000000&#038;s=0" title="(x-\alpha)" style="vertical-align:-20%;" class="tex" alt="(x-\alpha)" />. </p>
<p>The author states: <q>I&#8217;m very sure this isn&#8217;t considered <em>Lispy</em> and would surely seem like an awkward port from an extremely Algol-like mindset in the eyes of a seasoned Lisper.</q>  In the mood for the exercise, I reworked his code snippet into slightly more canonical lisp while leaving the basic structure the same:</p>
<div class="codecolorer-container lisp blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><pre class="lisp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">defun</span> synthetic-division <span style="color: #66cc66;">&#40;</span>polynomial divisor<span style="color: #66cc66;">&#41;</span>                                  
  <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">let</span>* <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span>result <span style="color: #66cc66;">&#40;</span>first polynomial<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>                                            
         <span style="color: #66cc66;">&#40;</span>quotient <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span> result<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>                                              
    <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">dolist</span> <span style="color: #66cc66;">&#40;</span>coefficient <span style="color: #66cc66;">&#40;</span>rest polynomial<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>                                     
      <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">setf</span> result <span style="color: #66cc66;">&#40;</span>* result divisor<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>                                          
      <span style="color: #66cc66;">&#40;</span>incf result coefficient<span style="color: #66cc66;">&#41;</span>                                                 
      <span style="color: #66cc66;">&#40;</span>push result quotient<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>                                                   
    <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">let</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span>remainder <span style="color: #66cc66;">&#40;</span>pop quotient<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>                                           
      <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span> <span style="color: #66cc66;">:</span><span style="color: #555;">quotient</span> <span style="color: #66cc66;">&#40;</span>nreverse quotient<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">:</span><span style="color: #555;">remainder</span> remainder<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></pre></div>
<p>From there, I went on to implement it using tail recursion to get rid of the <code class="codecolorer lisp default"><span class="lisp">#'<span style="color: #b1b100;">setf</span></span></code> and <code class="codecolorer lisp default"><span class="lisp">#'incf</span></code> and <code class="codecolorer lisp default"><span class="lisp">#'push</span></code>:</p>
<div class="codecolorer-container lisp blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><pre class="lisp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">defun</span> synthetic-division-<span style="color: #cc66cc;">2</span> <span style="color: #66cc66;">&#40;</span>polynomial divisor<span style="color: #66cc66;">&#41;</span>                                
  <span style="color: #66cc66;">&#40;</span>labels <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span>divide <span style="color: #66cc66;">&#40;</span>coefficients remainder quotient<span style="color: #66cc66;">&#41;</span>                            
             <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">if</span> coefficients                                                   
                 <span style="color: #66cc66;">&#40;</span>divide <span style="color: #66cc66;">&#40;</span>rest coefficients<span style="color: #66cc66;">&#41;</span>                                    
                         <span style="color: #66cc66;">&#40;</span>+ <span style="color: #66cc66;">&#40;</span>* divisor remainder<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span>first coefficients<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>         
                         <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span>* remainder quotient<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>                            
               <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span> <span style="color: #66cc66;">:</span><span style="color: #555;">quotient</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">reverse</span> quotient<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">:</span><span style="color: #555;">remainder</span> remainder<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>      
    <span style="color: #66cc66;">&#40;</span>divide <span style="color: #66cc66;">&#40;</span>rest polynomial<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span>first polynomial<span style="color: #66cc66;">&#41;</span> <span style="color: #b1b100;">nil</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></pre></div>
<p>What I didn&#8217;t like about this was the complexity of calling the tail-recursive portion.  If I just called it like I wished to  <code class="codecolorer lisp default"><span class="lisp"><span style="color: #66cc66;">&#40;</span>divide polynomial <span style="color: #cc66cc;">0</span> <span style="color: #b1b100;">nil</span><span style="color: #66cc66;">&#41;</span></span></code> then I ended up with one extra coefficient in the answer.  This wouldn&#8217;t do.</p>
<h3>The Joke</h3>
<p>There&#8217;s an old joke about a physicist, a biologist, and a mathematician who were having lunch at an outdoor café.  Just as their food was served, they noticed a couple walk into the house across the street.  As they were finishing up their meal, they saw three people walk out of that very same house.</p>
<p>The physicist said, <q>We must have miscounted.  Three must have entered before.</q></p>
<p>The biologist said, <q>They must have pro-created.</q></p>
<p>The mathematician said, <q>If one more person enters that house, it will again be empty.</q></p>
<h3>The Anti-Cons</h3>
<p>What I needed was a more-than-empty list.  I needed a negative cons-cell.  I needed something to put in place of the <code class="codecolorer lisp default"><span class="lisp"><span style="color: #b1b100;">nil</span></span></code> in <code class="codecolorer lisp default"><span class="lisp"><span style="color: #66cc66;">&#40;</span>divide polynomial <span style="color: #cc66cc;">0</span> <span style="color: #b1b100;">nil</span><span style="color: #66cc66;">&#41;</span></span></code> that would annihilate the first thing it was cons-ed to.</p>
<p>I haven&#8217;t come up with the right notation to make this clear.  It is somewhat like a <a href="http://en.wikipedia.org/wiki/Quasigroup">quasigroup</a> except that there is only one inverse element for all other elements.  Let&#8217;s denote this annihilator: <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_0824a1fdf135170467671326d5780e2b.png" title="\omega" style="vertical-align:-20%;" class="tex" alt="\omega" />.  Let&#8217;s denote list concatenation with <img src="http://l.wordpress.com/latex.php?latex=%5Coplus&#038;bg=FFFFCC&#038;fg=000000&#038;s=0" title="\oplus" style="vertical-align:-20%;" class="tex" alt="\oplus" />.</p>
<p>Only having one inverse-ish element means we have to give up associativity.  For lists <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_3cad95c81df4676e1b14e93cbb0b18ae.png" title="a" style="vertical-align:-20%;" class="tex" alt="a" /> and <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_97f4a1ba736119e3cd4de6fca35efd6b.png" title="b" style="vertical-align:-20%;" class="tex" alt="b" />, evaluating <img src="http://l.wordpress.com/latex.php?latex=%28a%20%5Coplus%20%5Comega%29%20%5Coplus%20b&#038;bg=FFFFCC&#038;fg=000000&#038;s=0" title="(a \oplus \omega) \oplus b" style="vertical-align:-20%;" class="tex" alt="(a \oplus \omega) \oplus b" /> equals <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_97f4a1ba736119e3cd4de6fca35efd6b.png" title="b" style="vertical-align:-20%;" class="tex" alt="b" />, but <img src="http://l.wordpress.com/latex.php?latex=a%20%5Coplus%20%28%5Comega%20%5Coplus%20b%29&#038;bg=FFFFCC&#038;fg=000000&#038;s=0" title="a \oplus (\omega \oplus b)" style="vertical-align:-20%;" class="tex" alt="a \oplus (\omega \oplus b)" /> equals <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_3cad95c81df4676e1b14e93cbb0b18ae.png" title="a" style="vertical-align:-20%;" class="tex" alt="a" />.</p>
<p>Associativity is a small price to pay though for a prettier call to my tail-recursive function, right?</p>
<h3>The Basic Operations</h3>
<p>For basic operations, I&#8217;m going to need the anti-cons itself and a lazy list of an arbitrary number of anti-conses.</p>
<div class="codecolorer-container lisp blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><pre class="lisp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #66cc66;">&#40;</span>defconstant anti-<span style="color: #b1b100;">cons</span> 'anti-<span style="color: #b1b100;">cons</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
<span style="color: #66cc66;">&#40;</span>defclass anti-cons-<span style="color: #b1b100;">list</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">length</span> <span style="color: #66cc66;">:</span><span style="color: #555;">initarg</span> <span style="color: #66cc66;">:</span><span style="color: #b1b100;">length</span> <span style="color: #66cc66;">:</span><span style="color: #555;">reader</span> anti-cons-list-<span style="color: #b1b100;">length</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">:</span><span style="color: #555;">default-initargs</span> <span style="color: #66cc66;">:</span><span style="color: #b1b100;">length</span> <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
<span style="color: #66cc66;">&#40;</span>defmethod print-object <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span>obj anti-cons-<span style="color: #b1b100;">list</span><span style="color: #66cc66;">&#41;</span> stream<span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#40;</span>print-unreadable-object <span style="color: #66cc66;">&#40;</span>obj stream<span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#40;</span>prin1 <span style="color: #66cc66;">&#40;</span>loop <span style="color: #66cc66;">:</span><span style="color: #555;">for</span> ii <span style="color: #66cc66;">:</span><span style="color: #555;">from</span> <span style="color: #cc66cc;">1</span> <span style="color: #66cc66;">:</span><span style="color: #555;">to</span> <span style="color: #66cc66;">&#40;</span>anti-cons-list-<span style="color: #b1b100;">length</span> obj<span style="color: #66cc66;">&#41;</span>
              <span style="color: #66cc66;">:</span><span style="color: #555;">collecting</span> 'anti-<span style="color: #b1b100;">cons</span><span style="color: #66cc66;">&#41;</span>
           stream<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></pre></div>
<p>Then, I&#8217;m going to make some macros to define generic functions named by adding a minus-sign to the end of a Common Lisp function.  The default implementation will simply be the common-lisp function.</p>
<div class="codecolorer-container lisp blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><pre class="lisp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">defmacro</span> defun- <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">name</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&amp;</span>rest args<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&amp;</span>body methods<span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">let</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span>name- <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">intern</span> <span style="color: #66cc66;">&#40;</span>concatenate 'string <span style="color: #66cc66;">&#40;</span>symbol-<span style="color: #b1b100;">name</span> <span style="color: #b1b100;">name</span><span style="color: #66cc66;">&#41;</span> <span style="color: #ff0000;">&quot;-&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
    `<span style="color: #66cc66;">&#40;</span>defgeneric <span style="color: #66cc66;">,</span>name- <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">,</span>@args<span style="color: #66cc66;">&#41;</span>
       <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">:</span><span style="color: #555;">method</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">,</span>@args<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">,</span><span style="color: #b1b100;">name</span> <span style="color: #66cc66;">,</span>@args<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
       <span style="color: #66cc66;">,</span>@methods<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></pre></div>
<p>I&#8217;m even going to go one step further for single-argument functions where I want to override the body for my lazy list of anti-conses using a single form for the body:</p>
<div class="codecolorer-container lisp blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><pre class="lisp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">defmacro</span> defun1- <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">name</span> <span style="color: #66cc66;">&#40;</span>arg<span style="color: #66cc66;">&#41;</span> a-list-form <span style="color: #66cc66;">&amp;</span>body body<span style="color: #66cc66;">&#41;</span>
  `<span style="color: #66cc66;">&#40;</span>defun- <span style="color: #66cc66;">,</span><span style="color: #b1b100;">name</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">,</span>arg<span style="color: #66cc66;">&#41;</span>
     <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">:</span><span style="color: #555;">method</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">,</span>arg anti-cons-<span style="color: #b1b100;">list</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">,</span>a-list-form<span style="color: #66cc66;">&#41;</span>
     <span style="color: #66cc66;">,</span>@body<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></pre></div>
<p>I need <code class="codecolorer lisp default"><span class="lisp">#'cons-</span></code> to set the stage.  I need to be able to cons an anti-cons with a normal list.  I need to be able to cons an anti-cons with a list of anti-conses.  And, I need to be able to cons something other than an anti-cons with a list of anti-conses.</p>
<div class="codecolorer-container lisp blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><pre class="lisp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #66cc66;">&#40;</span>defun- <span style="color: #b1b100;">cons</span> <span style="color: #66cc66;">&#40;</span>a b<span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">:</span><span style="color: #555;">method</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span>a <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">eql</span> 'anti-<span style="color: #b1b100;">cons</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span>b <span style="color: #b1b100;">list</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">null</span> b<span style="color: #66cc66;">&#41;</span>
        <span style="color: #66cc66;">&#40;</span>make-instance 'anti-cons-<span style="color: #b1b100;">list</span><span style="color: #66cc66;">&#41;</span>
        <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">cdr</span> b<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
  <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">:</span><span style="color: #555;">method</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span>a <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">eql</span> 'anti-<span style="color: #b1b100;">cons</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span>b anti-cons-<span style="color: #b1b100;">list</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#40;</span>make-instance 'anti-cons-<span style="color: #b1b100;">list</span> <span style="color: #66cc66;">:</span><span style="color: #b1b100;">length</span> <span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">1</span>+ <span style="color: #66cc66;">&#40;</span>anti-cons-list-<span style="color: #b1b100;">length</span> b<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
  <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">:</span><span style="color: #555;">method</span> <span style="color: #66cc66;">&#40;</span>a <span style="color: #66cc66;">&#40;</span>b anti-cons-<span style="color: #b1b100;">list</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">let</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span>b-len <span style="color: #66cc66;">&#40;</span>anti-cons-list-<span style="color: #b1b100;">length</span> b<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
      <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">when</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&gt;</span> b-len <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span>
        <span style="color: #66cc66;">&#40;</span>make-instance 'anti-cons-<span style="color: #b1b100;">list</span> <span style="color: #66cc66;">:</span><span style="color: #b1b100;">length</span> <span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">1</span>- b-len<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></pre></div>
<p>Now, I can go on to define some simple functions that can take either anti-cons lists or regular Common Lisp lists.</p>
<div class="codecolorer-container lisp blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><pre class="lisp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #66cc66;">&#40;</span>defun1- <span style="color: #b1b100;">length</span> <span style="color: #66cc66;">&#40;</span>a<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span>- <span style="color: #66cc66;">&#40;</span>anti-cons-list-<span style="color: #b1b100;">length</span> a<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
<span style="color: #66cc66;">&#40;</span>defun1- <span style="color: #b1b100;">car</span> <span style="color: #66cc66;">&#40;</span>a<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">:</span><span style="color: #555;">anti-</span><span style="color: #b1b100;">cons</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
<span style="color: #66cc66;">&#40;</span>defun1- <span style="color: #b1b100;">cdr</span> <span style="color: #66cc66;">&#40;</span>a<span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">let</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span>a-len <span style="color: #66cc66;">&#40;</span>anti-cons-list-<span style="color: #b1b100;">length</span> a<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">when</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&gt;</span> a-len <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span>
      <span style="color: #66cc66;">&#40;</span>make-instance 'anti-cons-<span style="color: #b1b100;">list</span> <span style="color: #66cc66;">:</span><span style="color: #b1b100;">length</span> <span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">1</span>- a-len<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
<span style="color: #66cc66;">&#40;</span>defun1- <span style="color: #b1b100;">cadr</span> <span style="color: #66cc66;">&#40;</span>a<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">when</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&gt;</span> <span style="color: #66cc66;">&#40;</span>anti-cons-list-<span style="color: #b1b100;">length</span> a<span style="color: #66cc66;">&#41;</span> <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">:</span><span style="color: #555;">anti-</span><span style="color: #b1b100;">cons</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#40;</span>defun1- <span style="color: #b1b100;">caddr</span> <span style="color: #66cc66;">&#40;</span>a<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">when</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&gt;</span> <span style="color: #66cc66;">&#40;</span>anti-cons-list-<span style="color: #b1b100;">length</span> a<span style="color: #66cc66;">&#41;</span> <span style="color: #cc66cc;">2</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">:</span><span style="color: #555;">anti-</span><span style="color: #b1b100;">cons</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></pre></div>
<p>To give a feel for how this all fits together, here&#8217;s a little interactive session:</p>
<div class="codecolorer-container lisp blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><pre class="lisp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">ANTI-CONS<span style="color: #66cc66;">&gt;</span> <span style="color: #66cc66;">&#40;</span>cons- anti-<span style="color: #b1b100;">cons</span> <span style="color: #b1b100;">nil</span><span style="color: #66cc66;">&#41;</span>
#<span style="color: #66cc66;">&lt;</span><span style="color: #66cc66;">&#40;</span>ANTI-<span style="color: #b1b100;">CONS</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&gt;</span>
&nbsp;
ANTI-CONS<span style="color: #66cc66;">&gt;</span> <span style="color: #66cc66;">&#40;</span>cons- anti-<span style="color: #b1b100;">cons</span> *<span style="color: #66cc66;">&#41;</span>
#<span style="color: #66cc66;">&lt;</span><span style="color: #66cc66;">&#40;</span>ANTI-<span style="color: #b1b100;">CONS</span> ANTI-<span style="color: #b1b100;">CONS</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&gt;</span>
&nbsp;
ANTI-CONS<span style="color: #66cc66;">&gt;</span> <span style="color: #66cc66;">&#40;</span>cons- <span style="color: #66cc66;">:</span><span style="color: #555;">a</span> *<span style="color: #66cc66;">&#41;</span>
#<span style="color: #66cc66;">&lt;</span><span style="color: #66cc66;">&#40;</span>ANTI-<span style="color: #b1b100;">CONS</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&gt;</span>
&nbsp;
ANTI-CONS<span style="color: #66cc66;">&gt;</span> <span style="color: #66cc66;">&#40;</span>length- *<span style="color: #66cc66;">&#41;</span>
-<span style="color: #cc66cc;">1</span></pre></div>
<h3>Denouement</h3>
<p>Only forty or fifty lines of code to go from:</p>
<div class="codecolorer-container lisp blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><pre class="lisp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #66cc66;">&#40;</span>divide <span style="color: #66cc66;">&#40;</span>rest polynomial<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span>first polynomial<span style="color: #66cc66;">&#41;</span> <span style="color: #b1b100;">nil</span><span style="color: #66cc66;">&#41;</span></pre></div>
<p>To this:</p>
<div class="codecolorer-container lisp blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><pre class="lisp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #66cc66;">&#40;</span>divide polynomial <span style="color: #cc66cc;">0</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">cons</span> anti-<span style="color: #b1b100;">cons</span> <span style="color: #b1b100;">nil</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></pre></div>
<p>Definitely worth it.</p>
]]></content:encoded>
			<wfw:commentRss>http://nklein.com/2012/02/the-anti-cons/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Complex Numbers for Rotating, Translating, and Scaling the Plane</title>
		<link>http://nklein.com/2009/06/complex-numbers-for-rotating-translating-and-scaling-the-plane/</link>
		<comments>http://nklein.com/2009/06/complex-numbers-for-rotating-translating-and-scaling-the-plane/#comments</comments>
		<pubDate>Sun, 07 Jun 2009 06:13:59 +0000</pubDate>
		<dc:creator>pat</dc:creator>
				<category><![CDATA[Articles]]></category>
		<category><![CDATA[Clifford Algebras]]></category>
		<category><![CDATA[Math]]></category>
		<category><![CDATA[conformal maps]]></category>
		<category><![CDATA[geometry]]></category>
		<category><![CDATA[polynomials]]></category>

		<guid isPermaLink="false">http://nklein.com/?p=511</guid>
		<description><![CDATA[A good friend of mine recently discovered some of the fun things you can do with complex numbers if you&#8217;re using them to represent points in the plane. Yesterday, I re-read a passage by Tony Smith about why one should be interested in Clifford algebras. Tony Smith&#8217;s passage included all of the fun one can [...]]]></description>
			<content:encoded><![CDATA[<p>A good friend of mine recently discovered some of the fun things you can do with complex numbers if you&#8217;re using them to represent points in the plane.  Yesterday, I re-read a passage by <a href="http://www.valdostamuseum.org/hamsmith/TShome.html">Tony Smith</a> about <a href="http://www.valdostamuseum.org/hamsmith/clfpq.html">why one should be interested in Clifford algebras</a>.  Tony Smith&#8217;s passage included all of the fun one can have with the complex plane and extends it to three, four, five, and more dimensions.  I thought, <q>I should segue from the complex numbers in the plane to Clifford algebras to quaternions in 3-space to Clifford algebras again in a series of posts here.</q></p>
<h3>What are Complex Numbers</h3>
<p>Say you&#8217;re playing around with polynomials.  You start playing with the equation <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_9fea09dd66c480fa713ecad13ed68053.png" title="z^2 - 1 = 0" style="vertical-align:-20%;" class="tex" alt="z^2 - 1 = 0" />.  WIth a little fiddling, you find this is equivalent to <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_3c8a1654ed4783532049c5df7fbc9449.png" title="z^2 = 1" style="vertical-align:-20%;" class="tex" alt="z^2 = 1" />.  Then, you take the square root of both sides to find that <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_bc13dda0bc6007242071d129b4d7b97e.png" title="z = \pm \sqrt{1} = \pm 1" style="vertical-align:-20%;" class="tex" alt="z = \pm \sqrt{1} = \pm 1" />.  We started with a polynomial equation in one variable in which the highest exponent was two and we found two answers.</p>
<p>Pounding your chest and sounding your barbaric yawp, you move on to <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_f365da03ebf58d14298115ccb3707800.png" title="z^2 + 1 = 0" style="vertical-align:-20%;" class="tex" alt="z^2 + 1 = 0" />.  This should be easy, right?  With the same fiddling, we find <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_c89984c5f0a8af60d1a7db39dd7a6594.png" title="z^2 = -1" style="vertical-align:-20%;" class="tex" alt="z^2 = -1" /> and then <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_73ccce548583ffab9beb7e11198019e5.png" title="z = \pm \sqrt{-1}" style="vertical-align:-20%;" class="tex" alt="z = \pm \sqrt{-1}" />.</p>
<p>Uh-oh.  What do we do now?  We can&#8217;t think of any number that when multiplied by itself gives us a negative number.  If we start with zero, we end with zero.  If we multiply a positive number by itself, we get a positive number.  If we multiply a negative number by itself, we get a <em>positive</em> number.  Again!</p>
<p><span id="more-511"></span></p>
<p>So, how do we get around this?  We pull an ace out of our sleeve.  We just run with the idea that there is such a number and see where it takes us.  We say, <q>There is a number <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_5477186a84cc2c889974ca6fd01ca96f.png" title="i" style="vertical-align:-20%;" class="tex" alt="i" /> such that <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_2effb75364932f653b3033ef36028f13.png" title="i^2 = -1" style="vertical-align:-20%;" class="tex" alt="i^2 = -1" />.  Everything else is going to stay the same.</q></p>
<p>Where does this take us?  It turns out, it takes us very, very far.  For starters, our equation <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_f365da03ebf58d14298115ccb3707800.png" title="z^2 + 1 = 0" style="vertical-align:-20%;" class="tex" alt="z^2 + 1 = 0" />, a polynomial equation in one variable where the highest exponent is two, now has two answers:  <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_de7bc04d341c737b5b97606a2dfe0981.png" title="z = \pm i" style="vertical-align:-20%;" class="tex" alt="z = \pm i" />.</p>
<p>What about <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_547b84259e1f91c9db9e937123e40aa2.png" title="z^2 + 4 = 0" style="vertical-align:-20%;" class="tex" alt="z^2 + 4 = 0" />?  It is a polynomial equation in one variable where the highest exponent is two.  It&#8217;d be pretty spiffy if there were two answers.  With the same manipulation as before, we find that <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_65f4411c3858e22f845bea4e9d5f340f.png" title="z = \pm \sqrt{-4}" style="vertical-align:-20%;" class="tex" alt="z = \pm \sqrt{-4}" />.</p>
<p>Now, we need to remember that if <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_3cad95c81df4676e1b14e93cbb0b18ae.png" title="a" style="vertical-align:-20%;" class="tex" alt="a" /> and <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_97f4a1ba736119e3cd4de6fca35efd6b.png" title="b" style="vertical-align:-20%;" class="tex" alt="b" /> are positive numbers, then <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_53519ada3075742e0a2ef81d7308daa0.png" title="\sqrt{a\cdot{b}} = \sqrt{a}\cdot\sqrt{b}" style="vertical-align:-20%;" class="tex" alt="\sqrt{a\cdot{b}} = \sqrt{a}\cdot\sqrt{b}" />.  Let&#8217;s see what happens if we extend this to allow our new number <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_5477186a84cc2c889974ca6fd01ca96f.png" title="i" style="vertical-align:-20%;" class="tex" alt="i" />.  If we said that <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_f5334506113f757d6e22338e56e99ab7.png" title="\sqrt{-4} = \sqrt{4}\cdot\sqrt{-1}" style="vertical-align:-20%;" class="tex" alt="\sqrt{-4} = \sqrt{4}\cdot\sqrt{-1}" />, then <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_b6464f3e24b21942e1d9926cbce23386.png" title="\sqrt{-4} = 2i" style="vertical-align:-20%;" class="tex" alt="\sqrt{-4} = 2i" />.  What happens if we multiply <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_06532e1cb171ae73340f6b0a07a3e9c2.png" title="2i\cdot2i" style="vertical-align:-20%;" class="tex" alt="2i\cdot2i" />?  When we multiply real number <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_3cad95c81df4676e1b14e93cbb0b18ae.png" title="a" style="vertical-align:-20%;" class="tex" alt="a" />, <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_97f4a1ba736119e3cd4de6fca35efd6b.png" title="b" style="vertical-align:-20%;" class="tex" alt="b" />, and <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_08c1898a88651cdd0a9fdb2d6ce6e8a1.png" title="c" style="vertical-align:-20%;" class="tex" alt="c" />, we can do it in any order.  We could do <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_22670214cc8aaea09f2552ba682a7958.png" title="a\cdot b\cdot c" style="vertical-align:-20%;" class="tex" alt="a\cdot b\cdot c" /> or <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_918f8f3d65feca981cbef93e84060ba6.png" title="a \cdot c \cdot b" style="vertical-align:-20%;" class="tex" alt="a \cdot c \cdot b" /> or <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_a2c2637af76fe91ad2a8df47a2c9bdec.png" title="c \cdot a \cdot b" style="vertical-align:-20%;" class="tex" alt="c \cdot a \cdot b" /> (or three other orders).  Well, let&#8217;s assume for now that when we multiply <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_5477186a84cc2c889974ca6fd01ca96f.png" title="i" style="vertical-align:-20%;" class="tex" alt="i" /> by a real number, we can do it in either order.  Then <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_a149d9a9bffb0ecf34821aab72924727.png" title="2i\cdot2i = 2\cdot2\cdot i\cdot i = 4 \cdot -1 = -4" style="vertical-align:-20%;" class="tex" alt="2i\cdot2i = 2\cdot2\cdot i\cdot i = 4 \cdot -1 = -4" />.  That&#8217;s exactly what we were hoping it would be.</p>
<p>Good.  Our equation <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_547b84259e1f91c9db9e937123e40aa2.png" title="z^2 + 4 = 0" style="vertical-align:-20%;" class="tex" alt="z^2 + 4 = 0" /> is a polynomial equation in one variable where the highest exponent is two and it has two solutions <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_4df9c813ea8509c221548a60da21d681.png" title="z = \pm 2i" style="vertical-align:-20%;" class="tex" alt="z = \pm 2i" />.</p>
<p>As it turns out, by adding in <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_5477186a84cc2c889974ca6fd01ca96f.png" title="i" style="vertical-align:-20%;" class="tex" alt="i" /> (and real number multiples of <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_5477186a84cc2c889974ca6fd01ca96f.png" title="i" style="vertical-align:-20%;" class="tex" alt="i" />) to our real numbers, we have the complex numbers.  These complex numbers are an algebraic completion of the real numbers.  That&#8217;s just a fancy way of saying that if you make a polynomial equation in one variable where all of the coefficients are real numbers and the highest exponent is <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_18a633fc025e2f1a862f7c8de3b31d6c.png" title="n" style="vertical-align:-20%;" class="tex" alt="n" />, then there will be <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_18a633fc025e2f1a862f7c8de3b31d6c.png" title="n" style="vertical-align:-20%;" class="tex" alt="n" /> solutions to the equation all in the complex numbers.</p>
<p>[It turns out that the complex numbers are algebraically complete themselves.  If you make a polynomial equation in one variable where all of the coefficients are complex numbers and the highest exponent is <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_18a633fc025e2f1a862f7c8de3b31d6c.png" title="n" style="vertical-align:-20%;" class="tex" alt="n" />, then there will be <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_18a633fc025e2f1a862f7c8de3b31d6c.png" title="n" style="vertical-align:-20%;" class="tex" alt="n" /> solutions to the equation in the complex numbers.]</p>
<h3>A Quick Review of Complex Arithmetic</h3>
<p>Above, we decided to say that <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_3cc652a30b982461e9f38a041117c43a.png" title="\sqrt{-1}" style="vertical-align:-20%;" class="tex" alt="\sqrt{-1}" /> is <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_5477186a84cc2c889974ca6fd01ca96f.png" title="i" style="vertical-align:-20%;" class="tex" alt="i" /> and go from there.  We also used the idea that <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_717a284e85ef0210c35106ec3b2e54f2.png" title="\sqrt{a\cdot b} = \sqrt{a}\cdot\sqrt{b}" style="vertical-align:-20%;" class="tex" alt="\sqrt{a\cdot b} = \sqrt{a}\cdot\sqrt{b}" /> to find square roots of all negative numbers.  And, we already played around a little bit with multiplying some numbers together.  Let&#8217;s take a step back for a moment though and just add.</p>
<p>We still want the rest of our algebra to work.  Because of that, we don&#8217;t have much choice for how imaginary numbers add together.  If we take any number <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_645f43d7c6169f257cafd2dc973d2421.png" title="z" style="vertical-align:-20%;" class="tex" alt="z" /> and add it to itself, we get <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_a6d66ac4d067413ca6c058d650c05924.png" title="z + z = (1 + 1) z = 2z" style="vertical-align:-20%;" class="tex" alt="z + z = (1 + 1) z = 2z" />.  We still want that to be true when <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_f8be652f4325571a12f2fee7e1bd26e1.png" title="z = i" style="vertical-align:-20%;" class="tex" alt="z = i" /> or <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_01d34053141c3fd4aa259aff5cdc1ddd.png" title="z = 5i" style="vertical-align:-20%;" class="tex" alt="z = 5i" />.  In general, then, we will need <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_3fbe3cd4cbb4b299a185390b21fd0134.png" title="az + bz = (a + b)z" style="vertical-align:-20%;" class="tex" alt="az + bz = (a + b)z" /> for any numbers <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_3cad95c81df4676e1b14e93cbb0b18ae.png" title="a" style="vertical-align:-20%;" class="tex" alt="a" />, <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_97f4a1ba736119e3cd4de6fca35efd6b.png" title="b" style="vertical-align:-20%;" class="tex" alt="b" />, and <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_645f43d7c6169f257cafd2dc973d2421.png" title="z" style="vertical-align:-20%;" class="tex" alt="z" />.</p>
<p>Earlier, we multiplied <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_06532e1cb171ae73340f6b0a07a3e9c2.png" title="2i\cdot2i" style="vertical-align:-20%;" class="tex" alt="2i\cdot2i" />.  What if we add one to <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_5477186a84cc2c889974ca6fd01ca96f.png" title="i" style="vertical-align:-20%;" class="tex" alt="i" />?  Well, we can definitely write this as <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_7a08e143cc76d57c8c288d77d726e9a6.png" title="1 + i" style="vertical-align:-20%;" class="tex" alt="1 + i" /> just like we can add one to two by just writing <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_c58688802f846bfc1980bba12853b24e.png" title="1 + 2" style="vertical-align:-20%;" class="tex" alt="1 + 2" />.  In the latter case, we already have a name for <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_c58688802f846bfc1980bba12853b24e.png" title="1 + 2" style="vertical-align:-20%;" class="tex" alt="1 + 2" />.  We could instead write <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_1b73081e7f571e9a816296b5a78040a1.png" title="3" style="vertical-align:-20%;" class="tex" alt="3" />.</p>
<p>We don&#8217;t already have a name for <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_7a08e143cc76d57c8c288d77d726e9a6.png" title="1 + i" style="vertical-align:-20%;" class="tex" alt="1 + i" />.  How do we know?  Well, let us assume that <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_7a08e143cc76d57c8c288d77d726e9a6.png" title="1 + i" style="vertical-align:-20%;" class="tex" alt="1 + i" /> is some real number or some real number times <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_5477186a84cc2c889974ca6fd01ca96f.png" title="i" style="vertical-align:-20%;" class="tex" alt="i" />.  If it is some real number, we should get a positive real number when we square it.  If it is a real number times <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_5477186a84cc2c889974ca6fd01ca96f.png" title="i" style="vertical-align:-20%;" class="tex" alt="i" />, we should get a negative real number when we square it.  But, if we multiply <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_2ac514bbaf1dc57d3298033a3518a4f7.png" title="(1 + i) \cdot (1 + i)" style="vertical-align:-20%;" class="tex" alt="(1 + i) \cdot (1 + i)" /> (using the old <a href="http://www.mathwarehouse.com/algebra/polynomial/foil-method-binomials.php"><q><b>F</b>irsts, <b>O</b>uters, <b>I</b>nners, <b>L</b>asts</q> method</a>), we get <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_c75a8f6adbb4b3af2519723da888b12e.png" title="1 + i + i + i^2 = 1 + 2i -1 = 2i" style="vertical-align:-20%;" class="tex" alt="1 + i + i + i^2 = 1 + 2i -1 = 2i" /> which is not a positive real number or a negative real number.</p>
<p>As it happens, all of our complex numbers will have the form:  <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_d21097041b32be657b9b67a074ca1685.png" title="a + bi" style="vertical-align:-20%;" class="tex" alt="a + bi" /> for some real numbers <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_3cad95c81df4676e1b14e93cbb0b18ae.png" title="a" style="vertical-align:-20%;" class="tex" alt="a" /> and <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_97f4a1ba736119e3cd4de6fca35efd6b.png" title="b" style="vertical-align:-20%;" class="tex" alt="b" />.  When we go to add two numbers together, we just add the corresponding pieces:  <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_52c57cd71ce73cc7fe190068f21a2740.png" title="(a + bi) + (c + di) = (a + c) + (b + d)i" style="vertical-align:-20%;" class="tex" alt="(a + bi) + (c + di) = (a + c) + (b + d)i" />.</p>
<p>For a complex number <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_d21097041b32be657b9b67a074ca1685.png" title="a + bi" style="vertical-align:-20%;" class="tex" alt="a + bi" />, we call <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_3cad95c81df4676e1b14e93cbb0b18ae.png" title="a" style="vertical-align:-20%;" class="tex" alt="a" /> the <q>real part</q> and <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_97f4a1ba736119e3cd4de6fca35efd6b.png" title="b" style="vertical-align:-20%;" class="tex" alt="b" /> the <q>imaginary part</q>.  When we add two complex numbers, we add the real parts together and we add the imaginary parts together.  Subtraction, likewise, goes by part.  If we want <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_76f1d667d4ef8ce89e277ccf434c5405.png" title="(a + bi) - (c + di)" style="vertical-align:-20%;" class="tex" alt="(a + bi) - (c + di)" />, we subtract the real parts and subtract the imaginary parts to obtain <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_39a47965ed260d8c5e3b752f99c23845.png" title="(a - c) + (b - d)i" style="vertical-align:-20%;" class="tex" alt="(a - c) + (b - d)i" />.</p>
<p>When we multiply two numbers together, we do it like we did with <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_cc088c202a9485ae8918eb220e951297.png" title="(1 + i)^2" style="vertical-align:-20%;" class="tex" alt="(1 + i)^2" /> above with the <abbr title="Firsts Outers Inners Lasts">FOIL</a> method.  <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_eb648edb4c17d7a96893f4aadccf60b2.png" title="(a + bi) \cdot (c + di) = ac + adi + bic + bidi" style="vertical-align:-20%;" class="tex" alt="(a + bi) \cdot (c + di) = ac + adi + bic + bidi" />.  We rearrange the orders of some of the bits as we did with <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_06532e1cb171ae73340f6b0a07a3e9c2.png" title="2i\cdot2i" style="vertical-align:-20%;" class="tex" alt="2i\cdot2i" /> above to get: <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_53a022afc07da91c056863a3e4497444.png" title="ac + adi + bci + bdi^2" style="vertical-align:-20%;" class="tex" alt="ac + adi + bci + bdi^2" />.  And since <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_2effb75364932f653b3033ef36028f13.png" title="i^2 = -1" style="vertical-align:-20%;" class="tex" alt="i^2 = -1" />, we have:<br />
<center><img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_2f2599c74e26e719f7903916da0d8c11.png" title="ac + adi + bci - bd = (ac - bd) + (ad + bc)i" style="vertical-align:-20%;" class="tex" alt="ac + adi + bci - bd = (ac - bd) + (ad + bc)i" /></center></p>
<h3>Transforming the Plane</h3>
<p>You&#8217;ll notice above that a complex number is made up of two real numbers (and, of course, <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_5477186a84cc2c889974ca6fd01ca96f.png" title="i" style="vertical-align:-20%;" class="tex" alt="i" />).  Every point in the plane has two real numbers as coordinates.  We can co-opt the real numbers in the complex number to use as coordinates for points in the plane.  If our points are: <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_df6a991ade7cc9ad3cd908b4e2ef9946.png" title="(x_1,y_1)" style="vertical-align:-20%;" class="tex" alt="(x_1,y_1)" />, <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_c1e27f0abd9ad9572f3887ef067770ab.png" title="(x_2,y_2)" style="vertical-align:-20%;" class="tex" alt="(x_2,y_2)" />, <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_4b30e31f5d5a5dbe4dcd2e370ee5d1ae.png" title="\ldots" style="vertical-align:-20%;" class="tex" alt="\ldots" />, <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_dd0bee170fcdd06706728b42cc47c899.png" title="(x_k,y_k)" style="vertical-align:-20%;" class="tex" alt="(x_k,y_k)" />, then we can represent them as complex numbers with <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_28ae3644980623b0f7a77c898670cdc3.png" title="z_1 = x_1 + y_1i" style="vertical-align:-20%;" class="tex" alt="z_1 = x_1 + y_1i" />, <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_08b436a2e08cd8bc08583466ab138831.png" title="z_2 = x_2 + y_2i" style="vertical-align:-20%;" class="tex" alt="z_2 = x_2 + y_2i" />, <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_4b30e31f5d5a5dbe4dcd2e370ee5d1ae.png" title="\ldots" style="vertical-align:-20%;" class="tex" alt="\ldots" />, <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_6d26aa7ee074b77157e823923c9236f0.png" title="z_k = x_k + y_ki" style="vertical-align:-20%;" class="tex" alt="z_k = x_k + y_ki" />.</p>
<p>We can plot these just as we would their normal coordinates if we put the real part on the x-axis and the imaginary part on the y-axis.  This is called an <a href="http://mathworld.wolfram.com/ArgandDiagram.html">Argand Diagram</a>.</p>
<p>Suppose now we want to translate all of our points by five units along the x-axis and negative two units along the y-axis.  We can simply let <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_d25d3e8d44f6c1eb6ada047a85f37ee6.png" title="t = 5 - 2i" style="vertical-align:-20%;" class="tex" alt="t = 5 - 2i" /> and then add this to each of our points so that <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_483aad77b980aa545aa0a1a03969b86d.png" title="z_j^\prime = z_j + t" style="vertical-align:-20%;" class="tex" alt="z_j^\prime = z_j + t" />.</p>
<p>Imagine instead that we want to scale the plane radially out from the origin by a factor of three.  We can simply multiply each of our points by <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_1d0af52a3cdde2fdbc455453f3812e5d.png" title="s = 3 + 0i" style="vertical-align:-20%;" class="tex" alt="s = 3 + 0i" /> so that <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_9a1111f05648bd17c3c50048d78f3abd.png" title="z_j^\prime = s \cdot z_j" style="vertical-align:-20%;" class="tex" alt="z_j^\prime = s \cdot z_j" />.  If we want to scale outward from <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_56cf2f11c08438ff33e442080a1ca952.png" title="(5,2)" style="vertical-align:-20%;" class="tex" alt="(5,2)" /> instead, we could translate by <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_873cb8d5053bdc96c2a08d726284ec4e.png" title="-t" style="vertical-align:-20%;" class="tex" alt="-t" />, scale by <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_5a4f530002151e61f4239aee0d82ad4a.png" title="s" style="vertical-align:-20%;" class="tex" alt="s" />, and then translate <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_4e156c4dfd6f5bd0adffc493c64bc7ca.png" title="t" style="vertical-align:-20%;" class="tex" alt="t" /> back.<br />
<center><img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_66656e80e1d57fbc78e0522339f0cdf8.png" title="z_j^\prime = s \cdot \left( z_j - t \right) + t = s \cdot z_j + (1 - s) \cdot t" style="vertical-align:-20%;" class="tex" alt="z_j^\prime = s \cdot \left( z_j - t \right) + t = s \cdot z_j + (1 - s) \cdot t" /></center></p>
<p>Suppose now that we&#8217;d like to rotate the plane by some angle <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_0004c76178a1d078888badee6891a8bd.png" title="\theta" style="vertical-align:-20%;" class="tex" alt="\theta" /> around the origin.  For that rotation, a point <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_1a4d392f8222c83b05eab4d12436f9ee.png" title="(x,y)" style="vertical-align:-20%;" class="tex" alt="(x,y)" /> should get rotated to the point <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_96e28a31d467902868cea0d38c9c319c.png" title="(x\cos\theta - y\sin\theta, x\sin\theta + y\cos\theta)" style="vertical-align:-20%;" class="tex" alt="(x\cos\theta - y\sin\theta, x\sin\theta + y\cos\theta)" />.  That is to say: the complex number <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_8b2a793cf0347bb25643542086c7fb58.png" title="x + yi" style="vertical-align:-20%;" class="tex" alt="x + yi" /> should get rotated to the complex number <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_54eb3f207cded71246b555ea4e6c09f3.png" title="(x\cos\theta - y\sin\theta) + (x\sin\theta + y\cos\theta)i" style="vertical-align:-20%;" class="tex" alt="(x\cos\theta - y\sin\theta) + (x\sin\theta + y\cos\theta)i" />.</p>
<p>Let&#8217;s look back to our equation for multiplying two complex numbers:<br />
<center><img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_b18d23815ab4cfee8f573603f73ef3c4.png" title="(a + bi) \cdot (c + di) = (ac - bd) + (ad + bc)i" style="vertical-align:-20%;" class="tex" alt="(a + bi) \cdot (c + di) = (ac - bd) + (ad + bc)i" /></center></p>
<p>We can see the similarity to the rotation.  If we take <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_5dd2cebbfc1f4ad422c9a3950cf267f8.png" title="(x + yi) \cdot (\cos\theta + i \sin\theta)" style="vertical-align:-20%;" class="tex" alt="(x + yi) \cdot (\cos\theta + i \sin\theta)" />, then we get precisely: <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_54eb3f207cded71246b555ea4e6c09f3.png" title="(x\cos\theta - y\sin\theta) + (x\sin\theta + y\cos\theta)i" style="vertical-align:-20%;" class="tex" alt="(x\cos\theta - y\sin\theta) + (x\sin\theta + y\cos\theta)i" />.</p>
<p>The points <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_ab5edd64e6aa0048b9551f1c5f4bf0e6.png" title="(\cos\theta,\sin\theta)" style="vertical-align:-20%;" class="tex" alt="(\cos\theta,\sin\theta)" /> are the points on the unit circle centered at the origin.  So, if we want to rotate all of our points by an angle <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_0004c76178a1d078888badee6891a8bd.png" title="\theta" style="vertical-align:-20%;" class="tex" alt="\theta" /> around the origin, we simply have to multiply them by the point on the unit circle that is at angle <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_0004c76178a1d078888badee6891a8bd.png" title="\theta" style="vertical-align:-20%;" class="tex" alt="\theta" /> around the origin from the x-axis:  <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_70c534a01e04cbae9705581c1eeb099a.png" title="r = \cos\theta + i \sin\theta" style="vertical-align:-20%;" class="tex" alt="r = \cos\theta + i \sin\theta" /> so that <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_b4cc18a857ea59ba3fafcec520137551.png" title="z_j^\prime = z_j \cdot r" style="vertical-align:-20%;" class="tex" alt="z_j^\prime = z_j \cdot r" />.  Again, if we want to rotate around the point <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_4e156c4dfd6f5bd0adffc493c64bc7ca.png" title="t" style="vertical-align:-20%;" class="tex" alt="t" />, we simply translate by <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_873cb8d5053bdc96c2a08d726284ec4e.png" title="-t" style="vertical-align:-20%;" class="tex" alt="-t" />, rotate by <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_de3adc820dbc4655c45b5555765fe84b.png" title="r" style="vertical-align:-20%;" class="tex" alt="r" />, and translate <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_4e156c4dfd6f5bd0adffc493c64bc7ca.png" title="t" style="vertical-align:-20%;" class="tex" alt="t" /> back again.<br />
<center><img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_5b30fcc567f5db71079946447fe4b680.png" title="z_j^\prime = r \cdot \left( z_j - t \right) + t = r \cdot z_j + (1 - r) \cdot t" style="vertical-align:-20%;" class="tex" alt="z_j^\prime = r \cdot \left( z_j - t \right) + t = r \cdot z_j + (1 - r) \cdot t" /></center></p>
<p>So, now we can translate points in the plane with complex addition (or subtraction).  We can scale points in the plane by multiplying by a real number.  We can rotate the plane by multiplying by a complex number.</p>
<h3>Conformal maps</h3>
<p>There are some other transformations that naturally arise from complex arithmetic.  A <a href="http://en.wikipedia.org/wiki/Conformal_map">conformal transform</a> is one that keeps angles constant.  All of the transformations we&#8217;ve done above are conformal.  If you translate the whole plane, the angles between lines are unchanged.  If you scale the whole plane, the angles between lines are unchanged.  If you rotate the whole plane, the angles between lines are unchanged.</p>
<p>As it happens, a transformation of the complex plane is a conformal map if and only if the transformation has a (complex) derivative everywhere and that derivative is non-zero everywhere.  Without getting into complex derivatives here, suffice it to say, they&#8217;re pretty much just like real derivatives for simple polynomials.  Let&#8217;s look at our cases above.  The derivative of translating by <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_4e156c4dfd6f5bd0adffc493c64bc7ca.png" title="t" style="vertical-align:-20%;" class="tex" alt="t" /> is <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_e04a0c4d52e7f8537abf80bb9beb4a8a.png" title="1" style="vertical-align:-20%;" class="tex" alt="1" /> which exists everywhere and is non-zero everywhere.  The derivative of multiplying by <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_5a4f530002151e61f4239aee0d82ad4a.png" title="s" style="vertical-align:-20%;" class="tex" alt="s" /> (or <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_de3adc820dbc4655c45b5555765fe84b.png" title="r" style="vertical-align:-20%;" class="tex" alt="r" />) is <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_5a4f530002151e61f4239aee0d82ad4a.png" title="s" style="vertical-align:-20%;" class="tex" alt="s" /> (or <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_de3adc820dbc4655c45b5555765fe84b.png" title="r" style="vertical-align:-20%;" class="tex" alt="r" />).  This exists everywhere and is non-zero everywhere (if anywhere).</p>
<p>Another conformal transformation of the complex plane is the <a href="http://en.wikipedia.org/wiki/Möbius_transformation">Möbius transformation</a>.  The Möbius transformations preserve angles where lines meet, but they generally turn lines into curves.  The Möbius transformations are of the form:  <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_7ff1d71a337997aab0ae6bd075f53992.png" title="z_j^\prime = \frac{a z_j + b}{c z_j + d}" style="vertical-align:-20%;" class="tex" alt="z_j^\prime = \frac{a z_j + b}{c z_j + d}" /> for complex numbers <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_3cad95c81df4676e1b14e93cbb0b18ae.png" title="a" style="vertical-align:-20%;" class="tex" alt="a" />, <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_97f4a1ba736119e3cd4de6fca35efd6b.png" title="b" style="vertical-align:-20%;" class="tex" alt="b" />, <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_08c1898a88651cdd0a9fdb2d6ce6e8a1.png" title="c" style="vertical-align:-20%;" class="tex" alt="c" />, and <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_fa25a1c273232af978e96f1ea4331d79.png" title="d" style="vertical-align:-20%;" class="tex" alt="d" /> (so long as <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_35360b379fbc4947d129676e9be98d6b.png" title="ad - bc \neq 0" style="vertical-align:-20%;" class="tex" alt="ad - bc \neq 0" />).  You can see the <a href="http://www.youtube.com/watch?v=JX3VmDgiFnY">Möbius transform in action</a> on YouTube.</p>
<h3>What&#8217;s next</h3>
<p>Next, we&#8217;re going to see how Clifford algebras can represent all we have done here.  But, that is for another day.</p>
]]></content:encoded>
			<wfw:commentRss>http://nklein.com/2009/06/complex-numbers-for-rotating-translating-and-scaling-the-plane/feed/</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
		<item>
		<title>Trying to Short-Stop Iterated Functions</title>
		<link>http://nklein.com/2009/05/trying-to-short-stop-iterated-functions/</link>
		<comments>http://nklein.com/2009/05/trying-to-short-stop-iterated-functions/#comments</comments>
		<pubDate>Fri, 15 May 2009 19:35:52 +0000</pubDate>
		<dc:creator>pat</dc:creator>
				<category><![CDATA[Articles]]></category>
		<category><![CDATA[Math]]></category>
		<category><![CDATA[iterated functions]]></category>
		<category><![CDATA[polynomials]]></category>

		<guid isPermaLink="false">http://nklein.com/?p=371</guid>
		<description><![CDATA[If a function is pretty fun, why not do it again? My post yesterday about the Mandelbrot set got me thinking again about iterated functions. With the Mandelbrot set, you start with some complex number and with . Then, you generate by doing . Here&#8217;s another way to write this iteration. Let . Let and [...]]]></description>
			<content:encoded><![CDATA[<p>If a function is pretty fun, why not do it again?</p>
<p>My post yesterday about the <a href="http://nklein.com/2009/05/a-different-look-at-the-mandelbrot-set/">Mandelbrot set</a> got me thinking again about iterated functions.  With the Mandelbrot set, you start with some complex number <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_08c1898a88651cdd0a9fdb2d6ce6e8a1.png" title="c" style="vertical-align:-20%;" class="tex" alt="c" /> and with <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_2dd78955205110e1fc89bfa4c11128d4.png" title="z_0 = 0" style="vertical-align:-20%;" class="tex" alt="z_0 = 0" />.  Then, you generate <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_e9d1f85e631936eb7ccb91ec0a5f673e.png" title="z_{i+1}" style="vertical-align:-20%;" class="tex" alt="z_{i+1}" /> by doing <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_f2af523186e33db5aa0147dbd79395bf.png" title="z_{i+1} = z_i^2 + c" style="vertical-align:-20%;" class="tex" alt="z_{i+1} = z_i^2 + c" />.</p>
<p>Here&#8217;s another way to write this iteration.  Let <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_8381410f17a67ffe2eefbdff8f51e793.png" title="f_c(z) = z^2 + c" style="vertical-align:-20%;" class="tex" alt="f_c(z) = z^2 + c" />.  Let <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_2dd78955205110e1fc89bfa4c11128d4.png" title="z_0 = 0" style="vertical-align:-20%;" class="tex" alt="z_0 = 0" /> and <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_ddb7ad59eb420c4926280b8bfc6af644.png" title="z_1 = f_c(z_0)" style="vertical-align:-20%;" class="tex" alt="z_1 = f_c(z_0)" /> and <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_130b4ddc2627ab63edc2bda5464519de.png" title="z_2 = f_c(z_1) = f_c(f_c(z_0)) = f_c^2(z_0)" style="vertical-align:-20%;" class="tex" alt="z_2 = f_c(z_1) = f_c(f_c(z_0)) = f_c^2(z_0)" />.  In general, we write <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_dfa3cd924b7a3e6dc83a3f8322031bda.png" title="f_c^n(z)" style="vertical-align:-20%;" class="tex" alt="f_c^n(z)" /> to mean <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_95e6e471c3e067587fe977e6def15821.png" title="f_c(f_c(\ldots f_c(z)))" style="vertical-align:-20%;" class="tex" alt="f_c(f_c(\ldots f_c(z)))" /> when there are <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_18a633fc025e2f1a862f7c8de3b31d6c.png" title="n" style="vertical-align:-20%;" class="tex" alt="n" /> copies of <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_522169daf9e992fcada6b792cb46f5ca.png" title="f_c" style="vertical-align:-20%;" class="tex" alt="f_c" />.  It is easy to verify then that <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_05eb570149d56854bf5d6e49df4f3669.png" title="f_c^{a}(f_c^{b}(z)) = f_c^{a+b}(x)" style="vertical-align:-20%;" class="tex" alt="f_c^{a}(f_c^{b}(z)) = f_c^{a+b}(x)" />.</p>
<h3>The Jam</h3>
<p><a href="http://nklein.com/wp-content/uploads/2009/05/fibers.png"><img src="http://nklein.com/wp-content/uploads/2009/05/fibers-150x117.png" alt="fibers" title="fibers" width="150" height="117" class="alignright size-thumbnail wp-image-368" /></a>  For that post, I generated the image linked at the right here.  Each level of that image represents one iteration of <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_763fab4c8dbbfb53d16551e38b7fe975.png" title="f_c^{i+1}(0) = f_c(f_c^i(0))" style="vertical-align:-20%;" class="tex" alt="f_c^{i+1}(0) = f_c(f_c^i(0))" /> with each strand made up of four different <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_522169daf9e992fcada6b792cb46f5ca.png" title="f_c" style="vertical-align:-20%;" class="tex" alt="f_c" /> functions with neighboring <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_08c1898a88651cdd0a9fdb2d6ce6e8a1.png" title="c" style="vertical-align:-20%;" class="tex" alt="c" />&#8216;s.</p>
<p>One of the annoying things about that image is that I went directly from one iteration to the next.  I had pondered using  conventional splines or other polynomials to interpolate between iterations in an effort to smooth out the transitions.  I didn&#8217;t go to the trouble for it though because any straightforward interpolation would be every bit as fake as the linear version.  There is no provision in the iteration for <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_63fa348bde3d4eaac8ab20246dcb9974.png" title="f_c^{\alpha}" style="vertical-align:-20%;" class="tex" alt="f_c^{\alpha}" /> unless <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_a7175a60e504c64ad51e7119b44ee9aa.png" title="\alpha" style="vertical-align:-20%;" class="tex" alt="\alpha" /> is a non-negative integer.</p>
<p>Now the question is, can we fake one?  Can we make some other function <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_0253c4de63d13a4c068b93ed7be6b4d7.png" title="g_c(z)" style="vertical-align:-20%;" class="tex" alt="g_c(z)" /> so that <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_d7888b2d976259f057779ef30a78629c.png" title="g_c^2(z) = f_c(z)" style="vertical-align:-20%;" class="tex" alt="g_c^2(z) = f_c(z)" />?</p>
<h3>Faking easy functions</h3>
<p>Let&#8217;s start with an easier function.  In fact, let&#8217;s start with the easiest function:  <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_ecbf87130a26c0d457f5a40ebf0bcc96.png" title="f(x) = 0" style="vertical-align:-20%;" class="tex" alt="f(x) = 0" />.  It is obvious that <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_cd2adc5c9c29ccbbce27f95b923ee628.png" title="f^n(x) = f(x)" style="vertical-align:-20%;" class="tex" alt="f^n(x) = f(x)" /> for all positive integers <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_18a633fc025e2f1a862f7c8de3b31d6c.png" title="n" style="vertical-align:-20%;" class="tex" alt="n" />.  As such, if we let <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_58dee688e399d21fa730723d8c2b4a37.png" title="g(x) = 0" style="vertical-align:-20%;" class="tex" alt="g(x) = 0" />, it is trivially true that <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_4014ea242dd9a90978a4a69c1efcf8f5.png" title="g^2(x) = f(x)" style="vertical-align:-20%;" class="tex" alt="g^2(x) = f(x)" />.  But, that was too easy.</p>
<p>Maybe <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_9f0ceb8ffbb5aee48ee79c9e9b610530.png" title="f(x) = x" style="vertical-align:-20%;" class="tex" alt="f(x) = x" /> will be harder?  No.  Again, <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_a9a011d9a7703cc1c3215c63d073a739.png" title="f^2(x) = f(f(x)) = f(x)" style="vertical-align:-20%;" class="tex" alt="f^2(x) = f(f(x)) = f(x)" />.</p>
<h3>Faking translations</h3>
<p>Let&#8217;s move up to something a little trickier.  Let&#8217;s say that <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_6f1085e342bef9b91ac631f9d8c2f496.png" title="f(x) = x + c" style="vertical-align:-20%;" class="tex" alt="f(x) = x + c" /> for some constant <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_08c1898a88651cdd0a9fdb2d6ce6e8a1.png" title="c" style="vertical-align:-20%;" class="tex" alt="c" />.  Now, we see that <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_216ed91f39bf9cbca104a54ad8f39d16.png" title="f^2(x) = f(f(x)) = f(x + c) = (x + c) + c = x + 2c" style="vertical-align:-20%;" class="tex" alt="f^2(x) = f(f(x)) = f(x + c) = (x + c) + c = x + 2c" />.  What are we going to try for <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_23341f0f402724ab87198ba1fcc3c378.png" title="g(x)" style="vertical-align:-20%;" class="tex" alt="g(x)" /> then?  Let&#8217;s try the obvious:  <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_7a59ec19ef75401fe10d66cace0e5476.png" title="g(x) = x + \frac{c}{2}" style="vertical-align:-20%;" class="tex" alt="g(x) = x + \frac{c}{2}" />.  Hooray!  <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_4014ea242dd9a90978a4a69c1efcf8f5.png" title="g^2(x) = f(x)" style="vertical-align:-20%;" class="tex" alt="g^2(x) = f(x)" />.  So, it makes sense then to think of <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_e79b0d102a7da565f62477423cf96313.png" title="f^{1/2}(x)" style="vertical-align:-20%;" class="tex" alt="f^{1/2}(x)" /> as <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_24f8645dd985bbfbdbdda4351d471a0b.png" title="x + \frac{c}{2}" style="vertical-align:-20%;" class="tex" alt="x + \frac{c}{2}" />.  Similarly, we can think of <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_547d0a9d34f227b2a8bfe42036744fe3.png" title="f^{1/n}(x)" style="vertical-align:-20%;" class="tex" alt="f^{1/n}(x)" /> as <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_fdf944877eaa94430eb421c26394a872.png" title="x + \frac{c}{n}" style="vertical-align:-20%;" class="tex" alt="x + \frac{c}{n}" /> for all positive integers <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_18a633fc025e2f1a862f7c8de3b31d6c.png" title="n" style="vertical-align:-20%;" class="tex" alt="n" />.  In this way, we can make values <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_d5f8dbaa4439daffe111969d609994cb.png" title="f^q(x)" style="vertical-align:-20%;" class="tex" alt="f^q(x)" /> for any positive rational number <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_ad82fe4a03707ce6f42cf72f2ae029be.png" title="q" style="vertical-align:-20%;" class="tex" alt="q" />.</p>
<h3>Faking translations with scaling</h3>
<p>Now, we&#8217;re cooking.  Let&#8217;s up the ante.  Let&#8217;s try <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_051ddeb7f3a335c1357a3dcfdf34e1df.png" title="f(x) = ax + b" style="vertical-align:-20%;" class="tex" alt="f(x) = ax + b" />.  What are we going to try for <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_23341f0f402724ab87198ba1fcc3c378.png" title="g(x)" style="vertical-align:-20%;" class="tex" alt="g(x)" /> then?  Let&#8217;s guess we can still use roughly the same form:  <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_1732e81335196033a98bcea2d4b28ea8.png" title="g(x) = \alpha x + \beta" style="vertical-align:-20%;" class="tex" alt="g(x) = \alpha x + \beta" />.  Then, <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_e8bf2688dc0453aabc2c0fea0a3d04c3.png" title="g^2(x) = \alpha^2 x + \alpha \beta + \beta" style="vertical-align:-20%;" class="tex" alt="g^2(x) = \alpha^2 x + \alpha \beta + \beta" />.  If we want <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_4014ea242dd9a90978a4a69c1efcf8f5.png" title="g^2(x) = f(x)" style="vertical-align:-20%;" class="tex" alt="g^2(x) = f(x)" />, then we need <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_0ff5491cf117eabd680e61a50678abc9.png" title="\alpha^2 = a" style="vertical-align:-20%;" class="tex" alt="\alpha^2 = a" /> and <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_bd678273ca2b5b885519c0b6a0aa195b.png" title="(\alpha + 1)\beta = b" style="vertical-align:-20%;" class="tex" alt="(\alpha + 1)\beta = b" />.  This means that <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_38ad382722ab100869ab494e946df2bc.png" title="\alpha = \sqrt{a}" style="vertical-align:-20%;" class="tex" alt="\alpha = \sqrt{a}" /> and <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_fad14bc506d46661e1c7adab0212102b.png" title="\beta = \frac{b}{1 + \sqrt{a}}" style="vertical-align:-20%;" class="tex" alt="\beta = \frac{b}{1 + \sqrt{a}}" />.  In particular, notice that if <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_3cad95c81df4676e1b14e93cbb0b18ae.png" title="a" style="vertical-align:-20%;" class="tex" alt="a" /> and <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_97f4a1ba736119e3cd4de6fca35efd6b.png" title="b" style="vertical-align:-20%;" class="tex" alt="b" /> are both real numbers with <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_3cad95c81df4676e1b14e93cbb0b18ae.png" title="a" style="vertical-align:-20%;" class="tex" alt="a" /> less than zero, then <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_a7175a60e504c64ad51e7119b44ee9aa.png" title="\alpha" style="vertical-align:-20%;" class="tex" alt="\alpha" /> is pure imaginary and <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_ed45436c4512976c4600afab449577d8.png" title="\beta" style="vertical-align:-20%;" class="tex" alt="\beta" /> is complex.</p>
<h3>TTFN</h3>
<p>I have much more to say about iterated functions, but I will save some for the next iteration.  Next time, I will start with that last case and calculate <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_d5f8dbaa4439daffe111969d609994cb.png" title="f^q(x)" style="vertical-align:-20%;" class="tex" alt="f^q(x)" /> for rational <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_ad82fe4a03707ce6f42cf72f2ae029be.png" title="q" style="vertical-align:-20%;" class="tex" alt="q" />.</p>
]]></content:encoded>
			<wfw:commentRss>http://nklein.com/2009/05/trying-to-short-stop-iterated-functions/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Finding Better Polynomials</title>
		<link>http://nklein.com/2009/05/finding-better-polynomials/</link>
		<comments>http://nklein.com/2009/05/finding-better-polynomials/#comments</comments>
		<pubDate>Tue, 12 May 2009 18:03:56 +0000</pubDate>
		<dc:creator>pat</dc:creator>
				<category><![CDATA[Articles]]></category>
		<category><![CDATA[Math]]></category>
		<category><![CDATA[domain-specific languages]]></category>
		<category><![CDATA[lisp]]></category>
		<category><![CDATA[polynomials]]></category>

		<guid isPermaLink="false">http://nklein.com/?p=354</guid>
		<description><![CDATA[Some time ago, I wrote a small domain-specific language for finding polynomials given their value or the value of their derivatives at particular points. It occurred to me shortly after writing that code that I could easily extend it to include the value of its integral over a certain range. I didn&#8217;t get to tackling [...]]]></description>
			<content:encoded><![CDATA[<p>Some time ago, I wrote a small domain-specific language for <a href="http://nklein.com/2009/04/find-the-polynomial-youve-been-looking-for/">finding polynomials</a> given their value or the value of their derivatives at particular points.</p>
<p>It occurred to me shortly after writing that code that I could easily extend it to include the value of its integral over a certain range.  I didn&#8217;t get to tackling that right away, and that was a Good Thing.  In the intervening time, it occurred to me that I could extend it to <a href="http://en.wikipedia.org/wiki/Moment_(mathematics)">moments</a> as well.</p>
<p>So, are you looking for a polynomial that is zero at both zero and one, has a derivative of zero at one, has a second derivative of negative one at one, whose integral from zero to one is one, and whose mean (first moment centered at zero) on the interval zero to one is one fourth?</p>
<div class="codecolorer-container lisp blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><pre class="lisp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #66cc66;">&#40;</span>polynomial-to-string
  <span style="color: #66cc66;">&#40;</span>calculate-polynomial-subject-to
    <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">value</span> <span style="color: #66cc66;">:</span><span style="color: #555;">at</span> <span style="color: #cc66cc;">0</span> <span style="color: #66cc66;">:</span><span style="color: #555;">equals</span> <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">value</span> <span style="color: #66cc66;">:</span><span style="color: #555;">at</span> <span style="color: #cc66cc;">1</span> <span style="color: #66cc66;">:</span><span style="color: #555;">equals</span> <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#40;</span>derivative <span style="color: #66cc66;">:</span><span style="color: #555;">at</span> <span style="color: #cc66cc;">1</span> <span style="color: #66cc66;">:</span><span style="color: #555;">equals</span> <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#40;</span>nth-derivative <span style="color: #cc66cc;">2</span> <span style="color: #66cc66;">:</span><span style="color: #555;">at</span> <span style="color: #cc66cc;">1</span> <span style="color: #66cc66;">:</span><span style="color: #555;">equals</span> -<span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#40;</span>integral <span style="color: #66cc66;">:</span><span style="color: #555;">from</span> <span style="color: #cc66cc;">0</span> <span style="color: #66cc66;">:</span><span style="color: #555;">to</span> <span style="color: #cc66cc;">1</span> <span style="color: #66cc66;">:</span><span style="color: #555;">equals</span> <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#40;</span>mean <span style="color: #66cc66;">:</span><span style="color: #555;">from</span> <span style="color: #cc66cc;">0</span> <span style="color: #66cc66;">:</span><span style="color: #555;">to</span> <span style="color: #cc66cc;">1</span> <span style="color: #66cc66;">:</span><span style="color: #555;">equals</span> <span style="color: #cc66cc;">1</span>/<span style="color: #cc66cc;">4</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></pre></div>
<p>Well, that would be <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_c0126f8e80cc81bd9fe1d70b37272a64.png" title="f(x) = \frac{149}{4}x - 163x^2 + 265x^3 - 190x^4 + \frac{203}{4}x^5" style="vertical-align:-20%;" class="tex" alt="f(x) = \frac{149}{4}x - 163x^2 + 265x^3 - 190x^4 + \frac{203}{4}x^5" />.  (For some reason though, gnuplot thinks <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_3a0a2f2a9686309735f2f0213c2faef3.png" title="f(x) = -1" style="vertical-align:-20%;" class="tex" alt="f(x) = -1" />, so no graph for you&#8230;)</p>
<p>Here is the <a href='http://nklein.com/wp-content/uploads/2009/05/polynomials.lisp'>source file</a>.</p>
<p><a href="http://nklein.com/wp-content/uploads/2009/05/graph.png"><img src="http://nklein.com/wp-content/uploads/2009/05/graph-300x225.png" alt="graph" title="graph" width="300" height="225" class="alignright size-medium wp-image-363" /></a><br />
<strong>Edit:</strong> I realized that gnuplot was off by one because it was truncating the fractions.  So, I just changed the 4&#8242;s in the denominators to 4.0&#8242;s and Bob&#8217;s your uncle.</p>
<p class="clear">&nbsp;</p>
]]></content:encoded>
			<wfw:commentRss>http://nklein.com/2009/05/finding-better-polynomials/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Phony Physics (a.k.a. Fun with Interpolation)</title>
		<link>http://nklein.com/2009/04/phony-physics-aka-fun-with-interpolation/</link>
		<comments>http://nklein.com/2009/04/phony-physics-aka-fun-with-interpolation/#comments</comments>
		<pubDate>Fri, 03 Apr 2009 15:23:07 +0000</pubDate>
		<dc:creator>pat</dc:creator>
				<category><![CDATA[Articles]]></category>
		<category><![CDATA[Math]]></category>
		<category><![CDATA[game design]]></category>
		<category><![CDATA[polynomials]]></category>
		<category><![CDATA[spell-it]]></category>

		<guid isPermaLink="false">http://nklein.com/?p=187</guid>
		<description><![CDATA[In a previous post, I mentioned looking for a polynomial for an application. I am working on an application that involves clicking or dragging tiles around. Once you release the tile, I want it to snap to where it&#8217;s supposed to be. The easiest way to do this is to just warp it to where [...]]]></description>
			<content:encoded><![CDATA[<p>In a <a href="http://nklein.com/2009/04/find-the-polynomial-youve-been-looking-for/">previous post</a>, I mentioned looking for a polynomial for an application.  I am working on an application that involves clicking or dragging tiles around. Once you release the tile, I want it to snap to where it&#8217;s supposed to be.</p>
<p>The easiest way to do this is to just warp it to where it&#8217;s supposed to go.  This isn&#8217;t very visually pleasing.  The next best thing is to set up a time-interval on which it moves into place.  Then, linearly interpolate from where it is to where it&#8217;s going (here with <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_4e156c4dfd6f5bd0adffc493c64bc7ca.png" title="t" style="vertical-align:-20%;" class="tex" alt="t" /> normalized to range between zero and one):</p>
<p><center><img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_bc65e9f2a636dba26b596808b7606619.png" title="(1-t)x_0 + tx_1" style="vertical-align:-20%;" class="tex" alt="(1-t)x_0 + tx_1" /></center></p>
<p>That&#8217;s much better than just warping, but it doesn&#8217;t have any sort of fade-in or fade-out.  It instantaneously has its maximum velocity and then instantaneously stops at the end.</p>
<p>Typically, then one uses a cubic spline to scale the <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_4e156c4dfd6f5bd0adffc493c64bc7ca.png" title="t" style="vertical-align:-20%;" class="tex" alt="t" /> value before interpolating to get a speed up in the beginning and then slow down in the end.<br />
<span id="more-187"></span></p>
<p><center><a href="http://nklein.com/2009/04/phony-physics-aka-fun-with-interpolation/attachment/3/" rel="attachment wp-att-190"><img src="http://nklein.com/wp-content/uploads/2009/04/3.png" alt="interp-3" title="interp-3" width="362" height="332" class="size-full wp-image-190" /></a></center><br />
<center><img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_192613d43790f7f6d1a03b285815852a.png" title="t' = 3t^2 - 2t^3" style="vertical-align:-20%;" class="tex" alt="t' = 3t^2 - 2t^3" /></center></p>
<p>I might like to decelerate faster than I accelerate though so I might employ a fancier (higher-order) polynomial like:</p>
<p><center><a href="http://nklein.com/2009/04/phony-physics-aka-fun-with-interpolation/attachment/4/" rel="attachment wp-att-189"><img src="http://nklein.com/wp-content/uploads/2009/04/4.png" alt="interp-4" title="interp-4" width="362" height="332" class="size-full wp-image-189" /></a></center><br />
<center><img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_56e7a6a5cab5289a0fbe06302ed04c7b.png" title="t' = \frac{1}{2}t^2 + 6t^3 - \frac{17}{2}t^4 + 3x^5" style="vertical-align:-20%;" class="tex" alt="t' = \frac{1}{2}t^2 + 6t^3 - \frac{17}{2}t^4 + 3x^5" /></center></p>
<p>But, in this case, I wanted the tile to overshoot the target a bit and then nestle/wiggle into place.  So, I experimented with a bunch of different polynomials.  I was having real trouble getting them to behave in a way that felt right.  So, I went back to the physics of it.</p>
<p>What I wanted was pretty similar to damped spring motion.  Imagine you&#8217;ve tied a heavy weight to a spring and suspended it all in a jar of oil.  If you perturb the weight, it will bounce up and down but<br />
each bounce is closer to the equilibrium point than the previous bounce. (Actually, it&#8217;s more complicated than that.  If the spring is too weak or the oil too viscous, the weight will just slowly return to the equilibrium point without ever going past it.  That&#8217;s called <q>overdamped</q> harmonic oscillation.  What I want is called <q>underdamped</q> harmonic oscillation and that&#8217;s all I will concern myself with here.)  The equation for this motion is:</p>
<p><center><a href="http://nklein.com/2009/04/phony-physics-aka-fun-with-interpolation/attachment/1/" rel="attachment wp-att-188"><img src="http://nklein.com/wp-content/uploads/2009/04/1.png" alt="interp-1" title="interp-1" width="362" height="332" class="size-full wp-image-188" /></a></center><br />
<center><img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_183e283439724e2d53461f2d35e45c55.png" title="e^{-kt}\cos(\omega t + \phi)" style="vertical-align:-20%;" class="tex" alt="e^{-kt}\cos(\omega t + \phi)" /></center></p>
<p>To get it to wiggle an appropriate amount, I was using <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_09e765cf1309e741b7d63896b0e5f131.png" title="\omega = 4\pi" style="vertical-align:-20%;" class="tex" alt="\omega = 4\pi" /> and <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_0ecb976ae09df37ce3eb04c2e5476ee1.png" title="\phi = -\frac{\pi}{2}" style="vertical-align:-20%;" class="tex" alt="\phi = -\frac{\pi}{2}" /> effectively making the cosine into a sine.</p>
<p>This is all well and good, but to really scale my t, I need this to start at zero and approach one.  So, I was really using:</p>
<p><center><a href="http://nklein.com/2009/04/phony-physics-aka-fun-with-interpolation/attachment/2/" rel="attachment wp-att-193"><img src="http://nklein.com/wp-content/uploads/2009/04/2.png" alt="interp-2" title="interp-2" width="362" height="332" class="size-full wp-image-193" /></a></center><br />
<center><img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_c6b4c32d153a59887d18aa66851ee824.png" title="t' = t + e^{-kt}\sin(\omega t)" style="vertical-align:-20%;" class="tex" alt="t' = t + e^{-kt}\sin(\omega t)" /></center></p>
<p>Well, now it starts zig-zagging on the way to its destination.  It already starts cutting back long before it makes it to where it&#8217;s going.  I needed to get close to the destination much sooner.  So, I jiggered the <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_4e156c4dfd6f5bd0adffc493c64bc7ca.png" title="t" style="vertical-align:-20%;" class="tex" alt="t" /> term:</p>
<p><center><a href="http://nklein.com/2009/04/phony-physics-aka-fun-with-interpolation/attachment/8/" rel="attachment wp-att-194"><img src="http://nklein.com/wp-content/uploads/2009/04/8.png" alt="interp-8" title="interp-8" width="362" height="332" class="size-full wp-image-194" /></a></center><br />
<center><img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_ea300b9cde964c1f248728a4e8518ef3.png" title="t' = 1 - (1-t)^4 + e^{-kt}\sin(\omega t)" style="vertical-align:-20%;" class="tex" alt="t' = 1 - (1-t)^4 + e^{-kt}\sin(\omega t)" /></center></p>
<p>This looks terrible though.  I hit the destination is less than a tenth of a second and then not again until about half a second.  The reason for this is that the oscillations happen at a fixed rate.  <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_0824a1fdf135170467671326d5780e2b.png" title="\omega" style="vertical-align:-20%;" class="tex" alt="\omega" /> never changes and <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_4e156c4dfd6f5bd0adffc493c64bc7ca.png" title="t" style="vertical-align:-20%;" class="tex" alt="t" /> moves steadily along so that <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_0ce8d212a10d8619e00e43a97ce75527.png" title="\sin(\omega t)" style="vertical-align:-20%;" class="tex" alt="\sin(\omega t)" /> keeps the same frequency throughout my interval.  So, I jiggered the <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_4e156c4dfd6f5bd0adffc493c64bc7ca.png" title="t" style="vertical-align:-20%;" class="tex" alt="t" /> going into the sine function to make my oscillations start out slowly and really speed up toward the end.</p>
<p><center><a href="http://nklein.com/2009/04/phony-physics-aka-fun-with-interpolation/attachment/5/" rel="attachment wp-att-195"><img src="http://nklein.com/wp-content/uploads/2009/04/5.png" alt="interp-5" title="interp-5" width="362" height="332" class="aligncenter size-full wp-image-195" /></a></center><br />
<center><img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_d206dd1b8a9975dac7a20d2f485d302c.png" title="t' = 1 - (1-t)^4 + e^{-kt}\sin(\omega t^8)" style="vertical-align:-20%;" class="tex" alt="t' = 1 - (1-t)^4 + e^{-kt}\sin(\omega t^8)" /></center></p>
<p>This is much better, but it still didn&#8217;t give the feeling of settling in because the second oscillation was almost as high as the first.  I could up the value of <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_98d86ac06986d02b6e40f483093b61e0.png" title="k" style="vertical-align:-20%;" class="tex" alt="k" /> and get somewhere.  But, I wanted it to be even more dramatic.  So, I changed the <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_4e156c4dfd6f5bd0adffc493c64bc7ca.png" title="t" style="vertical-align:-20%;" class="tex" alt="t" /> in the exponent to <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_2dfd71f3d0d6caca184676440dd9f788.png" title="t^3" style="vertical-align:-20%;" class="tex" alt="t^3" /> (and used a slightly higher <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_98d86ac06986d02b6e40f483093b61e0.png" title="k" style="vertical-align:-20%;" class="tex" alt="k" />: was 3 and is now 4):</p>
<p><center><a href="http://nklein.com/2009/04/phony-physics-aka-fun-with-interpolation/attachment/6/" rel="attachment wp-att-196"><img src="http://nklein.com/wp-content/uploads/2009/04/6.png" alt="interp-6" title="interp-6" width="362" height="332" class="size-full wp-image-196" /></a></center><br />
<center><img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_ab2071d4a434e9ab5ec3479bb94847f3.png" title="t' = 1 - (1-t)^4 + e^{-kt^3}\sin(\omega t^8)" style="vertical-align:-20%;" class="tex" alt="t' = 1 - (1-t)^4 + e^{-kt^3}\sin(\omega t^8)" /></center></p>
<p>This was pretty decent, but I felt the initial attack was a bit strong.  So, I dropped down the exponent on my base curve from 4 to 2:</p>
<p><center><a href="http://nklein.com/2009/04/phony-physics-aka-fun-with-interpolation/attachment/7/" rel="attachment wp-att-197"><img src="http://nklein.com/wp-content/uploads/2009/04/7.png" alt="interp-7" title="interp-7" width="362" height="332" class="size-full wp-image-197" /></a></center><br />
<center><img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_91b53d4e338118fb129365a6a9871631.png" title="t' = 1 - (1-t)^2 + e^{-kt^3}\sin(\omega t^8)" style="vertical-align:-20%;" class="tex" alt="t' = 1 - (1-t)^2 + e^{-kt^3}\sin(\omega t^8)" /></center></p>
<p>This is pretty nice.  It looks pretty natural (despite making an entire mess out of damped harmonic motion).  It doesn&#8217;t ease in at all though so I might still need to replace the <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_2440a4af622544eadc0e1791b4221865.png" title="1 - (1-t)^2" style="vertical-align:-20%;" class="tex" alt="1 - (1-t)^2" /> with something of a higher order.  But, I might not mind the tile achieving instant velocity.  It gives it that <q>I&#8217;m really supposed to be over there</q> sort of urgency.</p>
]]></content:encoded>
			<wfw:commentRss>http://nklein.com/2009/04/phony-physics-aka-fun-with-interpolation/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Find the Polynomial You&#8217;ve Been Looking For</title>
		<link>http://nklein.com/2009/04/find-the-polynomial-youve-been-looking-for/</link>
		<comments>http://nklein.com/2009/04/find-the-polynomial-youve-been-looking-for/#comments</comments>
		<pubDate>Thu, 02 Apr 2009 23:09:00 +0000</pubDate>
		<dc:creator>pat</dc:creator>
				<category><![CDATA[Articles]]></category>
		<category><![CDATA[Math]]></category>
		<category><![CDATA[domain-specific languages]]></category>
		<category><![CDATA[lisp]]></category>
		<category><![CDATA[polynomials]]></category>

		<guid isPermaLink="false">http://nklein.com/?p=116</guid>
		<description><![CDATA[So, very often for things that I'm toying with, I want to have a polynomial that meets certain criteria.  For most applications in the past, I have sat down and figured it all out by hand.

Often, I want to have a polynomial that meets certain criteria.  For most applications in the past, I figured it all out by hand.

Today, I was having trouble getting the conditions specified enough to get the sort of polynomial that I was expecting.  After finding the coefficients for several sixth degree polynomials in a row, I figured I should instead be able to do something like this with the proper lisp:]]></description>
			<content:encoded><![CDATA[<p>Often, I want to have a polynomial that meets certain criteria.  For most applications in the past, I figured it all out by hand.</p>
<p>Today, I was having trouble getting the conditions specified enough to get the sort of polynomial that I was expecting.  After finding the coefficients for several sixth degree polynomials in a row, I figured I should instead be able to do something like this with the proper lisp:</p>
<div class="codecolorer-container lisp blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><pre class="lisp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #66cc66;">&#40;</span>calculate-polynomial-subject-to
    <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">value</span> <span style="color: #66cc66;">:</span><span style="color: #555;">at</span> <span style="color: #cc66cc;">0</span> <span style="color: #66cc66;">:</span><span style="color: #555;">equals</span> <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#40;</span>derivative <span style="color: #66cc66;">:</span><span style="color: #555;">at</span> <span style="color: #cc66cc;">0</span> <span style="color: #66cc66;">:</span><span style="color: #555;">equals</span> <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#40;</span>nth-derivative <span style="color: #cc66cc;">2</span> <span style="color: #66cc66;">:</span><span style="color: #555;">at</span> <span style="color: #cc66cc;">0</span> <span style="color: #66cc66;">:</span><span style="color: #555;">equals</span> <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">value</span> <span style="color: #66cc66;">:</span><span style="color: #555;">at</span> <span style="color: #cc66cc;">1</span> <span style="color: #66cc66;">:</span><span style="color: #555;">equals</span> <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#40;</span>derivative <span style="color: #66cc66;">:</span><span style="color: #555;">at</span> <span style="color: #cc66cc;">1</span> <span style="color: #66cc66;">:</span><span style="color: #555;">equals</span> <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#40;</span>nth-derivative <span style="color: #cc66cc;">2</span> <span style="color: #66cc66;">:</span><span style="color: #555;">at</span> <span style="color: #cc66cc;">1</span> <span style="color: #66cc66;">:</span><span style="color: #555;">equals</span> <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">value</span> <span style="color: #66cc66;">:</span><span style="color: #555;">at</span> <span style="color: #cc66cc;">1</span>/<span style="color: #cc66cc;">2</span> <span style="color: #66cc66;">:</span><span style="color: #555;">equals</span> <span style="color: #cc66cc;">3</span>/<span style="color: #cc66cc;">4</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></pre></div>
<p>That&#8217;s all done now (<a href='http://nklein.com/2009/04/find-the-polynomial-youve-been-looking-for/polynomials/' rel='attachment wp-att-149'>polynomials.lisp</a>).  So, for the record, the above is:  <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_c08fc815e4f86686ef2843efd6ca32ae.png" title="26x^3 - 63x^4 + 54x^5 - 16x^6" style="vertical-align:-20%;" class="tex" alt="26x^3 - 63x^4 + 54x^5 - 16x^6" />.  That is still not quite the polynomial I want for this application, but it&#8217;s close.  A few minutes of Lisp saved me hours of whiteboard work.</p>
]]></content:encoded>
			<wfw:commentRss>http://nklein.com/2009/04/find-the-polynomial-youve-been-looking-for/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

