Skip to content

Instantly share code, notes, and snippets.

@ceptreee
Created July 3, 2018 16:54
Show Gist options
  • Save ceptreee/2975f98f09c47ff1e293159efeb99fc3 to your computer and use it in GitHub Desktop.
Save ceptreee/2975f98f09c47ff1e293159efeb99fc3 to your computer and use it in GitHub Desktop.
import numpy as np
import matplotlib.pyplot as plt
import sympy as sb
from scipy import integrate
plt.rcParams["font.size"] = 10
# 複素変数
z = sb.Symbol('z')
# 複素関数
F = 1/(z**4 + 1)
# sympy to lambda
f = sb.lambdify(z,F)
# 特異点
SP = list(sb.singularities(F,z))
# 留数
Res = []
for sp in SP:
res = sb.residue(F,z,sp)
Res.append(res)
# 押した時
def Press(e):
global x,y,C,DragFlag
# 値がNoneなら終了
if (e.xdata is None) or (e.ydata is None):
return
# フラグをたてる
DragFlag = True
# ドラッグした時
def Drag(e):
global x,y,C,DragFlag
# 値がNoneなら終了
if (e.xdata is None) or (e.ydata is None):
return
x0 = e.xdata
y0 = e.ydata
z0 = x0+1j*y0
x = np.append(x, x0)
y = np.append(y, y0)
C = np.append(C, z0)
w0 = f(z0)
w = f(C)
I = integrate.cumtrapz(w,C)
ln_z0.set_data(x0,y0)
ln_C.set_data(C.real,C.imag)
ln_C0.set_data(C[0].real,C[0].imag)
if len(I) is not 0:
ln_I.set_data(I.real,I.imag)
ln_i.set_data(I[-1].real,I[-1].imag)
plt.draw()
# 離した時
def Release(e):
global x,y,C,DragFlag
# フラグをたおす
DragFlag = False
x = np.array([])
y = np.array([])
C = np.array([])
x = np.array([])
y = np.array([])
C = np.array([])
DragFlag = False
plt.close('all')
fig = plt.figure(figsize=(8,4))
plt.subplot(1,2,1)
ln_z0, = plt.plot([],[],'ko',zorder=2)
ln_C0, = plt.plot([],[],'ko',zorder=2)
ln_C, = plt.plot([],[],zorder=1)
xlm=[-3,3]
ylm=[-3,3]
plt.xlim(xlm)
plt.ylim(ylm)
plt.xlabel('$\\rm{Re}(z)$')
plt.ylabel('$\\rm{Im}(z)$')
plt.title('$f(z)='+sb.latex(F)+'$')
for sp in SP:
plt.plot(sb.re(sp), sb.im(sp),'kx')
plt.subplot(1,2,2)
ln_I, = plt.plot([],[])
ln_i, = plt.plot([],[],'ko')
plt.title('$\int_C f(z)dz$')
for res in Res:
plt.plot(sb.re(2*np.pi*sb.I*res), sb.im(2*np.pi*sb.I*res),'kx')
plt.xlim(xlm)
plt.ylim(ylm)
plt.xlabel('$\\rm{Re}$')
plt.ylabel('$\\rm{Im}$')
axes = plt.gcf().get_axes()
for i,axis in enumerate(axes):
plt.axes(axis)
plt.grid()
plt.tight_layout()
plt.connect('button_press_event', Press)
plt.connect('motion_notify_event', Drag)
plt.connect('button_release_event', Release)
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment