```python
import sympy
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import cm
from sympy import sin, cos, Matrix
from sympy import symbols, Matrix, Transpose, init_session, Array, tensorproduct
from sympy.physics.vector import ReferenceFrame, outer

```

```python
phi, theta, t, w, L, A , psi, sigma= symbols('ϕ θ t ω L A ψ σ')
init_session(use_unicode=True)
```

    IPython console for SymPy 1.6.2 (Python 3.8.5-64-bit) (ground types: python)

    These commands were executed:
    >>> from __future__ import division
    >>> from sympy import *
    >>> x, y, z, t = symbols('x y z t')
    >>> k, m, n = symbols('k m n', integer=True)
    >>> f, g, h = symbols('f g h', cls=Function)
    >>> init_printing()

    Documentation can be found at https://docs.sympy.org/1.6.2/

```python
#Defining h
def h(t, L, e_mais, e_cruzado,A):
	h_mais = A*cos(w*t-w*L)
	h_cruzado = A*sin(w*t-w*L)
	return h_mais*e_mais + h_cruzado*e_cruzado
```

```python
#Defining h
def h_new(t, L, e_mais, e_cruzado,A):
	h_mais = A*cos(w*t-w*L)
	h_cruzado = A*sin(w*t-w*L)
	return h_mais*e_mais + h_cruzado*e_cruzado
```

```python

```

```python
def PSI(n,h,k):
	return 0.5*(n*h*n.transpose()/(1 - (k.dot(n))**2))
```

```python
#função PSI(t)
def PSI_new(n,h,k):

	return 0.5*(n.dot(h.dot(n)))/(1 - (k.dot(n))**2)
```

```python

```

```python
# Rotação de matrizes

#rotaçao em z por um angulo phi
R_z=Matrix([[cos(phi), sin(phi), 0], [-sin(phi), cos(phi), 0], [0,0,1]])

#rotaçao em X por um angulo theta
R_X=Matrix([[1, 0, 0], [0, cos(theta), sin(theta)], [0, -sin(theta), cos(theta)]])

#rotacao em Z por psi
R_Z=Matrix([[cos(psi), sin(psi), 0], [-sin(psi), cos(psi), 0], [0,0,1]])

#Matriz de Rotação EULER:(x,y,z) ------> (X,Y,Z)
R = R_Z*R_X*R_z
```

```python
LabFrame = ReferenceFrame("Lab")
WaveFrame = ReferenceFrame("Wave")
WaveFrame.orient(LabFrame, "body", (phi, theta, psi), 'zxz')
```

```python
#tensores no referencial da onda
e_mais_onda = Matrix([[1, 0, 0], [0, -1, 0], [0, 0, 0]])
e_cruzado_onda = Matrix([[0, 1, 0], [1, 0, 0], [0, 0, 0]])
```

```python
n2_mat=Matrix([[cos(sigma),sin(sigma),0]])
n3_mat=Matrix([[cos(sigma),-sin(sigma),0]])
```

```python
e_plus  = Array([[1, 0, 0], [0, -1, 0], [0, 0, 0]])
e_cross = Array([[0, 1, 0], [1,  0, 0], [0, 0, 0]])
```

```python
vx = WaveFrame.x
vy = WaveFrame.y
vz = WaveFrame.z
dbaseii = outer(vx, vx)
dbaseij = outer(vx, vy)
dbaseik = outer(vx, vz)
dbaseji = outer(vy, vx)
dbasejj = outer(vy, vy)
dbasejk = outer(vy, vz)
dbaseki = outer(vz, vx)
dbasekj = outer(vz, vy)
dbasekk = outer(vz, vz)
```

```python
e_plus_dyadic = dbaseii - dbasejj
e_cross_dyadic = dbaseij + dbaseji
e_cross_dyadic.to_matrix(WaveFrame)
```

```python
e_plus_dyadic.to_matrix(WaveFrame)
```

```python
e_plus_dyadic.to_matrix(LabFrame)
```

$\displaystyle \left[\begin{matrix}\left(- \sin{\left(ψ \right)} \cos{\left(ϕ \right)} - \sin{\left(ϕ \right)} \cos{\left(θ \right)} \cos{\left(ψ \right)}\right) \left(\sin{\left(ψ \right)} \cos{\left(ϕ \right)} + \sin{\left(ϕ \right)} \cos{\left(θ \right)} \cos{\left(ψ \right)}\right) + \left(- \sin{\left(ψ \right)} \sin{\left(ϕ \right)} \cos{\left(θ \right)} + \cos{\left(ψ \right)} \cos{\left(ϕ \right)}\right)^{2} & \left(- \sin{\left(ψ \right)} \sin{\left(ϕ \right)} + \cos{\left(θ \right)} \cos{\left(ψ \right)} \cos{\left(ϕ \right)}\right) \left(\sin{\left(ψ \right)} \cos{\left(ϕ \right)} + \sin{\left(ϕ \right)} \cos{\left(θ \right)} \cos{\left(ψ \right)}\right) + \left(- \sin{\left(ψ \right)} \sin{\left(ϕ \right)} \cos{\left(θ \right)} + \cos{\left(ψ \right)} \cos{\left(ϕ \right)}\right) \left(\sin{\left(ψ \right)} \cos{\left(θ \right)} \cos{\left(ϕ \right)} + \sin{\left(ϕ \right)} \cos{\left(ψ \right)}\right) & \left(\sin{\left(ψ \right)} \cos{\left(ϕ \right)} + \sin{\left(ϕ \right)} \cos{\left(θ \right)} \cos{\left(ψ \right)}\right) \sin{\left(θ \right)} \cos{\left(ψ \right)} + \left(- \sin{\left(ψ \right)} \sin{\left(ϕ \right)} \cos{\left(θ \right)} + \cos{\left(ψ \right)} \cos{\left(ϕ \right)}\right) \sin{\left(θ \right)} \sin{\left(ψ \right)}\\\left(\sin{\left(ψ \right)} \sin{\left(ϕ \right)} - \cos{\left(θ \right)} \cos{\left(ψ \right)} \cos{\left(ϕ \right)}\right) \left(- \sin{\left(ψ \right)} \cos{\left(ϕ \right)} - \sin{\left(ϕ \right)} \cos{\left(θ \right)} \cos{\left(ψ \right)}\right) + \left(- \sin{\left(ψ \right)} \sin{\left(ϕ \right)} \cos{\left(θ \right)} + \cos{\left(ψ \right)} \cos{\left(ϕ \right)}\right) \left(\sin{\left(ψ \right)} \cos{\left(θ \right)} \cos{\left(ϕ \right)} + \sin{\left(ϕ \right)} \cos{\left(ψ \right)}\right) & \left(- \sin{\left(ψ \right)} \sin{\left(ϕ \right)} + \cos{\left(θ \right)} \cos{\left(ψ \right)} \cos{\left(ϕ \right)}\right) \left(\sin{\left(ψ \right)} \sin{\left(ϕ \right)} - \cos{\left(θ \right)} \cos{\left(ψ \right)} \cos{\left(ϕ \right)}\right) + \left(\sin{\left(ψ \right)} \cos{\left(θ \right)} \cos{\left(ϕ \right)} + \sin{\left(ϕ \right)} \cos{\left(ψ \right)}\right)^{2} & \left(\sin{\left(ψ \right)} \sin{\left(ϕ \right)} - \cos{\left(θ \right)} \cos{\left(ψ \right)} \cos{\left(ϕ \right)}\right) \sin{\left(θ \right)} \cos{\left(ψ \right)} + \left(\sin{\left(ψ \right)} \cos{\left(θ \right)} \cos{\left(ϕ \right)} + \sin{\left(ϕ \right)} \cos{\left(ψ \right)}\right) \sin{\left(θ \right)} \sin{\left(ψ \right)}\\- \left(- \sin{\left(ψ \right)} \cos{\left(ϕ \right)} - \sin{\left(ϕ \right)} \cos{\left(θ \right)} \cos{\left(ψ \right)}\right) \sin{\left(θ \right)} \cos{\left(ψ \right)} + \left(- \sin{\left(ψ \right)} \sin{\left(ϕ \right)} \cos{\left(θ \right)} + \cos{\left(ψ \right)} \cos{\left(ϕ \right)}\right) \sin{\left(θ \right)} \sin{\left(ψ \right)} & - \left(- \sin{\left(ψ \right)} \sin{\left(ϕ \right)} + \cos{\left(θ \right)} \cos{\left(ψ \right)} \cos{\left(ϕ \right)}\right) \sin{\left(θ \right)} \cos{\left(ψ \right)} + \left(\sin{\left(ψ \right)} \cos{\left(θ \right)} \cos{\left(ϕ \right)} + \sin{\left(ϕ \right)} \cos{\left(ψ \right)}\right) \sin{\left(θ \right)} \sin{\left(ψ \right)} & \sin^{2}{\left(θ \right)} \sin^{2}{\left(ψ \right)} - \sin^{2}{\left(θ \right)} \cos^{2}{\left(ψ \right)}\end{matrix}\right]$

```python
#n no referencial do detector
n2 = cos(sigma)*LabFrame.x + sin(sigma)*LabFrame.y
n3 = cos(sigma)*LabFrame.x - sin(sigma)*LabFrame.y

```

```python
#k no referencial da onda
k_onda=Matrix([[0, 0, 1]])
k_onda=k_onda.transpose()
```

```python
k_vec = WaveFrame.z
k_vec.express(LabFrame)
```

```python
#k no referencial do detector
k_det=R.transpose()*k_onda
k_det
```

$\displaystyle \left[\begin{matrix}\sin{\left(θ \right)} \sin{\left(ϕ \right)}\\- \sin{\left(θ \right)} \cos{\left(ϕ \right)}\\\cos{\left(θ \right)}\end{matrix}\right]$

```python
k_det == k_vec.to_matrix(LabFrame)
```

    True

```python
#escrevendo tensores e no refecial do detector
e_mais_det=R.transpose()*e_mais_onda*R
e_cruzado_det=R.transpose()*e_cruzado_onda*R
e_mais_det
```

$\displaystyle \left[\begin{matrix}\left(- \sin{\left(ψ \right)} \cos{\left(ϕ \right)} - \sin{\left(ϕ \right)} \cos{\left(θ \right)} \cos{\left(ψ \right)}\right) \left(\sin{\left(ψ \right)} \cos{\left(ϕ \right)} + \sin{\left(ϕ \right)} \cos{\left(θ \right)} \cos{\left(ψ \right)}\right) + \left(- \sin{\left(ψ \right)} \sin{\left(ϕ \right)} \cos{\left(θ \right)} + \cos{\left(ψ \right)} \cos{\left(ϕ \right)}\right)^{2} & \left(- \sin{\left(ψ \right)} \sin{\left(ϕ \right)} + \cos{\left(θ \right)} \cos{\left(ψ \right)} \cos{\left(ϕ \right)}\right) \left(\sin{\left(ψ \right)} \cos{\left(ϕ \right)} + \sin{\left(ϕ \right)} \cos{\left(θ \right)} \cos{\left(ψ \right)}\right) + \left(- \sin{\left(ψ \right)} \sin{\left(ϕ \right)} \cos{\left(θ \right)} + \cos{\left(ψ \right)} \cos{\left(ϕ \right)}\right) \left(\sin{\left(ψ \right)} \cos{\left(θ \right)} \cos{\left(ϕ \right)} + \sin{\left(ϕ \right)} \cos{\left(ψ \right)}\right) & \left(\sin{\left(ψ \right)} \cos{\left(ϕ \right)} + \sin{\left(ϕ \right)} \cos{\left(θ \right)} \cos{\left(ψ \right)}\right) \sin{\left(θ \right)} \cos{\left(ψ \right)} + \left(- \sin{\left(ψ \right)} \sin{\left(ϕ \right)} \cos{\left(θ \right)} + \cos{\left(ψ \right)} \cos{\left(ϕ \right)}\right) \sin{\left(θ \right)} \sin{\left(ψ \right)}\\\left(\sin{\left(ψ \right)} \sin{\left(ϕ \right)} - \cos{\left(θ \right)} \cos{\left(ψ \right)} \cos{\left(ϕ \right)}\right) \left(- \sin{\left(ψ \right)} \cos{\left(ϕ \right)} - \sin{\left(ϕ \right)} \cos{\left(θ \right)} \cos{\left(ψ \right)}\right) + \left(- \sin{\left(ψ \right)} \sin{\left(ϕ \right)} \cos{\left(θ \right)} + \cos{\left(ψ \right)} \cos{\left(ϕ \right)}\right) \left(\sin{\left(ψ \right)} \cos{\left(θ \right)} \cos{\left(ϕ \right)} + \sin{\left(ϕ \right)} \cos{\left(ψ \right)}\right) & \left(- \sin{\left(ψ \right)} \sin{\left(ϕ \right)} + \cos{\left(θ \right)} \cos{\left(ψ \right)} \cos{\left(ϕ \right)}\right) \left(\sin{\left(ψ \right)} \sin{\left(ϕ \right)} - \cos{\left(θ \right)} \cos{\left(ψ \right)} \cos{\left(ϕ \right)}\right) + \left(\sin{\left(ψ \right)} \cos{\left(θ \right)} \cos{\left(ϕ \right)} + \sin{\left(ϕ \right)} \cos{\left(ψ \right)}\right)^{2} & \left(\sin{\left(ψ \right)} \sin{\left(ϕ \right)} - \cos{\left(θ \right)} \cos{\left(ψ \right)} \cos{\left(ϕ \right)}\right) \sin{\left(θ \right)} \cos{\left(ψ \right)} + \left(\sin{\left(ψ \right)} \cos{\left(θ \right)} \cos{\left(ϕ \right)} + \sin{\left(ϕ \right)} \cos{\left(ψ \right)}\right) \sin{\left(θ \right)} \sin{\left(ψ \right)}\\- \left(- \sin{\left(ψ \right)} \cos{\left(ϕ \right)} - \sin{\left(ϕ \right)} \cos{\left(θ \right)} \cos{\left(ψ \right)}\right) \sin{\left(θ \right)} \cos{\left(ψ \right)} + \left(- \sin{\left(ψ \right)} \sin{\left(ϕ \right)} \cos{\left(θ \right)} + \cos{\left(ψ \right)} \cos{\left(ϕ \right)}\right) \sin{\left(θ \right)} \sin{\left(ψ \right)} & - \left(- \sin{\left(ψ \right)} \sin{\left(ϕ \right)} + \cos{\left(θ \right)} \cos{\left(ψ \right)} \cos{\left(ϕ \right)}\right) \sin{\left(θ \right)} \cos{\left(ψ \right)} + \left(\sin{\left(ψ \right)} \cos{\left(θ \right)} \cos{\left(ϕ \right)} + \sin{\left(ϕ \right)} \cos{\left(ψ \right)}\right) \sin{\left(θ \right)} \sin{\left(ψ \right)} & \sin^{2}{\left(θ \right)} \sin^{2}{\left(ψ \right)} - \sin^{2}{\left(θ \right)} \cos^{2}{\left(ψ \right)}\end{matrix}\right]$

