```python
import sympy as sym
from sympy import *

init_printing(use_latex='mathjax')
```

# Derivation of the polynomial coefficients for the lens equation

Lens equation:

$$
w = z - \sum_{j=1}^N\frac{\epsilon_j}{\bar{z}-\bar{r}_j}
$$

It follows that

$$
\bar{z}=\bar{w} + \sum_{j=1}^N\frac{\epsilon_j}{z - r_j}
$$

We define $z_j\equiv z -r_j$ and two polynomials $G$ and $H$:

$$
\sum_{k=0}^{N-1}G_kz^k = G = \sum_{j=1}^N\epsilon_j\prod_{i=1,\;i\neq j}^Nz_i
$$

$$
\sum_{k=0}^{N}H_kz^k = H= \prod_{i=1}^Nz_i
$$

it follows that

$$
\frac{G}{H}=\frac{\sum_{j=1}^N\epsilon_j\prod_{i\neq j} z_i}{\prod_{j=1}^Nz_j}=\sum_{j=1}^N\frac{\epsilon_j}{z_j}
$$

and

$$
\bar{z} = \bar{w} + G/H
$$

Defining $\varpi_j\equiv \bar{r}_j-\bar{w}$, we have

$$
z - w = \sum_{j=1}^N\frac{\epsilon_j}{G/H+\bar{w}-\bar{r}_j}
$$

multiplying the above expression with $\prod_{j=1}^N(G-\varpi_j H)$ results in

$$
0=(z-w) \prod_{j=1}^{N}\left(G-\varpi_{j} H\right)-H \sum_{j=1}^{N}\left[\epsilon_{j} \prod_{i=1,\,j \neq i}^N\left(G-\varpi_{i} H\right)\right]
$$

We can then plug in the above expression into Sympy to obtain the coefficients.

```python
def compute_polynomial(N):
    # Define symbols
    z, w = symbols(['z', 'w'], complex=True)
    r = IndexedBase('r', complex=True)
    e = IndexedBase('e', positive=True)
    i, j = symbols(['i', 'j'], integer=True)
    a = symbols('a', positive=True)

    # Polynomial H
    H = Product(z - r[i], (i, 1, N)).doit()
    H = H.subs(r[1], a).subs(r[2], -a)

    # Polynomial G
    G = 0
    for j in range(1, N + 1):
        G += e[j]*prod([z - r[i] for i in range(1, N + 1) if i!=j])
    G = G.subs(r[1], a).subs(r[2], -a)

    # First term
    i, j = symbols(['i', 'j'], integer=True)
    # tmp = G - (conjugate(r[j]) - conjugate(w))*H
    # term1 = Product(tmp.subs(r[1], a).subs(r[2], -a), (j, 1, N)).doit()*(z - w)
    term1 = prod(
        [(G - (conjugate(r[j]) - conjugate(w))*H).subs(r[1], a).subs(r[2], -a) for j in range(1, N + 1)]
    )
    term1 *= (z - w)

    # Second term
    s = 0
    for j in range(1, N + 1):
        s += e[j]*prod(
            [(G - (conjugate(r[i]) - conjugate(w))*H).subs(r[1], a).subs(r[2], -a) for i in range(1, N + 1) if i!=j]
        )
    term2 = s*H

    return Poly(term1 - term2, z).all_coeffs()

```

## N = 2

```python
N = 2
coeffs = compute_polynomial(N)
e = IndexedBase('e', positive=True)
coeffs = [expand(c.subs(e[2], 1-e[1])) for c in coeffs]
```

```python
N = 2
# Print coefficients starting with the highest power coefficient
for i in range(N**2 + 2):
    print(f"p_{i} =", str(coeffs[i]))
    print("\n\n")
```

    p_0 = -a**2 + conjugate(w)**2

    p_1 = a**2*w - 2*a*e[1] + a - w*conjugate(w)**2 + conjugate(w)

    p_2 = 2*a**4 - 2*a**2*conjugate(w)**2 + 4*a*conjugate(w)*e[1] - 2*a*conjugate(w) - 2*w*conjugate(w)

    p_3 = -2*a**4*w + 4*a**3*e[1] - 2*a**3 + 2*a**2*w*conjugate(w)**2 - 4*a*w*conjugate(w)*e[1] + 2*a*w*conjugate(w) + 2*a*e[1] - a - w

    p_4 = -a**6 + a**4*conjugate(w)**2 - 4*a**3*conjugate(w)*e[1] + 2*a**3*conjugate(w) + 2*a**2*w*conjugate(w) + 4*a**2*e[1]**2 - 4*a**2*e[1] + 2*a**2 - 4*a*w*e[1] + 2*a*w

    p_5 = a**6*w - 2*a**5*e[1] + a**5 - a**4*w*conjugate(w)**2 - a**4*conjugate(w) + 4*a**3*w*conjugate(w)*e[1] - 2*a**3*w*conjugate(w) + 2*a**3*e[1] - a**3 - 4*a**2*w*e[1]**2 + 4*a**2*w*e[1] - a**2*w

## N = 3

```python
N = 3
coeffs = compute_polynomial(N)
coeffs = [expand(c.subs(e[3], 1- e[1] - e[2])) for c in coeffs]
```

