mirror of
https://github.com/ArthurDanjou/ArtStudies.git
synced 2026-03-18 12:49:41 +01:00
Refactor code for improved readability and consistency across notebooks
- Standardized spacing around operators and function arguments in TP7_Kmeans.ipynb and neural_network.ipynb. - Enhanced the formatting of model building and training code in neural_network.ipynb for better clarity. - Updated the pyproject.toml to remove a specific TensorFlow version and added linting configuration for Ruff. - Improved comments and organization in the code to facilitate easier understanding and maintenance.
This commit is contained in:
@@ -95,53 +95,63 @@
|
||||
"import numpy as np\n",
|
||||
"import matplotlib.pyplot as plt\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"# Fonction F définissant l'EDO\n",
|
||||
"def F(Y):\n",
|
||||
" x=Y[0]\n",
|
||||
" y=Y[1]\n",
|
||||
" A=np.array([[0,1],[-2,-3]])\n",
|
||||
" return np.dot(A,Y)\n",
|
||||
" x = Y[0]\n",
|
||||
" y = Y[1]\n",
|
||||
" A = np.array([[0, 1], [-2, -3]])\n",
|
||||
" return np.dot(A, Y)\n",
|
||||
"\n",
|
||||
"# ou \n",
|
||||
"def F1(x,y):\n",
|
||||
"\n",
|
||||
"# ou\n",
|
||||
"def F1(x, y):\n",
|
||||
" return y\n",
|
||||
"\n",
|
||||
"def F2(x,y):\n",
|
||||
" return -2*x-3*y\n",
|
||||
"\n",
|
||||
"def F2(x, y):\n",
|
||||
" return -2 * x - 3 * y\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"# Solution exacte de Y'=AY, Y(t_0)=Y_0\n",
|
||||
"def uex(t,t0,Y0):\n",
|
||||
" U1=np.array([1,-1])\n",
|
||||
" U2=np.array([1,-2])\n",
|
||||
" P=np.ones((2,2))\n",
|
||||
" P[:,0]=U1\n",
|
||||
" P[:,1]=U2\n",
|
||||
" C=np.linalg.solve(P,Y0)\n",
|
||||
" return np.array([(C[0]*np.exp(-(t-t0))*U1[0]+C[1]*np.exp(-2*(t-t0))*U2[0]),(C[0]*np.exp(-(t-t0))*U1[1]+C[1]*np.exp(-2*(t-t0))*U2[1])])\n",
|
||||
"def uex(t, t0, Y0):\n",
|
||||
" U1 = np.array([1, -1])\n",
|
||||
" U2 = np.array([1, -2])\n",
|
||||
" P = np.ones((2, 2))\n",
|
||||
" P[:, 0] = U1\n",
|
||||
" P[:, 1] = U2\n",
|
||||
" C = np.linalg.solve(P, Y0)\n",
|
||||
" return np.array(\n",
|
||||
" [\n",
|
||||
" (C[0] * np.exp(-(t - t0)) * U1[0] + C[1] * np.exp(-2 * (t - t0)) * U2[0]),\n",
|
||||
" (C[0] * np.exp(-(t - t0)) * U1[1] + C[1] * np.exp(-2 * (t - t0)) * U2[1]),\n",
|
||||
" ]\n",
|
||||
" )\n",
|
||||
"\n",
|
||||
"## Représentation des solutions pour chaque valeur de la donnée initiale \n",
|
||||
"\n",
|
||||
"## Représentation des solutions pour chaque valeur de la donnée initiale\n",
|
||||
"tt = np.linspace(-10, 10, 100)\n",
|
||||
"t0 = tt[0]\n",
|
||||
"for x, y in zip([1, -2, 0, 1, 3], [2, -2, -4, -2, 4]):\n",
|
||||
" sol = uex(tt, t0, [x, y])\n",
|
||||
" plt.plot(sol[0], sol[1], label=f'$((x0, y0) = ({x}, {y})$')\n",
|
||||
" plt.plot(sol[0], sol[1], label=f\"$((x0, y0) = ({x}, {y})$\")\n",
|
||||
" plt.scatter(x, y)\n",
|
||||
"\n",
|
||||
"#Tracé du champ de vecteurs\n",
|
||||
"x=np.linspace(-5,5,26)\n",
|
||||
"y=x\n",
|
||||
"xx,yy=np.meshgrid(x,y)\n",
|
||||
"U=F1(xx,yy)/np.sqrt(F1(xx,yy)**2+F2(xx,yy)**2)\n",
|
||||
"V=F2(xx,yy)/np.sqrt(F1(xx,yy)**2+F2(xx,yy)**2)\n",
|
||||
"plt.quiver(xx,yy,U,V,angles='xy', scale=20, color='gray')\n",
|
||||
"plt.axis([-5.,5,-5,5])\n",
|
||||
"# Tracé du champ de vecteurs\n",
|
||||
"x = np.linspace(-5, 5, 26)\n",
|
||||
"y = x\n",
|
||||
"xx, yy = np.meshgrid(x, y)\n",
|
||||
"U = F1(xx, yy) / np.sqrt(F1(xx, yy) ** 2 + F2(xx, yy) ** 2)\n",
|
||||
"V = F2(xx, yy) / np.sqrt(F1(xx, yy) ** 2 + F2(xx, yy) ** 2)\n",
|
||||
"plt.quiver(xx, yy, U, V, angles=\"xy\", scale=20, color=\"gray\")\n",
|
||||
"plt.axis([-5.0, 5, -5, 5])\n",
|
||||
"\n",
|
||||
"## Représentation des espaces propres \n",
|
||||
"plt.plot(tt, -tt, label=f'SEP associé à -1', linewidth=3)\n",
|
||||
"plt.plot(tt, -2*tt, label=f'SEP associé à -2', linewidth=3)\n",
|
||||
"## Représentation des espaces propres\n",
|
||||
"plt.plot(tt, -tt, label=\"SEP associé à -1\", linewidth=3)\n",
|
||||
"plt.plot(tt, -2 * tt, label=\"SEP associé à -2\", linewidth=3)\n",
|
||||
"\n",
|
||||
"plt.legend(fontsize=7)\n",
|
||||
"plt.title('Représentation des solutions et champs de vecteurs pour le système (S)')"
|
||||
"plt.title(\"Représentation des solutions et champs de vecteurs pour le système (S)\")"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -210,13 +220,15 @@
|
||||
"source": [
|
||||
"a, b, c, d = 0.1, 5e-5, 0.04, 5e-5\n",
|
||||
"H0, P0 = 2000, 1000\n",
|
||||
"He, Pe = c/d, a/b\n",
|
||||
"He, Pe = c / d, a / b\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"def F1(H, P):\n",
|
||||
" return H * (a - b*P)\n",
|
||||
" return H * (a - b * P)\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"def F2(H, P):\n",
|
||||
" return P * (-c + d*H)"
|
||||
" return P * (-c + d * H)"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -256,17 +268,17 @@
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"xx, yy = np.linspace(0, 3000, 20), np.linspace (0, 4500, 30)\n",
|
||||
"h,p = np.meshgrid(xx, yy)\n",
|
||||
"n=np.sqrt(F1(h,p)**2+F2(h,p)**2)\n",
|
||||
"plt.quiver(h, p, F1(h,p)/n, F2(h,p)/n, angles='xy', scale=20, color='gray')\n",
|
||||
"xx, yy = np.linspace(0, 3000, 20), np.linspace(0, 4500, 30)\n",
|
||||
"h, p = np.meshgrid(xx, yy)\n",
|
||||
"n = np.sqrt(F1(h, p) ** 2 + F2(h, p) ** 2)\n",
|
||||
"plt.quiver(h, p, F1(h, p) / n, F2(h, p) / n, angles=\"xy\", scale=20, color=\"gray\")\n",
|
||||
"\n",
|
||||
"plt.vlines(He, 0, 4500, label=f'H=He={He}')\n",
|
||||
"plt.hlines(Pe, 0, 3000, label=f'P=Pe={Pe}')\n",
|
||||
"plt.scatter(He, Pe, label=f'(H0, P0) = (He, Pe)')\n",
|
||||
"plt.scatter(H0, P0, label=f'(H, P)=(H0, P0)=({H0},{P0})', color='red')\n",
|
||||
"plt.vlines(He, 0, 4500, label=f\"H=He={He}\")\n",
|
||||
"plt.hlines(Pe, 0, 3000, label=f\"P=Pe={Pe}\")\n",
|
||||
"plt.scatter(He, Pe, label=\"(H0, P0) = (He, Pe)\")\n",
|
||||
"plt.scatter(H0, P0, label=f\"(H, P)=(H0, P0)=({H0},{P0})\", color=\"red\")\n",
|
||||
"\n",
|
||||
"plt.title('Le modèle de Lotka-Volterra')\n",
|
||||
"plt.title(\"Le modèle de Lotka-Volterra\")\n",
|
||||
"plt.legend(fontsize=7)"
|
||||
]
|
||||
},
|
||||
@@ -374,19 +386,20 @@
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"a, b, c, d = 0.1, 5e-5, 0.04, 5e-5\n",
|
||||
"T=200\n",
|
||||
"T = 200\n",
|
||||
"H0, P0 = 2000, 1000\n",
|
||||
"He, Pe = c/d, a/b\n",
|
||||
"p=0.02\n",
|
||||
"He, Pe = c / d, a / b\n",
|
||||
"p = 0.02\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"def voltEE(T, X0, h):\n",
|
||||
" t = np.arange(0, T+h, h)\n",
|
||||
" t = np.arange(0, T + h, h)\n",
|
||||
" H = 0 * t\n",
|
||||
" P = 0 * t\n",
|
||||
" H[0], P[0] = X0\n",
|
||||
" for n in range(len(t)-1):\n",
|
||||
" H[n+1] = H[n] + h * F1(H[n], P[n])\n",
|
||||
" P[n+1] = P[n] + h * F2(H[n], P[n])\n",
|
||||
" for n in range(len(t) - 1):\n",
|
||||
" H[n + 1] = H[n] + h * F1(H[n], P[n])\n",
|
||||
" P[n + 1] = P[n] + h * F2(H[n], P[n])\n",
|
||||
" return np.array([t, H, P])"
|
||||
]
|
||||
},
|
||||
@@ -438,22 +451,22 @@
|
||||
],
|
||||
"source": [
|
||||
"t, H, P = voltEE(T, [H0, P0], 0.001)\n",
|
||||
"plt.plot(t, H, label='Population de sardines')\n",
|
||||
"plt.plot(t, P, label='Population de requins')\n",
|
||||
"plt.plot(t, H, label=\"Population de sardines\")\n",
|
||||
"plt.plot(t, P, label=\"Population de requins\")\n",
|
||||
"plt.legend(fontsize=7)\n",
|
||||
"\n",
|
||||
"plt.figure()\n",
|
||||
"xx, yy = np.linspace(0, 3000, 20), np.linspace (0, 4500, 30)\n",
|
||||
"h,p = np.meshgrid(xx, yy)\n",
|
||||
"n=np.sqrt(F1(h,p)**2 + F2(h,p)**2)\n",
|
||||
"plt.quiver (h, p, F1(h,p)/n, F2(h,p)/n, angles='xy', scale=20, color='gray')\n",
|
||||
"xx, yy = np.linspace(0, 3000, 20), np.linspace(0, 4500, 30)\n",
|
||||
"h, p = np.meshgrid(xx, yy)\n",
|
||||
"n = np.sqrt(F1(h, p) ** 2 + F2(h, p) ** 2)\n",
|
||||
"plt.quiver(h, p, F1(h, p) / n, F2(h, p) / n, angles=\"xy\", scale=20, color=\"gray\")\n",
|
||||
"\n",
|
||||
"plt.vlines(He, 0, 4500, label=f'H=He={He}')\n",
|
||||
"plt.hlines(Pe, 0, 3000, label=f'P=Pe={Pe}')\n",
|
||||
"plt.scatter(He, Pe, label=f'(H0, P0) = (He, Pe)')\n",
|
||||
"plt.scatter(H0, P0, label=f'(H, P)=(H0, P0)=({H0},{P0})', color='red')\n",
|
||||
"plt.vlines(He, 0, 4500, label=f\"H=He={He}\")\n",
|
||||
"plt.hlines(Pe, 0, 3000, label=f\"P=Pe={Pe}\")\n",
|
||||
"plt.scatter(He, Pe, label=\"(H0, P0) = (He, Pe)\")\n",
|
||||
"plt.scatter(H0, P0, label=f\"(H, P)=(H0, P0)=({H0},{P0})\", color=\"red\")\n",
|
||||
"\n",
|
||||
"plt.title('Le modèle de Lotka-Volterra')\n",
|
||||
"plt.title(\"Le modèle de Lotka-Volterra\")\n",
|
||||
"plt.legend(fontsize=7)\n",
|
||||
"plt.plot(H, P)"
|
||||
]
|
||||
@@ -467,25 +480,28 @@
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"a, b, c, d = 0.1, 5e-5, 0.04, 5e-5\n",
|
||||
"T=200\n",
|
||||
"T = 200\n",
|
||||
"H0, P0 = 2000, 1000\n",
|
||||
"He, Pe = c/d, a/b\n",
|
||||
"p=0.02\n",
|
||||
"He, Pe = c / d, a / b\n",
|
||||
"p = 0.02\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"def F1_p(H, P):\n",
|
||||
" return (a - p) * H - b * H * P\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"def F2_p(H, P):\n",
|
||||
" return (-c-p) * P + d * H * P\n",
|
||||
" return (-c - p) * P + d * H * P\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"def voltEE_p(T, X0, h):\n",
|
||||
" t = np.arange(0, T+h, h)\n",
|
||||
" t = np.arange(0, T + h, h)\n",
|
||||
" H = 0 * t\n",
|
||||
" P = 0 * t\n",
|
||||
" H[0], P[0] = X0\n",
|
||||
" for n in range(len(t)-1):\n",
|
||||
" H[n+1] = H[n] + h * F1_p(H[n], P[n])\n",
|
||||
" P[n+1] = P[n] + h * F2_p(H[n], P[n])\n",
|
||||
" for n in range(len(t) - 1):\n",
|
||||
" H[n + 1] = H[n] + h * F1_p(H[n], P[n])\n",
|
||||
" P[n + 1] = P[n] + h * F2_p(H[n], P[n])\n",
|
||||
" return np.array([t, H, P])"
|
||||
]
|
||||
},
|
||||
@@ -535,22 +551,22 @@
|
||||
],
|
||||
"source": [
|
||||
"t, H, P = voltEE_p(T, [H0, P0], 0.001)\n",
|
||||
"plt.plot(t, H, label='Population de sardines')\n",
|
||||
"plt.plot(t, P, label='Population de requins')\n",
|
||||
"plt.plot(t, H, label=\"Population de sardines\")\n",
|
||||
"plt.plot(t, P, label=\"Population de requins\")\n",
|
||||
"plt.legend(fontsize=7)\n",
|
||||
"\n",
|
||||
"plt.figure()\n",
|
||||
"xx, yy = np.linspace(0, 3000, 20), np.linspace (0, 4500, 30)\n",
|
||||
"h,p = np.meshgrid(xx, yy)\n",
|
||||
"n=np.sqrt(F1(h,p)**2 + F2(h,p)**2)\n",
|
||||
"plt.quiver (h, p, F1(h,p)/n, F2(h,p)/n, angles='xy', scale=20, color='gray')\n",
|
||||
"xx, yy = np.linspace(0, 3000, 20), np.linspace(0, 4500, 30)\n",
|
||||
"h, p = np.meshgrid(xx, yy)\n",
|
||||
"n = np.sqrt(F1(h, p) ** 2 + F2(h, p) ** 2)\n",
|
||||
"plt.quiver(h, p, F1(h, p) / n, F2(h, p) / n, angles=\"xy\", scale=20, color=\"gray\")\n",
|
||||
"\n",
|
||||
"plt.vlines(He, 0, 4500, label=f'H=He={He}')\n",
|
||||
"plt.hlines(Pe, 0, 3000, label=f'P=Pe={Pe}')\n",
|
||||
"plt.scatter(He, Pe, label=f'(H0, P0) = (He, Pe)')\n",
|
||||
"plt.scatter(H0, P0, label=f'(H, P)=(H0, P0)=({H0},{P0})', color='red')\n",
|
||||
"plt.vlines(He, 0, 4500, label=f\"H=He={He}\")\n",
|
||||
"plt.hlines(Pe, 0, 3000, label=f\"P=Pe={Pe}\")\n",
|
||||
"plt.scatter(He, Pe, label=\"(H0, P0) = (He, Pe)\")\n",
|
||||
"plt.scatter(H0, P0, label=f\"(H, P)=(H0, P0)=({H0},{P0})\", color=\"red\")\n",
|
||||
"\n",
|
||||
"plt.title('Le modèle de Lotka-Volterra')\n",
|
||||
"plt.title(\"Le modèle de Lotka-Volterra\")\n",
|
||||
"plt.legend(fontsize=7)\n",
|
||||
"plt.plot(H, P)"
|
||||
]
|
||||
@@ -697,46 +713,49 @@
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"gamma=0.5\n",
|
||||
"a=0.004\n",
|
||||
"lm=10\n",
|
||||
"gamma = 0.5\n",
|
||||
"a = 0.004\n",
|
||||
"lm = 10\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"def fphi(d):\n",
|
||||
" return (gamma/d)*np.log(d/a)\n",
|
||||
" return (gamma / d) * np.log(d / a)\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"def F(X):\n",
|
||||
" (n,m)=np.shape(X)\n",
|
||||
" Y=np.zeros((n,m))\n",
|
||||
" Y[0,:]=X[1,:]\n",
|
||||
" Xaux=np.zeros(m+2)\n",
|
||||
" Xaux[-1]=1\n",
|
||||
" Xaux[1:-1]=X[0,:]\n",
|
||||
" Y[1,:]=fphi(Xaux[2:]-Xaux[1:-1])-fphi(Xaux[1:-1]-Xaux[0:-2])-lm*X[1,:]\n",
|
||||
" (n, m) = np.shape(X)\n",
|
||||
" Y = np.zeros((n, m))\n",
|
||||
" Y[0, :] = X[1, :]\n",
|
||||
" Xaux = np.zeros(m + 2)\n",
|
||||
" Xaux[-1] = 1\n",
|
||||
" Xaux[1:-1] = X[0, :]\n",
|
||||
" Y[1, :] = fphi(Xaux[2:] - Xaux[1:-1]) - fphi(Xaux[1:-1] - Xaux[0:-2]) - lm * X[1, :]\n",
|
||||
" return Y\n",
|
||||
"\n",
|
||||
"h=0.0002\n",
|
||||
"T=15\n",
|
||||
"N=100\n",
|
||||
"\n",
|
||||
"t=np.arange(0,T+h,h)\n",
|
||||
"Nt=np.size(t)\n",
|
||||
"X=np.zeros((2,N,Nt))\n",
|
||||
"R0=-1+2*np.random.rand(N)\n",
|
||||
"X0=np.arange(1/(N+1),1,1/(N+1))+0.1*R0*(1/(N+1))\n",
|
||||
"h = 0.0002\n",
|
||||
"T = 15\n",
|
||||
"N = 100\n",
|
||||
"\n",
|
||||
"X[0,:,0]=X0\n",
|
||||
"X[1,:,0]=X0\n",
|
||||
"t = np.arange(0, T + h, h)\n",
|
||||
"Nt = np.size(t)\n",
|
||||
"X = np.zeros((2, N, Nt))\n",
|
||||
"R0 = -1 + 2 * np.random.rand(N)\n",
|
||||
"X0 = np.arange(1 / (N + 1), 1, 1 / (N + 1)) + 0.1 * R0 * (1 / (N + 1))\n",
|
||||
"\n",
|
||||
"X[0, :, 0] = X0\n",
|
||||
"X[1, :, 0] = X0\n",
|
||||
"\n",
|
||||
"plt.figure(1, figsize=(24, 18))\n",
|
||||
"for n in range(Nt - 1):\n",
|
||||
" Y = F(X[:, :, n])\n",
|
||||
" X[:, :, n + 1] = X[:, :, n] + (h / 2) * Y + (h / 2) * F(X[:, :, n] + h * Y)\n",
|
||||
"\n",
|
||||
"plt.figure(1,figsize=(24,18))\n",
|
||||
"for n in range(Nt-1):\n",
|
||||
" Y=F(X[:,:,n])\n",
|
||||
" X[:,:,n+1]=X[:,:,n]+(h/2)*Y+(h/2)*F(X[:,:,n]+h*Y)\n",
|
||||
" \n",
|
||||
"for i in range(N):\n",
|
||||
" plt.plot(t,X[0,i,:],'k')\n",
|
||||
"plt.xlabel('t')\n",
|
||||
"plt.ylabel('$x_i$')\n",
|
||||
"plt.title('position x_i des globules rouges au cours du temps')\n"
|
||||
" plt.plot(t, X[0, i, :], \"k\")\n",
|
||||
"plt.xlabel(\"t\")\n",
|
||||
"plt.ylabel(\"$x_i$\")\n",
|
||||
"plt.title(\"position x_i des globules rouges au cours du temps\")"
|
||||
]
|
||||
},
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user