OSDN Git Service

Update docs.
[joypy/Thun.git] / docs / Quadratic.md
1
2
3 ```python
4 from notebook_preamble import J, V, define
5 ```
6
7 # [Quadratic formula](https://en.wikipedia.org/wiki/Quadratic_formula)
8
9 Cf. [jp-quadratic.html](http://www.kevinalbrecht.com/code/joy-mirror/jp-quadratic.html)
10
11        -b ± sqrt(b^2 - 4 * a * c)
12     --------------------------------
13                 2 * a
14
15 $\frac{-b  \pm \sqrt{b^2 - 4ac}}{2a}$
16
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.
19
20 ### `-b`
21     b neg
22
23 ### `sqrt(b^2 - 4 * a * c)`
24     b sqr 4 a c * * - sqrt
25
26 ### `/2a`
27     a 2 * /
28
29 ### `±`
30 There is a function `pm` that accepts two values on the stack and replaces them with their sum and difference.
31
32     pm == [+] [-] cleave popdd
33
34 ### Putting Them Together
35
36     b neg b sqr 4 a c * * - sqrt pm a 2 * [/] cons app2
37
38 We use `app2` to compute both roots by using a quoted program `[2a /]` built with `cons`.
39
40 ## Derive a definition.
41 Working backwards we use `dip` and `dipd` to extract the code from the variables:
42
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
48
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:
50
51
52 ```python
53 define('quadratic == over [[[neg] dupdip sqr 4] dipd * * - sqrt pm] dip 2 * [/] cons app2')
54 ```
55
56 Let's try it out:
57
58
59 ```python
60 J('3 1 1 quadratic')
61 ```
62
63     -0.3819660112501051 -2.618033988749895
64
65
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.
67
68
69 ```python
70 V('-5 1 4 quadratic')
71 ```
72
73                                                        . -5 1 4 quadratic
74                                                     -5 . 1 4 quadratic
75                                                   -5 1 . 4 quadratic
76                                                 -5 1 4 . quadratic
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
102                                          8.0 2.0 [2 /] . 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
116                                              4.0 [1.0] . first
117                                                4.0 1.0 . 
118