{ "cells": [ { "metadata": {}, "cell_type": "markdown", "source": "# Animations", "id": "948d1d7c205c2354" }, { "metadata": {}, "cell_type": "markdown", "source": "[![Binder](https://mybinder.org/badge_logo.svg)](https://mybinder.org/v2/gh/DyonOylloug/numerical_function_spaces/HEAD?labpath=docs%2Fanimations.ipynb) - link to interactive notebooks session.", "id": "2683a311c73e3de" }, { "metadata": { "ExecuteTime": { "end_time": "2024-09-09T12:15:25.759671Z", "start_time": "2024-09-09T12:15:24.726208Z" } }, "cell_type": "code", "source": [ "# https://www.geeksforgeeks.org/using-matplotlib-for-animations/\n", "\n", "import numpy as np\n", "from IPython.display import HTML\n", "from matplotlib import pyplot as plt\n", "from matplotlib.animation import FuncAnimation\n", "\n", "import numerical_function_spaces.orlicz_spaces as osm" ], "id": "526586d4e940de4a", "outputs": [], "execution_count": 1 }, { "metadata": {}, "cell_type": "markdown", "source": "Define Orlicz function", "id": "88b581837f187a9a" }, { "metadata": { "ExecuteTime": { "end_time": "2024-09-09T12:15:29.310427Z", "start_time": "2024-09-09T12:15:29.301739Z" } }, "cell_type": "code", "source": [ "def Orlicz_function(u):\n", " Phi = np.zeros(len(u))\n", " for i in range(len(u)):\n", " n = -1\n", " while True:\n", " if u[i] > n and u[i] <= n + 1: # below two conjugated functions?\n", " Phi[i] = (n + 1) * u[i] - (n + 1) * n / 2\n", " # Phi[i] = n * u[i] - (n) * (n + 1) / 2\n", " break\n", " n = n + 1\n", " return Phi" ], "id": "6649e9d174e79e49", "outputs": [], "execution_count": 2 }, { "metadata": {}, "cell_type": "markdown", "source": [ "Define sequence\n", "\\begin{equation*}\n", "x_n(t)=\\chi_{\\left[0,\\frac{n}{100}\\right)}(t)\n", "\\end{equation*}\n", "for $n \\in \\left\\{1, 2, \\dots,200\\right\\}.$ " ], "id": "f9130bbf7f92dc1f" }, { "metadata": { "ExecuteTime": { "end_time": "2024-09-09T12:15:30.096192Z", "start_time": "2024-09-09T12:15:30.088197Z" } }, "cell_type": "code", "source": [ "def x_n(n):\n", " x = np.array([[1], [n / 100]])\n", " return x\n", "\n", "n = range(1,201) # used in FuncAnimation as frames " ], "id": "16df4003580095c6", "outputs": [], "execution_count": 3 }, { "metadata": {}, "cell_type": "markdown", "source": "## kappa() function", "id": "353bbc66e88dddc9" }, { "metadata": { "ExecuteTime": { "end_time": "2024-09-09T12:16:42.762671Z", "start_time": "2024-09-09T12:15:30.159370Z" } }, "cell_type": "code", "source": [ "# execution took around 1 minute\n", "fig = plt.figure()\n", "ax = plt.axes(xlim=(0, 15),\n", " ylim=(0, 5))\n", "\n", "p_norm = 1\n", "\n", "k_min = 0.1\n", "k_max = 15\n", "dk = 0.01\n", "\n", "k = np.arange(k_min, k_max, dk)\n", "kappa = np.zeros(len(k))\n", "\n", "line, = ax.plot([], [], linewidth=2, label=f'$\\\\kappa_{{p, x_n}}(k)$')\n", "line1, = ax.plot([], [], marker=\"o\", color=\"crimson\", linewidth=5, ms=5, label=\"$||x_n||$\")\n", "\n", "\n", "def animate(n):\n", " x = x_n(n)\n", " for i in range(len(k)):\n", " kappa[i] = osm.kappa(Orlicz_function, x=x, k=k[i], p_norm=p_norm)\n", " line.set_data(k, kappa)\n", " k_s = osm.p_Amemiya_norm_with_stars(Orlicz_function, x=x, p_norm=p_norm,\n", " k_min=k_min, k_max=k_max, dk=dk)\n", " line1.set_data([k_s[1], k_s[2]], [k_s[0], k_s[0]])\n", " ax.legend()\n", " ax.set_title(f\"$p={p_norm}, n={n}$\")\n", " return line,\n", "\n", "\n", "anim = FuncAnimation(fig, animate,\n", " frames=n, interval=200, blit=True)\n", "# anim.save('im.gif', writer=\"pillow\")\n", "# anim.save(filename=\"html_example.html\", writer=\"html\")\n", "plt.close()\n", "HTML(anim.to_jshtml())" ], "id": "e0509fd7d9fc1db7", "outputs": [ { "data": { "text/plain": [ "" ], "text/html": [ "\n", "\n", "\n", "\n", "\n", "\n", "
\n", " \n", "
\n", " \n", "
\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
\n", "
\n", " \n", " \n", " \n", " \n", " \n", " \n", "
\n", "
\n", "
\n", "\n", "\n", "\n" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "execution_count": 4 }, { "metadata": {}, "cell_type": "markdown", "source": "## alpha() function", "id": "29df6136117bf895" }, { "metadata": { "ExecuteTime": { "end_time": "2024-09-09T12:18:44.549711Z", "start_time": "2024-09-09T12:16:43.348304Z" } }, "cell_type": "code", "source": [ "# execution took around 2 minutes\n", "fig = plt.figure()\n", "ax = plt.axes(xlim=(0, 15),\n", " ylim=(-2, 15))\n", "\n", "p_norm = 1\n", "\n", "k_min = 0.1\n", "k_max = 15\n", "dk = 0.01\n", "\n", "u_max = 20 # for p_plus and Psi and alpha function\n", "du = 0.01 # for p_plus and Psi and alpha function\n", "\n", "p_plus = osm.right_side_derivative(Orlicz_function, u_max=u_max, du=du)\n", "Psi = osm.conjugate_function(Orlicz_function, u_max=u_max, du=du)\n", "\n", "line, = ax.plot([], [], linewidth=2, label=f'$\\\\alpha_{{p, x_n}}(k)$')\n", "line1, = ax.plot([], [], marker=\"o\", color=\"crimson\", linewidth=5, ms=5,\n", " label=\"$K_{{\\\\alpha, p}}\\left(x_n\\\\right)$\")\n", "\n", "ax.axhline(y=0, linestyle=\"--\", linewidth=1.3, label=\"0\", color=\"green\")\n", "\n", "\n", "def animate(n):\n", " x = x_n(n)\n", " k, alpha = osm.array_for_alpha(Orlicz_function, u_max=u_max, du=du, p_norm=p_norm, x=x,\n", " p_plus=p_plus,\n", " Psi=Psi,\n", " k_min=k_min, k_max=k_max, dk=dk)\n", " line.set_data(k, alpha)\n", " k_s = osm.p_Amemiya_norm_with_stars(Orlicz_function, x=x, p_norm=p_norm)\n", " line1.set_data([k_s[1], k_s[2]], [0, 0])\n", " ax.legend()\n", " ax.set_title(f\"$p={p_norm}, n={n}$\")\n", " return line,\n", "\n", "\n", "anim = FuncAnimation(fig, animate,\n", " frames=n, interval=200, blit=True)\n", "# anim.save('im.gif', writer=\"pillow\")\n", "# anim.save(filename=\"html_example.html\", writer=\"html\")\n", "plt.close()\n", "HTML(anim.to_jshtml())" ], "id": "f9950783e5806167", "outputs": [ { "data": { "text/plain": [ "" ], "text/html": [ "\n", "\n", "\n", "\n", "\n", "\n", "
\n", " \n", "
\n", " \n", "
\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
\n", "
\n", " \n", " \n", " \n", " \n", " \n", " \n", "
\n", "
\n", "
\n", "\n", "\n", "\n" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "execution_count": 5 }, { "metadata": {}, "cell_type": "markdown", "source": [ "## tau() function\n", "For $p\\_norm=1$ functions $tau()$ and $alpha()$ are almost the same (shifted up and down by 1). " ], "id": "804359accc3f6c15" }, { "metadata": { "ExecuteTime": { "end_time": "2024-09-09T12:20:47.942220Z", "start_time": "2024-09-09T12:18:45.101042Z" } }, "cell_type": "code", "source": [ "# execution took around 2 minutes\n", "fig = plt.figure()\n", "ax = plt.axes(xlim=(0, 15),\n", " ylim=(-2, 15))\n", "\n", "p_norm = 1\n", "\n", "k_min = 0.1\n", "k_max = 15\n", "dk = 0.01\n", "\n", "u_max = 20 # for p_plus and Psi and alpha function\n", "du = 0.01 # for p_plus and Psi and alpha function\n", "\n", "p_plus = osm.right_side_derivative(Orlicz_function, u_max=u_max, du=du)\n", "Psi = osm.conjugate_function(Orlicz_function, u_max=u_max, du=du)\n", "\n", "line, = ax.plot([], [], linewidth=2, label=f'$\\\\tau_{{p, x_n}}(k)$')\n", "line1, = ax.plot([], [], marker=\"o\", color=\"crimson\", linewidth=5, ms=5,\n", " label=\"$K_{{\\\\tau, p}}\\left(x_n\\\\right)$\")\n", "\n", "ax.axhline(y=1, linestyle=\"--\", linewidth=1.3, label=\"1\", color=\"green\")\n", "\n", "\n", "def animate(n):\n", " x = x_n(n)\n", " k, tau = osm.array_for_tau(Orlicz_function, u_max=u_max, du=du, p_norm=p_norm, x=x,\n", " p_plus=p_plus,\n", " Psi=Psi,\n", " k_min=k_min, k_max=k_max, dk=dk)\n", " line.set_data(k, tau)\n", " k_s = osm.p_Amemiya_norm_with_stars(Orlicz_function, x=x, p_norm=p_norm)\n", " line1.set_data([k_s[1], k_s[2]], [1, 1])\n", " ax.legend()\n", " ax.set_title(f\"$p={p_norm}, n={n}$\")\n", " return line,\n", "\n", "\n", "anim = FuncAnimation(fig, animate,\n", " frames=n, interval=200, blit=True)\n", "# anim.save('im.gif', writer=\"pillow\")\n", "# anim.save(filename=\"html_example.html\", writer=\"html\")\n", "plt.close()\n", "HTML(anim.to_jshtml())" ], "id": "7362e68ad3bfcdd1", "outputs": [ { "data": { "text/plain": [ "" ], "text/html": [ "\n", "\n", "\n", "\n", "\n", "\n", "
\n", " \n", "
\n", " \n", "
\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
\n", "
\n", " \n", " \n", " \n", " \n", " \n", " \n", "
\n", "
\n", "
\n", "\n", "\n", "\n" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "execution_count": 6 }, { "metadata": {}, "cell_type": "markdown", "source": "We can animate relative to a parameter *p_norm*", "id": "6707b6a623ada9a0" }, { "metadata": {}, "cell_type": "code", "source": [ "def Orlicz_function(u):\n", " Phi = np.zeros(len(u))\n", " for i in range(len(u)):\n", " n = -1\n", " while True:\n", " if u[i] > n and u[i] <= n + 1: # below two conjugated functions?\n", " Phi[i] = (n + 1) * u[i] - (n + 1) * n / 2\n", " # Phi[i] = n * u[i] - (n) * (n + 1) / 2\n", " break\n", " n = n + 1\n", " return Phi\n", "\n", "\n", "x = np.array([[1], [1]]) # \\chi_{[0,1]}" ], "id": "3f3914503e04e355", "outputs": [], "execution_count": 7 }, { "metadata": { "ExecuteTime": { "end_time": "2024-09-09T12:21:23.321374Z", "start_time": "2024-09-09T12:20:48.335386Z" } }, "cell_type": "code", "source": [ "# execution took around 30 seconds\n", "fig = plt.figure()\n", "ax = plt.axes(xlim=(0, 7),\n", " ylim=(0.5, 4))\n", "\n", "p_norm = list(np.arange(1, 2, 0.05))\n", "p_norm.extend(list(np.arange(2, 5, 0.2)))\n", "p_norm.extend(list(np.arange(5, 10, 0.5)))\n", "p_norm.extend(list(np.arange(10, 50, 1)))\n", "# p_norm.extend(list(np.arange(50,200,5)))\n", "\n", "p_norm.append(np.inf)\n", "\n", "k_min = 0.1\n", "k_max = 15\n", "dk = 0.01\n", "\n", "k = np.arange(k_min, k_max, dk)\n", "kappa = np.zeros(len(k))\n", "\n", "line, = ax.plot([], [], linewidth=2, label=f'$\\\\kappa_{{p, x_n}}(k)$')\n", "line1, = ax.plot([], [], marker=\"o\", color=\"crimson\", linewidth=5, ms=5, label=\"$||x_n||$\")\n", "\n", "\n", "def animate(n):\n", " for i in range(len(k)):\n", " kappa[i] = osm.kappa(Orlicz_function, x=x, k=k[i], p_norm=n)\n", " line.set_data(k, kappa)\n", " k_s = osm.p_Amemiya_norm_with_stars(Orlicz_function, x=x, p_norm=n,\n", " k_min=k_min, k_max=k_max, dk=dk)\n", " line1.set_data([k_s[1], k_s[2]], [k_s[0], k_s[0]])\n", " ax.legend()\n", " ax.set_title(f\"$p={round(n, 2)}$\")\n", " return line,\n", "\n", "\n", "anim = FuncAnimation(fig, animate,\n", " frames=p_norm, interval=200, blit=True)\n", "# anim.save('im.gif', writer=\"pillow\")\n", "# anim.save(filename=\"html_example.html\", writer=\"html\")\n", "plt.close()\n", "HTML(anim.to_jshtml())" ], "id": "1b3edc1898cbb698", "outputs": [ { "data": { "text/plain": [ "" ], "text/html": [ "\n", "\n", "\n", "\n", "\n", "\n", "
\n", " \n", "
\n", " \n", "
\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
\n", "
\n", " \n", " \n", " \n", " \n", " \n", " \n", "
\n", "
\n", "
\n", "\n", "\n", "\n" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "execution_count": 8 }, { "metadata": { "ExecuteTime": { "end_time": "2024-09-09T12:22:08.501353Z", "start_time": "2024-09-09T12:21:23.575559Z" } }, "cell_type": "code", "source": [ "# execution took around 1 minute\n", "fig = plt.figure()\n", "ax = plt.axes(xlim=(0, 5),\n", " ylim=(-2, 30))\n", "\n", "k_min = 0.1\n", "k_max = 15\n", "dk = 0.01\n", "\n", "u_max = 20\n", "du = 0.01\n", "\n", "p_plus = osm.right_side_derivative(Orlicz_function, u_max=u_max, du=du)\n", "Psi = osm.conjugate_function(Orlicz_function, u_max=u_max, du=du)\n", "\n", "p_norm = list(np.arange(1, 2, 0.05))\n", "p_norm.extend(list(np.arange(2, 5, 0.2)))\n", "p_norm.extend(list(np.arange(5, 10, 0.5)))\n", "p_norm.extend(list(np.arange(10, 50, 1)))\n", "# p_norm.extend(list(np.arange(50,200,5)))\n", "\n", "p_norm.append(np.inf)\n", "\n", "line, = ax.plot([], [], linewidth=2, label=f'$\\\\alpha_{{p, x_n}}(k)$')\n", "line1, = ax.plot([], [], marker=\"o\", color=\"crimson\", linewidth=5, ms=5,\n", " label=\"$K_{{\\\\alpha, p}}\\left(x_n\\\\right)$\")\n", "\n", "ax.axhline(y=0, linestyle=\"--\", linewidth=1.3, label=\"0\", color=\"green\")\n", "\n", "\n", "def animate(n):\n", " k, alpha = osm.array_for_alpha(Orlicz_function, u_max=u_max, du=du, p_norm=n, x=x,\n", " p_plus=p_plus,\n", " Psi=Psi,\n", " k_min=k_min, k_max=k_max, dk=dk)\n", " line.set_data(k, alpha)\n", " k_s = osm.p_Amemiya_norm_with_stars(Orlicz_function, x=x, p_norm=n)\n", " line1.set_data([k_s[1], k_s[2]], [0, 0])\n", " ax.legend()\n", " ax.set_title(f\"$p={round(n, 2)}$\")\n", " return line,\n", "\n", "\n", "anim = FuncAnimation(fig, animate,\n", " frames=p_norm, interval=200, blit=True)\n", "# anim.save('im.gif', writer=\"pillow\")\n", "# anim.save(filename=\"html_example.html\", writer=\"html\")\n", "plt.close()\n", "HTML(anim.to_jshtml())" ], "id": "f84dc658dc16c03f", "outputs": [ { "data": { "text/plain": [ "" ], "text/html": [ "\n", "\n", "\n", "\n", "\n", "\n", "
\n", " \n", "
\n", " \n", "
\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
\n", "
\n", " \n", " \n", " \n", " \n", " \n", " \n", "
\n", "
\n", "
\n", "\n", "\n", "\n" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "execution_count": 9 }, { "metadata": { "ExecuteTime": { "end_time": "2024-09-09T12:22:55.101283Z", "start_time": "2024-09-09T12:22:08.695289Z" } }, "cell_type": "code", "source": [ "# execution took around 1 minute\n", "fig = plt.figure()\n", "ax = plt.axes(xlim=(0, 5),\n", " ylim=(-1, 15))\n", "\n", "k_min = 0.1\n", "k_max = 15\n", "dk = 0.01\n", "\n", "u_max = 20\n", "du = 0.01\n", "\n", "p_plus = osm.right_side_derivative(Orlicz_function, u_max=u_max, du=du)\n", "Psi = osm.conjugate_function(Orlicz_function, u_max=u_max, du=du)\n", "\n", "p_norm = list(np.arange(1, 2, 0.05))\n", "p_norm.extend(list(np.arange(2, 5, 0.2)))\n", "p_norm.extend(list(np.arange(5, 10, 0.5)))\n", "p_norm.extend(list(np.arange(10, 50, 1)))\n", "# p_norm.extend(list(np.arange(50,200,5)))\n", "\n", "p_norm.append(np.inf)\n", "\n", "line, = ax.plot([], [], linewidth=2, label=f'$\\\\tau_{{p, x_n}}(k)$')\n", "line1, = ax.plot([], [], marker=\"o\", color=\"crimson\", linewidth=5, ms=5,\n", " label=\"$K_{{\\\\alpha, p}}\\left(x_n\\\\right)$\")\n", "\n", "ax.axhline(y=1, linestyle=\"--\", linewidth=1.3, label=\"1\", color=\"green\")\n", "\n", "\n", "def animate(n):\n", " k, tau = osm.array_for_tau(Orlicz_function, u_max=u_max, du=du, p_norm=n, x=x,\n", " p_plus=p_plus,\n", " Psi=Psi,\n", " k_min=k_min, k_max=k_max, dk=dk)\n", " line.set_data(k, tau)\n", " k_s = osm.p_Amemiya_norm_with_stars(Orlicz_function, x=x, p_norm=n)\n", " line1.set_data([k_s[1], k_s[2]], [1, 1])\n", " ax.legend()\n", " ax.set_title(f\"$p={round(n, 2)}$\")\n", " return line,\n", "\n", "\n", "anim = FuncAnimation(fig, animate,\n", " frames=p_norm, interval=200, blit=True)\n", "# anim.save('im.gif', writer=\"pillow\")\n", "# anim.save(filename=\"html_example.html\", writer=\"html\")\n", "plt.close()\n", "HTML(anim.to_jshtml())" ], "id": "7bc9420da3734a77", "outputs": [ { "data": { "text/plain": [ "" ], "text/html": [ "\n", "\n", "\n", "\n", "\n", "\n", "
\n", " \n", "
\n", " \n", "
\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
\n", "
\n", " \n", " \n", " \n", " \n", " \n", " \n", "
\n", "
\n", "
\n", "\n", "\n", "\n" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "execution_count": 10 }, { "metadata": { "ExecuteTime": { "end_time": "2024-09-09T12:22:55.362716Z", "start_time": "2024-09-09T12:22:55.360183Z" } }, "cell_type": "code", "source": "", "id": "751e00407b40d497", "outputs": [], "execution_count": null } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 2 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython2", "version": "2.7.6" } }, "nbformat": 4, "nbformat_minor": 5 }