test_parameters.py 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. from sympy.abc import x, y
  2. from sympy.core.parameters import evaluate
  3. from sympy.core import Mul, Add, Pow, S
  4. from sympy.core.numbers import oo
  5. from sympy.functions.elementary.miscellaneous import sqrt
  6. def test_add():
  7. with evaluate(False):
  8. p = oo - oo
  9. assert isinstance(p, Add) and p.args == (oo, -oo)
  10. p = 5 - oo
  11. assert isinstance(p, Add) and p.args == (-oo, 5)
  12. p = oo - 5
  13. assert isinstance(p, Add) and p.args == (oo, -5)
  14. p = oo + 5
  15. assert isinstance(p, Add) and p.args == (oo, 5)
  16. p = 5 + oo
  17. assert isinstance(p, Add) and p.args == (oo, 5)
  18. p = -oo + 5
  19. assert isinstance(p, Add) and p.args == (-oo, 5)
  20. p = -5 - oo
  21. assert isinstance(p, Add) and p.args == (-oo, -5)
  22. with evaluate(False):
  23. expr = x + x
  24. assert isinstance(expr, Add)
  25. assert expr.args == (x, x)
  26. with evaluate(True):
  27. assert (x + x).args == (2, x)
  28. assert (x + x).args == (x, x)
  29. assert isinstance(x + x, Mul)
  30. with evaluate(False):
  31. assert S.One + 1 == Add(1, 1)
  32. assert 1 + S.One == Add(1, 1)
  33. assert S(4) - 3 == Add(4, -3)
  34. assert -3 + S(4) == Add(4, -3)
  35. assert S(2) * 4 == Mul(2, 4)
  36. assert 4 * S(2) == Mul(2, 4)
  37. assert S(6) / 3 == Mul(6, Pow(3, -1))
  38. assert S.One / 3 * 6 == Mul(S.One / 3, 6)
  39. assert 9 ** S(2) == Pow(9, 2)
  40. assert S(2) ** 9 == Pow(2, 9)
  41. assert S(2) / 2 == Mul(2, Pow(2, -1))
  42. assert S.One / 2 * 2 == Mul(S.One / 2, 2)
  43. assert S(2) / 3 + 1 == Add(S(2) / 3, 1)
  44. assert 1 + S(2) / 3 == Add(1, S(2) / 3)
  45. assert S(4) / 7 - 3 == Add(S(4) / 7, -3)
  46. assert -3 + S(4) / 7 == Add(-3, S(4) / 7)
  47. assert S(2) / 4 * 4 == Mul(S(2) / 4, 4)
  48. assert 4 * (S(2) / 4) == Mul(4, S(2) / 4)
  49. assert S(6) / 3 == Mul(6, Pow(3, -1))
  50. assert S.One / 3 * 6 == Mul(S.One / 3, 6)
  51. assert S.One / 3 + sqrt(3) == Add(S.One / 3, sqrt(3))
  52. assert sqrt(3) + S.One / 3 == Add(sqrt(3), S.One / 3)
  53. assert S.One / 2 * 10.333 == Mul(S.One / 2, 10.333)
  54. assert 10.333 * (S.One / 2) == Mul(10.333, S.One / 2)
  55. assert sqrt(2) * sqrt(2) == Mul(sqrt(2), sqrt(2))
  56. assert S.One / 2 + x == Add(S.One / 2, x)
  57. assert x + S.One / 2 == Add(x, S.One / 2)
  58. assert S.One / x * x == Mul(S.One / x, x)
  59. assert x * (S.One / x) == Mul(x, Pow(x, -1))
  60. assert S.One / 3 == Pow(3, -1)
  61. assert S.One / x == Pow(x, -1)
  62. assert 1 / S(3) == Pow(3, -1)
  63. assert 1 / x == Pow(x, -1)
  64. def test_nested():
  65. with evaluate(False):
  66. expr = (x + x) + (y + y)
  67. assert expr.args == ((x + x), (y + y))
  68. assert expr.args[0].args == (x, x)