```python
import sympy as sym
import symnum
import symnum.diffops.symbolic as sym_diffops
import symnum.numpy as snp
import numpy as np
import multiprocessing
```


```python
y = snp.array([1, 0])
σ = 1

def forward_func(x):
    return snp.array([[1, -1], [-2, 2]]) @ snp.array(
        [snp.cos(-x[1]**2 + 3 * x[0]), snp.sin(x[0] - 1)])

def neg_log_dens(x):
    return (
        snp.sum((y - forward_func(x))**2 / σ**2) + 
        snp.sum(x**2)) / 2
```

## Symbolic evaluation and derivatives


```python
x = symnum.named_array('x', 2)
v = symnum.named_array('v', 2)
m = symnum.named_array('m', (2, 2))
```


```python
forward_func(x)
```




$\displaystyle \left[\begin{matrix}- \sin{\left(x[0] - 1 \right)} + \cos{\left(3 x[0] - x[1]^{2} \right)} & 2 \sin{\left(x[0] - 1 \right)} - 2 \cos{\left(3 x[0] - x[1]^{2} \right)}\end{matrix}\right]$




```python
sym_diffops.jacobian(forward_func)(x)
```




$\displaystyle \left[\begin{matrix}- 3 \sin{\left(3 x[0] - x[1]^{2} \right)} - \cos{\left(x[0] - 1 \right)} & 2 x[1] \sin{\left(3 x[0] - x[1]^{2} \right)}\\6 \sin{\left(3 x[0] - x[1]^{2} \right)} + 2 \cos{\left(x[0] - 1 \right)} & - 4 x[1] \sin{\left(3 x[0] - x[1]^{2} \right)}\end{matrix}\right]$




```python
sym_diffops.vector_jacobian_product(forward_func)(x)(v)
```




$\displaystyle \left[\begin{matrix}v[0] \left(- 3 \sin{\left(3 x[0] - x[1]^{2} \right)} - \cos{\left(x[0] - 1 \right)}\right) + v[1] \left(6 \sin{\left(3 x[0] - x[1]^{2} \right)} + 2 \cos{\left(x[0] - 1 \right)}\right) & 2 v[0] x[1] \sin{\left(3 x[0] - x[1]^{2} \right)} - 4 v[1] x[1] \sin{\left(3 x[0] - x[1]^{2} \right)}\end{matrix}\right]$




```python
sym_diffops.jacobian_vector_product(forward_func)(x)(v)
```




$\displaystyle \left[\begin{matrix}v[0] \left(- 3 \sin{\left(3 x[0] - x[1]^{2} \right)} - \cos{\left(x[0] - 1 \right)}\right) + 2 v[1] x[1] \sin{\left(3 x[0] - x[1]^{2} \right)} & v[0] \left(6 \sin{\left(3 x[0] - x[1]^{2} \right)} + 2 \cos{\left(x[0] - 1 \right)}\right) - 4 v[1] x[1] \sin{\left(3 x[0] - x[1]^{2} \right)}\end{matrix}\right]$




```python
sym_diffops.matrix_hessian_product(forward_func)(x)(m)
```




$\displaystyle \left[\begin{matrix}m[0, 0] \left(\sin{\left(x[0] - 1 \right)} - 9 \cos{\left(3 x[0] - x[1]^{2} \right)}\right) + 6 m[0, 1] x[1] \cos{\left(3 x[0] - x[1]^{2} \right)} + m[1, 0] \left(- 2 \sin{\left(x[0] - 1 \right)} + 18 \cos{\left(3 x[0] - x[1]^{2} \right)}\right) - 12 m[1, 1] x[1] \cos{\left(3 x[0] - x[1]^{2} \right)} & 6 m[0, 0] x[1] \cos{\left(3 x[0] - x[1]^{2} \right)} + m[0, 1] \left(- 4 x[1]^{2} \cos{\left(3 x[0] - x[1]^{2} \right)} + 2 \sin{\left(3 x[0] - x[1]^{2} \right)}\right) - 12 m[1, 0] x[1] \cos{\left(3 x[0] - x[1]^{2} \right)} + m[1, 1] \left(8 x[1]^{2} \cos{\left(3 x[0] - x[1]^{2} \right)} - 4 \sin{\left(3 x[0] - x[1]^{2} \right)}\right)\end{matrix}\right]$




```python
neg_log_dens(x)
```




