summaryrefslogtreecommitdiffhomepage
path: root/mrbgems/mruby-complex/mrblib
diff options
context:
space:
mode:
authorYukihiro "Matz" Matsumoto <[email protected]>2019-05-24 13:15:26 +0900
committerYukihiro "Matz" Matsumoto <[email protected]>2019-05-26 09:19:29 +0900
commit4261ba944330f27dcb80e5e4580f73bd6b3a7106 (patch)
tree3a8b42a1d0d81217fa7cdddd911e3b0eba8fcff0 /mrbgems/mruby-complex/mrblib
parentc33ab7c0bb9416f772fa493da415553eb783f2fb (diff)
downloadmruby-4261ba944330f27dcb80e5e4580f73bd6b3a7106.tar.gz
mruby-4261ba944330f27dcb80e5e4580f73bd6b3a7106.zip
Remove some overhead from methods defined in Ruby in Complex.
Diffstat (limited to 'mrbgems/mruby-complex/mrblib')
-rw-r--r--mrbgems/mruby-complex/mrblib/complex.rb37
1 files changed, 10 insertions, 27 deletions
diff --git a/mrbgems/mruby-complex/mrblib/complex.rb b/mrbgems/mruby-complex/mrblib/complex.rb
index 4c0c19c70..0299e7675 100644
--- a/mrbgems/mruby-complex/mrblib/complex.rb
+++ b/mrbgems/mruby-complex/mrblib/complex.rb
@@ -1,19 +1,8 @@
class Complex < Numeric
- 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
@@ -23,43 +12,43 @@ class Complex < Numeric
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, :/
@@ -117,12 +106,6 @@ class Complex < Numeric
alias_method :imag, :imaginary
end
-module Kernel
- def Complex(real, imaginary = 0)
- Complex.rectangular(real, imaginary)
- end
-end
-
[Fixnum, Float].each do |cls|
[:+, :-, :*, :/, :==].each do |op|
cls.instance_exec do