{ "cells": [ { "cell_type": "markdown", "id": "5a09a7f6", "metadata": {}, "source": [ "# Exponencial function\n", "\n", "In this tutoial we show a few Padé approximants of $e^{x}\\,$ using infinite and finite precision." ] }, { "cell_type": "code", "execution_count": 119, "id": "28869892", "metadata": {}, "outputs": [], "source": [ "import sympy as sp\n", "from padepy import baker_algorithm as ba\n", "from padepy import direct_algorithm as da" ] }, { "cell_type": "code", "execution_count": 123, "id": "3c3818e7", "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$\\displaystyle e^{x}$" ], "text/plain": [ "exp(x)" ] }, "execution_count": 123, "metadata": {}, "output_type": "execute_result" } ], "source": [ "var = sp.Symbol(\"x\")\n", "func = sp.exp(var)\n", "p = 3\n", "q = 3\n", "func" ] }, { "cell_type": "code", "execution_count": 124, "id": "f2fafb2d", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Pade [3,3](x) stored at matrix index 6.\n" ] }, { "data": { "text/latex": [ "$\\displaystyle \\frac{\\frac{x^{3}}{120} + \\frac{x^{2}}{10} + \\frac{x}{2} + 1}{- \\frac{x^{3}}{120} + \\frac{x^{2}}{10} - \\frac{x}{2} + 1}$" ], "text/plain": [ "(x**3/120 + x**2/10 + x/2 + 1)/(-x**3/120 + x**2/10 - x/2 + 1)" ] }, "execution_count": 124, "metadata": {}, "output_type": "execute_result" } ], "source": [ "pade, path = ba.pade(p, q, var, func, 0, False)\n", "pade" ] }, { "cell_type": "code", "execution_count": 125, "id": "7d43f619", "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$\\displaystyle \\left[\\begin{matrix}\\frac{x^{6}}{720} + \\frac{x^{5}}{120} + \\frac{x^{4}}{24} + \\frac{x^{3}}{6} + \\frac{x^{2}}{2} + x + 1\\\\\\frac{x^{5}}{120} + \\frac{x^{4}}{24} + \\frac{x^{3}}{6} + \\frac{x^{2}}{2} + x + 1\\\\\\frac{\\frac{x^{5}}{720} + \\frac{x^{4}}{72} + \\frac{x^{3}}{12} + \\frac{x^{2}}{3} + \\frac{5 x}{6} + 1}{1 - \\frac{x}{6}}\\\\\\frac{\\frac{x^{4}}{120} + \\frac{x^{3}}{15} + \\frac{3 x^{2}}{10} + \\frac{4 x}{5} + 1}{1 - \\frac{x}{5}}\\\\\\frac{\\frac{x^{4}}{360} + \\frac{x^{3}}{30} + \\frac{x^{2}}{5} + \\frac{2 x}{3} + 1}{\\frac{x^{2}}{30} - \\frac{x}{3} + 1}\\\\\\frac{\\frac{x^{3}}{60} + \\frac{3 x^{2}}{20} + \\frac{3 x}{5} + 1}{\\frac{x^{2}}{20} - \\frac{2 x}{5} + 1}\\\\\\frac{\\frac{x^{3}}{120} + \\frac{x^{2}}{10} + \\frac{x}{2} + 1}{- \\frac{x^{3}}{120} + \\frac{x^{2}}{10} - \\frac{x}{2} + 1}\\\\\\frac{\\frac{x^{2}}{20} + \\frac{2 x}{5} + 1}{- \\frac{x^{3}}{60} + \\frac{3 x^{2}}{20} - \\frac{3 x}{5} + 1}\\\\\\frac{\\frac{x^{2}}{30} + \\frac{x}{3} + 1}{\\frac{x^{4}}{360} - \\frac{x^{3}}{30} + \\frac{x^{2}}{5} - \\frac{2 x}{3} + 1}\\\\\\frac{\\frac{x}{5} + 1}{\\frac{x^{4}}{120} - \\frac{x^{3}}{15} + \\frac{3 x^{2}}{10} - \\frac{4 x}{5} + 1}\\\\\\frac{\\frac{x}{6} + 1}{- \\frac{x^{5}}{720} + \\frac{x^{4}}{72} - \\frac{x^{3}}{12} + \\frac{x^{2}}{3} - \\frac{5 x}{6} + 1}\\\\\\frac{1}{- \\frac{x^{5}}{120} + \\frac{x^{4}}{24} - \\frac{x^{3}}{6} + \\frac{x^{2}}{2} - x + 1}\\\\\\frac{1}{\\frac{x^{6}}{720} - \\frac{x^{5}}{120} + \\frac{x^{4}}{24} - \\frac{x^{3}}{6} + \\frac{x^{2}}{2} - x + 1}\\end{matrix}\\right]$" ], "text/plain": [ "Matrix([\n", "[ x**6/720 + x**5/120 + x**4/24 + x**3/6 + x**2/2 + x + 1],\n", "[ x**5/120 + x**4/24 + x**3/6 + x**2/2 + x + 1],\n", "[ (x**5/720 + x**4/72 + x**3/12 + x**2/3 + 5*x/6 + 1)/(1 - x/6)],\n", "[ (x**4/120 + x**3/15 + 3*x**2/10 + 4*x/5 + 1)/(1 - x/5)],\n", "[ (x**4/360 + x**3/30 + x**2/5 + 2*x/3 + 1)/(x**2/30 - x/3 + 1)],\n", "[ (x**3/60 + 3*x**2/20 + 3*x/5 + 1)/(x**2/20 - 2*x/5 + 1)],\n", "[(x**3/120 + x**2/10 + x/2 + 1)/(-x**3/120 + x**2/10 - x/2 + 1)],\n", "[ (x**2/20 + 2*x/5 + 1)/(-x**3/60 + 3*x**2/20 - 3*x/5 + 1)],\n", "[ (x**2/30 + x/3 + 1)/(x**4/360 - x**3/30 + x**2/5 - 2*x/3 + 1)],\n", "[ (x/5 + 1)/(x**4/120 - x**3/15 + 3*x**2/10 - 4*x/5 + 1)],\n", "[(x/6 + 1)/(-x**5/720 + x**4/72 - x**3/12 + x**2/3 - 5*x/6 + 1)],\n", "[ 1/(-x**5/120 + x**4/24 - x**3/6 + x**2/2 - x + 1)],\n", "[ 1/(x**6/720 - x**5/120 + x**4/24 - x**3/6 + x**2/2 - x + 1)]])" ] }, "execution_count": 125, "metadata": {}, "output_type": "execute_result" } ], "source": [ "path" ] }, { "cell_type": "markdown", "id": "f82b93b2", "metadata": {}, "source": [ "It is possible to input a list with coefficients." ] }, { "cell_type": "code", "execution_count": 126, "id": "73f532ac", "metadata": {}, "outputs": [], "source": [ "from padepy import maclaurin as ma" ] }, { "cell_type": "code", "execution_count": 127, "id": "88ecfbb2", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 1, 1/2, 1/6, 1/24, 1/120, 1/720, 1/5040]" ] }, "execution_count": 127, "metadata": {}, "output_type": "execute_result" } ], "source": [ "n = p + q + 1\n", "coeffs_list = ma.coefficients(n, var, func)\n", "coeffs_list" ] }, { "cell_type": "code", "execution_count": 128, "id": "9d97b9ad", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Pade [3,3](x) stored at matrix index 6.\n" ] }, { "data": { "text/latex": [ "$\\displaystyle \\frac{\\frac{x^{3}}{120} + \\frac{x^{2}}{10} + \\frac{x}{2} + 1}{- \\frac{x^{3}}{120} + \\frac{x^{2}}{10} - \\frac{x}{2} + 1}$" ], "text/plain": [ "(x**3/120 + x**2/10 + x/2 + 1)/(-x**3/120 + x**2/10 - x/2 + 1)" ] }, "execution_count": 128, "metadata": {}, "output_type": "execute_result" } ], "source": [ "pade, path = ba.pade(p, q, var, coeffs_list, 0, False)\n", "pade" ] }, { "cell_type": "markdown", "id": "831cc33e", "metadata": {}, "source": [ "If we set $x=1$ we get an approximation of the value $e = 2.7182818284590452353602874713526624977572470936999595749669676277...$" ] }, { "cell_type": "code", "execution_count": 129, "id": "2406d733", "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$\\displaystyle 2.71830985915493$" ], "text/plain": [ "2.71830985915493" ] }, "execution_count": 129, "metadata": {}, "output_type": "execute_result" } ], "source": [ "e = 2.7182818284590452353602874713526624977572470936999595749669676277\n", "e_approx = pade.subs(var, 1.0)\n", "e_approx " ] }, { "cell_type": "code", "execution_count": 130, "id": "8e6558bf", "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$\\displaystyle 2.80306958844179 \\cdot 10^{-5}$" ], "text/plain": [ "2.80306958844179e-5" ] }, "execution_count": 130, "metadata": {}, "output_type": "execute_result" } ], "source": [ "error = e_approx - e\n", "error" ] }, { "cell_type": "markdown", "id": "0e6050f8", "metadata": {}, "source": [ "We can also replace in path variable." ] }, { "cell_type": "code", "execution_count": 131, "id": "95d0df25", "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$\\displaystyle \\left[\\begin{matrix}2.71805555555556\\\\2.71666666666667\\\\2.71833333333333\\\\2.71875\\\\2.71825396825397\\\\2.71794871794872\\\\2.71830985915493\\\\2.71875\\\\2.7182320441989\\\\2.71698113207547\\\\2.71844660194175\\\\2.72727272727273\\\\2.71698113207547\\end{matrix}\\right]$" ], "text/plain": [ "Matrix([\n", "[2.71805555555556],\n", "[2.71666666666667],\n", "[2.71833333333333],\n", "[ 2.71875],\n", "[2.71825396825397],\n", "[2.71794871794872],\n", "[2.71830985915493],\n", "[ 2.71875],\n", "[ 2.7182320441989],\n", "[2.71698113207547],\n", "[2.71844660194175],\n", "[2.72727272727273],\n", "[2.71698113207547]])" ] }, "execution_count": 131, "metadata": {}, "output_type": "execute_result" } ], "source": [ "e_approx_path = path.subs(var, 1.0)\n", "e_approx_path" ] }, { "cell_type": "markdown", "id": "b9000567", "metadata": {}, "source": [ "It is possible to work with floating precision." ] }, { "cell_type": "code", "execution_count": 132, "id": "8b4020a9", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Pade [3,3](x) stored at matrix index 6.\n" ] }, { "data": { "text/latex": [ "$\\displaystyle \\frac{0.00833333333333333 x^{3} + 0.09999999999999997 x^{2} + 0.4999999999999999 x + 1.0}{- 0.008333333333333339 x^{3} + 0.1 x^{2} - 0.5000000000000001 x + 1.0}$" ], "text/plain": [ "(0.00833333333333333*x**3 + 0.09999999999999997*x**2 + 0.4999999999999999*x + 1.0)/(-0.008333333333333339*x**3 + 0.1*x**2 - 0.5000000000000001*x + 1.0)" ] }, "execution_count": 132, "metadata": {}, "output_type": "execute_result" } ], "source": [ "precision = 16\n", "pade, path = ba.pade(p, q, var, coeffs_list, precision)\n", "pade" ] }, { "cell_type": "code", "execution_count": 133, "id": "c7f214c1", "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$\\displaystyle \\left[\\begin{matrix}0.001388888888888889 x^{6} + 0.008333333333333333 x^{5} + 0.04166666666666667 x^{4} + 0.1666666666666667 x^{3} + 0.5 x^{2} + 1.0 x + 1.0\\\\0.008333333333333333 x^{5} + 0.04166666666666667 x^{4} + 0.1666666666666667 x^{3} + 0.5 x^{2} + 1.0 x + 1.0\\\\\\frac{0.001388888888888889 x^{5} + 0.01388888888888889 x^{4} + 0.08333333333333333 x^{3} + 0.3333333333333333 x^{2} + 0.8333333333333333 x + 1.0}{1.0 - 0.1666666666666667 x}\\\\\\frac{0.008333333333333334 x^{4} + 0.06666666666666667 x^{3} + 0.3 x^{2} + 0.8 x + 1.0}{1.0 - 0.2 x}\\\\\\frac{0.00277777777777778 x^{4} + 0.03333333333333335 x^{3} + 0.2 x^{2} + 0.6666666666666667 x + 1.0}{0.03333333333333332 x^{2} - 0.3333333333333333 x + 1.0}\\\\\\frac{0.01666666666666667 x^{3} + 0.15 x^{2} + 0.6 x + 1.0}{0.05 x^{2} - 0.4 x + 1.0}\\\\\\frac{0.00833333333333333 x^{3} + 0.09999999999999997 x^{2} + 0.4999999999999999 x + 1.0}{- 0.008333333333333339 x^{3} + 0.1 x^{2} - 0.5000000000000001 x + 1.0}\\end{matrix}\\right]$" ], "text/plain": [ "Matrix([\n", "[ 0.001388888888888889*x**6 + 0.008333333333333333*x**5 + 0.04166666666666667*x**4 + 0.1666666666666667*x**3 + 0.5*x**2 + 1.0*x + 1.0],\n", "[ 0.008333333333333333*x**5 + 0.04166666666666667*x**4 + 0.1666666666666667*x**3 + 0.5*x**2 + 1.0*x + 1.0],\n", "[(0.001388888888888889*x**5 + 0.01388888888888889*x**4 + 0.08333333333333333*x**3 + 0.3333333333333333*x**2 + 0.8333333333333333*x + 1.0)/(1.0 - 0.1666666666666667*x)],\n", "[ (0.008333333333333334*x**4 + 0.06666666666666667*x**3 + 0.3*x**2 + 0.8*x + 1.0)/(1.0 - 0.2*x)],\n", "[ (0.00277777777777778*x**4 + 0.03333333333333335*x**3 + 0.2*x**2 + 0.6666666666666667*x + 1.0)/(0.03333333333333332*x**2 - 0.3333333333333333*x + 1.0)],\n", "[ (0.01666666666666667*x**3 + 0.15*x**2 + 0.6*x + 1.0)/(0.05*x**2 - 0.4*x + 1.0)],\n", "[ (0.00833333333333333*x**3 + 0.09999999999999997*x**2 + 0.4999999999999999*x + 1.0)/(-0.008333333333333339*x**3 + 0.1*x**2 - 0.5000000000000001*x + 1.0)]])" ] }, "execution_count": 133, "metadata": {}, "output_type": "execute_result" } ], "source": [ "path" ] }, { "cell_type": "code", "execution_count": 134, "id": "c57c09d1", "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$\\displaystyle 2.71830985915493$" ], "text/plain": [ "2.718309859154930" ] }, "execution_count": 134, "metadata": {}, "output_type": "execute_result" } ], "source": [ "e_approx = pade.subs(var, 1.0)\n", "e_approx" ] }, { "cell_type": "code", "execution_count": 135, "id": "32a47954", "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$\\displaystyle \\left[\\begin{matrix}2.718055555555556\\\\2.716666666666667\\\\2.718333333333333\\\\2.71875\\\\2.718253968253968\\\\2.717948717948718\\\\2.71830985915493\\end{matrix}\\right]$" ], "text/plain": [ "Matrix([\n", "[2.718055555555556],\n", "[2.716666666666667],\n", "[2.718333333333333],\n", "[ 2.71875],\n", "[2.718253968253968],\n", "[2.717948717948718],\n", "[ 2.71830985915493]])" ] }, "execution_count": 135, "metadata": {}, "output_type": "execute_result" } ], "source": [ "e_approx_path = path.subs(var, 1.0)\n", "e_approx_path" ] }, { "cell_type": "markdown", "id": "84102a8c", "metadata": {}, "source": [ "We will use bokeh library to plot. Use: pip install bokeh" ] }, { "cell_type": "code", "execution_count": 136, "id": "85627fdf", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "application/javascript": "(function(root) {\n function now() {\n return new Date();\n }\n\n const force = true;\n\n if (typeof root._bokeh_onload_callbacks === \"undefined\" || force === true) {\n root._bokeh_onload_callbacks = [];\n root._bokeh_is_loading = undefined;\n }\n\nconst JS_MIME_TYPE = 'application/javascript';\n const HTML_MIME_TYPE = 'text/html';\n const EXEC_MIME_TYPE = 'application/vnd.bokehjs_exec.v0+json';\n const CLASS_NAME = 'output_bokeh rendered_html';\n\n /**\n * Render data to the DOM node\n */\n function render(props, node) {\n const script = document.createElement(\"script\");\n node.appendChild(script);\n }\n\n /**\n * Handle when an output is cleared or removed\n */\n function handleClearOutput(event, handle) {\n const cell = handle.cell;\n\n const id = cell.output_area._bokeh_element_id;\n const server_id = cell.output_area._bokeh_server_id;\n // Clean up Bokeh references\n if (id != null && id in Bokeh.index) {\n Bokeh.index[id].model.document.clear();\n delete Bokeh.index[id];\n }\n\n if (server_id !== undefined) {\n // Clean up Bokeh references\n const cmd_clean = \"from bokeh.io.state import curstate; print(curstate().uuid_to_server['\" + server_id + \"'].get_sessions()[0].document.roots[0]._id)\";\n cell.notebook.kernel.execute(cmd_clean, {\n iopub: {\n output: function(msg) {\n const id = msg.content.text.trim();\n if (id in Bokeh.index) {\n Bokeh.index[id].model.document.clear();\n delete Bokeh.index[id];\n }\n }\n }\n });\n // Destroy server and session\n const cmd_destroy = \"import bokeh.io.notebook as ion; ion.destroy_server('\" + server_id + \"')\";\n cell.notebook.kernel.execute(cmd_destroy);\n }\n }\n\n /**\n * Handle when a new output is added\n */\n function handleAddOutput(event, handle) {\n const output_area = handle.output_area;\n const output = handle.output;\n\n // limit handleAddOutput to display_data with EXEC_MIME_TYPE content only\n if ((output.output_type != \"display_data\") || (!Object.prototype.hasOwnProperty.call(output.data, EXEC_MIME_TYPE))) {\n return\n }\n\n const toinsert = output_area.element.find(\".\" + CLASS_NAME.split(' ')[0]);\n\n if (output.metadata[EXEC_MIME_TYPE][\"id\"] !== undefined) {\n toinsert[toinsert.length - 1].firstChild.textContent = output.data[JS_MIME_TYPE];\n // store reference to embed id on output_area\n output_area._bokeh_element_id = output.metadata[EXEC_MIME_TYPE][\"id\"];\n }\n if (output.metadata[EXEC_MIME_TYPE][\"server_id\"] !== undefined) {\n const bk_div = document.createElement(\"div\");\n bk_div.innerHTML = output.data[HTML_MIME_TYPE];\n const script_attrs = bk_div.children[0].attributes;\n for (let i = 0; i < script_attrs.length; i++) {\n toinsert[toinsert.length - 1].firstChild.setAttribute(script_attrs[i].name, script_attrs[i].value);\n toinsert[toinsert.length - 1].firstChild.textContent = bk_div.children[0].textContent\n }\n // store reference to server id on output_area\n output_area._bokeh_server_id = output.metadata[EXEC_MIME_TYPE][\"server_id\"];\n }\n }\n\n function register_renderer(events, OutputArea) {\n\n function append_mime(data, metadata, element) {\n // create a DOM node to render to\n const toinsert = this.create_output_subarea(\n metadata,\n CLASS_NAME,\n EXEC_MIME_TYPE\n );\n this.keyboard_manager.register_events(toinsert);\n // Render to node\n const props = {data: data, metadata: metadata[EXEC_MIME_TYPE]};\n render(props, toinsert[toinsert.length - 1]);\n element.append(toinsert);\n return toinsert\n }\n\n /* Handle when an output is cleared or removed */\n events.on('clear_output.CodeCell', handleClearOutput);\n events.on('delete.Cell', handleClearOutput);\n\n /* Handle when a new output is added */\n events.on('output_added.OutputArea', handleAddOutput);\n\n /**\n * Register the mime type and append_mime function with output_area\n */\n OutputArea.prototype.register_mime_type(EXEC_MIME_TYPE, append_mime, {\n /* Is output safe? */\n safe: true,\n /* Index of renderer in `output_area.display_order` */\n index: 0\n });\n }\n\n // register the mime type if in Jupyter Notebook environment and previously unregistered\n if (root.Jupyter !== undefined) {\n const events = require('base/js/events');\n const OutputArea = require('notebook/js/outputarea').OutputArea;\n\n if (OutputArea.prototype.mime_types().indexOf(EXEC_MIME_TYPE) == -1) {\n register_renderer(events, OutputArea);\n }\n }\n if (typeof (root._bokeh_timeout) === \"undefined\" || force === true) {\n root._bokeh_timeout = Date.now() + 5000;\n root._bokeh_failed_load = false;\n }\n\n const NB_LOAD_WARNING = {'data': {'text/html':\n \"\\n\"+\n \"BokehJS does not appear to have successfully loaded. If loading BokehJS from CDN, this \\n\"+\n \"may be due to a slow or bad network connection. Possible fixes:\\n\"+\n \"
\\n\"+\n \"\\n\"+\n \"from bokeh.resources import INLINE\\n\"+\n \"output_notebook(resources=INLINE)\\n\"+\n \"\\n\"+\n \"