4 from notebook_preamble import J, V, define
7 # [Quadratic formula](https://en.wikipedia.org/wiki/Quadratic_formula)
9 Cf. [jp-quadratic.html](http://www.kevinalbrecht.com/code/joy-mirror/jp-quadratic.html)
11 -b ± sqrt(b^2 - 4 * a * c)
12 --------------------------------
15 $\frac{-b \pm \sqrt{b^2 - 4ac}}{2a}$
17 ## Write a straightforward program with variable names.
18 This math translates to Joy code in a straightforward manner. We are going to use named variables to keep track of the arguments, then write a definition without them.
23 ### `sqrt(b^2 - 4 * a * c)`
24 b sqr 4 a c * * - sqrt
30 There is a function `pm` that accepts two values on the stack and replaces them with their sum and difference.
32 pm == [+] [-] cleave popdd
34 ### Putting Them Together
36 b neg b sqr 4 a c * * - sqrt pm a 2 * [/] cons app2
38 We use `app2` to compute both roots by using a quoted program `[2a /]` built with `cons`.
40 ## Derive a definition.
41 Working backwards we use `dip` and `dipd` to extract the code from the variables:
43 b neg b sqr 4 a c * * - sqrt pm a 2 * [/] cons app2
44 b [neg] dupdip sqr 4 a c * * - sqrt pm a 2 * [/] cons app2
45 b a c [[neg] dupdip sqr 4] dipd * * - sqrt pm a 2 * [/] cons app2
46 b a c a [[[neg] dupdip sqr 4] dipd * * - sqrt pm] dip 2 * [/] cons app2
47 b a c over [[[neg] dupdip sqr 4] dipd * * - sqrt pm] dip 2 * [/] cons app2
49 The three arguments are to the left, so we can "chop off" everything to the right and say it's the definition of the `quadratic` function:
53 define('quadratic == over [[[neg] dupdip sqr 4] dipd * * - sqrt pm] dip 2 * [/] cons app2')
63 -0.3819660112501051 -2.618033988749895
66 If you look at the Joy evaluation trace you can see that the first few lines are the `dip` and `dipd` combinators building the main program by incorporating the values on the stack. Then that program runs and you get the results. This is pretty typical of Joy code.
77 -5 1 4 . over [[[neg] dupdip sqr 4] dipd * * - sqrt pm] dip 2 * [/] cons app2
78 -5 1 4 1 . [[[neg] dupdip sqr 4] dipd * * - sqrt pm] dip 2 * [/] cons app2
79 -5 1 4 1 [[[neg] dupdip sqr 4] dipd * * - sqrt pm] . dip 2 * [/] cons app2
80 -5 1 4 . [[neg] dupdip sqr 4] dipd * * - sqrt pm 1 2 * [/] cons app2
81 -5 1 4 [[neg] dupdip sqr 4] . dipd * * - sqrt pm 1 2 * [/] cons app2
82 -5 . [neg] dupdip sqr 4 1 4 * * - sqrt pm 1 2 * [/] cons app2
83 -5 [neg] . dupdip sqr 4 1 4 * * - sqrt pm 1 2 * [/] cons app2
84 -5 . neg -5 sqr 4 1 4 * * - sqrt pm 1 2 * [/] cons app2
85 5 . -5 sqr 4 1 4 * * - sqrt pm 1 2 * [/] cons app2
86 5 -5 . sqr 4 1 4 * * - sqrt pm 1 2 * [/] cons app2
87 5 -5 . dup mul 4 1 4 * * - sqrt pm 1 2 * [/] cons app2
88 5 -5 -5 . mul 4 1 4 * * - sqrt pm 1 2 * [/] cons app2
89 5 25 . 4 1 4 * * - sqrt pm 1 2 * [/] cons app2
90 5 25 4 . 1 4 * * - sqrt pm 1 2 * [/] cons app2
91 5 25 4 1 . 4 * * - sqrt pm 1 2 * [/] cons app2
92 5 25 4 1 4 . * * - sqrt pm 1 2 * [/] cons app2
93 5 25 4 4 . * - sqrt pm 1 2 * [/] cons app2
94 5 25 16 . - sqrt pm 1 2 * [/] cons app2
95 5 9 . sqrt pm 1 2 * [/] cons app2
96 5 3.0 . pm 1 2 * [/] cons app2
97 8.0 2.0 . 1 2 * [/] cons app2
98 8.0 2.0 1 . 2 * [/] cons app2
99 8.0 2.0 1 2 . * [/] cons app2
100 8.0 2.0 2 . [/] cons app2
101 8.0 2.0 2 [/] . cons app2
103 [8.0] [2 /] . infra first [2.0] [2 /] infra first
104 8.0 . 2 / [] swaack first [2.0] [2 /] infra first
105 8.0 2 . / [] swaack first [2.0] [2 /] infra first
106 4.0 . [] swaack first [2.0] [2 /] infra first
107 4.0 [] . swaack first [2.0] [2 /] infra first
108 [4.0] . first [2.0] [2 /] infra first
109 4.0 . [2.0] [2 /] infra first
110 4.0 [2.0] . [2 /] infra first
111 4.0 [2.0] [2 /] . infra first
112 2.0 . 2 / [4.0] swaack first
113 2.0 2 . / [4.0] swaack first
114 1.0 . [4.0] swaack first
115 1.0 [4.0] . swaack first