ktongue's picture
download
raw
4.84 kB
{
"metadata": {
"name": "Gotchas"
},
"nbformat": 3,
"nbformat_minor": 0,
"worksheets": [
{
"cells": [
{
"cell_type": "heading",
"level": 1,
"metadata": {},
"source": [
"Gotchas"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Boilerplate to make the doctester work. Run this cell first."
]
},
{
"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",
"# Work around a bug in IPython. This will disable the ability to paste things with >>>\n",
"def notransform(line): return line\n",
"from IPython.core import inputsplitter\n",
"inputsplitter.transform_classic_prompt = notransform"
],
"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": [
"Symbols"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"What will be the output of the following code?\n",
"\n",
" x = 3\n",
" y = symbols('y')\n",
" a = x + y\n",
" y = 5\n",
" print a\n",
"\n",
"Replace `???` in the below code with what you think the value of `a` will be. Remember to define any Symbols you need!"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"@test\n",
"def symbols_exercise():\n",
" \"\"\"\n",
" (This tests that your output is correct)\n",
"\n",
" >>> def testfunc():\n",
" ... x = 3\n",
" ... y = symbols('y')\n",
" ... a = x + y\n",
" ... y = 5\n",
" ... return a\n",
" >>> symbols_exercise() == testfunc()\n",
" True\n",
" \"\"\"\n",
" return ??? # Replace ??? with what you think the value of a is"
],
"language": "python",
"metadata": {},
"outputs": []
},
{
"cell_type": "heading",
"level": 2,
"metadata": {},
"source": [
"Equality"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Write a function that takes two expressions as input, and returns a tuple of two booleans. The first if they are equal symbolically, and the second if they are equal mathematically."
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"@test\n",
"def equality_exercise(a, b):\n",
" \"\"\"\n",
" Determine if a = b symbolically and mathematically.\n",
"\n",
" Returns a tuple of two booleans. The first is True if a = b symbolically,\n",
" the second is True if a = b mathematically. Note the second may be False\n",
" but the two still equal if SymPy is not powerful enough.\n",
"\n",
" Examples\n",
" ========\n",
"\n",
" >>> x = symbols('x')\n",
" >>> equality_exercise(x, 2)\n",
" (False, False)\n",
" >>> equality_exercise((x + 1)**2, x**2 + 2*x + 1)\n",
" (False, True)\n",
" >>> equality_exercise(2*x, 2*x)\n",
" (True, True)\n",
" \"\"\"\n"
],
"language": "python",
"metadata": {},
"outputs": []
},
{
"cell_type": "heading",
"level": 2,
"metadata": {},
"source": [
"`^` and `/`"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Correct the following functions"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"@test\n",
"def operator_exercise1():\n",
" \"\"\"\n",
" >>> operator_exercise1()\n",
" x**2 + 2*x + 1/2\n",
" \"\"\"\n",
" x = symbols('x')\n",
" return x^2 + 2*x + 1/2"
],
"language": "python",
"metadata": {},
"outputs": []
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"@test\n",
"def operator_exercise2():\n",
" \"\"\"\n",
" >>> operator_exercise2()\n",
" (x**2/2 + 2*x + 3/4)**(3/2)\n",
" \"\"\"\n",
" x = symbols('x')\n",
" return (1/2*x^2 + 2*x + 3/4)^(3/2)"
],
"language": "python",
"metadata": {},
"outputs": []
}
],
"metadata": {}
}
]
}

Xet Storage Details

Size:
4.84 kB
·
Xet hash:
fcb10bbc0b61664ef9479b02d05d804ae8d657c451822038a497815a417cd875

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