```python
# Print coefficients starting with the highest power coefficient
for i in range(N**2 + 2):
    print(f"p_{i}=\n\n", str(coeffs[i]))
    print("\n\n")
```

    p_0=

     -a**2*conjugate(w) + a**2*conjugate(r[3]) + conjugate(w)**3 - conjugate(w)**2*conjugate(r[3])

    p_1=

     a**2*w*conjugate(w) - a**2*w*conjugate(r[3]) + 3*a**2*conjugate(w)*r[3] - 3*a**2*conjugate(r[3])*r[3] - a**2*e[1] - a**2*e[2] - a*conjugate(w)*e[1] + a*conjugate(w)*e[2] + a*conjugate(r[3])*e[1] - a*conjugate(r[3])*e[2] - w*conjugate(w)**3 + w*conjugate(w)**2*conjugate(r[3]) - 3*conjugate(w)**3*r[3] + 3*conjugate(w)**2*conjugate(r[3])*r[3] + 2*conjugate(w)**2 + conjugate(w)*conjugate(r[3])*e[1] + conjugate(w)*conjugate(r[3])*e[2] - 2*conjugate(w)*conjugate(r[3])

    p_2=

     3*a**4*conjugate(w) - 3*a**4*conjugate(r[3]) - a**3*e[1] + a**3*e[2] - 3*a**2*w*conjugate(w)*r[3] + 3*a**2*w*conjugate(r[3])*r[3] + a**2*w - 3*a**2*conjugate(w)**3 + 3*a**2*conjugate(w)**2*conjugate(r[3]) - 3*a**2*conjugate(w)*r[3]**2 + 3*a**2*conjugate(r[3])*r[3]**2 + 4*a**2*e[1]*r[3] + 4*a**2*e[2]*r[3] - a**2*r[3] + 3*a*conjugate(w)**2*e[1] - 3*a*conjugate(w)**2*e[2] - 2*a*conjugate(w)*conjugate(r[3])*e[1] + 2*a*conjugate(w)*conjugate(r[3])*e[2] + 3*a*conjugate(w)*e[1]*r[3] - 3*a*conjugate(w)*e[2]*r[3] - 3*a*conjugate(r[3])*e[1]*r[3] + 3*a*conjugate(r[3])*e[2]*r[3] - a*e[1] + a*e[2] + 3*w*conjugate(w)**3*r[3] - 3*w*conjugate(w)**2*conjugate(r[3])*r[3] - 3*w*conjugate(w)**2 + 2*w*conjugate(w)*conjugate(r[3]) + 3*conjugate(w)**3*r[3]**2 - 3*conjugate(w)**2*conjugate(r[3])*r[3]**2 - 3*conjugate(w)**2*e[1]*r[3] - 3*conjugate(w)**2*e[2]*r[3] - 3*conjugate(w)**2*r[3] - conjugate(w)*conjugate(r[3])*e[1]*r[3] - conjugate(w)*conjugate(r[3])*e[2]*r[3] + 4*conjugate(w)*conjugate(r[3])*r[3] + conjugate(w) + conjugate(r[3])*e[1] + conjugate(r[3])*e[2] - conjugate(r[3])

    p_3=

     -3*a**4*w*conjugate(w) + 3*a**4*w*conjugate(r[3]) - 9*a**4*conjugate(w)*r[3] + 9*a**4*conjugate(r[3])*r[3] + 2*a**4*e[1] + 2*a**4*e[2] + a**3*w*e[1] - a**3*w*e[2] + 3*a**3*conjugate(w)*e[1] - 3*a**3*conjugate(w)*e[2] - 3*a**3*conjugate(r[3])*e[1] + 3*a**3*conjugate(r[3])*e[2] + 3*a**3*e[1]*r[3] - 3*a**3*e[2]*r[3] + 3*a**2*w*conjugate(w)**3 - 3*a**2*w*conjugate(w)**2*conjugate(r[3]) + 3*a**2*w*conjugate(w)*r[3]**2 - 3*a**2*w*conjugate(r[3])*r[3]**2 - a**2*w*e[1]*r[3] - a**2*w*e[2]*r[3] - 2*a**2*w*r[3] + 9*a**2*conjugate(w)**3*r[3] - 9*a**2*conjugate(w)**2*conjugate(r[3])*r[3] + 3*a**2*conjugate(w)**2*e[1] + 3*a**2*conjugate(w)**2*e[2] - 6*a**2*conjugate(w)**2 - 5*a**2*conjugate(w)*conjugate(r[3])*e[1] - 5*a**2*conjugate(w)*conjugate(r[3])*e[2] + 6*a**2*conjugate(w)*conjugate(r[3]) + a**2*conjugate(w)*r[3]**3 - a**2*conjugate(r[3])*r[3]**3 - a**2*e[1]**2 + 2*a**2*e[1]*e[2] - 5*a**2*e[1]*r[3]**2 - a**2*e[2]**2 - 5*a**2*e[2]*r[3]**2 + 2*a**2*r[3]**2 - 3*a*w*conjugate(w)**2*e[1] + 3*a*w*conjugate(w)**2*e[2] + 2*a*w*conjugate(w)*conjugate(r[3])*e[1] - 2*a*w*conjugate(w)*conjugate(r[3])*e[2] - 9*a*conjugate(w)**2*e[1]*r[3] + 9*a*conjugate(w)**2*e[2]*r[3] + 6*a*conjugate(w)*conjugate(r[3])*e[1]*r[3] - 6*a*conjugate(w)*conjugate(r[3])*e[2]*r[3] - 3*a*conjugate(w)*e[1]*r[3]**2 + 4*a*conjugate(w)*e[1] + 3*a*conjugate(w)*e[2]*r[3]**2 - 4*a*conjugate(w)*e[2] + a*conjugate(r[3])*e[1]**2 + 3*a*conjugate(r[3])*e[1]*r[3]**2 - 2*a*conjugate(r[3])*e[1] - a*conjugate(r[3])*e[2]**2 - 3*a*conjugate(r[3])*e[2]*r[3]**2 + 2*a*conjugate(r[3])*e[2] + a*e[1]**2*r[3] + 2*a*e[1]*r[3] - a*e[2]**2*r[3] - 2*a*e[2]*r[3] - 3*w*conjugate(w)**3*r[3]**2 + 3*w*conjugate(w)**2*conjugate(r[3])*r[3]**2 + 3*w*conjugate(w)**2*e[1]*r[3] + 3*w*conjugate(w)**2*e[2]*r[3] + 6*w*conjugate(w)**2*r[3] - 2*w*conjugate(w)*conjugate(r[3])*e[1]*r[3] - 2*w*conjugate(w)*conjugate(r[3])*e[2]*r[3] - 4*w*conjugate(w)*conjugate(r[3])*r[3] - 3*w*conjugate(w) + w*conjugate(r[3]) - conjugate(w)**3*r[3]**3 + conjugate(w)**2*conjugate(r[3])*r[3]**3 + 6*conjugate(w)**2*e[1]*r[3]**2 + 6*conjugate(w)**2*e[2]*r[3]**2 - conjugate(w)*conjugate(r[3])*e[1]*r[3]**2 - conjugate(w)*conjugate(r[3])*e[2]*r[3]**2 - 2*conjugate(w)*conjugate(r[3])*r[3]**2 - 4*conjugate(w)*e[1]*r[3] - 4*conjugate(w)*e[2]*r[3] + conjugate(w)*r[3] - conjugate(r[3])*e[1]**2*r[3] - 2*conjugate(r[3])*e[1]*e[2]*r[3] - conjugate(r[3])*e[2]**2*r[3] + conjugate(r[3])*r[3]

    p_4=

     -3*a**6*conjugate(w) + 3*a**6*conjugate(r[3]) + 2*a**5*e[1] - 2*a**5*e[2] + 9*a**4*w*conjugate(w)*r[3] - 9*a**4*w*conjugate(r[3])*r[3] + a**4*w*e[1] + a**4*w*e[2] - 3*a**4*w + 3*a**4*conjugate(w)**3 - 3*a**4*conjugate(w)**2*conjugate(r[3]) + 9*a**4*conjugate(w)*r[3]**2 - 9*a**4*conjugate(r[3])*r[3]**2 - 9*a**4*e[1]*r[3] - 9*a**4*e[2]*r[3] + 3*a**4*r[3] - 3*a**3*w*e[1]*r[3] + 3*a**3*w*e[2]*r[3] - 6*a**3*conjugate(w)**2*e[1] + 6*a**3*conjugate(w)**2*e[2] + 4*a**3*conjugate(w)*conjugate(r[3])*e[1] - 4*a**3*conjugate(w)*conjugate(r[3])*e[2] - 9*a**3*conjugate(w)*e[1]*r[3] + 9*a**3*conjugate(w)*e[2]*r[3] + 9*a**3*conjugate(r[3])*e[1]*r[3] - 9*a**3*conjugate(r[3])*e[2]*r[3] - a**3*e[1]**2 - 3*a**3*e[1]*r[3]**2 + 3*a**3*e[1] + a**3*e[2]**2 + 3*a**3*e[2]*r[3]**2 - 3*a**3*e[2] - 9*a**2*w*conjugate(w)**3*r[3] + 9*a**2*w*conjugate(w)**2*conjugate(r[3])*r[3] - 3*a**2*w*conjugate(w)**2*e[1] - 3*a**2*w*conjugate(w)**2*e[2] + 9*a**2*w*conjugate(w)**2 + 2*a**2*w*conjugate(w)*conjugate(r[3])*e[1] + 2*a**2*w*conjugate(w)*conjugate(r[3])*e[2] - 6*a**2*w*conjugate(w)*conjugate(r[3]) - a**2*w*conjugate(w)*r[3]**3 + a**2*w*conjugate(r[3])*r[3]**3 + 2*a**2*w*e[1]*r[3]**2 + 2*a**2*w*e[2]*r[3]**2 + a**2*w*r[3]**2 - 9*a**2*conjugate(w)**3*r[3]**2 + 9*a**2*conjugate(w)**2*conjugate(r[3])*r[3]**2 + 9*a**2*conjugate(w)**2*r[3] + 9*a**2*conjugate(w)*conjugate(r[3])*e[1]*r[3] + 9*a**2*conjugate(w)*conjugate(r[3])*e[2]*r[3] - 12*a**2*conjugate(w)*conjugate(r[3])*r[3] + 3*a**2*conjugate(w)*e[1]**2 - 6*a**2*conjugate(w)*e[1]*e[2] + 4*a**2*conjugate(w)*e[1] + 3*a**2*conjugate(w)*e[2]**2 + 4*a**2*conjugate(w)*e[2] - 3*a**2*conjugate(w) + 4*a**2*conjugate(r[3])*e[1]*e[2] - 5*a**2*conjugate(r[3])*e[1] - 5*a**2*conjugate(r[3])*e[2] + 3*a**2*conjugate(r[3]) + 3*a**2*e[1]**2*r[3] - 6*a**2*e[1]*e[2]*r[3] + 2*a**2*e[1]*r[3]**3 + 3*a**2*e[2]**2*r[3] + 2*a**2*e[2]*r[3]**3 - a**2*r[3]**3 + 9*a*w*conjugate(w)**2*e[1]*r[3] - 9*a*w*conjugate(w)**2*e[2]*r[3] - 6*a*w*conjugate(w)*conjugate(r[3])*e[1]*r[3] + 6*a*w*conjugate(w)*conjugate(r[3])*e[2]*r[3] - 6*a*w*conjugate(w)*e[1] + 6*a*w*conjugate(w)*e[2] + 2*a*w*conjugate(r[3])*e[1] - 2*a*w*conjugate(r[3])*e[2] + 9*a*conjugate(w)**2*e[1]*r[3]**2 - 9*a*conjugate(w)**2*e[2]*r[3]**2 - 6*a*conjugate(w)*conjugate(r[3])*e[1]*r[3]**2 + 6*a*conjugate(w)*conjugate(r[3])*e[2]*r[3]**2 - 6*a*conjugate(w)*e[1]**2*r[3] + a*conjugate(w)*e[1]*r[3]**3 - 6*a*conjugate(w)*e[1]*r[3] + 6*a*conjugate(w)*e[2]**2*r[3] - a*conjugate(w)*e[2]*r[3]**3 + 6*a*conjugate(w)*e[2]*r[3] - a*conjugate(r[3])*e[1]**2*r[3] - a*conjugate(r[3])*e[1]*r[3]**3 + 4*a*conjugate(r[3])*e[1]*r[3] + a*conjugate(r[3])*e[2]**2*r[3] + a*conjugate(r[3])*e[2]*r[3]**3 - 4*a*conjugate(r[3])*e[2]*r[3] - 2*a*e[1]**2*r[3]**2 - a*e[1]*r[3]**2 + a*e[1] + 2*a*e[2]**2*r[3]**2 + a*e[2]*r[3]**2 - a*e[2] + w*conjugate(w)**3*r[3]**3 - w*conjugate(w)**2*conjugate(r[3])*r[3]**3 - 6*w*conjugate(w)**2*e[1]*r[3]**2 - 6*w*conjugate(w)**2*e[2]*r[3]**2 - 3*w*conjugate(w)**2*r[3]**2 + 4*w*conjugate(w)*conjugate(r[3])*e[1]*r[3]**2 + 4*w*conjugate(w)*conjugate(r[3])*e[2]*r[3]**2 + 2*w*conjugate(w)*conjugate(r[3])*r[3]**2 + 6*w*conjugate(w)*e[1]*r[3] + 6*w*conjugate(w)*e[2]*r[3] + 3*w*conjugate(w)*r[3] - 2*w*conjugate(r[3])*e[1]*r[3] - 2*w*conjugate(r[3])*e[2]*r[3] - w*conjugate(r[3])*r[3] - w - 3*conjugate(w)**2*e[1]*r[3]**3 - 3*conjugate(w)**2*e[2]*r[3]**3 + conjugate(w)**2*r[3]**3 + conjugate(w)*conjugate(r[3])*e[1]*r[3]**3 + conjugate(w)*conjugate(r[3])*e[2]*r[3]**3 + 3*conjugate(w)*e[1]**2*r[3]**2 + 6*conjugate(w)*e[1]*e[2]*r[3]**2 + 2*conjugate(w)*e[1]*r[3]**2 + 3*conjugate(w)*e[2]**2*r[3]**2 + 2*conjugate(w)*e[2]*r[3]**2 - 2*conjugate(w)*r[3]**2 + conjugate(r[3])*e[1]**2*r[3]**2 + 2*conjugate(r[3])*e[1]*e[2]*r[3]**2 - conjugate(r[3])*e[1]*r[3]**2 + conjugate(r[3])*e[2]**2*r[3]**2 - conjugate(r[3])*e[2]*r[3]**2 - e[1]*r[3] - e[2]*r[3] + r[3]

    p_5=

     3*a**6*w*conjugate(w) - 3*a**6*w*conjugate(r[3]) + 9*a**6*conjugate(w)*r[3] - 9*a**6*conjugate(r[3])*r[3] - a**6*e[1] - a**6*e[2] - 2*a**5*w*e[1] + 2*a**5*w*e[2] - 3*a**5*conjugate(w)*e[1] + 3*a**5*conjugate(w)*e[2] + 3*a**5*conjugate(r[3])*e[1] - 3*a**5*conjugate(r[3])*e[2] - 6*a**5*e[1]*r[3] + 6*a**5*e[2]*r[3] - 3*a**4*w*conjugate(w)**3 + 3*a**4*w*conjugate(w)**2*conjugate(r[3]) - 9*a**4*w*conjugate(w)*r[3]**2 + 9*a**4*w*conjugate(r[3])*r[3]**2 + 6*a**4*w*r[3] - 9*a**4*conjugate(w)**3*r[3] + 9*a**4*conjugate(w)**2*conjugate(r[3])*r[3] - 6*a**4*conjugate(w)**2*e[1] - 6*a**4*conjugate(w)**2*e[2] + 6*a**4*conjugate(w)**2 + 7*a**4*conjugate(w)*conjugate(r[3])*e[1] + 7*a**4*conjugate(w)*conjugate(r[3])*e[2] - 6*a**4*conjugate(w)*conjugate(r[3]) - 3*a**4*conjugate(w)*r[3]**3 + 3*a**4*conjugate(r[3])*r[3]**3 + 2*a**4*e[1]**2 - 4*a**4*e[1]*e[2] + 12*a**4*e[1]*r[3]**2 + 2*a**4*e[2]**2 + 12*a**4*e[2]*r[3]**2 - 6*a**4*r[3]**2 + 6*a**3*w*conjugate(w)**2*e[1] - 6*a**3*w*conjugate(w)**2*e[2] - 4*a**3*w*conjugate(w)*conjugate(r[3])*e[1] + 4*a**3*w*conjugate(w)*conjugate(r[3])*e[2] + 3*a**3*w*e[1]*r[3]**2 - 3*a**3*w*e[2]*r[3]**2 + 18*a**3*conjugate(w)**2*e[1]*r[3] - 18*a**3*conjugate(w)**2*e[2]*r[3] - 12*a**3*conjugate(w)*conjugate(r[3])*e[1]*r[3] + 12*a**3*conjugate(w)*conjugate(r[3])*e[2]*r[3] + 6*a**3*conjugate(w)*e[1]**2 + 9*a**3*conjugate(w)*e[1]*r[3]**2 - 8*a**3*conjugate(w)*e[1] - 6*a**3*conjugate(w)*e[2]**2 - 9*a**3*conjugate(w)*e[2]*r[3]**2 + 8*a**3*conjugate(w)*e[2] - 4*a**3*conjugate(r[3])*e[1]**2 - 9*a**3*conjugate(r[3])*e[1]*r[3]**2 + 4*a**3*conjugate(r[3])*e[1] + 4*a**3*conjugate(r[3])*e[2]**2 + 9*a**3*conjugate(r[3])*e[2]*r[3]**2 - 4*a**3*conjugate(r[3])*e[2] + a**3*e[1]*r[3]**3 - 6*a**3*e[1]*r[3] - a**3*e[2]*r[3]**3 + 6*a**3*e[2]*r[3] + 9*a**2*w*conjugate(w)**3*r[3]**2 - 9*a**2*w*conjugate(w)**2*conjugate(r[3])*r[3]**2 - 18*a**2*w*conjugate(w)**2*r[3] + 12*a**2*w*conjugate(w)*conjugate(r[3])*r[3] - 3*a**2*w*conjugate(w)*e[1]**2 + 6*a**2*w*conjugate(w)*e[1]*e[2] - 6*a**2*w*conjugate(w)*e[1] - 3*a**2*w*conjugate(w)*e[2]**2 - 6*a**2*w*conjugate(w)*e[2] + 9*a**2*w*conjugate(w) + a**2*w*conjugate(r[3])*e[1]**2 - 2*a**2*w*conjugate(r[3])*e[1]*e[2] + 2*a**2*w*conjugate(r[3])*e[1] + a**2*w*conjugate(r[3])*e[2]**2 + 2*a**2*w*conjugate(r[3])*e[2] - 3*a**2*w*conjugate(r[3]) - a**2*w*e[1]*r[3]**3 - a**2*w*e[2]*r[3]**3 + 3*a**2*conjugate(w)**3*r[3]**3 - 3*a**2*conjugate(w)**2*conjugate(r[3])*r[3]**3 - 9*a**2*conjugate(w)**2*e[1]*r[3]**2 - 9*a**2*conjugate(w)**2*e[2]*r[3]**2 - 3*a**2*conjugate(w)*conjugate(r[3])*e[1]*r[3]**2 - 3*a**2*conjugate(w)*conjugate(r[3])*e[2]*r[3]**2 + 6*a**2*conjugate(w)*conjugate(r[3])*r[3]**2 - 15*a**2*conjugate(w)*e[1]**2*r[3] + 6*a**2*conjugate(w)*e[1]*e[2]*r[3] + 6*a**2*conjugate(w)*e[1]*r[3] - 15*a**2*conjugate(w)*e[2]**2*r[3] + 6*a**2*conjugate(w)*e[2]*r[3] - 3*a**2*conjugate(w)*r[3] + 5*a**2*conjugate(r[3])*e[1]**2*r[3] - 2*a**2*conjugate(r[3])*e[1]*e[2]*r[3] + 4*a**2*conjugate(r[3])*e[1]*r[3] + 5*a**2*conjugate(r[3])*e[2]**2*r[3] + 4*a**2*conjugate(r[3])*e[2]*r[3] - 3*a**2*conjugate(r[3])*r[3] - 3*a**2*e[1]**2*r[3]**2 + 2*a**2*e[1]**2 + 6*a**2*e[1]*e[2]*r[3]**2 - 4*a**2*e[1]*e[2] + a**2*e[1] - 3*a**2*e[2]**2*r[3]**2 + 2*a**2*e[2]**2 + a**2*e[2] - 9*a*w*conjugate(w)**2*e[1]*r[3]**2 + 9*a*w*conjugate(w)**2*e[2]*r[3]**2 + 6*a*w*conjugate(w)*conjugate(r[3])*e[1]*r[3]**2 - 6*a*w*conjugate(w)*conjugate(r[3])*e[2]*r[3]**2 + 6*a*w*conjugate(w)*e[1]**2*r[3] + 12*a*w*conjugate(w)*e[1]*r[3] - 6*a*w*conjugate(w)*e[2]**2*r[3] - 12*a*w*conjugate(w)*e[2]*r[3] - 2*a*w*conjugate(r[3])*e[1]**2*r[3] - 4*a*w*conjugate(r[3])*e[1]*r[3] + 2*a*w*conjugate(r[3])*e[2]**2*r[3] + 4*a*w*conjugate(r[3])*e[2]*r[3] - 3*a*w*e[1] + 3*a*w*e[2] - 3*a*conjugate(w)**2*e[1]*r[3]**3 + 3*a*conjugate(w)**2*e[2]*r[3]**3 + 2*a*conjugate(w)*conjugate(r[3])*e[1]*r[3]**3 - 2*a*conjugate(w)*conjugate(r[3])*e[2]*r[3]**3 + 12*a*conjugate(w)*e[1]**2*r[3]**2 - 12*a*conjugate(w)*e[2]**2*r[3]**2 - a*conjugate(r[3])*e[1]**2*r[3]**2 - 2*a*conjugate(r[3])*e[1]*r[3]**2 + a*conjugate(r[3])*e[2]**2*r[3]**2 + 2*a*conjugate(r[3])*e[2]*r[3]**2 + a*e[1]**2*r[3]**3 - 4*a*e[1]**2*r[3] + a*e[1]*r[3] - a*e[2]**2*r[3]**3 + 4*a*e[2]**2*r[3] - a*e[2]*r[3] + 3*w*conjugate(w)**2*e[1]*r[3]**3 + 3*w*conjugate(w)**2*e[2]*r[3]**3 - 2*w*conjugate(w)*conjugate(r[3])*e[1]*r[3]**3 - 2*w*conjugate(w)*conjugate(r[3])*e[2]*r[3]**3 - 3*w*conjugate(w)*e[1]**2*r[3]**2 - 6*w*conjugate(w)*e[1]*e[2]*r[3]**2 - 6*w*conjugate(w)*e[1]*r[3]**2 - 3*w*conjugate(w)*e[2]**2*r[3]**2 - 6*w*conjugate(w)*e[2]*r[3]**2 + w*conjugate(r[3])*e[1]**2*r[3]**2 + 2*w*conjugate(r[3])*e[1]*e[2]*r[3]**2 + 2*w*conjugate(r[3])*e[1]*r[3]**2 + w*conjugate(r[3])*e[2]**2*r[3]**2 + 2*w*conjugate(r[3])*e[2]*r[3]**2 + 3*w*e[1]*r[3] + 3*w*e[2]*r[3] - 3*conjugate(w)*e[1]**2*r[3]**3 - 6*conjugate(w)*e[1]*e[2]*r[3]**3 + 2*conjugate(w)*e[1]*r[3]**3 - 3*conjugate(w)*e[2]**2*r[3]**3 + 2*conjugate(w)*e[2]*r[3]**3 + 2*e[1]**2*r[3]**2 + 4*e[1]*e[2]*r[3]**2 - 2*e[1]*r[3]**2 + 2*e[2]**2*r[3]**2 - 2*e[2]*r[3]**2

    p_6=

     a**8*conjugate(w) - a**8*conjugate(r[3]) - a**7*e[1] + a**7*e[2] - 9*a**6*w*conjugate(w)*r[3] + 9*a**6*w*conjugate(r[3])*r[3] - 2*a**6*w*e[1] - 2*a**6*w*e[2] + 3*a**6*w - a**6*conjugate(w)**3 + a**6*conjugate(w)**2*conjugate(r[3]) - 9*a**6*conjugate(w)*r[3]**2 + 9*a**6*conjugate(r[3])*r[3]**2 + 6*a**6*e[1]*r[3] + 6*a**6*e[2]*r[3] - 3*a**6*r[3] + 6*a**5*w*e[1]*r[3] - 6*a**5*w*e[2]*r[3] + 3*a**5*conjugate(w)**2*e[1] - 3*a**5*conjugate(w)**2*e[2] - 2*a**5*conjugate(w)*conjugate(r[3])*e[1] + 2*a**5*conjugate(w)*conjugate(r[3])*e[2] + 9*a**5*conjugate(w)*e[1]*r[3] - 9*a**5*conjugate(w)*e[2]*r[3] - 9*a**5*conjugate(r[3])*e[1]*r[3] + 9*a**5*conjugate(r[3])*e[2]*r[3] + 2*a**5*e[1]**2 + 6*a**5*e[1]*r[3]**2 - 3*a**5*e[1] - 2*a**5*e[2]**2 - 6*a**5*e[2]*r[3]**2 + 3*a**5*e[2] + 9*a**4*w*conjugate(w)**3*r[3] - 9*a**4*w*conjugate(w)**2*conjugate(r[3])*r[3] + 6*a**4*w*conjugate(w)**2*e[1] + 6*a**4*w*conjugate(w)**2*e[2] - 9*a**4*w*conjugate(w)**2 - 4*a**4*w*conjugate(w)*conjugate(r[3])*e[1] - 4*a**4*w*conjugate(w)*conjugate(r[3])*e[2] + 6*a**4*w*conjugate(w)*conjugate(r[3]) + 3*a**4*w*conjugate(w)*r[3]**3 - 3*a**4*w*conjugate(r[3])*r[3]**3 - 3*a**4*w*e[1]*r[3]**2 - 3*a**4*w*e[2]*r[3]**2 - 3*a**4*w*r[3]**2 + 9*a**4*conjugate(w)**3*r[3]**2 - 9*a**4*conjugate(w)**2*conjugate(r[3])*r[3]**2 + 9*a**4*conjugate(w)**2*e[1]*r[3] + 9*a**4*conjugate(w)**2*e[2]*r[3] - 9*a**4*conjugate(w)**2*r[3] - 15*a**4*conjugate(w)*conjugate(r[3])*e[1]*r[3] - 15*a**4*conjugate(w)*conjugate(r[3])*e[2]*r[3] + 12*a**4*conjugate(w)*conjugate(r[3])*r[3] + 12*a**4*conjugate(w)*e[1]*e[2] - 8*a**4*conjugate(w)*e[1] - 8*a**4*conjugate(w)*e[2] + 3*a**4*conjugate(w) - 2*a**4*conjugate(r[3])*e[1]**2 - 8*a**4*conjugate(r[3])*e[1]*e[2] + 7*a**4*conjugate(r[3])*e[1] - 2*a**4*conjugate(r[3])*e[2]**2 + 7*a**4*conjugate(r[3])*e[2] - 3*a**4*conjugate(r[3]) - 6*a**4*e[1]**2*r[3] + 12*a**4*e[1]*e[2]*r[3] - 5*a**4*e[1]*r[3]**3 - 6*a**4*e[2]**2*r[3] - 5*a**4*e[2]*r[3]**3 + 3*a**4*r[3]**3 - 18*a**3*w*conjugate(w)**2*e[1]*r[3] + 18*a**3*w*conjugate(w)**2*e[2]*r[3] + 12*a**3*w*conjugate(w)*conjugate(r[3])*e[1]*r[3] - 12*a**3*w*conjugate(w)*conjugate(r[3])*e[2]*r[3] - 6*a**3*w*conjugate(w)*e[1]**2 + 12*a**3*w*conjugate(w)*e[1] + 6*a**3*w*conjugate(w)*e[2]**2 - 12*a**3*w*conjugate(w)*e[2] + 2*a**3*w*conjugate(r[3])*e[1]**2 - 4*a**3*w*conjugate(r[3])*e[1] - 2*a**3*w*conjugate(r[3])*e[2]**2 + 4*a**3*w*conjugate(r[3])*e[2] - a**3*w*e[1]*r[3]**3 + a**3*w*e[2]*r[3]**3 - 18*a**3*conjugate(w)**2*e[1]*r[3]**2 + 18*a**3*conjugate(w)**2*e[2]*r[3]**2 + 12*a**3*conjugate(w)*conjugate(r[3])*e[1]*r[3]**2 - 12*a**3*conjugate(w)*conjugate(r[3])*e[2]*r[3]**2 - 6*a**3*conjugate(w)*e[1]**2*r[3] - 3*a**3*conjugate(w)*e[1]*r[3]**3 + 12*a**3*conjugate(w)*e[1]*r[3] + 6*a**3*conjugate(w)*e[2]**2*r[3] + 3*a**3*conjugate(w)*e[2]*r[3]**3 - 12*a**3*conjugate(w)*e[2]*r[3] + 8*a**3*conjugate(r[3])*e[1]**2*r[3] + 3*a**3*conjugate(r[3])*e[1]*r[3]**3 - 8*a**3*conjugate(r[3])*e[1]*r[3] - 8*a**3*conjugate(r[3])*e[2]**2*r[3] - 3*a**3*conjugate(r[3])*e[2]*r[3]**3 + 8*a**3*conjugate(r[3])*e[2]*r[3] + a**3*e[1]**3 - 3*a**3*e[1]**2*e[2] + 3*a**3*e[1]**2*r[3]**2 + 4*a**3*e[1]**2 + 3*a**3*e[1]*e[2]**2 + 3*a**3*e[1]*r[3]**2 - 2*a**3*e[1] - a**3*e[2]**3 - 3*a**3*e[2]**2*r[3]**2 - 4*a**3*e[2]**2 - 3*a**3*e[2]*r[3]**2 + 2*a**3*e[2] - 3*a**2*w*conjugate(w)**3*r[3]**3 + 3*a**2*w*conjugate(w)**2*conjugate(r[3])*r[3]**3 + 9*a**2*w*conjugate(w)**2*e[1]*r[3]**2 + 9*a**2*w*conjugate(w)**2*e[2]*r[3]**2 + 9*a**2*w*conjugate(w)**2*r[3]**2 - 6*a**2*w*conjugate(w)*conjugate(r[3])*e[1]*r[3]**2 - 6*a**2*w*conjugate(w)*conjugate(r[3])*e[2]*r[3]**2 - 6*a**2*w*conjugate(w)*conjugate(r[3])*r[3]**2 + 15*a**2*w*conjugate(w)*e[1]**2*r[3] - 6*a**2*w*conjugate(w)*e[1]*e[2]*r[3] - 6*a**2*w*conjugate(w)*e[1]*r[3] + 15*a**2*w*conjugate(w)*e[2]**2*r[3] - 6*a**2*w*conjugate(w)*e[2]*r[3] - 9*a**2*w*conjugate(w)*r[3] - 5*a**2*w*conjugate(r[3])*e[1]**2*r[3] + 2*a**2*w*conjugate(r[3])*e[1]*e[2]*r[3] + 2*a**2*w*conjugate(r[3])*e[1]*r[3] - 5*a**2*w*conjugate(r[3])*e[2]**2*r[3] + 2*a**2*w*conjugate(r[3])*e[2]*r[3] + 3*a**2*w*conjugate(r[3])*r[3] - 3*a**2*w*e[1]**2 + 6*a**2*w*e[1]*e[2] - 3*a**2*w*e[1] - 3*a**2*w*e[2]**2 - 3*a**2*w*e[2] + 3*a**2*w + 6*a**2*conjugate(w)**2*e[1]*r[3]**3 + 6*a**2*conjugate(w)**2*e[2]*r[3]**3 - 3*a**2*conjugate(w)**2*r[3]**3 - a**2*conjugate(w)*conjugate(r[3])*e[1]*r[3]**3 - a**2*conjugate(w)*conjugate(r[3])*e[2]*r[3]**3 + 12*a**2*conjugate(w)*e[1]**2*r[3]**2 - 12*a**2*conjugate(w)*e[1]*e[2]*r[3]**2 - 6*a**2*conjugate(w)*e[1]*r[3]**2 + 12*a**2*conjugate(w)*e[2]**2*r[3]**2 - 6*a**2*conjugate(w)*e[2]*r[3]**2 + 6*a**2*conjugate(w)*r[3]**2 - 7*a**2*conjugate(r[3])*e[1]**2*r[3]**2 - 2*a**2*conjugate(r[3])*e[1]*e[2]*r[3]**2 + a**2*conjugate(r[3])*e[1]*r[3]**2 - 7*a**2*conjugate(r[3])*e[2]**2*r[3]**2 + a**2*conjugate(r[3])*e[2]*r[3]**2 - 3*a**2*e[1]**3*r[3] + 3*a**2*e[1]**2*e[2]*r[3] + a**2*e[1]**2*r[3]**3 - 7*a**2*e[1]**2*r[3] + 3*a**2*e[1]*e[2]**2*r[3] - 2*a**2*e[1]*e[2]*r[3]**3 - 2*a**2*e[1]*e[2]*r[3] + 4*a**2*e[1]*r[3] - 3*a**2*e[2]**3*r[3] + a**2*e[2]**2*r[3]**3 - 7*a**2*e[2]**2*r[3] + 4*a**2*e[2]*r[3] - 3*a**2*r[3] + 3*a*w*conjugate(w)**2*e[1]*r[3]**3 - 3*a*w*conjugate(w)**2*e[2]*r[3]**3 - 2*a*w*conjugate(w)*conjugate(r[3])*e[1]*r[3]**3 + 2*a*w*conjugate(w)*conjugate(r[3])*e[2]*r[3]**3 - 12*a*w*conjugate(w)*e[1]**2*r[3]**2 - 6*a*w*conjugate(w)*e[1]*r[3]**2 + 12*a*w*conjugate(w)*e[2]**2*r[3]**2 + 6*a*w*conjugate(w)*e[2]*r[3]**2 + 4*a*w*conjugate(r[3])*e[1]**2*r[3]**2 + 2*a*w*conjugate(r[3])*e[1]*r[3]**2 - 4*a*w*conjugate(r[3])*e[2]**2*r[3]**2 - 2*a*w*conjugate(r[3])*e[2]*r[3]**2 + 6*a*w*e[1]**2*r[3] + 3*a*w*e[1]*r[3] - 6*a*w*e[2]**2*r[3] - 3*a*w*e[2]*r[3] - 6*a*conjugate(w)*e[1]**2*r[3]**3 + 2*a*conjugate(w)*e[1]*r[3]**3 + 6*a*conjugate(w)*e[2]**2*r[3]**3 - 2*a*conjugate(w)*e[2]*r[3]**3 + a*conjugate(r[3])*e[1]**2*r[3]**3 - a*conjugate(r[3])*e[2]**2*r[3]**3 + 3*a*e[1]**3*r[3]**2 + 3*a*e[1]**2*e[2]*r[3]**2 + 2*a*e[1]**2*r[3]**2 - 3*a*e[1]*e[2]**2*r[3]**2 - 2*a*e[1]*r[3]**2 - 3*a*e[2]**3*r[3]**2 - 2*a*e[2]**2*r[3]**2 + 2*a*e[2]*r[3]**2 + 3*w*conjugate(w)*e[1]**2*r[3]**3 + 6*w*conjugate(w)*e[1]*e[2]*r[3]**3 + 3*w*conjugate(w)*e[2]**2*r[3]**3 - w*conjugate(r[3])*e[1]**2*r[3]**3 - 2*w*conjugate(r[3])*e[1]*e[2]*r[3]**3 - w*conjugate(r[3])*e[2]**2*r[3]**3 - 3*w*e[1]**2*r[3]**2 - 6*w*e[1]*e[2]*r[3]**2 - 3*w*e[2]**2*r[3]**2 - e[1]**3*r[3]**3 - 3*e[1]**2*e[2]*r[3]**3 + e[1]**2*r[3]**3 - 3*e[1]*e[2]**2*r[3]**3 + 2*e[1]*e[2]*r[3]**3 - e[2]**3*r[3]**3 + e[2]**2*r[3]**3

    p_7=

     -a**8*w*conjugate(w) + a**8*w*conjugate(r[3]) - 3*a**8*conjugate(w)*r[3] + 3*a**8*conjugate(r[3])*r[3] + a**7*w*e[1] - a**7*w*e[2] + a**7*conjugate(w)*e[1] - a**7*conjugate(w)*e[2] - a**7*conjugate(r[3])*e[1] + a**7*conjugate(r[3])*e[2] + 3*a**7*e[1]*r[3] - 3*a**7*e[2]*r[3] + a**6*w*conjugate(w)**3 - a**6*w*conjugate(w)**2*conjugate(r[3]) + 9*a**6*w*conjugate(w)*r[3]**2 - 9*a**6*w*conjugate(r[3])*r[3]**2 + 3*a**6*w*e[1]*r[3] + 3*a**6*w*e[2]*r[3] - 6*a**6*w*r[3] + 3*a**6*conjugate(w)**3*r[3] - 3*a**6*conjugate(w)**2*conjugate(r[3])*r[3] + 3*a**6*conjugate(w)**2*e[1] + 3*a**6*conjugate(w)**2*e[2] - 2*a**6*conjugate(w)**2 - 3*a**6*conjugate(w)*conjugate(r[3])*e[1] - 3*a**6*conjugate(w)*conjugate(r[3])*e[2] + 2*a**6*conjugate(w)*conjugate(r[3]) + 3*a**6*conjugate(w)*r[3]**3 - 3*a**6*conjugate(r[3])*r[3]**3 - a**6*e[1]**2 + 2*a**6*e[1]*e[2] - 9*a**6*e[1]*r[3]**2 - a**6*e[2]**2 - 9*a**6*e[2]*r[3]**2 + 6*a**6*r[3]**2 - 3*a**5*w*conjugate(w)**2*e[1] + 3*a**5*w*conjugate(w)**2*e[2] + 2*a**5*w*conjugate(w)*conjugate(r[3])*e[1] - 2*a**5*w*conjugate(w)*conjugate(r[3])*e[2] - 6*a**5*w*e[1]*r[3]**2 + 6*a**5*w*e[2]*r[3]**2 - 9*a**5*conjugate(w)**2*e[1]*r[3] + 9*a**5*conjugate(w)**2*e[2]*r[3] + 6*a**5*conjugate(w)*conjugate(r[3])*e[1]*r[3] - 6*a**5*conjugate(w)*conjugate(r[3])*e[2]*r[3] - 6*a**5*conjugate(w)*e[1]**2 - 9*a**5*conjugate(w)*e[1]*r[3]**2 + 4*a**5*conjugate(w)*e[1] + 6*a**5*conjugate(w)*e[2]**2 + 9*a**5*conjugate(w)*e[2]*r[3]**2 - 4*a**5*conjugate(w)*e[2] + 3*a**5*conjugate(r[3])*e[1]**2 + 9*a**5*conjugate(r[3])*e[1]*r[3]**2 - 2*a**5*conjugate(r[3])*e[1] - 3*a**5*conjugate(r[3])*e[2]**2 - 9*a**5*conjugate(r[3])*e[2]*r[3]**2 + 2*a**5*conjugate(r[3])*e[2] - 3*a**5*e[1]**2*r[3] - 2*a**5*e[1]*r[3]**3 + 6*a**5*e[1]*r[3] + 3*a**5*e[2]**2*r[3] + 2*a**5*e[2]*r[3]**3 - 6*a**5*e[2]*r[3] - 9*a**4*w*conjugate(w)**3*r[3]**2 + 9*a**4*w*conjugate(w)**2*conjugate(r[3])*r[3]**2 - 9*a**4*w*conjugate(w)**2*e[1]*r[3] - 9*a**4*w*conjugate(w)**2*e[2]*r[3] + 18*a**4*w*conjugate(w)**2*r[3] + 6*a**4*w*conjugate(w)*conjugate(r[3])*e[1]*r[3] + 6*a**4*w*conjugate(w)*conjugate(r[3])*e[2]*r[3] - 12*a**4*w*conjugate(w)*conjugate(r[3])*r[3] - 12*a**4*w*conjugate(w)*e[1]*e[2] + 12*a**4*w*conjugate(w)*e[1] + 12*a**4*w*conjugate(w)*e[2] - 9*a**4*w*conjugate(w) + 4*a**4*w*conjugate(r[3])*e[1]*e[2] - 4*a**4*w*conjugate(r[3])*e[1] - 4*a**4*w*conjugate(r[3])*e[2] + 3*a**4*w*conjugate(r[3]) + 2*a**4*w*e[1]*r[3]**3 + 2*a**4*w*e[2]*r[3]**3 - 3*a**4*conjugate(w)**3*r[3]**3 + 3*a**4*conjugate(w)**2*conjugate(r[3])*r[3]**3 + 9*a**4*conjugate(w)*conjugate(r[3])*e[1]*r[3]**2 + 9*a**4*conjugate(w)*conjugate(r[3])*e[2]*r[3]**2 - 6*a**4*conjugate(w)*conjugate(r[3])*r[3]**2 + 12*a**4*conjugate(w)*e[1]**2*r[3] - 12*a**4*conjugate(w)*e[1]*e[2]*r[3] + 12*a**4*conjugate(w)*e[2]**2*r[3] + 3*a**4*conjugate(w)*r[3] - a**4*conjugate(r[3])*e[1]**2*r[3] + 10*a**4*conjugate(r[3])*e[1]*e[2]*r[3] - 8*a**4*conjugate(r[3])*e[1]*r[3] - a**4*conjugate(r[3])*e[2]**2*r[3] - 8*a**4*conjugate(r[3])*e[2]*r[3] + 3*a**4*conjugate(r[3])*r[3] + 3*a**4*e[1]**3 - 3*a**4*e[1]**2*e[2] + 6*a**4*e[1]**2*r[3]**2 - 3*a**4*e[1]*e[2]**2 - 12*a**4*e[1]*e[2]*r[3]**2 + 8*a**4*e[1]*e[2] - 2*a**4*e[1] + 3*a**4*e[2]**3 + 6*a**4*e[2]**2*r[3]**2 - 2*a**4*e[2] + 18*a**3*w*conjugate(w)**2*e[1]*r[3]**2 - 18*a**3*w*conjugate(w)**2*e[2]*r[3]**2 - 12*a**3*w*conjugate(w)*conjugate(r[3])*e[1]*r[3]**2 + 12*a**3*w*conjugate(w)*conjugate(r[3])*e[2]*r[3]**2 + 6*a**3*w*conjugate(w)*e[1]**2*r[3] - 24*a**3*w*conjugate(w)*e[1]*r[3] - 6*a**3*w*conjugate(w)*e[2]**2*r[3] + 24*a**3*w*conjugate(w)*e[2]*r[3] - 2*a**3*w*conjugate(r[3])*e[1]**2*r[3] + 8*a**3*w*conjugate(r[3])*e[1]*r[3] + 2*a**3*w*conjugate(r[3])*e[2]**2*r[3] - 8*a**3*w*conjugate(r[3])*e[2]*r[3] - a**3*w*e[1]**3 + 3*a**3*w*e[1]**2*e[2] - 6*a**3*w*e[1]**2 - 3*a**3*w*e[1]*e[2]**2 + 6*a**3*w*e[1] + a**3*w*e[2]**3 + 6*a**3*w*e[2]**2 - 6*a**3*w*e[2] + 6*a**3*conjugate(w)**2*e[1]*r[3]**3 - 6*a**3*conjugate(w)**2*e[2]*r[3]**3 - 4*a**3*conjugate(w)*conjugate(r[3])*e[1]*r[3]**3 + 4*a**3*conjugate(w)*conjugate(r[3])*e[2]*r[3]**3 - 6*a**3*conjugate(w)*e[1]**2*r[3]**2 + 6*a**3*conjugate(w)*e[2]**2*r[3]**2 - 4*a**3*conjugate(r[3])*e[1]**2*r[3]**2 + 4*a**3*conjugate(r[3])*e[1]*r[3]**2 + 4*a**3*conjugate(r[3])*e[2]**2*r[3]**2 - 4*a**3*conjugate(r[3])*e[2]*r[3]**2 - 9*a**3*e[1]**3*r[3] + 3*a**3*e[1]**2*e[2]*r[3] - 2*a**3*e[1]**2*r[3]**3 + 2*a**3*e[1]**2*r[3] - 3*a**3*e[1]*e[2]**2*r[3] - 2*a**3*e[1]*r[3] + 9*a**3*e[2]**3*r[3] + 2*a**3*e[2]**2*r[3]**3 - 2*a**3*e[2]**2*r[3] + 2*a**3*e[2]*r[3] - 6*a**2*w*conjugate(w)**2*e[1]*r[3]**3 - 6*a**2*w*conjugate(w)**2*e[2]*r[3]**3 + 4*a**2*w*conjugate(w)*conjugate(r[3])*e[1]*r[3]**3 + 4*a**2*w*conjugate(w)*conjugate(r[3])*e[2]*r[3]**3 - 12*a**2*w*conjugate(w)*e[1]**2*r[3]**2 + 12*a**2*w*conjugate(w)*e[1]*e[2]*r[3]**2 + 12*a**2*w*conjugate(w)*e[1]*r[3]**2 - 12*a**2*w*conjugate(w)*e[2]**2*r[3]**2 + 12*a**2*w*conjugate(w)*e[2]*r[3]**2 + 4*a**2*w*conjugate(r[3])*e[1]**2*r[3]**2 - 4*a**2*w*conjugate(r[3])*e[1]*e[2]*r[3]**2 - 4*a**2*w*conjugate(r[3])*e[1]*r[3]**2 + 4*a**2*w*conjugate(r[3])*e[2]**2*r[3]**2 - 4*a**2*w*conjugate(r[3])*e[2]*r[3]**2 + 3*a**2*w*e[1]**3*r[3] - 3*a**2*w*e[1]**2*e[2]*r[3] + 12*a**2*w*e[1]**2*r[3] - 3*a**2*w*e[1]*e[2]**2*r[3] - 6*a**2*w*e[1]*r[3] + 3*a**2*w*e[2]**3*r[3] + 12*a**2*w*e[2]**2*r[3] - 6*a**2*w*e[2]*r[3] + 12*a**2*conjugate(w)*e[1]*e[2]*r[3]**3 - 4*a**2*conjugate(w)*e[1]*r[3]**3 - 4*a**2*conjugate(w)*e[2]*r[3]**3 + 2*a**2*conjugate(r[3])*e[1]**2*r[3]**3 + 2*a**2*conjugate(r[3])*e[2]**2*r[3]**3 + 9*a**2*e[1]**3*r[3]**2 + 3*a**2*e[1]**2*e[2]*r[3]**2 - 4*a**2*e[1]**2*r[3]**2 + 3*a**2*e[1]*e[2]**2*r[3]**2 - 8*a**2*e[1]*e[2]*r[3]**2 + 4*a**2*e[1]*r[3]**2 + 9*a**2*e[2]**3*r[3]**2 - 4*a**2*e[2]**2*r[3]**2 + 4*a**2*e[2]*r[3]**2 + 6*a*w*conjugate(w)*e[1]**2*r[3]**3 - 6*a*w*conjugate(w)*e[2]**2*r[3]**3 - 2*a*w*conjugate(r[3])*e[1]**2*r[3]**3 + 2*a*w*conjugate(r[3])*e[2]**2*r[3]**3 - 3*a*w*e[1]**3*r[3]**2 - 3*a*w*e[1]**2*e[2]*r[3]**2 - 6*a*w*e[1]**2*r[3]**2 + 3*a*w*e[1]*e[2]**2*r[3]**2 + 3*a*w*e[2]**3*r[3]**2 + 6*a*w*e[2]**2*r[3]**2 - 3*a*e[1]**3*r[3]**3 - 3*a*e[1]**2*e[2]*r[3]**3 + 2*a*e[1]**2*r[3]**3 + 3*a*e[1]*e[2]**2*r[3]**3 + 3*a*e[2]**3*r[3]**3 - 2*a*e[2]**2*r[3]**3 + w*e[1]**3*r[3]**3 + 3*w*e[1]**2*e[2]*r[3]**3 + 3*w*e[1]*e[2]**2*r[3]**3 + w*e[2]**3*r[3]**3

    p_8=

     3*a**8*w*conjugate(w)*r[3] - 3*a**8*w*conjugate(r[3])*r[3] + a**8*w*e[1] + a**8*w*e[2] - a**8*w + 3*a**8*conjugate(w)*r[3]**2 - 3*a**8*conjugate(r[3])*r[3]**2 - a**8*e[1]*r[3] - a**8*e[2]*r[3] + a**8*r[3] - 3*a**7*w*e[1]*r[3] + 3*a**7*w*e[2]*r[3] - 3*a**7*conjugate(w)*e[1]*r[3] + 3*a**7*conjugate(w)*e[2]*r[3] + 3*a**7*conjugate(r[3])*e[1]*r[3] - 3*a**7*conjugate(r[3])*e[2]*r[3] - a**7*e[1]**2 - 3*a**7*e[1]*r[3]**2 + a**7*e[1] + a**7*e[2]**2 + 3*a**7*e[2]*r[3]**2 - a**7*e[2] - 3*a**6*w*conjugate(w)**3*r[3] + 3*a**6*w*conjugate(w)**2*conjugate(r[3])*r[3] - 3*a**6*w*conjugate(w)**2*e[1] - 3*a**6*w*conjugate(w)**2*e[2] + 3*a**6*w*conjugate(w)**2 + 2*a**6*w*conjugate(w)*conjugate(r[3])*e[1] + 2*a**6*w*conjugate(w)*conjugate(r[3])*e[2] - 2*a**6*w*conjugate(w)*conjugate(r[3]) - 3*a**6*w*conjugate(w)*r[3]**3 + 3*a**6*w*conjugate(r[3])*r[3]**3 + 3*a**6*w*r[3]**2 - 3*a**6*conjugate(w)**3*r[3]**2 + 3*a**6*conjugate(w)**2*conjugate(r[3])*r[3]**2 - 6*a**6*conjugate(w)**2*e[1]*r[3] - 6*a**6*conjugate(w)**2*e[2]*r[3] + 3*a**6*conjugate(w)**2*r[3] + 7*a**6*conjugate(w)*conjugate(r[3])*e[1]*r[3] + 7*a**6*conjugate(w)*conjugate(r[3])*e[2]*r[3] - 4*a**6*conjugate(w)*conjugate(r[3])*r[3] - 3*a**6*conjugate(w)*e[1]**2 - 6*a**6*conjugate(w)*e[1]*e[2] + 4*a**6*conjugate(w)*e[1] - 3*a**6*conjugate(w)*e[2]**2 + 4*a**6*conjugate(w)*e[2] - a**6*conjugate(w) + 2*a**6*conjugate(r[3])*e[1]**2 + 4*a**6*conjugate(r[3])*e[1]*e[2] - 3*a**6*conjugate(r[3])*e[1] + 2*a**6*conjugate(r[3])*e[2]**2 - 3*a**6*conjugate(r[3])*e[2] + a**6*conjugate(r[3]) + 3*a**6*e[1]**2*r[3] - 6*a**6*e[1]*e[2]*r[3] + 4*a**6*e[1]*r[3]**3 + 3*a**6*e[2]**2*r[3] + 4*a**6*e[2]*r[3]**3 - 3*a**6*r[3]**3 + 9*a**5*w*conjugate(w)**2*e[1]*r[3] - 9*a**5*w*conjugate(w)**2*e[2]*r[3] - 6*a**5*w*conjugate(w)*conjugate(r[3])*e[1]*r[3] + 6*a**5*w*conjugate(w)*conjugate(r[3])*e[2]*r[3] + 6*a**5*w*conjugate(w)*e[1]**2 - 6*a**5*w*conjugate(w)*e[1] - 6*a**5*w*conjugate(w)*e[2]**2 + 6*a**5*w*conjugate(w)*e[2] - 2*a**5*w*conjugate(r[3])*e[1]**2 + 2*a**5*w*conjugate(r[3])*e[1] + 2*a**5*w*conjugate(r[3])*e[2]**2 - 2*a**5*w*conjugate(r[3])*e[2] + 2*a**5*w*e[1]*r[3]**3 - 2*a**5*w*e[2]*r[3]**3 + 9*a**5*conjugate(w)**2*e[1]*r[3]**2 - 9*a**5*conjugate(w)**2*e[2]*r[3]**2 - 6*a**5*conjugate(w)*conjugate(r[3])*e[1]*r[3]**2 + 6*a**5*conjugate(w)*conjugate(r[3])*e[2]*r[3]**2 + 12*a**5*conjugate(w)*e[1]**2*r[3] + 3*a**5*conjugate(w)*e[1]*r[3]**3 - 6*a**5*conjugate(w)*e[1]*r[3] - 12*a**5*conjugate(w)*e[2]**2*r[3] - 3*a**5*conjugate(w)*e[2]*r[3]**3 + 6*a**5*conjugate(w)*e[2]*r[3] - 7*a**5*conjugate(r[3])*e[1]**2*r[3] - 3*a**5*conjugate(r[3])*e[1]*r[3]**3 + 4*a**5*conjugate(r[3])*e[1]*r[3] + 7*a**5*conjugate(r[3])*e[2]**2*r[3] + 3*a**5*conjugate(r[3])*e[2]*r[3]**3 - 4*a**5*conjugate(r[3])*e[2]*r[3] + 3*a**5*e[1]**3 + 3*a**5*e[1]**2*e[2] - 4*a**5*e[1]**2 - 3*a**5*e[1]*e[2]**2 - 3*a**5*e[1]*r[3]**2 + a**5*e[1] - 3*a**5*e[2]**3 + 4*a**5*e[2]**2 + 3*a**5*e[2]*r[3]**2 - a**5*e[2] + 3*a**4*w*conjugate(w)**3*r[3]**3 - 3*a**4*w*conjugate(w)**2*conjugate(r[3])*r[3]**3 - 9*a**4*w*conjugate(w)**2*r[3]**2 + 6*a**4*w*conjugate(w)*conjugate(r[3])*r[3]**2 - 12*a**4*w*conjugate(w)*e[1]**2*r[3] + 12*a**4*w*conjugate(w)*e[1]*e[2]*r[3] - 6*a**4*w*conjugate(w)*e[1]*r[3] - 12*a**4*w*conjugate(w)*e[2]**2*r[3] - 6*a**4*w*conjugate(w)*e[2]*r[3] + 9*a**4*w*conjugate(w)*r[3] + 4*a**4*w*conjugate(r[3])*e[1]**2*r[3] - 4*a**4*w*conjugate(r[3])*e[1]*e[2]*r[3] + 2*a**4*w*conjugate(r[3])*e[1]*r[3] + 4*a**4*w*conjugate(r[3])*e[2]**2*r[3] + 2*a**4*w*conjugate(r[3])*e[2]*r[3] - 3*a**4*w*conjugate(r[3])*r[3] - 3*a**4*w*e[1]**3 + 3*a**4*w*e[1]**2*e[2] + 3*a**4*w*e[1]*e[2]**2 - 12*a**4*w*e[1]*e[2] + 6*a**4*w*e[1] - 3*a**4*w*e[2]**3 + 6*a**4*w*e[2] - 3*a**4*w - 3*a**4*conjugate(w)**2*e[1]*r[3]**3 - 3*a**4*conjugate(w)**2*e[2]*r[3]**3 + 3*a**4*conjugate(w)**2*r[3]**3 - a**4*conjugate(w)*conjugate(r[3])*e[1]*r[3]**3 - a**4*conjugate(w)*conjugate(r[3])*e[2]*r[3]**3 - 15*a**4*conjugate(w)*e[1]**2*r[3]**2 + 6*a**4*conjugate(w)*e[1]*e[2]*r[3]**2 + 6*a**4*conjugate(w)*e[1]*r[3]**2 - 15*a**4*conjugate(w)*e[2]**2*r[3]**2 + 6*a**4*conjugate(w)*e[2]*r[3]**2 - 6*a**4*conjugate(w)*r[3]**2 + 5*a**4*conjugate(r[3])*e[1]**2*r[3]**2 - 2*a**4*conjugate(r[3])*e[1]*e[2]*r[3]**2 + a**4*conjugate(r[3])*e[1]*r[3]**2 + 5*a**4*conjugate(r[3])*e[2]**2*r[3]**2 + a**4*conjugate(r[3])*e[2]*r[3]**2 - 9*a**4*e[1]**3*r[3] - 3*a**4*e[1]**2*e[2]*r[3] - 2*a**4*e[1]**2*r[3]**3 + 8*a**4*e[1]**2*r[3] - 3*a**4*e[1]*e[2]**2*r[3] + 4*a**4*e[1]*e[2]*r[3]**3 + 4*a**4*e[1]*e[2]*r[3] - 5*a**4*e[1]*r[3] - 9*a**4*e[2]**3*r[3] - 2*a**4*e[2]**2*r[3]**3 + 8*a**4*e[2]**2*r[3] - 5*a**4*e[2]*r[3] + 3*a**4*r[3] - 6*a**3*w*conjugate(w)**2*e[1]*r[3]**3 + 6*a**3*w*conjugate(w)**2*e[2]*r[3]**3 + 4*a**3*w*conjugate(w)*conjugate(r[3])*e[1]*r[3]**3 - 4*a**3*w*conjugate(w)*conjugate(r[3])*e[2]*r[3]**3 + 6*a**3*w*conjugate(w)*e[1]**2*r[3]**2 + 12*a**3*w*conjugate(w)*e[1]*r[3]**2 - 6*a**3*w*conjugate(w)*e[2]**2*r[3]**2 - 12*a**3*w*conjugate(w)*e[2]*r[3]**2 - 2*a**3*w*conjugate(r[3])*e[1]**2*r[3]**2 - 4*a**3*w*conjugate(r[3])*e[1]*r[3]**2 + 2*a**3*w*conjugate(r[3])*e[2]**2*r[3]**2 + 4*a**3*w*conjugate(r[3])*e[2]*r[3]**2 + 9*a**3*w*e[1]**3*r[3] - 3*a**3*w*e[1]**2*e[2]*r[3] + 3*a**3*w*e[1]*e[2]**2*r[3] - 6*a**3*w*e[1]*r[3] - 9*a**3*w*e[2]**3*r[3] + 6*a**3*w*e[2]*r[3] + 6*a**3*conjugate(w)*e[1]**2*r[3]**3 - 4*a**3*conjugate(w)*e[1]*r[3]**3 - 6*a**3*conjugate(w)*e[2]**2*r[3]**3 + 4*a**3*conjugate(w)*e[2]*r[3]**3 + 9*a**3*e[1]**3*r[3]**2 - 3*a**3*e[1]**2*e[2]*r[3]**2 - 4*a**3*e[1]**2*r[3]**2 + 3*a**3*e[1]*e[2]**2*r[3]**2 + 4*a**3*e[1]*r[3]**2 - 9*a**3*e[2]**3*r[3]**2 + 4*a**3*e[2]**2*r[3]**2 - 4*a**3*e[2]*r[3]**2 - 12*a**2*w*conjugate(w)*e[1]*e[2]*r[3]**3 + 4*a**2*w*conjugate(r[3])*e[1]*e[2]*r[3]**3 - 9*a**2*w*e[1]**3*r[3]**2 - 3*a**2*w*e[1]**2*e[2]*r[3]**2 - 3*a**2*w*e[1]*e[2]**2*r[3]**2 + 12*a**2*w*e[1]*e[2]*r[3]**2 - 9*a**2*w*e[2]**3*r[3]**2 - 3*a**2*e[1]**3*r[3]**3 + 3*a**2*e[1]**2*e[2]*r[3]**3 + 3*a**2*e[1]*e[2]**2*r[3]**3 - 4*a**2*e[1]*e[2]*r[3]**3 - 3*a**2*e[2]**3*r[3]**3 + 3*a*w*e[1]**3*r[3]**3 + 3*a*w*e[1]**2*e[2]*r[3]**3 - 3*a*w*e[1]*e[2]**2*r[3]**3 - 3*a*w*e[2]**3*r[3]**3

    p_9=

     -3*a**8*w*conjugate(w)*r[3]**2 + 3*a**8*w*conjugate(r[3])*r[3]**2 - 2*a**8*w*e[1]*r[3] - 2*a**8*w*e[2]*r[3] + 2*a**8*w*r[3] - a**8*conjugate(w)*r[3]**3 + a**8*conjugate(r[3])*r[3]**3 + 2*a**8*e[1]*r[3]**2 + 2*a**8*e[2]*r[3]**2 - 2*a**8*r[3]**2 + 3*a**7*w*e[1]*r[3]**2 - 3*a**7*w*e[2]*r[3]**2 + 3*a**7*conjugate(w)*e[1]*r[3]**2 - 3*a**7*conjugate(w)*e[2]*r[3]**2 - 3*a**7*conjugate(r[3])*e[1]*r[3]**2 + 3*a**7*conjugate(r[3])*e[2]*r[3]**2 + 2*a**7*e[1]**2*r[3] + a**7*e[1]*r[3]**3 - 2*a**7*e[1]*r[3] - 2*a**7*e[2]**2*r[3] - a**7*e[2]*r[3]**3 + 2*a**7*e[2]*r[3] + 3*a**6*w*conjugate(w)**3*r[3]**2 - 3*a**6*w*conjugate(w)**2*conjugate(r[3])*r[3]**2 + 6*a**6*w*conjugate(w)**2*e[1]*r[3] + 6*a**6*w*conjugate(w)**2*e[2]*r[3] - 6*a**6*w*conjugate(w)**2*r[3] - 4*a**6*w*conjugate(w)*conjugate(r[3])*e[1]*r[3] - 4*a**6*w*conjugate(w)*conjugate(r[3])*e[2]*r[3] + 4*a**6*w*conjugate(w)*conjugate(r[3])*r[3] + 3*a**6*w*conjugate(w)*e[1]**2 + 6*a**6*w*conjugate(w)*e[1]*e[2] - 6*a**6*w*conjugate(w)*e[1] + 3*a**6*w*conjugate(w)*e[2]**2 - 6*a**6*w*conjugate(w)*e[2] + 3*a**6*w*conjugate(w) - a**6*w*conjugate(r[3])*e[1]**2 - 2*a**6*w*conjugate(r[3])*e[1]*e[2] + 2*a**6*w*conjugate(r[3])*e[1] - a**6*w*conjugate(r[3])*e[2]**2 + 2*a**6*w*conjugate(r[3])*e[2] - a**6*w*conjugate(r[3]) - a**6*w*e[1]*r[3]**3 - a**6*w*e[2]*r[3]**3 + a**6*conjugate(w)**3*r[3]**3 - a**6*conjugate(w)**2*conjugate(r[3])*r[3]**3 + 3*a**6*conjugate(w)**2*e[1]*r[3]**2 + 3*a**6*conjugate(w)**2*e[2]*r[3]**2 - 5*a**6*conjugate(w)*conjugate(r[3])*e[1]*r[3]**2 - 5*a**6*conjugate(w)*conjugate(r[3])*e[2]*r[3]**2 + 2*a**6*conjugate(w)*conjugate(r[3])*r[3]**2 + 3*a**6*conjugate(w)*e[1]**2*r[3] + 6*a**6*conjugate(w)*e[1]*e[2]*r[3] - 2*a**6*conjugate(w)*e[1]*r[3] + 3*a**6*conjugate(w)*e[2]**2*r[3] - 2*a**6*conjugate(w)*e[2]*r[3] - a**6*conjugate(w)*r[3] - 3*a**6*conjugate(r[3])*e[1]**2*r[3] - 6*a**6*conjugate(r[3])*e[1]*e[2]*r[3] + 4*a**6*conjugate(r[3])*e[1]*r[3] - 3*a**6*conjugate(r[3])*e[2]**2*r[3] + 4*a**6*conjugate(r[3])*e[2]*r[3] - a**6*conjugate(r[3])*r[3] + a**6*e[1]**3 + 3*a**6*e[1]**2*e[2] - 3*a**6*e[1]**2*r[3]**2 - 2*a**6*e[1]**2 + 3*a**6*e[1]*e[2]**2 + 6*a**6*e[1]*e[2]*r[3]**2 - 4*a**6*e[1]*e[2] + a**6*e[1] + a**6*e[2]**3 - 3*a**6*e[2]**2*r[3]**2 - 2*a**6*e[2]**2 + a**6*e[2] - 9*a**5*w*conjugate(w)**2*e[1]*r[3]**2 + 9*a**5*w*conjugate(w)**2*e[2]*r[3]**2 + 6*a**5*w*conjugate(w)*conjugate(r[3])*e[1]*r[3]**2 - 6*a**5*w*conjugate(w)*conjugate(r[3])*e[2]*r[3]**2 - 12*a**5*w*conjugate(w)*e[1]**2*r[3] + 12*a**5*w*conjugate(w)*e[1]*r[3] + 12*a**5*w*conjugate(w)*e[2]**2*r[3] - 12*a**5*w*conjugate(w)*e[2]*r[3] + 4*a**5*w*conjugate(r[3])*e[1]**2*r[3] - 4*a**5*w*conjugate(r[3])*e[1]*r[3] - 4*a**5*w*conjugate(r[3])*e[2]**2*r[3] + 4*a**5*w*conjugate(r[3])*e[2]*r[3] - 3*a**5*w*e[1]**3 - 3*a**5*w*e[1]**2*e[2] + 6*a**5*w*e[1]**2 + 3*a**5*w*e[1]*e[2]**2 - 3*a**5*w*e[1] + 3*a**5*w*e[2]**3 - 6*a**5*w*e[2]**2 + 3*a**5*w*e[2] - 3*a**5*conjugate(w)**2*e[1]*r[3]**3 + 3*a**5*conjugate(w)**2*e[2]*r[3]**3 + 2*a**5*conjugate(w)*conjugate(r[3])*e[1]*r[3]**3 - 2*a**5*conjugate(w)*conjugate(r[3])*e[2]*r[3]**3 - 6*a**5*conjugate(w)*e[1]**2*r[3]**2 + 6*a**5*conjugate(w)*e[2]**2*r[3]**2 + 5*a**5*conjugate(r[3])*e[1]**2*r[3]**2 - 2*a**5*conjugate(r[3])*e[1]*r[3]**2 - 5*a**5*conjugate(r[3])*e[2]**2*r[3]**2 + 2*a**5*conjugate(r[3])*e[2]*r[3]**2 - 3*a**5*e[1]**3*r[3] - 3*a**5*e[1]**2*e[2]*r[3] + a**5*e[1]**2*r[3]**3 + 2*a**5*e[1]**2*r[3] + 3*a**5*e[1]*e[2]**2*r[3] + a**5*e[1]*r[3] + 3*a**5*e[2]**3*r[3] - a**5*e[2]**2*r[3]**3 - 2*a**5*e[2]**2*r[3] - a**5*e[2]*r[3] + 3*a**4*w*conjugate(w)**2*e[1]*r[3]**3 + 3*a**4*w*conjugate(w)**2*e[2]*r[3]**3 - 2*a**4*w*conjugate(w)*conjugate(r[3])*e[1]*r[3]**3 - 2*a**4*w*conjugate(w)*conjugate(r[3])*e[2]*r[3]**3 + 15*a**4*w*conjugate(w)*e[1]**2*r[3]**2 - 6*a**4*w*conjugate(w)*e[1]*e[2]*r[3]**2 - 6*a**4*w*conjugate(w)*e[1]*r[3]**2 + 15*a**4*w*conjugate(w)*e[2]**2*r[3]**2 - 6*a**4*w*conjugate(w)*e[2]*r[3]**2 - 5*a**4*w*conjugate(r[3])*e[1]**2*r[3]**2 + 2*a**4*w*conjugate(r[3])*e[1]*e[2]*r[3]**2 + 2*a**4*w*conjugate(r[3])*e[1]*r[3]**2 - 5*a**4*w*conjugate(r[3])*e[2]**2*r[3]**2 + 2*a**4*w*conjugate(r[3])*e[2]*r[3]**2 + 9*a**4*w*e[1]**3*r[3] + 3*a**4*w*e[1]**2*e[2]*r[3] - 12*a**4*w*e[1]**2*r[3] + 3*a**4*w*e[1]*e[2]**2*r[3] + 3*a**4*w*e[1]*r[3] + 9*a**4*w*e[2]**3*r[3] - 12*a**4*w*e[2]**2*r[3] + 3*a**4*w*e[2]*r[3] + 3*a**4*conjugate(w)*e[1]**2*r[3]**3 - 6*a**4*conjugate(w)*e[1]*e[2]*r[3]**3 + 2*a**4*conjugate(w)*e[1]*r[3]**3 + 3*a**4*conjugate(w)*e[2]**2*r[3]**3 + 2*a**4*conjugate(w)*e[2]*r[3]**3 - 2*a**4*conjugate(r[3])*e[1]**2*r[3]**3 - 2*a**4*conjugate(r[3])*e[2]**2*r[3]**3 + 3*a**4*e[1]**3*r[3]**2 - 3*a**4*e[1]**2*e[2]*r[3]**2 + 2*a**4*e[1]**2*r[3]**2 - 3*a**4*e[1]*e[2]**2*r[3]**2 + 4*a**4*e[1]*e[2]*r[3]**2 - 2*a**4*e[1]*r[3]**2 + 3*a**4*e[2]**3*r[3]**2 + 2*a**4*e[2]**2*r[3]**2 - 2*a**4*e[2]*r[3]**2 - 6*a**3*w*conjugate(w)*e[1]**2*r[3]**3 + 6*a**3*w*conjugate(w)*e[2]**2*r[3]**3 + 2*a**3*w*conjugate(r[3])*e[1]**2*r[3]**3 - 2*a**3*w*conjugate(r[3])*e[2]**2*r[3]**3 - 9*a**3*w*e[1]**3*r[3]**2 + 3*a**3*w*e[1]**2*e[2]*r[3]**2 + 6*a**3*w*e[1]**2*r[3]**2 - 3*a**3*w*e[1]*e[2]**2*r[3]**2 + 9*a**3*w*e[2]**3*r[3]**2 - 6*a**3*w*e[2]**2*r[3]**2 - a**3*e[1]**3*r[3]**3 + 3*a**3*e[1]**2*e[2]*r[3]**3 - 2*a**3*e[1]**2*r[3]**3 - 3*a**3*e[1]*e[2]**2*r[3]**3 + a**3*e[2]**3*r[3]**3 + 2*a**3*e[2]**2*r[3]**3 + 3*a**2*w*e[1]**3*r[3]**3 - 3*a**2*w*e[1]**2*e[2]*r[3]**3 - 3*a**2*w*e[1]*e[2]**2*r[3]**3 + 3*a**2*w*e[2]**3*r[3]**3

    p_10=

     a**8*w*conjugate(w)*r[3]**3 - a**8*w*conjugate(r[3])*r[3]**3 + a**8*w*e[1]*r[3]**2 + a**8*w*e[2]*r[3]**2 - a**8*w*r[3]**2 - a**8*e[1]*r[3]**3 - a**8*e[2]*r[3]**3 + a**8*r[3]**3 - a**7*w*e[1]*r[3]**3 + a**7*w*e[2]*r[3]**3 - a**7*conjugate(w)*e[1]*r[3]**3 + a**7*conjugate(w)*e[2]*r[3]**3 + a**7*conjugate(r[3])*e[1]*r[3]**3 - a**7*conjugate(r[3])*e[2]*r[3]**3 - a**7*e[1]**2*r[3]**2 + a**7*e[1]*r[3]**2 + a**7*e[2]**2*r[3]**2 - a**7*e[2]*r[3]**2 - a**6*w*conjugate(w)**3*r[3]**3 + a**6*w*conjugate(w)**2*conjugate(r[3])*r[3]**3 - 3*a**6*w*conjugate(w)**2*e[1]*r[3]**2 - 3*a**6*w*conjugate(w)**2*e[2]*r[3]**2 + 3*a**6*w*conjugate(w)**2*r[3]**2 + 2*a**6*w*conjugate(w)*conjugate(r[3])*e[1]*r[3]**2 + 2*a**6*w*conjugate(w)*conjugate(r[3])*e[2]*r[3]**2 - 2*a**6*w*conjugate(w)*conjugate(r[3])*r[3]**2 - 3*a**6*w*conjugate(w)*e[1]**2*r[3] - 6*a**6*w*conjugate(w)*e[1]*e[2]*r[3] + 6*a**6*w*conjugate(w)*e[1]*r[3] - 3*a**6*w*conjugate(w)*e[2]**2*r[3] + 6*a**6*w*conjugate(w)*e[2]*r[3] - 3*a**6*w*conjugate(w)*r[3] + a**6*w*conjugate(r[3])*e[1]**2*r[3] + 2*a**6*w*conjugate(r[3])*e[1]*e[2]*r[3] - 2*a**6*w*conjugate(r[3])*e[1]*r[3] + a**6*w*conjugate(r[3])*e[2]**2*r[3] - 2*a**6*w*conjugate(r[3])*e[2]*r[3] + a**6*w*conjugate(r[3])*r[3] - a**6*w*e[1]**3 - 3*a**6*w*e[1]**2*e[2] + 3*a**6*w*e[1]**2 - 3*a**6*w*e[1]*e[2]**2 + 6*a**6*w*e[1]*e[2] - 3*a**6*w*e[1] - a**6*w*e[2]**3 + 3*a**6*w*e[2]**2 - 3*a**6*w*e[2] + a**6*w - a**6*conjugate(w)**2*r[3]**3 + a**6*conjugate(w)*conjugate(r[3])*e[1]*r[3]**3 + a**6*conjugate(w)*conjugate(r[3])*e[2]*r[3]**3 - 2*a**6*conjugate(w)*e[1]*r[3]**2 - 2*a**6*conjugate(w)*e[2]*r[3]**2 + 2*a**6*conjugate(w)*r[3]**2 + a**6*conjugate(r[3])*e[1]**2*r[3]**2 + 2*a**6*conjugate(r[3])*e[1]*e[2]*r[3]**2 - a**6*conjugate(r[3])*e[1]*r[3]**2 + a**6*conjugate(r[3])*e[2]**2*r[3]**2 - a**6*conjugate(r[3])*e[2]*r[3]**2 + a**6*e[1]**2*r[3]**3 - a**6*e[1]**2*r[3] - 2*a**6*e[1]*e[2]*r[3]**3 - 2*a**6*e[1]*e[2]*r[3] + 2*a**6*e[1]*r[3] + a**6*e[2]**2*r[3]**3 - a**6*e[2]**2*r[3] + 2*a**6*e[2]*r[3] - a**6*r[3] + 3*a**5*w*conjugate(w)**2*e[1]*r[3]**3 - 3*a**5*w*conjugate(w)**2*e[2]*r[3]**3 - 2*a**5*w*conjugate(w)*conjugate(r[3])*e[1]*r[3]**3 + 2*a**5*w*conjugate(w)*conjugate(r[3])*e[2]*r[3]**3 + 6*a**5*w*conjugate(w)*e[1]**2*r[3]**2 - 6*a**5*w*conjugate(w)*e[1]*r[3]**2 - 6*a**5*w*conjugate(w)*e[2]**2*r[3]**2 + 6*a**5*w*conjugate(w)*e[2]*r[3]**2 - 2*a**5*w*conjugate(r[3])*e[1]**2*r[3]**2 + 2*a**5*w*conjugate(r[3])*e[1]*r[3]**2 + 2*a**5*w*conjugate(r[3])*e[2]**2*r[3]**2 - 2*a**5*w*conjugate(r[3])*e[2]*r[3]**2 + 3*a**5*w*e[1]**3*r[3] + 3*a**5*w*e[1]**2*e[2]*r[3] - 6*a**5*w*e[1]**2*r[3] - 3*a**5*w*e[1]*e[2]**2*r[3] + 3*a**5*w*e[1]*r[3] - 3*a**5*w*e[2]**3*r[3] + 6*a**5*w*e[2]**2*r[3] - 3*a**5*w*e[2]*r[3] + 2*a**5*conjugate(w)*e[1]*r[3]**3 - 2*a**5*conjugate(w)*e[2]*r[3]**3 - a**5*conjugate(r[3])*e[1]**2*r[3]**3 + a**5*conjugate(r[3])*e[2]**2*r[3]**3 + 2*a**5*e[1]**2*r[3]**2 - 2*a**5*e[1]*r[3]**2 - 2*a**5*e[2]**2*r[3]**2 + 2*a**5*e[2]*r[3]**2 - 3*a**4*w*conjugate(w)*e[1]**2*r[3]**3 + 6*a**4*w*conjugate(w)*e[1]*e[2]*r[3]**3 - 3*a**4*w*conjugate(w)*e[2]**2*r[3]**3 + a**4*w*conjugate(r[3])*e[1]**2*r[3]**3 - 2*a**4*w*conjugate(r[3])*e[1]*e[2]*r[3]**3 + a**4*w*conjugate(r[3])*e[2]**2*r[3]**3 - 3*a**4*w*e[1]**3*r[3]**2 + 3*a**4*w*e[1]**2*e[2]*r[3]**2 + 3*a**4*w*e[1]**2*r[3]**2 + 3*a**4*w*e[1]*e[2]**2*r[3]**2 - 6*a**4*w*e[1]*e[2]*r[3]**2 - 3*a**4*w*e[2]**3*r[3]**2 + 3*a**4*w*e[2]**2*r[3]**2 - a**4*e[1]**2*r[3]**3 + 2*a**4*e[1]*e[2]*r[3]**3 - a**4*e[2]**2*r[3]**3 + a**3*w*e[1]**3*r[3]**3 - 3*a**3*w*e[1]**2*e[2]*r[3]**3 + 3*a**3*w*e[1]*e[2]**2*r[3]**3 - a**3*w*e[2]**3*r[3]**3

