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
+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