{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Simplification" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from sympy import *\n", "x, y, z = symbols('x y z')\n", "init_printing()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "For each exercise, fill in the function according to its docstring." ] }, { "cell_type": "markdown", "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", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "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" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "polysimp1(cos(x)*sin(x) + cos(x))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "polysimp1(cos(x)*sin(x) + cos(x) + 1)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "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" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "polysimp2((2*x + 1)/(x**2 + x))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "polysimp2((x**2 + 3*x + 1)/(x**3 + 2*x**2 + x))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Powers" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In each exercise, apply specific simplification functions to get the desired result. " ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "def powersimp1(expr):\n", " \"\"\"\n", " >>> powersimp1(exp(x)*(exp(y) + 1))\n", " exp(x) + exp(x + y)\n", " \"\"\"\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "powersimp1(exp(x)*(exp(y) + 1))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "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" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "powersimp2(2**x*x**x)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "powersimp2(x**x*x**x)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "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" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "a, b, c = symbols('a b c')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "powersimp3((a**b)**c)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "powersimp3((a**b)**(c + 1))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Logs" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "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" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "a, b = symbols('a b', positive=True)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "logsimp1(log(x**y*a**b))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "logsimp1(log(x*y*a*b))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Miscellaneous " ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "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" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "miscsimp1(sin(x + y))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "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" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "miscsimp2(gamma(x + 4))" ] }, { "cell_type": "markdown", "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", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "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" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "a0, a1, a2, a3, a4 = symbols('a0:5')" ] }, { "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", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "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₀⋅a₁⋅a₂⋅a₃⋅a₄ + a₀⋅a₁⋅a₂ + a₀⋅a₃⋅a₄ + a₀ + a₁⋅a₂⋅a₃ + a₁⋅a₃⋅a₄ + a₁ + a₃\n", " ─────────────────────────────────────────────────────────────────────────\n", " a₀⋅a₁⋅a₂⋅a₄ + a₀⋅a₄ + a₁⋅a₂ + a₁⋅a₄ + 1\n", " >>> cancel(list_to_frac(continued_frac())) == orig_frac\n", " True\n", " \"\"\"\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "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)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "orig_frac" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "cancel(list_to_frac(continued_frac())) == orig_frac" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "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.1" } }, "nbformat": 4, "nbformat_minor": 1 }