<h1><aclass="reference external"href="https://en.wikipedia.org/wiki/Newton%27s_method">Newton’s method</a><aclass="headerlink"href="#newton-s-method"title="Permalink to this headline">¶</a></h1>
<p>Newton-Raphson for finding the root of an equation.</p>
<p>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.</p>
<p>Cf. <aclass="reference external"href="https://www.cs.kent.ac.uk/people/staff/dat/miranda/whyfp90.pdf">“Why Functional Programming Matters” by John
<h2>A Generator for Approximations<aclass="headerlink"href="#a-generator-for-approximations"title="Permalink to this headline">¶</a></h2>
<p>In <aclass="reference internal"href="Generator Programs.html"><spanclass="doc">Using x to Generate Values</span></a> we derive a function (called <codeclass="docutils literal notranslate"><spanclass="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 <codeclass="docutils literal notranslate"><spanclass="pre">x</span></code> combinator (<aclass="reference internal"href="../library.html#joy.library.x"title="joy.library.x"><codeclass="xref py py-func docutils literal notranslate"><spanclass="pre">joy.library.x()</span></code></a>), acts like a lazy stream.</p>
<p>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:</p>
<p>To make a generator that generates successive approximations let’s start
by assuming an initial approximation and then derive the function that
<h3>A Function to Compute the Next Approximation<aclass="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
<p>With <codeclass="docutils literal notranslate"><spanclass="pre">n</span></code> as part of the function <codeclass="docutils literal notranslate"><spanclass="pre">F</span></code>, but <codeclass="docutils literal notranslate"><spanclass="pre">n</span></code> is the input to the <codeclass="docutils literal notranslate"><spanclass="pre">sqrt</span></code> function we’re 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 we’re writing. If we let 1 be the initial approximation:</p>
<h3>A Generator of Square Root Approximations<aclass="headerlink"href="#a-generator-of-square-root-approximations"title="Permalink to this headline">¶</a></h3>
<h2>Finding Consecutive Approximations <codeclass="docutils literal notranslate"><spanclass="pre">within</span></code> a Tolerance<aclass="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<aclass="headerlink"href="#finding-consecutive-approximations-within-a-tolerance"title="Permalink to this headline">¶</a></h2>
<p>From <aclass="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><codeclass="docutils literal notranslate"><spanclass="pre">[a</span><spanclass="pre">G]</span></code> of the above <aclass="reference internal"href="Generator Programs.html"><spanclass="doc">generator</span></a> for square root approximations, and further assuming that the first term <codeclass="docutils literal notranslate"><spanclass="pre">a</span></code> has been generated already and epsilon <codeclass="docutils literal notranslate"><spanclass="pre">ε</span></code> is handy on the stack…</p>
<p>Using the <em>output</em><codeclass="docutils literal notranslate"><spanclass="pre">[a</span><spanclass="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>
<li>Use <codeclass="docutils literal notranslate"><spanclass="pre">x</span></code> combinator to generate next term from <codeclass="docutils literal notranslate"><spanclass="pre">G</span></code>.</li>
<li>Run <codeclass="docutils literal notranslate"><spanclass="pre">within</span></code> with <codeclass="docutils literal notranslate"><spanclass="pre">i</span></code> (it is a <codeclass="docutils literal notranslate"><spanclass="pre">primrec</span></code> function.)</li>
<h3>Setting up<aclass="headerlink"href="#setting-up"title="Permalink to this headline">¶</a></h3>
<p>The recursive function we have defined so far needs a slight preamble:<codeclass="docutils literal notranslate"><spanclass="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:
<codeclass="docutils literal notranslate"><spanclass="pre">x</span></code> to prime the generator and the epsilon value to use:</p>
<h3><codeclass="docutils literal notranslate"><spanclass="pre">within</span></code><aclass="headerlink"href="#within"title="Permalink to this headline">¶</a></h3>
<li><aclass="reference internal"href="#a-generator-for-approximations">A Generator for Approximations</a><ul>
<li><aclass="reference internal"href="#a-function-to-compute-the-next-approximation">A Function to Compute the Next Approximation</a></li>
<li><aclass="reference internal"href="#make-it-into-a-generator">Make it into a Generator</a></li>
<li><aclass="reference internal"href="#a-generator-of-square-root-approximations">A Generator of Square Root Approximations</a></li>
</ul>
</li>
<li><aclass="reference internal"href="#finding-consecutive-approximations-within-a-tolerance">Finding Consecutive Approximations <codeclass="docutils literal notranslate"><spanclass="pre">within</span></code> a Tolerance</a><ul>
<li><aclass="reference internal"href="#finding-consecutive-approximations-within-a-tolerance">Finding Consecutive Approximations within a Tolerance</a><ul>
<liclass="toctree-l2"><aclass="reference internal"href="Newton-Raphson.html#finding-consecutive-approximations-within-a-tolerance">Finding Consecutive Approximations within a Tolerance</a></li>
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 let’s 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 we’re 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``
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
(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
Reference in New Issue
Block a user
Blocking a user prevents them from interacting with repositories, such as opening or commenting on pull requests or issues. Learn more about blocking a user.