ktongue/GoogleDriveTongue / SciPy_SymPy /tutorial_exercises /Simplification Solutions.ipynb
ktongue's picture
download
raw
10 kB
{
"metadata": {
"name": "Simplification Solutions"
},
"nbformat": 3,
"nbformat_minor": 0,
"worksheets": [
{
"cells": [
{
"cell_type": "heading",
"level": 1,
"metadata": {},
"source": [
"Simplification Solutions"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Boilerplate to make the doctester work."
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"import sys\n",
"import os\n",
"sys.path.insert(1, os.path.join(os.path.pardir, \"ipython_doctester\"))\n",
"from sympy import *\n",
"from ipython_doctester import test\n",
"x, y, z = symbols('x y z')"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 68
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"For each exercise, fill in the function according to its docstring. Execute the cell to see if you did it right. "
]
},
{
"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": [
"@test\n",
"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",
" return collect(expr, cos(x))"
],
"language": "python",
"metadata": {},
"outputs": [
{
"html": [
"\n",
" <p style=\"color:green;font-size:250%;font-weight=bold\">Success!</p>\n",
" "
],
"output_type": "display_data"
}
],
"prompt_number": 69
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"@test\n",
"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",
" return expand(apart(expr))"
],
"language": "python",
"metadata": {},
"outputs": [
{
"html": [
"\n",
" <p style=\"color:green;font-size:250%;font-weight=bold\">Success!</p>\n",
" "
],
"output_type": "display_data"
}
],
"prompt_number": 70
},
{
"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": [
"@test\n",
"def powersimp1(expr):\n",
" \"\"\"\n",
" >>> powersimp1(exp(x)*(exp(y) + 1))\n",
" exp(x) + exp(x + y)\n",
" \"\"\"\n",
" return powsimp(expand(expr))"
],
"language": "python",
"metadata": {},
"outputs": [
{
"html": [
"\n",
" <p style=\"color:green;font-size:250%;font-weight=bold\">Success!</p>\n",
" "
],
"output_type": "display_data"
}
],
"prompt_number": 71
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"@test\n",
"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",
" return powsimp(expr, force=True)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"html": [
"\n",
" <p style=\"color:green;font-size:250%;font-weight=bold\">Success!</p>\n",
" "
],
"output_type": "display_data"
}
],
"prompt_number": 72
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"@test\n",
"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",
" return powdenest(expand_power_exp(expr), force=True)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"html": [
"\n",
" <p style=\"color:green;font-size:250%;font-weight=bold\">Success!</p>\n",
" "
],
"output_type": "display_data"
}
],
"prompt_number": 73
},
{
"cell_type": "heading",
"level": 2,
"metadata": {},
"source": [
"Logs"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"@test\n",
"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",
" return logcombine(expand_log(expr, force=True))"
],
"language": "python",
"metadata": {},
"outputs": [
{
"html": [
"\n",
" <p style=\"color:green;font-size:250%;font-weight=bold\">Success!</p>\n",
" "
],
"output_type": "display_data"
}
],
"prompt_number": 74
},
{
"cell_type": "heading",
"level": 2,
"metadata": {},
"source": [
"Miscellaneous "
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"@test\n",
"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",
" return expand_trig(expr).rewrite(tan)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"html": [
"\n",
" <p style=\"color:green;font-size:250%;font-weight=bold\">Success!</p>\n",
" "
],
"output_type": "display_data"
}
],
"prompt_number": 75
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"@test\n",
"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",
" return expand(expand_func(expr))"
],
"language": "python",
"metadata": {},
"outputs": [
{
"html": [
"\n",
" <p style=\"color:green;font-size:250%;font-weight=bold\">Success!</p>\n",
" "
],
"output_type": "display_data"
}
],
"prompt_number": 66
},
{
"cell_type": "heading",
"level": 2,
"metadata": {},
"source": [
"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": 77
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"a0, a1, a2, a3, a4 = symbols('a0:5')"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 78
},
{
"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": [
"@test\n",
"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, use_unicode=False, wrap_line=False)\n",
" a0*a1*a2*a3*a4 + a0*a1*a2 + a0*a3*a4 + a0 + a1*a2*a3 + a1*a3*a4 + a1 + a3\n",
" -------------------------------------------------------------------------\n",
" a0*a1*a2*a4 + a0*a4 + a1*a2 + a1*a4 + 1 \n",
" >>> cancel(list_to_frac(continued_frac())) == orig_frac\n",
" True\n",
" \"\"\"\n",
" return [a3, a4, a0, a2, a1]"
],
"language": "python",
"metadata": {},
"outputs": [
{
"html": [
"\n",
" <p style=\"color:green;font-size:250%;font-weight=bold\">Success!</p>\n",
" "
],
"output_type": "display_data"
}
],
"prompt_number": 88
},
{
"cell_type": "code",
"collapsed": false,
"input": [],
"language": "python",
"metadata": {},
"outputs": []
}
],
"metadata": {}
}
]
}

Xet Storage Details

Size:
10 kB
·
Xet hash:
0e45b4d311182ed554599b59429d9680eb8fd8aff5bd44d8fc399e8180ad1979

Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.