# Derivation of the polynomial coefficients for the critical/caustic curves

The critical curves are obtained by solving the following equation for the critical image positions $z$

$$
\sum_{i=1}^N\frac{\epsilon_i}{(\bar{z}-\bar{r}_i)^2}=e^{i\phi}
$$

for each value of the parameter $\phi=[0,2\pi)$. Taking the complex conjugate of the above equation, we obtain

$$
\sum_{i=1}^N\frac{\epsilon_i}{(z-r_i)^2}=e^{-i\phi}
$$

Defining

$$
G' = \sum_{j=1}^N\epsilon_j\prod_{i=1,\;i\neq j}^Nz^2_i
$$

$$
H'= \prod_{i=1}^Nz^2_i
$$

we have

$$
e^{-i\phi}=\frac{G'}{H'}
$$

and

$$
e^{-i\phi}H' - G' = 0
$$

```python
def compute_polynomial_critical(N):
    # Define symbols
    z, w = symbols(['z', 'w'], complex=True)
    r = IndexedBase('r', complex=True)
    e = IndexedBase('e', positive=True)
    i, j = symbols(['i', 'j'], integer=True)
    a = symbols('a', positive=True)
    phi = symbols('\phi', real=True)

    # Polynomial H'
    H = Product((z - r[i])**2, (i, 1, N)).doit()
    H = H.subs(r[1], a).subs(r[2], -a)

    # Polynomial G'
    G = 0
    for j in range(1, N + 1):
        G += e[j]*prod([(z - r[i])**2 for i in range(1, N + 1) if i!=j])
    G = G.subs(r[1], a).subs(r[2], -a)

    # First term
    i, j = symbols(['i', 'j'], integer=True)

    return Poly(exp(-I*phi)*H - G, z).all_coeffs()

```

