{ "metadata": { "name": "", "signature": "sha256:d5125a094772f2986705f9d9accd84b81e25c5a0fcac51a0ca9ff3bf7adbc1f2" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "heading", "level": 1, "metadata": {}, "source": [ "Advanced Expression Manipulation" ] }, { "cell_type": "code", "collapsed": false, "input": [ "from sympy import *\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. " ] }, { "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": [ "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": [ "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": [ "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": [ "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": [ "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": {} } ] }