test_rationaltools.py 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. from sympy.core.numbers import (I, Rational)
  2. from sympy.core.singleton import S
  3. from sympy.core.symbol import (Dummy, symbols)
  4. from sympy.functions.elementary.exponential import log
  5. from sympy.functions.elementary.miscellaneous import sqrt
  6. from sympy.functions.elementary.trigonometric import atan
  7. from sympy.integrals.integrals import integrate
  8. from sympy.polys.polytools import Poly
  9. from sympy.simplify.simplify import simplify
  10. from sympy.integrals.rationaltools import ratint, ratint_logpart, log_to_atan
  11. from sympy.abc import a, b, x, t
  12. half = S.Half
  13. def test_ratint():
  14. assert ratint(S.Zero, x) == 0
  15. assert ratint(S(7), x) == 7*x
  16. assert ratint(x, x) == x**2/2
  17. assert ratint(2*x, x) == x**2
  18. assert ratint(-2*x, x) == -x**2
  19. assert ratint(8*x**7 + 2*x + 1, x) == x**8 + x**2 + x
  20. f = S.One
  21. g = x + 1
  22. assert ratint(f / g, x) == log(x + 1)
  23. assert ratint((f, g), x) == log(x + 1)
  24. f = x**3 - x
  25. g = x - 1
  26. assert ratint(f/g, x) == x**3/3 + x**2/2
  27. f = x
  28. g = (x - a)*(x + a)
  29. assert ratint(f/g, x) == log(x**2 - a**2)/2
  30. f = S.One
  31. g = x**2 + 1
  32. assert ratint(f/g, x, real=None) == atan(x)
  33. assert ratint(f/g, x, real=True) == atan(x)
  34. assert ratint(f/g, x, real=False) == I*log(x + I)/2 - I*log(x - I)/2
  35. f = S(36)
  36. g = x**5 - 2*x**4 - 2*x**3 + 4*x**2 + x - 2
  37. assert ratint(f/g, x) == \
  38. -4*log(x + 1) + 4*log(x - 2) + (12*x + 6)/(x**2 - 1)
  39. f = x**4 - 3*x**2 + 6
  40. g = x**6 - 5*x**4 + 5*x**2 + 4
  41. assert ratint(f/g, x) == \
  42. atan(x) + atan(x**3) + atan(x/2 - Rational(3, 2)*x**3 + S.Half*x**5)
  43. f = x**7 - 24*x**4 - 4*x**2 + 8*x - 8
  44. g = x**8 + 6*x**6 + 12*x**4 + 8*x**2
  45. assert ratint(f/g, x) == \
  46. (4 + 6*x + 8*x**2 + 3*x**3)/(4*x + 4*x**3 + x**5) + log(x)
  47. assert ratint((x**3*f)/(x*g), x) == \
  48. -(12 - 16*x + 6*x**2 - 14*x**3)/(4 + 4*x**2 + x**4) - \
  49. 5*sqrt(2)*atan(x*sqrt(2)/2) + S.Half*x**2 - 3*log(2 + x**2)
  50. f = x**5 - x**4 + 4*x**3 + x**2 - x + 5
  51. g = x**4 - 2*x**3 + 5*x**2 - 4*x + 4
  52. assert ratint(f/g, x) == \
  53. x + S.Half*x**2 + S.Half*log(2 - x + x**2) + (9 - 4*x)/(7*x**2 - 7*x + 14) + \
  54. 13*sqrt(7)*atan(Rational(-1, 7)*sqrt(7) + 2*x*sqrt(7)/7)/49
  55. assert ratint(1/(x**2 + x + 1), x) == \
  56. 2*sqrt(3)*atan(sqrt(3)/3 + 2*x*sqrt(3)/3)/3
  57. assert ratint(1/(x**3 + 1), x) == \
  58. -log(1 - x + x**2)/6 + log(1 + x)/3 + sqrt(3)*atan(-sqrt(3)
  59. /3 + 2*x*sqrt(3)/3)/3
  60. assert ratint(1/(x**2 + x + 1), x, real=False) == \
  61. -I*3**half*log(half + x - half*I*3**half)/3 + \
  62. I*3**half*log(half + x + half*I*3**half)/3
  63. assert ratint(1/(x**3 + 1), x, real=False) == log(1 + x)/3 + \
  64. (Rational(-1, 6) + I*3**half/6)*log(-half + x + I*3**half/2) + \
  65. (Rational(-1, 6) - I*3**half/6)*log(-half + x - I*3**half/2)
  66. # issue 4991
  67. assert ratint(1/(x*(a + b*x)**3), x) == \
  68. (3*a + 2*b*x)/(2*a**4 + 4*a**3*b*x + 2*a**2*b**2*x**2) + (
  69. log(x) - log(a/b + x))/a**3
  70. assert ratint(x/(1 - x**2), x) == -log(x**2 - 1)/2
  71. assert ratint(-x/(1 - x**2), x) == log(x**2 - 1)/2
  72. assert ratint((x/4 - 4/(1 - x)).diff(x), x) == x/4 + 4/(x - 1)
  73. ans = atan(x)
  74. assert ratint(1/(x**2 + 1), x, symbol=x) == ans
  75. assert ratint(1/(x**2 + 1), x, symbol='x') == ans
  76. assert ratint(1/(x**2 + 1), x, symbol=a) == ans
  77. # this asserts that as_dummy must return a unique symbol
  78. # even if the symbol is already a Dummy
  79. d = Dummy()
  80. assert ratint(1/(d**2 + 1), d, symbol=d) == atan(d)
  81. def test_ratint_logpart():
  82. assert ratint_logpart(x, x**2 - 9, x, t) == \
  83. [(Poly(x**2 - 9, x), Poly(-2*t + 1, t))]
  84. assert ratint_logpart(x**2, x**3 - 5, x, t) == \
  85. [(Poly(x**3 - 5, x), Poly(-3*t + 1, t))]
  86. def test_issue_5414():
  87. assert ratint(1/(x**2 + 16), x) == atan(x/4)/4
  88. def test_issue_5249():
  89. assert ratint(
  90. 1/(x**2 + a**2), x) == (-I*log(-I*a + x)/2 + I*log(I*a + x)/2)/a
  91. def test_issue_5817():
  92. a, b, c = symbols('a,b,c', positive=True)
  93. assert simplify(ratint(a/(b*c*x**2 + a**2 + b*a), x)) == \
  94. sqrt(a)*atan(sqrt(
  95. b)*sqrt(c)*x/(sqrt(a)*sqrt(a + b)))/(sqrt(b)*sqrt(c)*sqrt(a + b))
  96. def test_issue_5981():
  97. u = symbols('u')
  98. assert integrate(1/(u**2 + 1)) == atan(u)
  99. def test_issue_10488():
  100. a,b,c,x = symbols('a b c x', positive=True)
  101. assert integrate(x/(a*x+b),x) == x/a - b*log(a*x + b)/a**2
  102. def test_issues_8246_12050_13501_14080():
  103. a = symbols('a', nonzero=True)
  104. assert integrate(a/(x**2 + a**2), x) == atan(x/a)
  105. assert integrate(1/(x**2 + a**2), x) == atan(x/a)/a
  106. assert integrate(1/(1 + a**2*x**2), x) == atan(a*x)/a
  107. def test_issue_6308():
  108. k, a0 = symbols('k a0', real=True)
  109. assert integrate((x**2 + 1 - k**2)/(x**2 + 1 + a0**2), x) == \
  110. x - (a0**2 + k**2)*atan(x/sqrt(a0**2 + 1))/sqrt(a0**2 + 1)
  111. def test_issue_5907():
  112. a = symbols('a', nonzero=True)
  113. assert integrate(1/(x**2 + a**2)**2, x) == \
  114. x/(2*a**4 + 2*a**2*x**2) + atan(x/a)/(2*a**3)
  115. def test_log_to_atan():
  116. f, g = (Poly(x + S.Half, x, domain='QQ'), Poly(sqrt(3)/2, x, domain='EX'))
  117. fg_ans = 2*atan(2*sqrt(3)*x/3 + sqrt(3)/3)
  118. assert log_to_atan(f, g) == fg_ans
  119. assert log_to_atan(g, f) == -fg_ans