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


```python
def get_jacobian(expressions, symbols):
    rows = len(expressions)
    columns = len(symbols)

    print("Expressions:")
    for expression in expressions:
        display(expression)

    results = [[0 for x in range(columns)] for y in range(rows)] 
    for row, expression in enumerate(expressions):
        for column, symbol in enumerate(symbols):
#             print('Row %d, column %d, expression: %s, symbol: %s' % (row, column, expression, symbol))
            df = diff(expression, symbol)
#             print("DF: %s" % df)
            results[row][column] = df
    return results
```


```python
def get_hessians(jacobian, symbols):
    jacobian = np.array(jacobian)
    rows, columns = jacobian.shape
    hessians = [[[None for z in range(len(symbols))] for x in range(columns)] for y in range(rows)] 
    for row in range(rows):
        for column in range(columns):
            for index, symbol in enumerate(symbols):
                df = diff(jacobian[row, column], symbol)
                hessians[row][column][index] = df
            
    return hessians
```


```python
x, y = symbols('x y')
j = get_jacobian([
    y * x ** 3 + x + 2 * y
], [x, y])
display(j)
h = get_hessians(j, [x, y])
display(h)
```

    Expressions:



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



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



$$\left [ \left [ \left [ 6 x y, \quad 3 x^{2}\right ], \quad \left [ 3 x^{2}, \quad 0\right ]\right ]\right ]$$



```python
x, y = symbols('x y')
j = get_jacobian([
    exp(x) * cos(y)
], [x, y])
display(j)
h = get_hessians(j, [x, y])
display(h)
```

    Expressions:



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



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



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



```python
x, y = symbols('x y')
j = get_jacobian([
    x ** 2 /2 + x * y + y ** 2 / 2
], [x, y])
display(j)
h = get_hessians(j, [x, y])
display(h)
```

    Expressions:



$$\frac{x^{2}}{2} + x y + \frac{y^{2}}{2}$$



$$\left [ \left [ x + y, \quad x + y\right ]\right ]$$



$$\left [ \left [ \left [ 1, \quad 1\right ], \quad \left [ 1, \quad 1\right ]\right ]\right ]$$



```python
x, y, z = symbols('x y z')
j = get_jacobian([
    x ** 2 * exp(-y) * cos(z)
], [x, y, z])
display(j)
h = get_hessians(j, [x, y, z])
display(h)
```

    Expressions:



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



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



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



```python
x, y, z = symbols('x y z')
j = get_jacobian([
    x * exp(y) + y ** 2 * cos(z)
], [x, y, z])
display(j)
h = get_hessians(j, [x, y, z])
display(h)
```

    Expressions:



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



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



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



```python

```

fc620867f42d8c2f83c822bc989a3cadbf3b2295
ipynb
Jupyter Notebook
Certification 2/Week2.4 - Hessians.ipynb
The-Brains/MathForMachineLearning
5cbd9006f166059efaa2f312b741e64ce584aa1f
MIT
2018-04-16T02:53:59.000Z
2021-05-16T06:51:57.000Z
Certification 2/Week2.4 - Hessians.ipynb
The-Brains/MathForMachineLearning
5cbd9006f166059efaa2f312b741e64ce584aa1f
MIT
Certification 2/Week2.4 - Hessians.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