3 from notebook_preamble import J, V, define
5 `Quadratic formula <https://en.wikipedia.org/wiki/Quadratic_formula>`__
6 =======================================================================
9 `jp-quadratic.html <http://www.kevinalbrecht.com/code/joy-mirror/jp-quadratic.html>`__
13 -b ± sqrt(b^2 - 4 * a * c)
14 --------------------------------
17 :math:`\frac{-b \pm \sqrt{b^2 - 4ac}}{2a}`
19 Write a straightforward program with variable names.
20 ----------------------------------------------------
22 This math translates to Joy code in a straightforward manner. We are
23 going to use named variables to keep track of the arguments, then write
24 a definition without them.
33 ``sqrt(b^2 - 4 * a * c)``
34 ~~~~~~~~~~~~~~~~~~~~~~~~~
38 b sqr 4 a c * * - sqrt
50 There is a function ``pm`` that accepts two values on the stack and
51 replaces them with their sum and difference.
55 pm == [+] [-] cleave popdd
62 b neg b sqr 4 a c * * - sqrt pm a 2 * [/] cons app2
64 We use ``app2`` to compute both roots by using a quoted program
65 ``[2a /]`` built with ``cons``.
70 Working backwards we use ``dip`` and ``dipd`` to extract the code from
75 b neg b sqr 4 a c * * - sqrt pm a 2 * [/] cons app2
76 b [neg] dupdip sqr 4 a c * * - sqrt pm a 2 * [/] cons app2
77 b a c [[neg] dupdip sqr 4] dipd * * - sqrt pm a 2 * [/] cons app2
78 b a c a [[[neg] dupdip sqr 4] dipd * * - sqrt pm] dip 2 * [/] cons app2
79 b a c over [[[neg] dupdip sqr 4] dipd * * - sqrt pm] dip 2 * [/] cons app2
81 The three arguments are to the left, so we can “chop off” everything to
82 the right and say it’s the definition of the ``quadratic`` function:
86 define('quadratic == over [[[neg] dupdip sqr 4] dipd * * - sqrt pm] dip 2 * [/] cons app2')
97 -0.3819660112501051 -2.618033988749895
100 If you look at the Joy evaluation trace you can see that the first few
101 lines are the ``dip`` and ``dipd`` combinators building the main program
102 by incorporating the values on the stack. Then that program runs and you
103 get the results. This is pretty typical of Joy code.
107 V('-5 1 4 quadratic')
116 -5 1 4 . over [[[neg] dupdip sqr 4] dipd * * - sqrt pm] dip 2 * [/] cons app2
117 -5 1 4 1 . [[[neg] dupdip sqr 4] dipd * * - sqrt pm] dip 2 * [/] cons app2
118 -5 1 4 1 [[[neg] dupdip sqr 4] dipd * * - sqrt pm] . dip 2 * [/] cons app2
119 -5 1 4 . [[neg] dupdip sqr 4] dipd * * - sqrt pm 1 2 * [/] cons app2
120 -5 1 4 [[neg] dupdip sqr 4] . dipd * * - sqrt pm 1 2 * [/] cons app2
121 -5 . [neg] dupdip sqr 4 1 4 * * - sqrt pm 1 2 * [/] cons app2
122 -5 [neg] . dupdip sqr 4 1 4 * * - sqrt pm 1 2 * [/] cons app2
123 -5 . neg -5 sqr 4 1 4 * * - sqrt pm 1 2 * [/] cons app2
124 5 . -5 sqr 4 1 4 * * - sqrt pm 1 2 * [/] cons app2
125 5 -5 . sqr 4 1 4 * * - sqrt pm 1 2 * [/] cons app2
126 5 -5 . dup mul 4 1 4 * * - sqrt pm 1 2 * [/] cons app2
127 5 -5 -5 . mul 4 1 4 * * - sqrt pm 1 2 * [/] cons app2
128 5 25 . 4 1 4 * * - sqrt pm 1 2 * [/] cons app2
129 5 25 4 . 1 4 * * - sqrt pm 1 2 * [/] cons app2
130 5 25 4 1 . 4 * * - sqrt pm 1 2 * [/] cons app2
131 5 25 4 1 4 . * * - sqrt pm 1 2 * [/] cons app2
132 5 25 4 4 . * - sqrt pm 1 2 * [/] cons app2
133 5 25 16 . - sqrt pm 1 2 * [/] cons app2
134 5 9 . sqrt pm 1 2 * [/] cons app2
135 5 3.0 . pm 1 2 * [/] cons app2
136 8.0 2.0 . 1 2 * [/] cons app2
137 8.0 2.0 1 . 2 * [/] cons app2
138 8.0 2.0 1 2 . * [/] cons app2
139 8.0 2.0 2 . [/] cons app2
140 8.0 2.0 2 [/] . cons app2
142 [8.0] [2 /] . infra first [2.0] [2 /] infra first
143 8.0 . 2 / [] swaack first [2.0] [2 /] infra first
144 8.0 2 . / [] swaack first [2.0] [2 /] infra first
145 4.0 . [] swaack first [2.0] [2 /] infra first
146 4.0 [] . swaack first [2.0] [2 /] infra first
147 [4.0] . first [2.0] [2 /] infra first
148 4.0 . [2.0] [2 /] infra first
149 4.0 [2.0] . [2 /] infra first
150 4.0 [2.0] [2 /] . infra first
151 2.0 . 2 / [4.0] swaack first
152 2.0 2 . / [4.0] swaack first
153 1.0 . [4.0] swaack first
154 1.0 [4.0] . swaack first