Coverage for /usr/lib/python3/dist-packages/sympy/series/residues.py: 21%
28 statements
« prev ^ index » next coverage.py v7.9.1, created at 2025-06-14 15:55 +0200
« prev ^ index » next coverage.py v7.9.1, created at 2025-06-14 15:55 +0200
1"""
2This module implements the Residue function and related tools for working
3with residues.
4"""
6from sympy.core.mul import Mul
7from sympy.core.singleton import S
8from sympy.core.sympify import sympify
9from sympy.utilities.timeutils import timethis
12@timethis('residue')
13def residue(expr, x, x0):
14 """
15 Finds the residue of ``expr`` at the point x=x0.
17 The residue is defined as the coefficient of ``1/(x-x0)`` in the power series
18 expansion about ``x=x0``.
20 Examples
21 ========
23 >>> from sympy import Symbol, residue, sin
24 >>> x = Symbol("x")
25 >>> residue(1/x, x, 0)
26 1
27 >>> residue(1/x**2, x, 0)
28 0
29 >>> residue(2/sin(x), x, 0)
30 2
32 This function is essential for the Residue Theorem [1].
34 References
35 ==========
37 .. [1] https://en.wikipedia.org/wiki/Residue_theorem
38 """
39 # The current implementation uses series expansion to
40 # calculate it. A more general implementation is explained in
41 # the section 5.6 of the Bronstein's book {M. Bronstein:
42 # Symbolic Integration I, Springer Verlag (2005)}. For purely
43 # rational functions, the algorithm is much easier. See
44 # sections 2.4, 2.5, and 2.7 (this section actually gives an
45 # algorithm for computing any Laurent series coefficient for
46 # a rational function). The theory in section 2.4 will help to
47 # understand why the resultant works in the general algorithm.
48 # For the definition of a resultant, see section 1.4 (and any
49 # previous sections for more review).
51 from sympy.series.order import Order
52 from sympy.simplify.radsimp import collect
53 expr = sympify(expr)
54 if x0 != 0:
55 expr = expr.subs(x, x + x0)
56 for n in (0, 1, 2, 4, 8, 16, 32):
57 s = expr.nseries(x, n=n)
58 if not s.has(Order) or s.getn() >= 0:
59 break
60 s = collect(s.removeO(), x)
61 if s.is_Add:
62 args = s.args
63 else:
64 args = [s]
65 res = S.Zero
66 for arg in args:
67 c, m = arg.as_coeff_mul(x)
68 m = Mul(*m)
69 if not (m in (S.One, x) or (m.is_Pow and m.exp.is_Integer)):
70 raise NotImplementedError('term of unexpected form: %s' % m)
71 if m == 1/x:
72 res += c
73 return res