```python
e_plus_dyadic.to_matrix(WaveFrame) == e_mais_onda
```

    True

```python
e_cross_dyadic.to_matrix(WaveFrame) == e_cruzado_onda
```

    True

```python
e_plus_dyadic.to_matrix(LabFrame) == e_mais_det
```

    True

```python
e_cross_dyadic.to_matrix(LabFrame) == e_cruzado_det
```

    True

```python
h1 = h_new(t, 2*L, e_plus_dyadic, e_cross_dyadic, A)
h1.to_matrix(WaveFrame)
```

$\displaystyle \left[\begin{matrix}A \cos{\left(2 L ω - t ω \right)} & - A \sin{\left(2 L ω - t ω \right)} & 0\\- A \sin{\left(2 L ω - t ω \right)} & - A \cos{\left(2 L ω - t ω \right)} & 0\\0 & 0 & 0\end{matrix}\right]$

```python
h1.to_matrix(LabFrame)
```

$\displaystyle \left[\begin{matrix}\left(- \sin{\left(ψ \right)} \cos{\left(ϕ \right)} - \sin{\left(ϕ \right)} \cos{\left(θ \right)} \cos{\left(ψ \right)}\right) \left(- A \left(- \sin{\left(ψ \right)} \cos{\left(ϕ \right)} - \sin{\left(ϕ \right)} \cos{\left(θ \right)} \cos{\left(ψ \right)}\right) \cos{\left(2 L ω - t ω \right)} - A \left(- \sin{\left(ψ \right)} \sin{\left(ϕ \right)} \cos{\left(θ \right)} + \cos{\left(ψ \right)} \cos{\left(ϕ \right)}\right) \sin{\left(2 L ω - t ω \right)}\right) + \left(- A \left(- \sin{\left(ψ \right)} \cos{\left(ϕ \right)} - \sin{\left(ϕ \right)} \cos{\left(θ \right)} \cos{\left(ψ \right)}\right) \sin{\left(2 L ω - t ω \right)} + A \left(- \sin{\left(ψ \right)} \sin{\left(ϕ \right)} \cos{\left(θ \right)} + \cos{\left(ψ \right)} \cos{\left(ϕ \right)}\right) \cos{\left(2 L ω - t ω \right)}\right) \left(- \sin{\left(ψ \right)} \sin{\left(ϕ \right)} \cos{\left(θ \right)} + \cos{\left(ψ \right)} \cos{\left(ϕ \right)}\right) & \left(- \sin{\left(ψ \right)} \sin{\left(ϕ \right)} + \cos{\left(θ \right)} \cos{\left(ψ \right)} \cos{\left(ϕ \right)}\right) \left(- A \left(- \sin{\left(ψ \right)} \cos{\left(ϕ \right)} - \sin{\left(ϕ \right)} \cos{\left(θ \right)} \cos{\left(ψ \right)}\right) \cos{\left(2 L ω - t ω \right)} - A \left(- \sin{\left(ψ \right)} \sin{\left(ϕ \right)} \cos{\left(θ \right)} + \cos{\left(ψ \right)} \cos{\left(ϕ \right)}\right) \sin{\left(2 L ω - t ω \right)}\right) + \left(- A \left(- \sin{\left(ψ \right)} \cos{\left(ϕ \right)} - \sin{\left(ϕ \right)} \cos{\left(θ \right)} \cos{\left(ψ \right)}\right) \sin{\left(2 L ω - t ω \right)} + A \left(- \sin{\left(ψ \right)} \sin{\left(ϕ \right)} \cos{\left(θ \right)} + \cos{\left(ψ \right)} \cos{\left(ϕ \right)}\right) \cos{\left(2 L ω - t ω \right)}\right) \left(\sin{\left(ψ \right)} \cos{\left(θ \right)} \cos{\left(ϕ \right)} + \sin{\left(ϕ \right)} \cos{\left(ψ \right)}\right) & \left(- A \left(- \sin{\left(ψ \right)} \cos{\left(ϕ \right)} - \sin{\left(ϕ \right)} \cos{\left(θ \right)} \cos{\left(ψ \right)}\right) \sin{\left(2 L ω - t ω \right)} + A \left(- \sin{\left(ψ \right)} \sin{\left(ϕ \right)} \cos{\left(θ \right)} + \cos{\left(ψ \right)} \cos{\left(ϕ \right)}\right) \cos{\left(2 L ω - t ω \right)}\right) \sin{\left(θ \right)} \sin{\left(ψ \right)} + \left(- A \left(- \sin{\left(ψ \right)} \cos{\left(ϕ \right)} - \sin{\left(ϕ \right)} \cos{\left(θ \right)} \cos{\left(ψ \right)}\right) \cos{\left(2 L ω - t ω \right)} - A \left(- \sin{\left(ψ \right)} \sin{\left(ϕ \right)} \cos{\left(θ \right)} + \cos{\left(ψ \right)} \cos{\left(ϕ \right)}\right) \sin{\left(2 L ω - t ω \right)}\right) \sin{\left(θ \right)} \cos{\left(ψ \right)}\\\left(- \sin{\left(ψ \right)} \cos{\left(ϕ \right)} - \sin{\left(ϕ \right)} \cos{\left(θ \right)} \cos{\left(ψ \right)}\right) \left(- A \left(- \sin{\left(ψ \right)} \sin{\left(ϕ \right)} + \cos{\left(θ \right)} \cos{\left(ψ \right)} \cos{\left(ϕ \right)}\right) \cos{\left(2 L ω - t ω \right)} - A \left(\sin{\left(ψ \right)} \cos{\left(θ \right)} \cos{\left(ϕ \right)} + \sin{\left(ϕ \right)} \cos{\left(ψ \right)}\right) \sin{\left(2 L ω - t ω \right)}\right) + \left(- A \left(- \sin{\left(ψ \right)} \sin{\left(ϕ \right)} + \cos{\left(θ \right)} \cos{\left(ψ \right)} \cos{\left(ϕ \right)}\right) \sin{\left(2 L ω - t ω \right)} + A \left(\sin{\left(ψ \right)} \cos{\left(θ \right)} \cos{\left(ϕ \right)} + \sin{\left(ϕ \right)} \cos{\left(ψ \right)}\right) \cos{\left(2 L ω - t ω \right)}\right) \left(- \sin{\left(ψ \right)} \sin{\left(ϕ \right)} \cos{\left(θ \right)} + \cos{\left(ψ \right)} \cos{\left(ϕ \right)}\right) & \left(- \sin{\left(ψ \right)} \sin{\left(ϕ \right)} + \cos{\left(θ \right)} \cos{\left(ψ \right)} \cos{\left(ϕ \right)}\right) \left(- A \left(- \sin{\left(ψ \right)} \sin{\left(ϕ \right)} + \cos{\left(θ \right)} \cos{\left(ψ \right)} \cos{\left(ϕ \right)}\right) \cos{\left(2 L ω - t ω \right)} - A \left(\sin{\left(ψ \right)} \cos{\left(θ \right)} \cos{\left(ϕ \right)} + \sin{\left(ϕ \right)} \cos{\left(ψ \right)}\right) \sin{\left(2 L ω - t ω \right)}\right) + \left(- A \left(- \sin{\left(ψ \right)} \sin{\left(ϕ \right)} + \cos{\left(θ \right)} \cos{\left(ψ \right)} \cos{\left(ϕ \right)}\right) \sin{\left(2 L ω - t ω \right)} + A \left(\sin{\left(ψ \right)} \cos{\left(θ \right)} \cos{\left(ϕ \right)} + \sin{\left(ϕ \right)} \cos{\left(ψ \right)}\right) \cos{\left(2 L ω - t ω \right)}\right) \left(\sin{\left(ψ \right)} \cos{\left(θ \right)} \cos{\left(ϕ \right)} + \sin{\left(ϕ \right)} \cos{\left(ψ \right)}\right) & \left(- A \left(- \sin{\left(ψ \right)} \sin{\left(ϕ \right)} + \cos{\left(θ \right)} \cos{\left(ψ \right)} \cos{\left(ϕ \right)}\right) \sin{\left(2 L ω - t ω \right)} + A \left(\sin{\left(ψ \right)} \cos{\left(θ \right)} \cos{\left(ϕ \right)} + \sin{\left(ϕ \right)} \cos{\left(ψ \right)}\right) \cos{\left(2 L ω - t ω \right)}\right) \sin{\left(θ \right)} \sin{\left(ψ \right)} + \left(- A \left(- \sin{\left(ψ \right)} \sin{\left(ϕ \right)} + \cos{\left(θ \right)} \cos{\left(ψ \right)} \cos{\left(ϕ \right)}\right) \cos{\left(2 L ω - t ω \right)} - A \left(\sin{\left(ψ \right)} \cos{\left(θ \right)} \cos{\left(ϕ \right)} + \sin{\left(ϕ \right)} \cos{\left(ψ \right)}\right) \sin{\left(2 L ω - t ω \right)}\right) \sin{\left(θ \right)} \cos{\left(ψ \right)}\\\left(- \sin{\left(ψ \right)} \cos{\left(ϕ \right)} - \sin{\left(ϕ \right)} \cos{\left(θ \right)} \cos{\left(ψ \right)}\right) \left(- A \sin{\left(θ \right)} \sin{\left(ψ \right)} \sin{\left(2 L ω - t ω \right)} - A \sin{\left(θ \right)} \cos{\left(ψ \right)} \cos{\left(2 L ω - t ω \right)}\right) + \left(- \sin{\left(ψ \right)} \sin{\left(ϕ \right)} \cos{\left(θ \right)} + \cos{\left(ψ \right)} \cos{\left(ϕ \right)}\right) \left(A \sin{\left(θ \right)} \sin{\left(ψ \right)} \cos{\left(2 L ω - t ω \right)} - A \sin{\left(θ \right)} \sin{\left(2 L ω - t ω \right)} \cos{\left(ψ \right)}\right) & \left(- \sin{\left(ψ \right)} \sin{\left(ϕ \right)} + \cos{\left(θ \right)} \cos{\left(ψ \right)} \cos{\left(ϕ \right)}\right) \left(- A \sin{\left(θ \right)} \sin{\left(ψ \right)} \sin{\left(2 L ω - t ω \right)} - A \sin{\left(θ \right)} \cos{\left(ψ \right)} \cos{\left(2 L ω - t ω \right)}\right) + \left(\sin{\left(ψ \right)} \cos{\left(θ \right)} \cos{\left(ϕ \right)} + \sin{\left(ϕ \right)} \cos{\left(ψ \right)}\right) \left(A \sin{\left(θ \right)} \sin{\left(ψ \right)} \cos{\left(2 L ω - t ω \right)} - A \sin{\left(θ \right)} \sin{\left(2 L ω - t ω \right)} \cos{\left(ψ \right)}\right) & \left(- A \sin{\left(θ \right)} \sin{\left(ψ \right)} \sin{\left(2 L ω - t ω \right)} - A \sin{\left(θ \right)} \cos{\left(ψ \right)} \cos{\left(2 L ω - t ω \right)}\right) \sin{\left(θ \right)} \cos{\left(ψ \right)} + \left(A \sin{\left(θ \right)} \sin{\left(ψ \right)} \cos{\left(2 L ω - t ω \right)} - A \sin{\left(θ \right)} \sin{\left(2 L ω - t ω \right)} \cos{\left(ψ \right)}\right) \sin{\left(θ \right)} \sin{\left(ψ \right)}\end{matrix}\right]$

```python
h2_onda = h(t, 2*L, e_mais_onda, e_cruzado_onda, A)
h2_onda
```

$\displaystyle \left[\begin{matrix}A \cos{\left(2 L ω - t ω \right)} & - A \sin{\left(2 L ω - t ω \right)} & 0\\- A \sin{\left(2 L ω - t ω \right)} & - A \cos{\left(2 L ω - t ω \right)} & 0\\0 & 0 & 0\end{matrix}\right]$

```python
h2_det = h(t, 2*L, e_mais_det, e_cruzado_det, A)
h2_det
```