$\displaystyle \frac{x[0]^{2}}{2} + \frac{x[1]^{2}}{2} + \frac{\left(- 2 \sin{\left(x[0] - 1 \right)} + 2 \cos{\left(3 x[0] - x[1]^{2} \right)}\right)^{2}}{2} + \frac{\left(\sin{\left(x[0] - 1 \right)} - \cos{\left(3 x[0] - x[1]^{2} \right)} + 1\right)^{2}}{2}$




```python
sym_diffops.grad(neg_log_dens)(x)
```




$\displaystyle \left[\begin{matrix}x[0] + \frac{\left(- 2 \sin{\left(x[0] - 1 \right)} + 2 \cos{\left(3 x[0] - x[1]^{2} \right)}\right) \left(- 12 \sin{\left(3 x[0] - x[1]^{2} \right)} - 4 \cos{\left(x[0] - 1 \right)}\right)}{2} + \frac{\left(6 \sin{\left(3 x[0] - x[1]^{2} \right)} + 2 \cos{\left(x[0] - 1 \right)}\right) \left(\sin{\left(x[0] - 1 \right)} - \cos{\left(3 x[0] - x[1]^{2} \right)} + 1\right)}{2} & 4 x[1] \left(- 2 \sin{\left(x[0] - 1 \right)} + 2 \cos{\left(3 x[0] - x[1]^{2} \right)}\right) \sin{\left(3 x[0] - x[1]^{2} \right)} - 2 x[1] \left(\sin{\left(x[0] - 1 \right)} - \cos{\left(3 x[0] - x[1]^{2} \right)} + 1\right) \sin{\left(3 x[0] - x[1]^{2} \right)} + x[1]\end{matrix}\right]$




```python
sym_diffops.hessian(neg_log_dens)(x)
```




$\displaystyle \left[\begin{matrix}\left(- 2 \sin{\left(x[0] - 1 \right)} + 2 \cos{\left(3 x[0] - x[1]^{2} \right)}\right) \left(2 \sin{\left(x[0] - 1 \right)} - 18 \cos{\left(3 x[0] - x[1]^{2} \right)}\right) + \left(- \sin{\left(x[0] - 1 \right)} + 9 \cos{\left(3 x[0] - x[1]^{2} \right)}\right) \left(\sin{\left(x[0] - 1 \right)} - \cos{\left(3 x[0] - x[1]^{2} \right)} + 1\right) + \left(- 6 \sin{\left(3 x[0] - x[1]^{2} \right)} - 2 \cos{\left(x[0] - 1 \right)}\right)^{2} + \left(3 \sin{\left(3 x[0] - x[1]^{2} \right)} + \cos{\left(x[0] - 1 \right)}\right)^{2} + 1 & 12 x[1] \left(- 2 \sin{\left(x[0] - 1 \right)} + 2 \cos{\left(3 x[0] - x[1]^{2} \right)}\right) \cos{\left(3 x[0] - x[1]^{2} \right)} + 4 x[1] \left(- 6 \sin{\left(3 x[0] - x[1]^{2} \right)} - 2 \cos{\left(x[0] - 1 \right)}\right) \sin{\left(3 x[0] - x[1]^{2} \right)} - 2 x[1] \left(3 \sin{\left(3 x[0] - x[1]^{2} \right)} + \cos{\left(x[0] - 1 \right)}\right) \sin{\left(3 x[0] - x[1]^{2} \right)} - 6 x[1] \left(\sin{\left(x[0] - 1 \right)} - \cos{\left(3 x[0] - x[1]^{2} \right)} + 1\right) \cos{\left(3 x[0] - x[1]^{2} \right)}\\12 x[1] \left(- 2 \sin{\left(x[0] - 1 \right)} + 2 \cos{\left(3 x[0] - x[1]^{2} \right)}\right) \cos{\left(3 x[0] - x[1]^{2} \right)} + 4 x[1] \left(- 6 \sin{\left(3 x[0] - x[1]^{2} \right)} - 2 \cos{\left(x[0] - 1 \right)}\right) \sin{\left(3 x[0] - x[1]^{2} \right)} - 2 x[1] \left(3 \sin{\left(3 x[0] - x[1]^{2} \right)} + \cos{\left(x[0] - 1 \right)}\right) \sin{\left(3 x[0] - x[1]^{2} \right)} - 6 x[1] \left(\sin{\left(x[0] - 1 \right)} - \cos{\left(3 x[0] - x[1]^{2} \right)} + 1\right) \cos{\left(3 x[0] - x[1]^{2} \right)} & - 8 x[1]^{2} \left(- 2 \sin{\left(x[0] - 1 \right)} + 2 \cos{\left(3 x[0] - x[1]^{2} \right)}\right) \cos{\left(3 x[0] - x[1]^{2} \right)} + 4 x[1]^{2} \left(\sin{\left(x[0] - 1 \right)} - \cos{\left(3 x[0] - x[1]^{2} \right)} + 1\right) \cos{\left(3 x[0] - x[1]^{2} \right)} + 20 x[1]^{2} \sin^{2}{\left(3 x[0] - x[1]^{2} \right)} + \left(- 8 \sin{\left(x[0] - 1 \right)} + 8 \cos{\left(3 x[0] - x[1]^{2} \right)}\right) \sin{\left(3 x[0] - x[1]^{2} \right)} + \left(- 2 \sin{\left(x[0] - 1 \right)} + 2 \cos{\left(3 x[0] - x[1]^{2} \right)} - 2\right) \sin{\left(3 x[0] - x[1]^{2} \right)} + 1\end{matrix}\right]$




