| 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 |
import math |
| 26 |
import numpy |
| 27 |
|
| 28 |
from sympy import oo, nan |
| 29 |
|
| 30 |
from wxgeometrie.mathlib import universal_functions as maths |
| 31 |
from wxgeometrie.mathlib.parsers import traduire_formule, simplifier_ecriture, convertir_en_latex |
| 32 |
from wxgeometrie.pylib import advanced_split |
| 33 |
from wxgeometrie.geolib.routines import nice_str |
| 34 |
|
| 35 |
|
| 36 |
maths.oo = oo |
| 37 |
maths.nan = nan |
| 38 |
maths.num_oo = numpy.inf |
| 39 |
maths.num_nan = numpy.nan |
| 40 |
maths.pi = math.pi |
| 41 |
maths.e = math.e |
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
. |
| 46 |
|
| 47 |
def traduire_latex(expression): |
| 48 |
return traduire_formule(expression, fonctions=maths.__dict__, OOo=False, |
| 49 |
LaTeX=True) |
| 50 |
|
| 51 |
|
| 52 |
def test_parentheses(chaine): |
| 53 |
"""Retourne True si le parenthésage est correct, False sinon. |
| 54 |
|
| 55 |
Note: il s'agit d'un test rapide (on ne tient pas compte des guillemets, crochets...)""" |
| 56 |
count = 0 |
| 57 |
n = 0 |
| 58 |
k = 0 |
| 59 |
while count >= 0: |
| 60 |
k += 1 |
| 61 |
i = chaine.find('(', n) |
| 62 |
j = chaine.find(')', n) |
| 63 |
if i == -1: |
| 64 |
n = j |
| 65 |
elif j == -1: |
| 66 |
n = i |
| 67 |
else: |
| 68 |
n = min(i, j) |
| 69 |
if n == -1: |
| 70 |
break |
| 71 |
parenthese = chaine[n] |
| 72 |
assert parenthese in '()' |
| 73 |
count += (1 if parenthese == '(' else -1) |
| 74 |
n += 1 |
| 75 |
return not count |
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
def _extraire_facteurs(chaine): |
| 81 |
es |
| 82 |
while chaine[0] == '(' and chaine[-1] == ')' and test_parentheses(chaine[1:-1]): |
| 83 |
chaine = chaine[1:-1] |
| 84 |
|
| 85 |
if '*' not in chaine and '/' not in chaine: |
| 86 |
return [chaine] |
| 87 |
|
| 88 |
e |
| 89 |
ce) |
| 90 |
_chaine = chaine.lstrip('+-') |
| 91 |
for symbole in ('+', '-'): |
| 92 |
if len(advanced_split(_chaine, symbole)) > 1: |
| 93 |
return [chaine] |
| 94 |
rs |
| 95 |
|
| 96 |
s) |
| 97 |
facteurs = advanced_split(chaine, '*') |
| 98 |
if len(facteurs) == 1: |
| 99 |
facteurs = advanced_split(chaine, '/') |
| 100 |
if len(facteurs) == 1: |
| 101 |
|
| 102 |
return facteurs |
| 103 |
|
| 104 |
rs |
| 105 |
decomposition = [] |
| 106 |
for facteur in facteurs: |
| 107 |
decomposition.extend(extraire_facteurs(facteur)) |
| 108 |
return decomposition |
| 109 |
|
| 110 |
def extraire_facteurs(chaine): |
| 111 |
|
| 112 |
chaine = traduire_formule(chaine, fonctions = maths.__dict__).replace('**', '^') |
| 113 |
, |
| 114 |
. |
| 115 |
return [simplifier_ecriture(facteur) for facteur in _extraire_facteurs(chaine)] |