$\displaystyle \left[\begin{matrix}A \left(\left(- \sin{\left(ψ \right)} \cos{\left(ϕ \right)} - \sin{\left(ϕ \right)} \cos{\left(θ \right)} \cos{\left(ψ \right)}\right) \left(\sin{\left(ψ \right)} \cos{\left(ϕ \right)} + \sin{\left(ϕ \right)} \cos{\left(θ \right)} \cos{\left(ψ \right)}\right) + \left(- \sin{\left(ψ \right)} \sin{\left(ϕ \right)} \cos{\left(θ \right)} + \cos{\left(ψ \right)} \cos{\left(ϕ \right)}\right)^{2}\right) \cos{\left(2 L ω - t ω \right)} - 2 A \left(- \sin{\left(ψ \right)} \cos{\left(ϕ \right)} - \sin{\left(ϕ \right)} \cos{\left(θ \right)} \cos{\left(ψ \right)}\right) \left(- \sin{\left(ψ \right)} \sin{\left(ϕ \right)} \cos{\left(θ \right)} + \cos{\left(ψ \right)} \cos{\left(ϕ \right)}\right) \sin{\left(2 L ω - t ω \right)} & A \left(\left(- \sin{\left(ψ \right)} \sin{\left(ϕ \right)} + \cos{\left(θ \right)} \cos{\left(ψ \right)} \cos{\left(ϕ \right)}\right) \left(\sin{\left(ψ \right)} \cos{\left(ϕ \right)} + \sin{\left(ϕ \right)} \cos{\left(θ \right)} \cos{\left(ψ \right)}\right) + \left(- \sin{\left(ψ \right)} \sin{\left(ϕ \right)} \cos{\left(θ \right)} + \cos{\left(ψ \right)} \cos{\left(ϕ \right)}\right) \left(\sin{\left(ψ \right)} \cos{\left(θ \right)} \cos{\left(ϕ \right)} + \sin{\left(ϕ \right)} \cos{\left(ψ \right)}\right)\right) \cos{\left(2 L ω - t ω \right)} - A \left(\left(- \sin{\left(ψ \right)} \sin{\left(ϕ \right)} + \cos{\left(θ \right)} \cos{\left(ψ \right)} \cos{\left(ϕ \right)}\right) \left(- \sin{\left(ψ \right)} \sin{\left(ϕ \right)} \cos{\left(θ \right)} + \cos{\left(ψ \right)} \cos{\left(ϕ \right)}\right) + \left(- \sin{\left(ψ \right)} \cos{\left(ϕ \right)} - \sin{\left(ϕ \right)} \cos{\left(θ \right)} \cos{\left(ψ \right)}\right) \left(\sin{\left(ψ \right)} \cos{\left(θ \right)} \cos{\left(ϕ \right)} + \sin{\left(ϕ \right)} \cos{\left(ψ \right)}\right)\right) \sin{\left(2 L ω - t ω \right)} & - A \left(\left(- \sin{\left(ψ \right)} \cos{\left(ϕ \right)} - \sin{\left(ϕ \right)} \cos{\left(θ \right)} \cos{\left(ψ \right)}\right) \sin{\left(θ \right)} \sin{\left(ψ \right)} + \left(- \sin{\left(ψ \right)} \sin{\left(ϕ \right)} \cos{\left(θ \right)} + \cos{\left(ψ \right)} \cos{\left(ϕ \right)}\right) \sin{\left(θ \right)} \cos{\left(ψ \right)}\right) \sin{\left(2 L ω - t ω \right)} + A \left(\left(\sin{\left(ψ \right)} \cos{\left(ϕ \right)} + \sin{\left(ϕ \right)} \cos{\left(θ \right)} \cos{\left(ψ \right)}\right) \sin{\left(θ \right)} \cos{\left(ψ \right)} + \left(- \sin{\left(ψ \right)} \sin{\left(ϕ \right)} \cos{\left(θ \right)} + \cos{\left(ψ \right)} \cos{\left(ϕ \right)}\right) \sin{\left(θ \right)} \sin{\left(ψ \right)}\right) \cos{\left(2 L ω - t ω \right)}\\- A \left(\left(- \sin{\left(ψ \right)} \sin{\left(ϕ \right)} + \cos{\left(θ \right)} \cos{\left(ψ \right)} \cos{\left(ϕ \right)}\right) \left(- \sin{\left(ψ \right)} \sin{\left(ϕ \right)} \cos{\left(θ \right)} + \cos{\left(ψ \right)} \cos{\left(ϕ \right)}\right) + \left(- \sin{\left(ψ \right)} \cos{\left(ϕ \right)} - \sin{\left(ϕ \right)} \cos{\left(θ \right)} \cos{\left(ψ \right)}\right) \left(\sin{\left(ψ \right)} \cos{\left(θ \right)} \cos{\left(ϕ \right)} + \sin{\left(ϕ \right)} \cos{\left(ψ \right)}\right)\right) \sin{\left(2 L ω - t ω \right)} + A \left(\left(\sin{\left(ψ \right)} \sin{\left(ϕ \right)} - \cos{\left(θ \right)} \cos{\left(ψ \right)} \cos{\left(ϕ \right)}\right) \left(- \sin{\left(ψ \right)} \cos{\left(ϕ \right)} - \sin{\left(ϕ \right)} \cos{\left(θ \right)} \cos{\left(ψ \right)}\right) + \left(- \sin{\left(ψ \right)} \sin{\left(ϕ \right)} \cos{\left(θ \right)} + \cos{\left(ψ \right)} \cos{\left(ϕ \right)}\right) \left(\sin{\left(ψ \right)} \cos{\left(θ \right)} \cos{\left(ϕ \right)} + \sin{\left(ϕ \right)} \cos{\left(ψ \right)}\right)\right) \cos{\left(2 L ω - t ω \right)} & A \left(\left(- \sin{\left(ψ \right)} \sin{\left(ϕ \right)} + \cos{\left(θ \right)} \cos{\left(ψ \right)} \cos{\left(ϕ \right)}\right) \left(\sin{\left(ψ \right)} \sin{\left(ϕ \right)} - \cos{\left(θ \right)} \cos{\left(ψ \right)} \cos{\left(ϕ \right)}\right) + \left(\sin{\left(ψ \right)} \cos{\left(θ \right)} \cos{\left(ϕ \right)} + \sin{\left(ϕ \right)} \cos{\left(ψ \right)}\right)^{2}\right) \cos{\left(2 L ω - t ω \right)} - 2 A \left(- \sin{\left(ψ \right)} \sin{\left(ϕ \right)} + \cos{\left(θ \right)} \cos{\left(ψ \right)} \cos{\left(ϕ \right)}\right) \left(\sin{\left(ψ \right)} \cos{\left(θ \right)} \cos{\left(ϕ \right)} + \sin{\left(ϕ \right)} \cos{\left(ψ \right)}\right) \sin{\left(2 L ω - t ω \right)} & - A \left(\left(- \sin{\left(ψ \right)} \sin{\left(ϕ \right)} + \cos{\left(θ \right)} \cos{\left(ψ \right)} \cos{\left(ϕ \right)}\right) \sin{\left(θ \right)} \sin{\left(ψ \right)} + \left(\sin{\left(ψ \right)} \cos{\left(θ \right)} \cos{\left(ϕ \right)} + \sin{\left(ϕ \right)} \cos{\left(ψ \right)}\right) \sin{\left(θ \right)} \cos{\left(ψ \right)}\right) \sin{\left(2 L ω - t ω \right)} + A \left(\left(\sin{\left(ψ \right)} \sin{\left(ϕ \right)} - \cos{\left(θ \right)} \cos{\left(ψ \right)} \cos{\left(ϕ \right)}\right) \sin{\left(θ \right)} \cos{\left(ψ \right)} + \left(\sin{\left(ψ \right)} \cos{\left(θ \right)} \cos{\left(ϕ \right)} + \sin{\left(ϕ \right)} \cos{\left(ψ \right)}\right) \sin{\left(θ \right)} \sin{\left(ψ \right)}\right) \cos{\left(2 L ω - t ω \right)}\\- A \left(\left(- \sin{\left(ψ \right)} \cos{\left(ϕ \right)} - \sin{\left(ϕ \right)} \cos{\left(θ \right)} \cos{\left(ψ \right)}\right) \sin{\left(θ \right)} \sin{\left(ψ \right)} + \left(- \sin{\left(ψ \right)} \sin{\left(ϕ \right)} \cos{\left(θ \right)} + \cos{\left(ψ \right)} \cos{\left(ϕ \right)}\right) \sin{\left(θ \right)} \cos{\left(ψ \right)}\right) \sin{\left(2 L ω - t ω \right)} + A \left(- \left(- \sin{\left(ψ \right)} \cos{\left(ϕ \right)} - \sin{\left(ϕ \right)} \cos{\left(θ \right)} \cos{\left(ψ \right)}\right) \sin{\left(θ \right)} \cos{\left(ψ \right)} + \left(- \sin{\left(ψ \right)} \sin{\left(ϕ \right)} \cos{\left(θ \right)} + \cos{\left(ψ \right)} \cos{\left(ϕ \right)}\right) \sin{\left(θ \right)} \sin{\left(ψ \right)}\right) \cos{\left(2 L ω - t ω \right)} & - A \left(\left(- \sin{\left(ψ \right)} \sin{\left(ϕ \right)} + \cos{\left(θ \right)} \cos{\left(ψ \right)} \cos{\left(ϕ \right)}\right) \sin{\left(θ \right)} \sin{\left(ψ \right)} + \left(\sin{\left(ψ \right)} \cos{\left(θ \right)} \cos{\left(ϕ \right)} + \sin{\left(ϕ \right)} \cos{\left(ψ \right)}\right) \sin{\left(θ \right)} \cos{\left(ψ \right)}\right) \sin{\left(2 L ω - t ω \right)} + A \left(- \left(- \sin{\left(ψ \right)} \sin{\left(ϕ \right)} + \cos{\left(θ \right)} \cos{\left(ψ \right)} \cos{\left(ϕ \right)}\right) \sin{\left(θ \right)} \cos{\left(ψ \right)} + \left(\sin{\left(ψ \right)} \cos{\left(θ \right)} \cos{\left(ϕ \right)} + \sin{\left(ϕ \right)} \cos{\left(ψ \right)}\right) \sin{\left(θ \right)} \sin{\left(ψ \right)}\right) \cos{\left(2 L ω - t ω \right)} & A \left(\sin^{2}{\left(θ \right)} \sin^{2}{\left(ψ \right)} - \sin^{2}{\left(θ \right)} \cos^{2}{\left(ψ \right)}\right) \cos{\left(2 L ω - t ω \right)} - 2 A \sin^{2}{\left(θ \right)} \sin{\left(ψ \right)} \sin{\left(2 L ω - t ω \right)} \cos{\left(ψ \right)}\end{matrix}\right]$

```python
h1.to_matrix(WaveFrame) == h2_onda
```

    True

```python
(h1.to_matrix(LabFrame) - h2_det).simplify()
```

```python
#def PSI(n,h,k):
	#eturn 0.5*(n*h*n.transpose()/(1 - (k.dot(n))**2))
#função PSI(t)
#ef PSI_new(n,h,k):
	#eturn 0.5*(n.dot(h.dot(n)))/(1 - (k.dot(n))**2)
```

```python
PSI1 =  PSI_new(n2,h_new(t, 2*L    , e_plus_dyadic, e_cross_dyadic,A),k_vec)

PSI1
```

```python
PSI2 =  PSI(n2_mat,h(t, 2*L, e_mais_det, e_cruzado_det,A),k_det)
PSI2 = PSI2[0]
PSI2
```

```python
(PSI1 - PSI2).simplify()
```

```python
expr1 = n2_mat*h(t, 2*L, e_mais_det, e_cruzado_det,A)
expr1
```

