# Sympy


```python
import sympy as sp
sp.init_printing()
# x = sp.symbols('x')
# sp.sin(x**3*sp.pi)
```

$$ L = T - V$$
kinetyczna - potencjalna energia

$$ x^2 + y^2 = z^2 $$
w tekście można inline $\pi$ koniec zdania

$$ \frac{\partial L}{\partial q_i} - \frac{d}{dt} \frac{d L}{d \dot{q}_i} = 0 $$

$$ T = \frac{1}{2} m (\dot{x_1}^2 + \dot {x_2}^2  + \dot {y_1}^2  + \dot {y_2}^2 ) $$

$$ V = m g y_1 + m g y_2 = mg(y_1 + y_2)$$


```python
m, g, L, t = sp.symbols('m g L t', positive = True)
theta1 = sp.Function('theta1')(t)
theta2 = sp.Function('theta2')(t)

x1 = L * sp.sin(theta1)
y1 = -L * sp.cos(theta1)

x2 = L * sp.sin(theta2) + x1
y2 = -L * sp.cos(theta2) + y1

T = 0.5 * m *  sum(x.diff(t)**2 for x in [x1, y1, x2, y2])
T = T.simplify()

V = m * g * (y1 + y2)

omega1 = sp.Function('omega1')(t)
omega2 = sp.Function('omega2')(t)

Lagrange = (T - V).subs({theta1.diff(t):omega1, theta2.diff(t):omega2})
```

$$ \frac{\partial L}{\partial q_i} - \frac{d}{dt} \frac{d L}{d \dot{q}_i} = 0 $$


```python
EL_equations = [sp.Eq(Lagrange.diff(theta),
                      Lagrange.diff(omega).diff(t).subs(
                        {theta1.diff(t):omega1, theta2.diff(t):omega2}))
                for theta, omega in [[theta1, omega1], [theta2, omega2]]]
```


```python
solution = sp.solve(EL_equations, omega1.diff(t), omega2.diff(t))
```


```python
import numpy as np 
v1 = sp.lambdify((theta1, theta2, omega1, omega2, L, g),
            solution[omega1.diff(t)], 'numpy')
v2 = sp.lambdify((theta1, theta2, omega1, omega2, L, g),
            solution[omega2.diff(t)], 'numpy')
```


```python
from scipy.integrate import odeint
L = 1
g = 9.81
def f(r, t):
    theta1, theta2, omega1, omega2 = r
    return np.array([omega1,
                     omega2,
                     v1(theta1, theta2, omega1, omega2, L, g),
                     v2(theta1, theta2, omega1, omega2, L, g),
                    ])

r = np.array([np.pi/2, np.pi/2, np.pi, 0])
t = np.linspace(0, 10, 1000)
r_array = odeint(f, r, t)
theta1 = r_array[:, 0]
theta2 = r_array[:, 1]

x1 = L * np.sin(theta1)
y1 = -L * np.cos(theta1)

x2 = L * np.sin(theta2) + x1
y2 = -L * np.cos(theta2) + y1


import matplotlib.pyplot as plt
from matplotlib import animation, rc
# %matplotlib inline
# rc('animation', html='html5')
# plt.plot(t, theta1)
# plt.plot(t, theta2)
# plt.figure()
# plt.plot(x1, y1)
# plt.plot(x2, y2)
# plt.show()

```


```python
fig, axes = plt.subplots()
i = 0
axes.set_xlim(-2*L, 2*L)
axes.set_ylim(-2*L, 2*L)
wahadlo, = axes.plot([0, x1[i], x2[i]], [0, y1[i], y2[i]], "ko-")
linia2, = axes.plot(x2[:i+1], y2[:i+1], "bo", alpha=0.5, lw=0)
def animate(i):
    wahadlo.set_data([0, x1[i], x2[i]], [0, y1[i], y2[i]])
    linia2.set_data(x2[:i+1], y2[:i+1])
    return [wahadlo, linia2]

anim = animation.FuncAnimation(fig, animate, len(t), interval=5)
plt.show()
```


```python
# def f(x):
#     return x**2

f = lambda x: x**2
f(3)
```


```python
plt.plot(t, (theta2+np.pi/2)//np.pi)
plt.show()
```

# Pandas


