{ "cells": [ { "cell_type": "code", "execution_count": 2, "metadata": { "ExecuteTime": { "end_time": "2021-01-12T16:11:12.111639Z", "start_time": "2021-01-12T16:11:11.914041Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Populating the interactive namespace from numpy and matplotlib\n" ] } ], "source": [ "%pylab inline" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Notebook magic" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "ExecuteTime": { "end_time": "2022-01-29T20:50:20.813162Z", "start_time": "2022-01-29T20:50:20.794562Z" } }, "outputs": [], "source": [ "from IPython.core.magic import Magics, magics_class, line_cell_magic\n", "from IPython.core.magic import cell_magic, register_cell_magic, register_line_magic\n", "from IPython.core.magic_arguments import argument, magic_arguments, parse_argstring\n", "import subprocess\n", "import os" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "ExecuteTime": { "end_time": "2022-01-29T20:50:21.613220Z", "start_time": "2022-01-29T20:50:21.557819Z" } }, "outputs": [], "source": [ "@magics_class\n", "class PyboardMagic(Magics):\n", " @cell_magic\n", " @magic_arguments()\n", " @argument('-skip')\n", " @argument('-unix')\n", " @argument('-pyboard')\n", " @argument('-file')\n", " @argument('-data')\n", " @argument('-time')\n", " @argument('-memory')\n", " def micropython(self, line='', cell=None):\n", " args = parse_argstring(self.micropython, line)\n", " if args.skip: # doesn't care about the cell's content\n", " print('skipped execution')\n", " return None # do not parse the rest\n", " if args.unix: # tests the code on the unix port. Note that this works on unix only\n", " with open('/dev/shm/micropython.py', 'w') as fout:\n", " fout.write(cell)\n", " proc = subprocess.Popen([\"../micropython/ports/unix/micropython-2\", \"/dev/shm/micropython.py\"], \n", " stdout=subprocess.PIPE, stderr=subprocess.PIPE)\n", " print(proc.stdout.read().decode(\"utf-8\"))\n", " print(proc.stderr.read().decode(\"utf-8\"))\n", " return None\n", " if args.file: # can be used to copy the cell content onto the pyboard's flash\n", " spaces = \" \"\n", " try:\n", " with open(args.file, 'w') as fout:\n", " fout.write(cell.replace('\\t', spaces))\n", " printf('written cell to {}'.format(args.file))\n", " except:\n", " print('Failed to write to disc!')\n", " return None # do not parse the rest\n", " if args.data: # can be used to load data from the pyboard directly into kernel space\n", " message = pyb.exec(cell)\n", " if len(message) == 0:\n", " print('pyboard >>>')\n", " else:\n", " print(message.decode('utf-8'))\n", " # register new variable in user namespace\n", " self.shell.user_ns[args.data] = string_to_matrix(message.decode(\"utf-8\"))\n", " \n", " if args.time: # measures the time of executions\n", " pyb.exec('import utime')\n", " message = pyb.exec('t = utime.ticks_us()\\n' + cell + '\\ndelta = utime.ticks_diff(utime.ticks_us(), t)' + \n", " \"\\nprint('execution time: {:d} us'.format(delta))\")\n", " print(message.decode('utf-8'))\n", " \n", " if args.memory: # prints out memory information \n", " message = pyb.exec('from micropython import mem_info\\nprint(mem_info())\\n')\n", " print(\"memory before execution:\\n========================\\n\", message.decode('utf-8'))\n", " message = pyb.exec(cell)\n", " print(\">>> \", message.decode('utf-8'))\n", " message = pyb.exec('print(mem_info())')\n", " print(\"memory after execution:\\n========================\\n\", message.decode('utf-8'))\n", "\n", " if args.pyboard:\n", " message = pyb.exec(cell)\n", " print(message.decode('utf-8'))\n", "\n", "ip = get_ipython()\n", "ip.register_magics(PyboardMagic)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## pyboard" ] }, { "cell_type": "code", "execution_count": 57, "metadata": { "ExecuteTime": { "end_time": "2020-05-07T07:35:35.126401Z", "start_time": "2020-05-07T07:35:35.105824Z" } }, "outputs": [], "source": [ "import pyboard\n", "pyb = pyboard.Pyboard('/dev/ttyACM0')\n", "pyb.enter_raw_repl()" ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "ExecuteTime": { "end_time": "2020-05-19T19:11:18.145548Z", "start_time": "2020-05-19T19:11:18.137468Z" } }, "outputs": [], "source": [ "pyb.exit_raw_repl()\n", "pyb.close()" ] }, { "cell_type": "code", "execution_count": 58, "metadata": { "ExecuteTime": { "end_time": "2020-05-07T07:35:38.725924Z", "start_time": "2020-05-07T07:35:38.645488Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n" ] } ], "source": [ "%%micropython -pyboard 1\n", "\n", "import utime\n", "import ulab as np\n", "\n", "def timeit(n=1000):\n", " def wrapper(f, *args, **kwargs):\n", " func_name = str(f).split(' ')[1]\n", " def new_func(*args, **kwargs):\n", " run_times = np.zeros(n, dtype=np.uint16)\n", " for i in range(n):\n", " t = utime.ticks_us()\n", " result = f(*args, **kwargs)\n", " run_times[i] = utime.ticks_diff(utime.ticks_us(), t)\n", " print('{}() execution times based on {} cycles'.format(func_name, n, (delta2-delta1)/n))\n", " print('\\tbest: %d us'%np.min(run_times))\n", " print('\\tworst: %d us'%np.max(run_times))\n", " print('\\taverage: %d us'%np.mean(run_times))\n", " print('\\tdeviation: +/-%.3f us'%np.std(run_times)) \n", " return result\n", " return new_func\n", " return wrapper\n", "\n", "def timeit(f, *args, **kwargs):\n", " func_name = str(f).split(' ')[1]\n", " def new_func(*args, **kwargs):\n", " t = utime.ticks_us()\n", " result = f(*args, **kwargs)\n", " print('execution time: ', utime.ticks_diff(utime.ticks_us(), t), ' us')\n", " return result\n", " return new_func" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "__END_OF_DEFS__" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# scipy.integrate\n", "\n", "This module provides a simplified subset of CPython's `scipy.integrate` module. The algorithms were not ported from CPython's `scipy.integrate` for the sake of resource usage, but derived from a paper found in https://www.genivia.com/qthsh.html. There are four numerical integration algorithms:\n", "\n", "1. [scipy.integrate.quad](#quad)\n", "2. [scipy.integrate.romberg](#romberg)\n", "3. [scipy.integrate.simpson](#simpson)\n", "4. [scipy.integrate.tanhsinh](#tanhsinh)\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Introduction\n", "\n", "Numerical integration works best with float64 math enabled. If you require float64 math, be sure to set `MICROPY_OBJ_REPR_A` and `MICROPY_FLOAT_IMPL_DOUBLE`. This being said, the modules work equally well using float32, albeit with reduced precision. The required error tolerance can be specified for each of the function calls using the \"eps=\" option, defaulting to the compiled in `etolerance` value (1e-14 for fp64, 1e-8 for fp32).\n", "\n", "The submodule can be enabled by setting `ULAB_SCIPY_HAS_INTEGRATE_MODULE` in `code/ulab.h`. As for the individual integration algorithms, you can select which to include by setting one or more of `ULAB_INTEGRATE_HAS_QUAD`, `ULAB_INTEGRATE_HAS_ROMBERG`, `ULAB_INTEGRATE_HAS_SIMPSON`, and `ULAB_INTEGRATE_HAS_TANHSINH`.\n", "\n", "Also note that these algorithms do not support complex numbers, although it is certainly possible to implement complex integration in MicroPython on top of this module, e.g. as in https://stackoverflow.com/questions/5965583/use-scipy-integrate-quad-to-integrate-complex-numbers. " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## quad\n", "\n", "`scipy`: https://docs.scipy.org/doc/scipy/reference/generated/scipy.integrate.quad.html \n", "\n", "In CPython `scipy.integrate`, `quad` is a wrapper implementing many algorithms based on the Fortran QUADPACK package. Gauss-Kronrod is just one of them, and it is useful for most general-purpose tasks. This particular function implements an Adaptive Gauss-Kronrod (G10,K21) quadrature algorithm. The Gauss–Kronrod quadrature formula is a variant of Gaussian quadrature, in which the evaluation points are chosen so that an accurate approximation can be computed by re-using the information produced by the computation of a less accurate approximation (https://en.wikipedia.org/wiki/Gauss%E2%80%93Kronrod_quadrature_formula). \n", "\n", "The function takes three to five arguments: \n", "\n", "* f, a callable,\n", "* a and b, the lower and upper integration limit, \n", "* order=, the order of integration (default 5),\n", "* eps=, the error tolerance (default etolerance) \n", "\n", "The function returns the result and the error estimate as a tuple of floats. " ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "ExecuteTime": { "end_time": "2020-06-19T20:24:10.529668Z", "start_time": "2020-06-19T20:24:10.520389Z" } }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "UsageError: Cell magic `%%micropython` not found.\n" ] } ], "source": [ "%%micropython -unix 1\n", "\n", "from ulab import scipy\n", "\n", "f = lambda x: x**2 + 2*x + 1\n", "result = scipy.integrate.quad(f, 0, 5, order=5, eps=1e-10)\n", "print (f\"result = {result}\")\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## romberg\n", "\n", "`scipy`: https://docs.scipy.org/doc/scipy/reference/generated/scipy.integrate.romberg.html \n", "\n", "This function implements the Romberg quadrature algorithm. Romberg's method is a Newton–Cotes formula – it evaluates the integrand at equally spaced points. The integrand must have continuous derivatives, though fairly good results may be obtained if only a few derivatives exist. If it is possible to evaluate the integrand at unequally spaced points, then other methods such as Gaussian quadrature and Clenshaw–Curtis quadrature are generally more accurate (https://en.wikipedia.org/wiki/Romberg%27s_method). \n", "\n", "Please note: This function is deprecated as of SciPy 1.12.0 and will be removed in SciPy 1.15.0. Please use `scipy.integrate.quad` instead. \n", "\n", "The function takes three to five arguments: \n", "\n", "* f, a callable,\n", "* a and b, the lower and upper integration limit, \n", "* steps=, the number of steps taken to calculate (default 100),\n", "* eps=, the error tolerance (default etolerance) \n", "\n", "The function returns the result as a float.\n" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "UsageError: Cell magic `%%micropython` not found.\n" ] } ], "source": [ "%%micropython -unix 1\n", "\n", "from ulab import scipy\n", "\n", "f = lambda x: x**2 + 2*x + 1\n", "result = scipy.integrate.romberg(f, 0, 5)\n", "print (f\"result = {result}\")\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## simpson\n", "\n", "`scipy`: https://docs.scipy.org/doc/scipy/reference/generated/scipy.integrate.simpson.html \n", "\n", "This function is different from CPython's `simpson` method in that it does not take an array of function values but determines the optimal spacing of samples itself. Adaptive Simpson's method, also called adaptive Simpson's rule, is a method of numerical integration proposed by G.F. Kuncir in 1962. It is probably the first recursive adaptive algorithm for numerical integration to appear in print, although more modern adaptive methods based on Gauss–Kronrod quadrature and Clenshaw–Curtis quadrature are now generally preferred (https://en.wikipedia.org/wiki/Adaptive_Simpson%27s_method). \n", "\n", "The function takes three to five arguments: \n", "\n", "* f, a callable,\n", "* a and b, the lower and upper integration limit, \n", "* steps=, the number of steps taken to calculate (default 100),\n", "* eps=, the error tolerance (default etolerance) \n", "\n", "The function returns the result as a float." ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "UsageError: Cell magic `%%micropython` not found.\n" ] } ], "source": [ "%%micropython -unix 1\n", "\n", "from ulab import scipy\n", "\n", "f = lambda x: x**2 + 2*x + 1\n", "result = scipy.integrate.simpson(f, 0, 5)\n", "print (f\"result = {result}\")\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## tanhsinh\n", "\n", "`scipy`: https://docs.scipy.org/doc/scipy/reference/generated/scipy.integrate.quad.html \n", "\n", "In CPython `scipy.integrate`, `tanhsinh` is written in Python (https://github.com/scipy/scipy/blob/main/scipy/integrate/_tanhsinh.py). It is used in cases where Newton-Cotes, Gauss-Kronrod, and other formulae do not work due to properties of the integrand or the integration limits. (In SciPy v1.14.1, it is not a public function but it has been marked as public in SciPy v1.15.0rc1). \n", "\n", "This particular function implements an optimized Tanh-Sinh, Sinh-Sinh and Exp-Sinh quadrature algorithm. It is especially applied where singularities or infinite derivatives exist at one or both endpoints. The method uses hyperbolic functions in a change of variables to transform an integral on the interval x ∈ (−1, 1) to an integral on the entire real line t ∈ (−∞, ∞), the two integrals having the same value. After this transformation, the integrand decays with a double exponential rate, and thus, this method is also known as the double exponential (DE) formula (https://en.wikipedia.org/wiki/Tanh-sinh_quadrature). \n", "\n", "As opposed to the three algorithms mentioned before, it also supports integrals with infinite limits like the Gaussian integral (https://en.wikipedia.org/wiki/Gaussian_integral), as shown below. \n", "\n", "The function takes three to five arguments: \n", "\n", "* f, a callable,\n", "* a and b, the lower and upper integration limit, \n", "* levels=, the number of loops taken to calculate (default 6),\n", "* eps=, the error tolerance (default: etolerance)\n", "\n", "The function returns the result and the error estimate as a tuple of floats.\n" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "UsageError: Cell magic `%%micropython` not found.\n" ] } ], "source": [ "%%micropython -unix 1\n", "\n", "from ulab import scipy, numpy as np\n", "from math import *\n", "f = lambda x: exp(- x**2)\n", "result = scipy.integrate.tanhsinh(f, -np.inf, np.inf)\n", "print (f\"result = {result}\")\n", "exact = sqrt(pi) # which is the exact value\n", "print (f\"exact value = {exact}\")\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.12.3" }, "toc": { "base_numbering": 1, "nav_menu": {}, "number_sections": true, "sideBar": true, "skip_h1_title": false, "title_cell": "Table of Contents", "title_sidebar": "Contents", "toc_cell": false, "toc_position": { "height": "calc(100% - 180px)", "left": "10px", "top": "150px", "width": "382.797px" }, "toc_section_display": true, "toc_window_display": true }, "varInspector": { "cols": { "lenName": 16, "lenType": 16, "lenVar": 40 }, "kernels_config": { "python": { "delete_cmd_postfix": "", "delete_cmd_prefix": "del ", "library": "var_list.py", "varRefreshCmd": "print(var_dic_list())" }, "r": { "delete_cmd_postfix": ") ", "delete_cmd_prefix": "rm(", "library": "var_list.r", "varRefreshCmd": "cat(var_dic_list()) " } }, "types_to_exclude": [ "module", "function", "builtin_function_or_method", "instance", "_Feature" ], "window_display": false } }, "nbformat": 4, "nbformat_minor": 4 }