$\displaystyle \left[\begin{matrix}\left(- A \left(\left(\sin{\left(ψ \right)} \sin{\left(ϕ \right)} - \cos{\left(θ \right)} \cos{\left(ψ \right)} \cos{\left(ϕ \right)}\right) \left(- \sin{\left(ψ \right)} \cos{\left(ϕ \right)} - \sin{\left(ϕ \right)} \cos{\left(θ \right)} \cos{\left(ψ \right)}\right) + \left(- \sin{\left(ψ \right)} \sin{\left(ϕ \right)} \cos{\left(θ \right)} + \cos{\left(ψ \right)} \cos{\left(ϕ \right)}\right) \left(\sin{\left(ψ \right)} \cos{\left(θ \right)} \cos{\left(ϕ \right)} + \sin{\left(ϕ \right)} \cos{\left(ψ \right)}\right)\right) \sin{\left(2 L ω - t ω \right)} + A \left(\left(\sin{\left(ψ \right)} \sin{\left(ϕ \right)} - \cos{\left(θ \right)} \cos{\left(ψ \right)} \cos{\left(ϕ \right)}\right) \left(- \sin{\left(ψ \right)} \cos{\left(ϕ \right)} - \sin{\left(ϕ \right)} \cos{\left(θ \right)} \cos{\left(ψ \right)}\right) + \left(- \sin{\left(ψ \right)} \sin{\left(ϕ \right)} \cos{\left(θ \right)} + \cos{\left(ψ \right)} \cos{\left(ϕ \right)}\right) \left(\sin{\left(ψ \right)} \cos{\left(θ \right)} \cos{\left(ϕ \right)} + \sin{\left(ϕ \right)} \cos{\left(ψ \right)}\right)\right) \cos{\left(2 L ω - t ω \right)}\right) \sin{\left(σ \right)} + \left(- A \left(\left(- \sin{\left(ψ \right)} \cos{\left(ϕ \right)} - \sin{\left(ϕ \right)} \cos{\left(θ \right)} \cos{\left(ψ \right)}\right) \left(\sin{\left(ψ \right)} \cos{\left(ϕ \right)} + \sin{\left(ϕ \right)} \cos{\left(θ \right)} \cos{\left(ψ \right)}\right) + \left(- \sin{\left(ψ \right)} \sin{\left(ϕ \right)} \cos{\left(θ \right)} + \cos{\left(ψ \right)} \cos{\left(ϕ \right)}\right)^{2}\right) \sin{\left(2 L ω - t ω \right)} + A \left(\left(- \sin{\left(ψ \right)} \cos{\left(ϕ \right)} - \sin{\left(ϕ \right)} \cos{\left(θ \right)} \cos{\left(ψ \right)}\right) \left(\sin{\left(ψ \right)} \cos{\left(ϕ \right)} + \sin{\left(ϕ \right)} \cos{\left(θ \right)} \cos{\left(ψ \right)}\right) + \left(- \sin{\left(ψ \right)} \sin{\left(ϕ \right)} \cos{\left(θ \right)} + \cos{\left(ψ \right)} \cos{\left(ϕ \right)}\right)^{2}\right) \cos{\left(2 L ω - t ω \right)}\right) \cos{\left(σ \right)} & \left(- A \left(\left(- \sin{\left(ψ \right)} \sin{\left(ϕ \right)} + \cos{\left(θ \right)} \cos{\left(ψ \right)} \cos{\left(ϕ \right)}\right) \left(\sin{\left(ψ \right)} \sin{\left(ϕ \right)} - \cos{\left(θ \right)} \cos{\left(ψ \right)} \cos{\left(ϕ \right)}\right) + \left(\sin{\left(ψ \right)} \cos{\left(θ \right)} \cos{\left(ϕ \right)} + \sin{\left(ϕ \right)} \cos{\left(ψ \right)}\right)^{2}\right) \sin{\left(2 L ω - t ω \right)} + A \left(\left(- \sin{\left(ψ \right)} \sin{\left(ϕ \right)} + \cos{\left(θ \right)} \cos{\left(ψ \right)} \cos{\left(ϕ \right)}\right) \left(\sin{\left(ψ \right)} \sin{\left(ϕ \right)} - \cos{\left(θ \right)} \cos{\left(ψ \right)} \cos{\left(ϕ \right)}\right) + \left(\sin{\left(ψ \right)} \cos{\left(θ \right)} \cos{\left(ϕ \right)} + \sin{\left(ϕ \right)} \cos{\left(ψ \right)}\right)^{2}\right) \cos{\left(2 L ω - t ω \right)}\right) \sin{\left(σ \right)} + \left(- A \left(\left(- \sin{\left(ψ \right)} \sin{\left(ϕ \right)} + \cos{\left(θ \right)} \cos{\left(ψ \right)} \cos{\left(ϕ \right)}\right) \left(\sin{\left(ψ \right)} \cos{\left(ϕ \right)} + \sin{\left(ϕ \right)} \cos{\left(θ \right)} \cos{\left(ψ \right)}\right) + \left(- \sin{\left(ψ \right)} \sin{\left(ϕ \right)} \cos{\left(θ \right)} + \cos{\left(ψ \right)} \cos{\left(ϕ \right)}\right) \left(\sin{\left(ψ \right)} \cos{\left(θ \right)} \cos{\left(ϕ \right)} + \sin{\left(ϕ \right)} \cos{\left(ψ \right)}\right)\right) \sin{\left(2 L ω - t ω \right)} + A \left(\left(- \sin{\left(ψ \right)} \sin{\left(ϕ \right)} + \cos{\left(θ \right)} \cos{\left(ψ \right)} \cos{\left(ϕ \right)}\right) \left(\sin{\left(ψ \right)} \cos{\left(ϕ \right)} + \sin{\left(ϕ \right)} \cos{\left(θ \right)} \cos{\left(ψ \right)}\right) + \left(- \sin{\left(ψ \right)} \sin{\left(ϕ \right)} \cos{\left(θ \right)} + \cos{\left(ψ \right)} \cos{\left(ϕ \right)}\right) \left(\sin{\left(ψ \right)} \cos{\left(θ \right)} \cos{\left(ϕ \right)} + \sin{\left(ϕ \right)} \cos{\left(ψ \right)}\right)\right) \cos{\left(2 L ω - t ω \right)}\right) \cos{\left(σ \right)} & \left(- A \left(\left(\sin{\left(ψ \right)} \sin{\left(ϕ \right)} - \cos{\left(θ \right)} \cos{\left(ψ \right)} \cos{\left(ϕ \right)}\right) \sin{\left(θ \right)} \cos{\left(ψ \right)} + \left(\sin{\left(ψ \right)} \cos{\left(θ \right)} \cos{\left(ϕ \right)} + \sin{\left(ϕ \right)} \cos{\left(ψ \right)}\right) \sin{\left(θ \right)} \sin{\left(ψ \right)}\right) \sin{\left(2 L ω - t ω \right)} + A \left(\left(\sin{\left(ψ \right)} \sin{\left(ϕ \right)} - \cos{\left(θ \right)} \cos{\left(ψ \right)} \cos{\left(ϕ \right)}\right) \sin{\left(θ \right)} \cos{\left(ψ \right)} + \left(\sin{\left(ψ \right)} \cos{\left(θ \right)} \cos{\left(ϕ \right)} + \sin{\left(ϕ \right)} \cos{\left(ψ \right)}\right) \sin{\left(θ \right)} \sin{\left(ψ \right)}\right) \cos{\left(2 L ω - t ω \right)}\right) \sin{\left(σ \right)} + \left(- A \left(\left(\sin{\left(ψ \right)} \cos{\left(ϕ \right)} + \sin{\left(ϕ \right)} \cos{\left(θ \right)} \cos{\left(ψ \right)}\right) \sin{\left(θ \right)} \cos{\left(ψ \right)} + \left(- \sin{\left(ψ \right)} \sin{\left(ϕ \right)} \cos{\left(θ \right)} + \cos{\left(ψ \right)} \cos{\left(ϕ \right)}\right) \sin{\left(θ \right)} \sin{\left(ψ \right)}\right) \sin{\left(2 L ω - t ω \right)} + A \left(\left(\sin{\left(ψ \right)} \cos{\left(ϕ \right)} + \sin{\left(ϕ \right)} \cos{\left(θ \right)} \cos{\left(ψ \right)}\right) \sin{\left(θ \right)} \cos{\left(ψ \right)} + \left(- \sin{\left(ψ \right)} \sin{\left(ϕ \right)} \cos{\left(θ \right)} + \cos{\left(ψ \right)} \cos{\left(ϕ \right)}\right) \sin{\left(θ \right)} \sin{\left(ψ \right)}\right) \cos{\left(2 L ω - t ω \right)}\right) \cos{\left(σ \right)}\end{matrix}\right]$

```python
expr2 = h1.dot(n2)
expr2 = expr2.to_matrix(LabFrame)
expr2
```

$\displaystyle \left[\begin{matrix}\left(- \sin{\left(ψ \right)} \cos{\left(ϕ \right)} - \sin{\left(ϕ \right)} \cos{\left(θ \right)} \cos{\left(ψ \right)}\right) \left(- A \left(\left(- \sin{\left(ψ \right)} \sin{\left(ϕ \right)} + \cos{\left(θ \right)} \cos{\left(ψ \right)} \cos{\left(ϕ \right)}\right) \sin{\left(σ \right)} + \left(- \sin{\left(ψ \right)} \cos{\left(ϕ \right)} - \sin{\left(ϕ \right)} \cos{\left(θ \right)} \cos{\left(ψ \right)}\right) \cos{\left(σ \right)}\right) \cos{\left(2 L ω - t ω \right)} - A \left(\left(- \sin{\left(ψ \right)} \sin{\left(ϕ \right)} \cos{\left(θ \right)} + \cos{\left(ψ \right)} \cos{\left(ϕ \right)}\right) \cos{\left(σ \right)} + \left(\sin{\left(ψ \right)} \cos{\left(θ \right)} \cos{\left(ϕ \right)} + \sin{\left(ϕ \right)} \cos{\left(ψ \right)}\right) \sin{\left(σ \right)}\right) \sin{\left(2 L ω - t ω \right)}\right) + \left(- A \left(\left(- \sin{\left(ψ \right)} \sin{\left(ϕ \right)} + \cos{\left(θ \right)} \cos{\left(ψ \right)} \cos{\left(ϕ \right)}\right) \sin{\left(σ \right)} + \left(- \sin{\left(ψ \right)} \cos{\left(ϕ \right)} - \sin{\left(ϕ \right)} \cos{\left(θ \right)} \cos{\left(ψ \right)}\right) \cos{\left(σ \right)}\right) \sin{\left(2 L ω - t ω \right)} + A \left(\left(- \sin{\left(ψ \right)} \sin{\left(ϕ \right)} \cos{\left(θ \right)} + \cos{\left(ψ \right)} \cos{\left(ϕ \right)}\right) \cos{\left(σ \right)} + \left(\sin{\left(ψ \right)} \cos{\left(θ \right)} \cos{\left(ϕ \right)} + \sin{\left(ϕ \right)} \cos{\left(ψ \right)}\right) \sin{\left(σ \right)}\right) \cos{\left(2 L ω - t ω \right)}\right) \left(- \sin{\left(ψ \right)} \sin{\left(ϕ \right)} \cos{\left(θ \right)} + \cos{\left(ψ \right)} \cos{\left(ϕ \right)}\right)\\\left(- \sin{\left(ψ \right)} \sin{\left(ϕ \right)} + \cos{\left(θ \right)} \cos{\left(ψ \right)} \cos{\left(ϕ \right)}\right) \left(- A \left(\left(- \sin{\left(ψ \right)} \sin{\left(ϕ \right)} + \cos{\left(θ \right)} \cos{\left(ψ \right)} \cos{\left(ϕ \right)}\right) \sin{\left(σ \right)} + \left(- \sin{\left(ψ \right)} \cos{\left(ϕ \right)} - \sin{\left(ϕ \right)} \cos{\left(θ \right)} \cos{\left(ψ \right)}\right) \cos{\left(σ \right)}\right) \cos{\left(2 L ω - t ω \right)} - A \left(\left(- \sin{\left(ψ \right)} \sin{\left(ϕ \right)} \cos{\left(θ \right)} + \cos{\left(ψ \right)} \cos{\left(ϕ \right)}\right) \cos{\left(σ \right)} + \left(\sin{\left(ψ \right)} \cos{\left(θ \right)} \cos{\left(ϕ \right)} + \sin{\left(ϕ \right)} \cos{\left(ψ \right)}\right) \sin{\left(σ \right)}\right) \sin{\left(2 L ω - t ω \right)}\right) + \left(- A \left(\left(- \sin{\left(ψ \right)} \sin{\left(ϕ \right)} + \cos{\left(θ \right)} \cos{\left(ψ \right)} \cos{\left(ϕ \right)}\right) \sin{\left(σ \right)} + \left(- \sin{\left(ψ \right)} \cos{\left(ϕ \right)} - \sin{\left(ϕ \right)} \cos{\left(θ \right)} \cos{\left(ψ \right)}\right) \cos{\left(σ \right)}\right) \sin{\left(2 L ω - t ω \right)} + A \left(\left(- \sin{\left(ψ \right)} \sin{\left(ϕ \right)} \cos{\left(θ \right)} + \cos{\left(ψ \right)} \cos{\left(ϕ \right)}\right) \cos{\left(σ \right)} + \left(\sin{\left(ψ \right)} \cos{\left(θ \right)} \cos{\left(ϕ \right)} + \sin{\left(ϕ \right)} \cos{\left(ψ \right)}\right) \sin{\left(σ \right)}\right) \cos{\left(2 L ω - t ω \right)}\right) \left(\sin{\left(ψ \right)} \cos{\left(θ \right)} \cos{\left(ϕ \right)} + \sin{\left(ϕ \right)} \cos{\left(ψ \right)}\right)\\\left(- A \left(\left(- \sin{\left(ψ \right)} \sin{\left(ϕ \right)} + \cos{\left(θ \right)} \cos{\left(ψ \right)} \cos{\left(ϕ \right)}\right) \sin{\left(σ \right)} + \left(- \sin{\left(ψ \right)} \cos{\left(ϕ \right)} - \sin{\left(ϕ \right)} \cos{\left(θ \right)} \cos{\left(ψ \right)}\right) \cos{\left(σ \right)}\right) \sin{\left(2 L ω - t ω \right)} + A \left(\left(- \sin{\left(ψ \right)} \sin{\left(ϕ \right)} \cos{\left(θ \right)} + \cos{\left(ψ \right)} \cos{\left(ϕ \right)}\right) \cos{\left(σ \right)} + \left(\sin{\left(ψ \right)} \cos{\left(θ \right)} \cos{\left(ϕ \right)} + \sin{\left(ϕ \right)} \cos{\left(ψ \right)}\right) \sin{\left(σ \right)}\right) \cos{\left(2 L ω - t ω \right)}\right) \sin{\left(θ \right)} \sin{\left(ψ \right)} + \left(- A \left(\left(- \sin{\left(ψ \right)} \sin{\left(ϕ \right)} + \cos{\left(θ \right)} \cos{\left(ψ \right)} \cos{\left(ϕ \right)}\right) \sin{\left(σ \right)} + \left(- \sin{\left(ψ \right)} \cos{\left(ϕ \right)} - \sin{\left(ϕ \right)} \cos{\left(θ \right)} \cos{\left(ψ \right)}\right) \cos{\left(σ \right)}\right) \cos{\left(2 L ω - t ω \right)} - A \left(\left(- \sin{\left(ψ \right)} \sin{\left(ϕ \right)} \cos{\left(θ \right)} + \cos{\left(ψ \right)} \cos{\left(ϕ \right)}\right) \cos{\left(σ \right)} + \left(\sin{\left(ψ \right)} \cos{\left(θ \right)} \cos{\left(ϕ \right)} + \sin{\left(ϕ \right)} \cos{\left(ψ \right)}\right) \sin{\left(σ \right)}\right) \sin{\left(2 L ω - t ω \right)}\right) \sin{\left(θ \right)} \cos{\left(ψ \right)}\end{matrix}\right]$

```python
expr1 == expr2
```

    False

```python
Y2_mat = (1+k_det.dot(n2_mat))*PSI(n2_mat,h(t, 2*L, e_mais_det, e_cruzado_det,A),k_det)\
   -(2*k_det.dot(n2_mat))*PSI(n2_mat,h(t,((1+k_det.dot(n2_mat))*L),e_mais_det,e_cruzado_det,A),k_det)\
   -(1-k_det.dot(n2_mat))*PSI(n2_mat,h(t, 0, e_mais_det, e_cruzado_det,A),k_det)
Y2_mat
```

