From: Masahiro TANAKA Date: 2003-07-22T17:04:27+09:00 Subject: [ruby-dev:20780] complex.rb complex.rb についての修正案を[ruby-math:00543]で提案しましたが、その後 合意していただいた点がまだ反映されていません。他に若干の修正点を加えて パッチを作りましたので、お取り込み願います。このパッチの修正点は、 ・Complex#** のバグfix ・image に imag という別名をつける ・Math.sqrt の再定義の修正 (正木さん案[ruby-math:00553]) ・Complex#abs, Complex#arg を少し効率よくする あと、ここには含まれていませんが、complex.rb 中の Fixnum#** の再定義は、 Complex とは関係ないだけでなく、(-2)**2 => Complex(4, 0) という副作用 があるので、削除して欲しいです。(というのも合意してもらったはず) 田中昌宏 Index: lib/complex.rb =================================================================== RCS file: /ruby/ruby/lib/complex.rb,v retrieving revision 1.15 diff -u -r1.15 complex.rb --- lib/complex.rb 17 Jul 2003 08:58:16 -0000 1.15 +++ lib/complex.rb 22 Jul 2003 07:40:58 -0000 @@ -184,7 +184,7 @@ Complex.polar(r.power!(other), theta * other) else x, y = other.coerce(self) - x/y + x**y end end @@ -222,7 +222,7 @@ # plane. # def abs - Math.sqrt!((@real*@real + @image*@image).to_f) + Math.hypot(@real, @image) end # @@ -352,6 +352,7 @@ # The imaginary part of a complex number. attr :image + alias imag image end @@ -382,6 +383,7 @@ def image 0 end + alias imag image # # See Complex#arg. @@ -390,7 +392,7 @@ if self >= 0 return 0 else - return Math.atan2(1,1)*4 + return Math::PI end end @@ -458,10 +460,12 @@ Complex(0,sqrt!(-z)) end else - if defined? Rational - z**Rational(1,2) + if z.image < 0 + sqrt(z.conjugate).conjugate else - z**0.5 + r = z.abs + x = z.real + Complex( sqrt!((r+x)/2), sqrt!((r-x)/2) ) end end end