OSDN Git Service

Still converting syntax highlighter spec.
[joypy/Thun.git] / docs / sphinx_docs / _build / html / _sources / notebooks / Quadratic.rst.txt
1 .. code:: python
2
3     from notebook_preamble import J, V, define
4
5 `Quadratic formula <https://en.wikipedia.org/wiki/Quadratic_formula>`__
6 =======================================================================
7
8 Cf.
9 `jp-quadratic.html <http://www.kevinalbrecht.com/code/joy-mirror/jp-quadratic.html>`__
10
11 ::
12
13       -b ± sqrt(b^2 - 4 * a * c)
14    --------------------------------
15                2 * a
16
17 :math:`\frac{-b \pm \sqrt{b^2 - 4ac}}{2a}`
18
19 Write a straightforward program with variable names.
20 ----------------------------------------------------
21
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.
25
26 ``-b``
27 ~~~~~~
28
29 ::
30
31    b neg
32
33 ``sqrt(b^2 - 4 * a * c)``
34 ~~~~~~~~~~~~~~~~~~~~~~~~~
35
36 ::
37
38    b sqr 4 a c * * - sqrt
39
40 ``/2a``
41 ~~~~~~~
42
43 ::
44
45    a 2 * /
46
47 ``±``
48 ~~~~~
49
50 There is a function ``pm`` that accepts two values on the stack and
51 replaces them with their sum and difference.
52
53 ::
54
55    pm == [+] [-] cleave popdd
56
57 Putting Them Together
58 ~~~~~~~~~~~~~~~~~~~~~
59
60 ::
61
62    b neg b sqr 4 a c * * - sqrt pm a 2 * [/] cons app2
63
64 We use ``app2`` to compute both roots by using a quoted program
65 ``[2a /]`` built with ``cons``.
66
67 Derive a definition.
68 --------------------
69
70 Working backwards we use ``dip`` and ``dipd`` to extract the code from
71 the variables:
72
73 ::
74
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
80
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:
83
84 .. code:: python
85
86     define('quadratic == over [[[neg] dupdip sqr 4] dipd * * - sqrt pm] dip 2 * [/] cons app2')
87
88 Let’s try it out:
89
90 .. code:: python
91
92     J('3 1 1 quadratic')
93
94
95 .. parsed-literal::
96
97     -0.3819660112501051 -2.618033988749895
98
99
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.
104
105 .. code:: python
106
107     V('-5 1 4 quadratic')
108
109
110 .. parsed-literal::
111
112                                                        . -5 1 4 quadratic
113                                                     -5 . 1 4 quadratic
114                                                   -5 1 . 4 quadratic
115                                                 -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
141                                          8.0 2.0 [2 /] . 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
155                                              4.0 [1.0] . first
156                                                4.0 1.0 . 
157
158