ktongue/GoogleDriveTongue / SciPy_SymPy /tutorial_exercises /Advanced Expression Manipulation Solutions.ipynb
ktongue's picture
download
raw
4.09 kB
{"nbformat":4,"nbformat_minor":0,"cells":[{"cell_type":"markdown","source":["#Advanced Expression Manipulation Solutions"],"metadata":{"id":"z8Ipv4rl6tJE"}},{"cell_type":"code","source":["import sys\n","import os\n","sys.path.insert(1, os.path.join(os.path.pardir, \"ipython_doctester\"))\n","# !pip install ipython_doctester\n","from sympy import *\n","from ipython_doctester import test\n","x, y, z = symbols('x y z')"],"outputs":[],"metadata":{"id":"o07MoS7_6tJG","executionInfo":{"status":"ok","timestamp":1763837747977,"user_tz":-60,"elapsed":7,"user":{"displayName":"Tongue Kevin","userId":"13768078595560270101"}}},"execution_count":3},{"cell_type":"markdown","source":["For each exercise, fill in the function according to its docstring. Execute the cell to see if you did it right."],"metadata":{"id":"7ijN2JYM6tJH"}},{"cell_type":"markdown","source":["##Creating expressions from classes"],"metadata":{"id":"JQD_FpJ66tJH"}},{"cell_type":"markdown","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"],"metadata":{"id":"o8Jquxmu6tJH"}},{"cell_type":"code","source":["@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"," return Add(Pow(x, 2), Mul(4, x, y, z))"],"outputs":[],"metadata":{"id":"bqgsDyDD6tJH"},"execution_count":null},{"cell_type":"code","source":["@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"," return Pow(x, Pow(x, y))"],"outputs":[],"metadata":{"id":"PY2Blsq_6tJH"},"execution_count":null},{"cell_type":"code","source":["@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"," return Add(x, Mul(-1, Mul(y, Pow(z, -1))))"],"outputs":[],"metadata":{"id":"9H8ed6d46tJH"},"execution_count":null},{"cell_type":"markdown","source":["##Nested args"],"metadata":{"id":"WDgWygOh6tJH"}},{"cell_type":"code","source":["expr = x**2 - y*(2**(x + 3) + z)"],"outputs":[],"metadata":{"id":"beCEnMJW6tJH"},"execution_count":null},{"cell_type":"markdown","source":["Use nested `.args` calls to get the 3 in expr."],"metadata":{"id":"dJ6r6VNU6tJI"}},{"cell_type":"code","source":["@test\n","def nested_args():\n"," \"\"\"\n"," Get the 3 in the above expression.\n","\n"," >>> nested_args()\n"," 3\n"," \"\"\"\n"," expr = x**2 - y*(2**(x + 3) + z)\n"," return expr.args[0].args[2].args[0].args[1].args[0]"],"outputs":[],"metadata":{"id":"QCV-qUk76tJI"},"execution_count":null},{"cell_type":"markdown","source":["##Traversal"],"metadata":{"id":"6gLmdgg16tJI"}},{"cell_type":"markdown","source":["Write a post-order traversal function that prints each node."],"metadata":{"id":"M9aoFcqi6tJI"}},{"cell_type":"code","source":["@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"," for arg in expr.args:\n"," post(arg)\n"," print expr"],"outputs":[],"metadata":{"id":"d7j4HFZk6tJI"},"execution_count":null},{"cell_type":"code","source":["for i in postorder_traversal(expr):\n"," print i"],"outputs":[],"metadata":{"id":"LURG28W86tJI"},"execution_count":null}],"metadata":{"colab":{"provenance":[]},"language_info":{"name":"python"},"kernelspec":{"name":"python3","display_name":"Python 3"}}}

Xet Storage Details

Size:
4.09 kB
·
Xet hash:
221304b56ab0c8687253ba9df97a00bfa6742493c68cf9635f6f0311f7f26890

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