test_eval.py 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. from sympy.core.function import Function
  2. from sympy.core.numbers import (I, Rational)
  3. from sympy.core.singleton import S
  4. from sympy.core.symbol import Symbol
  5. from sympy.functions.elementary.exponential import exp
  6. from sympy.functions.elementary.miscellaneous import sqrt
  7. from sympy.functions.elementary.trigonometric import (cos, tan)
  8. from sympy.testing.pytest import XFAIL
  9. def test_add_eval():
  10. a = Symbol("a")
  11. b = Symbol("b")
  12. c = Rational(1)
  13. p = Rational(5)
  14. assert a*b + c + p == a*b + 6
  15. assert c + a + p == a + 6
  16. assert c + a - p == a + (-4)
  17. assert a + a == 2*a
  18. assert a + p + a == 2*a + 5
  19. assert c + p == Rational(6)
  20. assert b + a - b == a
  21. def test_addmul_eval():
  22. a = Symbol("a")
  23. b = Symbol("b")
  24. c = Rational(1)
  25. p = Rational(5)
  26. assert c + a + b*c + a - p == 2*a + b + (-4)
  27. assert a*2 + p + a == a*2 + 5 + a
  28. assert a*2 + p + a == 3*a + 5
  29. assert a*2 + a == 3*a
  30. def test_pow_eval():
  31. # XXX Pow does not fully support conversion of negative numbers
  32. # to their complex equivalent
  33. assert sqrt(-1) == I
  34. assert sqrt(-4) == 2*I
  35. assert sqrt( 4) == 2
  36. assert (8)**Rational(1, 3) == 2
  37. assert (-8)**Rational(1, 3) == 2*((-1)**Rational(1, 3))
  38. assert sqrt(-2) == I*sqrt(2)
  39. assert (-1)**Rational(1, 3) != I
  40. assert (-10)**Rational(1, 3) != I*((10)**Rational(1, 3))
  41. assert (-2)**Rational(1, 4) != (2)**Rational(1, 4)
  42. assert 64**Rational(1, 3) == 4
  43. assert 64**Rational(2, 3) == 16
  44. assert 24/sqrt(64) == 3
  45. assert (-27)**Rational(1, 3) == 3*(-1)**Rational(1, 3)
  46. assert (cos(2) / tan(2))**2 == (cos(2) / tan(2))**2
  47. @XFAIL
  48. def test_pow_eval_X1():
  49. assert (-1)**Rational(1, 3) == S.Half + S.Half*I*sqrt(3)
  50. def test_mulpow_eval():
  51. x = Symbol('x')
  52. assert sqrt(50)/(sqrt(2)*x) == 5/x
  53. assert sqrt(27)/sqrt(3) == 3
  54. def test_evalpow_bug():
  55. x = Symbol("x")
  56. assert 1/(1/x) == x
  57. assert 1/(-1/x) == -x
  58. def test_symbol_expand():
  59. x = Symbol('x')
  60. y = Symbol('y')
  61. f = x**4*y**4
  62. assert f == x**4*y**4
  63. assert f == f.expand()
  64. g = (x*y)**4
  65. assert g == f
  66. assert g.expand() == f
  67. assert g.expand() == g.expand().expand()
  68. def test_function():
  69. f, l = map(Function, 'fl')
  70. x = Symbol('x')
  71. assert exp(l(x))*l(x)/exp(l(x)) == l(x)
  72. assert exp(f(x))*f(x)/exp(f(x)) == f(x)