图片解析应用
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.

209 lines
8.1 KiB

  1. from sympy.core.function import (Derivative, Function)
  2. from sympy.core.numbers import (I, Rational, oo, pi)
  3. from sympy.core.relational import (Eq, Ge, Gt, Le, Lt, Ne)
  4. from sympy.core.symbol import (Symbol, symbols)
  5. from sympy.functions.elementary.complexes import (Abs, conjugate)
  6. from sympy.functions.elementary.exponential import (exp, log)
  7. from sympy.functions.elementary.miscellaneous import sqrt
  8. from sympy.functions.elementary.trigonometric import sin
  9. from sympy.integrals.integrals import Integral
  10. from sympy.matrices.dense import Matrix
  11. from sympy.series.limits import limit
  12. from sympy.printing.python import python
  13. from sympy.testing.pytest import raises, XFAIL, skip
  14. from sympy.parsing.latex import parse_latex
  15. from sympy.external import import_module
  16. # To test latex to Python printing
  17. antlr4 = import_module("antlr4")
  18. x, y = symbols('x,y')
  19. th = Symbol('theta')
  20. ph = Symbol('phi')
  21. def test_python_basic():
  22. # Simple numbers/symbols
  23. assert python(-Rational(1)/2) == "e = Rational(-1, 2)"
  24. assert python(-Rational(13)/22) == "e = Rational(-13, 22)"
  25. assert python(oo) == "e = oo"
  26. # Powers
  27. assert python(x**2) == "x = Symbol(\'x\')\ne = x**2"
  28. assert python(1/x) == "x = Symbol('x')\ne = 1/x"
  29. assert python(y*x**-2) == "y = Symbol('y')\nx = Symbol('x')\ne = y/x**2"
  30. assert python(
  31. x**Rational(-5, 2)) == "x = Symbol('x')\ne = x**Rational(-5, 2)"
  32. # Sums of terms
  33. assert python(x**2 + x + 1) in [
  34. "x = Symbol('x')\ne = 1 + x + x**2",
  35. "x = Symbol('x')\ne = x + x**2 + 1",
  36. "x = Symbol('x')\ne = x**2 + x + 1", ]
  37. assert python(1 - x) in [
  38. "x = Symbol('x')\ne = 1 - x",
  39. "x = Symbol('x')\ne = -x + 1"]
  40. assert python(1 - 2*x) in [
  41. "x = Symbol('x')\ne = 1 - 2*x",
  42. "x = Symbol('x')\ne = -2*x + 1"]
  43. assert python(1 - Rational(3, 2)*y/x) in [
  44. "y = Symbol('y')\nx = Symbol('x')\ne = 1 - 3/2*y/x",
  45. "y = Symbol('y')\nx = Symbol('x')\ne = -3/2*y/x + 1",
  46. "y = Symbol('y')\nx = Symbol('x')\ne = 1 - 3*y/(2*x)"]
  47. # Multiplication
  48. assert python(x/y) == "x = Symbol('x')\ny = Symbol('y')\ne = x/y"
  49. assert python(-x/y) == "x = Symbol('x')\ny = Symbol('y')\ne = -x/y"
  50. assert python((x + 2)/y) in [
  51. "y = Symbol('y')\nx = Symbol('x')\ne = 1/y*(2 + x)",
  52. "y = Symbol('y')\nx = Symbol('x')\ne = 1/y*(x + 2)",
  53. "x = Symbol('x')\ny = Symbol('y')\ne = 1/y*(2 + x)",
  54. "x = Symbol('x')\ny = Symbol('y')\ne = (2 + x)/y",
  55. "x = Symbol('x')\ny = Symbol('y')\ne = (x + 2)/y"]
  56. assert python((1 + x)*y) in [
  57. "y = Symbol('y')\nx = Symbol('x')\ne = y*(1 + x)",
  58. "y = Symbol('y')\nx = Symbol('x')\ne = y*(x + 1)", ]
  59. # Check for proper placement of negative sign
  60. assert python(-5*x/(x + 10)) == "x = Symbol('x')\ne = -5*x/(x + 10)"
  61. assert python(1 - Rational(3, 2)*(x + 1)) in [
  62. "x = Symbol('x')\ne = Rational(-3, 2)*x + Rational(-1, 2)",
  63. "x = Symbol('x')\ne = -3*x/2 + Rational(-1, 2)",
  64. "x = Symbol('x')\ne = -3*x/2 + Rational(-1, 2)"
  65. ]
  66. def test_python_keyword_symbol_name_escaping():
  67. # Check for escaping of keywords
  68. assert python(
  69. 5*Symbol("lambda")) == "lambda_ = Symbol('lambda')\ne = 5*lambda_"
  70. assert (python(5*Symbol("lambda") + 7*Symbol("lambda_")) ==
  71. "lambda__ = Symbol('lambda')\nlambda_ = Symbol('lambda_')\ne = 7*lambda_ + 5*lambda__")
  72. assert (python(5*Symbol("for") + Function("for_")(8)) ==
  73. "for__ = Symbol('for')\nfor_ = Function('for_')\ne = 5*for__ + for_(8)")
  74. def test_python_keyword_function_name_escaping():
  75. assert python(
  76. 5*Function("for")(8)) == "for_ = Function('for')\ne = 5*for_(8)"
  77. def test_python_relational():
  78. assert python(Eq(x, y)) == "x = Symbol('x')\ny = Symbol('y')\ne = Eq(x, y)"
  79. assert python(Ge(x, y)) == "x = Symbol('x')\ny = Symbol('y')\ne = x >= y"
  80. assert python(Le(x, y)) == "x = Symbol('x')\ny = Symbol('y')\ne = x <= y"
  81. assert python(Gt(x, y)) == "x = Symbol('x')\ny = Symbol('y')\ne = x > y"
  82. assert python(Lt(x, y)) == "x = Symbol('x')\ny = Symbol('y')\ne = x < y"
  83. assert python(Ne(x/(y + 1), y**2)) in [
  84. "x = Symbol('x')\ny = Symbol('y')\ne = Ne(x/(1 + y), y**2)",
  85. "x = Symbol('x')\ny = Symbol('y')\ne = Ne(x/(y + 1), y**2)"]
  86. def test_python_functions():
  87. # Simple
  88. assert python(2*x + exp(x)) in "x = Symbol('x')\ne = 2*x + exp(x)"
  89. assert python(sqrt(2)) == 'e = sqrt(2)'
  90. assert python(2**Rational(1, 3)) == 'e = 2**Rational(1, 3)'
  91. assert python(sqrt(2 + pi)) == 'e = sqrt(2 + pi)'
  92. assert python((2 + pi)**Rational(1, 3)) == 'e = (2 + pi)**Rational(1, 3)'
  93. assert python(2**Rational(1, 4)) == 'e = 2**Rational(1, 4)'
  94. assert python(Abs(x)) == "x = Symbol('x')\ne = Abs(x)"
  95. assert python(
  96. Abs(x/(x**2 + 1))) in ["x = Symbol('x')\ne = Abs(x/(1 + x**2))",
  97. "x = Symbol('x')\ne = Abs(x/(x**2 + 1))"]
  98. # Univariate/Multivariate functions
  99. f = Function('f')
  100. assert python(f(x)) == "x = Symbol('x')\nf = Function('f')\ne = f(x)"
  101. assert python(f(x, y)) == "x = Symbol('x')\ny = Symbol('y')\nf = Function('f')\ne = f(x, y)"
  102. assert python(f(x/(y + 1), y)) in [
  103. "x = Symbol('x')\ny = Symbol('y')\nf = Function('f')\ne = f(x/(1 + y), y)",
  104. "x = Symbol('x')\ny = Symbol('y')\nf = Function('f')\ne = f(x/(y + 1), y)"]
  105. # Nesting of square roots
  106. assert python(sqrt((sqrt(x + 1)) + 1)) in [
  107. "x = Symbol('x')\ne = sqrt(1 + sqrt(1 + x))",
  108. "x = Symbol('x')\ne = sqrt(sqrt(x + 1) + 1)"]
  109. # Nesting of powers
  110. assert python((((x + 1)**Rational(1, 3)) + 1)**Rational(1, 3)) in [
  111. "x = Symbol('x')\ne = (1 + (1 + x)**Rational(1, 3))**Rational(1, 3)",
  112. "x = Symbol('x')\ne = ((x + 1)**Rational(1, 3) + 1)**Rational(1, 3)"]
  113. # Function powers
  114. assert python(sin(x)**2) == "x = Symbol('x')\ne = sin(x)**2"
  115. @XFAIL
  116. def test_python_functions_conjugates():
  117. a, b = map(Symbol, 'ab')
  118. assert python( conjugate(a + b*I) ) == '_ _\na - I*b'
  119. assert python( conjugate(exp(a + b*I)) ) == ' _ _\n a - I*b\ne '
  120. def test_python_derivatives():
  121. # Simple
  122. f_1 = Derivative(log(x), x, evaluate=False)
  123. assert python(f_1) == "x = Symbol('x')\ne = Derivative(log(x), x)"
  124. f_2 = Derivative(log(x), x, evaluate=False) + x
  125. assert python(f_2) == "x = Symbol('x')\ne = x + Derivative(log(x), x)"
  126. # Multiple symbols
  127. f_3 = Derivative(log(x) + x**2, x, y, evaluate=False)
  128. assert python(f_3) == \
  129. "x = Symbol('x')\ny = Symbol('y')\ne = Derivative(x**2 + log(x), x, y)"
  130. f_4 = Derivative(2*x*y, y, x, evaluate=False) + x**2
  131. assert python(f_4) in [
  132. "x = Symbol('x')\ny = Symbol('y')\ne = x**2 + Derivative(2*x*y, y, x)",
  133. "x = Symbol('x')\ny = Symbol('y')\ne = Derivative(2*x*y, y, x) + x**2"]
  134. def test_python_integrals():
  135. # Simple
  136. f_1 = Integral(log(x), x)
  137. assert python(f_1) == "x = Symbol('x')\ne = Integral(log(x), x)"
  138. f_2 = Integral(x**2, x)
  139. assert python(f_2) == "x = Symbol('x')\ne = Integral(x**2, x)"
  140. # Double nesting of pow
  141. f_3 = Integral(x**(2**x), x)
  142. assert python(f_3) == "x = Symbol('x')\ne = Integral(x**(2**x), x)"
  143. # Definite integrals
  144. f_4 = Integral(x**2, (x, 1, 2))
  145. assert python(f_4) == "x = Symbol('x')\ne = Integral(x**2, (x, 1, 2))"
  146. f_5 = Integral(x**2, (x, Rational(1, 2), 10))
  147. assert python(
  148. f_5) == "x = Symbol('x')\ne = Integral(x**2, (x, Rational(1, 2), 10))"
  149. # Nested integrals
  150. f_6 = Integral(x**2*y**2, x, y)
  151. assert python(f_6) == "x = Symbol('x')\ny = Symbol('y')\ne = Integral(x**2*y**2, x, y)"
  152. def test_python_matrix():
  153. p = python(Matrix([[x**2+1, 1], [y, x+y]]))
  154. s = "x = Symbol('x')\ny = Symbol('y')\ne = MutableDenseMatrix([[x**2 + 1, 1], [y, x + y]])"
  155. assert p == s
  156. def test_python_limits():
  157. assert python(limit(x, x, oo)) == 'e = oo'
  158. assert python(limit(x**2, x, 0)) == 'e = 0'
  159. def test_issue_20762():
  160. if not antlr4:
  161. skip('antlr not installed')
  162. # Make sure Python removes curly braces from subscripted variables
  163. expr = parse_latex(r'a_b \cdot b')
  164. assert python(expr) == "a_b = Symbol('a_{b}')\nb = Symbol('b')\ne = a_b*b"
  165. def test_settings():
  166. raises(TypeError, lambda: python(x, method="garbage"))