m2m模型翻译
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

332 lines
10 KiB

6 months ago
  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-python*-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^{3 + 1}", x**_Add(3, 1)),
  68. (r"-c", -c),
  69. (r"a \cdot b", a * b),
  70. (r"a / b", a / b),
  71. (r"a \div b", a / b),
  72. (r"a + b", a + b),
  73. (r"a + b - a", _Add(a+b, -a)),
  74. (r"a^2 + b^2 = c^2", Eq(a**2 + b**2, c**2)),
  75. (r"(x + y) z", _Mul(_Add(x, y), z)),
  76. (r"\left(x + y\right) z", _Mul(_Add(x, y), z)),
  77. (r"\left( x + y\right ) z", _Mul(_Add(x, y), z)),
  78. (r"\left( x + y\right ) z", _Mul(_Add(x, y), z)),
  79. (r"\left[x + y\right] z", _Mul(_Add(x, y), z)),
  80. (r"\left\{x + y\right\} z", _Mul(_Add(x, y), z)),
  81. (r"1+1", _Add(1, 1)),
  82. (r"0+1", _Add(0, 1)),
  83. (r"1*2", _Mul(1, 2)),
  84. (r"0*1", _Mul(0, 1)),
  85. (r"x = y", Eq(x, y)),
  86. (r"x \neq y", Ne(x, y)),
  87. (r"x < y", Lt(x, y)),
  88. (r"x > y", Gt(x, y)),
  89. (r"x \leq y", Le(x, y)),
  90. (r"x \geq y", Ge(x, y)),
  91. (r"x \le y", Le(x, y)),
  92. (r"x \ge y", Ge(x, y)),
  93. (r"\lfloor x \rfloor", floor(x)),
  94. (r"\lceil x \rceil", ceiling(x)),
  95. (r"\langle x |", Bra('x')),
  96. (r"| x \rangle", Ket('x')),
  97. (r"\sin \theta", sin(theta)),
  98. (r"\sin(\theta)", sin(theta)),
  99. (r"\sin^{-1} a", asin(a)),
  100. (r"\sin a \cos b", _Mul(sin(a), cos(b))),
  101. (r"\sin \cos \theta", sin(cos(theta))),
  102. (r"\sin(\cos \theta)", sin(cos(theta))),
  103. (r"\frac{a}{b}", a / b),
  104. (r"\frac{a + b}{c}", _Mul(a + b, _Pow(c, -1))),
  105. (r"\frac{7}{3}", _Mul(7, _Pow(3, -1))),
  106. (r"(\csc x)(\sec y)", csc(x)*sec(y)),
  107. (r"\lim_{x \to 3} a", Limit(a, x, 3)),
  108. (r"\lim_{x \rightarrow 3} a", Limit(a, x, 3)),
  109. (r"\lim_{x \Rightarrow 3} a", Limit(a, x, 3)),
  110. (r"\lim_{x \longrightarrow 3} a", Limit(a, x, 3)),
  111. (r"\lim_{x \Longrightarrow 3} a", Limit(a, x, 3)),
  112. (r"\lim_{x \to 3^{+}} a", Limit(a, x, 3, dir='+')),
  113. (r"\lim_{x \to 3^{-}} a", Limit(a, x, 3, dir='-')),
  114. (r"\infty", oo),
  115. (r"\lim_{x \to \infty} \frac{1}{x}", Limit(_Pow(x, -1), x, oo)),
  116. (r"\frac{d}{dx} x", Derivative(x, x)),
  117. (r"\frac{d}{dt} x", Derivative(x, t)),
  118. (r"f(x)", f(x)),
  119. (r"f(x, y)", f(x, y)),
  120. (r"f(x, y, z)", f(x, y, z)),
  121. (r"\frac{d f(x)}{dx}", Derivative(f(x), x)),
  122. (r"\frac{d\theta(x)}{dx}", Derivative(Function('theta')(x), x)),
  123. (r"x \neq y", Unequality(x, y)),
  124. (r"|x|", _Abs(x)),
  125. (r"||x||", _Abs(Abs(x))),
  126. (r"|x||y|", _Abs(x)*_Abs(y)),
  127. (r"||x||y||", _Abs(_Abs(x)*_Abs(y))),
  128. (r"\pi^{|xy|}", Symbol('pi')**_Abs(x*y)),
  129. (r"\int x dx", Integral(x, x)),
  130. (r"\int x d\theta", Integral(x, theta)),
  131. (r"\int (x^2 - y)dx", Integral(x**2 - y, x)),
  132. (r"\int x + a dx", Integral(_Add(x, a), x)),
  133. (r"\int da", Integral(1, a)),
  134. (r"\int_0^7 dx", Integral(1, (x, 0, 7))),
  135. (r"\int_a^b x dx", Integral(x, (x, a, b))),
  136. (r"\int^b_a x dx", Integral(x, (x, a, b))),
  137. (r"\int_{a}^b x dx", Integral(x, (x, a, b))),
  138. (r"\int^{b}_a x dx", Integral(x, (x, a, b))),
  139. (r"\int_{a}^{b} x dx", Integral(x, (x, a, b))),
  140. (r"\int^{b}_{a} x dx", Integral(x, (x, a, b))),
  141. (r"\int_{f(a)}^{f(b)} f(z) dz", Integral(f(z), (z, f(a), f(b)))),
  142. (r"\int (x+a)", Integral(_Add(x, a), x)),
  143. (r"\int a + b + c dx", Integral(_Add(_Add(a, b), c), x)),
  144. (r"\int \frac{dz}{z}", Integral(Pow(z, -1), z)),
  145. (r"\int \frac{3 dz}{z}", Integral(3*Pow(z, -1), z)),
  146. (r"\int \frac{1}{x} dx", Integral(Pow(x, -1), x)),
  147. (r"\int \frac{1}{a} + \frac{1}{b} dx",
  148. Integral(_Add(_Pow(a, -1), Pow(b, -1)), x)),
  149. (r"\int \frac{3 \cdot d\theta}{\theta}",
  150. Integral(3*_Pow(theta, -1), theta)),
  151. (r"\int \frac{1}{x} + 1 dx", Integral(_Add(_Pow(x, -1), 1), x)),
  152. (r"x_0", Symbol('x_{0}')),
  153. (r"x_{1}", Symbol('x_{1}')),
  154. (r"x_a", Symbol('x_{a}')),
  155. (r"x_{b}", Symbol('x_{b}')),
  156. (r"h_\theta", Symbol('h_{theta}')),
  157. (r"h_{\theta}", Symbol('h_{theta}')),
  158. (r"h_{\theta}(x_0, x_1)",
  159. Function('h_{theta}')(Symbol('x_{0}'), Symbol('x_{1}'))),
  160. (r"x!", _factorial(x)),
  161. (r"100!", _factorial(100)),
  162. (r"\theta!", _factorial(theta)),
  163. (r"(x + 1)!", _factorial(_Add(x, 1))),
  164. (r"(x!)!", _factorial(_factorial(x))),
  165. (r"x!!!", _factorial(_factorial(_factorial(x)))),
  166. (r"5!7!", _Mul(_factorial(5), _factorial(7))),
  167. (r"\sqrt{x}", sqrt(x)),
  168. (r"\sqrt{x + b}", sqrt(_Add(x, b))),
  169. (r"\sqrt[3]{\sin x}", root(sin(x), 3)),
  170. (r"\sqrt[y]{\sin x}", root(sin(x), y)),
  171. (r"\sqrt[\theta]{\sin x}", root(sin(x), theta)),
  172. (r"\sqrt{\frac{12}{6}}", _Sqrt(_Mul(12, _Pow(6, -1)))),
  173. (r"\overline{z}", _Conjugate(z)),
  174. (r"\overline{\overline{z}}", _Conjugate(_Conjugate(z))),
  175. (r"\overline{x + y}", _Conjugate(_Add(x, y))),
  176. (r"\overline{x} + \overline{y}", _Conjugate(x) + _Conjugate(y)),
  177. (r"x < y", StrictLessThan(x, y)),
  178. (r"x \leq y", LessThan(x, y)),
  179. (r"x > y", StrictGreaterThan(x, y)),
  180. (r"x \geq y", GreaterThan(x, y)),
  181. (r"\mathit{x}", Symbol('x')),
  182. (r"\mathit{test}", Symbol('test')),
  183. (r"\mathit{TEST}", Symbol('TEST')),
  184. (r"\mathit{HELLO world}", Symbol('HELLO world')),
  185. (r"\sum_{k = 1}^{3} c", Sum(c, (k, 1, 3))),
  186. (r"\sum_{k = 1}^3 c", Sum(c, (k, 1, 3))),
  187. (r"\sum^{3}_{k = 1} c", Sum(c, (k, 1, 3))),
  188. (r"\sum^3_{k = 1} c", Sum(c, (k, 1, 3))),
  189. (r"\sum_{k = 1}^{10} k^2", Sum(k**2, (k, 1, 10))),
  190. (r"\sum_{n = 0}^{\infty} \frac{1}{n!}",
  191. Sum(_Pow(_factorial(n), -1), (n, 0, oo))),
  192. (r"\prod_{a = b}^{c} x", Product(x, (a, b, c))),
  193. (r"\prod_{a = b}^c x", Product(x, (a, b, c))),
  194. (r"\prod^{c}_{a = b} x", Product(x, (a, b, c))),
  195. (r"\prod^c_{a = b} x", Product(x, (a, b, c))),
  196. (r"\exp x", _exp(x)),
  197. (r"\exp(x)", _exp(x)),
  198. (r"\ln x", _log(x, E)),
  199. (r"\ln xy", _log(x*y, E)),
  200. (r"\log x", _log(x, 10)),
  201. (r"\log xy", _log(x*y, 10)),
  202. (r"\log_{2} x", _log(x, 2)),
  203. (r"\log_{a} x", _log(x, a)),
  204. (r"\log_{11} x", _log(x, 11)),
  205. (r"\log_{a^2} x", _log(x, _Pow(a, 2))),
  206. (r"[x]", x),
  207. (r"[a + b]", _Add(a, b)),
  208. (r"\frac{d}{dx} [ \tan x ]", Derivative(tan(x), x)),
  209. (r"\binom{n}{k}", _binomial(n, k)),
  210. (r"\tbinom{n}{k}", _binomial(n, k)),
  211. (r"\dbinom{n}{k}", _binomial(n, k)),
  212. (r"\binom{n}{0}", _binomial(n, 0)),
  213. (r"a \, b", _Mul(a, b)),
  214. (r"a \thinspace b", _Mul(a, b)),
  215. (r"a \: b", _Mul(a, b)),
  216. (r"a \medspace b", _Mul(a, b)),
  217. (r"a \; b", _Mul(a, b)),
  218. (r"a \thickspace b", _Mul(a, b)),
  219. (r"a \quad b", _Mul(a, b)),
  220. (r"a \qquad b", _Mul(a, b)),
  221. (r"a \! b", _Mul(a, b)),
  222. (r"a \negthinspace b", _Mul(a, b)),
  223. (r"a \negmedspace b", _Mul(a, b)),
  224. (r"a \negthickspace b", _Mul(a, b)),
  225. (r"\int x \, dx", Integral(x, x)),
  226. (r"\log_2 x", _log(x, 2)),
  227. (r"\log_a x", _log(x, a)),
  228. (r"5^0 - 4^0", _Add(_Pow(5, 0), _Mul(-1, _Pow(4, 0)))),
  229. ]
  230. def test_parseable():
  231. from sympy.parsing.latex import parse_latex
  232. for latex_str, sympy_expr in GOOD_PAIRS:
  233. assert parse_latex(latex_str) == sympy_expr, latex_str
  234. # These bad LaTeX strings should raise a LaTeXParsingError when parsed
  235. BAD_STRINGS = [
  236. r"(",
  237. r")",
  238. r"\frac{d}{dx}",
  239. r"(\frac{d}{dx})",
  240. r"\sqrt{}",
  241. r"\sqrt",
  242. r"\overline{}",
  243. r"\overline",
  244. r"{",
  245. r"}",
  246. r"\mathit{x + y}",
  247. r"\mathit{21}",
  248. r"\frac{2}{}",
  249. r"\frac{}{2}",
  250. r"\int",
  251. r"!",
  252. r"!0",
  253. r"_",
  254. r"^",
  255. r"|",
  256. r"||x|",
  257. r"()",
  258. r"((((((((((((((((()))))))))))))))))",
  259. r"-",
  260. r"\frac{d}{dx} + \frac{d}{dt}",
  261. r"f(x,,y)",
  262. r"f(x,y,",
  263. r"\sin^x",
  264. r"\cos^2",
  265. r"@",
  266. r"#",
  267. r"$",
  268. r"%",
  269. r"&",
  270. r"*",
  271. r"" "\\",
  272. r"~",
  273. r"\frac{(2 + x}{1 - x)}",
  274. ]
  275. def test_not_parseable():
  276. from sympy.parsing.latex import parse_latex, LaTeXParsingError
  277. for latex_str in BAD_STRINGS:
  278. with raises(LaTeXParsingError):
  279. parse_latex(latex_str)
  280. # At time of migration from latex2sympy, should fail but doesn't
  281. FAILING_BAD_STRINGS = [
  282. r"\cos 1 \cos",
  283. r"f(,",
  284. r"f()",
  285. r"a \div \div b",
  286. r"a \cdot \cdot b",
  287. r"a // b",
  288. r"a +",
  289. r"1.1.1",
  290. r"1 +",
  291. r"a / b /",
  292. ]
  293. @XFAIL
  294. def test_failing_not_parseable():
  295. from sympy.parsing.latex import parse_latex, LaTeXParsingError
  296. for latex_str in FAILING_BAD_STRINGS:
  297. with raises(LaTeXParsingError):
  298. parse_latex(latex_str)