## N = 2

```python
N = 2
coeffs = compute_polynomial_critical(N)
e = IndexedBase('e', positive=True)
coeffs = [expand(c.subs(e[2], 1-e[1])) for c in coeffs]
```

```python
# Print coefficients starting with the highest power coefficient
for i in range(2*N + 1):
    print(f"p_{i} =", str(coeffs[i]))
    print("\n\n")
```

    p_0 = exp(-I*\phi)

    p_1 = 0

    p_2 = -2*a**2*exp(-I*\phi) - 1

    p_3 = -4*a*e[1] + 2*a

    p_4 = a**4*exp(-I*\phi) - a**2

## N = 3

```python
N = 3
coeffs = compute_polynomial_critical(N)
coeffs = [expand(c.subs(e[3], 1- e[1] - e[2])) for c in coeffs]
```

```python
# Print coefficients starting with the highest power coefficient
for i in range(2*N + 1):
    print(f"p_{i} =", str(coeffs[i]))
    print("\n\n")
```

    p_0 = exp(-I*\phi)

    p_1 = -2*exp(-I*\phi)*r[3]

    p_2 = -2*a**2*exp(-I*\phi) - 1 + exp(-I*\phi)*r[3]**2

    p_3 = 4*a**2*exp(-I*\phi)*r[3] - 2*a*e[1] + 2*a*e[2] + 2*e[1]*r[3] + 2*e[2]*r[3]

    p_4 = a**4*exp(-I*\phi) - 3*a**2*e[1] - 3*a**2*e[2] + 2*a**2 - 2*a**2*exp(-I*\phi)*r[3]**2 + 4*a*e[1]*r[3] - 4*a*e[2]*r[3] - e[1]*r[3]**2 - e[2]*r[3]**2

    p_5 = -2*a**4*exp(-I*\phi)*r[3] + 2*a**2*e[1]*r[3] + 2*a**2*e[2]*r[3] - 2*a*e[1]*r[3]**2 + 2*a*e[2]*r[3]**2

    p_6 = a**4*e[1] + a**4*e[2] - a**4 + a**4*exp(-I*\phi)*r[3]**2 - a**2*e[1]*r[3]**2 - a**2*e[2]*r[3]**2

