source/reference: 
 https://www.youtube.com/channel/UCaTLkDn9_1Wy5TRYfVULYUw/playlists
Residue Theorem
Motivation
    Recall: 𝑓 has an isolated singularity at 𝑧0 if 𝑓 is analytic in {0<|𝑧−𝑧0|<𝑟} for some 𝑟>0. In that case, 𝑓 has a Laurent series expansion
        𝑓(𝑧)=∞∑𝑘=−∞
𝑎𝑘(𝑧−𝑧0)𝑘, 0<|𝑧−𝑧0|<𝑟.
        Observe: If 0<𝜌<𝑟 then
         ∫|𝑧−𝑧0|=𝜌
𝑓(𝑧)𝑑𝑧=∞∑𝑘=−∞
𝑎𝑘 ∫|𝑧−𝑧0|=𝜌
(𝑧−𝑧0)𝑘𝑑𝑧
    
    What is  ∫|𝑧−𝑧0|=𝜌
(𝑧−𝑧0)𝑘𝑑𝑧?
        For 𝑘≠−1, the function ℎ(𝑧)=(𝑧−𝑧0)𝑘 has a primitive, namely 𝐻(𝑧)=1𝑘+1
(𝑧−𝑧0)𝑘+1. Therefore,  ∫|𝑧−𝑧0|=𝜌
(𝑧−𝑧0)𝑘𝑑𝑧=0 for 𝑘≠−1
        For 𝑘=−1, the integral is  ∫|𝑧−𝑧0|=𝜌
(𝑧−𝑧0)𝑘𝑑𝑧. We can use the Cauchy Integral Formula (or compute this directly) and find 
             ∫𝛾
𝑓(𝑧)𝑑𝑧=𝑏∫𝑎
𝑓(𝛾(𝑡))𝛾′(𝑡)𝑑𝑡
                 ∫|𝑧−𝑧0|=𝜌
1𝑧−𝑧0
𝑑𝑧=2𝜋∫0
1𝑧0+𝜌ℯ𝑖𝑡−𝑧0
⋅𝜌ℯ𝑖𝑡𝑑𝑡
                 =2𝜋∫0
𝑖𝑑𝑡=2𝜋𝑖
            
             ∫|𝑧−𝑧0|=𝜌
(𝑧−𝑧0)𝑘𝑑𝑧=2𝜋𝑖 for 𝑘=−1
        Hence
         ∫|𝑧−𝑧0|=𝜌
𝑓(𝑧)𝑑𝑧=∞∑𝑘=−∞
𝑎𝑘 ∫|𝑧−𝑧0|=𝜌
(𝑧−𝑧0)𝑘𝑑𝑧=2𝜋𝑖𝑎−1.
        Therefore, 𝑎−1 gets special attention!
    
The Residue
    DefinitionIf 𝑓 has an isolated singularity at 𝑧0 with Laurent series expansion
        𝑓(𝑧)=∞∑𝑘=−∞
𝑎𝑘(𝑧−𝑧0)𝑘, 0<|𝑧−𝑧0|<𝑟,
        then the residue of 𝑓 at 𝑧0 is Res(𝑓,𝑧0)=𝑎−1.
                    
    Examples of finding a residue: to find Laurent series centered at the singular point and read off the term 𝑎−1.
        𝑓(𝑧)=1(𝑧−1)(𝑧−2)
=−1𝑧−1
+∞∑𝑘=0
(−1)(𝑧−1)𝑘 in 0<|𝑧−1|<1. Therefore, Res(𝑓,1)=−1
        𝑓(𝑧)=1(𝑧−1)(𝑧−2)
=1𝑧−2
−∞∑𝑛=0
(−1)𝑛(𝑧−2)𝑛 in 0<|𝑧−2|<1. Therefore, Res(𝑓,2)=1
    
    More Residue Examples
    More examples:
        𝑓(𝑧)=sin 𝑧𝑧4
=1𝑧3
−13!
1𝑧
+15!
𝑧−17!
𝑧3+−⋯ in 0<|𝑧|<∞. Therefore, Res(𝑓,0)=−13!
=−16
.
        𝑓(𝑧)=cos1𝑧
=1−12!
1𝑧2
+14!
1𝑧4
−16!
1𝑧6
+−⋯. Therefore Res(𝑓,0)=0
        𝑓(𝑧)=sin1𝑧
