1 `Newton's method <https://en.wikipedia.org/wiki/Newton%27s_method>`__
2 =====================================================================
4 Let's use the Newton-Raphson method for finding the root of an equation
5 to write a function that can compute the square root of a number.
7 Cf. `"Why Functional Programming Matters" by John
8 Hughes <https://www.cs.kent.ac.uk/people/staff/dat/miranda/whyfp90.pdf>`__
12 from notebook_preamble import J, V, define
14 A Generator for Approximations
15 ------------------------------
17 To make a generator that generates successive approximations let’s start
18 by assuming an initial approximation and then derive the function that
19 computes the next approximation:
27 A Function to Compute the Next Approximation
28 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
30 This is the equation for computing the next approximate value of the
33 :math:`a_{i+1} = \frac{(a_i+\frac{n}{a_i})}{2}`
43 The function we want has the argument ``n`` in it:
49 Make it into a Generator
50 ~~~~~~~~~~~~~~~~~~~~~~~~
52 Our generator would be created by:
56 a [dup F] make_generator
58 With n as part of the function F, but n is the input to the sqrt
59 function we’re writing. If we let 1 be the initial approximation:
69 The generator can be written as:
73 23 1 swap [over / + 2 /] cons [dup] swoncat make_generator
74 1 23 [over / + 2 /] cons [dup] swoncat make_generator
75 1 [23 over / + 2 /] [dup] swoncat make_generator
76 1 [dup 23 over / + 2 /] make_generator
80 define('gsra 1 swap [over / + 2 /] cons [dup] swoncat make_generator')
89 [1 [dup 23 over / + 2 /] codireco]
92 Let's drive the generator a few time (with the ``x`` combinator) and
93 square the approximation to see how well it works...
97 J('23 gsra 6 [x popd] times first sqr')
105 Finding Consecutive Approximations within a Tolerance
106 -----------------------------------------------------
108 From `"Why Functional Programming Matters" by John
109 Hughes <https://www.cs.kent.ac.uk/people/staff/dat/miranda/whyfp90.pdf>`__:
111 The remainder of a square root finder is a function *within*, which
112 takes a tolerance and a list of approximations and looks down the
113 list for two successive approximations that differ by no more than
116 (And note that by “list” he means a lazily-evaluated list.)
118 Using the *output* ``[a G]`` of the above generator for square root
119 approximations, and further assuming that the first term a has been
120 generated already and epsilon ε is handy on the stack...
125 ---------------------- a b - abs ε <=
130 ---------------------- a b - abs ε >
138 a [b G] ε [first - abs] dip <=
139 a [b G] first - abs ε <=
147 define('_within_P [first - abs] dip <=')
154 a [b G] ε roll< popop first
155 [b G] ε a popop first
161 define('_within_B roll< popop first')
168 a [b G] ε R0 [within] R1
171 2. Use ``x`` combinator to generate next term from ``G``.
172 3. Run ``within`` with ``i`` (it is a "tail-recursive" function.)
174 Pretty straightforward:
178 a [b G] ε R0 [within] R1
179 a [b G] ε [popd x] dip [within] i
180 a [b G] popd x ε [within] i
189 define('_within_R [popd x] dip')
194 The recursive function we have defined so far needs a slight preamble:
195 ``x`` to prime the generator and the epsilon value to use:
204 define('within x 0.000000001 [_within_P] [_within_B] [_within_R] tailrec')
205 define('sqrt gsra within')
246 from math import sqrt