test_latex.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  1. from sympy.testing.pytest import raises, XFAIL
  2. from sympy.external import import_module
  3. from sympy.concrete.products import Product
  4. from sympy.concrete.summations import Sum
  5. from sympy.core.add import Add
  6. from sympy.core.function import (Derivative, Function)
  7. from sympy.core.mul import Mul
  8. from sympy.core.numbers import (E, oo)
  9. from sympy.core.power import Pow
  10. from sympy.core.relational import (GreaterThan, LessThan, StrictGreaterThan, StrictLessThan, Unequality)
  11. from sympy.core.symbol import Symbol
  12. from sympy.functions.combinatorial.factorials import (binomial, factorial)
  13. from sympy.functions.elementary.complexes import (Abs, conjugate)
  14. from sympy.functions.elementary.exponential import (exp, log)
  15. from sympy.functions.elementary.integers import (ceiling, floor)
  16. from sympy.functions.elementary.miscellaneous import (root, sqrt)
  17. from sympy.functions.elementary.trigonometric import (asin, cos, csc, sec, sin, tan)
  18. from sympy.integrals.integrals import Integral
  19. from sympy.series.limits import Limit
  20. from sympy.core.relational import Eq, Ne, Lt, Le, Gt, Ge
  21. from sympy.physics.quantum.state import Bra, Ket
  22. from sympy.abc import x, y, z, a, b, c, t, k, n
  23. antlr4 = import_module("antlr4")
  24. # disable tests if antlr4-python3-runtime is not present
  25. if not antlr4:
  26. disabled = True
  27. theta = Symbol('theta')
  28. f = Function('f')
  29. # shorthand definitions
  30. def _Add(a, b):
  31. return Add(a, b, evaluate=False)
  32. def _Mul(a, b):
  33. return Mul(a, b, evaluate=False)
  34. def _Pow(a, b):
  35. return Pow(a, b, evaluate=False)
  36. def _Sqrt(a):
  37. return sqrt(a, evaluate=False)
  38. def _Conjugate(a):
  39. return conjugate(a, evaluate=False)
  40. def _Abs(a):
  41. return Abs(a, evaluate=False)
  42. def _factorial(a):
  43. return factorial(a, evaluate=False)
  44. def _exp(a):
  45. return exp(a, evaluate=False)
  46. def _log(a, b):
  47. return log(a, b, evaluate=False)
  48. def _binomial(n, k):
  49. return binomial(n, k, evaluate=False)
  50. def test_import():
  51. from sympy.parsing.latex._build_latex_antlr import (
  52. build_parser,
  53. check_antlr_version,
  54. dir_latex_antlr
  55. )
  56. # XXX: It would be better to come up with a test for these...
  57. del build_parser, check_antlr_version, dir_latex_antlr
  58. # These LaTeX strings should parse to the corresponding SymPy expression
  59. GOOD_PAIRS = [
  60. (r"0", 0),
  61. (r"1", 1),
  62. (r"-3.14", -3.14),
  63. (r"(-7.13)(1.5)", _Mul(-7.13, 1.5)),
  64. (r"x", x),
  65. (r"2x", 2*x),
  66. (r"x^2", x**2),
  67. (r"x^\frac{1}{2}", _Pow(x, _Pow(2, -1))),
  68. (r"x^{3 + 1}", x**_Add(3, 1)),
  69. (r"-c", -c),
  70. (r"a \cdot b", a * b),
  71. (r"a / b", a / b),
  72. (r"a \div b", a / b),
  73. (r"a + b", a + b),
  74. (r"a + b - a", _Add(a+b, -a)),
  75. (r"a^2 + b^2 = c^2", Eq(a**2 + b**2, c**2)),
  76. (r"(x + y) z", _Mul(_Add(x, y), z)),
  77. (r"a'b+ab'", _Add(_Mul(Symbol("a'"), b), _Mul(a, Symbol("b'")))),
  78. (r"y''_1", Symbol("y_{1}''")),
  79. (r"y_1''", Symbol("y_{1}''")),
  80. (r"\left(x + y\right) z", _Mul(_Add(x, y), z)),
  81. (r"\left( x + y\right ) z", _Mul(_Add(x, y), z)),
  82. (r"\left( x + y\right ) z", _Mul(_Add(x, y), z)),
  83. (r"\left[x + y\right] z", _Mul(_Add(x, y), z)),
  84. (r"\left\{x + y\right\} z", _Mul(_Add(x, y), z)),
  85. (r"1+1", _Add(1, 1)),
  86. (r"0+1", _Add(0, 1)),
  87. (r"1*2", _Mul(1, 2)),
  88. (r"0*1", _Mul(0, 1)),
  89. (r"1 \times 2 ", _Mul(1, 2)),
  90. (r"x = y", Eq(x, y)),
  91. (r"x \neq y", Ne(x, y)),
  92. (r"x < y", Lt(x, y)),
  93. (r"x > y", Gt(x, y)),
  94. (r"x \leq y", Le(x, y)),
  95. (r"x \geq y", Ge(x, y)),
  96. (r"x \le y", Le(x, y)),
  97. (r"x \ge y", Ge(x, y)),
  98. (r"\lfloor x \rfloor", floor(x)),
  99. (r"\lceil x \rceil", ceiling(x)),
  100. (r"\langle x |", Bra('x')),
  101. (r"| x \rangle", Ket('x')),
  102. (r"\sin \theta", sin(theta)),
  103. (r"\sin(\theta)", sin(theta)),
  104. (r"\sin^{-1} a", asin(a)),
  105. (r"\sin a \cos b", _Mul(sin(a), cos(b))),
  106. (r"\sin \cos \theta", sin(cos(theta))),
  107. (r"\sin(\cos \theta)", sin(cos(theta))),
  108. (r"\frac{a}{b}", a / b),
  109. (r"\dfrac{a}{b}", a / b),
  110. (r"\tfrac{a}{b}", a / b),
  111. (r"\frac12", _Pow(2, -1)),
  112. (r"\frac12y", _Mul(_Pow(2, -1), y)),
  113. (r"\frac1234", _Mul(_Pow(2, -1), 34)),
  114. (r"\frac2{3}", _Mul(2, _Pow(3, -1))),
  115. (r"\frac{\sin{x}}2", _Mul(sin(x), _Pow(2, -1))),
  116. (r"\frac{a + b}{c}", _Mul(a + b, _Pow(c, -1))),
  117. (r"\frac{7}{3}", _Mul(7, _Pow(3, -1))),
  118. (r"(\csc x)(\sec y)", csc(x)*sec(y)),
  119. (r"\lim_{x \to 3} a", Limit(a, x, 3, dir='+-')),
  120. (r"\lim_{x \rightarrow 3} a", Limit(a, x, 3, dir='+-')),
  121. (r"\lim_{x \Rightarrow 3} a", Limit(a, x, 3, dir='+-')),
  122. (r"\lim_{x \longrightarrow 3} a", Limit(a, x, 3, dir='+-')),
  123. (r"\lim_{x \Longrightarrow 3} a", Limit(a, x, 3, dir='+-')),
  124. (r"\lim_{x \to 3^{+}} a", Limit(a, x, 3, dir='+')),
  125. (r"\lim_{x \to 3^{-}} a", Limit(a, x, 3, dir='-')),
  126. (r"\lim_{x \to 3^+} a", Limit(a, x, 3, dir='+')),
  127. (r"\lim_{x \to 3^-} a", Limit(a, x, 3, dir='-')),
  128. (r"\infty", oo),
  129. (r"\lim_{x \to \infty} \frac{1}{x}", Limit(_Pow(x, -1), x, oo)),
  130. (r"\frac{d}{dx} x", Derivative(x, x)),
  131. (r"\frac{d}{dt} x", Derivative(x, t)),
  132. (r"f(x)", f(x)),
  133. (r"f(x, y)", f(x, y)),
  134. (r"f(x, y, z)", f(x, y, z)),
  135. (r"f'_1(x)", Function("f_{1}'")(x)),
  136. (r"f_{1}''(x+y)", Function("f_{1}''")(x+y)),
  137. (r"\frac{d f(x)}{dx}", Derivative(f(x), x)),
  138. (r"\frac{d\theta(x)}{dx}", Derivative(Function('theta')(x), x)),
  139. (r"x \neq y", Unequality(x, y)),
  140. (r"|x|", _Abs(x)),
  141. (r"||x||", _Abs(Abs(x))),
  142. (r"|x||y|", _Abs(x)*_Abs(y)),
  143. (r"||x||y||", _Abs(_Abs(x)*_Abs(y))),
  144. (r"\pi^{|xy|}", Symbol('pi')**_Abs(x*y)),
  145. (r"\int x dx", Integral(x, x)),
  146. (r"\int x d\theta", Integral(x, theta)),
  147. (r"\int (x^2 - y)dx", Integral(x**2 - y, x)),
  148. (r"\int x + a dx", Integral(_Add(x, a), x)),
  149. (r"\int da", Integral(1, a)),
  150. (r"\int_0^7 dx", Integral(1, (x, 0, 7))),
  151. (r"\int\limits_{0}^{1} x dx", Integral(x, (x, 0, 1))),
  152. (r"\int_a^b x dx", Integral(x, (x, a, b))),
  153. (r"\int^b_a x dx", Integral(x, (x, a, b))),
  154. (r"\int_{a}^b x dx", Integral(x, (x, a, b))),
  155. (r"\int^{b}_a x dx", Integral(x, (x, a, b))),
  156. (r"\int_{a}^{b} x dx", Integral(x, (x, a, b))),
  157. (r"\int^{b}_{a} x dx", Integral(x, (x, a, b))),
  158. (r"\int_{f(a)}^{f(b)} f(z) dz", Integral(f(z), (z, f(a), f(b)))),
  159. (r"\int (x+a)", Integral(_Add(x, a), x)),
  160. (r"\int a + b + c dx", Integral(_Add(_Add(a, b), c), x)),
  161. (r"\int \frac{dz}{z}", Integral(Pow(z, -1), z)),
  162. (r"\int \frac{3 dz}{z}", Integral(3*Pow(z, -1), z)),
  163. (r"\int \frac{1}{x} dx", Integral(Pow(x, -1), x)),
  164. (r"\int \frac{1}{a} + \frac{1}{b} dx",
  165. Integral(_Add(_Pow(a, -1), Pow(b, -1)), x)),
  166. (r"\int \frac{3 \cdot d\theta}{\theta}",
  167. Integral(3*_Pow(theta, -1), theta)),
  168. (r"\int \frac{1}{x} + 1 dx", Integral(_Add(_Pow(x, -1), 1), x)),
  169. (r"x_0", Symbol('x_{0}')),
  170. (r"x_{1}", Symbol('x_{1}')),
  171. (r"x_a", Symbol('x_{a}')),
  172. (r"x_{b}", Symbol('x_{b}')),
  173. (r"h_\theta", Symbol('h_{theta}')),
  174. (r"h_{\theta}", Symbol('h_{theta}')),
  175. (r"h_{\theta}(x_0, x_1)",
  176. Function('h_{theta}')(Symbol('x_{0}'), Symbol('x_{1}'))),
  177. (r"x!", _factorial(x)),
  178. (r"100!", _factorial(100)),
  179. (r"\theta!", _factorial(theta)),
  180. (r"(x + 1)!", _factorial(_Add(x, 1))),
  181. (r"(x!)!", _factorial(_factorial(x))),
  182. (r"x!!!", _factorial(_factorial(_factorial(x)))),
  183. (r"5!7!", _Mul(_factorial(5), _factorial(7))),
  184. (r"\sqrt{x}", sqrt(x)),
  185. (r"\sqrt{x + b}", sqrt(_Add(x, b))),
  186. (r"\sqrt[3]{\sin x}", root(sin(x), 3)),
  187. (r"\sqrt[y]{\sin x}", root(sin(x), y)),
  188. (r"\sqrt[\theta]{\sin x}", root(sin(x), theta)),
  189. (r"\sqrt{\frac{12}{6}}", _Sqrt(_Mul(12, _Pow(6, -1)))),
  190. (r"\overline{z}", _Conjugate(z)),
  191. (r"\overline{\overline{z}}", _Conjugate(_Conjugate(z))),
  192. (r"\overline{x + y}", _Conjugate(_Add(x, y))),
  193. (r"\overline{x} + \overline{y}", _Conjugate(x) + _Conjugate(y)),
  194. (r"x < y", StrictLessThan(x, y)),
  195. (r"x \leq y", LessThan(x, y)),
  196. (r"x > y", StrictGreaterThan(x, y)),
  197. (r"x \geq y", GreaterThan(x, y)),
  198. (r"\mathit{x}", Symbol('x')),
  199. (r"\mathit{test}", Symbol('test')),
  200. (r"\mathit{TEST}", Symbol('TEST')),
  201. (r"\mathit{HELLO world}", Symbol('HELLO world')),
  202. (r"\sum_{k = 1}^{3} c", Sum(c, (k, 1, 3))),
  203. (r"\sum_{k = 1}^3 c", Sum(c, (k, 1, 3))),
  204. (r"\sum^{3}_{k = 1} c", Sum(c, (k, 1, 3))),
  205. (r"\sum^3_{k = 1} c", Sum(c, (k, 1, 3))),
  206. (r"\sum_{k = 1}^{10} k^2", Sum(k**2, (k, 1, 10))),
  207. (r"\sum_{n = 0}^{\infty} \frac{1}{n!}",
  208. Sum(_Pow(_factorial(n), -1), (n, 0, oo))),
  209. (r"\prod_{a = b}^{c} x", Product(x, (a, b, c))),
  210. (r"\prod_{a = b}^c x", Product(x, (a, b, c))),
  211. (r"\prod^{c}_{a = b} x", Product(x, (a, b, c))),
  212. (r"\prod^c_{a = b} x", Product(x, (a, b, c))),
  213. (r"\exp x", _exp(x)),
  214. (r"\exp(x)", _exp(x)),
  215. (r"\lg x", _log(x, 10)),
  216. (r"\ln x", _log(x, E)),
  217. (r"\ln xy", _log(x*y, E)),
  218. (r"\log x", _log(x, E)),
  219. (r"\log xy", _log(x*y, E)),
  220. (r"\log_{2} x", _log(x, 2)),
  221. (r"\log_{a} x", _log(x, a)),
  222. (r"\log_{11} x", _log(x, 11)),
  223. (r"\log_{a^2} x", _log(x, _Pow(a, 2))),
  224. (r"[x]", x),
  225. (r"[a + b]", _Add(a, b)),
  226. (r"\frac{d}{dx} [ \tan x ]", Derivative(tan(x), x)),
  227. (r"\binom{n}{k}", _binomial(n, k)),
  228. (r"\tbinom{n}{k}", _binomial(n, k)),
  229. (r"\dbinom{n}{k}", _binomial(n, k)),
  230. (r"\binom{n}{0}", _binomial(n, 0)),
  231. (r"x^\binom{n}{k}", _Pow(x, _binomial(n, k))),
  232. (r"a \, b", _Mul(a, b)),
  233. (r"a \thinspace b", _Mul(a, b)),
  234. (r"a \: b", _Mul(a, b)),
  235. (r"a \medspace b", _Mul(a, b)),
  236. (r"a \; b", _Mul(a, b)),
  237. (r"a \thickspace b", _Mul(a, b)),
  238. (r"a \quad b", _Mul(a, b)),
  239. (r"a \qquad b", _Mul(a, b)),
  240. (r"a \! b", _Mul(a, b)),
  241. (r"a \negthinspace b", _Mul(a, b)),
  242. (r"a \negmedspace b", _Mul(a, b)),
  243. (r"a \negthickspace b", _Mul(a, b)),
  244. (r"\int x \, dx", Integral(x, x)),
  245. (r"\log_2 x", _log(x, 2)),
  246. (r"\log_a x", _log(x, a)),
  247. (r"5^0 - 4^0", _Add(_Pow(5, 0), _Mul(-1, _Pow(4, 0)))),
  248. (r"3x - 1", _Add(_Mul(3, x), -1))
  249. ]
  250. def test_parseable():
  251. from sympy.parsing.latex import parse_latex
  252. for latex_str, sympy_expr in GOOD_PAIRS:
  253. assert parse_latex(latex_str) == sympy_expr, latex_str
  254. # These bad LaTeX strings should raise a LaTeXParsingError when parsed
  255. BAD_STRINGS = [
  256. r"(",
  257. r")",
  258. r"\frac{d}{dx}",
  259. r"(\frac{d}{dx})",
  260. r"\sqrt{}",
  261. r"\sqrt",
  262. r"\overline{}",
  263. r"\overline",
  264. r"{",
  265. r"}",
  266. r"\mathit{x + y}",
  267. r"\mathit{21}",
  268. r"\frac{2}{}",
  269. r"\frac{}{2}",
  270. r"\int",
  271. r"!",
  272. r"!0",
  273. r"_",
  274. r"^",
  275. r"|",
  276. r"||x|",
  277. r"()",
  278. r"((((((((((((((((()))))))))))))))))",
  279. r"-",
  280. r"\frac{d}{dx} + \frac{d}{dt}",
  281. r"f(x,,y)",
  282. r"f(x,y,",
  283. r"\sin^x",
  284. r"\cos^2",
  285. r"@",
  286. r"#",
  287. r"$",
  288. r"%",
  289. r"&",
  290. r"*",
  291. r"" "\\",
  292. r"~",
  293. r"\frac{(2 + x}{1 - x)}",
  294. ]
  295. def test_not_parseable():
  296. from sympy.parsing.latex import parse_latex, LaTeXParsingError
  297. for latex_str in BAD_STRINGS:
  298. with raises(LaTeXParsingError):
  299. parse_latex(latex_str)
  300. # At time of migration from latex2sympy, should fail but doesn't
  301. FAILING_BAD_STRINGS = [
  302. r"\cos 1 \cos",
  303. r"f(,",
  304. r"f()",
  305. r"a \div \div b",
  306. r"a \cdot \cdot b",
  307. r"a // b",
  308. r"a +",
  309. r"1.1.1",
  310. r"1 +",
  311. r"a / b /",
  312. ]
  313. @XFAIL
  314. def test_failing_not_parseable():
  315. from sympy.parsing.latex import parse_latex, LaTeXParsingError
  316. for latex_str in FAILING_BAD_STRINGS:
  317. with raises(LaTeXParsingError):
  318. parse_latex(latex_str)