{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Understanding gduals and floats\n", "(by Dario Izzo)\n", "\n", "Arithmetic operators +,-,+,/ and \\** work on a gdual as well as mathematical functions (from pyaudi). But not everything must be a gdual. You can operate between gduals and floats too, and between gdual having different truncation order." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Importing stuff" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "from pyaudi import gdual_double as gdual\n", "from pyaudi import sin, cos, tan" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Arithmetic between floats and gduals" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/latex": [ "\\[ 3.3+{dx}+\\mathcal{O}\\left(4\\right) \\]" ], "text/plain": [ "3.3+dx" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x = gdual(1.2, \"x\", 3) # a gdual\n", "y = 2.1 # a float\n", "f = x + y # this will be a gdual\n", "f" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "NOTE: the gdual f defined above only contains derivative infos w.r.t the variable \"x\". You cannot make derivatives w.r.t \"y\" as y was not defined as a gdual " ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "The result is a float: 4.183307552861899\n", "The result is a gdual (contains only derivatives w.r.t. x): 4.18331+5.06537*dx+1.54332*dx**2-0.0437224*dx**3\n", "The result is a gdual (contains derivatives w.r.t. x and y): -0.324612*dy+0.104137*dx*dy**2-0.0437224*dx**3+4.18331-0.250974*dx*dy-0.0345762*dy**3+5.06537*dx-0.0827871*dx**2*dy+0.10332*dy**2+1.54332*dx**2\n" ] } ], "source": [ "def f(x,y):\n", " return x*x+2*tan(x/(y+1))-sin(x)\n", "\n", "xf = 2\n", "yf = 3\n", "x = gdual(2., \"x\", 3)\n", "y = gdual(3., \"y\", 3)\n", "print(\"The result is a float: {}\".format(f(xf,yf)))\n", "print(\"The result is a gdual (contains only derivatives w.r.t. x): {}\".format(f(x,yf)))\n", "print(\"The result is a gdual (contains derivatives w.r.t. x and y): {}\".format(f(x,y)))\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Promoting a gdual\n", "\n", "You can also perform arithmetic operations with gduals defined with a different differentiation order. In this case the resulting gdual will be \"promoted\" to the higher order" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "-1.9+dy+dx\n", "Order: 4\n", "Degree: 1\n" ] } ], "source": [ "x = gdual(0.1, \"x\", 2)\n", "y = gdual(-2, \"y\", 4)\n", "f = x+y\n", "print(f)\n", "print(\"Order: {}\".format(f.order)) # The order is the maximum order terms will be retained during the\n", "# computations using the truncated Taylor Polynomial algebra computations\n", "print(\"Degree: {}\".format(f.degree)) # The degree is the actual degree of the polynomial\n", "#which will be less or equal than the order" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "NOTE: be careful when using gdual promotion. You may \"lose\" information implicitly if the order equals the degree. " ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "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.6.4" } }, "nbformat": 4, "nbformat_minor": 1 }