{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Advanced Expression Manipulation Solutions" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from sympy import *\n", "x, y, z = symbols('x y z')\n", "init_printing()\n", "from IPython.display import display" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "For each exercise, fill in the function according to its docstring. " ] }, { "cell_type": "markdown", "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", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "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))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "explicit_classes1()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "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))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "explicit_classes2()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "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))))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "explicit_classes3()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Nested args" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "expr = x**2 - y*(2**(x + 3) + z)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Use nested `.args` calls to get the 3 in expr." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "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[1].args[1].args[0]" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "nested_args()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Traversal " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Write a post-order traversal function that prints each node." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "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", " display(expr)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "expr = x**2 - y*(2**(x + 3) + z)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "post(expr)" ] } ], "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 }