ktongue/GoogleDriveTongue / SciPy_SymPy /tutorial_exercises /Advanced Expression Manipulation.ipynb
ktongue's picture
download
raw
4.74 kB
{
"metadata": {
"name": "Advanced Expression Manipulation"
},
"nbformat": 3,
"nbformat_minor": 0,
"worksheets": [
{
"cells": [
{
"cell_type": "heading",
"level": 1,
"metadata": {},
"source": [
"Advanced Expression Manipulation"
]
},
{
"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": []
},
{
"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": [
"Creating expressions from classes"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Create the following objects without using any mathematical operators like `+`, `-`, `*`, `/`, or `**` by explicitly using the classes `Add`, `Mul`, and `Pow`. You may use `x` instead of `Symbol('x')` and `4` instead of `Integer(4)`.\n",
"\n",
"$$x^2 + 4xyz$$\n",
"$$x^{(x^y)}$$\n",
"$$x - \\frac{y}{z}$$\n"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"@test\n",
"def explicit_classes1():\n",
" \"\"\"\n",
" Returns the expression x**2 + 4*x*y*z, built using SymPy classes explicitly.\n",
"\n",
" >>> explicit_classes1()\n",
" x**2 + 4*x*y*z\n",
" \"\"\"\n"
],
"language": "python",
"metadata": {},
"outputs": []
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"@test\n",
"def explicit_classes2():\n",
" \"\"\"\n",
" Returns the expression x**(x**y), built using SymPy classes explicitly.\n",
"\n",
" >>> explicit_classes2()\n",
" x**(x**y)\n",
" \"\"\"\n"
],
"language": "python",
"metadata": {},
"outputs": []
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"@test\n",
"def explicit_classes3():\n",
" \"\"\"\n",
" Returns the expression x - y/z, built using SymPy classes explicitly.\n",
"\n",
" >>> explicit_classes3()\n",
" x - y/z\n",
" \"\"\"\n"
],
"language": "python",
"metadata": {},
"outputs": []
},
{
"cell_type": "heading",
"level": 2,
"metadata": {},
"source": [
"Nested args"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"expr = x**2 - y*(2**(x + 3) + z)"
],
"language": "python",
"metadata": {},
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Use nested `.args` calls to get the 3 in expr."
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"@test\n",
"def nested_args():\n",
" \"\"\"\n",
" Get the 3 in the above expression.\n",
"\n",
" >>> nested_args()\n",
" 3\n",
" \"\"\"\n"
],
"language": "python",
"metadata": {},
"outputs": []
},
{
"cell_type": "heading",
"level": 2,
"metadata": {},
"source": [
"Traversal "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Write a post-order traversal function that prints each node."
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"@test\n",
"def post(expr):\n",
" \"\"\"\n",
" Post-order traversal\n",
"\n",
" >>> expr = x**2 - y*(2**(x + 3) + z)\n",
" >>> post(expr)\n",
" -1\n",
" y\n",
" 2\n",
" 3\n",
" x\n",
" x + 3\n",
" 2**(x + 3)\n",
" z\n",
" 2**(x + 3) + z\n",
" -y*(2**(x + 3) + z)\n",
" x\n",
" 2\n",
" x**2\n",
" x**2 - y*(2**(x + 3) + z)\n",
" \"\"\"\n"
],
"language": "python",
"metadata": {},
"outputs": []
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"for i in postorder_traversal(expr):\n",
" print i"
],
"language": "python",
"metadata": {},
"outputs": []
}
],
"metadata": {}
}
]
}

Xet Storage Details

Size:
4.74 kB
·
Xet hash:
3ed5c3c84492e7384551beea8a3181118e6ee9f27b4734e48c41ee2fc4a169ce

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