summaryrefslogtreecommitdiffhomepage
path: root/mrbgems/mruby-complex/mrblib/complex.rb
diff options
context:
space:
mode:
Diffstat (limited to 'mrbgems/mruby-complex/mrblib/complex.rb')
-rw-r--r--mrbgems/mruby-complex/mrblib/complex.rb155
1 files changed, 114 insertions, 41 deletions
diff --git a/mrbgems/mruby-complex/mrblib/complex.rb b/mrbgems/mruby-complex/mrblib/complex.rb
index 0815c9a71..266c00c36 100644
--- a/mrbgems/mruby-complex/mrblib/complex.rb
+++ b/mrbgems/mruby-complex/mrblib/complex.rb
@@ -1,95 +1,168 @@
class Complex < Numeric
- def initialize(real = 0, imaginary = 0)
+ def initialize(real, imaginary)
+ real = real.to_f unless real.is_a? Numeric
+ imaginary = imaginary.to_f unless imaginary.is_a? Numeric
@real = real
@imaginary = imaginary
end
+ def self.polar(abs, arg = 0)
+ Complex(abs * Math.cos(arg), abs * Math.sin(arg))
+ end
+
+ def self.rectangular(real, imaginary = 0)
+ _new(real, imaginary)
+ end
+
def inspect
"(#{to_s})"
end
def to_s
- "#{real}#{'+'}#{imaginary}i"
+ "#{real}#{'+' unless imaginary.negative?}#{imaginary}i"
end
def +@
- Complex.new(real, imaginary)
+ Complex(real, imaginary)
end
def -@
- Complex.new(-real, -imaginary)
+ Complex(-real, -imaginary)
end
def +(rhs)
if rhs.is_a? Complex
- Complex.new(real + rhs.real, imaginary + rhs.imaginary)
+ Complex(real + rhs.real, imaginary + rhs.imaginary)
elsif rhs.is_a? Numeric
- Complex.new(real + rhs, imaginary)
+ Complex(real + rhs, imaginary)
end
end
def -(rhs)
if rhs.is_a? Complex
- Complex.new(real - rhs.real, imaginary - rhs.imaginary)
+ Complex(real - rhs.real, imaginary - rhs.imaginary)
elsif rhs.is_a? Numeric
- Complex.new(real - rhs, imaginary)
+ Complex(real - rhs, imaginary)
end
end
def *(rhs)
if rhs.is_a? Complex
- Complex.new(real * rhs.real - imaginary * rhs.imaginary, real * rhs.imaginary + rhs.real * imaginary)
+ Complex(real * rhs.real - imaginary * rhs.imaginary, real * rhs.imaginary + rhs.real * imaginary)
elsif rhs.is_a? Numeric
- Complex.new(real * rhs, imaginary * rhs)
+ Complex(real * rhs, imaginary * rhs)
end
end
def /(rhs)
if rhs.is_a? Complex
div = rhs.real * rhs.real + rhs.imaginary * rhs.imaginary
- Complex.new((real * rhs.real + imaginary * rhs.imaginary) / div, (rhs.real * imaginary - real * rhs.imaginary) / div)
+ Complex((real * rhs.real + imaginary * rhs.imaginary) / div, (rhs.real * imaginary - real * rhs.imaginary) / div)
elsif rhs.is_a? Numeric
- Complex.new(real / rhs, imaginary / rhs)
+ Complex(real / rhs, imaginary / rhs)
end
end
+ alias_method :quo, :/
- attr_reader :real, :imaginary
-end
+ def ==(rhs)
+ if rhs.is_a? Complex
+ real == rhs.real && imaginary == rhs.imaginary
+ elsif rhs.is_a? Numeric
+ imaginary.zero? && real == rhs
+ end
+ end
-def Complex(real = 0, imaginary = 0)
- Complex.new(real, imaginary)
-end
+ def abs
+ Math.sqrt(abs2)
+ end
+ alias_method :magnitude, :abs
-module ForwardOperatorToComplex
- def __forward_operator_to_complex(op, &b)
- original_operator_name = "__original_operator_#{op}_complex"
- alias_method original_operator_name, op
- define_method op do |rhs|
- if rhs.is_a? Complex
- Complex.new(self).send(op, rhs)
- else
- send(original_operator_name, rhs)
- end
- end
+ def abs2
+ real * real + imaginary * imaginary
+ end
+
+ def arg
+ Math.atan2 imaginary, real
+ end
+ alias_method :angle, :arg
+ alias_method :phase, :arg
+
+ def conjugate
+ Complex(real, -imaginary)
+ end
+ alias_method :conj, :conjugate
+
+ def numerator
+ self
+ end
+
+ def denominator
+ 1
+ end
+
+ def fdiv(numeric)
+ Complex(real.to_f / numeric, imaginary.to_f / numeric)
+ end
+
+ def polar
+ [abs, arg]
+ end
+
+ def real?
+ false
end
- def __forward_operators_to_complex
- __forward_operator_to_complex :+
- __forward_operator_to_complex :-
- __forward_operator_to_complex :*
- __forward_operator_to_complex :/
+ def rectangular
+ [real, imaginary]
+ end
+ alias_method :rect, :rectangular
+
+ def to_c
+ self
+ end
+
+ def to_f
+ raise RangeError.new "can't convert #{to_s} into Float" unless imaginary.zero?
+ real.to_f
+ end
- singleton_class.undef_method :__forward_operator_to_complex
- singleton_class.undef_method :__forward_operators_to_complex
+ def to_i
+ raise RangeError.new "can't convert #{to_s} into Integer" unless imaginary.zero?
+ real.to_i
end
+
+ def to_r
+ raise RangeError.new "can't convert #{to_s} into Rational" unless imaginary.zero?
+ Rational(real, 1)
+ end
+
+ attr_reader :real, :imaginary
+ alias_method :imag, :imaginary
+end
+
+class << Complex
+ alias_method :_new, :new
+ undef_method :new
+
+ alias_method :rect, :rectangular
end
-class Fixnum
- extend ForwardOperatorToComplex
- __forward_operators_to_complex
+def Complex(real, imaginary = 0)
+ Complex.rectangular(real, imaginary)
end
-class Float
- extend ForwardOperatorToComplex
- __forward_operators_to_complex
+[Fixnum, Float].each do |cls|
+ [:+, :-, :*, :/, :==].each do |op|
+ cls.instance_exec do
+ original_operator_name = "__original_operator_#{op}_complex"
+ alias_method original_operator_name, op
+ define_method op do |rhs|
+ if rhs.is_a? Complex
+ Complex(self).send(op, rhs)
+ else
+ send(original_operator_name, rhs)
+ end
+ end
+ end
+ end
end \ No newline at end of file