```python
import pandas as pd
# pd.DataFrame?
dane = pd.DataFrame(index=t, data=
                    {"theta1":theta1, "theta2":theta2, "x2":x2, "y2":y2})
np.save("dane.dat", theta1)
dane
#(theta1
# pd?
```




<div>
<table border="1" class="dataframe">
  <thead>
    <tr style="text-align: right;">
      <th></th>
      <th>theta1</th>
      <th>theta2</th>
      <th>x2</th>
      <th>y2</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th>0.00000</th>
      <td>1.570796</td>
      <td>1.570796</td>
      <td>2.000000</td>
      <td>-1.224647e-16</td>
    </tr>
    <tr>
      <th>0.01001</th>
      <td>1.601747</td>
      <td>1.570806</td>
      <td>1.999521</td>
      <td>3.095589e-02</td>
    </tr>
    <tr>
      <th>0.02002</th>
      <td>1.631689</td>
      <td>1.570870</td>
      <td>1.998147</td>
      <td>6.092880e-02</td>
    </tr>
    <tr>
      <th>0.03003</th>
      <td>1.660601</td>
      <td>1.571031</td>
      <td>1.995970</td>
      <td>8.991869e-02</td>
    </tr>
    <tr>
      <th>0.04004</th>
      <td>1.688471</td>
      <td>1.571318</td>
      <td>1.993084</td>
      <td>1.179254e-01</td>
    </tr>
    <tr>
      <th>0.05005</th>
      <td>1.715293</td>
      <td>1.571751</td>
      <td>1.989578</td>
      <td>1.449489e-01</td>
    </tr>
    <tr>
      <th>0.06006</th>
      <td>1.741067</td>
      <td>1.572336</td>
      <td>1.985538</td>
      <td>1.709889e-01</td>
    </tr>
    <tr>
      <th>0.07007</th>
      <td>1.765796</td>
      <td>1.573075</td>
      <td>1.981045</td>
      <td>1.960452e-01</td>
    </tr>
    <tr>
      <th>0.08008</th>
      <td>1.789489</td>
      <td>1.573960</td>
      <td>1.976177</td>
      <td>2.201177e-01</td>
    </tr>
    <tr>
      <th>0.09009</th>
      <td>1.812158</td>
      <td>1.574978</td>
      <td>1.971005</td>
      <td>2.432063e-01</td>
    </tr>
    <tr>
      <th>0.10010</th>
      <td>1.833817</td>
      <td>1.576109</td>
      <td>1.965595</td>
      <td>2.653109e-01</td>
    </tr>
    <tr>
      <th>0.11011</th>
      <td>1.854482</td>
      <td>1.577332</td>
      <td>1.960009</td>
      <td>2.864317e-01</td>
    </tr>
    <tr>
      <th>0.12012</th>
      <td>1.874171</td>
      <td>1.578623</td>
      <td>1.954303</td>
      <td>3.065687e-01</td>
    </tr>
    <tr>
      <th>0.13013</th>
      <td>1.892902</td>
      <td>1.579954</td>
      <td>1.948529</td>
      <td>3.257222e-01</td>
    </tr>
    <tr>
      <th>0.14014</th>
      <td>1.910694</td>
      <td>1.581298</td>
      <td>1.942734</td>
      <td>3.438926e-01</td>
    </tr>
    <tr>
      <th>0.15015</th>
      <td>1.927566</td>
      <td>1.582627</td>
      <td>1.936960</td>
      <td>3.610803e-01</td>
    </tr>
    <tr>
      <th>0.16016</th>
      <td>1.943538</td>
      <td>1.583912</td>
      <td>1.931246</td>
      <td>3.772857e-01</td>
    </tr>
    <tr>
      <th>0.17017</th>
      <td>1.958629</td>
      <td>1.585124</td>
      <td>1.925628</td>
      <td>3.925094e-01</td>
    </tr>
    <tr>
      <th>0.18018</th>
      <td>1.972855</td>
      <td>1.586235</td>
      <td>1.920138</td>
      <td>4.067519e-01</td>
    </tr>
    <tr>
      <th>0.19019</th>
      <td>1.986236</td>
      <td>1.587218</td>
      <td>1.914804</td>
      <td>4.200139e-01</td>
    </tr>
    <tr>
      <th>0.20020</th>
      <td>1.998789</td>
      <td>1.588048</td>
      <td>1.909652</td>
      <td>4.322960e-01</td>
    </tr>
    <tr>
      <th>0.21021</th>
      <td>2.010529</td>
      <td>1.588698</td>
      <td>1.904705</td>
      <td>4.435986e-01</td>
    </tr>
    <tr>
      <th>0.22022</th>
      <td>2.021473</td>
      <td>1.589145</td>
      <td>1.899984</td>
      <td>4.539223e-01</td>
    </tr>
    <tr>
      <th>0.23023</th>
      <td>2.031634</td>
      <td>1.589367</td>
      <td>1.895508</td>
      <td>4.632675e-01</td>
    </tr>
    <tr>
      <th>0.24024</th>
      <td>2.041026</td>
      <td>1.589341</td>
      <td>1.891292</td>
      <td>4.716346e-01</td>
    </tr>
    <tr>
      <th>0.25025</th>
      <td>2.049661</td>
      <td>1.589049</td>
      <td>1.887352</td>
      <td>4.790238e-01</td>
    </tr>
    <tr>
      <th>0.26026</th>
      <td>2.057553</td>
      <td>1.588471</td>
      <td>1.883699</td>
      <td>4.854353e-01</td>
    </tr>
    <tr>
      <th>0.27027</th>
      <td>2.064710</td>
      <td>1.587591</td>
      <td>1.880343</td>
      <td>4.908691e-01</td>
    </tr>
    <tr>
      <th>0.28028</th>
      <td>2.071142</td>
      <td>1.586393</td>
      <td>1.877295</td>
      <td>4.953250e-01</td>
    </tr>
    <tr>
      <th>0.29029</th>
      <td>2.076859</td>
      <td>1.584863</td>
      <td>1.874561</td>
      <td>4.988029e-01</td>
    </tr>
    <tr>
      <th>...</th>
      <td>...</td>
      <td>...</td>
      <td>...</td>
      <td>...</td>
    </tr>
    <tr>
      <th>9.70971</th>
      <td>0.819635</td>
      <td>1.317962</td>
      <td>1.699104</td>
      <td>-9.326377e-01</td>
    </tr>
    <tr>
      <th>9.71972</th>
      <td>0.862663</td>
      <td>1.333015</td>
      <td>1.731440</td>
      <td>-8.859643e-01</td>
    </tr>
    <tr>
      <th>9.72973</th>
      <td>0.905857</td>
      <td>1.346123</td>
      <td>1.761821</td>
      <td>-8.397995e-01</td>
    </tr>
    <tr>
      <th>9.73974</th>
      <td>0.949138</td>
      <td>1.357397</td>
      <td>1.790231</td>
      <td>-7.941669e-01</td>
    </tr>
    <tr>
      <th>9.74975</th>
      <td>0.992428</td>
      <td>1.366960</td>
      <td>1.816653</td>
      <td>-7.490864e-01</td>
    </tr>
    <tr>
      <th>9.75976</th>
      <td>1.035642</td>
      <td>1.374946</td>
      <td>1.841072</td>
      <td>-7.045746e-01</td>
    </tr>
    <tr>
      <th>9.76977</th>
      <td>1.078694</td>
      <td>1.381502</td>
      <td>1.863479</td>
      <td>-6.606461e-01</td>
    </tr>
    <tr>
      <th>9.77978</th>
      <td>1.121492</td>
      <td>1.386785</td>
      <td>1.883867</td>
      <td>-6.173138e-01</td>
    </tr>
    <tr>
      <th>9.78979</th>
      <td>1.163941</td>
      <td>1.390962</td>
      <td>1.902243</td>
      <td>-5.745897e-01</td>
    </tr>
    <tr>
      <th>9.79980</th>
      <td>1.205944</td>
      <td>1.394205</td>
      <td>1.918625</td>
      <td>-5.324858e-01</td>
    </tr>
    <tr>
      <th>9.80981</th>
      <td>1.247402</td>
      <td>1.396690</td>
      <td>1.933044</td>
      <td>-4.910145e-01</td>
    </tr>
    <tr>
      <th>9.81982</th>
      <td>1.288218</td>
      <td>1.398590</td>
      <td>1.945549</td>
      <td>-4.501893e-01</td>
    </tr>
    <tr>
      <th>9.82983</th>
      <td>1.328297</td>
      <td>1.400072</td>
      <td>1.956203</td>
      <td>-4.100252e-01</td>
    </tr>
    <tr>
      <th>9.83984</th>
      <td>1.367552</td>
      <td>1.401295</td>
      <td>1.965086</td>
      <td>-3.705385e-01</td>
    </tr>
    <tr>
      <th>9.84985</th>
      <td>1.405903</td>
      <td>1.402401</td>
      <td>1.972291</td>
      <td>-3.317477e-01</td>
    </tr>
    <tr>
      <th>9.85986</th>
      <td>1.443279</td>
      <td>1.403517</td>
      <td>1.977922</td>
      <td>-2.936722e-01</td>
    </tr>
    <tr>
      <th>9.86987</th>
      <td>1.479622</td>
      <td>1.404749</td>
      <td>1.982092</td>
      <td>-2.563330e-01</td>
    </tr>
    <tr>
      <th>9.87988</th>
      <td>1.514886</td>
      <td>1.406184</td>
      <td>1.984919</td>
      <td>-2.197517e-01</td>
    </tr>
    <tr>
      <th>9.88989</th>
      <td>1.549034</td>
      <td>1.407887</td>
      <td>1.986523</td>
      <td>-1.839500e-01</td>
    </tr>
    <tr>
      <th>9.89990</th>
      <td>1.582043</td>
      <td>1.409907</td>
      <td>1.987022</td>
      <td>-1.489498e-01</td>
    </tr>
    <tr>
      <th>9.90991</th>
      <td>1.613898</td>
      <td>1.412272</td>
      <td>1.986533</td>
      <td>-1.147721e-01</td>
    </tr>
    <tr>
      <th>9.91992</th>
      <td>1.644595</td>
      <td>1.414998</td>
      <td>1.985166</td>
      <td>-8.143720e-02</td>
    </tr>
    <tr>
      <th>9.92993</th>
      <td>1.674133</td>
      <td>1.418086</td>
      <td>1.983028</td>
      <td>-4.896396e-02</td>
    </tr>
    <tr>
      <th>9.93994</th>
      <td>1.702523</td>
      <td>1.421527</td>
      <td>1.980217</td>
      <td>-1.737007e-02</td>
    </tr>
    <tr>
      <th>9.94995</th>
      <td>1.729775</td>
      <td>1.425302</td>
      <td>1.976824</td>
      <td>1.332832e-02</td>
    </tr>
    <tr>
      <th>9.95996</th>
      <td>1.755906</td>
      <td>1.429388</td>
      <td>1.972934</td>
      <td>4.311659e-02</td>
    </tr>
    <tr>
      <th>9.96997</th>
      <td>1.780936</td>
      <td>1.433753</td>
      <td>1.968626</td>
      <td>7.198158e-02</td>
    </tr>
    <tr>
      <th>9.97998</th>
      <td>1.804885</td>
      <td>1.438364</td>
      <td>1.963970</td>
      <td>9.991162e-02</td>
    </tr>
    <tr>
      <th>9.98999</th>
      <td>1.827776</td>
      <td>1.443186</td>
      <td>1.959031</td>
      <td>1.268964e-01</td>
    </tr>
    <tr>
      <th>10.00000</th>
      <td>1.849633</td>
      <td>1.448179</td>
      <td>1.953868</td>
      <td>1.529269e-01</td>
    </tr>
  </tbody>
</table>
<p>1000 rows × 4 columns</p>
</div>




```python
%%timeit
np.loadtxt("dane.csv");
```

    1 loop, best of 3: 156 ms per loop



```python
%%timeit
pd.read_csv('dane.csv');
```

    100 loops, best of 3: 7.09 ms per loop



```python
%matplotlib inline
import scipy.fftpack as fft
t = np.linspace(0, 10, 1001)
x = 3*np.sin(2*np.pi*t) + np.sin(3*2*np.pi*t)
plt.plot(t, x)
# x_fft = fft.fft?
```


```python
x_fft = fft.rfft(x)/len(x)*2
# fft.fft?
fft_freq =fft.rfftfreq(len(x), d=t[1]-t[0])

plt.plot(fft_freq, x_fft)
plt.xlim(0, 10)
```


```python
x = np.linspace(0, np.pi, 1000) #siatka
rho = np.zeros(1000)
rho[500]=1/(x[1]-x[0])
plt.plot(x, rho)

```

$$\nabla^2 \psi \sim \rho$$

$$-k^2 \psi_F \sim \rho_F$$