```python
sym_diffops.hessian_vector_product(neg_log_dens)(x)(v)
```




$\displaystyle \left[\begin{matrix}v[0] \left(\left(- 2 \sin{\left(x[0] - 1 \right)} + 2 \cos{\left(3 x[0] - x[1]^{2} \right)}\right) \left(2 \sin{\left(x[0] - 1 \right)} - 18 \cos{\left(3 x[0] - x[1]^{2} \right)}\right) + \left(- \sin{\left(x[0] - 1 \right)} + 9 \cos{\left(3 x[0] - x[1]^{2} \right)}\right) \left(\sin{\left(x[0] - 1 \right)} - \cos{\left(3 x[0] - x[1]^{2} \right)} + 1\right) + \left(- 6 \sin{\left(3 x[0] - x[1]^{2} \right)} - 2 \cos{\left(x[0] - 1 \right)}\right)^{2} + \left(3 \sin{\left(3 x[0] - x[1]^{2} \right)} + \cos{\left(x[0] - 1 \right)}\right)^{2} + 1\right) + v[1] \left(12 x[1] \left(- 2 \sin{\left(x[0] - 1 \right)} + 2 \cos{\left(3 x[0] - x[1]^{2} \right)}\right) \cos{\left(3 x[0] - x[1]^{2} \right)} + 4 x[1] \left(- 6 \sin{\left(3 x[0] - x[1]^{2} \right)} - 2 \cos{\left(x[0] - 1 \right)}\right) \sin{\left(3 x[0] - x[1]^{2} \right)} - 2 x[1] \left(3 \sin{\left(3 x[0] - x[1]^{2} \right)} + \cos{\left(x[0] - 1 \right)}\right) \sin{\left(3 x[0] - x[1]^{2} \right)} - 6 x[1] \left(\sin{\left(x[0] - 1 \right)} - \cos{\left(3 x[0] - x[1]^{2} \right)} + 1\right) \cos{\left(3 x[0] - x[1]^{2} \right)}\right) & v[0] \left(12 x[1] \left(- 2 \sin{\left(x[0] - 1 \right)} + 2 \cos{\left(3 x[0] - x[1]^{2} \right)}\right) \cos{\left(3 x[0] - x[1]^{2} \right)} + 4 x[1] \left(- 6 \sin{\left(3 x[0] - x[1]^{2} \right)} - 2 \cos{\left(x[0] - 1 \right)}\right) \sin{\left(3 x[0] - x[1]^{2} \right)} - 2 x[1] \left(3 \sin{\left(3 x[0] - x[1]^{2} \right)} + \cos{\left(x[0] - 1 \right)}\right) \sin{\left(3 x[0] - x[1]^{2} \right)} - 6 x[1] \left(\sin{\left(x[0] - 1 \right)} - \cos{\left(3 x[0] - x[1]^{2} \right)} + 1\right) \cos{\left(3 x[0] - x[1]^{2} \right)}\right) + v[1] \left(- 8 x[1]^{2} \left(- 2 \sin{\left(x[0] - 1 \right)} + 2 \cos{\left(3 x[0] - x[1]^{2} \right)}\right) \cos{\left(3 x[0] - x[1]^{2} \right)} + 4 x[1]^{2} \left(\sin{\left(x[0] - 1 \right)} - \cos{\left(3 x[0] - x[1]^{2} \right)} + 1\right) \cos{\left(3 x[0] - x[1]^{2} \right)} + 20 x[1]^{2} \sin^{2}{\left(3 x[0] - x[1]^{2} \right)} + \left(- 8 \sin{\left(x[0] - 1 \right)} + 8 \cos{\left(3 x[0] - x[1]^{2} \right)}\right) \sin{\left(3 x[0] - x[1]^{2} \right)} + \left(- 2 \sin{\left(x[0] - 1 \right)} + 2 \cos{\left(3 x[0] - x[1]^{2} \right)} - 2\right) \sin{\left(3 x[0] - x[1]^{2} \right)} + 1\right)\end{matrix}\right]$




