| 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 |
import re |
| 27 |
|
| 28 |
from sympy import oo, limit, Symbol, Float, Rational, Wild, sqrt, S |
| 29 |
|
| 30 |
from .tablatexlib import convertir_en_latex, test_parentheses, nice_str |
| 31 |
from wxgeometrie.mathlib.solvers import ensemble_definition |
| 32 |
from wxgeometrie.mathlib.sympy_functions import solve |
| 33 |
from wxgeometrie.mathlib.intervalles import R, conversion_chaine_ensemble |
| 34 |
from wxgeometrie.mathlib.interprete import Interprete |
| 35 |
from wxgeometrie.mathlib.parsers import VAR |
| 36 |
from wxgeometrie.mathlib.custom_functions import round_afz |
| 37 |
from wxgeometrie import param |
| 38 |
|
| 39 |
|
| 40 |
def _auto_tabvar(chaine='', derivee=True, limites=True, decimales=3, approche=False, parse_only=False, stretch=True): |
| 41 |
"""Génère le code du tableau de variations d'une fonction à variable réelle. |
| 42 |
|
| 43 |
On suppose que la fonction est de classe C1 sur tout intervalle ouvert de son |
| 44 |
ensemble de définition. |
| 45 |
Par ailleurs, les zéros de sa dérivée doivent être calculables pour la librairie sympy. |
| 46 |
|
| 47 |
Pour les valeurs approchées, on conserve par défaut 3 chiffres après la virgule. |
| 48 |
En mettant `decimales=2`, on peut par exemple afficher seulement 2 chiffres |
| 49 |
après la virgule, etc. |
| 50 |
""" |
| 51 |
def nice_str2(x): |
| 52 |
if (isinstance(x, (float, Float)) and not isinstance(x, Rational) |
| 53 |
or approche and x not in (-oo, oo)): |
| 54 |
x = round_afz(x, decimales) |
| 55 |
return nice_str(x) |
| 56 |
|
| 57 |
|
| 58 |
. |
| 59 |
|
| 60 |
chaine_initiale = chaine |
| 61 |
|
| 62 |
ur |
| 63 |
if ' sur ' in chaine: |
| 64 |
chaine, ens_def = chaine.split(' sur ') |
| 65 |
if ens_def[0] == ens_def[-1] == "$": |
| 66 |
X |
| 67 |
ens_def = ens_def[1:-1] |
| 68 |
ens_def = conversion_chaine_ensemble(ens_def, utiliser_sympy = True) |
| 69 |
else: |
| 70 |
ens_def = R |
| 71 |
|
| 72 |
if chaine[0] == chaine[-1] == "$": |
| 73 |
X |
| 74 |
chaine = chaine[1:-1] |
| 75 |
|
| 76 |
|
| 77 |
if '=' in chaine: |
| 78 |
nom_fonction, chaine = chaine.split('=', 1) |
| 79 |
else: |
| 80 |
nom_fonction = 'f' |
| 81 |
nom_fonction = nom_fonction.strip() |
| 82 |
|
| 83 |
|
| 84 |
interprete = Interprete() |
| 85 |
interprete.evaluer(chaine) |
| 86 |
expr = interprete.ans() |
| 87 |
ée. |
| 88 |
a = Wild('a') |
| 89 |
expr = expr.replace(abs(a), sqrt(a**2)) |
| 90 |
|
| 91 |
). |
| 92 |
variables = expr.atoms(Symbol) |
| 93 |
de. |
| 94 |
. |
| 95 |
m = re.match(r'%s\((%s)\)' % (VAR, VAR), nom_fonction) |
| 96 |
if m is not None: |
| 97 |
variables.add(Symbol(str(m.group(1)))) |
| 98 |
if len(variables) > 1: |
| 99 |
|
| 100 |
). |
| 101 |
raise ValueError("Il y a plusieurs variables dans l'expression !") |
| 102 |
elif not variables: |
| 103 |
. |
| 104 |
variables = [Symbol('x')] |
| 105 |
var = variables.pop() |
| 106 |
|
| 107 |
f). |
| 108 |
ens_def &= ensemble_definition(expr, var) |
| 109 |
|
| 110 |
|
| 111 |
ée |
| 112 |
|
| 113 |
df = expr.diff(var) |
| 114 |
ens_def_df = ensemble_definition(df, var) & ens_def |
| 115 |
|
| 116 |
ant. |
| 117 |
s, |
| 118 |
e. |
| 119 |
. |
| 120 |
racines_df = sorted(solve(df, var), key=(lambda x: x.evalf(200))) |
| 121 |
|
| 122 |
|
| 123 |
ode |
| 124 |
|
| 125 |
|
| 126 |
sups = [S(intervalle.sup) for intervalle in ens_def] |
| 127 |
infs = [S(intervalle.inf) for intervalle in ens_def] |
| 128 |
|
| 129 |
def _code_val(x): |
| 130 |
"Génère le code correspondant à une valeur `x` remarquable." |
| 131 |
if x in ens_def: |
| 132 |
|
| 133 |
fx = nice_str2(expr.subs(var, x)) |
| 134 |
else: |
| 135 |
|
| 136 |
symb = ('|' if x not in (-oo, oo) else '') |
| 137 |
gauche = droite = '' |
| 138 |
if limites: |
| 139 |
e. |
| 140 |
if x in sups: |
| 141 |
gauche = nice_str2(limit(expr, var, x, dir = '-')) |
| 142 |
if x in infs: |
| 143 |
droite = nice_str2(limit(expr, var, x, dir = '+')) |
| 144 |
fx = '%s%s%s' % (gauche, symb, droite) |
| 145 |
|
| 146 |
. |
| 147 |
if x in (-oo, oo): |
| 148 |
dfx = '' |
| 149 |
elif x in ens_def_df: |
| 150 |
dfx = ('0' if abs(df.subs(var, x).evalf()) < param.tolerance else '') |
| 151 |
else: |
| 152 |
dfx = '|' |
| 153 |
if dfx and derivee: |
| 154 |
return '(%s;%s;%s)' % (nice_str2(x), fx, dfx) |
| 155 |
else: |
| 156 |
return '(%s;%s)' % (nice_str2(x), fx) |
| 157 |
|
| 158 |
def _code_inter(a, b): |
| 159 |
"Retourne les variations entre a et b." |
| 160 |
if a == -oo and b == +oo: |
| 161 |
a = b = 0 |
| 162 |
elif a == -oo: |
| 163 |
a = b - 1 |
| 164 |
elif b == +oo: |
| 165 |
b = a + 1 |
| 166 |
signe_df = df.subs(var, (a + b)/2).evalf(200) |
| 167 |
if signe_df > 0: |
| 168 |
symb = '<<' |
| 169 |
elif signe_df < 0: |
| 170 |
symb = '>>' |
| 171 |
else: |
| 172 |
symb = '==' |
| 173 |
return ' %s ' % symb |
| 174 |
|
| 175 |
|
| 176 |
code = '%s;%s:' % (var, nom_fonction) |
| 177 |
|
| 178 |
pos = 0 |
| 179 |
sup = None |
| 180 |
|
| 181 |
. |
| 182 |
x, |
| 183 |
lle |
| 184 |
ur un |
| 185 |
pas, |
| 186 |
|
| 187 |
e. |
| 188 |
for intervalle in ens_def_df.intervalles: |
| 189 |
|
| 190 |
inf = S(intervalle.inf) |
| 191 |
inf_approx = inf.evalf(200) |
| 192 |
if inf != sup: |
| 193 |
if sup is not None: |
| 194 |
code += ' XX ' |
| 195 |
code += _code_val(inf) |
| 196 |
sup = S(intervalle.sup) |
| 197 |
sup_approx = sup.evalf(200) |
| 198 |
ré. |
| 199 |
while pos < len(racines_df): |
| 200 |
racine = racines_df[pos] |
| 201 |
if racine.evalf(200) > inf_approx: |
| 202 |
break |
| 203 |
pos += 1 |
| 204 |
e |
| 205 |
on. |
| 206 |
while pos < len(racines_df): |
| 207 |
racine = racines_df[pos] |
| 208 |
if racine.evalf(200) >= sup_approx: |
| 209 |
break |
| 210 |
pos += 1 |
| 211 |
code += _code_inter(inf, racine) |
| 212 |
code += _code_val(racine) |
| 213 |
inf = racine |
| 214 |
code += _code_inter(inf, sup) |
| 215 |
code += _code_val(sup) |
| 216 |
|
| 217 |
|
| 218 |
if param.debug and param.verbose: |
| 219 |
print('Code TABVar:', code) |
| 220 |
if parse_only: |
| 221 |
return code |
| 222 |
return tabvar(code, derivee=derivee, stretch=stretch) + '% ' + chaine_initiale + '\n' |
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
def tabvar(chaine="", derivee=True, limites=True, decimales=3, approche=False, stretch=True): |
| 229 |
"""Indiquer les variations de la fonction. |
| 230 |
|
| 231 |
Exemples : |
| 232 |
f: (-oo;3) << (1;2;0) << (3;+oo|-oo) << (5;2) >> (+oo;-oo) |
| 233 |
x;\\sqrt{x};(\\sqrt{x})': 0;0;| << +oo;+oo""" |
| 234 |
|
| 235 |
chaine_originale = chaine = chaine.strip() |
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
if not ':' in chaine and not '>>' in chaine and not '==' in chaine and not '<<' in chaine: |
| 240 |
return _auto_tabvar(chaine, derivee=derivee, limites=limites, decimales=decimales, approche=approche, stretch=stretch) |
| 241 |
|
| 242 |
chaine = chaine.replace("-oo", "-\\infty").replace("+oo", "+\\infty") |
| 243 |
|
| 244 |
liste = chaine.split(":", 1) |
| 245 |
|
| 246 |
if len(liste) == 1 or len(liste[0].strip()) == 0: |
| 247 |
ligne_variable = "x" |
| 248 |
ligne_derivee = "f'(x)" |
| 249 |
ligne_fonction = "f" |
| 250 |
|
| 251 |
else: |
| 252 |
legende, chaine = liste |
| 253 |
legende = legende.split(";") |
| 254 |
|
| 255 |
if len(legende) > 1: |
| 256 |
ligne_variable = legende[0].strip() |
| 257 |
ligne_fonction = legende[1].strip() |
| 258 |
|
| 259 |
if len(legende) > 2: |
| 260 |
ligne_derivee = legende[2].strip() |
| 261 |
else: |
| 262 |
deb = ligne_fonction.find("(") |
| 263 |
if deb == -1: |
| 264 |
ligne_derivee = ligne_fonction[:].strip() + "'(" + ligne_variable + ")" |
| 265 |
else: |
| 266 |
ligne_derivee = ligne_fonction[:deb].strip() + "'(" + ligne_variable + ")" |
| 267 |
|
| 268 |
else: |
| 269 |
ligne_fonction = legende[0].strip() |
| 270 |
deb = ligne_fonction.find("(") |
| 271 |
fin = ligne_fonction.find(")") |
| 272 |
if deb == -1: |
| 273 |
ligne_variable = "x" |
| 274 |
ligne_derivee = ligne_fonction + "'(x)" |
| 275 |
else: |
| 276 |
ligne_variable = ligne_fonction[deb+1:fin].strip() |
| 277 |
ligne_derivee = ligne_fonction[:deb].strip() + "'(" + ligne_variable + ")" |
| 278 |
|
| 279 |
|
| 280 |
.) |
| •281 |
ligne_variable = r'\,\,%s\,\,' % ligne_variable |
| 282 |
|
| 283 |
|
| 284 |
)), |
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
sequence = re.split(r"(>>|<<|==|\|\||XX)", chaine.strip()) |
| 290 |
|
| 291 |
if not sequence[0]: |
| 292 |
|
| 293 |
sequence[0] = "-\\infty;" |
| 294 |
if not sequence[-1]: |
| 295 |
o |
| 296 |
sequence[-1] = "+\\infty;" |
| 297 |
|
| 298 |
def formater(chaine): |
| 299 |
chaine = chaine.strip() |
| 300 |
if chaine not in ("<<", ">>", "==", '||', 'XX'): |
| 301 |
té. |
| 302 |
;0 |
| 303 |
if chaine[0] == '(' and chaine[-1] == ')' and test_parentheses(chaine[1:-1]): |
| 304 |
chaine = chaine[1:-1] |
| 305 |
if ";" not in chaine: |
| 306 |
chaine += ";" |
| 307 |
return chaine |
| 308 |
|
| 309 |
sequence = [formater(elt) for elt in sequence] |
| 310 |
|
| 311 |
|
| 312 |
niveaux = [] |
| 313 |
|
| 314 |
niveau = niveau_minimum = niveau_maximum = 0 |
| 315 |
for elt in sequence: |
| 316 |
if (";" in elt and "|" in elt.split(";")[1]) \ |
| 317 |
or elt in ('||', 'XX'): |
| 318 |
niveaux.append((niveau_minimum, niveau_maximum)) |
| 319 |
niveau = niveau_minimum = niveau_maximum = 0 |
| 320 |
else: |
| 321 |
if elt == "<<": |
| 322 |
niveau += 1 |
| 323 |
elif elt == ">>": |
| 324 |
niveau -= 1 |
| 325 |
if niveau < niveau_minimum: |
| 326 |
niveau_minimum = niveau |
| 327 |
if niveau > niveau_maximum: |
| 328 |
niveau_maximum = niveau |
| 329 |
niveaux.append((niveau_minimum, niveau_maximum)) |
| 330 |
ecart_maximal = max(val[1] - val[0] for val in niveaux) |
| 331 |
|
| 332 |
|
| 333 |
, |
| 334 |
, |
| 335 |
x). |
| 336 |
if ecart_maximal%2: |
| 337 |
ligne_fonction = "\\niveau{" + str((ecart_maximal+2)//2) +"}{" + str(ecart_maximal+1) + "}\\raisebox{0.5em}{$" + ligne_fonction + "$}" |
| 338 |
else: |
| 339 |
ligne_fonction = "\\niveau{" + str((ecart_maximal+2)//2) +"}{" + str(ecart_maximal+1) + "}" + ligne_fonction |
| 340 |
|
| 341 |
|
| 342 |
colonnes = 'C|' s |
| 343 |
portion = 0 |
| 344 |
debut = True |
| 345 |
|
| 346 |
def en_latex(chaine, stretch=False): |
| 347 |
if stretch: |
| 348 |
return '\\TVstretch{%s}' % convertir_en_latex(chaine)[1:-1] |
| 349 |
else: |
| 350 |
return convertir_en_latex(chaine)[1:-1] |
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
for i in range(len(sequence)): |
| 355 |
|
| 356 |
n = max(len(ligne_variable), len(ligne_derivee), len(ligne_fonction)) |
| 357 |
ligne_variable = ligne_variable.ljust(n) |
| 358 |
ligne_derivee = ligne_derivee.ljust(n) |
| 359 |
ligne_fonction = ligne_fonction.ljust(n) |
| 360 |
|
| 361 |
ligne_variable += "&" |
| 362 |
if debut: |
| 363 |
debut = False |
| 364 |
ligne_fonction += "&\\niveau{" + str(1 - niveaux[portion][0]) +"}{" + str(ecart_maximal + 1) + "}" |
| 365 |
else: |
| 366 |
ligne_fonction += "&" |
| 367 |
ligne_derivee += "&" |
| 368 |
|
| 369 |
elt = sequence[i] |
| 370 |
if elt in (">>", "<<", "==", "||", "XX"): |
| 371 |
colonnes += ('U' if elt in ('||', 'XX') else 'C') |
| 372 |
|
| 373 |
if elt == "<<": |
| 374 |
ligne_derivee += "+" |
| 375 |
ligne_fonction += r"\croit" |
| 376 |
elif elt == ">>": |
| 377 |
ligne_derivee += "-" |
| 378 |
ligne_fonction += r"\decroit" |
| 379 |
elif elt == "==": |
| 380 |
ligne_derivee += "0" |
| 381 |
ligne_fonction += r"\constante" |
| 382 |
else: |
| 383 |
ligne_variable += r"\hspace*{15mm}" |
| 384 |
|
| 385 |
else: |
| 386 |
valeurs = elt.split(";") |
| 387 |
|
| 388 |
|
| 389 |
|
| 390 |
|
| 391 |
|
| 392 |
r. |
| 393 |
|
| 394 |
|
| 395 |
). |
| 396 |
largeur = max((3 if "|" in val else 1) for val in valeurs) |
| 397 |
|
| 398 |
if largeur == 3: |
| 399 |
ligne_variable += " &" + en_latex(valeurs[0], stretch=stretch) + "& " |
| 400 |
|
| 401 |
vals_fonc = valeurs[1].split("|") |
| 402 |
if len(vals_fonc) == 2: |
| 403 |
portion += 1 |
| 404 |
ligne_fonction += en_latex(vals_fonc[0], stretch=stretch) + "&\\dbarre&" \ |
| 405 |
+ "\\niveau{" + str(1 - niveaux[portion][0]) +"}{" + str(ecart_maximal + 1) + "}" \ |
| 406 |
+ en_latex(vals_fonc[1], stretch=stretch) |
| 407 |
else: |
| 408 |
ligne_fonction += " &" + en_latex(vals_fonc[0], stretch=stretch) + "&" |
| 409 |
|
| 410 |
if len(valeurs) < 3: |
| 411 |
valeurs.append("|") |
| 412 |
|
| 413 |
vals_deriv = valeurs[2].split("|") |
| 414 |
if len(vals_deriv) == 2: |
| 415 |
ligne_derivee += en_latex(vals_deriv[0]) + "&\\dbarre&" + en_latex(vals_deriv[1]) |
| 416 |
else: |
| 417 |
ligne_derivee += " &" + en_latex(vals_deriv[0]) + "&" |
| 418 |
|
| 419 |
else: |
| 420 |
ligne_variable += en_latex(valeurs[0]) |
| 421 |
ligne_fonction += en_latex(valeurs[1]) |
| 422 |
|
| 423 |
if len(valeurs) < 3: |
| 424 |
if 0 < i < len(sequence)-1 and sequence[i-1] != sequence[i+1]: |
| 425 |
|
| 426 |
c. |
| 427 |
valeurs.append("0") |
| 428 |
else: |
| 429 |
valeurs.append(" ") |
| 430 |
ligne_derivee += en_latex(valeurs[2]) |
| 431 |
|
| 432 |
colonnes += largeur*'C' |
| 433 |
|
| 434 |
|
| 435 |
ion |
| 436 |
) |
| 437 |
, |
| 438 |
. |
| 439 |
code = [r"\setlength{\TVextraheight}{\baselineskip}", |
| 440 |
r"\[\begin{tabvar}{|%s|}" % colonnes, |
| 441 |
r"\hline", |
| 442 |
ligne_variable + r"\\"] |
| 443 |
|
| 444 |
if derivee: |
| 445 |
code.extend([r"\hline", |
| 446 |
ligne_derivee + r"\\"]) |
| 447 |
|
| 448 |
code.extend([r"\hline", |
| 449 |
ligne_fonction + r"\\"]) |
| 450 |
|
| 451 |
code.extend([r"\hline", |
| 452 |
r"\end{tabvar}\]", |
| 453 |
"% " + chaine_originale, |
| 454 |
""]) |
| 455 |
|
| 456 |
return "\n".join(code) |