$\displaystyle \left[\begin{matrix}- \frac{0.5 \left(\left(\left(A \left(\left(- \sin{\left(ψ \right)} \sin{\left(ϕ \right)} + \cos{\left(θ \right)} \cos{\left(ψ \right)} \cos{\left(ϕ \right)}\right) \left(\sin{\left(ψ \right)} \sin{\left(ϕ \right)} - \cos{\left(θ \right)} \cos{\left(ψ \right)} \cos{\left(ϕ \right)}\right) + \left(\sin{\left(ψ \right)} \cos{\left(θ \right)} \cos{\left(ϕ \right)} + \sin{\left(ϕ \right)} \cos{\left(ψ \right)}\right)^{2}\right) \cos{\left(t ω \right)} + 2 A \left(- \sin{\left(ψ \right)} \sin{\left(ϕ \right)} + \cos{\left(θ \right)} \cos{\left(ψ \right)} \cos{\left(ϕ \right)}\right) \left(\sin{\left(ψ \right)} \cos{\left(θ \right)} \cos{\left(ϕ \right)} + \sin{\left(ϕ \right)} \cos{\left(ψ \right)}\right) \sin{\left(t ω \right)}\right) \sin{\left(σ \right)} + \left(A \left(\left(- \sin{\left(ψ \right)} \sin{\left(ϕ \right)} + \cos{\left(θ \right)} \cos{\left(ψ \right)} \cos{\left(ϕ \right)}\right) \left(\sin{\left(ψ \right)} \cos{\left(ϕ \right)} + \sin{\left(ϕ \right)} \cos{\left(θ \right)} \cos{\left(ψ \right)}\right) + \left(- \sin{\left(ψ \right)} \sin{\left(ϕ \right)} \cos{\left(θ \right)} + \cos{\left(ψ \right)} \cos{\left(ϕ \right)}\right) \left(\sin{\left(ψ \right)} \cos{\left(θ \right)} \cos{\left(ϕ \right)} + \sin{\left(ϕ \right)} \cos{\left(ψ \right)}\right)\right) \cos{\left(t ω \right)} + A \left(\left(- \sin{\left(ψ \right)} \sin{\left(ϕ \right)} + \cos{\left(θ \right)} \cos{\left(ψ \right)} \cos{\left(ϕ \right)}\right) \left(- \sin{\left(ψ \right)} \sin{\left(ϕ \right)} \cos{\left(θ \right)} + \cos{\left(ψ \right)} \cos{\left(ϕ \right)}\right) + \left(- \sin{\left(ψ \right)} \cos{\left(ϕ \right)} - \sin{\left(ϕ \right)} \cos{\left(θ \right)} \cos{\left(ψ \right)}\right) \left(\sin{\left(ψ \right)} \cos{\left(θ \right)} \cos{\left(ϕ \right)} + \sin{\left(ϕ \right)} \cos{\left(ψ \right)}\right)\right) \sin{\left(t ω \right)}\right) \cos{\left(σ \right)}\right) \sin{\left(σ \right)} + \left(\left(A \left(\left(- \sin{\left(ψ \right)} \sin{\left(ϕ \right)} + \cos{\left(θ \right)} \cos{\left(ψ \right)} \cos{\left(ϕ \right)}\right) \left(- \sin{\left(ψ \right)} \sin{\left(ϕ \right)} \cos{\left(θ \right)} + \cos{\left(ψ \right)} \cos{\left(ϕ \right)}\right) + \left(- \sin{\left(ψ \right)} \cos{\left(ϕ \right)} - \sin{\left(ϕ \right)} \cos{\left(θ \right)} \cos{\left(ψ \right)}\right) \left(\sin{\left(ψ \right)} \cos{\left(θ \right)} \cos{\left(ϕ \right)} + \sin{\left(ϕ \right)} \cos{\left(ψ \right)}\right)\right) \sin{\left(t ω \right)} + A \left(\left(\sin{\left(ψ \right)} \sin{\left(ϕ \right)} - \cos{\left(θ \right)} \cos{\left(ψ \right)} \cos{\left(ϕ \right)}\right) \left(- \sin{\left(ψ \right)} \cos{\left(ϕ \right)} - \sin{\left(ϕ \right)} \cos{\left(θ \right)} \cos{\left(ψ \right)}\right) + \left(- \sin{\left(ψ \right)} \sin{\left(ϕ \right)} \cos{\left(θ \right)} + \cos{\left(ψ \right)} \cos{\left(ϕ \right)}\right) \left(\sin{\left(ψ \right)} \cos{\left(θ \right)} \cos{\left(ϕ \right)} + \sin{\left(ϕ \right)} \cos{\left(ψ \right)}\right)\right) \cos{\left(t ω \right)}\right) \sin{\left(σ \right)} + \left(A \left(\left(- \sin{\left(ψ \right)} \cos{\left(ϕ \right)} - \sin{\left(ϕ \right)} \cos{\left(θ \right)} \cos{\left(ψ \right)}\right) \left(\sin{\left(ψ \right)} \cos{\left(ϕ \right)} + \sin{\left(ϕ \right)} \cos{\left(θ \right)} \cos{\left(ψ \right)}\right) + \left(- \sin{\left(ψ \right)} \sin{\left(ϕ \right)} \cos{\left(θ \right)} + \cos{\left(ψ \right)} \cos{\left(ϕ \right)}\right)^{2}\right) \cos{\left(t ω \right)} + 2 A \left(- \sin{\left(ψ \right)} \cos{\left(ϕ \right)} - \sin{\left(ϕ \right)} \cos{\left(θ \right)} \cos{\left(ψ \right)}\right) \left(- \sin{\left(ψ \right)} \sin{\left(ϕ \right)} \cos{\left(θ \right)} + \cos{\left(ψ \right)} \cos{\left(ϕ \right)}\right) \sin{\left(t ω \right)}\right) \cos{\left(σ \right)}\right) \cos{\left(σ \right)}\right) \left(\sin{\left(θ \right)} \sin{\left(σ \right)} \cos{\left(ϕ \right)} - \sin{\left(θ \right)} \sin{\left(ϕ \right)} \cos{\left(σ \right)} + 1\right)}{1 - \left(- \sin{\left(θ \right)} \sin{\left(σ \right)} \cos{\left(ϕ \right)} + \sin{\left(θ \right)} \sin{\left(ϕ \right)} \cos{\left(σ \right)}\right)^{2}} + \frac{0.5 \left(\left(\left(A \left(\left(- \sin{\left(ψ \right)} \sin{\left(ϕ \right)} + \cos{\left(θ \right)} \cos{\left(ψ \right)} \cos{\left(ϕ \right)}\right) \left(\sin{\left(ψ \right)} \sin{\left(ϕ \right)} - \cos{\left(θ \right)} \cos{\left(ψ \right)} \cos{\left(ϕ \right)}\right) + \left(\sin{\left(ψ \right)} \cos{\left(θ \right)} \cos{\left(ϕ \right)} + \sin{\left(ϕ \right)} \cos{\left(ψ \right)}\right)^{2}\right) \cos{\left(2 L ω - t ω \right)} - 2 A \left(- \sin{\left(ψ \right)} \sin{\left(ϕ \right)} + \cos{\left(θ \right)} \cos{\left(ψ \right)} \cos{\left(ϕ \right)}\right) \left(\sin{\left(ψ \right)} \cos{\left(θ \right)} \cos{\left(ϕ \right)} + \sin{\left(ϕ \right)} \cos{\left(ψ \right)}\right) \sin{\left(2 L ω - t ω \right)}\right) \sin{\left(σ \right)} + \left(A \left(\left(- \sin{\left(ψ \right)} \sin{\left(ϕ \right)} + \cos{\left(θ \right)} \cos{\left(ψ \right)} \cos{\left(ϕ \right)}\right) \left(\sin{\left(ψ \right)} \cos{\left(ϕ \right)} + \sin{\left(ϕ \right)} \cos{\left(θ \right)} \cos{\left(ψ \right)}\right) + \left(- \sin{\left(ψ \right)} \sin{\left(ϕ \right)} \cos{\left(θ \right)} + \cos{\left(ψ \right)} \cos{\left(ϕ \right)}\right) \left(\sin{\left(ψ \right)} \cos{\left(θ \right)} \cos{\left(ϕ \right)} + \sin{\left(ϕ \right)} \cos{\left(ψ \right)}\right)\right) \cos{\left(2 L ω - t ω \right)} - A \left(\left(- \sin{\left(ψ \right)} \sin{\left(ϕ \right)} + \cos{\left(θ \right)} \cos{\left(ψ \right)} \cos{\left(ϕ \right)}\right) \left(- \sin{\left(ψ \right)} \sin{\left(ϕ \right)} \cos{\left(θ \right)} + \cos{\left(ψ \right)} \cos{\left(ϕ \right)}\right) + \left(- \sin{\left(ψ \right)} \cos{\left(ϕ \right)} - \sin{\left(ϕ \right)} \cos{\left(θ \right)} \cos{\left(ψ \right)}\right) \left(\sin{\left(ψ \right)} \cos{\left(θ \right)} \cos{\left(ϕ \right)} + \sin{\left(ϕ \right)} \cos{\left(ψ \right)}\right)\right) \sin{\left(2 L ω - t ω \right)}\right) \cos{\left(σ \right)}\right) \sin{\left(σ \right)} + \left(\left(- A \left(\left(- \sin{\left(ψ \right)} \sin{\left(ϕ \right)} + \cos{\left(θ \right)} \cos{\left(ψ \right)} \cos{\left(ϕ \right)}\right) \left(- \sin{\left(ψ \right)} \sin{\left(ϕ \right)} \cos{\left(θ \right)} + \cos{\left(ψ \right)} \cos{\left(ϕ \right)}\right) + \left(- \sin{\left(ψ \right)} \cos{\left(ϕ \right)} - \sin{\left(ϕ \right)} \cos{\left(θ \right)} \cos{\left(ψ \right)}\right) \left(\sin{\left(ψ \right)} \cos{\left(θ \right)} \cos{\left(ϕ \right)} + \sin{\left(ϕ \right)} \cos{\left(ψ \right)}\right)\right) \sin{\left(2 L ω - t ω \right)} + A \left(\left(\sin{\left(ψ \right)} \sin{\left(ϕ \right)} - \cos{\left(θ \right)} \cos{\left(ψ \right)} \cos{\left(ϕ \right)}\right) \left(- \sin{\left(ψ \right)} \cos{\left(ϕ \right)} - \sin{\left(ϕ \right)} \cos{\left(θ \right)} \cos{\left(ψ \right)}\right) + \left(- \sin{\left(ψ \right)} \sin{\left(ϕ \right)} \cos{\left(θ \right)} + \cos{\left(ψ \right)} \cos{\left(ϕ \right)}\right) \left(\sin{\left(ψ \right)} \cos{\left(θ \right)} \cos{\left(ϕ \right)} + \sin{\left(ϕ \right)} \cos{\left(ψ \right)}\right)\right) \cos{\left(2 L ω - t ω \right)}\right) \sin{\left(σ \right)} + \left(A \left(\left(- \sin{\left(ψ \right)} \cos{\left(ϕ \right)} - \sin{\left(ϕ \right)} \cos{\left(θ \right)} \cos{\left(ψ \right)}\right) \left(\sin{\left(ψ \right)} \cos{\left(ϕ \right)} + \sin{\left(ϕ \right)} \cos{\left(θ \right)} \cos{\left(ψ \right)}\right) + \left(- \sin{\left(ψ \right)} \sin{\left(ϕ \right)} \cos{\left(θ \right)} + \cos{\left(ψ \right)} \cos{\left(ϕ \right)}\right)^{2}\right) \cos{\left(2 L ω - t ω \right)} - 2 A \left(- \sin{\left(ψ \right)} \cos{\left(ϕ \right)} - \sin{\left(ϕ \right)} \cos{\left(θ \right)} \cos{\left(ψ \right)}\right) \left(- \sin{\left(ψ \right)} \sin{\left(ϕ \right)} \cos{\left(θ \right)} + \cos{\left(ψ \right)} \cos{\left(ϕ \right)}\right) \sin{\left(2 L ω - t ω \right)}\right) \cos{\left(σ \right)}\right) \cos{\left(σ \right)}\right) \left(- \sin{\left(θ \right)} \sin{\left(σ \right)} \cos{\left(ϕ \right)} + \sin{\left(θ \right)} \sin{\left(ϕ \right)} \cos{\left(σ \right)} + 1\right)}{1 - \left(- \sin{\left(θ \right)} \sin{\left(σ \right)} \cos{\left(ϕ \right)} + \sin{\left(θ \right)} \sin{\left(ϕ \right)} \cos{\left(σ \right)}\right)^{2}} - \frac{0.5 \left(\left(\left(A \left(\left(- \sin{\left(ψ \right)} \sin{\left(ϕ \right)} + \cos{\left(θ \right)} \cos{\left(ψ \right)} \cos{\left(ϕ \right)}\right) \left(\sin{\left(ψ \right)} \sin{\left(ϕ \right)} - \cos{\left(θ \right)} \cos{\left(ψ \right)} \cos{\left(ϕ \right)}\right) + \left(\sin{\left(ψ \right)} \cos{\left(θ \right)} \cos{\left(ϕ \right)} + \sin{\left(ϕ \right)} \cos{\left(ψ \right)}\right)^{2}\right) \cos{\left(L ω \left(- \sin{\left(θ \right)} \sin{\left(σ \right)} \cos{\left(ϕ \right)} + \sin{\left(θ \right)} \sin{\left(ϕ \right)} \cos{\left(σ \right)} + 1\right) - t ω \right)} - 2 A \left(- \sin{\left(ψ \right)} \sin{\left(ϕ \right)} + \cos{\left(θ \right)} \cos{\left(ψ \right)} \cos{\left(ϕ \right)}\right) \left(\sin{\left(ψ \right)} \cos{\left(θ \right)} \cos{\left(ϕ \right)} + \sin{\left(ϕ \right)} \cos{\left(ψ \right)}\right) \sin{\left(L ω \left(- \sin{\left(θ \right)} \sin{\left(σ \right)} \cos{\left(ϕ \right)} + \sin{\left(θ \right)} \sin{\left(ϕ \right)} \cos{\left(σ \right)} + 1\right) - t ω \right)}\right) \sin{\left(σ \right)} + \left(A \left(\left(- \sin{\left(ψ \right)} \sin{\left(ϕ \right)} + \cos{\left(θ \right)} \cos{\left(ψ \right)} \cos{\left(ϕ \right)}\right) \left(\sin{\left(ψ \right)} \cos{\left(ϕ \right)} + \sin{\left(ϕ \right)} \cos{\left(θ \right)} \cos{\left(ψ \right)}\right) + \left(- \sin{\left(ψ \right)} \sin{\left(ϕ \right)} \cos{\left(θ \right)} + \cos{\left(ψ \right)} \cos{\left(ϕ \right)}\right) \left(\sin{\left(ψ \right)} \cos{\left(θ \right)} \cos{\left(ϕ \right)} + \sin{\left(ϕ \right)} \cos{\left(ψ \right)}\right)\right) \cos{\left(L ω \left(- \sin{\left(θ \right)} \sin{\left(σ \right)} \cos{\left(ϕ \right)} + \sin{\left(θ \right)} \sin{\left(ϕ \right)} \cos{\left(σ \right)} + 1\right) - t ω \right)} - A \left(\left(- \sin{\left(ψ \right)} \sin{\left(ϕ \right)} + \cos{\left(θ \right)} \cos{\left(ψ \right)} \cos{\left(ϕ \right)}\right) \left(- \sin{\left(ψ \right)} \sin{\left(ϕ \right)} \cos{\left(θ \right)} + \cos{\left(ψ \right)} \cos{\left(ϕ \right)}\right) + \left(- \sin{\left(ψ \right)} \cos{\left(ϕ \right)} - \sin{\left(ϕ \right)} \cos{\left(θ \right)} \cos{\left(ψ \right)}\right) \left(\sin{\left(ψ \right)} \cos{\left(θ \right)} \cos{\left(ϕ \right)} + \sin{\left(ϕ \right)} \cos{\left(ψ \right)}\right)\right) \sin{\left(L ω \left(- \sin{\left(θ \right)} \sin{\left(σ \right)} \cos{\left(ϕ \right)} + \sin{\left(θ \right)} \sin{\left(ϕ \right)} \cos{\left(σ \right)} + 1\right) - t ω \right)}\right) \cos{\left(σ \right)}\right) \sin{\left(σ \right)} + \left(\left(- A \left(\left(- \sin{\left(ψ \right)} \sin{\left(ϕ \right)} + \cos{\left(θ \right)} \cos{\left(ψ \right)} \cos{\left(ϕ \right)}\right) \left(- \sin{\left(ψ \right)} \sin{\left(ϕ \right)} \cos{\left(θ \right)} + \cos{\left(ψ \right)} \cos{\left(ϕ \right)}\right) + \left(- \sin{\left(ψ \right)} \cos{\left(ϕ \right)} - \sin{\left(ϕ \right)} \cos{\left(θ \right)} \cos{\left(ψ \right)}\right) \left(\sin{\left(ψ \right)} \cos{\left(θ \right)} \cos{\left(ϕ \right)} + \sin{\left(ϕ \right)} \cos{\left(ψ \right)}\right)\right) \sin{\left(L ω \left(- \sin{\left(θ \right)} \sin{\left(σ \right)} \cos{\left(ϕ \right)} + \sin{\left(θ \right)} \sin{\left(ϕ \right)} \cos{\left(σ \right)} + 1\right) - t ω \right)} + A \left(\left(\sin{\left(ψ \right)} \sin{\left(ϕ \right)} - \cos{\left(θ \right)} \cos{\left(ψ \right)} \cos{\left(ϕ \right)}\right) \left(- \sin{\left(ψ \right)} \cos{\left(ϕ \right)} - \sin{\left(ϕ \right)} \cos{\left(θ \right)} \cos{\left(ψ \right)}\right) + \left(- \sin{\left(ψ \right)} \sin{\left(ϕ \right)} \cos{\left(θ \right)} + \cos{\left(ψ \right)} \cos{\left(ϕ \right)}\right) \left(\sin{\left(ψ \right)} \cos{\left(θ \right)} \cos{\left(ϕ \right)} + \sin{\left(ϕ \right)} \cos{\left(ψ \right)}\right)\right) \cos{\left(L ω \left(- \sin{\left(θ \right)} \sin{\left(σ \right)} \cos{\left(ϕ \right)} + \sin{\left(θ \right)} \sin{\left(ϕ \right)} \cos{\left(σ \right)} + 1\right) - t ω \right)}\right) \sin{\left(σ \right)} + \left(A \left(\left(- \sin{\left(ψ \right)} \cos{\left(ϕ \right)} - \sin{\left(ϕ \right)} \cos{\left(θ \right)} \cos{\left(ψ \right)}\right) \left(\sin{\left(ψ \right)} \cos{\left(ϕ \right)} + \sin{\left(ϕ \right)} \cos{\left(θ \right)} \cos{\left(ψ \right)}\right) + \left(- \sin{\left(ψ \right)} \sin{\left(ϕ \right)} \cos{\left(θ \right)} + \cos{\left(ψ \right)} \cos{\left(ϕ \right)}\right)^{2}\right) \cos{\left(L ω \left(- \sin{\left(θ \right)} \sin{\left(σ \right)} \cos{\left(ϕ \right)} + \sin{\left(θ \right)} \sin{\left(ϕ \right)} \cos{\left(σ \right)} + 1\right) - t ω \right)} - 2 A \left(- \sin{\left(ψ \right)} \cos{\left(ϕ \right)} - \sin{\left(ϕ \right)} \cos{\left(θ \right)} \cos{\left(ψ \right)}\right) \left(- \sin{\left(ψ \right)} \sin{\left(ϕ \right)} \cos{\left(θ \right)} + \cos{\left(ψ \right)} \cos{\left(ϕ \right)}\right) \sin{\left(L ω \left(- \sin{\left(θ \right)} \sin{\left(σ \right)} \cos{\left(ϕ \right)} + \sin{\left(θ \right)} \sin{\left(ϕ \right)} \cos{\left(σ \right)} + 1\right) - t ω \right)}\right) \cos{\left(σ \right)}\right) \cos{\left(σ \right)}\right) \left(- 2 \sin{\left(θ \right)} \sin{\left(σ \right)} \cos{\left(ϕ \right)} + 2 \sin{\left(θ \right)} \sin{\left(ϕ \right)} \cos{\left(σ \right)}\right)}{1 - \left(- \sin{\left(θ \right)} \sin{\left(σ \right)} \cos{\left(ϕ \right)} + \sin{\left(θ \right)} \sin{\left(ϕ \right)} \cos{\left(σ \right)}\right)^{2}}\end{matrix}\right]$