```python
sym_diffops.matrix_tressian_product(neg_log_dens)(x)(m)
```




$\displaystyle \left[\begin{matrix}m[0, 0] \left(\left(- 2 \sin{\left(x[0] - 1 \right)} + 2 \cos{\left(3 x[0] - x[1]^{2} \right)}\right) \left(54 \sin{\left(3 x[0] - x[1]^{2} \right)} + 2 \cos{\left(x[0] - 1 \right)}\right) + \left(- 2 \sin{\left(x[0] - 1 \right)} + 18 \cos{\left(3 x[0] - x[1]^{2} \right)}\right) \left(3 \sin{\left(3 x[0] - x[1]^{2} \right)} + \cos{\left(x[0] - 1 \right)}\right) + \left(- \sin{\left(x[0] - 1 \right)} + 9 \cos{\left(3 x[0] - x[1]^{2} \right)}\right) \left(3 \sin{\left(3 x[0] - x[1]^{2} \right)} + \cos{\left(x[0] - 1 \right)}\right) + \left(2 \sin{\left(x[0] - 1 \right)} - 18 \cos{\left(3 x[0] - x[1]^{2} \right)}\right) \left(- 6 \sin{\left(3 x[0] - x[1]^{2} \right)} - 2 \cos{\left(x[0] - 1 \right)}\right) + \left(4 \sin{\left(x[0] - 1 \right)} - 36 \cos{\left(3 x[0] - x[1]^{2} \right)}\right) \left(- 6 \sin{\left(3 x[0] - x[1]^{2} \right)} - 2 \cos{\left(x[0] - 1 \right)}\right) + \left(- 27 \sin{\left(3 x[0] - x[1]^{2} \right)} - \cos{\left(x[0] - 1 \right)}\right) \left(\sin{\left(x[0] - 1 \right)} - \cos{\left(3 x[0] - x[1]^{2} \right)} + 1\right)\right) + m[0, 1] \left(- 36 x[1] \left(- 2 \sin{\left(x[0] - 1 \right)} + 2 \cos{\left(3 x[0] - x[1]^{2} \right)}\right) \sin{\left(3 x[0] - x[1]^{2} \right)} - 2 x[1] \left(- \sin{\left(x[0] - 1 \right)} + 9 \cos{\left(3 x[0] - x[1]^{2} \right)}\right) \sin{\left(3 x[0] - x[1]^{2} \right)} + 4 x[1] \left(2 \sin{\left(x[0] - 1 \right)} - 18 \cos{\left(3 x[0] - x[1]^{2} \right)}\right) \sin{\left(3 x[0] - x[1]^{2} \right)} + 24 x[1] \left(- 6 \sin{\left(3 x[0] - x[1]^{2} \right)} - 2 \cos{\left(x[0] - 1 \right)}\right) \cos{\left(3 x[0] - x[1]^{2} \right)} - 12 x[1] \left(3 \sin{\left(3 x[0] - x[1]^{2} \right)} + \cos{\left(x[0] - 1 \right)}\right) \cos{\left(3 x[0] - x[1]^{2} \right)} + 18 x[1] \left(\sin{\left(x[0] - 1 \right)} - \cos{\left(3 x[0] - x[1]^{2} \right)} + 1\right) \sin{\left(3 x[0] - x[1]^{2} \right)}\right) + m[1, 0] \left(- 36 x[1] \left(- 2 \sin{\left(x[0] - 1 \right)} + 2 \cos{\left(3 x[0] - x[1]^{2} \right)}\right) \sin{\left(3 x[0] - x[1]^{2} \right)} - 2 x[1] \left(- \sin{\left(x[0] - 1 \right)} + 9 \cos{\left(3 x[0] - x[1]^{2} \right)}\right) \sin{\left(3 x[0] - x[1]^{2} \right)} + 4 x[1] \left(2 \sin{\left(x[0] - 1 \right)} - 18 \cos{\left(3 x[0] - x[1]^{2} \right)}\right) \sin{\left(3 x[0] - x[1]^{2} \right)} + 24 x[1] \left(- 6 \sin{\left(3 x[0] - x[1]^{2} \right)} - 2 \cos{\left(x[0] - 1 \right)}\right) \cos{\left(3 x[0] - x[1]^{2} \right)} - 12 x[1] \left(3 \sin{\left(3 x[0] - x[1]^{2} \right)} + \cos{\left(x[0] - 1 \right)}\right) \cos{\left(3 x[0] - x[1]^{2} \right)} + 18 x[1] \left(\sin{\left(x[0] - 1 \right)} - \cos{\left(3 x[0] - x[1]^{2} \right)} + 1\right) \sin{\left(3 x[0] - x[1]^{2} \right)}\right) + m[1, 1] \left(24 x[1]^{2} \left(- 2 \sin{\left(x[0] - 1 \right)} + 2 \cos{\left(3 x[0] - x[1]^{2} \right)}\right) \sin{\left(3 x[0] - x[1]^{2} \right)} - 8 x[1]^{2} \left(- 6 \sin{\left(3 x[0] - x[1]^{2} \right)} - 2 \cos{\left(x[0] - 1 \right)}\right) \cos{\left(3 x[0] - x[1]^{2} \right)} + 4 x[1]^{2} \left(3 \sin{\left(3 x[0] - x[1]^{2} \right)} + \cos{\left(x[0] - 1 \right)}\right) \cos{\left(3 x[0] - x[1]^{2} \right)} - 12 x[1]^{2} \left(\sin{\left(x[0] - 1 \right)} - \cos{\left(3 x[0] - x[1]^{2} \right)} + 1\right) \sin{\left(3 x[0] - x[1]^{2} \right)} + 120 x[1]^{2} \sin{\left(3 x[0] - x[1]^{2} \right)} \cos{\left(3 x[0] - x[1]^{2} \right)} + 3 \left(- 8 \sin{\left(x[0] - 1 \right)} + 8 \cos{\left(3 x[0] - x[1]^{2} \right)}\right) \cos{\left(3 x[0] - x[1]^{2} \right)} + \left(- 24 \sin{\left(3 x[0] - x[1]^{2} \right)} - 8 \cos{\left(x[0] - 1 \right)}\right) \sin{\left(3 x[0] - x[1]^{2} \right)} + \left(- 6 \sin{\left(3 x[0] - x[1]^{2} \right)} - 2 \cos{\left(x[0] - 1 \right)}\right) \sin{\left(3 x[0] - x[1]^{2} \right)} + 3 \left(- 2 \sin{\left(x[0] - 1 \right)} + 2 \cos{\left(3 x[0] - x[1]^{2} \right)} - 2\right) \cos{\left(3 x[0] - x[1]^{2} \right)}\right) & m[0, 0] \left(- 36 x[1] \left(- 2 \sin{\left(x[0] - 1 \right)} + 2 \cos{\left(3 x[0] - x[1]^{2} \right)}\right) \sin{\left(3 x[0] - x[1]^{2} \right)} - 2 x[1] \left(- \sin{\left(x[0] - 1 \right)} + 9 \cos{\left(3 x[0] - x[1]^{2} \right)}\right) \sin{\left(3 x[0] - x[1]^{2} \right)} + 4 x[1] \left(2 \sin{\left(x[0] - 1 \right)} - 18 \cos{\left(3 x[0] - x[1]^{2} \right)}\right) \sin{\left(3 x[0] - x[1]^{2} \right)} + 24 x[1] \left(- 6 \sin{\left(3 x[0] - x[1]^{2} \right)} - 2 \cos{\left(x[0] - 1 \right)}\right) \cos{\left(3 x[0] - x[1]^{2} \right)} - 12 x[1] \left(3 \sin{\left(3 x[0] - x[1]^{2} \right)} + \cos{\left(x[0] - 1 \right)}\right) \cos{\left(3 x[0] - x[1]^{2} \right)} + 18 x[1] \left(\sin{\left(x[0] - 1 \right)} - \cos{\left(3 x[0] - x[1]^{2} \right)} + 1\right) \sin{\left(3 x[0] - x[1]^{2} \right)}\right) + m[0, 1] \left(24 x[1]^{2} \left(- 2 \sin{\left(x[0] - 1 \right)} + 2 \cos{\left(3 x[0] - x[1]^{2} \right)}\right) \sin{\left(3 x[0] - x[1]^{2} \right)} - 8 x[1]^{2} \left(- 6 \sin{\left(3 x[0] - x[1]^{2} \right)} - 2 \cos{\left(x[0] - 1 \right)}\right) \cos{\left(3 x[0] - x[1]^{2} \right)} + 4 x[1]^{2} \left(3 \sin{\left(3 x[0] - x[1]^{2} \right)} + \cos{\left(x[0] - 1 \right)}\right) \cos{\left(3 x[0] - x[1]^{2} \right)} - 12 x[1]^{2} \left(\sin{\left(x[0] - 1 \right)} - \cos{\left(3 x[0] - x[1]^{2} \right)} + 1\right) \sin{\left(3 x[0] - x[1]^{2} \right)} + 120 x[1]^{2} \sin{\left(3 x[0] - x[1]^{2} \right)} \cos{\left(3 x[0] - x[1]^{2} \right)} + \left(- 24 \sin{\left(x[0] - 1 \right)} + 24 \cos{\left(3 x[0] - x[1]^{2} \right)}\right) \cos{\left(3 x[0] - x[1]^{2} \right)} + \left(- 24 \sin{\left(3 x[0] - x[1]^{2} \right)} - 8 \cos{\left(x[0] - 1 \right)}\right) \sin{\left(3 x[0] - x[1]^{2} \right)} + \left(- 6 \sin{\left(3 x[0] - x[1]^{2} \right)} - 2 \cos{\left(x[0] - 1 \right)}\right) \sin{\left(3 x[0] - x[1]^{2} \right)} + \left(- 6 \sin{\left(x[0] - 1 \right)} + 6 \cos{\left(3 x[0] - x[1]^{2} \right)} - 6\right) \cos{\left(3 x[0] - x[1]^{2} \right)}\right) + m[1, 0] \left(24 x[1]^{2} \left(- 2 \sin{\left(x[0] - 1 \right)} + 2 \cos{\left(3 x[0] - x[1]^{2} \right)}\right) \sin{\left(3 x[0] - x[1]^{2} \right)} - 8 x[1]^{2} \left(- 6 \sin{\left(3 x[0] - x[1]^{2} \right)} - 2 \cos{\left(x[0] - 1 \right)}\right) \cos{\left(3 x[0] - x[1]^{2} \right)} + 4 x[1]^{2} \left(3 \sin{\left(3 x[0] - x[1]^{2} \right)} + \cos{\left(x[0] - 1 \right)}\right) \cos{\left(3 x[0] - x[1]^{2} \right)} - 12 x[1]^{2} \left(\sin{\left(x[0] - 1 \right)} - \cos{\left(3 x[0] - x[1]^{2} \right)} + 1\right) \sin{\left(3 x[0] - x[1]^{2} \right)} + 120 x[1]^{2} \sin{\left(3 x[0] - x[1]^{2} \right)} \cos{\left(3 x[0] - x[1]^{2} \right)} + \left(- 24 \sin{\left(x[0] - 1 \right)} + 24 \cos{\left(3 x[0] - x[1]^{2} \right)}\right) \cos{\left(3 x[0] - x[1]^{2} \right)} + \left(- 24 \sin{\left(3 x[0] - x[1]^{2} \right)} - 8 \cos{\left(x[0] - 1 \right)}\right) \sin{\left(3 x[0] - x[1]^{2} \right)} + \left(- 6 \sin{\left(3 x[0] - x[1]^{2} \right)} - 2 \cos{\left(x[0] - 1 \right)}\right) \sin{\left(3 x[0] - x[1]^{2} \right)} + \left(- 6 \sin{\left(x[0] - 1 \right)} + 6 \cos{\left(3 x[0] - x[1]^{2} \right)} - 6\right) \cos{\left(3 x[0] - x[1]^{2} \right)}\right) + m[1, 1] \left(- 16 x[1]^{3} \left(- 2 \sin{\left(x[0] - 1 \right)} + 2 \cos{\left(3 x[0] - x[1]^{2} \right)}\right) \sin{\left(3 x[0] - x[1]^{2} \right)} + 8 x[1]^{3} \left(\sin{\left(x[0] - 1 \right)} - \cos{\left(3 x[0] - x[1]^{2} \right)} + 1\right) \sin{\left(3 x[0] - x[1]^{2} \right)} - 120 x[1]^{3} \sin{\left(3 x[0] - x[1]^{2} \right)} \cos{\left(3 x[0] - x[1]^{2} \right)} - 2 x[1] \left(- 8 \sin{\left(x[0] - 1 \right)} + 8 \cos{\left(3 x[0] - x[1]^{2} \right)}\right) \cos{\left(3 x[0] - x[1]^{2} \right)} - 16 x[1] \left(- 2 \sin{\left(x[0] - 1 \right)} + 2 \cos{\left(3 x[0] - x[1]^{2} \right)}\right) \cos{\left(3 x[0] - x[1]^{2} \right)} - 2 x[1] \left(- 2 \sin{\left(x[0] - 1 \right)} + 2 \cos{\left(3 x[0] - x[1]^{2} \right)} - 2\right) \cos{\left(3 x[0] - x[1]^{2} \right)} + 8 x[1] \left(\sin{\left(x[0] - 1 \right)} - \cos{\left(3 x[0] - x[1]^{2} \right)} + 1\right) \cos{\left(3 x[0] - x[1]^{2} \right)} + 60 x[1] \sin^{2}{\left(3 x[0] - x[1]^{2} \right)}\right)\end{matrix}\right]$



