text:
# 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?
```

      theta1
      theta2
      x2
      y2

      0.00000
      1.570796
      1.570796
      2.000000
      -1.224647e-16

      0.01001
      1.601747
      1.570806
      1.999521
      3.095589e-02

      0.02002
      1.631689
      1.570870
      1.998147
      6.092880e-02

      0.03003
      1.660601
      1.571031
      1.995970
      8.991869e-02

      0.04004
      1.688471
      1.571318
      1.993084
      1.179254e-01

      0.05005
      1.715293
      1.571751
      1.989578
      1.449489e-01

      0.06006
      1.741067
      1.572336
      1.985538
      1.709889e-01

      0.07007
      1.765796
      1.573075
      1.981045
      1.960452e-01

      0.08008
      1.789489
      1.573960
      1.976177
      2.201177e-01

      0.09009
      1.812158
      1.574978
      1.971005
      2.432063e-01

      0.10010
      1.833817
      1.576109
      1.965595
      2.653109e-01

      0.11011
      1.854482
      1.577332
      1.960009
      2.864317e-01

      0.12012
      1.874171
      1.578623
      1.954303
      3.065687e-01

      0.13013
      1.892902
      1.579954
      1.948529
      3.257222e-01

      0.14014
      1.910694
      1.581298
      1.942734
      3.438926e-01

      0.15015
      1.927566
      1.582627
      1.936960
      3.610803e-01

      0.16016
      1.943538
      1.583912
      1.931246
      3.772857e-01

      0.17017
      1.958629
      1.585124
      1.925628
      3.925094e-01

      0.18018
      1.972855
      1.586235
      1.920138
      4.067519e-01

      0.19019
      1.986236
      1.587218
      1.914804
      4.200139e-01

      0.20020
      1.998789
      1.588048
      1.909652
      4.322960e-01

      0.21021
      2.010529
      1.588698
      1.904705
      4.435986e-01

      0.22022
      2.021473
      1.589145
      1.899984
      4.539223e-01

      0.23023
      2.031634
      1.589367
      1.895508
      4.632675e-01

      0.24024
      2.041026
      1.589341
      1.891292
      4.716346e-01

      0.25025
      2.049661
      1.589049
      1.887352
      4.790238e-01

      0.26026
      2.057553
      1.588471
      1.883699
      4.854353e-01

      0.27027
      2.064710
      1.587591
      1.880343
      4.908691e-01

      0.28028
      2.071142
      1.586393
      1.877295
      4.953250e-01

      0.29029
      2.076859
      1.584863
      1.874561
      4.988029e-01

      ...
      ...
      ...
      ...
      ...

      9.70971
      0.819635
      1.317962
      1.699104
      -9.326377e-01

      9.71972
      0.862663
      1.333015
      1.731440
      -8.859643e-01

      9.72973
      0.905857
      1.346123
      1.761821
      -8.397995e-01

      9.73974
      0.949138
      1.357397
      1.790231
      -7.941669e-01

      9.74975
      0.992428
      1.366960
      1.816653
      -7.490864e-01

      9.75976
      1.035642
      1.374946
      1.841072
      -7.045746e-01

      9.76977
      1.078694
      1.381502
      1.863479
      -6.606461e-01

      9.77978
      1.121492
      1.386785
      1.883867
      -6.173138e-01

      9.78979
      1.163941
      1.390962
      1.902243
      -5.745897e-01

      9.79980
      1.205944
      1.394205
      1.918625
      -5.324858e-01

      9.80981
      1.247402
      1.396690
      1.933044
      -4.910145e-01

      9.81982
      1.288218
      1.398590
      1.945549
      -4.501893e-01

      9.82983
      1.328297
      1.400072
      1.956203
      -4.100252e-01

      9.83984
      1.367552
      1.401295
      1.965086
      -3.705385e-01

      9.84985
      1.405903
      1.402401
      1.972291
      -3.317477e-01

      9.85986
      1.443279
      1.403517
      1.977922
      -2.936722e-01

      9.86987
      1.479622
      1.404749
      1.982092
      -2.563330e-01

      9.87988
      1.514886
      1.406184
      1.984919
      -2.197517e-01

      9.88989
      1.549034
      1.407887
      1.986523
      -1.839500e-01

      9.89990
      1.582043
      1.409907
      1.987022
      -1.489498e-01

      9.90991
      1.613898
      1.412272
      1.986533
      -1.147721e-01

      9.91992
      1.644595
      1.414998
      1.985166
      -8.143720e-02

      9.92993
      1.674133
      1.418086
      1.983028
      -4.896396e-02

      9.93994
      1.702523
      1.421527
      1.980217
      -1.737007e-02

      9.94995
      1.729775
      1.425302
      1.976824
      1.332832e-02

      9.95996
      1.755906
      1.429388
      1.972934
      4.311659e-02

      9.96997
      1.780936
      1.433753
      1.968626
      7.198158e-02

      9.97998
      1.804885
      1.438364
      1.963970
      9.991162e-02

      9.98999
      1.827776
      1.443186
      1.959031
      1.268964e-01

      10.00000
      1.849633
      1.448179
      1.953868
      1.529269e-01

1000 rows × 4 columns

```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$$