```python
Y2 =   (1+k_vec.dot(n2)) * PSI_new(n2,h_new(t, 2*L                  , e_plus_dyadic, e_cross_dyadic,A),k_vec) \
     - (2*k_vec.dot(n2)) * PSI_new(n2,h_new(t, ((1+k_vec.dot(n2))*L), e_plus_dyadic, e_cross_dyadic,A),k_vec) \
     - (1-k_vec.dot(n2)) * PSI_new(n2,h_new(t, 0                    , e_plus_dyadic, e_cross_dyadic,A),k_vec)
Y2
```

```python
(Y2 - Y2_mat[0]).simplify()
```

```python
n2.dot(k_vec)
```

```python
#Calculando Y3
#Y3=(1+k_det.dot(n3))*PSI(n3,h(t, 2*L, e_mais_det, e_cruzado_det,A),k_det)-(2*k_det.dot(n3))*PSI(n3,h(t,((1+k_det.dot(n3))*L),e_mais_det,e_cruzado_det,A),k_det)-(1-k_det.dot(n3))*PSI(n3,h(t, 0, e_mais_det, e_cruzado_det,A),k_det)
Y3 =   (1+k_vec.dot(n3)) * PSI_new(n3,h_new(t, 2*L                  , e_plus_dyadic, e_cross_dyadic,A),k_vec) \
     - (2*k_vec.dot(n3)) * PSI_new(n3,h_new(t, ((1+k_vec.dot(n3))*L), e_plus_dyadic, e_cross_dyadic,A),k_vec) \
     - (1-k_vec.dot(n3)) * PSI_new(n3,h_new(t, 0                    , e_plus_dyadic, e_cross_dyadic,A),k_vec)
```

```python
#calculando M
M=Y2-Y3
#M=sympy.expand_trig(M)
#M=sympy.expand(M)
M
```

```python
M = M.simplify()
```

```python
#Calculando M direto da equação da aula
M1=(1+k_det.dot(n2))*PSI(n2_mat,h(t, 2*L, e_mais_det, e_cruzado_det,A),k_det)-((1+k_det.dot(n3_mat))*PSI(n3_mat,h(t, 2*L, e_mais_det, e_cruzado_det,A),k_det))-(((2*k_det.dot(n2_mat))*PSI(n2_det,h(t,((1+k_det.dot(n2_det))*L),e_mais_det,e_cruzado_det,A),k_det))-((2*k_det.dot(n3_det))*PSI(n3_det,h(t,((1+k_det.dot(n3_det))*L),e_mais_det,e_cruzado_det,A),k_det)))-(((1-k_det.dot(n2_det))*PSI(n2_det,h(t, 0, e_mais_det, e_cruzado_det,A),k_det))-((1-k_det.dot(n3))*PSI(n3,h(t, 0, e_mais_det, e_cruzado_det,A),k_det)))
termsM1=sympy.expand_trig(M1)
M1=termsM1[0]
M1=sympy.expand(M1)
```

```python
M==M1
```

```python
#F plus escolhida como coef de cos(wt)
F_mais=M.coeff(cos(w*t))
#F_mais=sympy.trigsimp(F_mais)
```

```python
#F cross escolhida como coef de cos(wt)
F_cruzado=M.coeff(sin(w*t))
#F_cruzado=sympy.trigsimp(F_cruzado)
```

```python
#permitindo entrada de array nas funçoes
f_mais = sympy.lambdify([phi, theta, L, w, A, sigma, psi], F_mais)
f_cruzado = sympy.lambdify([phi, theta, L, w, A, sigma, psi], F_cruzado)
```

```python
#defining parameters
phi_value, theta_value = np.mgrid[-np.pi:np.pi:100j, 0:np.pi:100j]
l=5e9/3e8 #segundos
f=10**-3 #Hz
freq=2*np.pi*f
a=1
sig=np.pi/4
PSI=0
```

```python
#atribuindos os valores acima nas funções
f_mais_data = f_mais((phi_value),(theta_value),l,freq,a,sig,PSI)
f_cruzado_data = f_cruzado((phi_value),(theta_value),l,freq,a,sig,PSI)
```

```python
#plot phi, theta e F
fig = plt.figure()
ax = fig.gca(projection='3d')
#ax.plot_surface(phi_value, theta_value,(f_mais_data),color='b')
ax.plot_surface(phi_value, theta_value,(f_cruzado_data-f_mais_data),color='g')
ax.set_xlabel('phi')
ax.set_ylabel('theta')
ax.set_zlabel('F+')
plt.show()

```

```python
#plot x,y,z

fig = plt.figure()
ax = fig.gca(projection='3d')

x_mais=(f_mais_data)*np.sin(theta_value)*np.sin(phi_value)
y_mais=-(f_mais_data)*np.sin(theta_value)*np.cos(phi_value)
z_mais=(f_mais_data)*np.cos(theta_value)

x_cruzado=(f_cruzado_data)*np.sin(theta_value)*np.sin(phi_value)
y_cruzado=-(f_cruzado_data)*np.sin(theta_value)*np.cos(phi_value)
z_cruzado=(f_cruzado_data)*np.cos(theta_value)

#ax.plot_surface(x_mais,y_mais,z_mais,color='b')
ax.plot_surface((x_cruzado-x_mais),(y_cruzado-y_mais),(z_cruzado-z_mais),color='g', label = 'F_cruzado')

ax.set_xlabel('x')
ax.set_ylabel('y')
ax.set_zlabel('z')
plt.show()
```

```python

```


# Autoregressive (AR) HMM Demo

Modified from

https://github.com/lindermanlab/ssm-jax-refactor/blob/main/notebooks/arhmm-example.ipynb

This notebook illustrates the use of the _auto_regression_ observation model.
Let $x_t$ denote the observation at time $t$. Let $z_t$ denote the corresponding discrete latent state.

The autoregressive hidden Markov model has the following likelihood,
$$
\begin{align}
x_t \mid x_{t-1}, z_t &\sim
\mathcal{N}\left(A_{z_t} x_{t-1} + b_{z_t}, Q_{z_t} \right).
\end{align}
$$
(Technically, higher-order autoregressive processes with extra linear terms from inputs are also implemented.)