=1𝑧
−13!
1𝑧3
+15!
1𝑧5
−+⋯. Therefore Res(𝑓,0)=1
        𝑓(𝑧)=cos 𝑧−1𝑧2
=−12!
+𝑧24!
−+⋯. Therefore Res(𝑓,0)=0
        𝑓(𝑧)=1(𝑧2+1
=1(𝑧−𝑖)(𝑧+𝑖)
=12!
(𝑧+𝑖)−(𝑧−𝑖)(𝑧−𝑖)(𝑧+𝑖)
             =12!
1(𝑧−𝑖)
−1(𝑧+𝑖)
=12𝑖
⋅1(𝑧−𝑖)
+(analytic function near 𝑖).
             Therefore, Res(𝑓,𝑖)=12𝑖
=−12
𝑖.
        
        Similarly, 𝑓(𝑧)=1(𝑧2+1
=−12𝑖
⋅1(𝑧+𝑖)
+(analytic function near −𝑖).
        Therefore, Res(𝑓,𝑖)=−12𝑖
=12
𝑖.
    
    The Residue Theorem
    Theorem (Residue Theorem)Let 𝐷 be a simply connected domain, and let 𝑓 be analytic in 𝐷, except for isolated singularities. Let 𝐶 be a simple closed curve in 𝐷 (oriented counterclockwise), and let 𝑧1,⋯,𝑧𝑛 be those isolated singularities of 𝑓 that lie inside of 𝐶. Then 
         ∫𝐶
𝑓(𝑧)𝑑𝑧=2𝜋𝑖𝑛∑𝑘=1
Res(𝑓,𝑧𝑘).
Example
    𝑓(𝑧)=1(𝑧2+1
 is analytic in 𝐷=𝐶, except for isolated singularities at 𝑧=±𝑖.
         ∫𝐶1
𝑓(𝑧)𝑑𝑧=2𝜋𝑖Res(𝑓,𝑖)=2𝜋𝑖(−12
𝑖)=𝜋
         ∫𝐶2
𝑓(𝑧)𝑑𝑧=2𝜋𝑖Res(𝑓,−𝑖)=2𝜋𝑖(12
𝑖)=−𝜋
         ∫𝐶3
𝑓(𝑧)𝑑𝑧=2𝜋𝑖(Res(𝑓,𝑖)+Res(𝑓,−𝑖))=2𝜋𝑖(−12
𝑖+12
𝑖)=−𝜋
         ∫𝐶4
𝑓(𝑧)𝑑𝑧=0
        In order to be able to fully take advantage of this powerful theorem, strategies and techniques that can help calculating residues are needed  
    Recall the Residue Theorem
    Recall:
        𝑓 has an isolated singularity at 𝑧0 if 𝑓 is analytic in the punctured disk {0<|𝑧−𝑧0|<𝑟}.
        In that case, 𝑓 has a Laurent series representation
            𝑓(𝑧)=∞∑𝑘=−∞
𝑎𝑘(𝑧−𝑧0)𝑘
            in this punctured disk. The representation is unique.
        
        The residue of 𝑓 at 𝑧0 is Res(𝑓,𝑧0)=𝑎−1, the coefficient of term 1𝑧−𝑧0
.
    
    Residues at Removable Singularities
    𝑓(𝑧)=∞∑𝑘=−∞
𝑎𝑘(𝑧−𝑧0)𝑘, 0<|𝑧−𝑧0|<𝑟.
    Recall: 𝑧0 is a removable singularity if 𝑎𝑘=0 for all 𝑘<0. In particular: 𝑎−1=0 in that case, so that Res(𝑓,𝑧0)=0. Example:
        𝑓(𝑧)=sin 𝑧𝑧
=1𝑧
∞∑𝑛=0
(−1)𝑛(2𝑛+1)!
𝑧2𝑛+1
             =1𝑧
𝑧−13!
𝑧3+15!
𝑧5−17!
𝑧7+−⋯
             =1−13!
𝑧2+15!
𝑧4−17!
𝑧6+−⋯
        
        Thus Res(𝑓,0)=0 
    
    Residues at Simple Poles
    Example:
    Res(𝑓,𝑧0)=
    Lim𝑧→𝑧0(𝑧−𝑧0)𝑓(𝑧).
    Example: 𝑓(𝑧)=1𝑧2+1
 has a simple pole at 𝑧0=𝑖 (and another one at −𝑖).
        Res1𝑧2+1
,𝑖=
    Lim𝑧→𝑖(𝑧−𝑖)1𝑧2+1
             =
    Lim𝑧→𝑖(𝑧−𝑖)1(𝑧−𝑖)(𝑧+𝑖)
             =
    Lim𝑧→𝑖(𝑧−𝑖)1(𝑧+𝑖)
=12𝑖
=−𝑖2
        
    
    Residues at Double Poles
    𝑓(𝑧)=∞∑𝑘=−∞
𝑎𝑘(𝑧−𝑧0)𝑘, 0<|𝑧−𝑧0|<𝑟.
    Recall: 𝑧0 is a double pole if 𝑎−2≠0 and 𝑎𝑘=0 for all 𝑘≤−3. So
        𝑓(𝑧)=𝑎−2(𝑧−𝑧0)2
+𝑎−𝑧−𝑧0
+𝑎0+𝑎1(𝑧−𝑧0)+𝑎2(𝑧−𝑧0)2+⋯
        How do we isolate 𝑎−? Idea:
        (𝑧−𝑧0)2𝑓(𝑧)=𝑎−2+𝑎−(𝑧−𝑧0)+𝑎0(𝑧−𝑧0)2+⋯,
        so that
        𝑑𝑑𝑧
(𝑧−𝑧0)2𝑓(𝑧)
=𝑎−+2𝑎0(𝑧−𝑧0)+⋯,
        Hence
        Res(𝑓,𝑧0)=𝑎−=
    Lim𝑧→𝑧0𝑑𝑑𝑧
(𝑧−𝑧0)2𝑓(𝑧)
    
    Example
    𝑓(𝑧)=1(𝑧−1)2(𝑧−3)
 has a double pole at 𝑧0=1 (and a simple one at 3).
        Res1(𝑧−1)2(𝑧−3)
,1=
    Lim𝑧→1
𝑑𝑑𝑧
(𝑧−1)21(𝑧−1)2(𝑧−3)
             =
    Lim𝑧→1
𝑑𝑑𝑧
1(𝑧−3)
             =
    Lim𝑧→1
−1(𝑧−3)2
=−14
.
        
    
    Residues at Poles of Order 𝑛
      𝑓(𝑧)=∞∑𝑘=−∞
𝑎𝑘(𝑧−𝑧0)𝑘, 0<|𝑧−𝑧0|<𝑟.
  Recall: 𝑧0 is a pole of order 𝑛 if 𝑎−𝑛≠0 and 𝑎𝑘=0 for all 𝑘≤−(𝑛+1).
      𝑓(𝑧)=𝑎−𝑛(𝑧−𝑧0)𝑛
+⋯+𝑎−𝑧−𝑧0
+𝑎0+𝑎1(𝑧−𝑧0)+𝑎2(𝑧−𝑧0)2+⋯
      Then
      Res(𝑓,𝑧0)=𝑎−=1(𝑛−<)!
Lim𝑧→𝑧0𝑑𝑛−𝑑𝑧𝑛−
(𝑧−𝑧0)𝑛𝑓(𝑧)
  
    More On Residues
    RemarkIf 𝑓(𝑧)=𝑔(𝑧)ℎ(𝑧)
, where 𝑔 and ℎ are analytic near 𝑧0, and ℎ has a simple zero at 𝑧0, then
        Res(𝑓(𝑧),𝑧0)=𝑔(𝑧0)ℎ′(𝑧0)
.
    Example: 𝑓(𝑧)=1(𝑧−1)2(𝑧−3)
, choose 𝑔(𝑧)=1(𝑧−1)2
 and ℎ(𝑧)=(𝑧−3). Then 𝑔 and ℎ are analytic near 𝑧0=3, and ℎ has a simple zero at 𝑧0=3. Thus
        Res(𝑓,𝑧0)=𝑔(3)ℎ′(3)
=1(3−1)2
1 
 =14
    
    Evaluating Integrals via the Residue Theorem
    Recall: The Residue Theorem
     ∫𝐶
𝑓(𝑧)𝑑𝑧=2𝜋𝑖𝑛∑𝑘=1
Res(𝑓,𝑧𝑘)
    Examples:
         ∫|𝑧|=1
ℯ3𝑧
𝑑𝑧=2𝜋𝑖Res(𝑓,0), where 𝑓(𝑧)=ℯ3𝑧
=∞∑𝑘=1
1𝑘!
3𝑧
𝑘. Thus Res(𝑓,0)=3, so that
             ∫|𝑧|=1
ℯ3𝑧
𝑑𝑧=6𝜋𝑖
        
         ∫|𝑧|=2
tan 𝑧𝑑𝑧=2𝜋𝑖Res(𝑓,𝜋2
)+Res(𝑓,−𝜋2
), where 𝑓(𝑧)=tan 𝑧=sin 𝑧cos 𝑧
.
            To find Res(𝑓,𝜋2
): Note that 𝑓(𝑧)=𝑔(𝑧)ℎ(𝑧)
, where 𝑔(𝑧)=sin(𝑧) and ℎ(𝑧)=cos(𝑧) are analytic near 𝜋2
 and ℎ(𝜋2
)=0. Thus Res(𝑓,𝜋2
)=𝑔(𝜋2
)ℎ′(𝜋2
)=sin(𝜋2
)−sin(𝜋2
)=−1
                Similarly, Res(𝑓,−𝜋2
)=𝑔(−𝜋2
)ℎ′(−𝜋2
)=sin(−𝜋2
)−sin(−𝜋2
)=−1−(−1)
=−1
            
            Thus  ∫|𝑧|=2
tan 𝑧𝑑𝑧=2𝜋𝑖(−1−1)=−4𝜋𝑖.
        
         ∫𝐶1
1(𝑧−1)2(𝑧−3)
𝑑𝑧=2𝜋𝑖Res(𝑓,1).
            Res(𝑓,1)=
    Lim𝑧→1
𝑑𝑑𝑧
(𝑧−1)2(𝑧−1)2(𝑧−3)
                
                 =
    Lim𝑧→1
𝑑𝑑𝑧
1𝑧−3
=
    Lim𝑧→1
−1(𝑧−3)2
=−14
            
            Thus  ∫𝐶1
1(𝑧−1)2(𝑧−3)
𝑑𝑧=−14
2𝜋𝑖=−𝜋𝑖2
.
         ∫𝐶2
1(𝑧−1)2(𝑧−3)
𝑑𝑧=2𝜋𝑖(Res(𝑓,1)+Res(𝑓,3))
            Res(𝑓,3)=
    Lim𝑧→3
(𝑧−3)(𝑧−1)2(𝑧−3)
=
    Lim𝑧→3
1(𝑧−1)2
=14
        
    
    More Examples
    The Residue Theorem can also be used to evaluate real integrals, for example of the following forms:
        2𝜋∫0
𝑅(cos 𝑡,sin 𝑡)𝑑𝑡, where 𝑅(𝑥,𝑦) is a rational function of the real variables 𝑥 and 𝑦.
        ∞∫−∞
𝑓(𝑥)𝑑𝑥, where 𝑓 is a rational function of 𝑥.
        ∞∫−∞
𝑓(𝑥)cos(𝛼𝑥)𝑑𝑥, where 𝑓 is a rational function of 𝑥.
        ∞∫−∞
𝑓(𝑥)sin(𝛼𝑥)𝑑𝑥, where 𝑓 is a rational function of 𝑥.
    
    Evaluating an Improper Integral via the Residue Theorem
    GoalEvaluate ∞∫0
cos 𝑥1+𝑥2
𝑑𝑥.
    An Improper Integral
    Note: ∞∫0
⋯𝑑𝑥 means Lim𝑅→∞
𝑅∫0
⋯𝑑𝑥, so we need to consider 𝑅∫0
cos 𝑥1+𝑥2
𝑑𝑥 and then let 𝑅→∞. Idea:
        
            𝑅∫0
cos 𝑥1+𝑥2
𝑑𝑥=12
𝑅∫−𝑅
cos 𝑥1+𝑥2
𝑑𝑥
             =12
𝑅∫−𝑅
cos 𝑥+𝑖sin 𝑥1+𝑥2
𝑑𝑥
             =12
𝑅∫−𝑅
ℯ𝑖𝑥1+𝑥2
𝑑𝑥.
        
        Idea:
        12
𝑅∫−𝑅
ℯ𝑖𝑥1+𝑥2
𝑑𝑥=
            12
 ∫[−𝑅,𝑅]
ℯ𝑖𝑧1+𝑧2
𝑑𝑧
              
             =12
 ∫𝐶𝑅
ℯ𝑖𝑧1+𝑧2
𝑑𝑧−12
 ∫Γ𝑅
ℯ𝑖𝑧1+𝑧2
𝑑𝑧
             =12
2𝜋𝑖Resℯ𝑖𝑧1+𝑧2
,𝑖−12
 ∫Γ𝑅
ℯ𝑖𝑧1+𝑧2
𝑑𝑧
        
        we thus need to find the residue of 𝑓(𝑧)=ℯ𝑖𝑧1+𝑧2
 at 𝑧0=𝑖 and estimate the integral over Γ𝑅.
        Finding the residue of 𝑓(𝑧)=ℯ𝑖𝑧1+𝑧2
 at 𝑧0=𝑖 
            
                𝑓 has a simple pole at 𝑧=𝑖.
                Thus Res(𝑓,𝑖)=
    Lim𝑧→𝑖
(𝑧−𝑖)𝑓(𝑧)=
    Lim𝑧→𝑖
(𝑧−𝑖)ℯ𝑖𝑧1+𝑧2
=
    Lim𝑧→𝑖
ℯ𝑖𝑧𝑧+𝑖
=ℯ-12𝑖
.
                Hence 12
 ∫𝐶𝑅
ℯ𝑖𝑧1+𝑧2
𝑑𝑧=12
2𝜋𝑖12𝑖ℯ
=𝜋2ℯ
.
            
        
Estimating 12
 ∫Γ𝑅
ℯ𝑖𝑧1+𝑧2
𝑑𝑧
  
      We are only interested in what happens as 𝑅→∞
      Want to show 12
 ∫Γ𝑅
ℯ𝑖𝑧1+𝑧2
𝑑𝑧→0 as 𝑅→∞
      Therefore, it suffices to show that  ∫Γ𝑅
ℯ𝑖𝑧1+𝑧2
𝑑𝑧≤const(𝑅), where the constant, const(𝑅) goes to zero as 𝑅→∞
          
              Recall:  ∫Γ𝑅
𝑓(𝑧)𝑑𝑧≤length(Γ𝑅)⋅
    max𝑧∈Γ𝑅
|𝑓(𝑧)|.
              
              Thus:  ∫Γ𝑅
ℯ𝑖𝑧1+𝑧2
𝑑𝑧≤length(Γ𝑅)⋅
    max𝑧∈Γ𝑅
ℯ𝑖𝑧1+𝑧2
.
              
              ℯ𝑖𝑧1+𝑧2
=ℯRe(𝑖𝑧)|1+𝑧2|
=ℯ−𝑦|1+𝑧2|
≤ℯ−𝑦𝑅2−1
≤1𝑅2−1
 for 𝑧∈Γ𝑅, since |𝑧|=𝑅 and 𝑦≥0 on Γ𝑅.
              
              So  ∫Γ𝑅
ℯ𝑖𝑧1+𝑧2
𝑑𝑧≤𝜋𝑅1𝑅2−1
→0 as 𝑅→∞
              Thus  ∫Γ𝑅
ℯ𝑖𝑧1+𝑧2
𝑑𝑧→0 as 𝑅→∞
          
      
    
        To find: ∞∫0
cos 𝑥1+𝑥2
𝑑𝑥.
            
                ∞∫0
cos 𝑥1+𝑥2
𝑑𝑥=Lim𝑅→∞
𝑅∫0
cos 𝑥1+𝑥2
𝑑𝑥
                𝑅∫0
cos 𝑥1+𝑥2
𝑑𝑥=12
 ∫𝐶𝑅
ℯ𝑖𝑧1+𝑧2
𝑑𝑧−12
 ∫Γ𝑅
ℯ𝑖𝑧1+𝑧2
𝑑𝑧
                
                12
 ∫𝐶𝑅
ℯ𝑖𝑧1+𝑧2
𝑑𝑧=12
⋅2𝜋𝑖Resℯ𝑖𝑧1+𝑧2
,𝑖=𝜋2ℯ
                
                 ∫Γ𝑅
ℯ𝑖𝑧1+𝑧2
𝑑𝑧→0 as 𝑅→∞
                
                Hence ∞∫0
cos 𝑥1+𝑥2
𝑑𝑥=𝜋2ℯ
.