## NumPy function generation


```python
x_np = np.array([0.2, 1.1])
v_np = np.array([1., -1.])
m_np = np.array([[1., 2.], [3., 4.]])
```


```python
forward_func_np = symnum.numpify(2)(forward_func)
forward_func_np(x_np)
```




    array([ 1.53700411, -3.07400822])




```python
neg_log_dens_np = symnum.numpify(2)(neg_log_dens)
neg_log_dens_np(x_np)
```




    5.493949967002545




```python
symnum.grad(neg_log_dens_np)(x_np)
```




    array([ 7.03139355, -7.32518763])




```python
symnum.hessian(neg_log_dens_np)(x_np)
```




    array([[-47.88846093,  29.72428278],
           [ 29.72428278, -25.23749599]])




```python
symnum.hessian_vector_product(neg_log_dens_np)(x_np)(v_np)
```




    array([-77.61274371,  54.96177876])




```python
symnum.matrix_tressian_product(neg_log_dens_np)(x_np)(m_np)
```




    array([ 219.62500437, -142.62315559])




```python
symnum.jacobian(forward_func_np)(x_np)
```




    array([[ 1.02189567, -1.26030841],
           [-2.04379134,  2.52061682]])




```python
symnum.jacobian_vector_product(forward_func_np)(x_np)(v_np)
```




    array([ 2.28220408, -4.56440817])




