mirror of
https://github.com/ArthurDanjou/ArtStudies.git
synced 2026-01-14 18:59:59 +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:
File diff suppressed because one or more lines are too long
@@ -308,7 +308,6 @@
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"import numpy as np\n",
|
||||
"\n",
|
||||
"u = lambda x: np.sqrt((6 - x) ** 2 + 4)\n",
|
||||
"\n",
|
||||
@@ -364,7 +363,9 @@
|
||||
"# Run Newton's method\n",
|
||||
"optimal_point_newton, iterations_newton = newton_method(initial_guess_newton)\n",
|
||||
"print(f\"Optimal point (Newton): {optimal_point_newton}\")\n",
|
||||
"print(f\"Objective function value at optimal point (Newton): {objective_function(optimal_point_newton)}\")\n",
|
||||
"print(\n",
|
||||
" f\"Objective function value at optimal point (Newton): {objective_function(optimal_point_newton)}\"\n",
|
||||
")\n",
|
||||
"print(f\"Number of iterations (Newton): {iterations_newton}\")\n",
|
||||
"\n",
|
||||
"# Initial interval for dichotomy method\n",
|
||||
@@ -373,7 +374,9 @@
|
||||
"# Run dichotomy method\n",
|
||||
"optimal_point_dichotomy, iterations_dichotomy = dichotomy_method(aL, aR)\n",
|
||||
"print(f\"Optimal point (Dichotomy): {optimal_point_dichotomy}\")\n",
|
||||
"print(f\"Objective function value at optimal point (Dichotomy): {objective_function(optimal_point_dichotomy)}\")\n",
|
||||
"print(\n",
|
||||
" f\"Objective function value at optimal point (Dichotomy): {objective_function(optimal_point_dichotomy)}\"\n",
|
||||
")\n",
|
||||
"print(f\"Number of iterations (Dichotomy): {iterations_dichotomy}\")"
|
||||
]
|
||||
},
|
||||
@@ -564,9 +567,7 @@
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"\n"
|
||||
]
|
||||
"source": []
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
|
||||
@@ -42,20 +42,24 @@
|
||||
"import numpy as np\n",
|
||||
"import matplotlib.pyplot as plt\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"def generate_thetas(n):\n",
|
||||
" random_steps = np.random.random(n)\n",
|
||||
" return np.concatenate(([0], np.cumsum(random_steps / np.sum(random_steps) * (2*np.pi))))\n",
|
||||
" return np.concatenate(\n",
|
||||
" ([0], np.cumsum(random_steps / np.sum(random_steps) * (2 * np.pi)))\n",
|
||||
" )\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"n = 4\n",
|
||||
"thetas = generate_thetas(n)\n",
|
||||
"thetas_inf = np.linspace(0, 2*np.pi, 1000)\n",
|
||||
"thetas_inf = np.linspace(0, 2 * np.pi, 1000)\n",
|
||||
"\n",
|
||||
"plt.figure(figsize=(7, 7))\n",
|
||||
"plt.plot(np.cos(thetas), np.sin(thetas), label='polygon')\n",
|
||||
"plt.scatter(np.cos(thetas), np.sin(thetas), color='red', label='vertices')\n",
|
||||
"plt.plot(np.cos(thetas_inf), np.sin(thetas_inf), 'k--', label='unit circle', alpha=0.5)\n",
|
||||
"plt.plot(np.cos(thetas), np.sin(thetas), label=\"polygon\")\n",
|
||||
"plt.scatter(np.cos(thetas), np.sin(thetas), color=\"red\", label=\"vertices\")\n",
|
||||
"plt.plot(np.cos(thetas_inf), np.sin(thetas_inf), \"k--\", label=\"unit circle\", alpha=0.5)\n",
|
||||
"plt.legend()\n",
|
||||
"plt.title(f'Polygon with {n} sides')\n",
|
||||
"plt.title(f\"Polygon with {n} sides\")\n",
|
||||
"plt.grid(True)\n",
|
||||
"plt.xlim(-1.1, 1.1)\n",
|
||||
"plt.ylim(-1.1, 1.1)\n",
|
||||
@@ -207,58 +211,61 @@
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"from cProfile import label\n",
|
||||
"import numpy as np\n",
|
||||
"from scipy.optimize import minimize\n",
|
||||
"import matplotlib.pyplot as plt\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"def polygon_perimeter(theta, n):\n",
|
||||
" points = np.array([[np.cos(t), np.sin(t)] for t in theta])\n",
|
||||
" perimeter = 0\n",
|
||||
" for i in range(n-1):\n",
|
||||
" perimeter += np.sqrt(np.sum((points[i+1] - points[i])**2))\n",
|
||||
" perimeter += np.sqrt(np.sum((points[0] - points[-1])**2))\n",
|
||||
" for i in range(n - 1):\n",
|
||||
" perimeter += np.sqrt(np.sum((points[i + 1] - points[i]) ** 2))\n",
|
||||
" perimeter += np.sqrt(np.sum((points[0] - points[-1]) ** 2))\n",
|
||||
" return -perimeter\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"def constraint_increasing(theta):\n",
|
||||
" return np.array([theta[i+1] - theta[i] for i in range(len(theta)-1)])\n",
|
||||
" return np.array([theta[i + 1] - theta[i] for i in range(len(theta) - 1)])\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"def optimize_polygon(n):\n",
|
||||
" theta0 = generate_thetas(n)\n",
|
||||
" \n",
|
||||
"\n",
|
||||
" constraints = [\n",
|
||||
" {'type': 'ineq', 'fun': constraint_increasing},\n",
|
||||
" {'type': 'eq', 'fun': lambda x: x[0]},\n",
|
||||
" {'type': 'ineq', 'fun': lambda x: 2*np.pi - x[-1]}\n",
|
||||
" {\"type\": \"ineq\", \"fun\": constraint_increasing},\n",
|
||||
" {\"type\": \"eq\", \"fun\": lambda x: x[0]},\n",
|
||||
" {\"type\": \"ineq\", \"fun\": lambda x: 2 * np.pi - x[-1]},\n",
|
||||
" ]\n",
|
||||
"\n",
|
||||
" result = minimize(\n",
|
||||
" lambda x: polygon_perimeter(x, n),\n",
|
||||
" theta0,\n",
|
||||
" constraints=constraints,\n",
|
||||
" method='SLSQP'\n",
|
||||
" method=\"SLSQP\",\n",
|
||||
" )\n",
|
||||
" \n",
|
||||
"\n",
|
||||
" return result.x\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"def plot_perimeter(n):\n",
|
||||
" optimal_angles = optimize_polygon(n + 1)\n",
|
||||
" plt.figure(figsize=(7, 7))\n",
|
||||
" t = np.linspace(0, 2*np.pi, 100)\n",
|
||||
" plt.plot(np.cos(t), np.sin(t), 'k--', alpha=0.5, label='unit circle')\n",
|
||||
" t = np.linspace(0, 2 * np.pi, 100)\n",
|
||||
" plt.plot(np.cos(t), np.sin(t), \"k--\", alpha=0.5, label=\"unit circle\")\n",
|
||||
"\n",
|
||||
" points = np.array([[np.cos(t), np.sin(t)] for t in optimal_angles])\n",
|
||||
" points = np.vstack([points, points[0]])\n",
|
||||
" plt.plot(points[:, 0], points[:, 1], 'b-', linewidth=2, label='optimal polygon')\n",
|
||||
" plt.scatter(points[:-1, 0], points[:-1, 1], color='red', label='vertices')\n",
|
||||
" plt.plot(points[:, 0], points[:, 1], \"b-\", linewidth=2, label=\"optimal polygon\")\n",
|
||||
" plt.scatter(points[:-1, 0], points[:-1, 1], color=\"red\", label=\"vertices\")\n",
|
||||
"\n",
|
||||
" plt.legend()\n",
|
||||
" plt.axis('equal')\n",
|
||||
" plt.axis(\"equal\")\n",
|
||||
" plt.grid(True)\n",
|
||||
" plt.title(f'Optimal {n}-sided Polygon Inscribed in Unit Circle')\n",
|
||||
" plt.xlabel('x')\n",
|
||||
" plt.ylabel('y')\n",
|
||||
" plt.axis('equal')\n",
|
||||
" plt.title(f\"Optimal {n}-sided Polygon Inscribed in Unit Circle\")\n",
|
||||
" plt.xlabel(\"x\")\n",
|
||||
" plt.ylabel(\"y\")\n",
|
||||
" plt.axis(\"equal\")\n",
|
||||
" plt.grid(True)\n",
|
||||
" plt.show()\n",
|
||||
"\n",
|
||||
@@ -266,6 +273,7 @@
|
||||
" print(f\"Maximum perimeter: {-polygon_perimeter(optimal_angles, n)}\")\n",
|
||||
" print(f\"2 * pi = {2 * np.pi}\")\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"for n in np.arange(3, 10, 1):\n",
|
||||
" plot_perimeter(n)"
|
||||
]
|
||||
@@ -316,9 +324,11 @@
|
||||
"source": [
|
||||
"x0 = np.array([2, -1, 3, 0, -5])\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"def K(x):\n",
|
||||
" return np.minimum(x, 0)\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"print(f\"Initial point: {x0}\")\n",
|
||||
"print(f\"Projection of x0 onto K: {K(x0)}\")"
|
||||
]
|
||||
|
||||
Reference in New Issue
Block a user