# 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.


```python
from sympy import *
init_printing()  # Set up fancy printing
```


```python
import math
math.sqrt(2)
```


```python
sqrt(2)  # This `sqrt` comes from SymPy
```


```python
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?


```python
# Call acos on -1 to find where on the circle the x coordinate equals -1


```


```python
# Call `math.acos` on -1 to find the same result using the builtin math module.  
# Is the result the same?  
# What does `numpy.arccos` give you?

```

## Symbols

Just like the NumPy `ndarray` or the Pandas `DataFrame`, SymPy has `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.


```python
x, y, z = symbols('x,y,z')
alpha, beta, gamma = symbols('alpha,beta,gamma')
```


```python
x + 1
```


```python
log(alpha**beta) + gamma
```


```python
sin(x)**2 + cos(x)**2
```

### Exercise

Use `symbols` to create two variables, `mu` and `sigma`.  


```python
?, ? = 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}} $$


```python
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


```python
(x**2).diff(x)
```


```python
sin(x).diff(x)
```


```python
(x**2 + x*y + y**2).diff(x)
```


```python
diff(x**2 + x*y + y**2, y) # diff is also available as a function
```

### Exercise

In the last section you made a normal distribution


```python
mu, sigma = symbols('mu,sigma')
```


```python
bell = exp((x - mu)**2 / sigma**2)
bell
```

Take the derivative of this expression with respect to $x$


```python
?.diff(?)
```

### Exercise

There are three symbols in that expression.  We normally are interested in the derivative with respect to `x`, but we could just as easily ask for the derivative with respect to `sigma`.  Try this now


```python
# 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.


```python
#  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`.


```python
expr = sin(x)**2 + cos(x)**2
expr
```


```python
simplify(expr)
```

### Exercise

In the last section you found the third derivative of the bell curve


```python
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.


```python
# 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 functions.*


```python
sympify('r * cos(theta)^2')
```

It's useful whenever you interact with the real world, or for quickly copy-paste an expression from an external source.

01fdad76c13f0cfa031cc581446b8ef60954b82d
ipynb
Jupyter Notebook
tutorial_exercises/01-Symbols-Derivatives-Functions.ipynb
gvvynplaine/scipy-2016-tutorial
aa417427a1de2dcab2a9640b631b809d525d7929
BSD-3-Clause
2016-06-21T21:11:02.000Z
2021-02-04T07:51:03.000Z
tutorial_exercises/01-Symbols-Derivatives-Functions.ipynb
gvvynplaine/scipy-2016-tutorial
aa417427a1de2dcab2a9640b631b809d525d7929
BSD-3-Clause
2016-07-02T20:24:06.000Z
2016-07-11T11:31:44.000Z
tutorial_exercises/01-Symbols-Derivatives-Functions.ipynb
gvvynplaine/scipy-2016-tutorial
aa417427a1de2dcab2a9640b631b809d525d7929
BSD-3-Clause
2016-06-25T09:04:24.000Z
2021-08-09T06:46:01.000Z
Qwen/Qwen-72B
1. YES
2. YES