*(rhs)
            click to toggle source
          
          
          
  
            
            
            
            
            
               
               
def *(rhs)
  if rhs.is_a? Complex
    Complex(real * rhs.real - imaginary * rhs.imaginary, real * rhs.imaginary + rhs.real * imaginary)
  elsif rhs.is_a? Numeric
    Complex(real * rhs, imaginary * rhs)
  end
end
             
             
            
           
          
          
         
      
        
          
          
          
            +(rhs)
            click to toggle source
          
          
          
  
            
            
            
            
            
               
               
def +(rhs)
  if rhs.is_a? Complex
    Complex(real + rhs.real, imaginary + rhs.imaginary)
  elsif rhs.is_a? Numeric
    Complex(real + rhs, imaginary)
  end
end
             
             
            
           
          
          
         
      
        
          
          
          
            +@()
            click to toggle source
          
          
          
  
            
            
            
            
            
               
               
def +@
  Complex(real, imaginary)
end
             
             
            
           
          
          
         
      
        
          
          
          
            -(rhs)
            click to toggle source
          
          
          
  
            
            
            
            
            
               
               
def -(rhs)
  if rhs.is_a? Complex
    Complex(real - rhs.real, imaginary - rhs.imaginary)
  elsif rhs.is_a? Numeric
    Complex(real - rhs, imaginary)
  end
end
             
             
            
           
          
          
         
      
        
          
          
          
            -@()
            click to toggle source
          
          
          
  
            
            
            
            
            
               
               
def -@
  Complex(-real, -imaginary)
end
             
             
            
           
          
          
         
      
        
          
          
          
            /(rhs)
            click to toggle source
          
          
          
  
            
            
            
            
            
               
               
def /(rhs)
  if rhs.is_a? Complex
    __div__(rhs)
  elsif rhs.is_a? Numeric
    Complex(real / rhs, imaginary / rhs)
  end
end
             
             
            
           
          
          
          
          
         
      
        
          
          
          
            ==(rhs)
            click to toggle source
          
          
          
  
            
            
            
            
            
               
               
def ==(rhs)
  if rhs.is_a? Complex
    real == rhs.real && imaginary == rhs.imaginary
  elsif rhs.is_a? Numeric
    imaginary.zero? && real == rhs
  end
end
             
             
            
           
          
          
         
      
        
          
          
          
            abs()
            click to toggle source
          
          
          
  
            
            
            
            
            
               
               
def abs
  Math.hypot imaginary, real
end
             
             
            
           
          
          
          
          
         
      
        
          
          
          
            abs2()
            click to toggle source
          
          
          
  
            
            
            
            
            
               
               
def abs2
  real * real + imaginary * imaginary
end
             
             
            
           
          
          
         
      
        
          
          
          
            angle()
            click to toggle source
          
          
          
  
            
            
            
            
          
          
          
          
          
         
      
        
          
          
          
            arg()
            click to toggle source
          
          
          
  
            
            
            
            
            
               
               
def arg
  Math.atan2 imaginary, real
end
             
             
            
           
          
          
          
          
         
      
        
          
          
          
            conj()
            click to toggle source
          
          
          
  
            
            
            
            
          
          
          
          
          
         
      
        
          
          
          
            conjugate()
            click to toggle source
          
          
          
  
            
            
            
            
            
               
               
def conjugate
  Complex(real, -imaginary)
end
             
             
            
           
          
          
          
          
         
      
        
          
          
          
            fdiv(numeric)
            click to toggle source
          
          
          
  
            
            
            
            
            
               
               
def fdiv(numeric)
  Complex(real.to_f / numeric, imaginary.to_f / numeric)
end
             
             
            
           
          
          
         
      
        
          
          
          
            inspect()
            click to toggle source
          
          
          
  
            
            
            
            
            
               
               
def inspect
  "(#{to_s})"
end
             
             
            
           
          
          
         
      
        
          
          
          
            magnitude()
            click to toggle source
          
          
          
  
            
            
            
            
          
          
          
          
          
         
      
        
          
          
          
            phase()
            click to toggle source
          
          
          
  
            
            
            
            
          
          
          
          
          
         
      
        
          
          
          
            polar()
            click to toggle source
          
          
          
          
          
         
      
        
          
          
          
            quo(rhs)
            click to toggle source
          
          
          
  
            
            
            
            
          
          
          
          
          
         
      
        
          
          
          
            real?()
            click to toggle source
          
          
          
          
          
         
      
        
          
          
          
            rect()
            click to toggle source
          
          
          
  
            
            
            
            
          
          
          
          
          
         
      
        
          
          
          
            rectangular()
            click to toggle source
          
          
          
  
            
            
            
            
            
               
               
def rectangular
  [real, imaginary]
end
             
             
            
           
          
          
          
          
         
      
        
          
          
          
            to_r()
            click to toggle source
          
          
          
  
            
            
            
            
            
               
               
def to_r
  raise RangeError.new "can't convert #{to_s} into Rational" unless imaginary.zero?
  Rational(real, 1)
end
             
             
            
           
          
          
         
      
        
          
          
          
            to_s()
            click to toggle source
          
          
          
  
            
            
            
            
            
               
               
def to_s
  "#{real}#{'+' unless imaginary.negative?}#{imaginary}i"
end