```python
from sympy import *
init_printing(use_latex='mathjax')
```


```python
def get_diff(expression, *symbols):
    print("Expression:")
    display(expression)
    results = []
    for symbol in symbols:
        print("derivated by ", symbol)
        df = diff(expression, symbol)
        display(df)
        results.append(df)
    return results
```


```python
x, y, z = symbols('x y z')
expr = sin(x) * exp(y * z ** 2)
get_diff(expr, z)
```

    Expression:



$$e^{y z^{2}} \sin{\left (x \right )}$$


    derivated by  z



$$2 y z e^{y z^{2}} \sin{\left (x \right )}$$





$$\left [ 2 y z e^{y z^{2}} \sin{\left (x \right )}\right ]$$




```python
x, y, m = symbols('x y m')
expr = pi * x ** 3 + x * y ** 2 + m * y ** 4
get_diff(expr, y)
```

    Expression:



$$m y^{4} + \pi x^{3} + x y^{2}$$


    derivated by  y



$$4 m y^{3} + 2 x y$$





$$\left [ 4 m y^{3} + 2 x y\right ]$$




```python
x, y, m = symbols('x y m')
expr = pi * x ** 3 + x * y ** 2 + m * y ** 4
get_diff(expr, y)
```

    Expression:



$$m y^{4} + \pi x^{3} + x y^{2}$$


    derivated by  y



$$4 m y^{3} + 2 x y$$





$$\left [ 4 m y^{3} + 2 x y\right ]$$




```python
x, y, z = symbols('x y z')
expr = y * x ** 2 + z * y ** 2 + x * z ** 2
get_diff(expr, x, y, z)
```

    Expression:



$$x^{2} y + x z^{2} + y^{2} z$$


    derivated by  x



$$2 x y + z^{2}$$


    derivated by  y



$$x^{2} + 2 y z$$


    derivated by  z



$$2 x z + y^{2}$$





$$\left [ 2 x y + z^{2}, \quad x^{2} + 2 y z, \quad 2 x z + y^{2}\right ]$$




```python
x, y, z = symbols('x y z')
expr = exp(2 * x) * sin(y) * z ** 2 + cos(z) * exp(x) * exp(y)
get_diff(expr, x,y,z)
```

    Expression:



$$z^{2} e^{2 x} \sin{\left (y \right )} + e^{x} e^{y} \cos{\left (z \right )}$$


    derivated by  x



$$2 z^{2} e^{2 x} \sin{\left (y \right )} + e^{x} e^{y} \cos{\left (z \right )}$$


    derivated by  y



$$z^{2} e^{2 x} \cos{\left (y \right )} + e^{x} e^{y} \cos{\left (z \right )}$$


    derivated by  z



$$2 z e^{2 x} \sin{\left (y \right )} - e^{x} e^{y} \sin{\left (z \right )}$$





$$\left [ 2 z^{2} e^{2 x} \sin{\left (y \right )} + e^{x} e^{y} \cos{\left (z \right )}, \quad z^{2} e^{2 x} \cos{\left (y \right )} + e^{x} e^{y} \cos{\left (z \right )}, \quad 2 z e^{2 x} \sin{\left (y \right )} - e^{x} e^{y} \sin{\left (z \right )}\right ]$$



## Total derivative


```python
def total_derivative(expr, replacements, symbol):
    print("Original expression: ")
    display(expr)
    expr = expr.subs(replacements)
    return get_diff(expr, symbol)[0]
```


```python
x, y, t = symbols('x y t')
expr = pi * y * x ** 2
df = total_derivative(expr, {y: t ** 2 - 1, x : t ** 2 + 1}, t)
expand(df)
```

    Original expression: 



$$\pi x^{2} y$$


    Expression:



$$\pi \left(t^{2} - 1\right) \left(t^{2} + 1\right)^{2}$$


    derivated by  t



$$4 \pi t \left(t^{2} - 1\right) \left(t^{2} + 1\right) + 2 \pi t \left(t^{2} + 1\right)^{2}$$





$$6 \pi t^{5} + 4 \pi t^{3} - 2 \pi t$$




```python
expr = 4 * pi * t * ( t ** 2 + 1 ) * (t ** 2 - 1 ) + 2 * pi * t * ( t ** 2 + 1 ) ** 2
display(expr)
expand(expr)
# Good one!
```


$$4 \pi t \left(t^{2} - 1\right) \left(t^{2} + 1\right) + 2 \pi t \left(t^{2} + 1\right)^{2}$$





$$6 \pi t^{5} + 4 \pi t^{3} - 2 \pi t$$




```python
expand(2 * pi * (t ** 2 + 1 ) ** 2 * (t ** 2 - 1) + pi * (t ** 2 + 1 ) ** 2 * ( t * 2 - 1))
# Not good one
```




$$2 \pi t^{6} + 2 \pi t^{5} + \pi t^{4} + 4 \pi t^{3} - 4 \pi t^{2} + 2 \pi t - 3 \pi$$




```python
expand(4 * pi * t * ((t ** 2) + 1 ) ** 2 + 2 * pi * t * ( (t ** 2) + 1 ) ** 2)
# Not good one
```




$$6 \pi t^{5} + 12 \pi t^{3} + 6 \pi t$$




```python
x, y, z, t = symbols('x y z t')
expr = cos(x) * sin(y) * exp(2 * z)
df = total_derivative(expr, {x : t + 1, y : t - 1, z: t ** 2}, t)
factor(df)
```

    Original expression: 



