# "Differentiation and Integration with SymPy"
> "Differentiation and Integration are some of the most important topics in maths. This post walks through how python can be used to solve these kinds of problems."

 - toc:false
 - badges: true
 - comments: true
 - author: Rohit Thakur
 - categories: [maths, calculus]

Differentiation and Integration and the building blocks of calculus. They are usually taught in Calculus I. 
These two concept are heavily used in alot of fields, including machine learning. 

If you don't remember, here is a high level introduction of what these two concepts are:

Differentiation (also called derivation) measures the rate of change of a function. Think of it like calculating slope in linear models. Only difference here is that you can calculate the slope at every point and for functions that are non linear. 
For Example: if `f(x) = x**2` then, the derivative of this function is `f'(x) = 2x`. The derivative function is read "f prime of x".

Intergration on the other hand measures the area under the curve. 
For the above function, the integration or often called antiderivative will be `x**3 / 3`



Calculating Derivatives and Integrals is sometimes confusing so we'll use [SymPy](https://www.sympy.org/en/index.html). 
We will start by installing the library. 


```python
#collapse-output
!pip install sympy
```

    Requirement already satisfied: sympy in c:\users\rohit\miniconda3\envs\math\lib\site-packages (1.8)
    Requirement already satisfied: mpmath>=0.19 in c:\users\rohit\miniconda3\envs\math\lib\site-packages (from sympy) (1.2.1)


Next we import everything we need.


```python
from sympy import Symbol, diff, integrate
```

Since we will be calculating for `x`, we'll have to create a symbol for it. 


```python
x = Symbol('x')
```

Lets start by writing our function


```python
f = 2*x**3+6
f
```




$\displaystyle 2 x^{3} + 6$



Now we calculate the derivative of the function using sympy.


```python
f_prime = f.diff(x)
f_prime
```




$\displaystyle 6 x^{2}$



So now we have the derivative of our function. Lets calculate the integral. 


```python
f_int = integrate(f_prime, x)
f_int
```




$\displaystyle 2 x^{3}$



So this is how we calculate both the derivative and anti-derivative of a function using sympy. There are other libraries as well that work really well,
e.g scipy. 

>Important: These aren't actual functions. In order to use these, you need to use "lambdify" them. We'll start by importing it.


```python
from sympy.utilities.lambdify import lambdify
```


```python
f = lambdify(x, f)
f_prime = lambdify(x, f_prime)
f_int = lambdify(x, f_int)
```


```python
print(f"The value of function at 3 is: {f(3)} and its derivative is {f_prime(3)}")
```

    The value of function at 3 is: 60 and its derivative is 54


3623558e16408df1ebc5d3095e07cc7d22f30ddf
ipynb
Jupyter Notebook
_notebooks/2021-05-22-Integration-and-Differentiation-with-python.ipynb
rohit-thakur12/ml
1b365560f0ba809f32e27025586a6cd6df9472ac
Apache-2.0
_notebooks/2021-05-22-Integration-and-Differentiation-with-python.ipynb
rohit-thakur12/ml
1b365560f0ba809f32e27025586a6cd6df9472ac
Apache-2.0
_notebooks/2021-05-22-Integration-and-Differentiation-with-python.ipynb
rohit-thakur12/ml
1b365560f0ba809f32e27025586a6cd6df9472ac
Apache-2.0
Qwen/Qwen-72B
1. YES
2. YES