Class: Complex
- Defined in:
- mrbgems/mruby-complex/mrblib/complex.rb
Class Method Summary collapse
-
.polar(abs, arg = 0) ⇒ Object
call-seq: Complex.polar(abs [, arg]) -> complex.
Instance Method Summary collapse
-
#+@ ⇒ Object
call-seq: +cmp -> cmp.
-
#-@ ⇒ Object
call-seq: -cmp -> complex.
-
#<=>(other) ⇒ Object
call-seq: cmp <=> numeric -> -1, 0, +1, or nil.
-
#abs ⇒ Object
(also: #magnitude)
call-seq: cmp.abs -> real cmp.magnitude -> real.
-
#abs2 ⇒ Object
call-seq: cmp.abs2 -> real.
-
#arg ⇒ Object
(also: #angle, #phase)
call-seq: cmp.arg -> float cmp.angle -> float cmp.phase -> float.
-
#conjugate ⇒ Object
(also: #conj)
call-seq: cmp.conjugate -> complex cmp.conj -> complex.
-
#fdiv(numeric) ⇒ Object
call-seq: cmp.fdiv(numeric) -> complex.
-
#inspect ⇒ Object
call-seq: cmp.inspect -> string.
-
#polar ⇒ Object
call-seq: cmp.polar -> array.
-
#real? ⇒ Boolean
call-seq: cmp.real? -> false.
-
#rectangular ⇒ Object
(also: #rect)
call-seq: cmp.rectangular -> array cmp.rect -> array.
-
#to_c ⇒ Object
call-seq: cmp.to_c -> cmp.
-
#to_r ⇒ Object
call-seq: cmp.to_r -> rational.
-
#to_s ⇒ Object
call-seq: cmp.to_s -> string.
Methods inherited from Numeric
#integer?, #negative?, #nonzero?, #positive?, #zero?
Methods included from Comparable
#<, #<=, #==, #>, #>=, #between?, #clamp
Class Method Details
.polar(abs, arg = 0) ⇒ Object
call-seq:
Complex.polar(abs [, arg]) -> complex
Returns a complex number in terms of its polar coordinates. abs is the absolute value (magnitude) and arg is the argument (angle).
Complex.polar(3, 0) #=> (3+0i)
Complex.polar(3, Math::PI/2) #=> (1.836909530733566e-16+3.0i)
Complex.polar(3, Math::PI) #=> (-3.0+3.673819061467132e-16i)
13 14 15 |
# File 'mrbgems/mruby-complex/mrblib/complex.rb', line 13 def self.polar(abs, arg = 0) Complex(abs * Math.cos(arg), abs * Math.sin(arg)) end |
Instance Method Details
#+@ ⇒ Object
call-seq:
+cmp -> cmp
Returns self.
+Complex(1, 2) #=> (1+2i)
53 54 55 |
# File 'mrbgems/mruby-complex/mrblib/complex.rb', line 53 def +@ self end |
#-@ ⇒ Object
call-seq:
-cmp -> complex
Returns the negation of self.
-Complex(1, 2) #=> (-1-2i)
-Complex(-1, 2) #=> (1-2i)
66 67 68 |
# File 'mrbgems/mruby-complex/mrblib/complex.rb', line 66 def -@ Complex(-real, -imaginary) end |
#<=>(other) ⇒ Object
call-seq:
cmp <=> numeric -> -1, 0, +1, or nil
Returns -1, 0, or +1 depending on whether cmp is less than, equal to, or greater than numeric. This is the basis for the tests in the Comparable module. Returns nil if the two values are incomparable.
Complex(2, 3) <=> Complex(2, 3) #=> 0
Complex(5) <=> 5 #=> 0
Complex(2, 3) <=> 1 #=> 1
82 83 84 85 86 87 |
# File 'mrbgems/mruby-complex/mrblib/complex.rb', line 82 def <=>(other) return nil unless other.kind_of?(Numeric) self.to_f <=> other.to_f rescue nil end |
#abs ⇒ Object Also known as: magnitude
call-seq:
cmp.abs -> real
cmp.magnitude -> real
Returns the absolute part of its polar form.
Complex(-1).abs #=> 1.0
Complex(3.0, -4.0).abs #=> 5.0
99 100 101 |
# File 'mrbgems/mruby-complex/mrblib/complex.rb', line 99 def abs Math.hypot(imaginary, real) end |
#abs2 ⇒ Object
call-seq:
cmp.abs2 -> real
Returns square of the absolute value.
Complex(-1).abs2 #=> 1
Complex(3.0, -4.0).abs2 #=> 25.0
113 114 115 |
# File 'mrbgems/mruby-complex/mrblib/complex.rb', line 113 def abs2 real * real + imaginary * imaginary end |
#arg ⇒ Object Also known as: angle, phase
call-seq:
cmp.arg -> float
cmp.angle -> float
cmp.phase -> float
Returns the angle part of its polar form.
Complex.polar(3, Math::PI/2).arg #=> 1.5707963267948966
127 128 129 |
# File 'mrbgems/mruby-complex/mrblib/complex.rb', line 127 def arg Math.atan2(imaginary, real) end |
#conjugate ⇒ Object Also known as: conj
call-seq:
cmp.conjugate -> complex
cmp.conj -> complex
Returns the complex conjugate.
Complex(1, 2).conjugate #=> (1-2i)
142 143 144 |
# File 'mrbgems/mruby-complex/mrblib/complex.rb', line 142 def conjugate Complex(real, -imaginary) end |
#fdiv(numeric) ⇒ Object
call-seq:
cmp.fdiv(numeric) -> complex
Performs division as each part is a float, even if the parts are not floats.
Complex(11, 22).fdiv(3) #=> (3.6666666666666665+7.333333333333333i)
155 156 157 |
# File 'mrbgems/mruby-complex/mrblib/complex.rb', line 155 def fdiv(numeric) Complex(real / numeric, imaginary / numeric) end |
#inspect ⇒ Object
call-seq:
cmp.inspect -> string
Returns the value as a string for inspection.
Complex(2).inspect #=> "(2+0i)"
Complex(-8, 6).inspect #=> "(-8+6i)"
Complex(1, 2).inspect #=> "(1+2i)"
27 28 29 |
# File 'mrbgems/mruby-complex/mrblib/complex.rb', line 27 def inspect "(#{to_s})" end |
#polar ⇒ Object
call-seq:
cmp.polar -> array
Returns an array; [cmp.abs, cmp.arg].
Complex(1, 2).polar #=> [2.23606797749979, 1.1071487177940904]
167 168 169 |
# File 'mrbgems/mruby-complex/mrblib/complex.rb', line 167 def polar [abs, arg] end |
#real? ⇒ Boolean
call-seq:
cmp.real? -> false
Returns false.
Complex(1).real? #=> false
179 180 181 |
# File 'mrbgems/mruby-complex/mrblib/complex.rb', line 179 def real? false end |
#rectangular ⇒ Object Also known as: rect
call-seq:
cmp.rectangular -> array
cmp.rect -> array
Returns an array; [cmp.real, cmp.imag].
Complex(1, 2).rectangular #=> [1, 2]
192 193 194 |
# File 'mrbgems/mruby-complex/mrblib/complex.rb', line 192 def rectangular [real, imaginary] end |
#to_c ⇒ Object
call-seq:
cmp.to_c -> cmp
Returns self.
Complex(2).to_c #=> (2+0i)
Complex(-8, 6).to_c #=> (-8+6i)
206 207 208 |
# File 'mrbgems/mruby-complex/mrblib/complex.rb', line 206 def to_c self end |
#to_r ⇒ Object
call-seq:
cmp.to_r -> rational
Returns the value as a rational if possible (the imaginary part should be exactly zero).
Complex(1, 0).to_r #=> (1/1)
Complex(1, 0.0).to_r #=> (1/1)
Complex(1, 2).to_r #=> RangeError
220 221 222 223 |
# File 'mrbgems/mruby-complex/mrblib/complex.rb', line 220 def to_r raise RangeError.new "can't convert #{to_s} into Rational" unless imaginary.zero? Rational(real, 1) end |
#to_s ⇒ Object
call-seq:
cmp.to_s -> string
Returns the value as a string.
Complex(2).to_s #=> "2+0i"
Complex(-8, 6).to_s #=> "-8+6i"
Complex(1, -2).to_s #=> "1-2i"
41 42 43 |
# File 'mrbgems/mruby-complex/mrblib/complex.rb', line 41 def to_s "#{real}#{'+' unless imaginary < 0}#{imaginary}#{'*' unless imaginary.finite?}i" end |