```python
symnum.vector_jacobian_product(forward_func_np)(x_np)(v_np)
```




    array([ 3.06568701, -3.78092524])




```python
symnum.matrix_hessian_product(forward_func_np)(x_np)(m_np)
```




    array([8.01287975, 3.62860337])



## Compatibility with `multiprocessing`


```python
grad_neg_log_dens = symnum.grad(neg_log_dens_np)
```


```python
pool = multiprocessing.Pool(4)
```


```python
pool.map(grad_neg_log_dens, [x_np + 0.1 * i for i in range(8)])
```




    [array([ 7.03139355, -7.32518763]),
     array([ 5.36166701, -6.83240645]),
     array([ 4.05701734, -6.32916535]),
     array([ 3.1698736 , -5.90591748]),
     array([ 2.69164583, -5.61080858]),
     array([ 2.56667444, -5.4433092 ]),
     array([ 2.70213256, -5.34858144]),
     array([ 2.97340715, -5.21063384])]



3439483639dda2e5604af593146f1ca4881c2ad5
ipynb
Jupyter Notebook
Demo.ipynb
matt-graham/symnum
c7238f436c748a0fe2677cc194f176071dfc912c
MIT
2020-03-13T15:16:43.000Z
2021-09-25T16:26:26.000Z
Demo.ipynb
matt-graham/symnum
c7238f436c748a0fe2677cc194f176071dfc912c
MIT
Demo.ipynb
matt-graham/symnum
c7238f436c748a0fe2677cc194f176071dfc912c
MIT
Qwen/Qwen-72B
1. YES
2. YES