#!/usr/bin/env python # coding: utf-8 # # Introduction # # In this section we learn to do the following: # # * Import SymPy and set up pretty printing # * Use mathematical operations like `sqrt` and `sin` # * Make SymPy Symbols # * Take derivatives of expressions # * Simplify expressions # ## Preamble # 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. # In[ ]: from sympy import * init_printing() # Set up fancy printing # In[ ]: import math math.sqrt(2) # In[ ]: sqrt(2) # This `sqrt` comes from SymPy # In[ ]: cos(0) # ### Exercise # # 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? # In[ ]: # Call acos on -1 to find where on the circle the x coordinate equals -1 # In[ ]: # Call `math.acos` on -1 to find the same result using the builtin math module. # Is the result the same? # What do you think `numpy.acos` give you? # ## Symbols # # Just like the NumPy `ndarray` or the Pandas `DataFrame`, SymPy has the `Symbol`, which represents a mathematical variable. # # 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. # In[ ]: x, y, z = symbols('x,y,z') alpha, beta, gamma = symbols('alpha,beta,gamma') # In[ ]: x + 1 # In[ ]: log(alpha ** beta) + gamma # In[ ]: sin(x)**2 + cos(x)**2 # ### Exercise # # Use `symbols` to create two variables, `mu` and `sigma`. # In[ ]: get_ipython().run_line_magic('pinfo', ", ? = symbols('?')") # ### Exercise # # Use `exp`, `sqrt`, and Python's arithmetic operators like `+, -, *, **` to create the standard bell curve with SymPy objects # # $$ e^{\frac{(x - \mu)^2}{ \sigma^2}} $$ # In[ ]: exp(?) # ## Derivatives # # One of the most commonly requested operations in SymPy is the derivative. To take the derivative of an expression use the `.diff` method # In[ ]: (x**2).diff(x) # In[ ]: sin(x).diff(x) # In[ ]: (x**2 + x*y + y**2).diff(x) # In[ ]: (x**2 + x*y + y**2).diff(y) # ### Exercise # # In the last section you made a normal distribution # In[ ]: mu, sigma = symbols('mu,sigma') # In[ ]: bell = exp((x - mu)**2 / sigma**2) bell # Take the derivative of this expression with respect to $x$ # In[ ]: get_ipython().run_line_magic('pinfo', '.diff(?)') # ### Exercise # # 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 # In[ ]: # Derivative of bell curve with respect to sigma # ### Exercise # # 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. # In[ ]: # Find the second and third derivative of `bell` # ## Functions # # SymPy has a number of useful routines to manipulate expressions. The most commonly used function is `simplify`. # In[ ]: expr = sin(x)**2 + cos(x)**2 expr # In[ ]: simplify(expr) # ### Exercise # # In the last section you found the third derivative of the bell curve # In[ ]: bell.diff(x).diff(x).diff(x) # You might notice that this expression has lots of shared structure. We can factor out some terms to simplify this expression. # # Call `simplify` on this expression and observe the result. # In[ ]: # Call simplify on the third derivative of the bell expression # ## Sympify # # The `sympify` function transforms Python objects (ints, floats, strings) into SymPy objects (Integers, Reals, Symbols). # # *note the difference between `sympify` and `simplify`. These are not the same function.* # In[ ]: sympify('r * cos(theta)^2') # It's useful whenever you interact with the real world, or for quickly copy-pasting an expression from an external source.