[#20525] [BigDecimal] changing rule of coerce — "Tadashi Saito" <shiba@...2.accsnet.ne.jp>

斎藤です。

44 messages 2003/07/07
[#20527] Re: [BigDecimal] changing rule of coerce — "Shigeo Kobayashi" <shigeo@...> 2003/07/07

小林です。

[#20528] Re: [BigDecimal] changing rule of coerce — matz@... (Yukihiro Matsumoto) 2003/07/07

まつもと ゆきひろです

[#20570] Marshal upgrade — matz@... (Yukihiro Matsumoto)

まつもと ゆきひろです

41 messages 2003/07/09
[#20575] Re: Marshal upgrade — Masatoshi SEKI <m_seki@...> 2003/07/09

咳といいます。

[#20583] Re: Marshal upgrade — matz@... (Yukihiro Matsumoto) 2003/07/09

まつもと ゆきひろです

[#21016] Re: Marshal upgrade — matz@... (Yukihiro Matsumoto) 2003/07/30

まつもと ゆきひろです

[#20804] add library — nobu.nakada@... 2003/07/23

なかだです。

[#20580] add library(Re:ruby-dev:20570) — たむらけんいち <sgs02516@...>

たむらです。

30 messages 2003/07/09
[#20656] Re: add library — "NAKAMURA, Hiroshi" <nakahiro@...> 2003/07/14

なひです。

[#20658] Re: add library — GOTOU Yuuzou <gotoyuzo@...> 2003/07/14

In message <038d01c349cb$eaad71d0$93222fc0@sarion.co.jp>,

[#20659] Re: add library — matz@... (Yukihiro Matsumoto) 2003/07/14

まつもと ゆきひろです

[#20660] Re: add library — GOTOU Yuuzou <gotoyuzo@...> 2003/07/14

In message <1058171960.400840.10041.nullmailer@picachu.netlab.jp>,

[#20661] Re: add library — Takahiro Kambe <taca@...> 2003/07/14

話をそらしてしまうかもしれませんが、

[#20665] Re: add library — GOTOU Yuuzou <gotoyuzo@...> 2003/07/14

In message <20030714.183104.09092354.taca@back-street.net>,

[#20666] Re: add library — Takahiro Kambe <taca@...> 2003/07/14

In message <20030715.013655.424936247.gotoyuzo@kotetsu.does.notwork.org>

[#20668] Re: add library — GOTOU Yuuzou <gotoyuzo@...> 2003/07/14

In message <20030715.025907.26217115.taca@back-street.net>,

[#20750] Re: add library — Takahiro Kambe <taca@...> 2003/07/21

In message <20030715.051853.968499478.gotoyuzo@kotetsu.does.notwork.org>

[#20751] Re: add library — GOTOU Yuuzou <gotoyuzo@...> 2003/07/21

In message <20030721.163444.09092937.taca@back-street.net>,

[#20655] frozen ThreadGroup — Hidetoshi NAGAI <nagai@...>

永井@知能.九工大です.

26 messages 2003/07/14
[#20671] Re: frozen ThreadGroup — matz@... (Yukihiro Matsumoto) 2003/07/14

まつもと ゆきひろです

[#20673] Re: frozen ThreadGroup — Hidetoshi NAGAI <nagai@...> 2003/07/15

永井@知能.九工大です.

[#20676] Re: frozen ThreadGroup — matz@... (Yukihiro Matsumoto) 2003/07/15

まつもと ゆきひろです

[#20677] Re: frozen ThreadGroup — Hidetoshi NAGAI <nagai@...> 2003/07/15

永井@知能.九工大です.

[#20681] Re: frozen ThreadGroup — matz@... (Yukihiro Matsumoto) 2003/07/15

まつもと ゆきひろです

[#20690] portable(?) UserID/GroupID control (Re: frozen ThreadGroup) — Hidetoshi NAGAI <nagai@...> 2003/07/16

永井@知能.九工大です.

[#20712] Re: portable(?) UserID/GroupID control — Hidetoshi NAGAI <nagai@...> 2003/07/17

永井@知能.九工大です.

[#20735] Re: portable(?) UserID/GroupID control — matz@... (Yukihiro Matsumoto) 2003/07/20

まつもと ゆきひろです

[#20736] Re: portable(?) UserID/GroupID control — Hidetoshi NAGAI <nagai@...> 2003/07/20

永井@知能.九工大です.

[#20737] Re: portable(?) UserID/GroupID control — matz@... (Yukihiro Matsumoto) 2003/07/20

まつもと ゆきひろです

[#20748] [BigDecimal] exception handling — "Tadashi Saito" <shiba@...2.accsnet.ne.jp>

斎藤です。

20 messages 2003/07/21

[#20765] Re: [ruby-cvs] ruby/lib: * lib/tmpdir.rb: new library to get temporary directory path, — WATANABE Hirofumi <eban@...>

わたなべです。

9 messages 2003/07/21

[#20780] complex.rb — Masahiro TANAKA <masa@...>

complex.rb についての修正案を[ruby-math:00543]で提案しましたが、その後

25 messages 2003/07/22
[#20782] Re: complex.rb — matz@... (Yukihiro Matsumoto) 2003/07/22

まつもと ゆきひろです

[#20900] Re: complex.rb — Masahiro TANAKA <masa@...> 2003/07/25

At Tue, 22 Jul 2003 17:30:31 +0900, Yukihiro Matsumoto wrote:

[#20905] Re: complex.rb — matz@... (Yukihiro Matsumoto) 2003/07/25

まつもと ゆきひろです

[#20906] Re: complex.rb — keiju@... (石塚圭樹) 2003/07/25

けいじゅ@いしつかです.

[#20810] Rational 始めました。 — Shin-ichiro HARA <sinara@...>

原です。

13 messages 2003/07/23
[#20876] Re: Rational 始めました。 — keiju@... (石塚圭樹) 2003/07/24

けいじゅ@いしつかです.

[#20954] ruby 1.8.0 preview5 — matz@... (Yukihiro Matsumoto)

まつもと ゆきひろです

15 messages 2003/07/28

[#20957] [BigDecimal] conflict between Numeric#div and BigDecimal#div — "Tadashi Saito" <shiba@...2.accsnet.ne.jp>

斎藤です。

29 messages 2003/07/28
[#20960] Re: [BigDecimal] conflict between Numeric#div and BigDecimal#div — Masahiro TANAKA <masa@...> 2003/07/28

At Mon, 28 Jul 2003 18:26:20 +0900, Tadashi Saito wrote:

[#20962] Re: [BigDecimal] conflict between Numeric#div and BigDecimal#div — matz@... (Yukihiro Matsumoto) 2003/07/28

まつもと ゆきひろです

[#20990] Re: [BigDecimal] conflict between Numeric#div and BigDecimal#div — Masahiro TANAKA <masa@...> 2003/07/29

At Mon, 28 Jul 2003 21:16:08 +0900, Yukihiro Matsumoto wrote:

[#20992] Re: [BigDecimal] conflict between Numeric#div and BigDecimal#div — matz@... (Yukihiro Matsumoto) 2003/07/29

まつもと ゆきひろです

[ruby-dev:20900] Re: complex.rb

From: Masahiro TANAKA <masa@...>
Date: 2003-07-25 10:55:17 UTC
List: ruby-dev #20900
At Tue, 22 Jul 2003 17:30:31 +0900, Yukihiro Matsumoto wrote:

> |あと、ここには含まれていませんが、complex.rb 中の Fixnum#** の再定義は、
> |Complex とは関係ないだけでなく、(-2)**2 => Complex(4, 0) という副作用
> |があるので、削除して欲しいです。(というのも合意してもらったはず)
> 
> 石塚さん、作業しますか。それとも私がやりましょうか?

石塚さんがお忙しそうなので、complex.rb のパッチを作りました。
** の再定義を削除する他に、以下の変更も含みます。

 * Complex.new を public にする
 * Complex.new の引数が Complex であったときに例外を上げる
 * Math.atan2 の再定義の削除
 * Complex版 cosh,sinh,tanh,acos,asin,atan,acosh,asinh,atanh の追加
 * 別名の追加 ( arg -> angle, conjugate -> conj )

あと Complex(real,imag) をもっと単純にしたほうがいいと思いますが、
とりあえず問題ではないのでそのままにしてあります。

田中昌宏


Index: complex.rb
===================================================================
RCS file: /ruby/ruby/lib/complex.rb,v
retrieving revision 1.16
diff -u -r1.16 complex.rb
--- complex.rb	22 Jul 2003 08:42:47 -0000	1.16
+++ complex.rb	25 Jul 2003 10:41:28 -0000
@@ -21,7 +21,8 @@
 #
 # The following +Math+ module methods are redefined to handle Complex arguments.
 # They will work as normal with non-Complex arguments.
-#    sqrt exp cos sin tan log log10 atan2
+#    sqrt exp cos sin tan log log10
+#    cosh sinh tanh acos asin atan acosh asinh atanh
 #
 
 
@@ -66,7 +67,6 @@
     Complex(r*Math.cos(theta), r*Math.sin(theta))
   end
 
-  private_class_method :new
   #
   # Creates a +Complex+ number <tt>a</tt>+<tt>b</tt><i>i</i>.
   #
@@ -76,7 +76,9 @@
 
   def initialize(a, b)
     raise "non numeric 1st arg `#{a.inspect}'" if !a.kind_of? Numeric
+    raise "`#{a.inspect}' for 1st arg" if a.kind_of? Complex
     raise "non numeric 2nd arg `#{b.inspect}'" if !b.kind_of? Numeric
+    raise "`#{b.inspect}' for 2nd arg" if b.kind_of? Complex
     @real = a
     @image = b
   end
@@ -181,7 +183,7 @@
       end
     elsif Complex.generic?(other)
       r, theta = polar
-      Complex.polar(r.power!(other), theta * other)
+      Complex.polar(r**other, theta*other)
     else
       x, y = other.coerce(self)
       x**y
@@ -238,6 +240,7 @@
   def arg
     Math.atan2(@image.to_f, @real.to_f)
   end
+  alias angle arg
   
   #
   # Returns the absolute value _and_ the argument.
@@ -252,6 +255,7 @@
   def conjugate
     Complex(@real, -@image)
   end
+  alias conj conjugate
   
   #
   # Compares the absolute values of the two numbers.
@@ -395,6 +399,7 @@
       return Math::PI
     end
   end
+  alias angle arg
   
   #
   # See Complex#polar.
@@ -409,47 +414,27 @@
   def conjugate
     self
   end
+  alias conj conjugate
 end
 
 
-class Fixnum
-  unless defined? 1.power!
-    alias power! ** 
-    p [__FILE__, defined? 1.power!]
-  end
-
-  # Redefined to handle a Complex argument.
-  def ** (other)
-    if self < 0
-      Complex.new!(self, 0) ** other
-    else
-      if defined? self.rpower
-        self.rpower(other)
-      else
-	self.power!(other)
-      end
-    end
-  end
-end
-
-class Bignum
-  alias power! **
-end
-
-class Float
-  alias power! **
-end
-
 module Math
   alias sqrt! sqrt
   alias exp! exp
+  alias log! log
+  alias log10! log10
   alias cos! cos
   alias sin! sin
   alias tan! tan
-  alias log! log
-  alias atan! atan  
-  alias log10! log10
-  alias atan2! atan2
+  alias cosh! cosh
+  alias sinh! sinh
+  alias tanh! tanh
+  alias acos! acos
+  alias asin! asin
+  alias atan! atan
+  alias acosh! acosh
+  alias asinh! asinh
+  alias atanh! atanh  
 
   # Redefined to handle a Complex argument.
   def sqrt(z)
@@ -479,20 +464,6 @@
     end
   end
   
-  #
-  # Hyperbolic cosine.
-  #
-  def cosh!(x)
-    (exp!(x) + exp!(-x))/2.0
-  end
-  
-  #
-  # Hyperbolic sine.
-  #
-  def sinh!(x)
-    (exp!(x) - exp!(-x))/2.0
-  end
-  
   # Redefined to handle a Complex argument.
   def cos(z)
     if Complex.generic?(z)
@@ -521,6 +492,30 @@
       sin(z)/cos(z)
     end
   end
+
+  def sinh(z)
+    if Complex.generic?(z)
+      sinh!(z)
+    else
+      Complex( sinh!(z.real)*cos!(z.image), cosh!(z.real)*sin!(z.image) )
+    end
+  end
+
+  def cosh(z)
+    if Complex.generic?(z)
+      cosh!(z)
+    else
+      Complex( cosh!(z.real)*cos!(z.image), sinh!(z.real)*sin!(z.image) )
+    end
+  end
+
+  def tanh(z)
+    if Complex.generic?(z)
+      tanh!(z)
+    else
+      sinh(z)/cosh(z)
+    end
+  end
   
   # Redefined to handle a Complex argument.
   def log(z)
@@ -540,69 +535,92 @@
       log(z)/log!(10)
     end
   end
-  
-  # FIXME: I don't know what the point of this is.  If you give it Complex
-  # arguments, it will fail.
-  def atan2(x, y)
-    if Complex.generic?(x) and Complex.generic?(y)
-      atan2!(x, y)
+
+  def acos(z)
+    if Complex.generic?(z)
+      acos!(z)
     else
-      fail "Not yet implemented."
+      -1.0.im * log( z + 1.0.im * sqrt(1.0-z*z) )
     end
   end
-  
-  #
-  # Hyperbolic arctangent.
-  #
-  def atanh!(x)
-    log((1.0 + x.to_f) / ( 1.0 - x.to_f)) / 2.0
+
+  def asin(z)
+    if Complex.generic?(z)
+      asin!(z)
+    else
+      -1.0.im * log( 1.0.im * z + sqrt(1.0-z*z) )
+    end
   end
-  
-  # Redefined to handle a Complex argument.
+
   def atan(z)
     if Complex.generic?(z)
-      atan2!(z, 1)
-    elsif z.image == 0
-      atan2(z.real,1)
-    else
-      a = z.real
-      b = z.image
-      
-      c = (a*a + b*b - 1.0)
-      d = (a*a + b*b + 1.0)
+      atan!(z)
+    else
+      1.0.im * log( (1.0.im+z) / (1.0.im-z) ) / 2.0
+    end
+  end
 
-      Complex(atan2!((c + sqrt(c*c + 4.0*a*a)), 2.0*a),
-	      atanh!((-d + sqrt(d*d - 4.0*b*b))/(2.0*b)))
+  def acosh(z)
+    if Complex.generic?(z)
+      acosh!(z)
+    else
+      log( z + sqrt(z*z-1.0) )
     end
   end
-  
-  module_function :sqrt
+
+  def asinh(z)
+    if Complex.generic?(z)
+      asinh!(z)
+    else
+      log( z + sqrt(1.0+z*z) )
+    end
+  end
+
+  def atanh(z)
+    if Complex.generic?(z)
+      atanh!(z)
+    else
+      log( (1.0+z) / (1.0-z) ) / 2.0
+    end
+  end
+
   module_function :sqrt!
+  module_function :sqrt
   module_function :exp!
   module_function :exp
+  module_function :log!
+  module_function :log
+  module_function :log10!
+  module_function :log10
   module_function :cosh!
+  module_function :cosh
   module_function :cos!
   module_function :cos
   module_function :sinh!
+  module_function :sinh
   module_function :sin!
   module_function :sin
   module_function :tan!
   module_function :tan
-  module_function :log!
-  module_function :log
-  module_function :log10!
-  module_function :log
-  module_function :atan2!
-  module_function :atan2
-#  module_function :atan!
+  module_function :tanh!
+  module_function :tanh
+  module_function :acos!
+  module_function :acos
+  module_function :asin!
+  module_function :asin
+  module_function :atan!
   module_function :atan
+  module_function :acosh!
+  module_function :acosh
+  module_function :asinh!
+  module_function :asinh
   module_function :atanh!
+  module_function :atanh
   
 end
 
 # Documentation comments:
 #  - source: original (researched from pickaxe)
 #  - a couple of fixme's
-#  - Math module methods sinh! etc. a bit fuzzy.  What exactly is the intention?
 #  - RDoc output for Bignum etc. is a bit short, with nothing but an
 #    (undocumented) alias.  No big deal.

In This Thread