In Files

  • mrbgems/mruby-complex/mrblib/complex.rb

Complex

Public Class Methods

polar(abs, arg = 0) click to toggle source
 
               # File mrbgems/mruby-complex/mrblib/complex.rb, line 2
def self.polar(abs, arg = 0)
  Complex(abs * Math.cos(arg), abs * Math.sin(arg))
end
            

Public Instance Methods

*(rhs) click to toggle source
 
               # File mrbgems/mruby-complex/mrblib/complex.rb, line 38
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
 
               # File mrbgems/mruby-complex/mrblib/complex.rb, line 22
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
 
               # File mrbgems/mruby-complex/mrblib/complex.rb, line 14
def +@
  Complex(real, imaginary)
end
            
-(rhs) click to toggle source
 
               # File mrbgems/mruby-complex/mrblib/complex.rb, line 30
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
 
               # File mrbgems/mruby-complex/mrblib/complex.rb, line 18
def -@
  Complex(-real, -imaginary)
end
            
/(rhs) click to toggle source
 
               # File mrbgems/mruby-complex/mrblib/complex.rb, line 46
def /(rhs)
  if rhs.is_a? Complex
    __div__(rhs)
  elsif rhs.is_a? Numeric
    Complex(real / rhs, imaginary / rhs)
  end
end
            
Also aliased as: quo
==(rhs) click to toggle source
 
               # File mrbgems/mruby-complex/mrblib/complex.rb, line 55
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
 
               # File mrbgems/mruby-complex/mrblib/complex.rb, line 63
def abs
  Math.hypot imaginary, real
end
            
Also aliased as: magnitude
abs2() click to toggle source
 
               # File mrbgems/mruby-complex/mrblib/complex.rb, line 68
def abs2
  real * real + imaginary * imaginary
end
            
angle() click to toggle source
Alias for: arg
arg() click to toggle source
 
               # File mrbgems/mruby-complex/mrblib/complex.rb, line 72
def arg
  Math.atan2 imaginary, real
end
            
Also aliased as: angle, phase
conj() click to toggle source
Alias for: conjugate
conjugate() click to toggle source
 
               # File mrbgems/mruby-complex/mrblib/complex.rb, line 78
def conjugate
  Complex(real, -imaginary)
end
            
Also aliased as: conj
fdiv(numeric) click to toggle source
 
               # File mrbgems/mruby-complex/mrblib/complex.rb, line 83
def fdiv(numeric)
  Complex(real.to_f / numeric, imaginary.to_f / numeric)
end
            
inspect() click to toggle source
 
               # File mrbgems/mruby-complex/mrblib/complex.rb, line 6
def inspect
  "(#{to_s})"
end
            
magnitude() click to toggle source
Alias for: abs
phase() click to toggle source
Alias for: arg
polar() click to toggle source
 
               # File mrbgems/mruby-complex/mrblib/complex.rb, line 87
def polar
  [abs, arg]
end
            
quo(rhs) click to toggle source
Alias for: /
real?() click to toggle source
 
               # File mrbgems/mruby-complex/mrblib/complex.rb, line 91
def real?
  false
end
            
rect() click to toggle source
Alias for: rectangular
rectangular() click to toggle source
 
               # File mrbgems/mruby-complex/mrblib/complex.rb, line 95
def rectangular
  [real, imaginary]
end
            
Also aliased as: rect
to_r() click to toggle source
 
               # File mrbgems/mruby-complex/mrblib/complex.rb, line 100
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
 
               # File mrbgems/mruby-complex/mrblib/complex.rb, line 10
def to_s
  "#{real}#{'+' unless imaginary.negative?}#{imaginary}i"
end