| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
from itertools import chain |
| 25 |
from numpy import isnan, isinf, sign, arange, inf, append |
| 26 |
|
| 27 |
from sympy import oo |
| 28 |
|
| 29 |
from .objet import Objet, Argument, Ref |
| 30 |
from .points import Point_generique |
| 31 |
from .contexte import contexte |
| 32 |
from .routines import distance_segment |
| 33 |
from .. import param |
| 34 |
from ..pylib import print_error |
| 35 |
|
| 36 |
|
| 37 |
def inf_or_nan(x): |
| 38 |
return isinf(x) or isnan(x) |
| 39 |
|
| 40 |
|
| 41 |
class Courbe_generique(Objet): |
| 42 |
"""Classe mère de toutes les courbes.""" |
| 43 |
|
| 44 |
_affichage_depend_de_la_fenetre = True |
| 45 |
_style_defaut = param.courbes |
| 46 |
|
| 47 |
def __init__(self, **styles): |
| 48 |
Objet.__init__(self, **styles) |
| 49 |
|
| 50 |
def _distance_inf(self, x, y, d): |
| 51 |
if len(self.xarray) and len(self.yarray): |
| 52 |
u, v = self.feuille.coo2pix(self.xarray, self.yarray) |
| 53 |
u -= x |
| 54 |
v -= y |
| 55 |
m = min(u*u + v*v) |
| 56 |
return m < d**2 |
| 57 |
return False |
| 58 |
|
| 59 |
def _espace_vital(self): |
| 60 |
if len(self.xarray) and len(self.yarray): |
| 61 |
return (min(self.xarray), max(self.xarray), min(self.yarray), max(self.yarray)) |
| 62 |
|
| 63 |
@property |
| 64 |
def xarray(self): |
| 65 |
if self._Objet__figure_perimee: |
| 66 |
self._creer_figure() |
| 67 |
return self._xarray |
| 68 |
|
| 69 |
@property |
| 70 |
def yarray(self): |
| 71 |
if self._Objet__figure_perimee: |
| 72 |
self._creer_figure() |
| 73 |
return self._yarray |
| 74 |
|
| 75 |
|
| 76 |
class Courbe(Courbe_generique): |
| 77 |
"""Une courbe de fonction. |
| 78 |
|
| 79 |
L'expression doit être donnée en fonction de 'x'. |
| 80 |
Exemple : '2x^2-1/x+1' |
| 81 |
""" |
| 82 |
_prefixe_nom = "C" |
| 83 |
__fonction = fonction = Argument("Fonction") |
| 84 |
|
| 85 |
def __new__(cls, *args, **kw): |
| 86 |
if args and isinstance(args[0], Point_generique): |
| 87 |
from .interpolations import Interpolation_lineaire, scipy_found,\ |
| 88 |
Interpolation_polynomiale_par_morceaux |
| 89 |
cls = (Interpolation_polynomiale_par_morceaux if scipy_found |
| 90 |
else Interpolation_lineaire) |
| 91 |
return cls(*args, **kw) |
| 92 |
return object.__new__(cls) |
| 93 |
|
| 94 |
def __init__(self, fonction, **styles): |
| 95 |
self.__fonction = fonction = Ref(fonction) |
| 96 |
Courbe_generique.__init__(self, **styles) |
| 97 |
|
| 98 |
def _creer_figure(self): |
| 99 |
|
| 100 |
self._representation = [] |
| 101 |
fenetre = self.feuille.fenetre_reellement_affichee() |
| 102 |
pas = self.canvas.pas() |
| 103 |
self._xarray = () |
| 104 |
self._yarray = () |
| 105 |
ancien_intervalle = None |
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
for fonction, union, e_cach in zip(self.__fonction._Fonction__fonctions, |
| 111 |
self.__fonction._Fonction__unions, |
| 112 |
self.__fonction.style('extremites_cachees')): |
| 113 |
for intervalle in union.intervalles: |
| 114 |
x = intervalle.asarray(fenetre[0], fenetre[1], pas)[0] |
| 115 |
if len(x): |
| 116 |
) |
| 117 |
y = fonction(x) |
| 118 |
|
| 119 |
x0 = x[0] |
| 120 |
xN = x[-1] |
| 121 |
y0 = y[0] |
| 122 |
yN = y[-1] |
| 123 |
|
| 124 |
|
| 125 |
x, y = self.supprimer_valeurs_extremes(x, y, fonction) |
| 126 |
|
| 127 |
self._representation.append(self.rendu.ligne(x, y, |
| 128 |
color = self.style("couleur"), |
| 129 |
linestyle = self.style("style"), |
| 130 |
linewidth = self.style("epaisseur"), |
| 131 |
zorder = self.style("niveau"), |
| 132 |
)) |
| 133 |
, |
| 134 |
|
| 135 |
self._xarray = append(self._xarray, x) |
| 136 |
self._yarray = append(self._yarray, y) |
| 137 |
|
| 138 |
if fenetre[0] < intervalle.inf < fenetre[1]: |
| 139 |
if ancien_intervalle is None: |
| 140 |
self._creer_debut_morceau(x, y, intervalle, e_cach) |
| 141 |
else: |
| 142 |
print(intervalle, y[0], abs(y[0] - ancien_y[-1]), abs(x[0] - ancien_x[-1]), contexte['tolerance'] , pas) |
| 143 |
fusion = abs(x0 - ancien_xN) < contexte['tolerance'] \ |
| 144 |
and (abs(y0 - ancien_yN) < contexte['tolerance'] or (isnan(y0) and isnan(ancien_yN))) |
| 145 |
if fusion: |
| 146 |
|
| 147 |
print('Fusion', y0) |
| 148 |
if isnan(y0): |
| 149 |
print('Fusion avancée') |
| 150 |
for i in range(10, 70, 10): |
| 151 |
try: |
| 152 |
val1 = ancienne_fonction(ancien_xN - 8**(-i)) |
| 153 |
val2 = ancienne_fonction(x0 + 8**(-i)) |
| 154 |
if abs(val1 - val2) < contexte['tolerance']: |
| 155 |
self._append_point(x0, val1, plein = False) |
| 156 |
break |
| 157 |
except (ZeroDivisionError, ValueError): |
| 158 |
print_error() |
| 159 |
fusion = False |
| 160 |
break |
| 161 |
else: |
| 162 |
fusion = False |
| 163 |
elif not(ancien_intervalle.sup_inclus or intervalle.inf_inclus): |
| 164 |
print('Fusion classique') |
| 165 |
self._append_point(x[0], y[0], plein = False) |
| 166 |
|
| 167 |
if not fusion: |
| 168 |
self._creer_fin_morceau(ancien_x, ancien_y, ancien_intervalle, e_cach) |
| 169 |
self._creer_debut_morceau(x, y, intervalle, e_cach) |
| 170 |
|
| 171 |
ancien_x = x |
| 172 |
ancien_y = y |
| 173 |
ancien_xN = xN |
| 174 |
ancien_yN = yN |
| 175 |
ancien_intervalle = intervalle |
| 176 |
ancienne_fonction = fonction |
| 177 |
if ancien_intervalle is not None and fenetre[0] < ancien_intervalle.sup < fenetre[1]: |
| 178 |
self._creer_fin_morceau(ancien_x, ancien_y, ancien_intervalle, e_cach) |
| 179 |
|
| 180 |
def _creer_debut_morceau(self, x, y, intervalle, e_cach): |
| 181 |
if len(y) == 0: |
| 182 |
return |
| 183 |
elif len(y) == 1: |
| 184 |
if not inf_or_nan(y[0]): |
| 185 |
self._append_point(x[0], y[0]) |
| 186 |
return |
| 187 |
if self._extremite_cachee(x[0], e_cach): |
| 188 |
return |
| 189 |
if not(inf_or_nan(y[0]) or inf_or_nan(y[1])): |
| 190 |
if intervalle.inf_inclus: |
| 191 |
self._append_point(x[0], y[0]) |
| 192 |
else: |
| 193 |
vec = x[1] - x[0], y[1] - y[0] |
| 194 |
self._append_arc(x[0], y[0], vec) |
| 195 |
3 |
| 196 |
elif isnan(y[0]) and not (isnan(y[1]) or isnan(y[2])) : |
| 197 |
if not intervalle.inf_inclus: |
| 198 |
vec = x[2] - x[1], y[2] - y[1] |
| 199 |
self._append_arc(x[1], y[1], vec) |
| 200 |
|
| 201 |
def _creer_fin_morceau(self, x, y, intervalle, e_cach): |
| 202 |
if len(y) <= 1: |
| 203 |
return |
| 204 |
if self._extremite_cachee(x[-1], e_cach): |
| 205 |
return |
| 206 |
if not(inf_or_nan(y[-1]) or inf_or_nan(y[-2])): |
| 207 |
if intervalle.sup_inclus: |
| 208 |
self._append_point(x[-1], y[-1]) |
| 209 |
else: |
| 210 |
vec = x[-2] - x[-1], y[-2] - y[-1] |
| 211 |
self._append_arc(x[-1], y[-1], vec) |
| 212 |
3 |
| 213 |
elif isnan(y[-1]) and not (isnan(y[-2]) or isnan(y[-3])) : |
| 214 |
if not intervalle.inf_inclus: |
| 215 |
vec = x[-3] - x[-2], y[-3] - y[-2] |
| 216 |
self._append_arc(x[-2], y[-2], vec) |
| 217 |
|
| 218 |
def _extremite_cachee(self, val, e_cach): |
| 219 |
"Renvoie True si une variable de `e_cach` vaut `val`." |
| 220 |
return val in [v.val for v in e_cach] |
| 221 |
|
| 222 |
def _append_arc(self, x0, y0, vec): |
| 223 |
if self.style("extremites"): |
| 224 |
self._representation.append(self.rendu.arc(x0, y0, vec, |
| 225 |
taille=self.style('taille_extremites'), color=self.style("couleur"), |
| 226 |
linewidth=self.style("epaisseur"))) |
| 227 |
|
| 228 |
def _append_point(self, x0, y0, plein = True): |
| 229 |
if self.style("extremites"): |
| 230 |
self._representation.append(self.rendu.point(x0, y0, plein=plein, |
| 231 |
taille=.8*self.style('taille_extremites'), color=self.style("couleur"), |
| 232 |
markeredgewidth=self.style("epaisseur"))) |
| 233 |
|
| 234 |
|
| 235 |
def _supprimer_valeurs_extremes(self, x, y, fonction, i, j): |
| 236 |
"""Lorsque les valeurs aux bornes sont indéterminées (NaN), infinies (+/-Inf) |
| 237 |
ou très éloignées de zéro (2e200), on cherche à les convertir en une valeur |
| 238 |
raisonnable pour la fenêtre d'affichage. |
| 239 |
|
| 240 |
La principale difficulté est de déterminer **numériquement** la limite probable. |
| 241 |
|
| 242 |
On commence par regarder la valeur calculée par numpy à la borne considérée : |
| 243 |
|
| 244 |
* Si la valeur est +/-Inf, il faut étudier son signe. |
| 245 |
En effet, numpy ne peut généralement par faire la différence entre des calculs |
| 246 |
du type 1/0+ et 1/0- (ex: 1/(x-3) en 3+ et 3-). |
| 247 |
L'idée est la suivante : si les valeurs diminuent en se rapprochant de la borne, |
| 248 |
alors la limite est -Inf. De même, si elles augmentent, la limite est +Inf. |
| 249 |
On retourne alors une valeur en dehors de la fenêtre d'affichage, qui simule l'infini. |
| 250 |
|
| 251 |
* Si le résultat est un nombre très éloigné de zéro, on le tronque tout en restant |
| 252 |
en dehors de la fenêtre d'affichage, de manière à simuler l'infini. |
| 253 |
En effet, le traceur de matplotlib réagit mal aux valeurs "extrêmes". |
| 254 |
|
| 255 |
* Enfin si le résultat est de type NaN, on s'éloigne légèrement (puis de plus en plus vite) |
| 256 |
de la borne, et on reitère, dans une limite de 20 itérations. |
| 257 |
""" |
| 258 |
x0 = x[i]; y0 = y[i] |
| 259 |
x1 = x[j]; y1 = y[j] |
| 260 |
k = 2**arange(-20., 0.) |
| 261 |
entre = k*(x1 - x0) + x0 |
| 262 |
xk = chain([x0], entre, [x1]) |
| 263 |
yk = chain([y0], fonction(entre), [y1]) |
| 264 |
y_finis = [] s |
| 265 |
infini = False |
| 266 |
xi_infini = None |
| 267 |
|
| 268 |
for xi, yi in zip(xk, yk): |
| 269 |
if infini: |
| 270 |
if not inf_or_nan(yi): |
| 271 |
y_finis.append(yi) |
| 272 |
if len(y_finis) == 2: |
| 273 |
x0 = xi_infini |
| 274 |
y0 = self._rogner_valeur(sign(y_finis[0] - y_finis[1])*inf) |
| 275 |
break |
| 276 |
else: |
| 277 |
if isinf(yi): |
| 278 |
infini = True |
| 279 |
xi_infini = xi |
| 280 |
elif not isnan(yi): |
| 281 |
x0 = xi |
| 282 |
y0 = self._rogner_valeur(yi) |
| 283 |
break |
| 284 |
x[i] = x0 |
| 285 |
y[i] = y0 |
| 286 |
return x, y |
| 287 |
|
| 288 |
|
| 289 |
def _rogner_valeur(self, y0): |
| 290 |
"Remplace -inf et +inf par des valeurs numériques dépassant la fenêtre." |
| 291 |
if isnan(y0): |
| 292 |
return y0 |
| 293 |
xmin, xmax, ymin, ymax = self.feuille.fenetre |
| 294 |
decalage = 100*(ymax - ymin) |
| 295 |
if isinf(y0): |
| 296 |
return (ymin - decalage) if (y0 < 0) else (ymax + decalage) |
| 297 |
|
| 298 |
return max(min(y0, ymax + decalage), ymin - decalage) |
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
def supprimer_valeurs_extremes(self, x, y, fonction): |
| 303 |
x, y = self._supprimer_valeurs_extremes(x, y, fonction, 0, 1) |
| 304 |
x, y = self._supprimer_valeurs_extremes(x, y, fonction, -1, -2) |
| 305 |
return x, y |
| 306 |
|
| 307 |
|
| 308 |
@property |
| 309 |
def xmin(self): |
| 310 |
return self.__fonction._Fonction__unions[0].intervalles[0].inf |
| 311 |
|
| 312 |
@property |
| 313 |
def xmax(self): |
| 314 |
return self.__fonction._Fonction__unions[-1].intervalles[-1].sup |
| 315 |
|
| 316 |
def _espace_vital(self): |
| 317 |
xmin = self.xmin |
| 318 |
xmax = self.xmax |
| 319 |
if xmin == -oo: |
| 320 |
xmin = None |
| 321 |
if xmax == oo: |
| 322 |
xmax = None |
| 323 |
return (xmin, xmax, min(self.yarray), max(self.yarray)) |
| 324 |
|
| 325 |
|
| 326 |
def _distance_inf(self, x, y, d): |
| 327 |
P = x, y |
| 328 |
xm = self.feuille.pix2coo(x - d, y)[0] |
| 329 |
xM = self.feuille.pix2coo(x + d, y)[0] |
| 330 |
xarray = self.xarray |
| 331 |
filtre = (xm < xarray) & (xarray < xM) |
| 332 |
xa, ya = self.feuille.coo2pix(xarray[filtre], self.yarray[filtre]) |
| 333 |
A = None |
| 334 |
for x, y in zip(xa, ya): |
| 335 |
B = A |
| 336 |
A = x, y |
| 337 |
if distance_segment(P, A, B, d): |
| 338 |
return True |
| 339 |
return False |
| 340 |
|
| 341 |
|
| 342 |
@staticmethod |
| 343 |
def _convertir(objet): |
| 344 |
"Convertit un objet en fonction." |
| 345 |
return NotImplemented |
| 346 |
|
| 347 |
def _update(self, objet): |
| 348 |
if not isinstance(objet, Courbe): |
| 349 |
objet = self._convertir(objet) |
| 350 |
if isinstance(objet, Courbe): |
| 351 |
self.fonction = objet.fonction |
| 352 |
else: |
| 353 |
raise TypeError("L'objet n'est pas une courbe.") |