```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
def evaluate_at(exprs, symbols):
    results = []
    def process_list(exprs, symbols):
        r = []
        for expr in exprs:
            if isinstance(expr, list):
                r.append(process_list(expr, symbols))
            else:
                r.append(expr.subs(symbols))
        return r

    results = []
    if isinstance(exprs, list):
        results.append(process_list(exprs, symbols))
    else:
        results.append(exprs.subs(symbols))
    return results
```


```python
x, y,z = symbols('x y z')
s = [x, y, z]
expr = x ** 2 * cos(y) + exp(z) * sin(y)
j = get_jacobian([expr], s)
r = evaluate_at(j, {x: pi, y: pi, z: 1})
r
```

    Expressions:



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





$$\left [ \left [ \left [ - 2 \pi, \quad - e, \quad 0\right ]\right ]\right ]$$




```python
x, y = symbols('x y')
s = [x, y]
j = get_jacobian([
    x**2 * y - cos(x) * sin(y),
    exp(x + y)
], s)
r = evaluate_at(j, {x: 0, y: pi})
r
```

    Expressions:



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



$$e^{x + y}$$





$$\left [ \left [ \left [ 0, \quad 1\right ], \quad \left [ e^{\pi}, \quad e^{\pi}\right ]\right ]\right ]$$




```python
x, y = symbols('x y')
s = [x, y]
j = get_jacobian([
    x ** 3 * cos(y) - x * sin(y)
], s)
h = get_hessians(j, s)
h
```

    Expressions:



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





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




```python
x, y, z = symbols('x y z')
s = [x, y, z]
j = get_jacobian([
    x * y + sin(y) * sin(z) + z ** 3 * exp(x)
], s)
h = get_hessians(j, s)
h
```

    Expressions:



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





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




```python
x, y, z = symbols('x y z')
s = [x, y, z]
j = get_jacobian([
    x * y *  cos(z) - sin(x) * exp(y) * z ** 3
], s)
h = get_hessians(j, s)
evaluate_at(h, {x: 0, y: 0, z: 0 })
```

    Expressions:



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





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




```python

```

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