$$e^{2 z} \sin{\left (y \right )} \cos{\left (x \right )}$$


    Expression:



$$e^{2 t^{2}} \sin{\left (t - 1 \right )} \cos{\left (t + 1 \right )}$$


    derivated by  t



$$4 t e^{2 t^{2}} \sin{\left (t - 1 \right )} \cos{\left (t + 1 \right )} - e^{2 t^{2}} \sin{\left (t - 1 \right )} \sin{\left (t + 1 \right )} + e^{2 t^{2}} \cos{\left (t - 1 \right )} \cos{\left (t + 1 \right )}$$





$$\left(4 t \sin{\left (t - 1 \right )} \cos{\left (t + 1 \right )} - \sin{\left (t - 1 \right )} \sin{\left (t + 1 \right )} + \cos{\left (t - 1 \right )} \cos{\left (t + 1 \right )}\right) e^{2 t^{2}}$$



## Jacobian


```python
x, y = symbols('x y')
expr = y * x ** 2 + x * y * 3 / 4 + 10
get_diff(expr, x,y)
```

    Expression:



$$x^{2} y + \frac{3 x}{4} y + 10$$


    derivated by  x



$$2 x y + \frac{3 y}{4}$$


    derivated by  y



$$x^{2} + \frac{3 x}{4}$$





$$\left [ 2 x y + \frac{3 y}{4}, \quad x^{2} + \frac{3 x}{4}\right ]$$




```python
x, y = symbols('x y')
expr = exp(x) * cos(y) + x * exp(3 * y) - 2
get_diff(expr, x,y)
```

    Expression:



$$x e^{3 y} + e^{x} \cos{\left (y \right )} - 2$$


    derivated by  x



$$e^{x} \cos{\left (y \right )} + e^{3 y}$$


    derivated by  y



$$3 x e^{3 y} - e^{x} \sin{\left (y \right )}$$





$$\left [ e^{x} \cos{\left (y \right )} + e^{3 y}, \quad 3 x e^{3 y} - e^{x} \sin{\left (y \right )}\right ]$$




```python
x, y, z = symbols('x y z')
expr = exp(x) * cos(y) + x**2 * y**2 * z**2
get_diff(expr, x,y,z)
```

    Expression:



$$x^{2} y^{2} z^{2} + e^{x} \cos{\left (y \right )}$$


    derivated by  x



$$2 x y^{2} z^{2} + e^{x} \cos{\left (y \right )}$$


    derivated by  y



$$2 x^{2} y z^{2} - e^{x} \sin{\left (y \right )}$$


    derivated by  z



$$2 x^{2} y^{2} z$$





$$\left [ 2 x y^{2} z^{2} + e^{x} \cos{\left (y \right )}, \quad 2 x^{2} y z^{2} - e^{x} \sin{\left (y \right )}, \quad 2 x^{2} y^{2} z\right ]$$




```python
x, y, z = symbols('x y z')
expr = x **2 + 3 * exp(y) * exp(z) + cos(x) * sin(z)
J = get_diff(expr, x,y,z)
list(map(lambda j : j.evalf(subs={x: 0, y:0, z:0}), J))
```

    Expression:



$$x^{2} + 3 e^{y} e^{z} + \sin{\left (z \right )} \cos{\left (x \right )}$$


    derivated by  x



$$2 x - \sin{\left (x \right )} \sin{\left (z \right )}$$


    derivated by  y



$$3 e^{y} e^{z}$$


    derivated by  z



$$3 e^{y} e^{z} + \cos{\left (x \right )} \cos{\left (z \right )}$$





$$\left [ 0, \quad 3.0, \quad 4.0\right ]$$




```python
x, y, z = symbols('x y z')
expr = x * exp(y) * cos(z) + 5 * x ** 2 * sin(y) * exp(z)
J = get_diff(expr, x,y,z)
list(map(lambda j : j.evalf(subs={x: 0, y:0, z:0}), J))
```

    Expression:



$$5 x^{2} e^{z} \sin{\left (y \right )} + x e^{y} \cos{\left (z \right )}$$


    derivated by  x



$$10 x e^{z} \sin{\left (y \right )} + e^{y} \cos{\left (z \right )}$$


    derivated by  y



$$5 x^{2} e^{z} \cos{\left (y \right )} + x e^{y} \cos{\left (z \right )}$$


    derivated by  z



$$5 x^{2} e^{z} \sin{\left (y \right )} - x e^{y} \sin{\left (z \right )}$$





$$\left [ 1.0, \quad 0, \quad 0\right ]$$




```python

```

18d7790106532a7d78949391b4ee106f37394ce0
ipynb
Jupyter Notebook
Certification 2/Week2.1 - Differential with multiple variable.ipynb
The-Brains/MathForMachineLearning
5cbd9006f166059efaa2f312b741e64ce584aa1f
MIT
2018-04-16T02:53:59.000Z
2021-05-16T06:51:57.000Z
Certification 2/Week2.1 - Differential with multiple variable.ipynb
The-Brains/MathForMachineLearning
5cbd9006f166059efaa2f312b741e64ce584aa1f
MIT
Certification 2/Week2.1 - Differential with multiple variable.ipynb
The-Brains/MathForMachineLearning
5cbd9006f166059efaa2f312b741e64ce584aa1f
MIT
2019-05-20T02:06:55.000Z
2020-05-18T06:21:41.000Z
Qwen/Qwen-72B
1. YES
2. YES