test_python.py 7.9 KB

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