| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
import os, sys |
| 5 |
TOPDIR = os.path.abspath(os.path.join(os.path.dirname(__file__),"../..")) |
| 6 |
sys.path.insert(0, TOPDIR) |
| 7 |
|
| 8 |
import re |
| 9 |
|
| 10 |
from wxgeometrie.mathlib import universal_functions |
| 11 |
from wxgeometrie.mathlib.parsers import ( |
| 12 |
traduire_formule, NBR, NBR_SIGNE, VAR, |
| 13 |
VAR_NOT_ATTR, NBR_OR_VAR, _arguments_latex, |
| 14 |
convertir_en_latex, _fast_closing_bracket_search, |
| 15 |
_fast_opening_bracket_search, mathtext_parser, |
| 16 |
_rechercher_numerateur, _rechercher_denominateur, |
| 17 |
) |
| 18 |
|
| 19 |
liste_fonctions = [ |
| 20 |
key for key in universal_functions.__dict__ if "_" not in key] |
| 21 |
liste_fonctions.append("limite") |
| 22 |
liste_fonctions.append("log10") |
| 23 |
liste_fonctions.append("mat") |
| 24 |
liste_fonctions.append('range') |
| 25 |
|
| •26 |
import tools.unittests |
| 27 |
|
| •28 |
class MathlibTest(tools.unittests.TestCase): |
| 29 |
|
| 30 |
def assert_formule(self, x, y, OOo, LaTeX): |
| 31 |
y_ = traduire_formule(x, fonctions = liste_fonctions, OOo = OOo, LaTeX = LaTeX, verbose = False) |
| 32 |
self.assertEqual(y_, y) |
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
def assert_arg_latex(self, x, *y): |
| 40 |
x = _arguments_latex(x, 2) |
| 41 |
y = list(y) |
| 42 |
|
| 43 |
|
| 44 |
self.assertEqual(x, y) |
| 45 |
|
| 46 |
def assert_all(self, x, y): |
| 47 |
self.assert_formule(x, y, OOo = True, LaTeX = False) |
| 48 |
self.assert_formule(x, y, OOo = True, LaTeX = False) |
| 49 |
self.assert_formule(x, y, OOo = False, LaTeX = True) |
| 50 |
self.assert_formule(x, y, OOo = True, LaTeX = True) |
| 51 |
|
| 52 |
def assert_OOo(self, x, y): |
| 53 |
self.assert_formule(x, y, OOo = True, LaTeX = False) |
| 54 |
self.assert_formule(x, y, OOo = True, LaTeX = True) |
| 55 |
|
| 56 |
def assert_latex(self, x, y): |
| 57 |
self.assert_formule(x, y, OOo = False, LaTeX = True) |
| 58 |
self.assert_formule(x, y, OOo = True, LaTeX = True) |
| 59 |
|
| 60 |
def assert_match(self, pattern, chaine): |
| 61 |
"""Teste si la chaine correspond entièrement au pattern.""" |
| 62 |
self.assertTrue(re.match(pattern + "$", chaine)) |
| 63 |
|
| 64 |
def assert_not_match(self, pattern, chaine): |
| 65 |
"""Teste si la chaine ne correspond pas entièrement au pattern.""" |
| 66 |
self.assertFalse(re.match(pattern + "$", chaine)) |
| 67 |
|
| 68 |
def assert_VAR(self, chaine): |
| 69 |
self.assert_match(VAR, chaine) |
| 70 |
self.assert_match(NBR_OR_VAR, chaine) |
| 71 |
|
| 72 |
def assert_not_VAR(self, chaine): |
| 73 |
self.assert_not_match(VAR, chaine) |
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
def assert_NBR(self, chaine): |
| 80 |
self.assert_match(NBR, chaine) |
| 81 |
self.assert_match(NBR_SIGNE, chaine) |
| 82 |
self.assert_match(NBR_OR_VAR, chaine) |
| 83 |
|
| 84 |
def assert_NBR_SIGNE(self, chaine): |
| 85 |
self.assert_match(NBR_SIGNE, chaine) |
| 86 |
self.assert_not_match(NBR_OR_VAR, chaine) |
| 87 |
|
| 88 |
def assert_find_VAR_NOT_ATTR(self, chaine): |
| 89 |
self.assertTrue(re.search(VAR_NOT_ATTR, chaine)) |
| 90 |
|
| 91 |
def assert_not_find_VAR_NOT_ATTR(self, chaine): |
| 92 |
self.assertFalse(re.search(VAR_NOT_ATTR, chaine)) |
| 93 |
|
| 94 |
def assert_not_NBR(self, chaine): |
| 95 |
self.assert_not_match(NBR, chaine) |
| 96 |
|
| 97 |
def test_tous_modes(self): |
| 98 |
self.assert_all('a z', 'a*z') |
| 99 |
self.assert_all("2x+3", "2*x+3") |
| 100 |
self.assert_all("2(x+3)", "2*(x+3)") |
| 101 |
self.assert_all("(x+1)x(x+3)", "(x+1)*x*(x+3)") |
| 102 |
self.assert_all("sin(x+1)x(x+3)", "sin(x+1)*x*(x+3)") |
| 103 |
self.assert_all("(x+1)cos(x+3)", "(x+1)*cos(x+3)") |
| 104 |
self.assert_all("-1.5x^(-2)+ab+3ab(2x+y)+x(y(z+1)))2(x)", |
| 105 |
"-1.5*x**(-2)+ab+3*ab*(2*x+y)+x*(y*(z+1)))*2*(x)") |
| 106 |
self.assert_all("3x³-2x²y-2x==5y", "3*x**3-2*x**2*y-2*x==5*y") |
| 107 |
self.assert_all("25%*12 mod 5", "25/100*12%5") |
| 108 |
self.assert_all("(25%*12)mod 5", "(25/100*12)%5") |
| 109 |
self.assert_all("limite(1/x^3,x,1+)", "limite(1/x**3,x,1,'+')") |
| 110 |
self.assert_all("limite(1/x^3,x, 1- )", "limite(1/x**3,x,1,'-')") |
| 111 |
self.assert_all("x sin x+1", "x*sin(x)+1") |
| 112 |
self.assert_all("log10 ab y sin 2x+1", "log10(ab)*y*sin(2*x)+1") |
| 113 |
self.assert_all("cos 3.5x(x+1)", "cos(3.5*x)*(x+1)") |
| 114 |
self.assert_all("cos 2", "cos(2)") |
| 115 |
|
| 116 |
self.assert_all("cos -3", "cos-3") |
| 117 |
que |
| 118 |
self.assert_all("17.03[45]", "((1703+45/99)/100)") |
| 119 |
self.assert_all("17.[045]", "((17+45/999)/1)") |
| 120 |
self.assert_all("17.1[0]", "((171+0/9)/10)") |
| 121 |
de |
| 122 |
self.assert_all("A.transpose()", "A.transpose()") |
| 123 |
self.assert_all("[j for j in liste]", "[j for j in liste]") |
| 124 |
e |
| 125 |
self.assert_all("\u2013x\u22123\u00D7y\u00F7z²", "-x-3*y/z**2") |
| 126 |
|
| 127 |
self.assert_all(".015(x-50)^2-20", ".015*(x-50)**2-20") |
| 128 |
self.assert_all("-1.015 (x-50)", "-1.015*(x-50)") |
| 129 |
self.assert_all('5|x+3|+1-|2x|', '5*abs(x+3)+1-abs(2*x)') |
| 130 |
self.assert_all('[f for j in range(1, 11)]', '[f for j in range(1,11)]') |
| 131 |
|
| 132 |
def test_texte(self): |
| 133 |
. |
| 134 |
self.assert_all("'1.2345'", "'1.2345'") |
| 135 |
self.assert_all('"ok"', '"ok"') |
| 136 |
self.assert_all('"x(x+1)" x(x+1) """x(x+1) " """', '"x(x+1)"x*(x+1)"""x(x+1) " """') |
| 137 |
self.assert_all(r'"\""', r'"\""') |
| 138 |
self.assert_all(r'"""\"+1\" ici, et non \"+n\""""', r'"""\"+1\" ici, et non \"+n\""""') |
| 139 |
|
| 140 |
def test_matrice(self): |
| 141 |
|
| 142 |
self.assert_all("[[1, 2], [3, 4]]", "mat([[1,2],[3,4]])") |
| 143 |
self.assert_all("[ [1,2;2,5] ; [-3,4;4,2] ]", "mat([[1.2,2.5],[-3.4,4.2]])") |
| 144 |
à. |
| 145 |
self.assert_all("mat([[1, 2], [3, 4]])", "mat([[1,2],[3,4]])") |
| 146 |
self.assert_all("mat( [[1, 2], [3, 4]] )", "mat([[1,2],[3,4]])") |
| 147 |
|
| 148 |
|
| 149 |
def test_mode_OOo(self): |
| 150 |
self.assert_OOo("2 times 3", "2*3") |
| 151 |
self.assert_OOo("2 over 3", "2/3") |
| 152 |
self.assert_OOo("{2+5x} over {3-x}", "(2+5*x)/(3-x)") |
| 153 |
self.assert_OOo("{2-.5x}over{3-x}", "(2-.5*x)/(3-x)") |
| 154 |
self.assert_OOo("0.85 sup {1 over 7} - 1", "0.85**(1/7)-1") |
| 155 |
|
| 156 |
|
| 157 |
def test_mode_LaTeX(self): |
| 158 |
self.assert_latex("2\\times3", "2*3") |
| 159 |
self.assert_latex("\\cos x\\sin x\\exp x", "cos(x)*sin(x)*exp(x)") |
| 160 |
self.assert_latex("\\frac{2}{3}", "((2)/(3))") |
| 161 |
self.assert_latex("\\frac{2+x}{3}", "((2+x)/(3))") |
| 162 |
self.assert_latex("\\dfrac{2+x}{1-3}", "((2+x)/(1-3))") |
| 163 |
self.assert_latex("\\tfrac{2+x}{3}", "((2+x)/(3))") |
| 164 |
self.assert_latex("\\dfrac{2x^2+x-7}{6-4x}", "((2*x**2+x-7)/(6-4*x))") |
| 165 |
self.assert_latex("-\\frac12-\\dfrac4{(6-4x)^2}", "-(1/2)-(4/((6-4*x)**2))") |
| 166 |
self.assert_latex("\\left((1+10~\\%)(1+5~\\%)(1-7~\\%)\\right)^{\\frac{1}{3} }", |
| 167 |
"((1+10/100)*(1+5/100)*(1-7/100))**(((1)/(3)))") |
| 168 |
self.assert_latex("\\text{0.7}\\times (-50)^2-9\\times (-50)+200", "(0.7)*(-50)**2-9*(-50)+200") |
| 169 |
self.assert_latex("\\ln(2)+\\exp(3)+\\log(\\pi+1)", "ln(2)+exp(3)+log(pi+1)") |
| •170 |
self.assert_latex(r"x\ge1\le3", "x>=1<=3") |
| 171 |
self.assert_latex(r"100\left(\left(1+\dfrac{50}{100}\right)^\frac{1}{10}-1\right)", |
| 172 |
"100*((1+((50)/(100)))**((1)/(10))-1)") |
| 173 |
self.assert_latex("M = \\begin{pmatrix}\n0,6 & 0,4\\\\\n0,75& 0,25\\\\\n\\end{pmatrix}", |
| 174 |
'M=mat([[0.6,0.4],[0.75,0.25]])') |
| 175 |
self.assert_latex(r"\begin{pmatrix}0.65& 0.35\end{pmatrix}\begin{pmatrix}0.55 & 0.45\\0.3 & 0.7\end{pmatrix}", |
| 176 |
"mat([[0.65,0.35]])*mat([[0.55,0.45],[0.3,0.7]])") |
| 177 |
|
| 178 |
def test_NBR(self): |
| 179 |
self.assert_NBR_SIGNE("-2.56") |
| 180 |
self.assert_NBR_SIGNE("-.56") |
| 181 |
self.assert_NBR_SIGNE("+5.") |
| 182 |
self.assert_NBR_SIGNE("+5.056") |
| 183 |
self.assert_NBR("56") |
| 184 |
self.assert_NBR(".46") |
| 185 |
self.assert_NBR(".015") |
| 186 |
self.assert_NBR("752.") |
| 187 |
self.assert_NBR("740.54") |
| 188 |
self.assert_not_NBR("5-6") |
| 189 |
self.assert_not_NBR(".") |
| 190 |
|
| •191 |
self.assert_match(r'\(' + NBR_SIGNE, "(-2.3") |
| 192 |
|
| 193 |
def test_VAR(self): |
| 194 |
self.assert_VAR("Arertytre") |
| 195 |
self.assert_VAR("a") |
| 196 |
self.assert_VAR("_") |
| 197 |
self.assert_VAR("_45ui") |
| 198 |
self.assert_VAR("A13") |
| 199 |
self.assert_not_VAR("1A") |
| 200 |
self.assert_not_VAR("2") |
| 201 |
|
| 202 |
def test_search_VAR_NOT_ATTR(self): |
| 203 |
self.assert_find_VAR_NOT_ATTR("a") |
| 204 |
self.assert_find_VAR_NOT_ATTR("1+_arrt9876") |
| 205 |
self.assert_find_VAR_NOT_ATTR("5*t_566") |
| 206 |
self.assert_find_VAR_NOT_ATTR("(_.t)/3") |
| 207 |
self.assert_not_find_VAR_NOT_ATTR(".aert") |
| 208 |
self.assert_not_find_VAR_NOT_ATTR("4.tyu+4") |
| 209 |
self.assert_not_find_VAR_NOT_ATTR("89eeazt") |
| 210 |
self.assert_not_find_VAR_NOT_ATTR("2-._ez") |
| 211 |
|
| 212 |
def test_arguments_LaTeX(self): |
| 213 |
self.assert_arg_latex('2{x+1}+4', '2', '{x+1}', '+4') |
| 214 |
self.assert_arg_latex('{x+2}5+4x-17^{2+x}', '{x+2}', '5', '+4x-17^{2+x}') |
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
def assert_conv(self, input, output): |
| 224 |
self.assertEqual(convertir_en_latex(input), '$%s$' %output) |
| 225 |
|
| 226 |
def test_convertir_en_LaTeX(self): |
| 227 |
self.assert_conv('2*x', '2 x') |
| 228 |
self.assert_conv('2*3', r'2\times 3') |
| 229 |
|
| 230 |
self.assert_conv('2*.005', r'2\times .005') |
| 231 |
self.assert_conv('2*x+3', '2 x+3') |
| 232 |
self.assert_conv('x**-3.5+x**2*y-x**(2*y)+8', 'x^{-3.5}+x^{2} y-x^{2 y}+8') |
| 233 |
self.assert_conv('--x+-----3--y', 'x-3+y') |
| 234 |
self.assert_conv('sqrt(x) + exp(-y)', r'\sqrt{x}+\exp(-y)') |
| 235 |
self.assert_conv('+oo', r'+\infty') |
| 236 |
|
| 237 |
def test_convertir_en_LaTeX_mode_dollars(self): |
| 238 |
self.assertEqual(convertir_en_latex('-1', mode='$'), '$-1$') |
| 239 |
self.assertEqual(convertir_en_latex('', mode='$'), '') |
| 240 |
|
| 241 |
def test_convertir_en_LaTeX_fractions(self): |
| 242 |
self.assert_conv('2/3', r'\frac{2}{3}') |
| 243 |
self.assert_conv('-2/3', r'-\frac{2}{3}') |
| 244 |
self.assert_conv('x**(2/3)', r'x^{\frac{2}{3}}') |
| 245 |
self.assert_conv('(x+1)/(2*x)', r'\frac{x+1}{2 x}') |
| 246 |
self.assert_conv('(x(x+1))/(2*x*(x+2)*(x**25+7*x+5))*(x+3)', |
| 247 |
r'\frac{x(x+1)}{2 x (x+2) (x^{25}+7 x+5)} (x+3)') |
| 248 |
self.assert_conv('2/3x', r'\frac{2}{3}x') |
| 249 |
self.assert_conv('2/0.4', r'\frac{2}{0.4}') |
| 250 |
|
| 251 |
def test_convertir_en_LaTeX_fractions_imbriquees(self): |
| 252 |
self.assert_conv('(9*x+3/7)/(-8*x-6)', r'\frac{9 x+\frac{3}{7}}{-8 x-6}') |
| 253 |
self.assert_conv('2/3/4', r'\frac{\frac{2}{3}}{4}') |
| 254 |
self.assert_conv('25/(4/7)+8/pi', r'\frac{25}{\frac{4}{7}}+\frac{8}{\pi}') |
| 255 |
self.assert_conv('(2/3)/(25/(4/7)+8/pi)', |
| 256 |
r'\frac{\frac{2}{3}}{\frac{25}{\frac{4}{7}}+\frac{8}{\pi}}') |
| 257 |
|
| 258 |
|
| 259 |
def test_convertir_en_LaTeX_bad_expression(self): |
| 260 |
r |
| 261 |
?? |
| 262 |
e |
| 263 |
e. |
| 264 |
|
| 265 |
self.assert_conv('2/', '2/') |
| 266 |
self.assert_conv('/', '/') |
| 267 |
self.assert_conv('-+', '-') |
| 268 |
es |
| 269 |
self.assert_conv('+', '+') |
| 270 |
|
| 271 |
|
| 272 |
def test_parentheses_inutiles(self): |
| 273 |
self.assert_conv('(x+1)', 'x+1') |
| 274 |
self.assert_conv('(x(x+1))', 'x(x+1)') |
| 275 |
self.assert_conv('(((x)))', 'x') |
| 276 |
|
| 277 |
def test_convertir_en_LaTeX_mode_None(self): |
| 278 |
self.assertEqual(convertir_en_latex('2*x', mode=None), '2 x') |
| 279 |
|
| 280 |
def assert_search(self, input, expected): |
| 281 |
i = _fast_closing_bracket_search(input) |
| 282 |
self.assertEqual(input[:i], expected) |
| 283 |
|
| 284 |
def test_fast_closing_bracket_search(self): |
| 285 |
self.assert_search('(ok)', '(ok)') |
| 286 |
self.assert_search('(ok)+3', '(ok)') |
| 287 |
self.assert_search('(x+(x-3(x-4))+(x-7))-x(x+8)', '(x+(x-3(x-4))+(x-7))') |
| 288 |
|
| 289 |
def assert_backsearch(self, input, expected): |
| 290 |
i = _fast_opening_bracket_search(input) |
| 291 |
self.assertEqual(input[i:], expected) |
| 292 |
|
| 293 |
def test_fast_opening_bracket_search(self): |
| 294 |
self.assert_backsearch('(ok)', '(ok)') |
| 295 |
self.assert_backsearch('3+(ok)', '(ok)') |
| 296 |
self.assert_backsearch('x(x+8)-(x+(x-3(x-4))+(x-7))', '(x+(x-3(x-4))+(x-7))') |
| 297 |
|
| 298 |
def assert_numerateur(self, input, expected): |
| 299 |
i = _rechercher_numerateur(input) |
| 300 |
self.assertIsNotNone(i) |
| 301 |
self.assertEqual(input[i:], expected) |
| 302 |
|
| 303 |
def test_rechercher_numerateur(self): |
| 304 |
self.assert_numerateur('2', '2') |
| 305 |
self.assert_numerateur('-2', '2') |
| 306 |
self.assert_numerateur('1+2', '2') |
| 307 |
self.assert_numerateur('cos(x)', 'cos(x)') |
| 308 |
self.assert_numerateur('x-2^cos(x)', '2^cos(x)') |
| 309 |
self.assert_numerateur('3-(x+1)^(x-2^cos(x))', '(x+1)^(x-2^cos(x))') |
| 310 |
self.assert_numerateur('3-@', '@') |
| 311 |
self.assert_numerateur('(4/7)+8', '8') |
| 312 |
self.assert_numerateur('@+8', '8') |
| 313 |
|
| 314 |
def assert_denominateur(self, input, expected): |
| 315 |
i = _rechercher_denominateur(input) |
| 316 |
self.assertIsNotNone(i) |
| 317 |
self.assertEqual(input[:i], expected) |
| 318 |
|
| 319 |
def test_rechercher_denominateur(self): |
| 320 |
self.assert_denominateur('2', '2') |
| 321 |
self.assert_denominateur('-2', '-2') |
| 322 |
self.assert_denominateur('1+2', '1') |
| 323 |
self.assert_denominateur('cos(x)-x', 'cos(x)') |
| 324 |
self.assert_denominateur('2^cos(x)-x', '2^cos(x)') |
| 325 |
self.assert_denominateur('(x+1)^(x-2^cos(x))-3', '(x+1)^(x-2^cos(x))') |
| 326 |
|
| 327 |
def test_mathtext_parser(self): |
| 328 |
"On teste simplement qu'aucune erreur n'est renvoyée." |
| 329 |
|
| 330 |
mathtext_parser("$A'$") |
| 331 |
mathtext_parser("$A'$") |
| 332 |
mathtext_parser("$f'$ est la dérivée") |
| 333 |
mathtext_parser("$1^{er}$ dé") |
| 334 |
mathtext_parser(r"$\left]-\infty;\frac{1}{3}\right]\cup\left[2;5\right[$") |