-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
60 lines (47 loc) · 1.21 KB
/
utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
""" Fonctions génériques
"""
from decimal import Decimal
from matplotlib import cm
from matplotlib.ticker import LinearLocator
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
import numpy as np
def dicothomie(f, min, max, valeur):
""" Calcule la valeur de x pour laquelle f(x) = valeur
f est une fonction continue et croissante.
"""
x = (min + max) // 2
while max - min > 1:
if f(x) < valeur:
max = x
else:
min = x
# print(f"min = {min}, max = {max}")
x = (min + max) // 2
return x, f(x)
def plot(x, y, x_name, y_name, title):
""" Plot the results
"""
plt.plot(x, y, "b:o")
plt.xlabel(x_name)
plt.ylabel(y_name)
plt.title(title)
plt.show()
def plot3d(x, y, z, x_name, y_name, z_name, title):
""" Plot the results as a surface plot
"""
fig, ax = plt.subplots(subplot_kw={"projection": "3d"})
# Make data.
X = np.unique(np.array(y))
Y = np.unique(np.array(x))
X, Y = np.meshgrid(X, Y)
# Z, tableau en 2D, de taille (x, y)
Z = np.array(z).reshape(X.shape)
# Plot the surface.
surf = ax.plot_surface(X, Y, Z, cmap=cm.coolwarm,
linewidth=0, antialiased=False)
plt.xlabel(y_name)
plt.ylabel(x_name)
ax.set_zlabel(z_name)
plt.title(title)
plt.show()