## SymPy

SymPy is a python library for [symbolic computing](https://en.wikipedia.org/wiki/Symbolic_computation).  Perhaps the most well known software that embraces symbolic computing is [Maple](https://en.wikipedia.org/wiki/Maple_(software)).  SymPy although significantly less powerul enables numerous symbolic areas of mathematics: algebra, arithmetic, calculus, discrete mathematics and quantum physics. It is capable of formatting the result of the computations as LaTeX code.

SymPy is free software and is licensed under New BSD License. The lead developers are Ondřej Čertík and Aaron Meurer.  The following code will import everything from the package and enable latex rendering of output.



```python
from sympy import *
from sympy import init_printing
init_printing()
```

### Symbolic Algebra

In the same way we do algebra on paper we can use a computer.  Symbolic computation or [algebraic computation](https://en.wikipedia.org/wiki/Computer_algebra), is a scientific area that refers to the study and development of algorithms and software for manipulating mathematical expressions and other mathematical objects.

For example


```python
sqrt(8)
```

We need to define our variables symbolically before we use them


```python
x,y,z = symbols('x y z')
x
```

once the variables are available we can define expressions and perform actions on them


```python
expr = x + 2*y
print(expr)
expr = expr + 9*x
print(expr)
```

    x + 2*y
    10*x + 2*y


you may want to **expand** the algebric representation


```python
expr2 = x*expr
print(expr2)
print(expand(expr2))
```

    x*(10*x + 2*y)
    10*x**2 + 2*x*y


Recall that factoring is the process of finding the factors--that is finding the terms to multiply together to get an expression. If you need a refresher check out the [Khan academy materials on factoring](https://www.khanacademy.org/math/algebra/polynomial-factorization).  Sympy can factorize


```python
expr = x + x**2
factor(expr)
```

### Symbolic calculus - derivatives

#### Definition of a derivative

From [wiki](https://en.wikipedia.org/wiki/Derivative) 

>Derivatives are a fundamental tool of calculus. For example, the derivative of the position of a moving object with respect to time is the object's velocity: this measures how quickly the position of the object changes when time advances.

The derivative of f with respect to x is given by  

$ f'(x) = \lim_{h \to 0} \frac{f(x+h) - f(x)}{h}$    

when this limit exits.

#### Basic derivative properties

Assuming *c* and *n* to be real constants, then these theorems hold true:

and if you want [proofs.](http://www2.bc.cc.ca.us/resperic/Math6A/Lectures/ch2/3/DerivativeRuleProofs.htm)

>**1**. *The derivative of a constant is 0*

$ \frac{d}{dx}c = 0 $ 


```python
c = 1
diff(c)
```

>**2**. *The derivative of a variable is 1*

$ \frac{d}{dx}x = 1 $


```python
diff(x)
```

>**3**. *The derivative of a constant times a function is the same as the deriviative of that function times the constant*

$ \frac{d}{dx}(c \cdot f(x)) = c \cdot \frac{d}{dx}f(x) = c \cdot f'(x) $  


```python
diff(1/x * c)
```


```python
c * diff(1/x)
```

    -1/x**2


>**4**. The derivative of a function plus or minus another function is the same as if we took the derivative separately

$ \frac{d}{dx}(f(x) + g(x)) = f'(x) + g'(x) $  (true for minus as well)  


```python
diff(1/x + ln(x))
```


```python
diff(1/x) + diff(ln(x))
```

>**5**. The derivative

$ \frac{d}{dx}x^n = n \cdot x^{n-1} $  the power rule


```python
## can you show that this rule holds?
```


```python
## here is a way to write partial derivatives
expr = Derivative(1/x+y,x)
expr

```


```python

```

4322961557bb5ceed89587e9bced77b836b22d6b
ipynb
Jupyter Notebook
math-essentials/calculus/sympy-primer.ipynb
chyld/demoX
27f26a553aeb6682173f6b1b8dc8969101993324
MIT
2018-09-21T23:14:35.000Z
2022-01-21T10:38:52.000Z
math-essentials/calculus/sympy-primer.ipynb
chyld/demoX
27f26a553aeb6682173f6b1b8dc8969101993324
MIT
math-essentials/calculus/sympy-primer.ipynb
chyld/demoX
27f26a553aeb6682173f6b1b8dc8969101993324
MIT
2018-01-08T22:59:38.000Z
2022-02-09T06:44:38.000Z
Qwen/Qwen-72B
1. YES
2. YES