```python

```


# Matrix generation

## Init symbols for *sympy*

```python
from sympy import *
from geom_util import *
from sympy.vector import CoordSys3D
N = CoordSys3D('N')
alpha1, alpha2, alpha3 = symbols("alpha_1 alpha_2 alpha_3", real = True, positive=True)
init_printing()

%matplotlib inline

%reload_ext autoreload
%autoreload 2
%aimport geom_util
```

### Lame params

```python
# h1 = Function("H1")
# h2 = Function("H2")
# h3 = Function("H3")

# H1 = h1(alpha1, alpha2, alpha3)
# H2 = h2(alpha1, alpha2, alpha3)
# H3 = h3(alpha1, alpha2, alpha3)

H1,H2,H3=symbols('H1,H2,H3')

H=[H1, H2, H3]
DIM=3
dH = zeros(DIM,DIM)
for i in range(DIM):
    for j in range(DIM):
        dH[i,j]=Symbol('H_{{{},{}}}'.format(i+1,j+1))

dH

```

### Metric tensor

${\displaystyle \hat{G}=\sum_{i,j} g^{ij}\vec{R}_i\vec{R}_j}$

```python
G_up = getMetricTensorUpLame(H1, H2, H3)
```

${\displaystyle \hat{G}=\sum_{i,j} g_{ij}\vec{R}^i\vec{R}^j}$

```python
G_down = getMetricTensorDownLame(H1, H2, H3)
```

### Christoffel symbols

```python
DIM=3

G_down_diff = MutableDenseNDimArray.zeros(DIM, DIM, DIM)
for i in range(DIM):
    for j in range(DIM):
        for k in range(DIM):

            G_down_diff[i,i,k]=2*H[i]*dH[i,k]

GK = getChristoffelSymbols2(G_up, G_down_diff, (alpha1, alpha2, alpha3))
GK
```

### Gradient of vector

$
\left(
\begin{array}{c}
\nabla_1 u_1 \\ \nabla_2 u_1 \\ \nabla_3 u_1 \\
\nabla_1 u_2 \\ \nabla_2 u_2 \\ \nabla_3 u_2 \\
\nabla_1 u_3 \\ \nabla_2 u_3 \\ \nabla_3 u_3 \\
\end{array}
\right)
=
B \cdot
\left(
\begin{array}{c}
u_1 \\
\frac { \partial u_1 } { \partial \alpha_1} \\
\frac { \partial u_1 } { \partial \alpha_2} \\
\frac { \partial u_1 } { \partial \alpha_3} \\
u_2 \\
\frac { \partial u_2 } { \partial \alpha_1} \\
\frac { \partial u_2 } { \partial \alpha_2} \\
\frac { \partial u_2 } { \partial \alpha_3} \\
u_3 \\
\frac { \partial u_3 } { \partial \alpha_1} \\
\frac { \partial u_3 } { \partial \alpha_2} \\
\frac { \partial u_3 } { \partial \alpha_3} \\
\end{array}
\right)
= B \cdot D \cdot
\left(
\begin{array}{c}
u^1 \\
\frac { \partial u^1 } { \partial \alpha_1} \\
\frac { \partial u^1 } { \partial \alpha_2} \\
\frac { \partial u^1 } { \partial \alpha_3} \\
u^2 \\
\frac { \partial u^2 } { \partial \alpha_1} \\
\frac { \partial u^2 } { \partial \alpha_2} \\
\frac { \partial u^2 } { \partial \alpha_3} \\
u^3 \\
\frac { \partial u^3 } { \partial \alpha_1} \\
\frac { \partial u^3 } { \partial \alpha_2} \\
\frac { \partial u^3 } { \partial \alpha_3} \\
\end{array}
\right)
$

```python
def row_index_to_i_j_grad(i_row):
    return i_row // 3, i_row % 3

B = zeros(9, 12)
B[0,1] = S(1)
B[1,2] = S(1)

B[2,3] = S(1)

B[3,5] = S(1)
B[4,6] = S(1)
B[5,7] = S(1)

B[6,9] = S(1)
B[7,10] = S(1)
B[8,11] = S(1)

for row_index in range(9):
    i,j=row_index_to_i_j_grad(row_index)
    B[row_index, 0] = -GK[i,j,0]
    B[row_index, 4] = -GK[i,j,1]
    B[row_index, 8] = -GK[i,j,2]

B
```

### Strain tensor

$
\left(
\begin{array}{c}
\varepsilon_{11} \\
\varepsilon_{22} \\
\varepsilon_{33} \\
2\varepsilon_{12} \\
2\varepsilon_{13} \\
2\varepsilon_{23} \\
\end{array}
\right)
=
\left(E + E_{NL} \left( \nabla \vec{u} \right) \right) \cdot
\left(
\begin{array}{c}
\nabla_1 u_1 \\ \nabla_2 u_1 \\ \nabla_3 u_1 \\
\nabla_1 u_2 \\ \nabla_2 u_2 \\ \nabla_3 u_2 \\
\nabla_1 u_3 \\ \nabla_2 u_3 \\ \nabla_3 u_3 \\
\end{array}
\right)$

```python
E=zeros(6,9)
E[0,0]=1
E[1,4]=1
E[2,8]=1
E[3,1]=1
E[3,3]=1
E[4,2]=1
E[4,6]=1
E[5,5]=1
E[5,7]=1
E
```

```python
def E_NonLinear(grad_u):
    N = 3

    du = zeros(N, N)

    #    print("===Deformations===")

    for i in range(N):
        for j in range(N):
            index = i*N+j
            du[j,i] = grad_u[index]

    #    print("========")

    I = eye(3)

    a_values = S(1)/S(2) * du * G_up

    E_NL = zeros(6,9)
    E_NL[0,0] = a_values[0,0]
    E_NL[0,3] = a_values[0,1]
    E_NL[0,6] = a_values[0,2]

    E_NL[1,1] = a_values[1,0]
    E_NL[1,4] = a_values[1,1]
    E_NL[1,7] = a_values[1,2]

    E_NL[2,2] = a_values[2,0]
    E_NL[2,5] = a_values[2,1]
    E_NL[2,8] = a_values[2,2]

    E_NL[3,1] = 2*a_values[0,0]
    E_NL[3,4] = 2*a_values[0,1]
    E_NL[3,7] = 2*a_values[0,2]

    E_NL[4,0] = 2*a_values[2,0]
    E_NL[4,3] = 2*a_values[2,1]
    E_NL[4,6] = 2*a_values[2,2]

    E_NL[5,2] = 2*a_values[1,0]
    E_NL[5,5] = 2*a_values[1,1]
    E_NL[5,8] = 2*a_values[1,2]

    return E_NL

%aimport geom_util
u=getUHat3D(alpha1, alpha2, alpha3)

# u=getUHatU3Main(alpha1, alpha2, alpha3)

gradu=B*u

E_NL = E_NonLinear(gradu)*B

E_NL

```

### Physical coordinates

$u_i=u_{[i]} H_i$

```python
P=zeros(12,12)
P[0,0]=H[0]
P[1,0]=dH[0,0]
P[1,1]=H[0]
P[2,0]=dH[0,1]
P[2,2]=H[0]
P[3,0]=dH[0,2]
P[3,3]=H[0]

P[4,4]=H[1]
P[5,4]=dH[1,0]
P[5,5]=H[1]
P[6,4]=dH[1,1]
P[6,6]=H[1]
P[7,4]=dH[1,2]
P[7,7]=H[1]

P[8,8]=H[2]
P[9,8]=dH[2,0]
P[9,9]=H[2]
P[10,8]=dH[2,1]
P[10,10]=H[2]
P[11,8]=dH[2,2]
P[11,11]=H[2]
P=simplify(P)
P
```

```python
B_P = zeros(9,9)

for i in range(3):
    for j in range(3):

        row_index = i*3+j

        B_P[row_index, row_index] = 1/(H[i]*H[j])

Grad_U_P = simplify(B_P*B*P)
Grad_U_P
```

```python
StrainL=simplify(E*Grad_U_P)
StrainL
```

```python
%aimport geom_util

u=getUHat3D(alpha1, alpha2, alpha3)

gradup=Grad_U_P*u

E_NLp = E_NonLinear(gradup)*Grad_U_P

simplify(E_NLp)
```

### Tymoshenko theory

$u_1 \left( \alpha_1, \alpha_2, \alpha_3 \right)=u\left( \alpha_1 \right)+\alpha_3\gamma \left( \alpha_1 \right) $

