OSDN Git Service

Some work on docs.
[joypy/Thun.git] / docs / Quadratic.ipynb
1 {
2  "cells": [
3   {
4    "cell_type": "code",
5    "execution_count": 1,
6    "metadata": {},
7    "outputs": [],
8    "source": [
9     "from notebook_preamble import J, V, define"
10    ]
11   },
12   {
13    "cell_type": "markdown",
14    "metadata": {},
15    "source": [
16     "# [Quadratic formula](https://en.wikipedia.org/wiki/Quadratic_formula)"
17    ]
18   },
19   {
20    "cell_type": "markdown",
21    "metadata": {},
22    "source": [
23     "Cf. [jp-quadratic.html](http://www.kevinalbrecht.com/code/joy-mirror/jp-quadratic.html)"
24    ]
25   },
26   {
27    "cell_type": "markdown",
28    "metadata": {},
29    "source": [
30     "       -b ± sqrt(b^2 - 4 * a * c)\n",
31     "    --------------------------------\n",
32     "                2 * a"
33    ]
34   },
35   {
36    "cell_type": "markdown",
37    "metadata": {},
38    "source": [
39     "$\\frac{-b  \\pm \\sqrt{b^2 - 4ac}}{2a}$"
40    ]
41   },
42   {
43    "cell_type": "markdown",
44    "metadata": {},
45    "source": [
46     "## Write a straightforward program with variable names.\n",
47     "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."
48    ]
49   },
50   {
51    "cell_type": "markdown",
52    "metadata": {},
53    "source": [
54     "### `-b`\n",
55     "    b neg"
56    ]
57   },
58   {
59    "cell_type": "markdown",
60    "metadata": {},
61    "source": [
62     "### `sqrt(b^2 - 4 * a * c)`\n",
63     "    b sqr 4 a c * * - sqrt"
64    ]
65   },
66   {
67    "cell_type": "markdown",
68    "metadata": {},
69    "source": [
70     "### `/2a`\n",
71     "    a 2 * /"
72    ]
73   },
74   {
75    "cell_type": "markdown",
76    "metadata": {},
77    "source": [
78     "### `±`\n",
79     "There is a function `pm` that accepts two values on the stack and replaces them with their sum and difference.\n",
80     "\n",
81     "    pm == [+] [-] cleave popdd"
82    ]
83   },
84   {
85    "cell_type": "markdown",
86    "metadata": {},
87    "source": [
88     "### Putting Them Together\n",
89     "\n",
90     "    b neg b sqr 4 a c * * - sqrt pm a 2 * [/] cons app2\n",
91     "\n",
92     "We use `app2` to compute both roots by using a quoted program `[2a /]` built with `cons`."
93    ]
94   },
95   {
96    "cell_type": "markdown",
97    "metadata": {},
98    "source": [
99     "## Derive a definition.\n",
100     "Working backwards we use `dip` and `dipd` to extract the code from the variables:\n",
101     "\n",
102     "    b             neg  b      sqr 4 a c   * * - sqrt pm a    2 * [/] cons app2\n",
103     "    b            [neg] dupdip sqr 4 a c   * * - sqrt pm a    2 * [/] cons app2\n",
104     "    b a c       [[neg] dupdip sqr 4] dipd * * - sqrt pm a    2 * [/] cons app2\n",
105     "    b a c a    [[[neg] dupdip sqr 4] dipd * * - sqrt pm] dip 2 * [/] cons app2\n",
106     "    b a c over [[[neg] dupdip sqr 4] dipd * * - sqrt pm] dip 2 * [/] cons app2\n",
107     "\n",
108     "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:"
109    ]
110   },
111   {
112    "cell_type": "code",
113    "execution_count": 2,
114    "metadata": {},
115    "outputs": [],
116    "source": [
117     "define('quadratic == over [[[neg] dupdip sqr 4] dipd * * - sqrt pm] dip 2 * [/] cons app2')"
118    ]
119   },
120   {
121    "cell_type": "markdown",
122    "metadata": {},
123    "source": [
124     "Let's try it out:"
125    ]
126   },
127   {
128    "cell_type": "code",
129    "execution_count": 3,
130    "metadata": {},
131    "outputs": [
132     {
133      "name": "stdout",
134      "output_type": "stream",
135      "text": [
136       "-0.3819660112501051 -2.618033988749895\n"
137      ]
138     }
139    ],
140    "source": [
141     "J('3 1 1 quadratic')"
142    ]
143   },
144   {
145    "cell_type": "markdown",
146    "metadata": {},
147    "source": [
148     "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."
149    ]
150   },
151   {
152    "cell_type": "code",
153    "execution_count": 4,
154    "metadata": {},
155    "outputs": [
156     {
157      "name": "stdout",
158      "output_type": "stream",
159      "text": [
160       "                                                   . -5 1 4 quadratic\n",
161       "                                                -5 . 1 4 quadratic\n",
162       "                                              -5 1 . 4 quadratic\n",
163       "                                            -5 1 4 . quadratic\n",
164       "                                            -5 1 4 . over [[[neg] dupdip sqr 4] dipd * * - sqrt pm] dip 2 * [/] cons app2\n",
165       "                                          -5 1 4 1 . [[[neg] dupdip sqr 4] dipd * * - sqrt pm] dip 2 * [/] cons app2\n",
166       "-5 1 4 1 [[[neg] dupdip sqr 4] dipd * * - sqrt pm] . dip 2 * [/] cons app2\n",
167       "                                            -5 1 4 . [[neg] dupdip sqr 4] dipd * * - sqrt pm 1 2 * [/] cons app2\n",
168       "                       -5 1 4 [[neg] dupdip sqr 4] . dipd * * - sqrt pm 1 2 * [/] cons app2\n",
169       "                                                -5 . [neg] dupdip sqr 4 1 4 * * - sqrt pm 1 2 * [/] cons app2\n",
170       "                                          -5 [neg] . dupdip sqr 4 1 4 * * - sqrt pm 1 2 * [/] cons app2\n",
171       "                                                -5 . neg -5 sqr 4 1 4 * * - sqrt pm 1 2 * [/] cons app2\n",
172       "                                                 5 . -5 sqr 4 1 4 * * - sqrt pm 1 2 * [/] cons app2\n",
173       "                                              5 -5 . sqr 4 1 4 * * - sqrt pm 1 2 * [/] cons app2\n",
174       "                                              5 -5 . dup mul 4 1 4 * * - sqrt pm 1 2 * [/] cons app2\n",
175       "                                           5 -5 -5 . mul 4 1 4 * * - sqrt pm 1 2 * [/] cons app2\n",
176       "                                              5 25 . 4 1 4 * * - sqrt pm 1 2 * [/] cons app2\n",
177       "                                            5 25 4 . 1 4 * * - sqrt pm 1 2 * [/] cons app2\n",
178       "                                          5 25 4 1 . 4 * * - sqrt pm 1 2 * [/] cons app2\n",
179       "                                        5 25 4 1 4 . * * - sqrt pm 1 2 * [/] cons app2\n",
180       "                                          5 25 4 4 . * - sqrt pm 1 2 * [/] cons app2\n",
181       "                                           5 25 16 . - sqrt pm 1 2 * [/] cons app2\n",
182       "                                               5 9 . sqrt pm 1 2 * [/] cons app2\n",
183       "                                             5 3.0 . pm 1 2 * [/] cons app2\n",
184       "                                           8.0 2.0 . 1 2 * [/] cons app2\n",
185       "                                         8.0 2.0 1 . 2 * [/] cons app2\n",
186       "                                       8.0 2.0 1 2 . * [/] cons app2\n",
187       "                                         8.0 2.0 2 . [/] cons app2\n",
188       "                                     8.0 2.0 2 [/] . cons app2\n",
189       "                                     8.0 2.0 [2 /] . app2\n",
190       "                                       [8.0] [2 /] . infra first [2.0] [2 /] infra first\n",
191       "                                               8.0 . 2 / [] swaack first [2.0] [2 /] infra first\n",
192       "                                             8.0 2 . / [] swaack first [2.0] [2 /] infra first\n",
193       "                                               4.0 . [] swaack first [2.0] [2 /] infra first\n",
194       "                                            4.0 [] . swaack first [2.0] [2 /] infra first\n",
195       "                                             [4.0] . first [2.0] [2 /] infra first\n",
196       "                                               4.0 . [2.0] [2 /] infra first\n",
197       "                                         4.0 [2.0] . [2 /] infra first\n",
198       "                                   4.0 [2.0] [2 /] . infra first\n",
199       "                                               2.0 . 2 / [4.0] swaack first\n",
200       "                                             2.0 2 . / [4.0] swaack first\n",
201       "                                               1.0 . [4.0] swaack first\n",
202       "                                         1.0 [4.0] . swaack first\n",
203       "                                         4.0 [1.0] . first\n",
204       "                                           4.0 1.0 . \n"
205      ]
206     }
207    ],
208    "source": [
209     "V('-5 1 4 quadratic')"
210    ]
211   },
212   {
213    "cell_type": "code",
214    "execution_count": null,
215    "metadata": {},
216    "outputs": [],
217    "source": []
218   }
219  ],
220  "metadata": {
221   "kernelspec": {
222    "display_name": "Python 3 (ipykernel)",
223    "language": "python",
224    "name": "python3"
225   },
226   "language_info": {
227    "codemirror_mode": {
228     "name": "ipython",
229     "version": 3
230    },
231    "file_extension": ".py",
232    "mimetype": "text/x-python",
233    "name": "python",
234    "nbconvert_exporter": "python",
235    "pygments_lexer": "ipython3",
236    "version": "3.7.10"
237   }
238  },
239  "nbformat": 4,
240  "nbformat_minor": 2
241 }