```python
!pip install  git+git://github.com/lindermanlab/ssm-jax-refactor.git

```

    Collecting git+git://github.com/lindermanlab/ssm-jax-refactor.git
      Cloning git://github.com/lindermanlab/ssm-jax-refactor.git to /tmp/pip-req-build-2x6xx8nv
      Running command git clone -q git://github.com/lindermanlab/ssm-jax-refactor.git /tmp/pip-req-build-2x6xx8nv
    Requirement already satisfied: numpy in /usr/local/lib/python3.7/dist-packages (from ssm==0.1) (1.19.5)
    Requirement already satisfied: scipy in /usr/local/lib/python3.7/dist-packages (from ssm==0.1) (1.4.1)
    Requirement already satisfied: matplotlib in /usr/local/lib/python3.7/dist-packages (from ssm==0.1) (3.2.2)
    Requirement already satisfied: scikit-learn in /usr/local/lib/python3.7/dist-packages (from ssm==0.1) (1.0.2)
    Requirement already satisfied: tqdm in /usr/local/lib/python3.7/dist-packages (from ssm==0.1) (4.62.3)
    Requirement already satisfied: seaborn in /usr/local/lib/python3.7/dist-packages (from ssm==0.1) (0.11.2)
    Collecting jax==0.2.21
      Downloading jax-0.2.21.tar.gz (756 kB)
         |████████████████████████████████| 756 kB 15.4 MB/s
    Requirement already satisfied: jaxlib in /usr/local/lib/python3.7/dist-packages (from ssm==0.1) (0.1.71+cuda111)
    Requirement already satisfied: h5py in /usr/local/lib/python3.7/dist-packages (from ssm==0.1) (3.1.0)
    Requirement already satisfied: jupyter in /usr/local/lib/python3.7/dist-packages (from ssm==0.1) (1.0.0)
    Requirement already satisfied: ipywidgets in /usr/local/lib/python3.7/dist-packages (from ssm==0.1) (7.6.5)
    Requirement already satisfied: tensorflow-probability in /usr/local/lib/python3.7/dist-packages (from ssm==0.1) (0.15.0)
    Requirement already satisfied: absl-py in /usr/local/lib/python3.7/dist-packages (from jax==0.2.21->ssm==0.1) (1.0.0)
    Requirement already satisfied: opt_einsum in /usr/local/lib/python3.7/dist-packages (from jax==0.2.21->ssm==0.1) (3.3.0)
    Requirement already satisfied: six in /usr/local/lib/python3.7/dist-packages (from absl-py->jax==0.2.21->ssm==0.1) (1.15.0)
    Requirement already satisfied: cached-property in /usr/local/lib/python3.7/dist-packages (from h5py->ssm==0.1) (1.5.2)
    Requirement already satisfied: jupyterlab-widgets>=1.0.0 in /usr/local/lib/python3.7/dist-packages (from ipywidgets->ssm==0.1) (1.0.2)
    Requirement already satisfied: ipython-genutils~=0.2.0 in /usr/local/lib/python3.7/dist-packages (from ipywidgets->ssm==0.1) (0.2.0)
    Requirement already satisfied: ipython>=4.0.0 in /usr/local/lib/python3.7/dist-packages (from ipywidgets->ssm==0.1) (5.5.0)
    Requirement already satisfied: widgetsnbextension~=3.5.0 in /usr/local/lib/python3.7/dist-packages (from ipywidgets->ssm==0.1) (3.5.2)
    Requirement already satisfied: nbformat>=4.2.0 in /usr/local/lib/python3.7/dist-packages (from ipywidgets->ssm==0.1) (5.1.3)
    Requirement already satisfied: traitlets>=4.3.1 in /usr/local/lib/python3.7/dist-packages (from ipywidgets->ssm==0.1) (5.1.1)
    Requirement already satisfied: ipykernel>=4.5.1 in /usr/local/lib/python3.7/dist-packages (from ipywidgets->ssm==0.1) (4.10.1)
    Requirement already satisfied: jupyter-client in /usr/local/lib/python3.7/dist-packages (from ipykernel>=4.5.1->ipywidgets->ssm==0.1) (5.3.5)
    Requirement already satisfied: tornado>=4.0 in /usr/local/lib/python3.7/dist-packages (from ipykernel>=4.5.1->ipywidgets->ssm==0.1) (5.1.1)
    Requirement already satisfied: simplegeneric>0.8 in /usr/local/lib/python3.7/dist-packages (from ipython>=4.0.0->ipywidgets->ssm==0.1) (0.8.1)
    Requirement already satisfied: setuptools>=18.5 in /usr/local/lib/python3.7/dist-packages (from ipython>=4.0.0->ipywidgets->ssm==0.1) (57.4.0)
    Requirement already satisfied: prompt-toolkit<2.0.0,>=1.0.4 in /usr/local/lib/python3.7/dist-packages (from ipython>=4.0.0->ipywidgets->ssm==0.1) (1.0.18)
    Requirement already satisfied: pygments in /usr/local/lib/python3.7/dist-packages (from ipython>=4.0.0->ipywidgets->ssm==0.1) (2.6.1)
    Requirement already satisfied: decorator in /usr/local/lib/python3.7/dist-packages (from ipython>=4.0.0->ipywidgets->ssm==0.1) (4.4.2)
    Requirement already satisfied: pexpect in /usr/local/lib/python3.7/dist-packages (from ipython>=4.0.0->ipywidgets->ssm==0.1) (4.8.0)
    Requirement already satisfied: pickleshare in /usr/local/lib/python3.7/dist-packages (from ipython>=4.0.0->ipywidgets->ssm==0.1) (0.7.5)
    Requirement already satisfied: jupyter-core in /usr/local/lib/python3.7/dist-packages (from nbformat>=4.2.0->ipywidgets->ssm==0.1) (4.9.1)
    Requirement already satisfied: jsonschema!=2.5.0,>=2.4 in /usr/local/lib/python3.7/dist-packages (from nbformat>=4.2.0->ipywidgets->ssm==0.1) (4.3.3)
    Requirement already satisfied: attrs>=17.4.0 in /usr/local/lib/python3.7/dist-packages (from jsonschema!=2.5.0,>=2.4->nbformat>=4.2.0->ipywidgets->ssm==0.1) (21.4.0)
    Requirement already satisfied: pyrsistent!=0.17.0,!=0.17.1,!=0.17.2,>=0.14.0 in /usr/local/lib/python3.7/dist-packages (from jsonschema!=2.5.0,>=2.4->nbformat>=4.2.0->ipywidgets->ssm==0.1) (0.18.1)
    Requirement already satisfied: importlib-metadata in /usr/local/lib/python3.7/dist-packages (from jsonschema!=2.5.0,>=2.4->nbformat>=4.2.0->ipywidgets->ssm==0.1) (4.10.1)
    Requirement already satisfied: importlib-resources>=1.4.0 in /usr/local/lib/python3.7/dist-packages (from jsonschema!=2.5.0,>=2.4->nbformat>=4.2.0->ipywidgets->ssm==0.1) (5.4.0)
    Requirement already satisfied: typing-extensions in /usr/local/lib/python3.7/dist-packages (from jsonschema!=2.5.0,>=2.4->nbformat>=4.2.0->ipywidgets->ssm==0.1) (3.10.0.2)
    Requirement already satisfied: zipp>=3.1.0 in /usr/local/lib/python3.7/dist-packages (from importlib-resources>=1.4.0->jsonschema!=2.5.0,>=2.4->nbformat>=4.2.0->ipywidgets->ssm==0.1) (3.7.0)
    Requirement already satisfied: wcwidth in /usr/local/lib/python3.7/dist-packages (from prompt-toolkit<2.0.0,>=1.0.4->ipython>=4.0.0->ipywidgets->ssm==0.1) (0.2.5)
    Requirement already satisfied: notebook>=4.4.1 in /usr/local/lib/python3.7/dist-packages (from widgetsnbextension~=3.5.0->ipywidgets->ssm==0.1) (5.3.1)
    Requirement already satisfied: jinja2 in /usr/local/lib/python3.7/dist-packages (from notebook>=4.4.1->widgetsnbextension~=3.5.0->ipywidgets->ssm==0.1) (2.11.3)
    Requirement already satisfied: nbconvert in /usr/local/lib/python3.7/dist-packages (from notebook>=4.4.1->widgetsnbextension~=3.5.0->ipywidgets->ssm==0.1) (5.6.1)
    Requirement already satisfied: Send2Trash in /usr/local/lib/python3.7/dist-packages (from notebook>=4.4.1->widgetsnbextension~=3.5.0->ipywidgets->ssm==0.1) (1.8.0)
    Requirement already satisfied: terminado>=0.8.1 in /usr/local/lib/python3.7/dist-packages (from notebook>=4.4.1->widgetsnbextension~=3.5.0->ipywidgets->ssm==0.1) (0.13.1)
    Requirement already satisfied: python-dateutil>=2.1 in /usr/local/lib/python3.7/dist-packages (from jupyter-client->ipykernel>=4.5.1->ipywidgets->ssm==0.1) (2.8.2)
    Requirement already satisfied: pyzmq>=13 in /usr/local/lib/python3.7/dist-packages (from jupyter-client->ipykernel>=4.5.1->ipywidgets->ssm==0.1) (22.3.0)
    Requirement already satisfied: ptyprocess in /usr/local/lib/python3.7/dist-packages (from terminado>=0.8.1->notebook>=4.4.1->widgetsnbextension~=3.5.0->ipywidgets->ssm==0.1) (0.7.0)
    Requirement already satisfied: flatbuffers<3.0,>=1.12 in /usr/local/lib/python3.7/dist-packages (from jaxlib->ssm==0.1) (2.0)
    Requirement already satisfied: MarkupSafe>=0.23 in /usr/local/lib/python3.7/dist-packages (from jinja2->notebook>=4.4.1->widgetsnbextension~=3.5.0->ipywidgets->ssm==0.1) (2.0.1)
    Requirement already satisfied: jupyter-console in /usr/local/lib/python3.7/dist-packages (from jupyter->ssm==0.1) (5.2.0)
    Requirement already satisfied: qtconsole in /usr/local/lib/python3.7/dist-packages (from jupyter->ssm==0.1) (5.2.2)
    Requirement already satisfied: kiwisolver>=1.0.1 in /usr/local/lib/python3.7/dist-packages (from matplotlib->ssm==0.1) (1.3.2)
    Requirement already satisfied: pyparsing!=2.0.4,!=2.1.2,!=2.1.6,>=2.0.1 in /usr/local/lib/python3.7/dist-packages (from matplotlib->ssm==0.1) (3.0.7)
    Requirement already satisfied: cycler>=0.10 in /usr/local/lib/python3.7/dist-packages (from matplotlib->ssm==0.1) (0.11.0)
    Requirement already satisfied: defusedxml in /usr/local/lib/python3.7/dist-packages (from nbconvert->notebook>=4.4.1->widgetsnbextension~=3.5.0->ipywidgets->ssm==0.1) (0.7.1)
    Requirement already satisfied: mistune<2,>=0.8.1 in /usr/local/lib/python3.7/dist-packages (from nbconvert->notebook>=4.4.1->widgetsnbextension~=3.5.0->ipywidgets->ssm==0.1) (0.8.4)
    Requirement already satisfied: testpath in /usr/local/lib/python3.7/dist-packages (from nbconvert->notebook>=4.4.1->widgetsnbextension~=3.5.0->ipywidgets->ssm==0.1) (0.5.0)
    Requirement already satisfied: bleach in /usr/local/lib/python3.7/dist-packages (from nbconvert->notebook>=4.4.1->widgetsnbextension~=3.5.0->ipywidgets->ssm==0.1) (4.1.0)
    Requirement already satisfied: entrypoints>=0.2.2 in /usr/local/lib/python3.7/dist-packages (from nbconvert->notebook>=4.4.1->widgetsnbextension~=3.5.0->ipywidgets->ssm==0.1) (0.3)
    Requirement already satisfied: pandocfilters>=1.4.1 in /usr/local/lib/python3.7/dist-packages (from nbconvert->notebook>=4.4.1->widgetsnbextension~=3.5.0->ipywidgets->ssm==0.1) (1.5.0)
    Requirement already satisfied: packaging in /usr/local/lib/python3.7/dist-packages (from bleach->nbconvert->notebook>=4.4.1->widgetsnbextension~=3.5.0->ipywidgets->ssm==0.1) (21.3)
    Requirement already satisfied: webencodings in /usr/local/lib/python3.7/dist-packages (from bleach->nbconvert->notebook>=4.4.1->widgetsnbextension~=3.5.0->ipywidgets->ssm==0.1) (0.5.1)
    Requirement already satisfied: qtpy in /usr/local/lib/python3.7/dist-packages (from qtconsole->jupyter->ssm==0.1) (2.0.0)
    Requirement already satisfied: threadpoolctl>=2.0.0 in /usr/local/lib/python3.7/dist-packages (from scikit-learn->ssm==0.1) (3.0.0)
    Requirement already satisfied: joblib>=0.11 in /usr/local/lib/python3.7/dist-packages (from scikit-learn->ssm==0.1) (1.1.0)
    Requirement already satisfied: pandas>=0.23 in /usr/local/lib/python3.7/dist-packages (from seaborn->ssm==0.1) (1.3.5)
    Requirement already satisfied: pytz>=2017.3 in /usr/local/lib/python3.7/dist-packages (from pandas>=0.23->seaborn->ssm==0.1) (2018.9)
    Requirement already satisfied: cloudpickle>=1.3 in /usr/local/lib/python3.7/dist-packages (from tensorflow-probability->ssm==0.1) (1.3.0)
    Requirement already satisfied: gast>=0.3.2 in /usr/local/lib/python3.7/dist-packages (from tensorflow-probability->ssm==0.1) (0.4.0)
    Requirement already satisfied: dm-tree in /usr/local/lib/python3.7/dist-packages (from tensorflow-probability->ssm==0.1) (0.1.6)
    Building wheels for collected packages: ssm, jax
      Building wheel for ssm (setup.py) ... done
      Created wheel for ssm: filename=ssm-0.1-py3-none-any.whl size=75282 sha256=bde077b314fa6664e8403f9bed8b69a2ae93fde31ed7843c500b456c71f7a819
      Stored in directory: /tmp/pip-ephem-wheel-cache-s91lg85w/wheels/78/93/24/866323c03bb6444c9ad2485bc0abe61ad5e6828d66c2c2fda3
      Building wheel for jax (setup.py) ... done
      Created wheel for jax: filename=jax-0.2.21-py3-none-any.whl size=869303 sha256=dc33be4778a51583c3656c00dad98a420e13b2905773a4f7fd321de9ac478eb7
      Stored in directory: /root/.cache/pip/wheels/5c/69/0d/3784dd6d281be0837d8cef1db0c8b37d108c8bff727b961178
    Successfully built ssm jax
    Installing collected packages: jax, ssm
      Attempting uninstall: jax
        Found existing installation: jax 0.2.25
        Uninstalling jax-0.2.25:
          Successfully uninstalled jax-0.2.25
    Successfully installed jax-0.2.21 ssm-0.1

```python
import ssm
```

```python
import copy

import jax.numpy as np
import jax.random as jr

from tensorflow_probability.substrates import jax as tfp

from ssm.distributions.linreg import GaussianLinearRegression
from ssm.arhmm import GaussianARHMM
from ssm.utils import find_permutation, random_rotation
from ssm.plots import gradient_cmap #, white_to_color_cmap

import matplotlib.pyplot as plt
%matplotlib inline

import seaborn as sns
```

```python
sns.set_style("white")
sns.set_context("talk")

color_names = [
    "windows blue",
    "red",
    "amber",
    "faded green",
    "dusty purple",
    "orange",
    "brown",
    "pink"
    ]

colors = sns.xkcd_palette(color_names)
cmap = gradient_cmap(colors)
```

