The Hylos notebook is not ready for public yet.

This commit is contained in:
Simon Forman
2018-06-08 13:05:59 -07:00
parent f5fe7d9726
commit b98e9f2107
18 changed files with 463 additions and 2825 deletions
@@ -157,6 +157,9 @@
<span class="s1">dudipd == dup dipd</span>
<span class="s1">primrec == [i] genrec</span>
<span class="s1">step_zero == 0 roll&gt; step</span>
<span class="s1">codireco == cons dip rest cons</span>
<span class="s1">make_generator == [codireco] ccons</span>
<span class="s1">ccons == cons cons</span>
<span class="s1">&#39;&#39;&#39;</span>
<span class="c1">##Zipper</span>
@@ -17,7 +17,7 @@
<link rel="index" title="Index" href="../genindex.html" />
<link rel="search" title="Search" href="../search.html" />
<link rel="next" title="Quadratic formula" href="Quadratic.html" />
<link rel="prev" title="Treating Trees" href="Trees.html" />
<link rel="prev" title="Using x to Generate Values" href="Generator_Programs.html" />
<link rel="stylesheet" href="../_static/custom.css" type="text/css" />
@@ -34,16 +34,18 @@
<div class="section" id="newton-s-method">
<h1><a class="reference external" href="https://en.wikipedia.org/wiki/Newton%27s_method">Newtons method</a><a class="headerlink" href="#newton-s-method" title="Permalink to this headline"></a></h1>
<p>Newton-Raphson for finding the root of an equation.</p>
<p>Lets use the Newton-Raphson method for finding the root of an equation
to write a function that can compute the square root of a number.</p>
<p>Cf. <a class="reference external" href="https://www.cs.kent.ac.uk/people/staff/dat/miranda/whyfp90.pdf">“Why Functional Programming Matters” by John
Hughes</a></p>
<div class="code ipython2 highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">notebook_preamble</span> <span class="k">import</span> <span class="n">J</span><span class="p">,</span> <span class="n">V</span><span class="p">,</span> <span class="n">define</span>
</pre></div>
</div>
<p>Cf. <a class="reference external" href="https://www.cs.kent.ac.uk/people/staff/dat/miranda/whyfp90.pdf">“Why Functional Programming Matters” by John
Hughes</a></p>
<div class="section" id="a-generator-for-approximations">
<h2>A Generator for Approximations<a class="headerlink" href="#a-generator-for-approximations" title="Permalink to this headline"></a></h2>
<p>In <a class="reference internal" href="Generator Programs.html"><span class="doc">Using x to Generate Values</span></a> we derive a function (called <code class="docutils literal notranslate"><span class="pre">make_generator</span></code> in the dictionary) that accepts an initial value and a quoted program and returns a new quoted program that, when driven by the <code class="docutils literal notranslate"><span class="pre">x</span></code> combinator (<a class="reference internal" href="../library.html#joy.library.x" title="joy.library.x"><code class="xref py py-func docutils literal notranslate"><span class="pre">joy.library.x()</span></code></a>), acts like a lazy stream.</p>
<p>To make a generator that generates successive approximations lets start by assuming an initial approximation and then derive the function that computes the next approximation:</p>
<p>To make a generator that generates successive approximations lets start
by assuming an initial approximation and then derive the function that
computes the next approximation:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span> <span class="n">a</span> <span class="n">F</span>
<span class="o">---------</span>
<span class="n">a</span><span class="s1">&#39;</span>
@@ -51,7 +53,8 @@ Hughes</a></p>
</div>
<div class="section" id="a-function-to-compute-the-next-approximation">
<h3>A Function to Compute the Next Approximation<a class="headerlink" href="#a-function-to-compute-the-next-approximation" title="Permalink to this headline"></a></h3>
<p>Looking at the equation again:</p>
<p>This is the equation for computing the next approximate value of the
square root:</p>
<p><span class="math notranslate nohighlight">\(a_{i+1} = \frac{(a_i+\frac{n}{a_i})}{2}\)</span></p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">a</span> <span class="n">n</span> <span class="n">over</span> <span class="o">/</span> <span class="o">+</span> <span class="mi">2</span> <span class="o">/</span>
<span class="n">a</span> <span class="n">n</span> <span class="n">a</span> <span class="o">/</span> <span class="o">+</span> <span class="mi">2</span> <span class="o">/</span>
@@ -71,7 +74,8 @@ Hughes</a></p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">a</span> <span class="p">[</span><span class="n">dup</span> <span class="n">F</span><span class="p">]</span> <span class="n">make_generator</span>
</pre></div>
</div>
<p>With <code class="docutils literal notranslate"><span class="pre">n</span></code> as part of the function <code class="docutils literal notranslate"><span class="pre">F</span></code>, but <code class="docutils literal notranslate"><span class="pre">n</span></code> is the input to the <code class="docutils literal notranslate"><span class="pre">sqrt</span></code> function were writing. If we let 1 be the initial approximation:</p>
<p>With n as part of the function F, but n is the input to the sqrt
function were writing. If we let 1 be the initial approximation:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="mi">1</span> <span class="n">n</span> <span class="mi">1</span> <span class="o">/</span> <span class="o">+</span> <span class="mi">2</span> <span class="o">/</span>
<span class="mi">1</span> <span class="n">n</span><span class="o">/</span><span class="mi">1</span> <span class="o">+</span> <span class="mi">2</span> <span class="o">/</span>
<span class="mi">1</span> <span class="n">n</span> <span class="o">+</span> <span class="mi">2</span> <span class="o">/</span>
@@ -80,48 +84,51 @@ Hughes</a></p>
</pre></div>
</div>
<p>The generator can be written as:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="mi">1</span> <span class="n">swap</span> <span class="p">[</span><span class="n">over</span> <span class="o">/</span> <span class="o">+</span> <span class="mi">2</span> <span class="o">/</span><span class="p">]</span> <span class="n">cons</span> <span class="p">[</span><span class="n">dup</span><span class="p">]</span> <span class="n">swoncat</span> <span class="n">make_generator</span>
</pre></div>
</div>
<p>Example:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="mi">23</span> <span class="mi">1</span> <span class="n">swap</span> <span class="p">[</span><span class="n">over</span> <span class="o">/</span> <span class="o">+</span> <span class="mi">2</span> <span class="o">/</span><span class="p">]</span> <span class="n">cons</span> <span class="p">[</span><span class="n">dup</span><span class="p">]</span> <span class="n">swoncat</span> <span class="n">make_generator</span>
<span class="mi">1</span> <span class="mi">23</span> <span class="p">[</span><span class="n">over</span> <span class="o">/</span> <span class="o">+</span> <span class="mi">2</span> <span class="o">/</span><span class="p">]</span> <span class="n">cons</span> <span class="p">[</span><span class="n">dup</span><span class="p">]</span> <span class="n">swoncat</span> <span class="n">make_generator</span>
<span class="mi">1</span> <span class="p">[</span><span class="mi">23</span> <span class="n">over</span> <span class="o">/</span> <span class="o">+</span> <span class="mi">2</span> <span class="o">/</span><span class="p">]</span> <span class="p">[</span><span class="n">dup</span><span class="p">]</span> <span class="n">swoncat</span> <span class="n">make_generator</span>
<span class="mi">1</span> <span class="p">[</span><span class="n">dup</span> <span class="mi">23</span> <span class="n">over</span> <span class="o">/</span> <span class="o">+</span> <span class="mi">2</span> <span class="o">/</span><span class="p">]</span> <span class="n">make_generator</span>
<span class="o">.</span>
<span class="o">.</span>
<span class="o">.</span>
<span class="p">[</span><span class="mi">1</span> <span class="n">swap</span> <span class="p">[</span><span class="n">dup</span> <span class="mi">23</span> <span class="n">over</span> <span class="o">/</span> <span class="o">+</span> <span class="mi">2</span> <span class="o">/</span><span class="p">]</span> <span class="n">direco</span><span class="p">]</span>
</pre></div>
</div>
<div class="code ipython2 highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">define</span><span class="p">(</span><span class="s1">&#39;gsra == 1 swap [over / + 2 /] cons [dup] swoncat make_generator&#39;</span><span class="p">)</span>
</pre></div>
</div>
<div class="section" id="a-generator-of-square-root-approximations">
<h3>A Generator of Square Root Approximations<a class="headerlink" href="#a-generator-of-square-root-approximations" title="Permalink to this headline"></a></h3>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">gsra</span> <span class="o">==</span> <span class="mi">1</span> <span class="n">swap</span> <span class="p">[</span><span class="n">over</span> <span class="o">/</span> <span class="o">+</span> <span class="mi">2</span> <span class="o">/</span><span class="p">]</span> <span class="n">cons</span> <span class="p">[</span><span class="n">dup</span><span class="p">]</span> <span class="n">swoncat</span> <span class="n">make_generator</span>
<div class="code ipython2 highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">J</span><span class="p">(</span><span class="s1">&#39;23 gsra&#39;</span><span class="p">)</span>
</pre></div>
</div>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="p">[</span><span class="mi">1</span> <span class="p">[</span><span class="n">dup</span> <span class="mi">23</span> <span class="n">over</span> <span class="o">/</span> <span class="o">+</span> <span class="mi">2</span> <span class="o">/</span><span class="p">]</span> <span class="n">codireco</span><span class="p">]</span>
</pre></div>
</div>
<p>Lets drive the generator a few time (with the <code class="docutils literal notranslate"><span class="pre">x</span></code> combinator) and
square the approximation to see how well it works…</p>
<div class="code ipython2 highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">J</span><span class="p">(</span><span class="s1">&#39;23 gsra 6 [x popd] times first sqr&#39;</span><span class="p">)</span>
</pre></div>
</div>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="mf">23.0000000001585</span>
</pre></div>
</div>
</div>
</div>
<div class="section" id="finding-consecutive-approximations-within-a-tolerance">
<h2>Finding Consecutive Approximations <code class="docutils literal notranslate"><span class="pre">within</span></code> a Tolerance<a class="headerlink" href="#finding-consecutive-approximations-within-a-tolerance" title="Permalink to this headline"></a></h2>
<blockquote>
<div>The remainder of a square root finder is a function <em>within</em>, which takes a tolerance and a list of approximations and looks down the list for two successive approximations that differ by no more than the given tolerance.</div></blockquote>
<h2>Finding Consecutive Approximations within a Tolerance<a class="headerlink" href="#finding-consecutive-approximations-within-a-tolerance" title="Permalink to this headline"></a></h2>
<p>From <a class="reference external" href="https://www.cs.kent.ac.uk/people/staff/dat/miranda/whyfp90.pdf">“Why Functional Programming Matters” by John
Hughes</a></p>
Hughes</a>:</p>
<blockquote>
<div>The remainder of a square root finder is a function <em>within</em>, which
takes a tolerance and a list of approximations and looks down the
list for two successive approximations that differ by no more than
the given tolerance.</div></blockquote>
<p>(And note that by “list” he means a lazily-evaluated list.)</p>
<p>Using the <em>output</em> <code class="docutils literal notranslate"><span class="pre">[a</span> <span class="pre">G]</span></code> of the above <a class="reference internal" href="Generator Programs.html"><span class="doc">generator</span></a> for square root approximations, and further assuming that the first term <code class="docutils literal notranslate"><span class="pre">a</span></code> has been generated already and epsilon <code class="docutils literal notranslate"><span class="pre">ε</span></code> is handy on the stack…</p>
<p>Using the <em>output</em> <code class="docutils literal notranslate"><span class="pre">[a</span> <span class="pre">G]</span></code> of the above generator for square root
approximations, and further assuming that the first term a has been
generated already and epsilon ε is handy on the stack…</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span> <span class="n">a</span> <span class="p">[</span><span class="n">b</span> <span class="n">G</span><span class="p">]</span> <span class="n">ε</span> <span class="n">within</span>
<span class="o">----------------------</span> <span class="n">a</span> <span class="n">b</span> <span class="o">-</span> <span class="nb">abs</span> <span class="n">ε</span> <span class="o">&lt;=</span>
<span class="n">b</span>
</pre></div>
</div>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span> <span class="n">a</span> <span class="p">[</span><span class="n">b</span> <span class="n">G</span><span class="p">]</span> <span class="n">ε</span> <span class="n">within</span>
<span class="n">a</span> <span class="p">[</span><span class="n">b</span> <span class="n">G</span><span class="p">]</span> <span class="n">ε</span> <span class="n">within</span>
<span class="o">----------------------</span> <span class="n">a</span> <span class="n">b</span> <span class="o">-</span> <span class="nb">abs</span> <span class="n">ε</span> <span class="o">&gt;</span>
<span class="o">.</span>
<span class="p">[</span><span class="n">b</span> <span class="n">G</span><span class="p">]</span> <span class="n">x</span> <span class="n">ε</span> <span class="o">...</span>
<span class="n">b</span> <span class="p">[</span><span class="n">c</span> <span class="n">G</span><span class="p">]</span> <span class="n">ε</span> <span class="o">...</span>
<span class="o">.</span>
<span class="o">----------------------</span>
<span class="n">b</span> <span class="p">[</span><span class="n">c</span> <span class="n">G</span><span class="p">]</span> <span class="n">ε</span> <span class="n">within</span>
</pre></div>
</div>
@@ -135,7 +142,7 @@ Hughes</a></p>
<span class="p">(</span><span class="nb">abs</span><span class="p">(</span><span class="n">a</span><span class="o">-</span><span class="n">b</span><span class="p">)</span><span class="o">&lt;=</span><span class="n">ε</span><span class="p">)</span>
</pre></div>
</div>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">P</span> <span class="o">==</span> <span class="p">[</span><span class="n">first</span> <span class="o">-</span> <span class="nb">abs</span><span class="p">]</span> <span class="n">dip</span> <span class="o">&lt;=</span>
<div class="code ipython2 highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">define</span><span class="p">(</span><span class="s1">&#39;_within_P == [first - abs] dip &lt;=&#39;</span><span class="p">)</span>
</pre></div>
</div>
</div>
@@ -147,7 +154,7 @@ Hughes</a></p>
<span class="n">b</span>
</pre></div>
</div>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">B</span> <span class="o">==</span> <span class="n">roll</span><span class="o">&lt;</span> <span class="n">popop</span> <span class="n">first</span>
<div class="code ipython2 highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">define</span><span class="p">(</span><span class="s1">&#39;_within_B == roll&lt; popop first&#39;</span><span class="p">)</span>
</pre></div>
</div>
</div>
@@ -157,10 +164,11 @@ Hughes</a></p>
</pre></div>
</div>
<ol class="arabic simple">
<li>Discard <code class="docutils literal notranslate"><span class="pre">a</span></code>.</li>
<li>Discard a.</li>
<li>Use <code class="docutils literal notranslate"><span class="pre">x</span></code> combinator to generate next term from <code class="docutils literal notranslate"><span class="pre">G</span></code>.</li>
<li>Run <code class="docutils literal notranslate"><span class="pre">within</span></code> with <code class="docutils literal notranslate"><span class="pre">i</span></code> (it is a <code class="docutils literal notranslate"><span class="pre">primrec</span></code> function.)</li>
</ol>
<p>Pretty straightforward:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">a</span> <span class="p">[</span><span class="n">b</span> <span class="n">G</span><span class="p">]</span> <span class="n">ε</span> <span class="n">R0</span> <span class="p">[</span><span class="n">within</span><span class="p">]</span> <span class="n">R1</span>
<span class="n">a</span> <span class="p">[</span><span class="n">b</span> <span class="n">G</span><span class="p">]</span> <span class="n">ε</span> <span class="p">[</span><span class="n">popd</span> <span class="n">x</span><span class="p">]</span> <span class="n">dip</span> <span class="p">[</span><span class="n">within</span><span class="p">]</span> <span class="n">i</span>
<span class="n">a</span> <span class="p">[</span><span class="n">b</span> <span class="n">G</span><span class="p">]</span> <span class="n">popd</span> <span class="n">x</span> <span class="n">ε</span> <span class="p">[</span><span class="n">within</span><span class="p">]</span> <span class="n">i</span>
@@ -171,34 +179,51 @@ Hughes</a></p>
<span class="n">b</span> <span class="p">[</span><span class="n">c</span> <span class="n">G</span><span class="p">]</span> <span class="n">ε</span> <span class="n">within</span>
</pre></div>
</div>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">R0</span> <span class="o">==</span> <span class="p">[</span><span class="n">popd</span> <span class="n">x</span><span class="p">]</span> <span class="n">dip</span>
<div class="code ipython2 highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">define</span><span class="p">(</span><span class="s1">&#39;_within_R == [popd x] dip&#39;</span><span class="p">)</span>
</pre></div>
</div>
</div>
<div class="section" id="setting-up">
<h3>Setting up<a class="headerlink" href="#setting-up" title="Permalink to this headline"></a></h3>
<p>The recursive function we have defined so far needs a slight preamble: <code class="docutils literal notranslate"><span class="pre">x</span></code> to prime the generator and the epsilon value to use:</p>
<p>The recursive function we have defined so far needs a slight preamble:
<code class="docutils literal notranslate"><span class="pre">x</span></code> to prime the generator and the epsilon value to use:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="p">[</span><span class="n">a</span> <span class="n">G</span><span class="p">]</span> <span class="n">x</span> <span class="n">ε</span> <span class="o">...</span>
<span class="n">a</span> <span class="p">[</span><span class="n">b</span> <span class="n">G</span><span class="p">]</span> <span class="n">ε</span> <span class="o">...</span>
</pre></div>
</div>
</div>
<div class="section" id="within">
<h3><code class="docutils literal notranslate"><span class="pre">within</span></code><a class="headerlink" href="#within" title="Permalink to this headline"></a></h3>
<p>Giving us the following definitions:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">_within_P</span> <span class="o">==</span> <span class="p">[</span><span class="n">first</span> <span class="o">-</span> <span class="nb">abs</span><span class="p">]</span> <span class="n">dip</span> <span class="o">&lt;=</span>
<span class="n">_within_B</span> <span class="o">==</span> <span class="n">roll</span><span class="o">&lt;</span> <span class="n">popop</span> <span class="n">first</span>
<span class="n">_within_R</span> <span class="o">==</span> <span class="p">[</span><span class="n">popd</span> <span class="n">x</span><span class="p">]</span> <span class="n">dip</span>
<span class="n">within</span> <span class="o">==</span> <span class="n">x</span> <span class="n">ε</span> <span class="p">[</span><span class="n">_within_P</span><span class="p">]</span> <span class="p">[</span><span class="n">_within_B</span><span class="p">]</span> <span class="p">[</span><span class="n">_within_R</span><span class="p">]</span> <span class="n">primrec</span>
<div class="code ipython2 highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">define</span><span class="p">(</span><span class="s1">&#39;within == x 0.000000001 [_within_P] [_within_B] [_within_R] primrec&#39;</span><span class="p">)</span>
<span class="n">define</span><span class="p">(</span><span class="s1">&#39;sqrt == gsra within&#39;</span><span class="p">)</span>
</pre></div>
</div>
</div>
</div>
<div class="section" id="finding-square-roots">
<h2>Finding Square Roots<a class="headerlink" href="#finding-square-roots" title="Permalink to this headline"></a></h2>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">sqrt</span> <span class="o">==</span> <span class="n">gsra</span> <span class="n">within</span>
<p>Try it out…</p>
<div class="code ipython2 highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">J</span><span class="p">(</span><span class="s1">&#39;36 sqrt&#39;</span><span class="p">)</span>
</pre></div>
</div>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="mf">6.0</span>
</pre></div>
</div>
<div class="code ipython2 highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">J</span><span class="p">(</span><span class="s1">&#39;23 sqrt&#39;</span><span class="p">)</span>
</pre></div>
</div>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="mf">4.795831523312719</span>
</pre></div>
</div>
<p>Check it.</p>
<div class="code ipython2 highlight-default notranslate"><div class="highlight"><pre><span></span><span class="mf">4.795831523312719</span><span class="o">**</span><span class="mi">2</span>
</pre></div>
</div>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="mf">22.999999999999996</span>
</pre></div>
</div>
<div class="code ipython2 highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">math</span> <span class="k">import</span> <span class="n">sqrt</span>
<span class="n">sqrt</span><span class="p">(</span><span class="mi">23</span><span class="p">)</span>
</pre></div>
</div>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="mf">4.795831523312719</span>
</pre></div>
</div>
</div>
</div>
</div>
@@ -214,18 +239,15 @@ Hughes</a></p>
<li><a class="reference internal" href="#a-generator-for-approximations">A Generator for Approximations</a><ul>
<li><a class="reference internal" href="#a-function-to-compute-the-next-approximation">A Function to Compute the Next Approximation</a></li>
<li><a class="reference internal" href="#make-it-into-a-generator">Make it into a Generator</a></li>
<li><a class="reference internal" href="#a-generator-of-square-root-approximations">A Generator of Square Root Approximations</a></li>
</ul>
</li>
<li><a class="reference internal" href="#finding-consecutive-approximations-within-a-tolerance">Finding Consecutive Approximations <code class="docutils literal notranslate"><span class="pre">within</span></code> a Tolerance</a><ul>
<li><a class="reference internal" href="#finding-consecutive-approximations-within-a-tolerance">Finding Consecutive Approximations within a Tolerance</a><ul>
<li><a class="reference internal" href="#predicate">Predicate</a></li>
<li><a class="reference internal" href="#base-case">Base-Case</a></li>
<li><a class="reference internal" href="#recur">Recur</a></li>
<li><a class="reference internal" href="#setting-up">Setting up</a></li>
<li><a class="reference internal" href="#within"><code class="docutils literal notranslate"><span class="pre">within</span></code></a></li>
</ul>
</li>
<li><a class="reference internal" href="#finding-square-roots">Finding Square Roots</a></li>
</ul>
</li>
</ul>
@@ -234,7 +256,7 @@ Hughes</a></p>
<ul>
<li><a href="../index.html">Documentation overview</a><ul>
<li><a href="index.html">Essays about Programming in Joy</a><ul>
<li>Previous: <a href="Trees.html" title="previous chapter">Treating Trees</a></li>
<li>Previous: <a href="Generator_Programs.html" title="previous chapter">Using <code class="docutils literal notranslate"><span class="pre">x</span></code> to Generate Values</a></li>
<li>Next: <a href="Quadratic.html" title="next chapter">Quadratic formula</a></li>
</ul></li>
</ul></li>
@@ -16,7 +16,7 @@
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.1/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script>
<link rel="index" title="Index" href="../genindex.html" />
<link rel="search" title="Search" href="../search.html" />
<link rel="next" title="No Updates" href="NoUpdates.html" />
<link rel="next" title="Traversing Datastructures with Zippers" href="Zipper.html" />
<link rel="prev" title="Newtons method" href="Newton-Raphson.html" />
<link rel="stylesheet" href="../_static/custom.css" type="text/css" />
@@ -213,7 +213,7 @@ already.)</p>
<li><a href="../index.html">Documentation overview</a><ul>
<li><a href="index.html">Essays about Programming in Joy</a><ul>
<li>Previous: <a href="Newton-Raphson.html" title="previous chapter">Newtons method</a></li>
<li>Next: <a href="NoUpdates.html" title="next chapter">No Updates</a></li>
<li>Next: <a href="Zipper.html" title="next chapter">Traversing Datastructures with Zippers</a></li>
</ul></li>
</ul></li>
</ul>
@@ -16,6 +16,8 @@
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.1/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script>
<link rel="index" title="Index" href="../genindex.html" />
<link rel="search" title="Search" href="../search.html" />
<link rel="next" title="No Updates" href="NoUpdates.html" />
<link rel="prev" title="Quadratic formula" href="Quadratic.html" />
<link rel="stylesheet" href="../_static/custom.css" type="text/css" />
@@ -323,6 +325,10 @@ i d i d i d d Bingo!
<h3>Related Topics</h3>
<ul>
<li><a href="../index.html">Documentation overview</a><ul>
<li><a href="index.html">Essays about Programming in Joy</a><ul>
<li>Previous: <a href="Quadratic.html" title="previous chapter">Quadratic formula</a></li>
<li>Next: <a href="NoUpdates.html" title="next chapter">No Updates</a></li>
</ul></li>
</ul></li>
</ul>
</div>
@@ -86,8 +86,7 @@
</li>
<li class="toctree-l1"><a class="reference internal" href="Newton-Raphson.html">Newtons method</a><ul>
<li class="toctree-l2"><a class="reference internal" href="Newton-Raphson.html#a-generator-for-approximations">A Generator for Approximations</a></li>
<li class="toctree-l2"><a class="reference internal" href="Newton-Raphson.html#finding-consecutive-approximations-within-a-tolerance">Finding Consecutive Approximations <code class="docutils literal notranslate"><span class="pre">within</span></code> a Tolerance</a></li>
<li class="toctree-l2"><a class="reference internal" href="Newton-Raphson.html#finding-square-roots">Finding Square Roots</a></li>
<li class="toctree-l2"><a class="reference internal" href="Newton-Raphson.html#finding-consecutive-approximations-within-a-tolerance">Finding Consecutive Approximations within a Tolerance</a></li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="Quadratic.html">Quadratic formula</a><ul>
Binary file not shown.
File diff suppressed because one or more lines are too long
+128 -74
View File
@@ -1,33 +1,35 @@
*********************************************************************
`Newton's method <https://en.wikipedia.org/wiki/Newton%27s_method>`__
*********************************************************************
Newton-Raphson for finding the root of an equation.
`Newton's method <https://en.wikipedia.org/wiki/Newton%27s_method>`__
=====================================================================
Let's use the Newton-Raphson method for finding the root of an equation
to write a function that can compute the square root of a number.
Cf. `"Why Functional Programming Matters" by John
Hughes <https://www.cs.kent.ac.uk/people/staff/dat/miranda/whyfp90.pdf>`__
.. code:: ipython2
from notebook_preamble import J, V, define
Cf. `"Why Functional Programming Matters" by John
Hughes <https://www.cs.kent.ac.uk/people/staff/dat/miranda/whyfp90.pdf>`__
A Generator for Approximations
==============================
------------------------------
In :doc:`Generator Programs` we derive a function (called ``make_generator`` in the dictionary) that accepts an initial value and a quoted program and returns a new quoted program that, when driven by the ``x`` combinator (:py:func:`joy.library.x`), acts like a lazy stream.
To make a generator that generates successive approximations lets start
by assuming an initial approximation and then derive the function that
computes the next approximation:
To make a generator that generates successive approximations let's start by assuming an initial approximation and then derive the function that computes the next approximation::
::
a F
---------
a'
A Function to Compute the Next Approximation
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Looking at the equation again:
This is the equation for computing the next approximate value of the
square root:
:math:`a_{i+1} = \frac{(a_i+\frac{n}{a_i})}{2}`
@@ -39,19 +41,25 @@ Looking at the equation again:
a+n/a 2 /
(a+n/a)/2
The function we want has the argument ``n`` in it::
The function we want has the argument ``n`` in it:
::
F == n over / + 2 /
Make it into a Generator
^^^^^^^^^^^^^^^^^^^^^^^^
~~~~~~~~~~~~~~~~~~~~~~~~
Our generator would be created by::
Our generator would be created by:
::
a [dup F] make_generator
With ``n`` as part of the function ``F``, but ``n`` is the input to the ``sqrt`` function we're writing. If we let 1 be the initial approximation::
With n as part of the function F, but n is the input to the sqrt
function were writing. If we let 1 be the initial approximation:
::
1 n 1 / + 2 /
1 n/1 + 2 /
@@ -59,41 +67,58 @@ With ``n`` as part of the function ``F``, but ``n`` is the input to the ``sqrt``
n+1 2 /
(n+1)/2
The generator can be written as::
The generator can be written as:
1 swap [over / + 2 /] cons [dup] swoncat make_generator
Example::
::
23 1 swap [over / + 2 /] cons [dup] swoncat make_generator
1 23 [over / + 2 /] cons [dup] swoncat make_generator
1 [23 over / + 2 /] [dup] swoncat make_generator
1 [dup 23 over / + 2 /] make_generator
.
.
.
[1 swap [dup 23 over / + 2 /] direco]
.. code:: ipython2
define('gsra == 1 swap [over / + 2 /] cons [dup] swoncat make_generator')
.. code:: ipython2
J('23 gsra')
A Generator of Square Root Approximations
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
.. parsed-literal::
::
gsra == 1 swap [over / + 2 /] cons [dup] swoncat make_generator
[1 [dup 23 over / + 2 /] codireco]
Finding Consecutive Approximations ``within`` a Tolerance
=========================================================
Let's drive the generator a few time (with the ``x`` combinator) and
square the approximation to see how well it works...
The remainder of a square root finder is a function *within*, which takes a tolerance and a list of approximations and looks down the list for two successive approximations that differ by no more than the given tolerance.
.. code:: ipython2
J('23 gsra 6 [x popd] times first sqr')
.. parsed-literal::
23.0000000001585
Finding Consecutive Approximations within a Tolerance
-----------------------------------------------------
From `"Why Functional Programming Matters" by John
Hughes <https://www.cs.kent.ac.uk/people/staff/dat/miranda/whyfp90.pdf>`__
Hughes <https://www.cs.kent.ac.uk/people/staff/dat/miranda/whyfp90.pdf>`__:
(And note that by "list" he means a lazily-evaluated list.)
The remainder of a square root finder is a function *within*, which
takes a tolerance and a list of approximations and looks down the
list for two successive approximations that differ by no more than
the given tolerance.
Using the *output* ``[a G]`` of the above :doc:`generator <Generator Programs>` for square root approximations, and further assuming that the first term ``a`` has been generated already and epsilon ``ε`` is handy on the stack...
(And note that by “list” he means a lazily-evaluated list.)
Using the *output* ``[a G]`` of the above generator for square root
approximations, and further assuming that the first term a has been
generated already and epsilon ε is handy on the stack...
::
@@ -101,21 +126,13 @@ Using the *output* ``[a G]`` of the above :doc:`generator <Generator Programs>`
---------------------- a b - abs ε <=
b
::
a [b G] ε within
---------------------- a b - abs ε >
.
[b G] x ε ...
b [c G] ε ...
.
----------------------
b [c G] ε within
Predicate
^^^^^^^^^^^^^
~~~~~~~~~
::
@@ -126,14 +143,12 @@ Predicate
abs(a-b) ε <=
(abs(a-b)<=ε)
.. code:: ipython2
::
P == [first - abs] dip <=
define('_within_P == [first - abs] dip <=')
Base-Case
^^^^^^^^^^^^^
~~~~~~~~~
::
@@ -142,23 +157,23 @@ Base-Case
[b G] first
b
::
B == roll< popop first
.. code:: ipython2
define('_within_B == roll< popop first')
Recur
^^^^^^^^^^^^^
~~~~~
::
a [b G] ε R0 [within] R1
1. Discard ``a``.
1. Discard a.
2. Use ``x`` combinator to generate next term from ``G``.
3. Run ``within`` with ``i`` (it is a ``primrec`` function.)
Pretty straightforward:
::
a [b G] ε R0 [within] R1
@@ -170,35 +185,74 @@ Recur
b [c G] ε within
::
R0 == [popd x] dip
.. code:: ipython2
define('_within_R == [popd x] dip')
Setting up
^^^^^^^^^^
~~~~~~~~~~
The recursive function we have defined so far needs a slight preamble: ``x`` to prime the generator and the epsilon value to use::
The recursive function we have defined so far needs a slight preamble:
``x`` to prime the generator and the epsilon value to use:
::
[a G] x ε ...
a [b G] ε ...
.. code:: ipython2
``within``
^^^^^^^^^^
define('within == x 0.000000001 [_within_P] [_within_B] [_within_R] primrec')
define('sqrt == gsra within')
Giving us the following definitions::
Try it out...
_within_P == [first - abs] dip <=
_within_B == roll< popop first
_within_R == [popd x] dip
within == x ε [_within_P] [_within_B] [_within_R] primrec
.. code:: ipython2
J('36 sqrt')
Finding Square Roots
====================
.. parsed-literal::
::
6.0
.. code:: ipython2
J('23 sqrt')
.. parsed-literal::
4.795831523312719
Check it.
.. code:: ipython2
4.795831523312719**2
.. parsed-literal::
22.999999999999996
.. code:: ipython2
from math import sqrt
sqrt(23)
.. parsed-literal::
4.795831523312719
sqrt == gsra within