$u_2 \left( \alpha_1, \alpha_2, \alpha_3 \right)=0 $

$u_3 \left( \alpha_1, \alpha_2, \alpha_3 \right)=w\left( \alpha_1 \right) $

$ \left(
\begin{array}{c}
u_1 \\
\frac { \partial u_1 } { \partial \alpha_1} \\
\frac { \partial u_1 } { \partial \alpha_2} \\
\frac { \partial u_1 } { \partial \alpha_3} \\
u_2 \\
\frac { \partial u_2 } { \partial \alpha_1} \\
\frac { \partial u_2 } { \partial \alpha_2} \\
\frac { \partial u_2 } { \partial \alpha_3} \\
u_3 \\
\frac { \partial u_3 } { \partial \alpha_1} \\
\frac { \partial u_3 } { \partial \alpha_2} \\
\frac { \partial u_3 } { \partial \alpha_3} \\
\end{array}
\right) = T \cdot
\left(
\begin{array}{c}
u \\
\frac { \partial u } { \partial \alpha_1} \\
\gamma \\
\frac { \partial \gamma } { \partial \alpha_1} \\
w \\
\frac { \partial w } { \partial \alpha_1} \\
\end{array}
\right) $

```python
T=zeros(12,6)
T[0,0]=1
T[0,2]=alpha3
T[1,1]=1
T[1,3]=alpha3
T[3,2]=1

T[8,4]=1
T[9,5]=1
T
```

```python
D_p_T = StrainL*T
simplify(D_p_T)
```

```python
u = Function("u")
t = Function("theta")
w = Function("w")

u1=u(alpha1)+alpha3*t(alpha1)
u3=w(alpha1)

gu = zeros(12,1)
gu[0] = u1
gu[1] = u1.diff(alpha1)
gu[3] = u1.diff(alpha3)

gu[8] = u3
gu[9] = u3.diff(alpha1)

gradup=Grad_U_P*gu

# o20=(K*u(alpha1)-w(alpha1).diff(alpha1)+t(alpha1))/2
# o21=K*t(alpha1)
# O=1/2*o20*o20+alpha3*o20*o21-alpha3*K/2*o20*o20
# O=expand(O)
# O=collect(O,alpha3)
# simplify(O)

StrainNL = E_NonLinear(gradup)*gradup
simplify(StrainNL)
```

### Square theory

$u^1 \left( \alpha_1, \alpha_2, \alpha_3 \right)=u_{10}\left( \alpha_1 \right)p_0\left( \alpha_3 \right)+u_{11}\left( \alpha_1 \right)p_1\left( \alpha_3 \right)+u_{12}\left( \alpha_1 \right)p_2\left( \alpha_3 \right) $

$u^2 \left( \alpha_1, \alpha_2, \alpha_3 \right)=0 $

$u^3 \left( \alpha_1, \alpha_2, \alpha_3 \right)=u_{30}\left( \alpha_1 \right)p_0\left( \alpha_3 \right)+u_{31}\left( \alpha_1 \right)p_1\left( \alpha_3 \right)+u_{32}\left( \alpha_1 \right)p_2\left( \alpha_3 \right) $

$ \left(
\begin{array}{c}
u^1 \\
\frac { \partial u^1 } { \partial \alpha_1} \\
\frac { \partial u^1 } { \partial \alpha_2} \\
\frac { \partial u^1 } { \partial \alpha_3} \\
u^2 \\
\frac { \partial u^2 } { \partial \alpha_1} \\
\frac { \partial u^2 } { \partial \alpha_2} \\
\frac { \partial u^2 } { \partial \alpha_3} \\
u^3 \\
\frac { \partial u^3 } { \partial \alpha_1} \\
\frac { \partial u^3 } { \partial \alpha_2} \\
\frac { \partial u^3 } { \partial \alpha_3} \\
\end{array}
\right) = L \cdot
\left(
\begin{array}{c}
u_{10} \\
\frac { \partial u_{10} } { \partial \alpha_1} \\
u_{11} \\
\frac { \partial u_{11} } { \partial \alpha_1} \\
u_{12} \\
\frac { \partial u_{12} } { \partial \alpha_1} \\
u_{30} \\
\frac { \partial u_{30} } { \partial \alpha_1} \\
u_{31} \\
\frac { \partial u_{31} } { \partial \alpha_1} \\
u_{32} \\
\frac { \partial u_{32} } { \partial \alpha_1} \\
\end{array}
\right) $

```python
L=zeros(12,12)
h=Symbol('h')
# p0=1/2-alpha3/h
# p1=1/2+alpha3/h
# p2=1-(2*alpha3/h)**2

P0=Function('p_0')
P1=Function('p_1')
P2=Function('p_2')
# p1=1/2+alpha3/h
# p2=1-(2*alpha3/h)**2

p0=P0(alpha3)
p1=P1(alpha3)
p2=P2(alpha3)

L[0,0]=p0
L[0,2]=p1
L[0,4]=p2

L[1,1]=p0
L[1,3]=p1
L[1,5]=p2

L[3,0]=p0.diff(alpha3)
L[3,2]=p1.diff(alpha3)
L[3,4]=p2.diff(alpha3)

L[8,6]=p0
L[8,8]=p1
L[8,10]=p2

L[9,7]=p0
L[9,9]=p1
L[9,11]=p2

L[11,6]=p0.diff(alpha3)
L[11,8]=p1.diff(alpha3)
L[11,10]=p2.diff(alpha3)

L
```

```python
D_p_L = StrainL*L
simplify(D_p_L)
```

```python
h = 0.5
exp=(0.5-alpha3/h)*(1-(2*alpha3/h)**2)#/(1+alpha3*0.8)
p02=integrate(exp, (alpha3, -h/2, h/2))
integral = expand(simplify(p02))
integral
```

## Mass matrix

```python
rho=Symbol('rho')
B_h=zeros(3,12)
B_h[0,0]=1
B_h[1,4]=1
B_h[2,8]=1
M=simplify(rho*P.T*B_h.T*G_up*B_h*P)
M
```


# GP Regression with Uncertain Inputs

## Introduction

In this notebook, we're going to demonstrate one way of dealing with uncertainty in our training data. Let's say that we're collecting training data that models the following function.

\begin{align}
y &= \sin(2\pi x) + \epsilon \\
  \epsilon &\sim \mathcal{N}(0, 0.2)
\end{align}

However, now assume that we're a bit uncertain about our features. In particular, we're going to assume that every `x_i` value is not a point but a distribution instead. E.g.

$$ x_i \sim \mathcal{N}(\mu_i, \sigma_i). $$

### Using a distributional kernel to deal with uncertain inputs

Rather than using a variational method (see the GP Regression with Uncertian Inputs tutorial in the variational examples), if we explicitly know the type of uncertainty in our inputs we can pass that into our kernel.

More specifically, assuming Gaussian inputs, we will compute the symmetrized KL divergence between the Gaussian inputs.

```python
import math
import torch
import tqdm
import gpytorch
from matplotlib import pyplot as plt

%matplotlib inline
%load_ext autoreload
%autoreload 2
```

```python
# Training data is 100 points in [0,1] inclusive regularly spaced
train_x_mean = torch.linspace(0, 1, 20)
# We'll assume the variance shrinks the closer we get to 1
train_x_stdv = torch.linspace(0.03, 0.01, 20)

# True function is sin(2*pi*x) with Gaussian noise
train_y = torch.sin(train_x_mean * (2 * math.pi)) + torch.randn(train_x_mean.size()) * 0.2
```

To effectively pass in the training distributional data, we will need to stack the mean and log variances.

```python
train_x_distributional = torch.stack((train_x_mean, (train_x_stdv**2).log()), dim=1)
```

```python
f, ax = plt.subplots(1, 1, figsize=(8, 3))
ax.errorbar(train_x_mean, train_y, xerr=(train_x_stdv * 2), fmt="k*", label="Train Data")
ax.legend()
```

We train the hyperparameters of the resulting distributional GP via type-II gradient descent, as is standard in many settings. We could also do fully Bayesian inference.

```python
from gpytorch.models import ExactGP
from gpytorch.kernels import GaussianSymmetrizedKLKernel, ScaleKernel
from gpytorch.means import ConstantMean

class ExactGPModel(ExactGP):
    def __init__(self, train_x, train_y, likelihood):
        super(ExactGPModel, self).__init__(train_x, train_y, likelihood)
        self.mean_module = ConstantMean()
        self.covar_module = ScaleKernel(GaussianSymmetrizedKLKernel())

    def forward(self, x):
        mean_x = self.mean_module(x)
        covar_x = self.covar_module(x)
        return gpytorch.distributions.MultivariateNormal(mean_x, covar_x)

# initialize likelihood and model
likelihood = gpytorch.likelihoods.GaussianLikelihood()
model = ExactGPModel(train_x_distributional, train_y, likelihood)
```

