OSDN Git Service

Initial move of code from git repo.
[joypy/Thun.git] / docs / Quadratic.ipynb
1 {
2  "cells": [
3   {
4    "cell_type": "markdown",
5    "metadata": {},
6    "source": [
7     "# [Quadratic formula](https://en.wikipedia.org/wiki/Quadratic_formula)"
8    ]
9   },
10   {
11    "cell_type": "code",
12    "execution_count": 1,
13    "metadata": {},
14    "outputs": [],
15    "source": [
16     "from notebook_preamble import J, V, define"
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     "    b neg b sqr 4 a c * * - sqrt [+] [-] cleave a 2 * [truediv] cons app2\n",
48     "\n",
49     "### Check it.\n",
50     "\n",
51     "     b neg b sqr 4 a c * * - sqrt [+] [-] cleave a 2 * [truediv] cons app2\n",
52     "    -b     b sqr 4 a c * * - sqrt [+] [-] cleave a 2 * [truediv] cons app2\n",
53     "    -b     b^2   4 a c * * - sqrt [+] [-] cleave a 2 * [truediv] cons app2\n",
54     "    -b     b^2 4ac         - sqrt [+] [-] cleave a 2 * [truediv] cons app2\n",
55     "    -b     b^2-4ac           sqrt [+] [-] cleave a 2 * [truediv] cons app2\n",
56     "    -b sqrt(b^2-4ac)              [+] [-] cleave a 2 * [truediv] cons app2\n",
57     "\n",
58     "    -b -b+sqrt(b^2-4ac)    -b-sqrt(b^2-4ac)    a 2 * [truediv] cons app2\n",
59     "    -b -b+sqrt(b^2-4ac)    -b-sqrt(b^2-4ac)    2a    [truediv] cons app2\n",
60     "    -b -b+sqrt(b^2-4ac)    -b-sqrt(b^2-4ac)    [2a truediv]         app2\n",
61     "    -b -b+sqrt(b^2-4ac)/2a -b-sqrt(b^2-4ac)/2a\n",
62     "### Codicil\n",
63     "    -b -b+sqrt(b^2-4ac)/2a -b-sqrt(b^2-4ac)/2a                          roll< pop\n",
64     "       -b+sqrt(b^2-4ac)/2a -b-sqrt(b^2-4ac)/2a -b                             pop\n",
65     "       -b+sqrt(b^2-4ac)/2a -b-sqrt(b^2-4ac)/2a"
66    ]
67   },
68   {
69    "cell_type": "markdown",
70    "metadata": {},
71    "source": [
72     "# Derive a definition.\n",
73     "\n",
74     "    b neg b           sqr 4 a c        * * - sqrt [+] [-] cleave a       2 * [truediv] cons app2 roll< pop\n",
75     "    b    [neg] dupdip sqr 4 a c        * * - sqrt [+] [-] cleave a       2 * [truediv] cons app2 roll< pop\n",
76     "    b a c    [[neg] dupdip sqr 4] dipd * * - sqrt [+] [-] cleave a       2 * [truediv] cons app2 roll< pop\n",
77     "    b a c a    [[[neg] dupdip sqr 4] dipd * * - sqrt [+] [-] cleave] dip 2 * [truediv] cons app2 roll< pop\n",
78     "    b a c over [[[neg] dupdip sqr 4] dipd * * - sqrt [+] [-] cleave] dip 2 * [truediv] cons app2 roll< pop"
79    ]
80   },
81   {
82    "cell_type": "code",
83    "execution_count": 2,
84    "metadata": {},
85    "outputs": [],
86    "source": [
87     "define('quadratic == over [[[neg] dupdip sqr 4] dipd * * - sqrt [+] [-] cleave] dip 2 * [truediv] cons app2 roll< pop')"
88    ]
89   },
90   {
91    "cell_type": "code",
92    "execution_count": 3,
93    "metadata": {},
94    "outputs": [
95     {
96      "name": "stdout",
97      "output_type": "stream",
98      "text": [
99       "-0.3819660112501051 -2.618033988749895\n"
100      ]
101     }
102    ],
103    "source": [
104     "J('3 1 1 quadratic')"
105    ]
106   },
107   {
108    "cell_type": "markdown",
109    "metadata": {},
110    "source": [
111     "### Simplify\n",
112     "We can define a `pm` plus-or-minus function:"
113    ]
114   },
115   {
116    "cell_type": "code",
117    "execution_count": 4,
118    "metadata": {},
119    "outputs": [],
120    "source": [
121     "define('pm == [+] [-] cleave popdd')"
122    ]
123   },
124   {
125    "cell_type": "markdown",
126    "metadata": {},
127    "source": [
128     "Then `quadratic` becomes:"
129    ]
130   },
131   {
132    "cell_type": "code",
133    "execution_count": 5,
134    "metadata": {},
135    "outputs": [],
136    "source": [
137     "define('quadratic == over [[[neg] dupdip sqr 4] dipd * * - sqrt pm] dip 2 * [truediv] cons app2')"
138    ]
139   },
140   {
141    "cell_type": "code",
142    "execution_count": 6,
143    "metadata": {},
144    "outputs": [
145     {
146      "name": "stdout",
147      "output_type": "stream",
148      "text": [
149       "-0.3819660112501051 -2.618033988749895\n"
150      ]
151     }
152    ],
153    "source": [
154     "J('3 1 1 quadratic')"
155    ]
156   },
157   {
158    "cell_type": "markdown",
159    "metadata": {},
160    "source": [
161     "### Define a \"native\" `pm` function.\n",
162     "The definition of `pm` above is pretty elegant, but the implementation takes a lot of steps relative to what it's accomplishing.  Since we are likely to use `pm` more than once in the future, let's write a primitive in Python and add it to the dictionary."
163    ]
164   },
165   {
166    "cell_type": "code",
167    "execution_count": 7,
168    "metadata": {},
169    "outputs": [],
170    "source": [
171     "from joy.library import SimpleFunctionWrapper\n",
172     "from notebook_preamble import D\n",
173     "\n",
174     "\n",
175     "@SimpleFunctionWrapper\n",
176     "def pm(stack):\n",
177     "    a, (b, stack) = stack\n",
178     "    p, m, = b + a, b - a\n",
179     "    return m, (p, stack)\n",
180     "\n",
181     "\n",
182     "D['pm'] = pm"
183    ]
184   },
185   {
186    "cell_type": "markdown",
187    "metadata": {},
188    "source": [
189     "The resulting trace is short enough to fit on a page."
190    ]
191   },
192   {
193    "cell_type": "code",
194    "execution_count": 8,
195    "metadata": {},
196    "outputs": [
197     {
198      "name": "stdout",
199      "output_type": "stream",
200      "text": [
201       "                                                    . 3 1 1 quadratic\n",
202       "                                                  3 . 1 1 quadratic\n",
203       "                                                3 1 . 1 quadratic\n",
204       "                                              3 1 1 . quadratic\n",
205       "                                              3 1 1 . over [[[neg] dupdip sqr 4] dipd * * - sqrt pm] dip 2 * [truediv] cons app2\n",
206       "                                            3 1 1 1 . [[[neg] dupdip sqr 4] dipd * * - sqrt pm] dip 2 * [truediv] cons app2\n",
207       "  3 1 1 1 [[[neg] dupdip sqr 4] dipd * * - sqrt pm] . dip 2 * [truediv] cons app2\n",
208       "                                              3 1 1 . [[neg] dupdip sqr 4] dipd * * - sqrt pm 1 2 * [truediv] cons app2\n",
209       "                         3 1 1 [[neg] dupdip sqr 4] . dipd * * - sqrt pm 1 2 * [truediv] cons app2\n",
210       "                                                  3 . [neg] dupdip sqr 4 1 1 * * - sqrt pm 1 2 * [truediv] cons app2\n",
211       "                                            3 [neg] . dupdip sqr 4 1 1 * * - sqrt pm 1 2 * [truediv] cons app2\n",
212       "                                                  3 . neg 3 sqr 4 1 1 * * - sqrt pm 1 2 * [truediv] cons app2\n",
213       "                                                 -3 . 3 sqr 4 1 1 * * - sqrt pm 1 2 * [truediv] cons app2\n",
214       "                                               -3 3 . sqr 4 1 1 * * - sqrt pm 1 2 * [truediv] cons app2\n",
215       "                                               -3 3 . dup mul 4 1 1 * * - sqrt pm 1 2 * [truediv] cons app2\n",
216       "                                             -3 3 3 . mul 4 1 1 * * - sqrt pm 1 2 * [truediv] cons app2\n",
217       "                                               -3 9 . 4 1 1 * * - sqrt pm 1 2 * [truediv] cons app2\n",
218       "                                             -3 9 4 . 1 1 * * - sqrt pm 1 2 * [truediv] cons app2\n",
219       "                                           -3 9 4 1 . 1 * * - sqrt pm 1 2 * [truediv] cons app2\n",
220       "                                         -3 9 4 1 1 . * * - sqrt pm 1 2 * [truediv] cons app2\n",
221       "                                           -3 9 4 1 . * - sqrt pm 1 2 * [truediv] cons app2\n",
222       "                                             -3 9 4 . - sqrt pm 1 2 * [truediv] cons app2\n",
223       "                                               -3 5 . sqrt pm 1 2 * [truediv] cons app2\n",
224       "                                -3 2.23606797749979 . pm 1 2 * [truediv] cons app2\n",
225       "              -0.7639320225002102 -5.23606797749979 . 1 2 * [truediv] cons app2\n",
226       "            -0.7639320225002102 -5.23606797749979 1 . 2 * [truediv] cons app2\n",
227       "          -0.7639320225002102 -5.23606797749979 1 2 . * [truediv] cons app2\n",
228       "            -0.7639320225002102 -5.23606797749979 2 . [truediv] cons app2\n",
229       "  -0.7639320225002102 -5.23606797749979 2 [truediv] . cons app2\n",
230       "  -0.7639320225002102 -5.23606797749979 [2 truediv] . app2\n",
231       "                  [-0.7639320225002102] [2 truediv] . infra first [-5.23606797749979] [2 truediv] infra first\n",
232       "                                -0.7639320225002102 . 2 truediv [] swaack first [-5.23606797749979] [2 truediv] infra first\n",
233       "                              -0.7639320225002102 2 . truediv [] swaack first [-5.23606797749979] [2 truediv] infra first\n",
234       "                                -0.3819660112501051 . [] swaack first [-5.23606797749979] [2 truediv] infra first\n",
235       "                             -0.3819660112501051 [] . swaack first [-5.23606797749979] [2 truediv] infra first\n",
236       "                              [-0.3819660112501051] . first [-5.23606797749979] [2 truediv] infra first\n",
237       "                                -0.3819660112501051 . [-5.23606797749979] [2 truediv] infra first\n",
238       "            -0.3819660112501051 [-5.23606797749979] . [2 truediv] infra first\n",
239       "-0.3819660112501051 [-5.23606797749979] [2 truediv] . infra first\n",
240       "                                  -5.23606797749979 . 2 truediv [-0.3819660112501051] swaack first\n",
241       "                                -5.23606797749979 2 . truediv [-0.3819660112501051] swaack first\n",
242       "                                 -2.618033988749895 . [-0.3819660112501051] swaack first\n",
243       "           -2.618033988749895 [-0.3819660112501051] . swaack first\n",
244       "           -0.3819660112501051 [-2.618033988749895] . first\n",
245       "             -0.3819660112501051 -2.618033988749895 . \n"
246      ]
247     }
248    ],
249    "source": [
250     "V('3 1 1 quadratic')"
251    ]
252   }
253  ],
254  "metadata": {
255   "kernelspec": {
256    "display_name": "Python 2",
257    "language": "python",
258    "name": "python2"
259   },
260   "language_info": {
261    "codemirror_mode": {
262     "name": "ipython",
263     "version": 2
264    },
265    "file_extension": ".py",
266    "mimetype": "text/x-python",
267    "name": "python",
268    "nbconvert_exporter": "python",
269    "pygments_lexer": "ipython2",
270    "version": "2.7.13"
271   }
272  },
273  "nbformat": 4,
274  "nbformat_minor": 2
275 }