diff --git a/docs/Newton-Raphson.ipynb b/docs/Newton-Raphson.ipynb index 706da9e..63da387 100644 --- a/docs/Newton-Raphson.ipynb +++ b/docs/Newton-Raphson.ipynb @@ -74,18 +74,43 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "Let's try out this function `over / + 2 /` on an example:" + "Let's try out this function `over / + 2 /` on an example:\n", + "\n", + " F == over / + 2 /" ] }, { "cell_type": "code", - "execution_count": 14, + "execution_count": 1, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [] + } + ], + "source": [ + "[F over / + 2 /] inscribe" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "In order to use this function `F` we have to provide an initial estimate for the value of the square root, and we want to keep the input value `n` handy for iterations (we don't want the user to have to keep reentering it.)" + ] + }, + { + "cell_type": "code", + "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ + " 5 36 • F\n", " 5 36 • over / + 2 /\n", "5 36 5 • / + 2 /\n", " 5 7 • + 2 /\n", @@ -100,16 +125,368 @@ "source": [ "clear\n", "\n", - "5 36 [over / + 2 /] trace" + "5 36 [F] trace" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "The function we want has the argument `n` in it:\n", + "The initial estimate can be 2, and we can `cons` the input value onto a quote with `F`:" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "6" + ] + } + ], + "source": [ + "[F1 2 swap [F] cons] inscribe" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + " 36 • F1\n", + " 36 • 2 swap [F] cons\n", + " 36 2 • swap [F] cons\n", + " 2 36 • [F] cons\n", + "2 36 [F] • cons\n", + "2 [36 F] • \n", + "\n", + "2 [36 F]" + ] + } + ], + "source": [ + "clear\n", "\n", - " n over / + 2 /" + "36 [F1] trace" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + " 2 • 36 F\n", + " 2 36 • F\n", + " 2 36 • over / + 2 /\n", + "2 36 2 • / + 2 /\n", + " 2 18 • + 2 /\n", + " 20 • 2 /\n", + " 20 2 • /\n", + " 10 • \n", + "\n", + "10" + ] + } + ], + "source": [ + "trace" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "6" + ] + } + ], + "source": [ + "36 F" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [] + } + ], + "source": [ + "clear" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[2 [36 F] codireco]" + ] + } + ], + "source": [ + "36 F1 make_generator" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "6" + ] + } + ], + "source": [ + "x x x first" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "6 12" + ] + } + ], + "source": [ + "144 F1 make_generator x x x x first" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "2 [36 F]" + ] + } + ], + "source": [ + "clear\n", + "\n", + "2 [36 F]" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "metadata": { + "scrolled": true + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "2 [36 F] false" + ] + } + ], + "source": [ + "[first] [pop sqr] fork - abs 3 <" + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "10" + ] + } + ], + "source": [ + "pop i" + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "metadata": { + "scrolled": true + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "10 [36 F] false" + ] + } + ], + "source": [ + "[36 F] [first] [pop sqr] fork - abs 3 <" + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "6" + ] + } + ], + "source": [ + "pop i" + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "metadata": { + "scrolled": true + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "6 [36 F] true" + ] + } + ], + "source": [ + "[36 F] [first] [pop sqr] fork - abs 3 <" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": 17, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "2" + ] + } + ], + "source": [ + "clear\n", + "\n", + "2" + ] + }, + { + "cell_type": "code", + "execution_count": 18, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "6" + ] + } + ], + "source": [ + "[] true [i [36 F] [first] [pop sqr] fork - abs 3 >] loop pop" + ] + }, + { + "cell_type": "code", + "execution_count": 19, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "12" + ] + } + ], + "source": [ + "clear\n", + "\n", + "7 [] true [i [144 F] [first] [pop sqr] fork - abs 3 >] loop pop" + ] + }, + { + "cell_type": "code", + "execution_count": 20, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "120" + ] + } + ], + "source": [ + "clear\n", + "\n", + "7 [] true [i [14400 F] [first] [pop sqr] fork - abs 3 >] loop pop" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "broken due to no float div\n", + "\n", + " clear\n", + "\n", + " 7 [] true [i [1000 F] [first] [pop sqr] fork - abs 10 >] loop pop" ] }, { @@ -140,7 +517,7 @@ }, { "cell_type": "code", - "execution_count": 2, + "execution_count": null, "metadata": { "scrolled": true }, @@ -151,17 +528,9 @@ }, { "cell_type": "code", - "execution_count": 3, + "execution_count": null, "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "[1 [dup 23 over / + 2 /] codireco]\n" - ] - } - ], + "outputs": [], "source": [ "J('23 gsra')" ] @@ -175,17 +544,9 @@ }, { "cell_type": "code", - "execution_count": 4, + "execution_count": null, "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "23.0000000001585\n" - ] - } - ], + "outputs": [], "source": [ "J('23 gsra 6 [x popd] times first sqr')" ] @@ -232,7 +593,7 @@ }, { "cell_type": "code", - "execution_count": 5, + "execution_count": null, "metadata": {}, "outputs": [], "source": [ @@ -253,7 +614,7 @@ }, { "cell_type": "code", - "execution_count": 6, + "execution_count": null, "metadata": {}, "outputs": [], "source": [ @@ -286,7 +647,7 @@ }, { "cell_type": "code", - "execution_count": 7, + "execution_count": null, "metadata": {}, "outputs": [], "source": [ @@ -307,7 +668,7 @@ }, { "cell_type": "code", - "execution_count": 8, + "execution_count": null, "metadata": {}, "outputs": [], "source": [ @@ -324,38 +685,22 @@ }, { "cell_type": "code", - "execution_count": 9, + "execution_count": null, "metadata": { "scrolled": true }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "6.0\n" - ] - } - ], + "outputs": [], "source": [ "J('36 sqrt')" ] }, { "cell_type": "code", - "execution_count": 10, + "execution_count": null, "metadata": { "scrolled": true }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "4.795831523312719\n" - ] - } - ], + "outputs": [], "source": [ "J('23 sqrt')" ] @@ -369,42 +714,20 @@ }, { "cell_type": "code", - "execution_count": 11, + "execution_count": null, "metadata": { "scrolled": true }, - "outputs": [ - { - "data": { - "text/plain": [ - "22.999999999999996" - ] - }, - "execution_count": 11, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "4.795831523312719**2" ] }, { "cell_type": "code", - "execution_count": 12, + "execution_count": null, "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "4.795831523312719" - ] - }, - "execution_count": 12, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "from math import sqrt\n", "\n",