{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Introduction\n", "\n", "In this section we learn to do the following:\n", "\n", "* Import SymPy and set up pretty printing\n", "* Use mathematical operations like `sqrt` and `sin`\n", "* Make SymPy Symbols\n", "* Take derivatives of expressions\n", "* Simplify expressions" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Preamble" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Just like NumPy and Pandas replace functions like `sin`, `cos`, `exp`, and `log` to powerful numeric implementations, SymPy replaces `sin`, `cos`, `exp` and `log` with powerful mathematical implementations." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from sympy import *\n", "init_printing() # Set up fancy printing" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import math\n", "math.sqrt(2)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "sqrt(2) # This `sqrt` comes from SymPy" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "cos(0)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Exercise\n", "\n", "Use the function `acos` on `-1` to find when cosine equals `-1`. Try this same function with the math library. Do you get the same result?" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Call acos on -1 to find where on the circle the x coordinate equals -1\n", "\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Call `math.acos` on -1 to find the same result using the builtin math module. \n", "# Is the result the same? \n", "# What do you think `numpy.acos` give you?\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Symbols\n", "\n", "Just like the NumPy `ndarray` or the Pandas `DataFrame`, SymPy has the `Symbol`, which represents a mathematical variable.\n", "\n", "We create symbols using the function `symbols`. Operations on these symbols don't do numeric work like with NumPy or Pandas, instead they build up mathematical expressions." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "x, y, z = symbols('x,y,z')\n", "alpha, beta, gamma = symbols('alpha,beta,gamma')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "x + 1" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "log(alpha ** beta) + gamma" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "sin(x)**2 + cos(x)**2" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Exercise\n", "\n", "Use `symbols` to create two variables, `mu` and `sigma`. " ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "?, ? = symbols('?')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Exercise\n", "\n", "Use `exp`, `sqrt`, and Python's arithmetic operators like `+, -, *, **` to create the standard bell curve with SymPy objects\n", "\n", "$$ e^{\\frac{(x - \\mu)^2}{ \\sigma^2}} $$" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "exp(?)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Derivatives\n", "\n", "One of the most commonly requested operations in SymPy is the derivative. To take the derivative of an expression use the `.diff` method" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "(x**2).diff(x)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "sin(x).diff(x)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "(x**2 + x*y + y**2).diff(x)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "(x**2 + x*y + y**2).diff(y)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Exercise\n", "\n", "In the last section you made a normal distribution" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "mu, sigma = symbols('mu,sigma')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "bell = exp((x - mu)**2 / sigma**2)\n", "bell" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Take the derivative of this expression with respect to $x$" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "?.diff(?)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Exercise\n", "\n", "There are three symbols in that expression. We normally are interested in the derivative with repspect to `x`, but we could just as easily ask for the derivative with respect to `sigma`. Try this now" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Derivative of bell curve with respect to sigma\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Exercise\n", "\n", "The second derivative of an expression is just the derivative of the derivative. Chain `.diff( )` calls to find the second and third derivatives of your expression." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Find the second and third derivative of `bell`\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Functions\n", "\n", "SymPy has a number of useful routines to manipulate expressions. The most commonly used function is `simplify`." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "expr = sin(x)**2 + cos(x)**2\n", "expr" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "simplify(expr)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Exercise\n", "\n", "In the last section you found the third derivative of the bell curve" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "bell.diff(x).diff(x).diff(x)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "You might notice that this expression has lots of shared structure. We can factor out some terms to simplify this expression. \n", "\n", "Call `simplify` on this expression and observe the result." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Call simplify on the third derivative of the bell expression\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Sympify\n", "\n", "The `sympify` function transforms Python objects (ints, floats, strings) into SymPy objects (Integers, Reals, Symbols). \n", "\n", "*note the difference between `sympify` and `simplify`. These are not the same function.*" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "sympify('r * cos(theta)^2')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "It's useful whenever you interact with the real world, or for quickly copy-pasting an expression from an external source." ] } ], "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 }