test_partfrac.py 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. """Tests for algorithms for partial fraction decomposition of rational
  2. functions. """
  3. from sympy.polys.partfrac import (
  4. apart_undetermined_coeffs,
  5. apart,
  6. apart_list, assemble_partfrac_list
  7. )
  8. from sympy.core.expr import Expr
  9. from sympy.core.function import Lambda
  10. from sympy.core.numbers import (E, I, Rational, pi)
  11. from sympy.core.relational import Eq
  12. from sympy.core.singleton import S
  13. from sympy.core.symbol import (Dummy, Symbol)
  14. from sympy.functions.elementary.miscellaneous import sqrt
  15. from sympy.matrices.dense import Matrix
  16. from sympy.polys.polytools import (Poly, factor)
  17. from sympy.polys.rationaltools import together
  18. from sympy.polys.rootoftools import RootSum
  19. from sympy.testing.pytest import raises, XFAIL
  20. from sympy.abc import x, y, a, b, c
  21. def test_apart():
  22. assert apart(1) == 1
  23. assert apart(1, x) == 1
  24. f, g = (x**2 + 1)/(x + 1), 2/(x + 1) + x - 1
  25. assert apart(f, full=False) == g
  26. assert apart(f, full=True) == g
  27. f, g = 1/(x + 2)/(x + 1), 1/(1 + x) - 1/(2 + x)
  28. assert apart(f, full=False) == g
  29. assert apart(f, full=True) == g
  30. f, g = 1/(x + 1)/(x + 5), -1/(5 + x)/4 + 1/(1 + x)/4
  31. assert apart(f, full=False) == g
  32. assert apart(f, full=True) == g
  33. assert apart((E*x + 2)/(x - pi)*(x - 1), x) == \
  34. 2 - E + E*pi + E*x + (E*pi + 2)*(pi - 1)/(x - pi)
  35. assert apart(Eq((x**2 + 1)/(x + 1), x), x) == Eq(x - 1 + 2/(x + 1), x)
  36. assert apart(x/2, y) == x/2
  37. f, g = (x+y)/(2*x - y), Rational(3, 2)*y/(2*x - y) + S.Half
  38. assert apart(f, x, full=False) == g
  39. assert apart(f, x, full=True) == g
  40. f, g = (x+y)/(2*x - y), 3*x/(2*x - y) - 1
  41. assert apart(f, y, full=False) == g
  42. assert apart(f, y, full=True) == g
  43. raises(NotImplementedError, lambda: apart(1/(x + 1)/(y + 2)))
  44. def test_apart_matrix():
  45. M = Matrix(2, 2, lambda i, j: 1/(x + i + 1)/(x + j))
  46. assert apart(M) == Matrix([
  47. [1/x - 1/(x + 1), (x + 1)**(-2)],
  48. [1/(2*x) - (S.Half)/(x + 2), 1/(x + 1) - 1/(x + 2)],
  49. ])
  50. def test_apart_symbolic():
  51. f = a*x**4 + (2*b + 2*a*c)*x**3 + (4*b*c - a**2 + a*c**2)*x**2 + \
  52. (-2*a*b + 2*b*c**2)*x - b**2
  53. g = a**2*x**4 + (2*a*b + 2*c*a**2)*x**3 + (4*a*b*c + b**2 +
  54. a**2*c**2)*x**2 + (2*c*b**2 + 2*a*b*c**2)*x + b**2*c**2
  55. assert apart(f/g, x) == 1/a - 1/(x + c)**2 - b**2/(a*(a*x + b)**2)
  56. assert apart(1/((x + a)*(x + b)*(x + c)), x) == \
  57. 1/((a - c)*(b - c)*(c + x)) - 1/((a - b)*(b - c)*(b + x)) + \
  58. 1/((a - b)*(a - c)*(a + x))
  59. def _make_extension_example():
  60. # https://github.com/sympy/sympy/issues/18531
  61. from sympy.core import Mul
  62. def mul2(expr):
  63. # 2-arg mul hack...
  64. return Mul(2, expr, evaluate=False)
  65. f = ((x**2 + 1)**3/((x - 1)**2*(x + 1)**2*(-x**2 + 2*x + 1)*(x**2 + 2*x - 1)))
  66. g = (1/mul2(x - sqrt(2) + 1)
  67. - 1/mul2(x - sqrt(2) - 1)
  68. + 1/mul2(x + 1 + sqrt(2))
  69. - 1/mul2(x - 1 + sqrt(2))
  70. + 1/mul2((x + 1)**2)
  71. + 1/mul2((x - 1)**2))
  72. return f, g
  73. def test_apart_extension():
  74. f = 2/(x**2 + 1)
  75. g = I/(x + I) - I/(x - I)
  76. assert apart(f, extension=I) == g
  77. assert apart(f, gaussian=True) == g
  78. f = x/((x - 2)*(x + I))
  79. assert factor(together(apart(f)).expand()) == f
  80. f, g = _make_extension_example()
  81. # XXX: Only works with dotprodsimp. See test_apart_extension_xfail below
  82. from sympy.matrices import dotprodsimp
  83. with dotprodsimp(True):
  84. assert apart(f, x, extension={sqrt(2)}) == g
  85. def test_apart_extension_xfail():
  86. f, g = _make_extension_example()
  87. assert apart(f, x, extension={sqrt(2)}) == g
  88. def test_apart_full():
  89. f = 1/(x**2 + 1)
  90. assert apart(f, full=False) == f
  91. assert apart(f, full=True).dummy_eq(
  92. -RootSum(x**2 + 1, Lambda(a, a/(x - a)), auto=False)/2)
  93. f = 1/(x**3 + x + 1)
  94. assert apart(f, full=False) == f
  95. assert apart(f, full=True).dummy_eq(
  96. RootSum(x**3 + x + 1,
  97. Lambda(a, (a**2*Rational(6, 31) - a*Rational(9, 31) + Rational(4, 31))/(x - a)), auto=False))
  98. f = 1/(x**5 + 1)
  99. assert apart(f, full=False) == \
  100. (Rational(-1, 5))*((x**3 - 2*x**2 + 3*x - 4)/(x**4 - x**3 + x**2 -
  101. x + 1)) + (Rational(1, 5))/(x + 1)
  102. assert apart(f, full=True).dummy_eq(
  103. -RootSum(x**4 - x**3 + x**2 - x + 1,
  104. Lambda(a, a/(x - a)), auto=False)/5 + (Rational(1, 5))/(x + 1))
  105. def test_apart_undetermined_coeffs():
  106. p = Poly(2*x - 3)
  107. q = Poly(x**9 - x**8 - x**6 + x**5 - 2*x**2 + 3*x - 1)
  108. r = (-x**7 - x**6 - x**5 + 4)/(x**8 - x**5 - 2*x + 1) + 1/(x - 1)
  109. assert apart_undetermined_coeffs(p, q) == r
  110. p = Poly(1, x, domain='ZZ[a,b]')
  111. q = Poly((x + a)*(x + b), x, domain='ZZ[a,b]')
  112. r = 1/((a - b)*(b + x)) - 1/((a - b)*(a + x))
  113. assert apart_undetermined_coeffs(p, q) == r
  114. def test_apart_list():
  115. from sympy.utilities.iterables import numbered_symbols
  116. def dummy_eq(i, j):
  117. if type(i) in (list, tuple):
  118. return all(dummy_eq(i, j) for i, j in zip(i, j))
  119. return i == j or i.dummy_eq(j)
  120. w0, w1, w2 = Symbol("w0"), Symbol("w1"), Symbol("w2")
  121. _a = Dummy("a")
  122. f = (-2*x - 2*x**2) / (3*x**2 - 6*x)
  123. got = apart_list(f, x, dummies=numbered_symbols("w"))
  124. ans = (-1, Poly(Rational(2, 3), x, domain='QQ'),
  125. [(Poly(w0 - 2, w0, domain='ZZ'), Lambda(_a, 2), Lambda(_a, -_a + x), 1)])
  126. assert dummy_eq(got, ans)
  127. got = apart_list(2/(x**2-2), x, dummies=numbered_symbols("w"))
  128. ans = (1, Poly(0, x, domain='ZZ'), [(Poly(w0**2 - 2, w0, domain='ZZ'),
  129. Lambda(_a, _a/2),
  130. Lambda(_a, -_a + x), 1)])
  131. assert dummy_eq(got, ans)
  132. f = 36 / (x**5 - 2*x**4 - 2*x**3 + 4*x**2 + x - 2)
  133. got = apart_list(f, x, dummies=numbered_symbols("w"))
  134. ans = (1, Poly(0, x, domain='ZZ'),
  135. [(Poly(w0 - 2, w0, domain='ZZ'), Lambda(_a, 4), Lambda(_a, -_a + x), 1),
  136. (Poly(w1**2 - 1, w1, domain='ZZ'), Lambda(_a, -3*_a - 6), Lambda(_a, -_a + x), 2),
  137. (Poly(w2 + 1, w2, domain='ZZ'), Lambda(_a, -4), Lambda(_a, -_a + x), 1)])
  138. assert dummy_eq(got, ans)
  139. def test_assemble_partfrac_list():
  140. f = 36 / (x**5 - 2*x**4 - 2*x**3 + 4*x**2 + x - 2)
  141. pfd = apart_list(f)
  142. assert assemble_partfrac_list(pfd) == -4/(x + 1) - 3/(x + 1)**2 - 9/(x - 1)**2 + 4/(x - 2)
  143. a = Dummy("a")
  144. pfd = (1, Poly(0, x, domain='ZZ'), [([sqrt(2),-sqrt(2)], Lambda(a, a/2), Lambda(a, -a + x), 1)])
  145. assert assemble_partfrac_list(pfd) == -1/(sqrt(2)*(x + sqrt(2))) + 1/(sqrt(2)*(x - sqrt(2)))
  146. @XFAIL
  147. def test_noncommutative_pseudomultivariate():
  148. # apart doesn't go inside noncommutative expressions
  149. class foo(Expr):
  150. is_commutative=False
  151. e = x/(x + x*y)
  152. c = 1/(1 + y)
  153. assert apart(e + foo(e)) == c + foo(c)
  154. assert apart(e*foo(e)) == c*foo(c)
  155. def test_noncommutative():
  156. class foo(Expr):
  157. is_commutative=False
  158. e = x/(x + x*y)
  159. c = 1/(1 + y)
  160. assert apart(e + foo()) == c + foo()
  161. def test_issue_5798():
  162. assert apart(
  163. 2*x/(x**2 + 1) - (x - 1)/(2*(x**2 + 1)) + 1/(2*(x + 1)) - 2/x) == \
  164. (3*x + 1)/(x**2 + 1)/2 + 1/(x + 1)/2 - 2/x