Animations
- link to interactive notebooks session.
# https://www.geeksforgeeks.org/using-matplotlib-for-animations/
import numpy as np
from IPython.display import HTML
from matplotlib import pyplot as plt
from matplotlib.animation import FuncAnimation
import numerical_function_spaces.orlicz_spaces as osm
Define Orlicz function
def Orlicz_function(u):
Phi = np.zeros(len(u))
for i in range(len(u)):
n = -1
while True:
if u[i] > n and u[i] <= n + 1: # below two conjugated functions?
Phi[i] = (n + 1) * u[i] - (n + 1) * n / 2
# Phi[i] = n * u[i] - (n) * (n + 1) / 2
break
n = n + 1
return Phi
Define sequence
\[\begin{equation*}
x_n(t)=\chi_{\left[0,\frac{n}{100}\right)}(t)
\end{equation*}\]
for \(n \in \left\{1, 2, \dots,200\right\}.\)
def x_n(n):
x = np.array([[1], [n / 100]])
return x
n = range(1,201) # used in FuncAnimation as frames
kappa() function
# execution took around 1 minute
fig = plt.figure()
ax = plt.axes(xlim=(0, 15),
ylim=(0, 5))
p_norm = 1
k_min = 0.1
k_max = 15
dk = 0.01
k = np.arange(k_min, k_max, dk)
kappa = np.zeros(len(k))
line, = ax.plot([], [], linewidth=2, label=f'$\\kappa_{{p, x_n}}(k)$')
line1, = ax.plot([], [], marker="o", color="crimson", linewidth=5, ms=5, label="$||x_n||$")
def animate(n):
x = x_n(n)
for i in range(len(k)):
kappa[i] = osm.kappa(Orlicz_function, x=x, k=k[i], p_norm=p_norm)
line.set_data(k, kappa)
k_s = osm.p_Amemiya_norm_with_stars(Orlicz_function, x=x, p_norm=p_norm,
k_min=k_min, k_max=k_max, dk=dk)
line1.set_data([k_s[1], k_s[2]], [k_s[0], k_s[0]])
ax.legend()
ax.set_title(f"$p={p_norm}, n={n}$")
return line,
anim = FuncAnimation(fig, animate,
frames=n, interval=200, blit=True)
# anim.save('im.gif', writer="pillow")
# anim.save(filename="html_example.html", writer="html")
plt.close()
HTML(anim.to_jshtml())
alpha() function
# execution took around 2 minutes
fig = plt.figure()
ax = plt.axes(xlim=(0, 15),
ylim=(-2, 15))
p_norm = 1
k_min = 0.1
k_max = 15
dk = 0.01
u_max = 20 # for p_plus and Psi and alpha function
du = 0.01 # for p_plus and Psi and alpha function
p_plus = osm.right_side_derivative(Orlicz_function, u_max=u_max, du=du)
Psi = osm.conjugate_function(Orlicz_function, u_max=u_max, du=du)
line, = ax.plot([], [], linewidth=2, label=f'$\\alpha_{{p, x_n}}(k)$')
line1, = ax.plot([], [], marker="o", color="crimson", linewidth=5, ms=5,
label="$K_{{\\alpha, p}}\left(x_n\\right)$")
ax.axhline(y=0, linestyle="--", linewidth=1.3, label="0", color="green")
def animate(n):
x = x_n(n)
k, alpha = osm.array_for_alpha(Orlicz_function, u_max=u_max, du=du, p_norm=p_norm, x=x,
p_plus=p_plus,
Psi=Psi,
k_min=k_min, k_max=k_max, dk=dk)
line.set_data(k, alpha)
k_s = osm.p_Amemiya_norm_with_stars(Orlicz_function, x=x, p_norm=p_norm)
line1.set_data([k_s[1], k_s[2]], [0, 0])
ax.legend()
ax.set_title(f"$p={p_norm}, n={n}$")
return line,
anim = FuncAnimation(fig, animate,
frames=n, interval=200, blit=True)
# anim.save('im.gif', writer="pillow")
# anim.save(filename="html_example.html", writer="html")
plt.close()
HTML(anim.to_jshtml())
tau() function
For \(p\_norm=1\) functions \(tau()\) and \(alpha()\) are almost the same (shifted up and down by 1).
# execution took around 2 minutes
fig = plt.figure()
ax = plt.axes(xlim=(0, 15),
ylim=(-2, 15))
p_norm = 1
k_min = 0.1
k_max = 15
dk = 0.01
u_max = 20 # for p_plus and Psi and alpha function
du = 0.01 # for p_plus and Psi and alpha function
p_plus = osm.right_side_derivative(Orlicz_function, u_max=u_max, du=du)
Psi = osm.conjugate_function(Orlicz_function, u_max=u_max, du=du)
line, = ax.plot([], [], linewidth=2, label=f'$\\tau_{{p, x_n}}(k)$')
line1, = ax.plot([], [], marker="o", color="crimson", linewidth=5, ms=5,
label="$K_{{\\tau, p}}\left(x_n\\right)$")
ax.axhline(y=1, linestyle="--", linewidth=1.3, label="1", color="green")
def animate(n):
x = x_n(n)
k, tau = osm.array_for_tau(Orlicz_function, u_max=u_max, du=du, p_norm=p_norm, x=x,
p_plus=p_plus,
Psi=Psi,
k_min=k_min, k_max=k_max, dk=dk)
line.set_data(k, tau)
k_s = osm.p_Amemiya_norm_with_stars(Orlicz_function, x=x, p_norm=p_norm)
line1.set_data([k_s[1], k_s[2]], [1, 1])
ax.legend()
ax.set_title(f"$p={p_norm}, n={n}$")
return line,
anim = FuncAnimation(fig, animate,
frames=n, interval=200, blit=True)
# anim.save('im.gif', writer="pillow")
# anim.save(filename="html_example.html", writer="html")
plt.close()
HTML(anim.to_jshtml())
We can animate relative to a parameter p_norm
def Orlicz_function(u):
Phi = np.zeros(len(u))
for i in range(len(u)):
n = -1
while True:
if u[i] > n and u[i] <= n + 1: # below two conjugated functions?
Phi[i] = (n + 1) * u[i] - (n + 1) * n / 2
# Phi[i] = n * u[i] - (n) * (n + 1) / 2
break
n = n + 1
return Phi
x = np.array([[1], [1]]) # \chi_{[0,1]}
# execution took around 30 seconds
fig = plt.figure()
ax = plt.axes(xlim=(0, 7),
ylim=(0.5, 4))
p_norm = list(np.arange(1, 2, 0.05))
p_norm.extend(list(np.arange(2, 5, 0.2)))
p_norm.extend(list(np.arange(5, 10, 0.5)))
p_norm.extend(list(np.arange(10, 50, 1)))
# p_norm.extend(list(np.arange(50,200,5)))
p_norm.append(np.inf)
k_min = 0.1
k_max = 15
dk = 0.01
k = np.arange(k_min, k_max, dk)
kappa = np.zeros(len(k))
line, = ax.plot([], [], linewidth=2, label=f'$\\kappa_{{p, x_n}}(k)$')
line1, = ax.plot([], [], marker="o", color="crimson", linewidth=5, ms=5, label="$||x_n||$")
def animate(n):
for i in range(len(k)):
kappa[i] = osm.kappa(Orlicz_function, x=x, k=k[i], p_norm=n)
line.set_data(k, kappa)
k_s = osm.p_Amemiya_norm_with_stars(Orlicz_function, x=x, p_norm=n,
k_min=k_min, k_max=k_max, dk=dk)
line1.set_data([k_s[1], k_s[2]], [k_s[0], k_s[0]])
ax.legend()
ax.set_title(f"$p={round(n, 2)}$")
return line,
anim = FuncAnimation(fig, animate,
frames=p_norm, interval=200, blit=True)
# anim.save('im.gif', writer="pillow")
# anim.save(filename="html_example.html", writer="html")
plt.close()
HTML(anim.to_jshtml())
# execution took around 1 minute
fig = plt.figure()
ax = plt.axes(xlim=(0, 5),
ylim=(-2, 30))
k_min = 0.1
k_max = 15
dk = 0.01
u_max = 20
du = 0.01
p_plus = osm.right_side_derivative(Orlicz_function, u_max=u_max, du=du)
Psi = osm.conjugate_function(Orlicz_function, u_max=u_max, du=du)
p_norm = list(np.arange(1, 2, 0.05))
p_norm.extend(list(np.arange(2, 5, 0.2)))
p_norm.extend(list(np.arange(5, 10, 0.5)))
p_norm.extend(list(np.arange(10, 50, 1)))
# p_norm.extend(list(np.arange(50,200,5)))
p_norm.append(np.inf)
line, = ax.plot([], [], linewidth=2, label=f'$\\alpha_{{p, x_n}}(k)$')
line1, = ax.plot([], [], marker="o", color="crimson", linewidth=5, ms=5,
label="$K_{{\\alpha, p}}\left(x_n\\right)$")
ax.axhline(y=0, linestyle="--", linewidth=1.3, label="0", color="green")
def animate(n):
k, alpha = osm.array_for_alpha(Orlicz_function, u_max=u_max, du=du, p_norm=n, x=x,
p_plus=p_plus,
Psi=Psi,
k_min=k_min, k_max=k_max, dk=dk)
line.set_data(k, alpha)
k_s = osm.p_Amemiya_norm_with_stars(Orlicz_function, x=x, p_norm=n)
line1.set_data([k_s[1], k_s[2]], [0, 0])
ax.legend()
ax.set_title(f"$p={round(n, 2)}$")
return line,
anim = FuncAnimation(fig, animate,
frames=p_norm, interval=200, blit=True)
# anim.save('im.gif', writer="pillow")
# anim.save(filename="html_example.html", writer="html")
plt.close()
HTML(anim.to_jshtml())
# execution took around 1 minute
fig = plt.figure()
ax = plt.axes(xlim=(0, 5),
ylim=(-1, 15))
k_min = 0.1
k_max = 15
dk = 0.01
u_max = 20
du = 0.01
p_plus = osm.right_side_derivative(Orlicz_function, u_max=u_max, du=du)
Psi = osm.conjugate_function(Orlicz_function, u_max=u_max, du=du)
p_norm = list(np.arange(1, 2, 0.05))
p_norm.extend(list(np.arange(2, 5, 0.2)))
p_norm.extend(list(np.arange(5, 10, 0.5)))
p_norm.extend(list(np.arange(10, 50, 1)))
# p_norm.extend(list(np.arange(50,200,5)))
p_norm.append(np.inf)
line, = ax.plot([], [], linewidth=2, label=f'$\\tau_{{p, x_n}}(k)$')
line1, = ax.plot([], [], marker="o", color="crimson", linewidth=5, ms=5,
label="$K_{{\\alpha, p}}\left(x_n\\right)$")
ax.axhline(y=1, linestyle="--", linewidth=1.3, label="1", color="green")
def animate(n):
k, tau = osm.array_for_tau(Orlicz_function, u_max=u_max, du=du, p_norm=n, x=x,
p_plus=p_plus,
Psi=Psi,
k_min=k_min, k_max=k_max, dk=dk)
line.set_data(k, tau)
k_s = osm.p_Amemiya_norm_with_stars(Orlicz_function, x=x, p_norm=n)
line1.set_data([k_s[1], k_s[2]], [1, 1])
ax.legend()
ax.set_title(f"$p={round(n, 2)}$")
return line,
anim = FuncAnimation(fig, animate,
frames=p_norm, interval=200, blit=True)
# anim.save('im.gif', writer="pillow")
# anim.save(filename="html_example.html", writer="html")
plt.close()
HTML(anim.to_jshtml())