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
#integer?, #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
#+@ ⇒ Object
14
15
16
|
# File 'mrbgems/mruby-complex/mrblib/complex.rb', line 14
def +@
self
end
|
#-@ ⇒ Object
18
19
20
|
# File 'mrbgems/mruby-complex/mrblib/complex.rb', line 18
def -@
Complex(-real, -imaginary)
end
|
#<=>(other) ⇒ Object
22
23
24
25
26
27
|
# File 'mrbgems/mruby-complex/mrblib/complex.rb', line 22
def <=>(other)
return nil unless other.kind_of?(Numeric)
self.to_f <=> other.to_f
rescue
nil
end
|
#abs ⇒ Object
Also known as:
magnitude
29
30
31
|
# File 'mrbgems/mruby-complex/mrblib/complex.rb', line 29
def abs
Math.hypot imaginary, real
end
|
#abs2 ⇒ Object
34
35
36
|
# File 'mrbgems/mruby-complex/mrblib/complex.rb', line 34
def abs2
real * real + imaginary * imaginary
end
|
#arg ⇒ Object
Also known as:
angle, phase
38
39
40
|
# File 'mrbgems/mruby-complex/mrblib/complex.rb', line 38
def arg
Math.atan2 imaginary, real
end
|
#conjugate ⇒ Object
Also known as:
conj
44
45
46
|
# File 'mrbgems/mruby-complex/mrblib/complex.rb', line 44
def conjugate
Complex(real, -imaginary)
end
|
#fdiv(numeric) ⇒ Object
49
50
51
|
# File 'mrbgems/mruby-complex/mrblib/complex.rb', line 49
def fdiv(numeric)
Complex(real / numeric, imaginary / numeric)
end
|
#inspect ⇒ Object
6
7
8
|
# File 'mrbgems/mruby-complex/mrblib/complex.rb', line 6
def inspect
"(#{to_s})"
end
|
#polar ⇒ Object
53
54
55
|
# File 'mrbgems/mruby-complex/mrblib/complex.rb', line 53
def polar
[abs, arg]
end
|
#real? ⇒ Boolean
57
58
59
|
# File 'mrbgems/mruby-complex/mrblib/complex.rb', line 57
def real?
false
end
|
#rectangular ⇒ Object
Also known as:
rect
61
62
63
|
# File 'mrbgems/mruby-complex/mrblib/complex.rb', line 61
def rectangular
[real, imaginary]
end
|
#to_c ⇒ Object
66
67
68
|
# File 'mrbgems/mruby-complex/mrblib/complex.rb', line 66
def to_c
self
end
|
#to_r ⇒ Object
70
71
72
73
|
# File 'mrbgems/mruby-complex/mrblib/complex.rb', line 70
def to_r
raise RangeError.new "can't convert #{to_s} into Rational" unless imaginary.zero?
Rational(real, 1)
end
|
#to_s ⇒ Object
10
11
12
|
# File 'mrbgems/mruby-complex/mrblib/complex.rb', line 10
def to_s
"#{real}#{'+' unless imaginary < 0}#{imaginary}#{'*' unless imaginary.finite?}i"
end
|