| 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 re |
| 26 |
from numpy import arange |
| 27 |
|
| 28 |
from .tablatexlib import traduire_latex, maths |
| 29 |
from wxgeometrie.pylib import print_error |
| 30 |
from wxgeometrie.mathlib.custom_functions import round_afz |
| 31 |
from wxgeometrie import param |
| 32 |
|
| 33 |
def _eval_math(chaine): |
| 34 |
return float(eval(chaine, maths.__dict__)) |
| 35 |
|
| 36 |
|
| 37 |
def _auto_tabval(chaine='', formatage_antecedents='VAL', formatage_images='VAL', precision=0.01): |
| 38 |
|
| 39 |
chaine_initiale = chaine |
| 40 |
|
| 41 |
chaine = chaine.replace(param.separateur_decimal, ".") |
| 42 |
|
| 43 |
|
| 44 |
m = re.match(r"(?P<fonction>[^:]+)" |
| 45 |
r"[ ]+sur[ ]+" |
| 46 |
r"\[(?P<intervalle>[^]]+)\]" |
| 47 |
r"(.*[ ]+pas[ ]+(de[ ]+)?" |
| 48 |
r"(?P<pas>[0-9.]+))?", |
| 49 |
chaine) |
| 50 |
|
| 51 |
. |
| 52 |
|
| 53 |
if m is None: |
| 54 |
raise ValueError("Format incorrect.") |
| 55 |
|
| 56 |
fonction = m.group('fonction') |
| 57 |
intervalle = m.group('intervalle') |
| 58 |
pas = _eval_math(m.group('pas') or '1') |
| 59 |
|
| 60 |
|
| 61 |
] |
| 62 |
!) |
| 63 |
if ';' not in intervalle: |
| 64 |
intervalle = intervalle.replace('.', ';') |
| 65 |
a, b = intervalle.split(';') |
| 66 |
a = _eval_math(a) |
| 67 |
|
| 68 |
code = "%s:[%s]:%s,%s..%s" % (fonction, precision, a, a + pas, b) |
| 69 |
|
| 70 |
if param.debug and param.verbose: |
| 71 |
print('Code TABVal:', code) |
| 72 |
return tabval(code, formatage_antecedents=formatage_antecedents, formatage_images=formatage_images) + '% ' + chaine_initiale + '\n' |
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
def tabval(chaine='', formatage_antecedents='VAL', formatage_images='VAL', precision=0.01): |
| 77 |
r"""Syntaxe: |
| 78 |
fonction: [precision d'arrondi]: 1ere valeur,2e valeur..valeur finale |
| 79 |
|
| 80 |
Exemples: |
| 81 |
\sin(x): -5,-4.9..5 |
| 82 |
h(x)=sin(x)+1: [0.01]: -5,-4.5..0 ; 0,1..3 |
| 83 |
|
| 84 |
Utilisez ; pour séparer plusieurs bloc de valeurs, et // pour indiquer |
| 85 |
un retour à la ligne (si le tableau est trop long). |
| 86 |
|
| 87 |
`formatage_images` contient éventuellement une formule pour formater |
| 88 |
les valeurs, par exemple '\nombre{VAL}'. La variable VAL correspond à la |
| 89 |
valeur du résultat. |
| 90 |
""" |
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
chaine_originale = chaine = chaine.strip() |
| 97 |
|
| 98 |
if ':' not in chaine: |
| 99 |
return _auto_tabval(chaine, formatage_antecedents=formatage_antecedents, |
| 100 |
formatage_images=formatage_images, |
| 101 |
precision=precision) |
| 102 |
|
| 103 |
chaine = chaine.replace(r'\\', '\n').replace('//', '\n') |
| 104 |
sequence = chaine.split(":", 2) |
| 105 |
|
| 106 |
if len(sequence) == 3: |
| 107 |
precision = _eval_math(sequence[1].strip('[] ')) |
| 108 |
|
| 109 |
legende = [txt.strip() for txt in sequence[0].split("=", 1)] |
| 110 |
if len(legende) == 2: |
| 111 |
fonction, expression = legende |
| 112 |
) |
| 113 |
deb = fonction.find("(") |
| 114 |
fin = fonction.find(")") |
| 115 |
if deb == -1: |
| 116 |
variable = "x" |
| 117 |
else: |
| 118 |
variable = fonction[deb+1:fin].strip() |
| 119 |
else: |
| 120 |
fonction = expression = legende[0] |
| 121 |
. |
| 122 |
)) |
| 123 |
m = re.search('(?<![A-Za-z])[A-DF-Za-df-z](?![A-Za-z])', expression) |
| 124 |
|
| 125 |
variable = m.group() if m else 'x' |
| 126 |
|
| 127 |
|
| 128 |
lignes = [txt.strip() for txt in sequence[-1].split('\n') if txt.strip()] |
| 129 |
|
| 130 |
eX |
| 131 |
code = "\\begin{center}" |
| 132 |
|
| 133 |
s |
| 134 |
for ligne in lignes: |
| 135 |
ensemble_valeurs = set() |
| 136 |
for intervalle in ligne.split(';'): |
| 137 |
if '..' in intervalle: |
| 138 |
premier, dernier = intervalle.split('..') |
| 139 |
if ',' in premier: |
| 140 |
premier, suivant = premier.split(',') |
| 141 |
else: |
| 142 |
suivant = None |
| 143 |
first_val = float(eval(premier, maths.__dict__)) |
| 144 |
last_val = float(eval(dernier, maths.__dict__)) |
| 145 |
if suivant is None: |
| 146 |
pas = 1 |
| 147 |
else: |
| 148 |
next_val = float(eval(suivant, maths.__dict__)) |
| 149 |
pas = next_val - first_val |
| 150 |
ensemble_valeurs.update(arange(first_val, last_val, pas)) |
| 151 |
ensemble_valeurs.add(last_val) |
| 152 |
else: |
| 153 |
ensemble_valeurs.add(float(eval(intervalle, maths.__dict__))) |
| 154 |
|
| 155 |
valeurs = sorted(ensemble_valeurs) |
| 156 |
|
| 157 |
nbr_colonnes = len(valeurs) + 1 |
| 158 |
|
| 159 |
code_variable = "$" + variable + "$ " |
| 160 |
code_expression = "$" + fonction + "$ " |
| 161 |
|
| 162 |
expression = traduire_latex(expression) |
| 163 |
|
| 164 |
def formater(expr, formatage='VAL'): |
| 165 |
assert isinstance(expr, float) |
| 166 |
s = str(expr).rstrip('0') |
| 167 |
if s[-1] == '.': |
| 168 |
s = s[:-1] |
| 169 |
if param.separateur_decimal != '.': |
| 170 |
s = s.replace(".", param.separateur_decimal) |
| 171 |
return ' $' + formatage.replace('VAL', s) + '$ ' |
| 172 |
|
| 173 |
|
| 174 |
for val in valeurs: |
| 175 |
n = max(len(code_variable), len(code_expression)) |
| 176 |
!) |
| 177 |
code_variable = code_variable.ljust(n) |
| 178 |
code_expression = code_expression.ljust(n) |
| 179 |
code_variable += '&' + formater(val, formatage_antecedents) |
| 180 |
try: |
| 181 |
dict = maths.__dict__.copy() |
| 182 |
dict.update({variable: val}) |
| 183 |
evaluation = eval(expression, dict) |
| 184 |
if evaluation in (maths.num_oo, maths.num_nan, -maths.num_oo, maths.oo, maths.nan, -maths.oo): |
| 185 |
code_expression += "& $\\times$ " |
| 186 |
else: |
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
inv = (1/precision) |
| 193 |
val = round_afz(inv*evaluation)/inv |
| 194 |
code_expression += '&' + formater(val, formatage_images) |
| 195 |
except: |
| 196 |
print_error() |
| 197 |
code_expression += "& $\\times$ " |
| 198 |
|
| 199 |
code += "\n\\begin{tabular}{|" + nbr_colonnes*"c|" + "}\n\\hline\n" |
| 200 |
code += code_variable + "\\\\\n" |
| 201 |
code += "\\hline\n" |
| 202 |
code += code_expression + "\\\\\n" |
| 203 |
code += "\\hline\n\\end{tabular}\n" |
| 204 |
|
| 205 |
return code + "\\end{center}\n% " + chaine_originale + "\n" |