```python
# Make a transition matrix
num_states = 5
transition_probs = (np.arange(num_states)**10).astype(float)
transition_probs /= transition_probs.sum()
transition_matrix = np.zeros((num_states, num_states))
for k, p in enumerate(transition_probs[::-1]):
    transition_matrix += np.roll(p * np.eye(num_states), k, axis=1)

plt.imshow(transition_matrix, vmin=0, vmax=1, cmap="Greys")
plt.xlabel("next state")
plt.ylabel("current state")
plt.title("transition matrix")
plt.colorbar()

plt.savefig('arhmm-transmat.pdf')
```

```python
# Make observation distributions
data_dim = 2
num_lags = 1

keys = jr.split(jr.PRNGKey(0), num_states)
angles = np.linspace(0, 2 * np.pi, num_states, endpoint=False)
theta = np.pi / 25 # rotational frequency
weights = np.array([0.8 * random_rotation(key, data_dim, theta=theta) for key in keys])
biases = np.column_stack([np.cos(angles), np.sin(angles), np.zeros((num_states, data_dim - 2))])
covariances = np.tile(0.001 * np.eye(data_dim), (num_states, 1, 1))

# Compute the stationary points
stationary_points = np.linalg.solve(np.eye(data_dim) - weights, biases)
```

```python
print(theta / (2*np.pi) * 360)
print(360/5)
```

    7.2
    72.0

# Plot dynamics functions

```python
if data_dim == 2:
    lim = 5
    x = np.linspace(-lim, lim, 10)
    y = np.linspace(-lim, lim, 10)
    X, Y = np.meshgrid(x, y)
    xy = np.column_stack((X.ravel(), Y.ravel()))

    fig, axs = plt.subplots(1, num_states, figsize=(3 * num_states, 6))
    for k in range(num_states):
        A, b = weights[k], biases[k]
        dxydt_m = xy.dot(A.T) + b - xy
        axs[k].quiver(xy[:, 0], xy[:, 1],
                    dxydt_m[:, 0], dxydt_m[:, 1],
                    color=colors[k % len(colors)])

        axs[k].set_xlabel('$y_1$')
        #axs[k].set_xticks([])
        if k == 0:
            axs[k].set_ylabel("$y_2$")
        #axs[k].set_yticks([])
        axs[k].set_aspect("equal")

    plt.tight_layout()

    plt.savefig('arhmm-flow-matrices.pdf')
```

```python
colors
```

<svg  width="440" height="55"><rect x="0" y="0" width="55" height="55" style="fill:#3778bf;stroke-width:2;stroke:rgb(255,255,255)"/><rect x="55" y="0" width="55" height="55" style="fill:#e50000;stroke-width:2;stroke:rgb(255,255,255)"/><rect x="110" y="0" width="55" height="55" style="fill:#feb308;stroke-width:2;stroke:rgb(255,255,255)"/><rect x="165" y="0" width="55" height="55" style="fill:#7bb274;stroke-width:2;stroke:rgb(255,255,255)"/><rect x="220" y="0" width="55" height="55" style="fill:#825f87;stroke-width:2;stroke:rgb(255,255,255)"/><rect x="275" y="0" width="55" height="55" style="fill:#f97306;stroke-width:2;stroke:rgb(255,255,255)"/><rect x="330" y="0" width="55" height="55" style="fill:#653700;stroke-width:2;stroke:rgb(255,255,255)"/><rect x="385" y="0" width="55" height="55" style="fill:#ff81c0;stroke-width:2;stroke:rgb(255,255,255)"/></svg>

```python
print(stationary_points)
```

    [[ 3.9209812  -1.9056123 ]
     [ 3.0239954   3.140209  ]
     [-2.0520504   3.8463693 ]
     [-4.2922325  -0.7630231 ]
     [-0.60069436 -4.3179426 ]]

# Sample data from the ARHMM

```python
# Make an Autoregressive (AR) HMM
true_initial_distribution = tfp.distributions.Categorical(logits=np.zeros(num_states))
true_transition_distribution = tfp.distributions.Categorical(probs=transition_matrix)

true_arhmm = GaussianARHMM(num_states,
                           transition_matrix=transition_matrix,
                           emission_weights=weights,
                           emission_biases=biases,
                           emission_covariances=covariances)
time_bins = 10000
true_states, data = true_arhmm.sample(jr.PRNGKey(0), time_bins)
```

```python
fig = plt.figure(figsize=(8, 8))
for k in range(num_states):
    plt.plot(*data[true_states==k].T, 'o', color=colors[k],
         alpha=0.75, markersize=3)

plt.plot(*data[:1000].T, '-k', lw=0.5, alpha=0.2)
plt.xlabel("$y_1$")
plt.ylabel("$y_2$")
# plt.gca().set_aspect("equal")

plt.savefig('arhmm-samples-2d.pdf')
```

```python
fig = plt.figure(figsize=(8, 8))
for k in range(num_states):
  ndx = (true_states == k)
  data_k = data[ndx]
  T = 12
  data_k = data_k[:T, :]
  plt.plot(data_k[:,0], data_k[:,1], 'o', color=colors[k],
         alpha=0.75, markersize=3)
  for t in range(T):
    plt.text(data_k[t,0], data_k[t,1], t, color=colors[k], fontsize=12)

#plt.plot(*data[:1000].T, '-k', lw=0.5, alpha=0.2)
plt.xlabel("$y_1$")
plt.ylabel("$y_2$")
# plt.gca().set_aspect("equal")

plt.savefig('arhmm-samples-2d-temporal.pdf')
```

```python
print(biases)
```

    [[ 1.          0.        ]
     [ 0.30901697  0.95105654]
     [-0.80901706  0.58778524]
     [-0.80901694 -0.5877853 ]
     [ 0.30901712 -0.9510565 ]]

```python
print(stationary_points)
```

    [[ 3.9209812  -1.9056123 ]
     [ 3.0239954   3.140209  ]
     [-2.0520504   3.8463693 ]
     [-4.2922325  -0.7630231 ]
     [-0.60069436 -4.3179426 ]]

```python
colors
```

<svg  width="440" height="55"><rect x="0" y="0" width="55" height="55" style="fill:#3778bf;stroke-width:2;stroke:rgb(255,255,255)"/><rect x="55" y="0" width="55" height="55" style="fill:#e50000;stroke-width:2;stroke:rgb(255,255,255)"/><rect x="110" y="0" width="55" height="55" style="fill:#feb308;stroke-width:2;stroke:rgb(255,255,255)"/><rect x="165" y="0" width="55" height="55" style="fill:#7bb274;stroke-width:2;stroke:rgb(255,255,255)"/><rect x="220" y="0" width="55" height="55" style="fill:#825f87;stroke-width:2;stroke:rgb(255,255,255)"/><rect x="275" y="0" width="55" height="55" style="fill:#f97306;stroke-width:2;stroke:rgb(255,255,255)"/><rect x="330" y="0" width="55" height="55" style="fill:#653700;stroke-width:2;stroke:rgb(255,255,255)"/><rect x="385" y="0" width="55" height="55" style="fill:#ff81c0;stroke-width:2;stroke:rgb(255,255,255)"/></svg>

Below, we visualize each component of of the observation variable as a time series. The colors correspond to the latent state. The dotted lines represent the stationary point of the the corresponding AR state while the solid lines are the actual observations sampled from the HMM.

```python
lim
```

    DeviceArray(4.7118726, dtype=float32)

```python
# Plot the data and the smoothed data
plot_slice = (0, 200)
lim = 1.05 * abs(data).max()
plt.figure(figsize=(8, 6))
plt.imshow(true_states[None, :],
           aspect="auto",
           cmap=cmap,
           vmin=0,
           vmax=len(colors)-1,
           extent=(0, time_bins, -lim, (data_dim)*lim))

Ey = np.array(stationary_points)[true_states]
for d in range(data_dim):
    plt.plot(data[:,d] + lim * d, '-k')
    plt.plot(Ey[:,d] + lim * d, ':k')

plt.xlim(plot_slice)
plt.xlabel("time")
#plt.yticks(lim * np.arange(data_dim), ["$y_{{{}}}$".format(d+1) for d in range(data_dim)])
plt.ylabel('observations')

plt.tight_layout()

plt.savefig('arhmm-samples-1d.pdf')

```

```python
data.shape
```

    (10000, 2)

```python
data[:10,:]
```

    DeviceArray([[-0.8169615 ,  0.55239207],
                 [-1.423961  ,  1.1161395 ],
                 [-1.8636721 ,  1.6114323 ],
                 [-2.194045  ,  2.0294168 ],
                 [-2.3697448 ,  2.4288142 ],
                 [-2.4682324 ,  2.7841942 ],
                 [-2.4781866 ,  2.9988554 ],
                 [-2.4822226 ,  3.1525304 ],
                 [-2.489001  ,  3.346456  ],
                 [-2.4774575 ,  3.4129188 ]], dtype=float32)

# Fit an ARHMM

```python
# Now fit an HMM to the data
key1, key2 = jr.split(jr.PRNGKey(0), 2)
test_num_states = num_states
initial_distribution = tfp.distributions.Categorical(logits=np.zeros(test_num_states))
transition_distribution = tfp.distributions.Categorical(logits=np.zeros((test_num_states, test_num_states)))
emission_distribution = GaussianLinearRegression(
    weights=np.tile(0.99 * np.eye(data_dim), (test_num_states, 1, 1)),
    bias=0.01 * jr.normal(key2, (test_num_states, data_dim)),
    scale_tril=np.tile(np.eye(data_dim), (test_num_states, 1, 1))
)

arhmm = GaussianARHMM(test_num_states,
                      data_dim,
                      num_lags,
                      seed=jr.PRNGKey(0))

lps, arhmm, posterior = arhmm.fit(data, method="em")
```

    Initializing...
    Done.

      0%|          | 0/100 [00:00<?, ?it/s]

```python
# Plot the log likelihoods against the true likelihood, for comparison
true_lp = true_arhmm.marginal_likelihood(data)
plt.plot(lps, label="EM")
plt.plot(true_lp * np.ones(len(lps)), ':k', label="True")
plt.xlabel("EM Iteration")
plt.ylabel("Log Probability")
plt.legend(loc="lower right")
plt.show()
```

```python
# # Find a permutation of the states that best matches the true and inferred states
# most_likely_states = posterior.most_likely_states()
# arhmm.permute(find_permutation(true_states[num_lags:], most_likely_states))
# posterior.update()
# most_likely_states = posterior.most_likely_states()
```

```python
if data_dim == 2:
    lim = abs(data).max()
    x = np.linspace(-lim, lim, 10)
    y = np.linspace(-lim, lim, 10)
    X, Y = np.meshgrid(x, y)
    xy = np.column_stack((X.ravel(), Y.ravel()))

    fig, axs = plt.subplots(2, max(num_states, test_num_states), figsize=(3 * num_states, 6))
    for i, model in enumerate([true_arhmm, arhmm]):
        for j in range(model.num_states):
            dist = model._emissions._distribution[j]
            A, b = dist.weights, dist.bias
            dxydt_m = xy.dot(A.T) + b - xy
            axs[i,j].quiver(xy[:, 0], xy[:, 1],
                        dxydt_m[:, 0], dxydt_m[:, 1],
                        color=colors[j % len(colors)])

            axs[i,j].set_xlabel('$x_1$')
            axs[i,j].set_xticks([])
            if j == 0:
                axs[i,j].set_ylabel("$x_2$")
            axs[i,j].set_yticks([])
            axs[i,j].set_aspect("equal")

    plt.tight_layout()

    plt.savefig('argmm-flow-matrices-true-and-estimated.pdf')
```

```python
if data_dim == 2:
    lim = abs(data).max()
    x = np.linspace(-lim, lim, 10)
    y = np.linspace(-lim, lim, 10)
    X, Y = np.meshgrid(x, y)
    xy = np.column_stack((X.ravel(), Y.ravel()))

    fig, axs = plt.subplots(1, max(num_states, test_num_states), figsize=(3 * num_states, 6))
    for i, model in enumerate([arhmm]):
        for j in range(model.num_states):
            dist = model._emissions._distribution[j]
            A, b = dist.weights, dist.bias
            dxydt_m = xy.dot(A.T) + b - xy
            axs[j].quiver(xy[:, 0], xy[:, 1],
                        dxydt_m[:, 0], dxydt_m[:, 1],
                        color=colors[j % len(colors)])

            axs[j].set_xlabel('$y_1$')
            axs[j].set_xticks([])
            if j == 0:
                axs[j].set_ylabel("$y_2$")
            axs[j].set_yticks([])
            axs[j].set_aspect("equal")

    plt.tight_layout()

    plt.savefig('arhmm-flow-matrices-estimated.pdf')
```

```python
# Plot the true and inferred discrete states
plot_slice = (0, 1000)
plt.figure(figsize=(8, 4))
plt.subplot(211)
plt.imshow(true_states[None,num_lags:], aspect="auto", interpolation="none", cmap=cmap, vmin=0, vmax=len(colors)-1)
plt.xlim(plot_slice)
plt.ylabel("$z_{\\mathrm{true}}$")
plt.yticks([])

plt.subplot(212)
# plt.imshow(most_likely_states[None,: :], aspect="auto", cmap=cmap, vmin=0, vmax=len(colors)-1)
plt.imshow(posterior.expected_states[0].T, aspect="auto", interpolation="none", cmap="Greys", vmin=0, vmax=1)
plt.xlim(plot_slice)
plt.ylabel("$z_{\\mathrm{inferred}}$")
plt.yticks([])
plt.xlabel("time")

plt.tight_layout()

plt.savefig('arhmm-state-est.pdf')
```

```python
# Sample the fitted model
sampled_states, sampled_data = arhmm.sample(jr.PRNGKey(0), time_bins)
```

```python
fig = plt.figure(figsize=(8, 8))
for k in range(num_states):
    plt.plot(*sampled_data[sampled_states==k].T, 'o', color=colors[k],
         alpha=0.75, markersize=3)

#plt.plot(*sampled_data.T, '-k', lw=0.5, alpha=0.2)
plt.plot(*sampled_data[:1000].T, '-k', lw=0.5, alpha=0.2)
plt.xlabel("$x_1$")
plt.ylabel("$x_2$")
# plt.gca().set_aspect("equal")

plt.savefig('arhmm-samples-2d-estimated.pdf')
```