```python
import numpy as np
```


```python
from sympy import *
```


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

### Calculate the Jacobian of the function
- f(x,y,z) = (x^2)*cos(y) + exp(z)*sin(y) 
- evaluate at the point (x,y,z)=(π,π,1).


```python
f = x**2*cos(y) + exp(z)*sin(y)
```


```python
J = np.array(
    [diff(f, x), diff(f, y), diff(f, z)])

print J
```

    [2*x*cos(y) -x**2*sin(y) + exp(z)*cos(y) exp(z)*sin(y)]



```python
print np.array([
    diff(f, x).subs({x:pi, y:pi, z:1}),
    diff(f, y).subs({x:pi, y:pi, z:1}),
    diff(f, z).subs({x:pi, y:pi, z:1})])
```

    [-2*pi -E 0]


### Calculate the Jacobian of the vector valued functions

- u(x,y) = (x^2)*y − cos(x)*sin(y) and v(x,y)=exp(x+y)
- evaluate at the point (0,π)


```python
u = x**2*y - cos(x)*sin(y)
v = exp(x+y)
```


```python
J = np.array([
    [diff(u, x), diff(u, y)],
    [diff(v, x), diff(v, y)]
])
    
print J
```

    [[2*x*y + sin(x)*sin(y) x**2 - cos(x)*cos(y)]
     [exp(x + y) exp(x + y)]]



```python
print np.array([
    [diff(u, x).subs({x:0, y:pi}), diff(u, y).subs({x:0, y:pi})],
    [diff(v, x).subs({x:0, y:pi}), diff(v, y).subs({x:0, y:pi})]
])
```

    [[0 1]
     [exp(pi) exp(pi)]]


### Calculate the Hessian for the function.
- f(x,y) = (x^3)*cos(y) − x*sin(y)


```python
f = x**3*cos(y) + x*sin(y)
```


```python
H = np.array([
    [diff(diff(f, x), x), diff(diff(f, x), y)],
    [diff(diff(f, y), x), diff(diff(f, y), y)]
])

print H
```

    [[6*x*cos(y) -3*x**2*sin(y) + cos(y)]
     [-3*x**2*sin(y) + cos(y) -x**3*cos(y) - x*sin(y)]]


### Calculate the Hessian for the function 
- f(x,y,z) = xy + sin(y)sin(z) + (z^3)*exp(x)


```python
f = x*y + sin(y)*sin(z) + (z**3)*exp(x)
```


```python
H = np.array([
    [diff(diff(f, x), x), diff(diff(f, x), y), diff(diff(f, x), z)],
    [diff(diff(f, y), x), diff(diff(f, y), y), diff(diff(f, y), z)],
    [diff(diff(f, z), x), diff(diff(f, z), y), diff(diff(f, z), z)]
])

print H
```

    [[z**3*exp(x) 1 3*z**2*exp(x)]
     [1 -sin(y)*sin(z) cos(y)*cos(z)]
     [3*z**2*exp(x) cos(y)*cos(z) 6*z*exp(x) - sin(y)*sin(z)]]


### Calculate the Hessian for the function
- f(x,y,z) = xycos(z) − sin(x)*exp(y)*(z^3)
- evaluate at the point (x,y,z)=(0,0,0)


```python
f = x*y*cos(z) - sin(x)*exp(y)*(z**3)
```


```python
H = np.array([
    [diff(diff(f, x), x), diff(diff(f, x), y), diff(diff(f, x), z)],
    [diff(diff(f, y), x), diff(diff(f, y), y), diff(diff(f, y), z)],
    [diff(diff(f, z), x), diff(diff(f, z), y), diff(diff(f, z), z)]
])

print H
```

    [[z**3*exp(y)*sin(x) -z**3*exp(y)*cos(x) + cos(z)
      -y*sin(z) - 3*z**2*exp(y)*cos(x)]
     [-z**3*exp(y)*cos(x) + cos(z) -z**3*exp(y)*sin(x)
      -x*sin(z) - 3*z**2*exp(y)*sin(x)]
     [-y*sin(z) - 3*z**2*exp(y)*cos(x) -x*sin(z) - 3*z**2*exp(y)*sin(x)
      -x*y*cos(z) - 6*z*exp(y)*sin(x)]]



```python
print np.array([
    [diff(diff(f, x), x).subs({x:0, y:0, z:0}), diff(diff(f, x), y).subs({x:0, y:0, z:0}), diff(diff(f, x), z).subs({x:0, y:0, z:0})],
    [diff(diff(f, y), x).subs({x:0, y:0, z:0}), diff(diff(f, y), y).subs({x:0, y:0, z:0}), diff(diff(f, y), z).subs({x:0, y:0, z:0})],
    [diff(diff(f, z), x).subs({x:0, y:0, z:0}), diff(diff(f, z), y).subs({x:0, y:0, z:0}), diff(diff(f, z), z).subs({x:0, y:0, z:0})]
])
```

    [[0 1 0]
     [1 0 0]
     [0 0 0]]


2870468adfa95078ec3927b9fd583b099f3db510
ipynb
Jupyter Notebook
Coursera_Mathematics_for_Machine Learning/2_Mathematics_for_Machine_Learning_Multivariate_Calculus/Week 2 - Jacobian - Hessian Matrix/Jacobian_Hessian.ipynb
dangkhoa1992/Coursera-Advanced-Machine-Learning-Specialization
c5aa17b0131d9813fe00da2de82b5c0db8bc1130
Unlicense
2018-08-18T17:26:53.000Z
2018-08-18T17:26:53.000Z
Coursera_Mathematics_for_Machine Learning/2_Mathematics_for_Machine_Learning_Multivariate_Calculus/Week 2 - Jacobian - Hessian Matrix/Jacobian_Hessian.ipynb
dangkhoa1992/Coursera-Advanced-Machine-Learning-Specialization
c5aa17b0131d9813fe00da2de82b5c0db8bc1130
Unlicense
Coursera_Mathematics_for_Machine Learning/2_Mathematics_for_Machine_Learning_Multivariate_Calculus/Week 2 - Jacobian - Hessian Matrix/Jacobian_Hessian.ipynb
dangkhoa1992/Coursera-Advanced-Machine-Learning-Specialization
c5aa17b0131d9813fe00da2de82b5c0db8bc1130
Unlicense
2018-08-15T21:01:55.000Z
2018-11-05T06:44:07.000Z
Qwen/Qwen-72B
1. YES
2. YES