```python
# this is for running the notebook in our testing framework
import os
smoke_test = ('CI' in os.environ)
training_iter = 2 if smoke_test else 500

# Find optimal model hyperparameters
model.train()
likelihood.train()

# Use the adam optimizer
optimizer = torch.optim.Adam(model.parameters(), lr=0.25)  # Includes GaussianLikelihood parameters

# "Loss" for GPs - the marginal log likelihood
mll = gpytorch.mlls.ExactMarginalLogLikelihood(likelihood, model)

for i in range(training_iter):
    # Zero gradients from previous iteration
    optimizer.zero_grad()
    # Output from model
    output = model(train_x_distributional)
    # Calc loss and backprop gradients
    loss = -mll(output, train_y)
    loss.backward()
    print('Iter %d/%d - Loss: %.3f   lengthscale: %.3f   noise: %.3f' % (
        i + 1, training_iter, loss.item(),
        model.covar_module.base_kernel.lengthscale.item(),
        model.likelihood.noise.item()
    ))
    optimizer.step()
```

    Iter 1/500 - Loss: 1.274   lengthscale: 0.693   noise: 0.693
    Iter 2/500 - Loss: 1.242   lengthscale: 0.826   noise: 0.576
    Iter 3/500 - Loss: 1.173   lengthscale: 0.955   noise: 0.475
    Iter 4/500 - Loss: 1.141   lengthscale: 1.102   noise: 0.389
    Iter 5/500 - Loss: 1.121   lengthscale: 1.266   noise: 0.317
    Iter 6/500 - Loss: 1.097   lengthscale: 1.442   noise: 0.261
    Iter 7/500 - Loss: 1.093   lengthscale: 1.621   noise: 0.220
    Iter 8/500 - Loss: 1.112   lengthscale: 1.801   noise: 0.192
    Iter 9/500 - Loss: 1.112   lengthscale: 1.988   noise: 0.177
    Iter 10/500 - Loss: 1.095   lengthscale: 2.188   noise: 0.172
    Iter 11/500 - Loss: 1.081   lengthscale: 2.401   noise: 0.172
    Iter 12/500 - Loss: 1.068   lengthscale: 2.624   noise: 0.177
    Iter 13/500 - Loss: 1.049   lengthscale: 2.856   noise: 0.183
    Iter 14/500 - Loss: 1.031   lengthscale: 3.095   noise: 0.188
    Iter 15/500 - Loss: 1.020   lengthscale: 3.340   noise: 0.191
    Iter 16/500 - Loss: 1.014   lengthscale: 3.589   noise: 0.189
    Iter 17/500 - Loss: 1.007   lengthscale: 3.842   noise: 0.183
    Iter 18/500 - Loss: 0.997   lengthscale: 4.098   noise: 0.173
    Iter 19/500 - Loss: 0.983   lengthscale: 4.356   noise: 0.159
    Iter 20/500 - Loss: 0.968   lengthscale: 4.616   noise: 0.144
    Iter 21/500 - Loss: 0.951   lengthscale: 4.877   noise: 0.128
    Iter 22/500 - Loss: 0.933   lengthscale: 5.139   noise: 0.112
    Iter 23/500 - Loss: 0.913   lengthscale: 5.402   noise: 0.097
    Iter 24/500 - Loss: 0.891   lengthscale: 5.667   noise: 0.083
    Iter 25/500 - Loss: 0.870   lengthscale: 5.933   noise: 0.070
    Iter 26/500 - Loss: 0.850   lengthscale: 6.202   noise: 0.059
    Iter 27/500 - Loss: 0.832   lengthscale: 6.474   noise: 0.050
    Iter 28/500 - Loss: 0.814   lengthscale: 6.748   noise: 0.042
    Iter 29/500 - Loss: 0.794   lengthscale: 7.025   noise: 0.035
    Iter 30/500 - Loss: 0.775   lengthscale: 7.304   noise: 0.029
    Iter 31/500 - Loss: 0.756   lengthscale: 7.586   noise: 0.024
    Iter 32/500 - Loss: 0.737   lengthscale: 7.868   noise: 0.020
    Iter 33/500 - Loss: 0.719   lengthscale: 8.152   noise: 0.017
    Iter 34/500 - Loss: 0.701   lengthscale: 8.437   noise: 0.014
    Iter 35/500 - Loss: 0.684   lengthscale: 8.722   noise: 0.012
    Iter 36/500 - Loss: 0.668   lengthscale: 9.007   noise: 0.010
    Iter 37/500 - Loss: 0.653   lengthscale: 9.291   noise: 0.008
    Iter 38/500 - Loss: 0.637   lengthscale: 9.575   noise: 0.007
    Iter 39/500 - Loss: 0.621   lengthscale: 9.859   noise: 0.006
    Iter 40/500 - Loss: 0.606   lengthscale: 10.142   noise: 0.005
    Iter 41/500 - Loss: 0.591   lengthscale: 10.424   noise: 0.005
    Iter 42/500 - Loss: 0.577   lengthscale: 10.705   noise: 0.004
    Iter 43/500 - Loss: 0.564   lengthscale: 10.985   noise: 0.003
    Iter 44/500 - Loss: 0.551   lengthscale: 11.263   noise: 0.003
    Iter 45/500 - Loss: 0.539   lengthscale: 11.540   noise: 0.003
    Iter 46/500 - Loss: 0.526   lengthscale: 11.815   noise: 0.002
    Iter 47/500 - Loss: 0.514   lengthscale: 12.088   noise: 0.002
    Iter 48/500 - Loss: 0.503   lengthscale: 12.359   noise: 0.002
    Iter 49/500 - Loss: 0.492   lengthscale: 12.627   noise: 0.002
    Iter 50/500 - Loss: 0.482   lengthscale: 12.892   noise: 0.001
    Iter 51/500 - Loss: 0.472   lengthscale: 13.154   noise: 0.001
    Iter 52/500 - Loss: 0.462   lengthscale: 13.412   noise: 0.001
    Iter 53/500 - Loss: 0.453   lengthscale: 13.668   noise: 0.001
    Iter 54/500 - Loss: 0.445   lengthscale: 13.919   noise: 0.001
    Iter 55/500 - Loss: 0.437   lengthscale: 14.167   noise: 0.001
    Iter 56/500 - Loss: 0.429   lengthscale: 14.410   noise: 0.001
    Iter 57/500 - Loss: 0.422   lengthscale: 14.649   noise: 0.001
    Iter 58/500 - Loss: 0.415   lengthscale: 14.883   noise: 0.001
    Iter 59/500 - Loss: 0.409   lengthscale: 15.111   noise: 0.001
    Iter 60/500 - Loss: 0.403   lengthscale: 15.335   noise: 0.001
    Iter 61/500 - Loss: 0.398   lengthscale: 15.552   noise: 0.001
    Iter 62/500 - Loss: 0.393   lengthscale: 15.764   noise: 0.001
    Iter 63/500 - Loss: 0.389   lengthscale: 15.970   noise: 0.001
    Iter 64/500 - Loss: 0.385   lengthscale: 16.170   noise: 0.000
    Iter 65/500 - Loss: 0.381   lengthscale: 16.363   noise: 0.000
    Iter 66/500 - Loss: 0.378   lengthscale: 16.551   noise: 0.000
    Iter 67/500 - Loss: 0.375   lengthscale: 16.731   noise: 0.000
    Iter 68/500 - Loss: 0.373   lengthscale: 16.905   noise: 0.000
    Iter 69/500 - Loss: 0.371   lengthscale: 17.072   noise: 0.000
    Iter 70/500 - Loss: 0.369   lengthscale: 17.231   noise: 0.000
    Iter 71/500 - Loss: 0.367   lengthscale: 17.383   noise: 0.000
    Iter 72/500 - Loss: 0.366   lengthscale: 17.527   noise: 0.000
    Iter 73/500 - Loss: 0.365   lengthscale: 17.662   noise: 0.000
    Iter 74/500 - Loss: 0.364   lengthscale: 17.791   noise: 0.000
    Iter 75/500 - Loss: 0.363   lengthscale: 17.911   noise: 0.000
    Iter 76/500 - Loss: 0.363   lengthscale: 18.024   noise: 0.000
    Iter 77/500 - Loss: 0.362   lengthscale: 18.129   noise: 0.000
    Iter 78/500 - Loss: 0.362   lengthscale: 18.227   noise: 0.000
    Iter 79/500 - Loss: 0.362   lengthscale: 18.317   noise: 0.000
    Iter 80/500 - Loss: 0.362   lengthscale: 18.400   noise: 0.000
    Iter 81/500 - Loss: 0.362   lengthscale: 18.477   noise: 0.000
    Iter 82/500 - Loss: 0.362   lengthscale: 18.546   noise: 0.000
    Iter 83/500 - Loss: 0.362   lengthscale: 18.608   noise: 0.000
    Iter 84/500 - Loss: 0.362   lengthscale: 18.664   noise: 0.000
    Iter 85/500 - Loss: 0.362   lengthscale: 18.712   noise: 0.000
    Iter 86/500 - Loss: 0.362   lengthscale: 18.755   noise: 0.000
    Iter 87/500 - Loss: 0.362   lengthscale: 18.791   noise: 0.000
    Iter 88/500 - Loss: 0.362   lengthscale: 18.822   noise: 0.000
    Iter 89/500 - Loss: 0.362   lengthscale: 18.848   noise: 0.000
    Iter 90/500 - Loss: 0.363   lengthscale: 18.868   noise: 0.000
    Iter 91/500 - Loss: 0.363   lengthscale: 18.884   noise: 0.000
    Iter 92/500 - Loss: 0.363   lengthscale: 18.896   noise: 0.000
    Iter 93/500 - Loss: 0.363   lengthscale: 18.904   noise: 0.000
    Iter 94/500 - Loss: 0.363   lengthscale: 18.909   noise: 0.000
    Iter 95/500 - Loss: 0.363   lengthscale: 18.910   noise: 0.000
    Iter 96/500 - Loss: 0.363   lengthscale: 18.907   noise: 0.000
    Iter 97/500 - Loss: 0.363   lengthscale: 18.902   noise: 0.000
    Iter 98/500 - Loss: 0.363   lengthscale: 18.894   noise: 0.000
    Iter 99/500 - Loss: 0.362   lengthscale: 18.884   noise: 0.000
    Iter 100/500 - Loss: 0.362   lengthscale: 18.873   noise: 0.000
    Iter 101/500 - Loss: 0.362   lengthscale: 18.859   noise: 0.000
    Iter 102/500 - Loss: 0.362   lengthscale: 18.844   noise: 0.000
    Iter 103/500 - Loss: 0.362   lengthscale: 18.828   noise: 0.000
    Iter 104/500 - Loss: 0.362   lengthscale: 18.811   noise: 0.000
    Iter 105/500 - Loss: 0.362   lengthscale: 18.794   noise: 0.000
    Iter 106/500 - Loss: 0.362   lengthscale: 18.776   noise: 0.000
    Iter 107/500 - Loss: 0.362   lengthscale: 18.758   noise: 0.000
    Iter 108/500 - Loss: 0.362   lengthscale: 18.740   noise: 0.000
    Iter 109/500 - Loss: 0.362   lengthscale: 18.722   noise: 0.000
    Iter 110/500 - Loss: 0.362   lengthscale: 18.704   noise: 0.000
    Iter 111/500 - Loss: 0.362   lengthscale: 18.687   noise: 0.000
    Iter 112/500 - Loss: 0.362   lengthscale: 18.670   noise: 0.000
    Iter 113/500 - Loss: 0.362   lengthscale: 18.653   noise: 0.000
    Iter 114/500 - Loss: 0.362   lengthscale: 18.638   noise: 0.000
    Iter 115/500 - Loss: 0.362   lengthscale: 18.622   noise: 0.000
    Iter 116/500 - Loss: 0.362   lengthscale: 18.608   noise: 0.000
    Iter 117/500 - Loss: 0.362   lengthscale: 18.595   noise: 0.000
    Iter 118/500 - Loss: 0.362   lengthscale: 18.583   noise: 0.000
    Iter 119/500 - Loss: 0.362   lengthscale: 18.572   noise: 0.000
    Iter 120/500 - Loss: 0.362   lengthscale: 18.561   noise: 0.000
    Iter 121/500 - Loss: 0.362   lengthscale: 18.552   noise: 0.000
    Iter 122/500 - Loss: 0.362   lengthscale: 18.543   noise: 0.000
    Iter 123/500 - Loss: 0.362   lengthscale: 18.536   noise: 0.000
    Iter 124/500 - Loss: 0.362   lengthscale: 18.529   noise: 0.000
    Iter 125/500 - Loss: 0.362   lengthscale: 18.524   noise: 0.000
    Iter 126/500 - Loss: 0.362   lengthscale: 18.519   noise: 0.000
    Iter 127/500 - Loss: 0.362   lengthscale: 18.516   noise: 0.000
    Iter 128/500 - Loss: 0.362   lengthscale: 18.513   noise: 0.000
    Iter 129/500 - Loss: 0.362   lengthscale: 18.511   noise: 0.000
    Iter 130/500 - Loss: 0.362   lengthscale: 18.510   noise: 0.000
    Iter 131/500 - Loss: 0.362   lengthscale: 18.509   noise: 0.000
    Iter 132/500 - Loss: 0.362   lengthscale: 18.510   noise: 0.000
    Iter 133/500 - Loss: 0.362   lengthscale: 18.511   noise: 0.000
    Iter 134/500 - Loss: 0.362   lengthscale: 18.512   noise: 0.000
    Iter 135/500 - Loss: 0.362   lengthscale: 18.514   noise: 0.000
    Iter 136/500 - Loss: 0.362   lengthscale: 18.517   noise: 0.000
    Iter 137/500 - Loss: 0.362   lengthscale: 18.520   noise: 0.000
    Iter 138/500 - Loss: 0.362   lengthscale: 18.524   noise: 0.000
    Iter 139/500 - Loss: 0.362   lengthscale: 18.528   noise: 0.000
    Iter 140/500 - Loss: 0.362   lengthscale: 18.532   noise: 0.000
    Iter 141/500 - Loss: 0.362   lengthscale: 18.537   noise: 0.000
    Iter 142/500 - Loss: 0.362   lengthscale: 18.542   noise: 0.000
    Iter 143/500 - Loss: 0.362   lengthscale: 18.547   noise: 0.000
    Iter 144/500 - Loss: 0.362   lengthscale: 18.552   noise: 0.000
    Iter 145/500 - Loss: 0.362   lengthscale: 18.558   noise: 0.000
    Iter 146/500 - Loss: 0.362   lengthscale: 18.563   noise: 0.000
    Iter 147/500 - Loss: 0.362   lengthscale: 18.569   noise: 0.000
    Iter 148/500 - Loss: 0.362   lengthscale: 18.574   noise: 0.000
    Iter 149/500 - Loss: 0.362   lengthscale: 18.580   noise: 0.000
    Iter 150/500 - Loss: 0.362   lengthscale: 18.586   noise: 0.000
    Iter 151/500 - Loss: 0.362   lengthscale: 18.592   noise: 0.000
    Iter 152/500 - Loss: 0.362   lengthscale: 18.597   noise: 0.000
    Iter 153/500 - Loss: 0.362   lengthscale: 18.603   noise: 0.000
    Iter 154/500 - Loss: 0.362   lengthscale: 18.608   noise: 0.000
    Iter 155/500 - Loss: 0.362   lengthscale: 18.614   noise: 0.000
    Iter 156/500 - Loss: 0.362   lengthscale: 18.619   noise: 0.000
    Iter 157/500 - Loss: 0.362   lengthscale: 18.625   noise: 0.000
    Iter 158/500 - Loss: 0.362   lengthscale: 18.630   noise: 0.000
    Iter 159/500 - Loss: 0.362   lengthscale: 18.635   noise: 0.000
    Iter 160/500 - Loss: 0.362   lengthscale: 18.640   noise: 0.000
    Iter 161/500 - Loss: 0.362   lengthscale: 18.645   noise: 0.000
    Iter 162/500 - Loss: 0.362   lengthscale: 18.650   noise: 0.000
    Iter 163/500 - Loss: 0.362   lengthscale: 18.655   noise: 0.000
    Iter 164/500 - Loss: 0.362   lengthscale: 18.659   noise: 0.000
    Iter 165/500 - Loss: 0.361   lengthscale: 18.664   noise: 0.000
    Iter 166/500 - Loss: 0.361   lengthscale: 18.669   noise: 0.000
    Iter 167/500 - Loss: 0.361   lengthscale: 18.673   noise: 0.000
    Iter 168/500 - Loss: 0.361   lengthscale: 18.678   noise: 0.000
    Iter 169/500 - Loss: 0.361   lengthscale: 18.682   noise: 0.000
    Iter 170/500 - Loss: 0.361   lengthscale: 18.686   noise: 0.000
    Iter 171/500 - Loss: 0.361   lengthscale: 18.691   noise: 0.000
    Iter 172/500 - Loss: 0.361   lengthscale: 18.695   noise: 0.000
    Iter 173/500 - Loss: 0.361   lengthscale: 18.699   noise: 0.000
    Iter 174/500 - Loss: 0.361   lengthscale: 18.704   noise: 0.000
    Iter 175/500 - Loss: 0.361   lengthscale: 18.708   noise: 0.000
    Iter 176/500 - Loss: 0.361   lengthscale: 18.713   noise: 0.000
    Iter 177/500 - Loss: 0.361   lengthscale: 18.717   noise: 0.000
    Iter 178/500 - Loss: 0.361   lengthscale: 18.721   noise: 0.000
    Iter 179/500 - Loss: 0.361   lengthscale: 18.726   noise: 0.000
    Iter 180/500 - Loss: 0.361   lengthscale: 18.731   noise: 0.000
    Iter 181/500 - Loss: 0.361   lengthscale: 18.735   noise: 0.000
    Iter 182/500 - Loss: 0.361   lengthscale: 18.740   noise: 0.000
    Iter 183/500 - Loss: 0.361   lengthscale: 18.745   noise: 0.000
    Iter 184/500 - Loss: 0.361   lengthscale: 18.750   noise: 0.000
    Iter 185/500 - Loss: 0.361   lengthscale: 18.755   noise: 0.000
    Iter 186/500 - Loss: 0.361   lengthscale: 18.760   noise: 0.000
    Iter 187/500 - Loss: 0.361   lengthscale: 18.765   noise: 0.000
    Iter 188/500 - Loss: 0.361   lengthscale: 18.771   noise: 0.000
    Iter 189/500 - Loss: 0.361   lengthscale: 18.776   noise: 0.000
    Iter 190/500 - Loss: 0.361   lengthscale: 18.782   noise: 0.000
    Iter 191/500 - Loss: 0.361   lengthscale: 18.788   noise: 0.000
    Iter 192/500 - Loss: 0.361   lengthscale: 18.794   noise: 0.000
    Iter 193/500 - Loss: 0.361   lengthscale: 18.800   noise: 0.000
    Iter 194/500 - Loss: 0.361   lengthscale: 18.806   noise: 0.000
    Iter 195/500 - Loss: 0.361   lengthscale: 18.813   noise: 0.000
    Iter 196/500 - Loss: 0.361   lengthscale: 18.820   noise: 0.000
    Iter 197/500 - Loss: 0.361   lengthscale: 18.827   noise: 0.000
    Iter 198/500 - Loss: 0.361   lengthscale: 18.834   noise: 0.000
    Iter 199/500 - Loss: 0.361   lengthscale: 18.841   noise: 0.000
    Iter 200/500 - Loss: 0.361   lengthscale: 18.848   noise: 0.000
    Iter 201/500 - Loss: 0.361   lengthscale: 18.856   noise: 0.000
    Iter 202/500 - Loss: 0.361   lengthscale: 18.863   noise: 0.000
    Iter 203/500 - Loss: 0.361   lengthscale: 18.871   noise: 0.000
    Iter 204/500 - Loss: 0.361   lengthscale: 18.880   noise: 0.000
    Iter 205/500 - Loss: 0.361   lengthscale: 18.888   noise: 0.000
    Iter 206/500 - Loss: 0.361   lengthscale: 18.897   noise: 0.001
    Iter 207/500 - Loss: 0.361   lengthscale: 18.905   noise: 0.001
    Iter 208/500 - Loss: 0.361   lengthscale: 18.914   noise: 0.001
    Iter 209/500 - Loss: 0.361   lengthscale: 18.924   noise: 0.001
    Iter 210/500 - Loss: 0.361   lengthscale: 18.933   noise: 0.001
    Iter 211/500 - Loss: 0.361   lengthscale: 18.943   noise: 0.001
    Iter 212/500 - Loss: 0.361   lengthscale: 18.953   noise: 0.001
    Iter 213/500 - Loss: 0.361   lengthscale: 18.963   noise: 0.001
    Iter 214/500 - Loss: 0.361   lengthscale: 18.974   noise: 0.001
    Iter 215/500 - Loss: 0.361   lengthscale: 18.985   noise: 0.001
    Iter 216/500 - Loss: 0.361   lengthscale: 18.996   noise: 0.001
    Iter 217/500 - Loss: 0.361   lengthscale: 19.007   noise: 0.001
    Iter 218/500 - Loss: 0.361   lengthscale: 19.019   noise: 0.001
    Iter 219/500 - Loss: 0.361   lengthscale: 19.030   noise: 0.001
    Iter 220/500 - Loss: 0.361   lengthscale: 19.043   noise: 0.001
    Iter 221/500 - Loss: 0.360   lengthscale: 19.055   noise: 0.001
    Iter 222/500 - Loss: 0.360   lengthscale: 19.068   noise: 0.001
    Iter 223/500 - Loss: 0.360   lengthscale: 19.081   noise: 0.001
    Iter 224/500 - Loss: 0.360   lengthscale: 19.095   noise: 0.001
    Iter 225/500 - Loss: 0.360   lengthscale: 19.109   noise: 0.001
    Iter 226/500 - Loss: 0.360   lengthscale: 19.123   noise: 0.001
    Iter 227/500 - Loss: 0.360   lengthscale: 19.138   noise: 0.001
    Iter 228/500 - Loss: 0.360   lengthscale: 19.153   noise: 0.001
    Iter 229/500 - Loss: 0.360   lengthscale: 19.169   noise: 0.001
    Iter 230/500 - Loss: 0.360   lengthscale: 19.185   noise: 0.001
    Iter 231/500 - Loss: 0.360   lengthscale: 19.202   noise: 0.001
    Iter 232/500 - Loss: 0.360   lengthscale: 19.219   noise: 0.001
    Iter 233/500 - Loss: 0.360   lengthscale: 19.236   noise: 0.001
    Iter 234/500 - Loss: 0.360   lengthscale: 19.254   noise: 0.001
    Iter 235/500 - Loss: 0.360   lengthscale: 19.273   noise: 0.001
    Iter 236/500 - Loss: 0.360   lengthscale: 19.292   noise: 0.001
    Iter 237/500 - Loss: 0.360   lengthscale: 19.311   noise: 0.001
    Iter 238/500 - Loss: 0.359   lengthscale: 19.332   noise: 0.001
    Iter 239/500 - Loss: 0.359   lengthscale: 19.353   noise: 0.001
    Iter 240/500 - Loss: 0.359   lengthscale: 19.374   noise: 0.001
    Iter 241/500 - Loss: 0.359   lengthscale: 19.396   noise: 0.001
    Iter 242/500 - Loss: 0.359   lengthscale: 19.419   noise: 0.001
    Iter 243/500 - Loss: 0.359   lengthscale: 19.443   noise: 0.001
    Iter 244/500 - Loss: 0.359   lengthscale: 19.467   noise: 0.001
    Iter 245/500 - Loss: 0.359   lengthscale: 19.492   noise: 0.001
    Iter 246/500 - Loss: 0.359   lengthscale: 19.518   noise: 0.001
    Iter 247/500 - Loss: 0.358   lengthscale: 19.545   noise: 0.001
    Iter 248/500 - Loss: 0.358   lengthscale: 19.572   noise: 0.001
    Iter 249/500 - Loss: 0.358   lengthscale: 19.600   noise: 0.001
    Iter 250/500 - Loss: 0.358   lengthscale: 19.630   noise: 0.001
    Iter 251/500 - Loss: 0.358   lengthscale: 19.660   noise: 0.001
    Iter 252/500 - Loss: 0.358   lengthscale: 19.690   noise: 0.001
    Iter 253/500 - Loss: 0.358   lengthscale: 19.722   noise: 0.001
    Iter 254/500 - Loss: 0.357   lengthscale: 19.755   noise: 0.001
    Iter 255/500 - Loss: 0.357   lengthscale: 19.789   noise: 0.001
    Iter 256/500 - Loss: 0.357   lengthscale: 19.823   noise: 0.001
    Iter 257/500 - Loss: 0.357   lengthscale: 19.859   noise: 0.001
    Iter 258/500 - Loss: 0.357   lengthscale: 19.896   noise: 0.001
    Iter 259/500 - Loss: 0.357   lengthscale: 19.933   noise: 0.001
    Iter 260/500 - Loss: 0.356   lengthscale: 19.972   noise: 0.001
    Iter 261/500 - Loss: 0.356   lengthscale: 20.012   noise: 0.001
    Iter 262/500 - Loss: 0.356   lengthscale: 20.052   noise: 0.001
    Iter 263/500 - Loss: 0.356   lengthscale: 20.094   noise: 0.002
    Iter 264/500 - Loss: 0.356   lengthscale: 20.136   noise: 0.002
    Iter 265/500 - Loss: 0.355   lengthscale: 20.179   noise: 0.002
    Iter 266/500 - Loss: 0.355   lengthscale: 20.224   noise: 0.002
    Iter 267/500 - Loss: 0.355   lengthscale: 20.269   noise: 0.002
    Iter 268/500 - Loss: 0.355   lengthscale: 20.315   noise: 0.002
    Iter 269/500 - Loss: 0.355   lengthscale: 20.362   noise: 0.002
    Iter 270/500 - Loss: 0.354   lengthscale: 20.409   noise: 0.002
    Iter 271/500 - Loss: 0.354   lengthscale: 20.458   noise: 0.002
    Iter 272/500 - Loss: 0.354   lengthscale: 20.507   noise: 0.002
    Iter 273/500 - Loss: 0.354   lengthscale: 20.557   noise: 0.002
    Iter 274/500 - Loss: 0.354   lengthscale: 20.607   noise: 0.002
    Iter 275/500 - Loss: 0.353   lengthscale: 20.658   noise: 0.002
    Iter 276/500 - Loss: 0.353   lengthscale: 20.709   noise: 0.002
    Iter 277/500 - Loss: 0.353   lengthscale: 20.761   noise: 0.002
    Iter 278/500 - Loss: 0.353   lengthscale: 20.813   noise: 0.002
    Iter 279/500 - Loss: 0.353   lengthscale: 20.866   noise: 0.002
    Iter 280/500 - Loss: 0.352   lengthscale: 20.919   noise: 0.002
    Iter 281/500 - Loss: 0.352   lengthscale: 20.972   noise: 0.002
    Iter 282/500 - Loss: 0.352   lengthscale: 21.025   noise: 0.002
    Iter 283/500 - Loss: 0.352   lengthscale: 21.078   noise: 0.002
    Iter 284/500 - Loss: 0.352   lengthscale: 21.132   noise: 0.002
    Iter 285/500 - Loss: 0.352   lengthscale: 21.185   noise: 0.002
    Iter 286/500 - Loss: 0.351   lengthscale: 21.239   noise: 0.003
    Iter 287/500 - Loss: 0.351   lengthscale: 21.292   noise: 0.003
    Iter 288/500 - Loss: 0.351   lengthscale: 21.345   noise: 0.003
    Iter 289/500 - Loss: 0.351   lengthscale: 21.398   noise: 0.003
    Iter 290/500 - Loss: 0.351   lengthscale: 21.451   noise: 0.003
    Iter 291/500 - Loss: 0.350   lengthscale: 21.504   noise: 0.003
    Iter 292/500 - Loss: 0.350   lengthscale: 21.556   noise: 0.003
    Iter 293/500 - Loss: 0.350   lengthscale: 21.608   noise: 0.003
    Iter 294/500 - Loss: 0.350   lengthscale: 21.661   noise: 0.003
    Iter 295/500 - Loss: 0.350   lengthscale: 21.712   noise: 0.003
    Iter 296/500 - Loss: 0.350   lengthscale: 21.764   noise: 0.003
    Iter 297/500 - Loss: 0.349   lengthscale: 21.815   noise: 0.003
    Iter 298/500 - Loss: 0.349   lengthscale: 21.867   noise: 0.003
    Iter 299/500 - Loss: 0.349   lengthscale: 21.918   noise: 0.003
    Iter 300/500 - Loss: 0.349   lengthscale: 21.968   noise: 0.003
    Iter 301/500 - Loss: 0.349   lengthscale: 22.019   noise: 0.003
    Iter 302/500 - Loss: 0.349   lengthscale: 22.070   noise: 0.003
    Iter 303/500 - Loss: 0.348   lengthscale: 22.120   noise: 0.003
    Iter 304/500 - Loss: 0.348   lengthscale: 22.170   noise: 0.003
    Iter 305/500 - Loss: 0.348   lengthscale: 22.220   noise: 0.003
    Iter 306/500 - Loss: 0.348   lengthscale: 22.270   noise: 0.003
    Iter 307/500 - Loss: 0.348   lengthscale: 22.320   noise: 0.003
    Iter 308/500 - Loss: 0.348   lengthscale: 22.370   noise: 0.003
    Iter 309/500 - Loss: 0.347   lengthscale: 22.419   noise: 0.004
    Iter 310/500 - Loss: 0.347   lengthscale: 22.469   noise: 0.004
    Iter 311/500 - Loss: 0.347   lengthscale: 22.519   noise: 0.004
    Iter 312/500 - Loss: 0.347   lengthscale: 22.568   noise: 0.004
    Iter 313/500 - Loss: 0.347   lengthscale: 22.618   noise: 0.004
    Iter 314/500 - Loss: 0.347   lengthscale: 22.668   noise: 0.004
    Iter 315/500 - Loss: 0.347   lengthscale: 22.717   noise: 0.004
    Iter 316/500 - Loss: 0.346   lengthscale: 22.767   noise: 0.004
    Iter 317/500 - Loss: 0.346   lengthscale: 22.817   noise: 0.004
    Iter 318/500 - Loss: 0.346   lengthscale: 22.867   noise: 0.004
    Iter 319/500 - Loss: 0.346   lengthscale: 22.916   noise: 0.004
    Iter 320/500 - Loss: 0.346   lengthscale: 22.966   noise: 0.004
    Iter 321/500 - Loss: 0.346   lengthscale: 23.016   noise: 0.004
    Iter 322/500 - Loss: 0.346   lengthscale: 23.066   noise: 0.004
    Iter 323/500 - Loss: 0.345   lengthscale: 23.116   noise: 0.004
    Iter 324/500 - Loss: 0.345   lengthscale: 23.166   noise: 0.004
    Iter 325/500 - Loss: 0.345   lengthscale: 23.217   noise: 0.004
    Iter 326/500 - Loss: 0.345   lengthscale: 23.267   noise: 0.004
    Iter 327/500 - Loss: 0.345   lengthscale: 23.318   noise: 0.004
    Iter 328/500 - Loss: 0.345   lengthscale: 23.369   noise: 0.004
    Iter 329/500 - Loss: 0.344   lengthscale: 23.420   noise: 0.005
    Iter 330/500 - Loss: 0.344   lengthscale: 23.471   noise: 0.005
    Iter 331/500 - Loss: 0.344   lengthscale: 23.523   noise: 0.005
    Iter 332/500 - Loss: 0.344   lengthscale: 23.574   noise: 0.005
    Iter 333/500 - Loss: 0.344   lengthscale: 23.626   noise: 0.005
    Iter 334/500 - Loss: 0.344   lengthscale: 23.679   noise: 0.005
    Iter 335/500 - Loss: 0.343   lengthscale: 23.731   noise: 0.005
    Iter 336/500 - Loss: 0.343   lengthscale: 23.784   noise: 0.005
    Iter 337/500 - Loss: 0.343   lengthscale: 23.837   noise: 0.005
    Iter 338/500 - Loss: 0.343   lengthscale: 23.891   noise: 0.005
    Iter 339/500 - Loss: 0.343   lengthscale: 23.945   noise: 0.005
    Iter 340/500 - Loss: 0.343   lengthscale: 23.999   noise: 0.005
    Iter 341/500 - Loss: 0.342   lengthscale: 24.053   noise: 0.005
    Iter 342/500 - Loss: 0.342   lengthscale: 24.108   noise: 0.005
    Iter 343/500 - Loss: 0.342   lengthscale: 24.164   noise: 0.005
    Iter 344/500 - Loss: 0.342   lengthscale: 24.219   noise: 0.005
    Iter 345/500 - Loss: 0.342   lengthscale: 24.276   noise: 0.005
    Iter 346/500 - Loss: 0.341   lengthscale: 24.332   noise: 0.005
    Iter 347/500 - Loss: 0.341   lengthscale: 24.389   noise: 0.006
    Iter 348/500 - Loss: 0.341   lengthscale: 24.447   noise: 0.006
    Iter 349/500 - Loss: 0.341   lengthscale: 24.505   noise: 0.006
    Iter 350/500 - Loss: 0.341   lengthscale: 24.563   noise: 0.006
    Iter 351/500 - Loss: 0.340   lengthscale: 24.622   noise: 0.006
    Iter 352/500 - Loss: 0.340   lengthscale: 24.681   noise: 0.006
    Iter 353/500 - Loss: 0.340   lengthscale: 24.741   noise: 0.006
    Iter 354/500 - Loss: 0.340   lengthscale: 24.801   noise: 0.006
    Iter 355/500 - Loss: 0.340   lengthscale: 24.862   noise: 0.006
    Iter 356/500 - Loss: 0.339   lengthscale: 24.924   noise: 0.006
    Iter 357/500 - Loss: 0.339   lengthscale: 24.985   noise: 0.006
    Iter 358/500 - Loss: 0.339   lengthscale: 25.048   noise: 0.006
    Iter 359/500 - Loss: 0.339   lengthscale: 25.111   noise: 0.006
    Iter 360/500 - Loss: 0.338   lengthscale: 25.174   noise: 0.006
    Iter 361/500 - Loss: 0.338   lengthscale: 25.239   noise: 0.006
    Iter 362/500 - Loss: 0.338   lengthscale: 25.303   noise: 0.006
    Iter 363/500 - Loss: 0.338   lengthscale: 25.369   noise: 0.007
    Iter 364/500 - Loss: 0.337   lengthscale: 25.435   noise: 0.007
    Iter 365/500 - Loss: 0.337   lengthscale: 25.501   noise: 0.007
    Iter 366/500 - Loss: 0.337   lengthscale: 25.568   noise: 0.007
    Iter 367/500 - Loss: 0.337   lengthscale: 25.636   noise: 0.007
    Iter 368/500 - Loss: 0.336   lengthscale: 25.704   noise: 0.007
    Iter 369/500 - Loss: 0.336   lengthscale: 25.773   noise: 0.007
    Iter 370/500 - Loss: 0.336   lengthscale: 25.842   noise: 0.007
    Iter 371/500 - Loss: 0.335   lengthscale: 25.913   noise: 0.007
    Iter 372/500 - Loss: 0.335   lengthscale: 25.983   noise: 0.007
    Iter 373/500 - Loss: 0.335   lengthscale: 26.055   noise: 0.007
    Iter 374/500 - Loss: 0.335   lengthscale: 26.127   noise: 0.007
    Iter 375/500 - Loss: 0.334   lengthscale: 26.199   noise: 0.007
    Iter 376/500 - Loss: 0.334   lengthscale: 26.273   noise: 0.007
    Iter 377/500 - Loss: 0.334   lengthscale: 26.347   noise: 0.007
    Iter 378/500 - Loss: 0.333   lengthscale: 26.421   noise: 0.008
    Iter 379/500 - Loss: 0.333   lengthscale: 26.496   noise: 0.008
    Iter 380/500 - Loss: 0.333   lengthscale: 26.572   noise: 0.008
    Iter 381/500 - Loss: 0.332   lengthscale: 26.649   noise: 0.008
    Iter 382/500 - Loss: 0.332   lengthscale: 26.726   noise: 0.008
    Iter 383/500 - Loss: 0.332   lengthscale: 26.804   noise: 0.008
    Iter 384/500 - Loss: 0.331   lengthscale: 26.882   noise: 0.008
    Iter 385/500 - Loss: 0.331   lengthscale: 26.961   noise: 0.008
    Iter 386/500 - Loss: 0.331   lengthscale: 27.041   noise: 0.008
    Iter 387/500 - Loss: 0.330   lengthscale: 27.121   noise: 0.008
    Iter 388/500 - Loss: 0.330   lengthscale: 27.202   noise: 0.008
    Iter 389/500 - Loss: 0.330   lengthscale: 27.284   noise: 0.008
    Iter 390/500 - Loss: 0.329   lengthscale: 27.366   noise: 0.008
    Iter 391/500 - Loss: 0.329   lengthscale: 27.449   noise: 0.008
    Iter 392/500 - Loss: 0.328   lengthscale: 27.532   noise: 0.008
    Iter 393/500 - Loss: 0.328   lengthscale: 27.616   noise: 0.009
    Iter 394/500 - Loss: 0.328   lengthscale: 27.701   noise: 0.009
    Iter 395/500 - Loss: 0.327   lengthscale: 27.786   noise: 0.009
    Iter 396/500 - Loss: 0.327   lengthscale: 27.872   noise: 0.009
    Iter 397/500 - Loss: 0.326   lengthscale: 27.959   noise: 0.009
    Iter 398/500 - Loss: 0.326   lengthscale: 28.046   noise: 0.009
    Iter 399/500 - Loss: 0.326   lengthscale: 28.133   noise: 0.009
    Iter 400/500 - Loss: 0.325   lengthscale: 28.222   noise: 0.009
    Iter 401/500 - Loss: 0.325   lengthscale: 28.311   noise: 0.009
    Iter 402/500 - Loss: 0.324   lengthscale: 28.400   noise: 0.009
    Iter 403/500 - Loss: 0.324   lengthscale: 28.490   noise: 0.009
    Iter 404/500 - Loss: 0.323   lengthscale: 28.580   noise: 0.009
    Iter 405/500 - Loss: 0.323   lengthscale: 28.672   noise: 0.009
    Iter 406/500 - Loss: 0.323   lengthscale: 28.763   noise: 0.009
    Iter 407/500 - Loss: 0.322   lengthscale: 28.855   noise: 0.009
    Iter 408/500 - Loss: 0.322   lengthscale: 28.948   noise: 0.010
    Iter 409/500 - Loss: 0.321   lengthscale: 29.041   noise: 0.010
    Iter 410/500 - Loss: 0.321   lengthscale: 29.135   noise: 0.010
    Iter 411/500 - Loss: 0.320   lengthscale: 29.229   noise: 0.010
    Iter 412/500 - Loss: 0.320   lengthscale: 29.324   noise: 0.010
    Iter 413/500 - Loss: 0.319   lengthscale: 29.419   noise: 0.010
    Iter 414/500 - Loss: 0.319   lengthscale: 29.515   noise: 0.010
    Iter 415/500 - Loss: 0.318   lengthscale: 29.611   noise: 0.010
    Iter 416/500 - Loss: 0.318   lengthscale: 29.708   noise: 0.010
    Iter 417/500 - Loss: 0.317   lengthscale: 29.805   noise: 0.010
    Iter 418/500 - Loss: 0.317   lengthscale: 29.903   noise: 0.010
    Iter 419/500 - Loss: 0.316   lengthscale: 30.001   noise: 0.010
    Iter 420/500 - Loss: 0.316   lengthscale: 30.099   noise: 0.010
    Iter 421/500 - Loss: 0.315   lengthscale: 30.198   noise: 0.010
    Iter 422/500 - Loss: 0.315   lengthscale: 30.297   noise: 0.010
    Iter 423/500 - Loss: 0.314   lengthscale: 30.397   noise: 0.010
    Iter 424/500 - Loss: 0.314   lengthscale: 30.497   noise: 0.010
    Iter 425/500 - Loss: 0.313   lengthscale: 30.598   noise: 0.011
    Iter 426/500 - Loss: 0.313   lengthscale: 30.699   noise: 0.011
    Iter 427/500 - Loss: 0.312   lengthscale: 30.800   noise: 0.011
    Iter 428/500 - Loss: 0.312   lengthscale: 30.901   noise: 0.011
    Iter 429/500 - Loss: 0.311   lengthscale: 31.003   noise: 0.011
    Iter 430/500 - Loss: 0.311   lengthscale: 31.106   noise: 0.011
    Iter 431/500 - Loss: 0.310   lengthscale: 31.209   noise: 0.011
    Iter 432/500 - Loss: 0.310   lengthscale: 31.312   noise: 0.011
    Iter 433/500 - Loss: 0.309   lengthscale: 31.415   noise: 0.011
    Iter 434/500 - Loss: 0.309   lengthscale: 31.519   noise: 0.011
    Iter 435/500 - Loss: 0.308   lengthscale: 31.623   noise: 0.011
    Iter 436/500 - Loss: 0.308   lengthscale: 31.727   noise: 0.011
    Iter 437/500 - Loss: 0.307   lengthscale: 31.832   noise: 0.011
    Iter 438/500 - Loss: 0.306   lengthscale: 31.936   noise: 0.011
    Iter 439/500 - Loss: 0.306   lengthscale: 32.042   noise: 0.011
    Iter 440/500 - Loss: 0.305   lengthscale: 32.147   noise: 0.011
    Iter 441/500 - Loss: 0.305   lengthscale: 32.253   noise: 0.011
    Iter 442/500 - Loss: 0.304   lengthscale: 32.359   noise: 0.011
    Iter 443/500 - Loss: 0.304   lengthscale: 32.465   noise: 0.011
    Iter 444/500 - Loss: 0.303   lengthscale: 32.571   noise: 0.012
    Iter 445/500 - Loss: 0.303   lengthscale: 32.678   noise: 0.012
    Iter 446/500 - Loss: 0.302   lengthscale: 32.785   noise: 0.012
    Iter 447/500 - Loss: 0.301   lengthscale: 32.892   noise: 0.012
    Iter 448/500 - Loss: 0.301   lengthscale: 32.999   noise: 0.012
    Iter 449/500 - Loss: 0.300   lengthscale: 33.106   noise: 0.012
    Iter 450/500 - Loss: 0.300   lengthscale: 33.214   noise: 0.012
    Iter 451/500 - Loss: 0.299   lengthscale: 33.322   noise: 0.012
    Iter 452/500 - Loss: 0.299   lengthscale: 33.430   noise: 0.012
    Iter 453/500 - Loss: 0.298   lengthscale: 33.538   noise: 0.012
    Iter 454/500 - Loss: 0.298   lengthscale: 33.646   noise: 0.012
    Iter 455/500 - Loss: 0.297   lengthscale: 33.755   noise: 0.012
    Iter 456/500 - Loss: 0.296   lengthscale: 33.863   noise: 0.012
    Iter 457/500 - Loss: 0.296   lengthscale: 33.972   noise: 0.012
    Iter 458/500 - Loss: 0.295   lengthscale: 34.081   noise: 0.012
    Iter 459/500 - Loss: 0.295   lengthscale: 34.190   noise: 0.012
    Iter 460/500 - Loss: 0.294   lengthscale: 34.299   noise: 0.012
    Iter 461/500 - Loss: 0.294   lengthscale: 34.408   noise: 0.012
    Iter 462/500 - Loss: 0.293   lengthscale: 34.517   noise: 0.012
    Iter 463/500 - Loss: 0.292   lengthscale: 34.626   noise: 0.012
    Iter 464/500 - Loss: 0.292   lengthscale: 34.736   noise: 0.012
    Iter 465/500 - Loss: 0.291   lengthscale: 34.845   noise: 0.012
    Iter 466/500 - Loss: 0.291   lengthscale: 34.955   noise: 0.012
    Iter 467/500 - Loss: 0.290   lengthscale: 35.064   noise: 0.012
    Iter 468/500 - Loss: 0.290   lengthscale: 35.174   noise: 0.012
    Iter 469/500 - Loss: 0.289   lengthscale: 35.284   noise: 0.013
    Iter 470/500 - Loss: 0.289   lengthscale: 35.394   noise: 0.013
    Iter 471/500 - Loss: 0.288   lengthscale: 35.503   noise: 0.013
    Iter 472/500 - Loss: 0.287   lengthscale: 35.613   noise: 0.013
    Iter 473/500 - Loss: 0.287   lengthscale: 35.723   noise: 0.013
    Iter 474/500 - Loss: 0.286   lengthscale: 35.833   noise: 0.013
    Iter 475/500 - Loss: 0.286   lengthscale: 35.943   noise: 0.013
    Iter 476/500 - Loss: 0.285   lengthscale: 36.052   noise: 0.013
    Iter 477/500 - Loss: 0.285   lengthscale: 36.162   noise: 0.013
    Iter 478/500 - Loss: 0.284   lengthscale: 36.272   noise: 0.013
    Iter 479/500 - Loss: 0.284   lengthscale: 36.382   noise: 0.013
    Iter 480/500 - Loss: 0.283   lengthscale: 36.491   noise: 0.013
    Iter 481/500 - Loss: 0.282   lengthscale: 36.601   noise: 0.013
    Iter 482/500 - Loss: 0.282   lengthscale: 36.711   noise: 0.013
    Iter 483/500 - Loss: 0.281   lengthscale: 36.821   noise: 0.013
    Iter 484/500 - Loss: 0.281   lengthscale: 36.930   noise: 0.013
    Iter 485/500 - Loss: 0.280   lengthscale: 37.040   noise: 0.013
    Iter 486/500 - Loss: 0.280   lengthscale: 37.149   noise: 0.013
    Iter 487/500 - Loss: 0.279   lengthscale: 37.259   noise: 0.013
    Iter 488/500 - Loss: 0.279   lengthscale: 37.368   noise: 0.013
    Iter 489/500 - Loss: 0.278   lengthscale: 37.477   noise: 0.013
    Iter 490/500 - Loss: 0.278   lengthscale: 37.586   noise: 0.013
    Iter 491/500 - Loss: 0.277   lengthscale: 37.695   noise: 0.013
    Iter 492/500 - Loss: 0.276   lengthscale: 37.804   noise: 0.013
    Iter 493/500 - Loss: 0.276   lengthscale: 37.913   noise: 0.013
    Iter 494/500 - Loss: 0.275   lengthscale: 38.022   noise: 0.013
    Iter 495/500 - Loss: 0.275   lengthscale: 38.131   noise: 0.013
    Iter 496/500 - Loss: 0.274   lengthscale: 38.239   noise: 0.013
    Iter 497/500 - Loss: 0.274   lengthscale: 38.348   noise: 0.013
    Iter 498/500 - Loss: 0.273   lengthscale: 38.456   noise: 0.013
    Iter 499/500 - Loss: 0.273   lengthscale: 38.564   noise: 0.013
    Iter 500/500 - Loss: 0.272   lengthscale: 38.672   noise: 0.013

