| 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 |
from functools import partial |
| 26 |
|
| 27 |
from sympy import (exp, ln, tan, pi, Symbol, oo, solve, Wild, sympify, |
| 28 |
Add, Mul, sqrt, Abs, preorder_traversal, Lambda, Dummy, |
| 29 |
count_ops, sin, cos, S, |
| 30 |
) |
| 31 |
from .intervalles import Intervalle, vide, Union, R |
| 32 |
from .internal_functions import extract_var, is_pos, is_var, is_neg |
| 33 |
from .custom_functions import floats2rationals |
| 34 |
from ..pylib import split_around_parenthesis |
| 35 |
from .sympy_functions import factor, solve as solve_ |
| 36 |
from .. import param |
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
def nul(expression, variable=None, intervalle=True, ensemble='R'): |
| 44 |
"""Retourne l'ensemble sur lequel une expression à variable réelle est nulle. |
| 45 |
|
| 46 |
:param expression: une expression mathématique |
| 47 |
:type expression: string |
| 48 |
:param variable: un nom de variable |
| 49 |
:type variable: string |
| 50 |
:param intervalle: returne le résultat sous forme d'intervalle |
| 51 |
:type intervalle: bool |
| 52 |
:param ensemble: dans quel ensemble l'équation doit être résolue ('R' ou 'C') |
| 53 |
:type ensemble: string |
| 54 |
|
| 55 |
.. note:: Si la variable n'est pas précisée, elle est déduite de l'expression. |
| 56 |
(S'il y a plusieurs variables, une des variables est choisie au hasard.) |
| 57 |
|
| 58 |
""" |
| 59 |
if ensemble == 'C': |
| 60 |
intervalle = False |
| 61 |
if variable is None: |
| 62 |
variable = extract_var(expression) |
| 63 |
expression = factor(expression, variable, ensemble=ensemble, decomposer_entiers=False) |
| 64 |
if expression.is_Mul: |
| 65 |
facteurs = expression.args |
| 66 |
else: |
| 67 |
facteurs = [expression] |
| 68 |
|
| 69 |
solutions = (vide if intervalle else set()) |
| 70 |
|
| 71 |
for facteur in facteurs: |
| 72 |
liste_solutions = solve_(facteur, variable, ensemble=ensemble) |
| 73 |
if intervalle: |
| 74 |
solutions += Union(*liste_solutions) |
| 75 |
else: |
| 76 |
solutions.update(liste_solutions) |
| 77 |
return solutions |
| 78 |
|
| 79 |
|
| 80 |
def ensemble_definition(expression, variable = None): |
| 81 |
|
| 82 |
if variable is None: |
| 83 |
variable = extract_var(expression) |
| 84 |
ens_def = R |
| 85 |
if hasattr(expression, "is_Add") and expression.is_Add: |
| 86 |
for terme in expression.args: |
| 87 |
ens_def *= ensemble_definition(terme, variable) |
| 88 |
return ens_def |
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
if hasattr(expression, "subs"): |
| 95 |
old_variable = variable |
| 96 |
variable = Symbol("_tmp",real=True) |
| 97 |
expression = expression.subs({old_variable:variable}) |
| 98 |
if hasattr(expression, "is_Pow") and expression.is_Pow: |
| 99 |
base, p = expression.as_base_exp() |
| 100 |
if p.is_integer: |
| 101 |
if p >= 0: |
| 102 |
return ensemble_definition(base, variable) |
| 103 |
else: |
| 104 |
return ensemble_definition(base, variable) - nul(base, variable) |
| 105 |
elif p.is_rational: |
| 106 |
n, d = p.as_numer_denom() |
| 107 |
if abs(n) == 1 and not d.is_even: |
| 108 |
if p >= 0: |
| 109 |
return ensemble_definition(base, variable) |
| 110 |
else: |
| 111 |
return ensemble_definition(base, variable) - nul(base, variable) |
| 112 |
else: |
| 113 |
if p >= 0: |
| 114 |
return positif(base, variable) |
| 115 |
else: |
| 116 |
return positif(base, variable, strict = True) |
| 117 |
else: |
| 118 |
if p >= 0: |
| 119 |
return positif(base, variable) |
| 120 |
else: |
| 121 |
return positif(base, variable, strict = True) |
| 122 |
elif hasattr(expression, "is_Mul") and expression.is_Mul: |
| 123 |
for facteur in expression.args: |
| 124 |
ens_def *= ensemble_definition(facteur, variable) |
| 125 |
return ens_def |
| 126 |
elif isinstance(expression, tan): |
| 127 |
arg = expression.args[0] |
| 128 |
return positif(arg + pi/2, variable, strict = True)*positif(pi/2-arg, variable, strict = True) |
| 129 |
elif isinstance(expression, ln): |
| 130 |
arg = expression.args[0] |
| 131 |
return positif(arg, variable, strict = True) |
| 132 |
return ens_def |
| 133 |
|
| 134 |
|
| 135 |
def periode(expression, variable=None): |
| 136 |
"""Retourne la période minimale de la fonction. |
| 137 |
|
| 138 |
Si la fonction n'est pas périodique (ou si elle n'est pas encore supportée), |
| 139 |
retourne +oo. |
| 140 |
Retourne 0 si la fonction est constante. |
| 141 |
""" |
| 142 |
expression = sympify(expression) |
| 143 |
if variable is None: |
| 144 |
variable = extract_var(expression) |
| 145 |
a = Wild('a', exclude=[0, variable]) |
| 146 |
b = Wild('b', exclude=[variable]) |
| 147 |
if isinstance(expression, (cos, sin)): |
| 148 |
match = expression.args[0].match(a*variable + b) |
| 149 |
if match: |
| 150 |
return 2*pi/match[a] |
| 151 |
else: |
| 152 |
e, |
| 153 |
|
| 154 |
return oo |
| 155 |
elif isinstance(expression, tan): |
| 156 |
match = expression.args[0].match(a*variable + b) |
| 157 |
if match: |
| 158 |
return pi/match[a] |
| 159 |
else: |
| 160 |
e, |
| 161 |
|
| 162 |
return oo |
| 163 |
elif expression.args: |
| 164 |
T = 0 |
| 165 |
for arg in expression.args: |
| 166 |
T_ = periode(arg) |
| 167 |
if not T: |
| 168 |
T = T_ |
| 169 |
if T_ is oo: |
| 170 |
return oo |
| 171 |
elif T_: |
| 172 |
ratio = T/T_ |
| 173 |
. |
| 174 |
if ratio.is_Rational: |
| 175 |
T = ratio.q*T |
| 176 |
else: |
| 177 |
|
| 178 |
. |
| 179 |
return oo |
| 180 |
return T |
| 181 |
else: |
| 182 |
if expression.has(variable): |
| 183 |
. |
| 184 |
return oo |
| 185 |
else: |
| 186 |
|
| 187 |
return 0 |
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
def positif(expression, variable=None, strict=False, _niveau=0, _changement_variable=None): |
| 194 |
"""Retourne l'ensemble sur lequel une expression à variable réelle est positive (resp. strictement positive).""" |
| 195 |
from .sympy_functions import factor |
| 196 |
le. |
| 197 |
if variable is None: |
| 198 |
variable = extract_var(expression) |
| 199 |
if hasattr(expression, "subs"): |
| 200 |
old_variable = variable |
| 201 |
variable = Dummy(real=True) |
| 202 |
expression = expression.subs({old_variable:variable}) |
| 203 |
|
| 204 |
té |
| 205 |
ens_def = ensemble_definition(expression, variable) |
| 206 |
T = periode(expression, variable) |
| 207 |
if T not in (0, oo): |
| 208 |
ens_def &= Intervalle(0, T) |
| 209 |
if param.debug: |
| 210 |
print('Fonction périodique %s détectée. Résolution sur [0;%s]' % (expression, T)) |
| 211 |
|
| 212 |
|
| 213 |
a = Wild('a', exclude=[expression]) |
| 214 |
dic = expression.match(sqrt(a**2)) |
| 215 |
if dic is not None: |
| 216 |
sub_expr = dic[a] |
| 217 |
) ! |
| 218 |
if not (sub_expr.is_Pow and sub_expr.as_base_exp()[1].is_integer): |
| 219 |
expression = expression.replace(sqrt(a**2),Abs(a)) |
| 220 |
expression = expression.replace(Abs(sqrt(a)),sqrt(a)) |
| 221 |
del a |
| 222 |
|
| 223 |
|
| 224 |
try: |
| 225 |
expression = factor(expression, variable, "R", decomposer_entiers = False) |
| 226 |
except NotImplementedError: |
| 227 |
if param.debug: |
| 228 |
print("Warning: Factorisation impossible de ", expression) |
| 229 |
|
| 230 |
if expression.is_Pow and expression.as_base_exp()[1].is_rational: |
| 231 |
base, p = expression.as_base_exp() |
| 232 |
: |
| 233 |
if p < 0: |
| 234 |
strict = True |
| 235 |
if p.is_integer and p.is_even: |
| 236 |
if strict: |
| 237 |
return ens_def & (R - (positif(base, variable, strict=False) - positif(base, variable, strict=True))) |
| 238 |
else: |
| 239 |
return ens_def |
| 240 |
else: |
| 241 |
return ens_def & positif(base, variable, strict=strict) |
| 242 |
|
| 243 |
if expression.is_Mul: |
| 244 |
posit = R |
| 245 |
posit_nul = R |
| 246 |
for facteur in expression.args: |
| 247 |
|
| 248 |
|
| 249 |
pos = positif(facteur, variable, strict = True) |
| 250 |
pos_nul = positif(facteur, variable, strict = False) |
| 251 |
s |
| 252 |
s |
| 253 |
posit, posit_nul = ((posit & pos) + (-posit_nul & -pos_nul) & ens_def, |
| 254 |
((posit_nul & pos_nul) + (-posit & -pos)) & ens_def) |
| 255 |
if strict: |
| 256 |
return posit |
| 257 |
else: |
| 258 |
return posit_nul |
| 259 |
|
| 260 |
if expression.is_positive is True: |
| 261 |
return ens_def |
| 262 |
if expression.is_negative is True: |
| 263 |
return vide |
| 264 |
if expression.is_positive is False and strict: |
| 265 |
return vide |
| 266 |
if expression.is_negative is False and not strict: |
| 267 |
return ens_def |
| 268 |
if expression.is_zero is True and not strict: |
| 269 |
return ens_def |
| 270 |
|
| 271 |
if isinstance(expression, (int, float)): |
| 272 |
if expression > 0 or (expression == 0 and not strict): |
| 273 |
return ens_def |
| 274 |
else: |
| 275 |
return vide |
| 276 |
|
| 277 |
es. |
| 278 |
if expression.is_polynomial(): |
| 279 |
P = expression.as_poly(variable) |
| 280 |
if P.degree() == 1: |
| 281 |
a, b = P.all_coeffs() |
| 282 |
if a > 0: |
| 283 |
return Intervalle(-b/a, +oo, inf_inclus = not strict) |
| 284 |
else: |
| 285 |
return Intervalle(-oo, -b/a, sup_inclus = not strict) |
| 286 |
elif P.degree() == 2: |
| 287 |
a, b, c = P.all_coeffs() |
| 288 |
d = b**2 - 4*a*c |
| 289 |
if d > 0: |
| 290 |
x1 = (-b - sqrt(d))/(2*a) |
| 291 |
x2 = (-b + sqrt(d))/(2*a) |
| 292 |
x1, x2 = min(x1, x2), max(x1, x2) |
| 293 |
if a > 0: |
| 294 |
return Intervalle(-oo, x1, sup_inclus = not strict) + Intervalle(x2, +oo, inf_inclus = not strict) |
| 295 |
else: |
| 296 |
return Intervalle(x1, x2, inf_inclus = not strict, sup_inclus = not strict) |
| 297 |
elif d == 0: |
| 298 |
x0 = -b/(2*a) |
| 299 |
if a > 0: |
| 300 |
return Intervalle(-oo, x0, sup_inclus = not strict) + Intervalle(x0, +oo, inf_inclus = not strict) |
| 301 |
else: |
| 302 |
return Intervalle(x0, x0, sup_inclus = not strict) |
| 303 |
else: |
| 304 |
if a > 0: |
| 305 |
return R |
| 306 |
else: |
| 307 |
return vide |
| 308 |
|
| 309 |
|
| 310 |
if isinstance(expression, Abs): |
| 311 |
return ens_def |
| 312 |
|
| 313 |
eur |
| 314 |
|
| 315 |
..) |
| 316 |
for subexpr in expression.find(Abs): |
| 317 |
|
| 318 |
|
| 319 |
val = subexpr.args[0] |
| 320 |
pos = positif(val, variable) |
| 321 |
|
| 322 |
return ((positif(expression.xreplace({subexpr: val}), variable, strict=strict) & pos) | |
| 323 |
(positif(expression.xreplace({subexpr: -val}), variable, strict=strict) & -pos)) |
| 324 |
|
| 325 |
|
| 326 |
er |
| 327 |
if expression.is_Add: |
| 328 |
a = Wild('a', exclude=[variable, 0]) |
| 329 |
q = Wild('q', exclude=[variable, 1]) |
| 330 |
b = Wild('b', exclude=[variable]) |
| 331 |
X = Wild('X') |
| 332 |
|
| 333 |
match = expression.match(a*X**q + b) |
| 334 |
if match and X in match: |
| 335 |
a = match[a] |
| 336 |
q = match[q] |
| 337 |
b = -match[b]/a |
| 338 |
X = match[X] |
| 339 |
mes). |
| 340 |
/n) |
| 341 |
|
| 342 |
. |
| 343 |
if not q.is_integer: |
| 344 |
if a.is_negative: |
| 345 |
|
| 346 |
strict = not strict |
| 347 |
if b.is_negative: |
| 348 |
|
| 349 |
sols = ens_def |
| 350 |
else: |
| 351 |
|
| 352 |
sols = ens_def & positif(X - b**(1/q), strict=strict) |
| 353 |
|
| 354 |
if a.is_negative: |
| 355 |
sols = ens_def - sols |
| 356 |
return sols |
| 357 |
del a, q, b, X |
| 358 |
|
| 359 |
a) |
| 360 |
if expression.is_Add: |
| 361 |
a = Wild('a', exclude=[0, variable]) |
| 362 |
B = Wild('B', exclude=[0]) |
| 363 |
c = Wild('c', exclude=[0, variable]) |
| 364 |
D = Wild('D', exclude=[0]) |
| 365 |
match = expression.match(a*sqrt(B) - c*sqrt(D)) |
| 366 |
if match: |
| 367 |
a = match[a] |
| 368 |
B = match[B] |
| 369 |
c = match[c] |
| 370 |
D = match[D] |
| 371 |
if a.is_positive and c.is_positive: |
| 372 |
return ens_def & positif(a**2*B - c**2*D, variable, strict=strict) |
| 373 |
elif a.is_negative and c.is_negative: |
| 374 |
return ens_def & positif(c**2*D - a**2*B, variable, strict=strict) |
| 375 |
del a, B, c, D |
| 376 |
|
| 377 |
|
| 378 |
if isinstance(expression, ln): |
| 379 |
return positif(expression.args[0] - 1, variable, strict=strict) |
| 380 |
|
| 381 |
|
| 382 |
.. |
| 383 |
if expression.is_Add and expression.has(ln): |
| 384 |
args = expression.args |
| 385 |
liste_constantes = [] |
| 386 |
liste_ln = [] |
| 387 |
|
| 388 |
for arg in args: |
| 389 |
if getattr(arg, "is_Mul", False): |
| 390 |
liste_constantes = [] |
| 391 |
liste_ln = [] |
| 392 |
for facteur in arg.args: |
| 393 |
if isinstance(facteur, ln) and is_var(facteur, variable): |
| 394 |
liste_ln.append(facteur) |
| 395 |
elif not is_var(facteur, variable): |
| 396 |
liste_constantes.append(facteur) |
| 397 |
if len(liste_constantes) == len(arg.args) - 1 and len(liste_ln) == 1: |
| 398 |
expression += ln(liste_ln[0].args[0]**Add(*liste_constantes)) - arg |
| 399 |
0 |
| 400 |
for arg in args: |
| 401 |
if isinstance(arg, ln) and hasattr(arg, "has") and arg.has(variable): |
| 402 |
liste_ln.append(arg) |
| 403 |
elif not hasattr(arg, "has") or not arg.has(variable): |
| 404 |
liste_constantes.append(arg) |
| 405 |
|
| 406 |
if liste_ln and len(liste_ln) + len(liste_constantes) == len(args): |
| 407 |
|
| 408 |
contenu_log = Mul(*(logarithme.args[0] for logarithme in liste_ln)) |
| 409 |
contenu_cst = exp(- Add(*liste_constantes)) |
| 410 |
return ens_def & positif(contenu_log - contenu_cst, variable, strict=strict) |
| 411 |
|
| 412 |
[ |
| 413 |
expr = expression |
| 414 |
changements = False |
| 415 |
for arg in expr.args: |
| 416 |
if isinstance(arg, ln): |
| 417 |
changements = True |
| 418 |
expr += arg.args[0] + 1 - arg |
| 419 |
if changements: |
| 420 |
try: |
| 421 |
non_positif = positif(-expr, variable, strict = not strict) |
| 422 |
(ens_def - positif(expr, variable, strict = not strict) == vide) |
| 423 |
if (ens_def - non_positif == vide): |
| 424 |
return vide |
| 425 |
except NotImplementedError: |
| 426 |
pass |
| 427 |
|
| 428 |
0) |
| 429 |
expr = Mul(*(exp(arg) for arg in expression.args)) - 1 |
| 430 |
try: |
| 431 |
return ens_def*positif(expr, variable, strict=strict) |
| 432 |
except NotImplementedError: |
| 433 |
pass |
| 434 |
|
| 435 |
|
| 436 |
0 |
| 437 |
if expression.is_Add and expression.has(exp): |
| 438 |
a = Wild('a', exclude=[variable, 0]) |
| 439 |
b = Wild('b', exclude=[variable]) |
| 440 |
X = Wild('X') |
| 441 |
match = expression.match(a*exp(X) + b) |
| 442 |
if match: |
| 443 |
a = match[a] |
| 444 |
b = match[b] |
| 445 |
X = match[X] |
| 446 |
if is_pos(b): |
| 447 |
if is_pos(a): |
| 448 |
return ens_def |
| 449 |
elif is_neg(a): |
| 450 |
s |
| 451 |
return positif(- X + ln(-b/a), variable, strict=strict) |
| 452 |
elif is_neg(b): |
| 453 |
if is_pos(a): |
| 454 |
return positif(X - ln(-b/a), variable, strict=strict) |
| 455 |
elif is_neg(a): |
| 456 |
return vide |
| 457 |
del a, b, X |
| 458 |
|
| 459 |
R |
| 460 |
expr = expression |
| 461 |
changements = False |
| 462 |
for arg in expr.args: |
| 463 |
if isinstance(arg, exp): |
| 464 |
changements = True |
| 465 |
expr += arg.args[0] + 1 - arg |
| 466 |
if changements and (ens_def - positif(expr, variable, strict=strict) == vide): |
| 467 |
return ens_def |
| 468 |
|
| 469 |
x>0 |
| 470 |
a = Wild('a', exclude=[variable, 0]) |
| 471 |
b = Wild('b', exclude=[variable, 0]) |
| 472 |
X = Wild('X') |
| 473 |
match = expression.match(b*(a*X - 1 + exp(X))) |
| 474 |
if match is not None and X in match: |
| 475 |
a = match[a] |
| 476 |
if a >= 0: |
| 477 |
b = match[b] |
| 478 |
X = match[X] |
| 479 |
return positif(b*X, variable, strict=strict) |
| 480 |
del a, b, X |
| 481 |
|
| 482 |
ble. |
| 483 |
s ! |
| 484 |
if _niveau > 10: |
| 485 |
print("Infinite recursion suspected. Aborting...") |
| 486 |
raise NotImplementedError |
| 487 |
|
| 488 |
|
| 489 |
tc. |
| 490 |
|
| 491 |
def _resolution_par_substitution(expr, X): |
| 492 |
try: |
| 493 |
solutions_intermediaires = positif(expr, X, strict=strict, |
| 494 |
_niveau=_niveau + 1, |
| 495 |
_changement_variable=Lambda(variable,sub)) |
| 496 |
solution = vide |
| 497 |
for intervalle in solutions_intermediaires.intervalles: |
| 498 |
sol = R |
| 499 |
a = intervalle.inf |
| 500 |
b = intervalle.sup |
| 501 |
if a != - oo: |
| 502 |
sol &= positif(sub - a, variable, strict=(not intervalle.inf_inclus)) |
| 503 |
if b != oo: |
| 504 |
sol &= positif(b - sub, variable, strict=(not intervalle.sup_inclus)) |
| 505 |
solution += sol |
| 506 |
return ens_def & solution |
| 507 |
except NotImplementedError: |
| 508 |
return None |
| 509 |
|
| 510 |
|
| 511 |
. |
| 512 |
changements = sorted(set(subexpr for subexpr in preorder_traversal(expression) |
| 513 |
if subexpr.has(variable)) - {variable, expression}, |
| 514 |
key=count_ops) |
| 515 |
a = Wild('a', exclude=[variable, 0]) |
| 516 |
b = Wild('b', exclude=[variable, 0]) |
| 517 |
X = Dummy(real=True) |
| 518 |
changements_incomplets = [] |
| 519 |
for sub in changements: |
| 520 |
if _changement_variable is not None: |
| 521 |
enchainement = _changement_variable(sub) |
| 522 |
if enchainement.match(a*variable) or enchainement.match(a*abs(variable)): |
| 523 |
e, |
| 524 |
. |
| 525 |
e. |
| 526 |
continue |
| 527 |
match = expression.match(a*sub + b) |
| 528 |
if match and match[a] in (-1, 1): |
| 529 |
: |
| 530 |
tc. |
| 531 |
continue |
| 532 |
|
| 533 |
new = expression.subs(sub, X) |
| 534 |
if new.has(variable): |
| 535 |
|
| 536 |
x. |
| 537 |
changements_incomplets.append(sub) |
| 538 |
else: |
| 539 |
sols = _resolution_par_substitution(new, X) |
| 540 |
if sols is not None: |
| 541 |
return sols |
| 542 |
for sub in changements_incomplets: |
| 543 |
|
| 544 |
) |
| 545 |
|
| 546 |
|
| 547 |
|
| 548 |
var1 = Dummy() |
| 549 |
var2 = Dummy() |
| 550 |
antecedents = solve((sub - X).xreplace({variable:var1, X:var2}), var1) |
| 551 |
if len(antecedents) == 1: |
| 552 |
new = expression.subs(variable, antecedents[0].xreplace({var1:variable, var2:X})) |
| 553 |
sols = _resolution_par_substitution(new, X) |
| 554 |
if sols is not None: |
| 555 |
return sols |
| 556 |
|
| 557 |
é. |
| 558 |
. |
| 559 |
ne |
| 560 |
. |
| 561 |
en |
| 562 |
as. |
| 563 |
. |
| 564 |
if param.debug: |
| 565 |
print("Warning: résolution par continuité. Les résultats peuvent être\n" |
| 566 |
"faux si certaines racines ne sont pas touvées !") |
| 567 |
|
| 568 |
racines = nul(expression, variable, intervalle=False) |
| 569 |
solutions = vide |
| 570 |
for intervalle in ens_def.intervalles: |
| 571 |
inf = intervalle.inf |
| 572 |
sup = intervalle.sup |
| 573 |
decoupage = {S(inf), S(sup)} |
| 574 |
for borne in decoupage: |
| 575 |
if borne in intervalle: |
| 576 |
if expression.subs(variable, borne) > 0: |
| 577 |
solutions |= Intervalle(borne, borne) |
| 578 |
for rac in racines: |
| 579 |
if rac in intervalle: |
| 580 |
if not strict: |
| 581 |
solutions |= Intervalle(rac, rac) |
| 582 |
decoupage.add(rac) |
| 583 |
decoupage = sorted(decoupage) |
| 584 |
for val1, val2 in zip(decoupage[:-1], decoupage[1:]): |
| 585 |
if val1 == -oo and val2 == oo: |
| 586 |
milieu = 0 |
| 587 |
elif val2 == oo: |
| 588 |
milieu = val1 + 1 |
| 589 |
elif val1 == -oo: |
| 590 |
milieu = val2 - 1 |
| 591 |
else: |
| 592 |
milieu = (val1 + val2)/2 |
| 593 |
if expression.subs(variable, milieu) > 0: |
| 594 |
solutions |= Intervalle(val1, val2, False, False) |
| 595 |
return ens_def&solutions |
| 596 |
|
| 597 |
|
| 598 |
|
| 599 |
|
| 600 |
|
| 601 |
def resoudre(chaine, variables=(), local_dict=None, ensemble='R'): |
| 602 |
"""Résout une équation ou inéquation, rentrée sous forme de chaîne. |
| 603 |
|
| 604 |
|
| 605 |
""" |
| 606 |
def evaluer(expression, local_dict=local_dict): |
| 607 |
if local_dict is None: |
| 608 |
s |
| 609 |
expression = sympify(expression, rational=True) |
| 610 |
else: |
| 611 |
expression = eval(expression, local_dict) |
| 612 |
s |
| 613 |
expression = floats2rationals(expression) |
| 614 |
return expression |
| 615 |
|
| 616 |
: |
| 617 |
chaine = chaine.replace(')et', ') et').replace(')ou', ') ou').replace('et(', 'et (').replace('ou(', 'ou (') |
| 618 |
chaine = chaine.replace("==", "=").replace("<>", "!=").replace("=>", ">=").replace("=<", "<=") |
| 619 |
|
| 620 |
if not variables: |
| 621 |
n |
| 622 |
arguments = chaine.split(',') |
| 623 |
variables = [Symbol(s.strip()) for s in arguments[1:]] |
| 624 |
|
| 625 |
if not variables: |
| 626 |
. |
| 627 |
. |
| 628 |
variables = set() |
| 629 |
chaine2tuple = arguments[0] |
| 630 |
for s in (' et ', ' ou ', '>=', '<=', '==', '!=', '=', '<', '>'): |
| 631 |
chaine2tuple = chaine2tuple.replace(s, ',') |
| 632 |
|
| 633 |
def find_all_symbols(e): |
| 634 |
s = set() |
| 635 |
if isinstance(e, tuple): |
| 636 |
s.update(*(find_all_symbols(elt) for elt in e)) |
| 637 |
else: |
| 638 |
s.update(e.atoms(Symbol)) |
| 639 |
return s |
| 640 |
variables = find_all_symbols(evaluer(chaine2tuple)) |
| 641 |
|
| 642 |
chaine = arguments[0] |
| 643 |
|
| 644 |
|
| 645 |
|
| 646 |
if len(variables) > 1: |
| 647 |
return systeme(chaine, local_dict = local_dict) |
| 648 |
|
| 649 |
assert len(variables) == 1 |
| 650 |
variable = tuple(variables)[0] |
| 651 |
|
| 652 |
|
| 653 |
|
| 654 |
debut = '' |
| 655 |
_resoudre = partial(resoudre, variables=variables, ensemble=ensemble, local_dict=local_dict) |
| 656 |
while chaine: |
| 657 |
l = [s for s in split_around_parenthesis(chaine)] |
| 658 |
if len(l) == 3: |
| 659 |
if l[0].strip() == l[2].strip() == '': |
| 660 |
return _resoudre(chaine[1:-1]) |
| 661 |
if ' et ' in l[0]: |
| 662 |
retour = chaine.split(' et ', 1) |
| 663 |
retour[0] = debut + retour[0] |
| 664 |
return _resoudre(retour[0]) & _resoudre(retour[1]) |
| 665 |
elif ' ou ' in l[0]: |
| 666 |
retour = chaine.split(' ou ', 1) |
| 667 |
retour[0] = debut + retour[0] |
| 668 |
return _resoudre(retour[0]) | _resoudre(retour[1]) |
| 669 |
chaine = l[2] |
| 670 |
debut += l[0] + l[1] |
| 671 |
else: |
| 672 |
if ' et ' in chaine: |
| 673 |
retour = chaine.split(' et ', 1) |
| 674 |
retour[0] = debut + retour[0] |
| 675 |
return _resoudre(retour[0]) & _resoudre(retour[1]) |
| 676 |
elif ' ou ' in chaine: |
| 677 |
retour = chaine.split(' ou ', 1) |
| 678 |
retour[0] = debut + retour[0] |
| 679 |
return _resoudre(retour[0]) | _resoudre(retour[1]) |
| 680 |
else: |
| 681 |
break |
| 682 |
chaine = debut + chaine |
| 683 |
|
| 684 |
if ">=" in chaine: |
| 685 |
gauche, droite = chaine.split(">=") |
| 686 |
if ensemble != 'R': |
| 687 |
print("Warning: can't solve in '%s', solving in 'R'." % ensemble) |
| 688 |
return positif(evaluer(gauche + "-(" + droite + ")"), variable) |
| 689 |
elif "<=" in chaine: |
| 690 |
gauche, droite = chaine.split("<=") |
| 691 |
if ensemble != 'R': |
| 692 |
print("Warning: can't solve in '%s', solving in 'R'." % ensemble) |
| 693 |
return positif(evaluer(droite + "-(" + gauche + ")"), variable) |
| 694 |
if ">" in chaine: |
| 695 |
gauche, droite = chaine.split(">") |
| 696 |
if ensemble != 'R': |
| 697 |
print("Warning: can't solve in '%s', solving in 'R'." % ensemble) |
| 698 |
return positif(evaluer(gauche + "-(" + droite + ")"), variable, **{"strict": True}) |
| 699 |
elif "<" in chaine: |
| 700 |
gauche, droite = chaine.split("<") |
| 701 |
if ensemble != 'R': |
| 702 |
print("Warning: can't solve in '%s', solving in 'R'." % ensemble) |
| 703 |
return positif(evaluer(droite + "-(" + gauche + ")"), variable, **{"strict": True}) |
| 704 |
elif "!=" in chaine: |
| 705 |
gauche, droite = chaine.split("!=") |
| 706 |
expression = evaluer(gauche + "-(" + droite + ")") |
| 707 |
if ensemble != 'R': |
| 708 |
print("Warning: can't solve in '%s', solving in 'R'." % ensemble) |
| 709 |
return ensemble_definition(expression, variable) - nul(expression, variable) |
| 710 |
elif "=" in chaine: |
| 711 |
gauche, droite = chaine.split("=") |
| 712 |
expression = evaluer(gauche + "-(" + droite + ")") |
| 713 |
return nul(expression, variable, ensemble=ensemble) |
| 714 |
else: |
| 715 |
raise TypeError("'" + chaine + "' must be an (in)equation.") |
| 716 |
|
| 717 |
|
| 718 |
def systeme(chaine, variables = (), local_dict = None): |
| 719 |
chaine = chaine.replace("==", "=") |
| 720 |
if local_dict is None: |
| 721 |
evaluer = sympify |
| 722 |
else: |
| 723 |
def evaluer(expression, local_dict=local_dict): |
| 724 |
return eval(expression, local_dict) |
| 725 |
|
| 726 |
def transformer(eq): |
| 727 |
gauche, droite = eq.split("=") |
| 728 |
return evaluer(gauche + "-(" + droite + ")") |
| 729 |
|
| 730 |
if not variables: |
| 731 |
arguments = chaine.split(',') |
| 732 |
variables = tuple(Symbol(s.strip()) for s in arguments[1:]) |
| 733 |
chaine = arguments[0] |
| 734 |
eqs = tuple(transformer(eq) for eq in chaine.split("et")) |
| 735 |
if not variables: |
| 736 |
variables = set() |
| 737 |
for eq in eqs: |
| 738 |
variables.update(eq.atoms(Symbol)) |
| 739 |
return solve(eqs, *variables) |
| 740 |
|