{ "metadata": { "name": "", "signature": "sha256:1ea7eb5baa9b54b9c8bb4ec97fe085e1ae285d5775adecea239fa6b380a18a68" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "heading", "level": 1, "metadata": {}, "source": [ "Simplification" ] }, { "cell_type": "code", "collapsed": false, "input": [ "from sympy import *\n", "x, y, z = symbols('x y z')\n", "init_printing()" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 1 }, { "cell_type": "markdown", "metadata": {}, "source": [ "For each exercise, fill in the function according to its docstring." ] }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Polynomial/Rational Function Simplification" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In each exercise, apply specific simplification functions to get the desired result." ] }, { "cell_type": "code", "collapsed": false, "input": [ "def polysimp1(expr):\n", " \"\"\"\n", " >>> polysimp1(cos(x)*sin(x) + cos(x))\n", " (sin(x) + 1)*cos(x)\n", " >>> polysimp1(cos(x)*sin(x) + cos(x) + 1)\n", " (sin(x) + 1)*cos(x) + 1\n", " \"\"\"\n" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 2 }, { "cell_type": "code", "collapsed": false, "input": [ "polysimp1(cos(x)*sin(x) + cos(x))" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 3 }, { "cell_type": "code", "collapsed": false, "input": [ "polysimp1(cos(x)*sin(x) + cos(x) + 1)" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 4 }, { "cell_type": "code", "collapsed": false, "input": [ "def polysimp2(expr):\n", " \"\"\"\n", " >>> polysimp2((2*x + 1)/(x**2 + x))\n", " 1/(x + 1) + 1/x\n", " >>> polysimp2((x**2 + 3*x + 1)/(x**3 + 2*x**2 + x))\n", " 1/(x**2 + 2*x + 1) + 1/x\n", " \"\"\"\n" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 5 }, { "cell_type": "code", "collapsed": false, "input": [ "polysimp2((2*x + 1)/(x**2 + x))" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 6 }, { "cell_type": "code", "collapsed": false, "input": [ "polysimp2((x**2 + 3*x + 1)/(x**3 + 2*x**2 + x))" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 7 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Powers" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In each exercise, apply specific simplification functions to get the desired result. " ] }, { "cell_type": "code", "collapsed": false, "input": [ "def powersimp1(expr):\n", " \"\"\"\n", " >>> powersimp1(exp(x)*(exp(y) + 1))\n", " exp(x) + exp(x + y)\n", " \"\"\"\n" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 8 }, { "cell_type": "code", "collapsed": false, "input": [ "powersimp1(exp(x)*(exp(y) + 1))" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 9 }, { "cell_type": "code", "collapsed": false, "input": [ "def powersimp2(expr):\n", " \"\"\"\n", " >>> powersimp2(2**x*x**x)\n", " (2*x)**x\n", " >>> powersimp2(x**x*x**x)\n", " (x**2)**x\n", " \"\"\"\n" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 10 }, { "cell_type": "code", "collapsed": false, "input": [ "powersimp2(2**x*x**x)" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 11 }, { "cell_type": "code", "collapsed": false, "input": [ "powersimp2(x**x*x**x)" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 12 }, { "cell_type": "code", "collapsed": false, "input": [ "def powersimp3(expr):\n", " \"\"\"\n", " >>> a, b, c = symbols('a b c')\n", " >>> powersimp3((a**b)**c)\n", " a**(b*c)\n", " >>> powersimp3((a**b)**(c + 1))\n", " a**(b*c + b)\n", " \"\"\"\n" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 13 }, { "cell_type": "code", "collapsed": false, "input": [ "a, b, c = symbols('a b c')" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 14 }, { "cell_type": "code", "collapsed": false, "input": [ "powersimp3((a**b)**c)" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 15 }, { "cell_type": "code", "collapsed": false, "input": [ "powersimp3((a**b)**(c + 1))" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 16 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Logs" ] }, { "cell_type": "code", "collapsed": false, "input": [ "def logsimp1(expr):\n", " \"\"\"\n", " >>> a, b = symbols('a b', positive=True)\n", " >>> logsimp1(log(x**y*a**b))\n", " y*log(x) + log(a**b)\n", " >>> logsimp1(log(x*y*a*b))\n", " log(x) + log(y) + log(a*b)\n", " \"\"\"\n" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 17 }, { "cell_type": "code", "collapsed": false, "input": [ "a, b = symbols('a b', positive=True)" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 18 }, { "cell_type": "code", "collapsed": false, "input": [ "logsimp1(log(x**y*a**b))" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 19 }, { "cell_type": "code", "collapsed": false, "input": [ "logsimp1(log(x*y*a*b))" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 20 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Miscellaneous " ] }, { "cell_type": "code", "collapsed": false, "input": [ "def miscsimp1(expr):\n", " \"\"\"\n", " >>> miscsimp1(sin(x + y))\n", " 2*(-tan(x/2)**2 + 1)*tan(y/2)/((tan(x/2)**2 + 1)*(tan(y/2)**2 + 1)) + 2*(-tan(y/2)**2 + 1)*tan(x/2)/((tan(x/2)**2 + 1)*(tan(y/2)**2 + 1))\n", " \"\"\"\n" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 21 }, { "cell_type": "code", "collapsed": false, "input": [ "miscsimp1(sin(x + y))" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 22 }, { "cell_type": "code", "collapsed": false, "input": [ "def miscsimp2(expr):\n", " \"\"\"\n", " >>> miscsimp2(gamma(x + 4))\n", " x**4*gamma(x) + 6*x**3*gamma(x) + 11*x**2*gamma(x) + 6*x*gamma(x)\n", " \"\"\"\n" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 23 }, { "cell_type": "code", "collapsed": false, "input": [ "miscsimp2(gamma(x + 4))" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 24 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Continued Fractions" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "If we do not cover this, see http://asmeurer.github.io/scipy-2014-tutorial/html/tutorial/simplification.html#example-continued-fractions." ] }, { "cell_type": "code", "collapsed": false, "input": [ "def list_to_frac(l):\n", " expr = Integer(0)\n", " for i in reversed(l[1:]):\n", " expr += i\n", " expr = 1/expr\n", " return l[0] + expr" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 25 }, { "cell_type": "code", "collapsed": false, "input": [ "a0, a1, a2, a3, a4 = symbols('a0:5')" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 27 }, { "cell_type": "markdown", "metadata": {}, "source": [ "Determine the list used to create the continued fraction $$\\frac{a_{0} a_{1} a_{2} a_{3} a_{4} + a_{0} a_{1} a_{2} + a_{0} a_{3} a_{4} + a_{0} + a_{1} a_{2} a_{3} + a_{1} a_{3} a_{4} + a_{1} + a_{3}}{a_{0} a_{1} a_{2} a_{4} + a_{0} a_{4} + a_{1} a_{2} + a_{1} a_{4} + 1}.$$" ] }, { "cell_type": "code", "collapsed": false, "input": [ "def continued_frac():\n", " \"\"\"\n", " Determine the original list used to create the fraction. \n", "\n", " Return the original list from this function.\n", "\n", " >>> orig_frac = (a0*a1*a2*a3*a4 + a0*a1*a2 + a0*a3*a4 + a0 + a1*a2*a3 + a1*a3*a4 + a1 + a3)/(a0*a1*a2*a4 + a0*a4 + a1*a2 + a1*a4 + 1)\n", " >>> pprint(orig_frac)\n", " a\u2080\u22c5a\u2081\u22c5a\u2082\u22c5a\u2083\u22c5a\u2084 + a\u2080\u22c5a\u2081\u22c5a\u2082 + a\u2080\u22c5a\u2083\u22c5a\u2084 + a\u2080 + a\u2081\u22c5a\u2082\u22c5a\u2083 + a\u2081\u22c5a\u2083\u22c5a\u2084 + a\u2081 + a\u2083\n", " \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\n", " a\u2080\u22c5a\u2081\u22c5a\u2082\u22c5a\u2084 + a\u2080\u22c5a\u2084 + a\u2081\u22c5a\u2082 + a\u2081\u22c5a\u2084 + 1\n", " >>> cancel(list_to_frac(continued_frac())) == orig_frac\n", " True\n", " \"\"\"\n" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 28 }, { "cell_type": "code", "collapsed": false, "input": [ "orig_frac = (a0*a1*a2*a3*a4 + a0*a1*a2 + a0*a3*a4 + a0 + a1*a2*a3 + a1*a3*a4 + a1 + a3)/(a0*a1*a2*a4 + a0*a4 + a1*a2 + a1*a4 + 1)" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 29 }, { "cell_type": "code", "collapsed": false, "input": [ "orig_frac" ], "language": "python", "metadata": {}, "outputs": [ { "latex": [ "$$\\frac{a_{0} a_{1} a_{2} a_{3} a_{4} + a_{0} a_{1} a_{2} + a_{0} a_{3} a_{4} + a_{0} + a_{1} a_{2} a_{3} + a_{1} a_{3} a_{4} + a_{1} + a_{3}}{a_{0} a_{1} a_{2} a_{4} + a_{0} a_{4} + a_{1} a_{2} + a_{1} a_{4} + 1}$$" ], "metadata": {}, "output_type": "pyout", "png": "iVBORw0KGgoAAAANSUhE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==\n", "prompt_number": 30, "text": [ "a\u2080\u22c5a\u2081\u22c5a\u2082\u22c5a\u2083\u22c5a\u2084 + a\u2080\u22c5a\u2081\u22c5a\u2082 + a\u2080\u22c5a\u2083\u22c5a\u2084 + a\u2080 + a\u2081\u22c5a\u2082\u22c5a\u2083 + a\u2081\u22c5a\u2083\u22c5a\u2084 + a\u2081 + a\u2083\n", "\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\n", " a\u2080\u22c5a\u2081\u22c5a\u2082\u22c5a\u2084 + a\u2080\u22c5a\u2084 + a\u2081\u22c5a\u2082 + a\u2081\u22c5a\u2084 + 1 " ] } ], "prompt_number": 30 }, { "cell_type": "code", "collapsed": false, "input": [ "cancel(list_to_frac(continued_frac())) == orig_frac" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 31, "text": [ "True" ] } ], "prompt_number": 31 }, { "cell_type": "code", "collapsed": false, "input": [], "language": "python", "metadata": {}, "outputs": [] } ], "metadata": {} } ] }