Now, we test predictions. For simplicity, we will assume a fixed variance of $0.01.$

```python
# Get into evaluation (predictive posterior) mode
model.eval()
likelihood.eval()

# Test points are regularly spaced along [0,1]
# Make predictions by feeding model through likelihood
with torch.no_grad(), gpytorch.settings.fast_pred_var():
    test_x = torch.linspace(0, 1, 51)
    test_x_distributional = torch.stack((test_x, (1e-2 * torch.ones_like(test_x)).log()), dim=1)
    observed_pred = likelihood(model(test_x_distributional))

with torch.no_grad():
    # Initialize plot
    f, ax = plt.subplots(1, 1, figsize=(8, 3))

    # Get upper and lower confidence bounds
    lower, upper = observed_pred.confidence_region()
    # Plot training data as black stars
    ax.errorbar(train_x_mean.numpy(), train_y.numpy(), xerr=train_x_stdv, fmt='k*')
    # Plot predictive means as blue line
    ax.plot(test_x.numpy(), observed_pred.mean.numpy(), 'b')
    # Shade between the lower and upper confidence bounds
    ax.fill_between(test_x.numpy(), lower.numpy(), upper.numpy(), alpha=0.5)
    ax.set_ylim([-3, 3])
    ax.legend(['Observed Data', 'Mean', 'Confidence'])
```

As a final note, we've made it very easy to extend the distributional kernel class by exposing a generic `DistributionalInputKernel` class that takes as input any distance function over probability distributions.

```python

```