{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Gotchas" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from sympy import *\n", "init_printing()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "For each exercise, fill in the function according to its docstring. " ] }, { "cell_type": "markdown", "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", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "def symbols_exercise():\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" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "def testfunc():\n", " x = 3\n", " y = symbols('y')\n", " a = x + y\n", " y = 5\n", " return a\n", "\n", "symbols_exercise() == testfunc()" ] }, { "cell_type": "markdown", "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", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "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" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "x = symbols('x')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "equality_exercise(x, 2)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "equality_exercise((x + 1)**2, x**2 + 2*x + 1)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "equality_exercise(2*x, 2*x)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## `^` and `/`" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Correct the following functions" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "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" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "operator_exercise1()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "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)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "operator_exercise2()" ] } ], "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 }