Debian Codegraph @

wxgeometrie/modules/tablatex/tablatexlib.py

raw symbols SCIP
1 # -*- coding: utf-8 -*-
2
3 ##--------------------------------------##
4 # WxGeometrie #
5 # tabvar #
6 ##--------------------------------------##
7 # WxGeometrie
8 # Dynamic geometry, graph plotter, and more for french mathematic teachers.
9 # Copyright (C) 2005-2013 Nicolas Pourcelot
10 #
11 # This program is free software; you can redistribute it and/or modify
12 # it under the terms of the GNU General Public License as published by
13 # the Free Software Foundation; either version 2 of the License, or
14 # (at your option) any later version.
15 #
16 # This program is distributed in the hope that it will be useful,
17 # but WITHOUT ANY WARRANTY; without even the implied warranty of
18 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 # GNU General Public License for more details.
20 #
21 # You should have received a copy of the GNU General Public License
22 # along with this program; if not, write to the Free Software
23 # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
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 #TODO: déplacer autant que possibles ces fonctions vers le parser de mathlib.
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 # 1. On enlève les parenthèses superflues
82 while chaine[0] == '(' and chaine[-1] == ')' and test_parentheses(chaine[1:-1]):
83 chaine = chaine[1:-1]
84 # Premier test rapide
85 if '*' not in chaine and '/' not in chaine:
86 return [chaine]
87
88 # 2. On regarde s'il s'agit d'une somme/différence
89 # On commence par enlever les '+' ou '-' en début de chaîne (-2x n'est pas une différence)
90 _chaine = chaine.lstrip('+-')
91 for symbole in ('+', '-'):
92 if len(advanced_split(_chaine, symbole)) > 1:
93 return [chaine]
94 # c'est une somme/différence, pas de décomposition en facteurs
95
96 # 3. On découpe autour des '*' (en tenant compte des parenthèses)
97 facteurs = advanced_split(chaine, '*')
98 if len(facteurs) == 1:
99 facteurs = advanced_split(chaine, '/')
100 if len(facteurs) == 1:
101 # Ce n'est ni un produit ni un quotient
102 return facteurs
103
104 # 4. On redécoupe récursivement chacun des facteurs
105 decomposition = []
106 for facteur in facteurs:
107 decomposition.extend(extraire_facteurs(facteur))
108 return decomposition
109
110 def extraire_facteurs(chaine):
111 ## chaine = _ajouter_mult_manquants(chaine, fonctions = maths.__dict__)
112 chaine = traduire_formule(chaine, fonctions = maths.__dict__).replace('**', '^')
113 # Pour faciliter la décomposition en produit,
114 # il est important que la puissance ne soit pas notée '**'.
115 return [simplifier_ecriture(facteur) for facteur in _extraire_facteurs(chaine)]