Class: Complex

Inherits:
Numeric show all
Defined in:
mrbgems/mruby-complex/mrblib/complex.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Numeric

#finite?, #infinite?, #negative?, #nonzero?, #positive?, #zero?

Methods included from Comparable

#<, #<=, #>, #>=, #between?, #clamp

Class Method Details

.polar(abs, arg = 0) ⇒ Object



2
3
4
# 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

Instance Method Details

#*(rhs) ⇒ Object



38
39
40
41
42
43
44
# 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) ⇒ Object



22
23
24
25
26
27
28
# 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

#+@Object



14
15
16
# File 'mrbgems/mruby-complex/mrblib/complex.rb', line 14

def +@
  Complex(real, imaginary)
end

#-(rhs) ⇒ Object



30
31
32
33
34
35
36
# 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

#-@Object



18
19
20
# File 'mrbgems/mruby-complex/mrblib/complex.rb', line 18

def -@
  Complex(-real, -imaginary)
end

#/(rhs) ⇒ Object Also known as: quo



46
47
48
49
50
51
52
# 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

#==(rhs) ⇒ Object



55
56
57
58
59
60
61
# 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

#absObject Also known as: magnitude



63
64
65
# File 'mrbgems/mruby-complex/mrblib/complex.rb', line 63

def abs
  Math.hypot imaginary, real
end

#abs2Object



68
69
70
# File 'mrbgems/mruby-complex/mrblib/complex.rb', line 68

def abs2
  real * real + imaginary * imaginary
end

#argObject Also known as: angle, phase



72
73
74
# File 'mrbgems/mruby-complex/mrblib/complex.rb', line 72

def arg
  Math.atan2 imaginary, real
end

#conjugateObject Also known as: conj



78
79
80
# File 'mrbgems/mruby-complex/mrblib/complex.rb', line 78

def conjugate
  Complex(real, -imaginary)
end

#fdiv(numeric) ⇒ Object



83
84
85
# File 'mrbgems/mruby-complex/mrblib/complex.rb', line 83

def fdiv(numeric)
  Complex(real.to_f / numeric, imaginary.to_f / numeric)
end

#inspectObject



6
7
8
# File 'mrbgems/mruby-complex/mrblib/complex.rb', line 6

def inspect
  "(#{to_s})"
end

#polarObject



87
88
89
# File 'mrbgems/mruby-complex/mrblib/complex.rb', line 87

def polar
  [abs, arg]
end

#real?Boolean

Returns:

  • (Boolean)


91
92
93
# File 'mrbgems/mruby-complex/mrblib/complex.rb', line 91

def real?
  false
end

#rectangularObject Also known as: rect



95
96
97
# File 'mrbgems/mruby-complex/mrblib/complex.rb', line 95

def rectangular
  [real, imaginary]
end

#to_rObject



100
101
102
103
# 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_sObject



10
11
12
# File 'mrbgems/mruby-complex/mrblib/complex.rb', line 10

def to_s
  "#{real}#{'+' unless imaginary.negative?}#{imaginary}i"
end