Class: Complex

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

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Numeric

#negative?, #nonzero?, #positive?, #zero?

Methods included from Comparable

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

Class Method Details

.polar(abs, arg = 0) ⇒ Object

[View source]

2
3
4
# File 'ext/enterprise_script_service/mruby/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

[View source]

38
39
40
41
42
43
44
# File 'ext/enterprise_script_service/mruby/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

[View source]

22
23
24
25
26
27
28
# File 'ext/enterprise_script_service/mruby/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

[View source]

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

def +@
  Complex(real, imaginary)
end

#-(rhs) ⇒ Object

[View source]

30
31
32
33
34
35
36
# File 'ext/enterprise_script_service/mruby/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

[View source]

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

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

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

[View source]

46
47
48
49
50
51
52
# File 'ext/enterprise_script_service/mruby/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

[View source]

55
56
57
58
59
60
61
# File 'ext/enterprise_script_service/mruby/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 == 0 && real == rhs
  end
end

#absObject Also known as: magnitude

[View source]

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

def abs
  Math.hypot imaginary, real
end

#abs2Object

[View source]

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

def abs2
  real * real + imaginary * imaginary
end

#argObject Also known as: angle, phase

[View source]

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

def arg
  Math.atan2 imaginary, real
end

#conjugateObject Also known as: conj

[View source]

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

def conjugate
  Complex(real, -imaginary)
end

#fdiv(numeric) ⇒ Object

[View source]

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

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

#inspectObject

[View source]

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

def inspect
  "(#{to_s})"
end

#polarObject

[View source]

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

def polar
  [abs, arg]
end

#real?Boolean

Returns:

  • (Boolean)
[View source]

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

def real?
  false
end

#rectangularObject Also known as: rect

[View source]

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

def rectangular
  [real, imaginary]
end

#to_rObject

[View source]

100
101
102
103
# File 'ext/enterprise_script_service/mruby/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

[View source]

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

def to_s
  "#{real}#{'+' unless imaginary < 0}#{imaginary}i"
end