[前][次][番号順一覧][スレッド一覧]

ruby-changes:8074

From: tadf <ko1@a...>
Date: Sun, 28 Sep 2008 08:41:41 +0900 (JST)
Subject: [ruby-changes:8074] Ruby:r19600 (trunk): * lib/mathn.rb: a hack to provide canonicalization. This must be

tadf	2008-09-28 08:41:21 +0900 (Sun, 28 Sep 2008)

  New Revision: 19600

  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=19600

  Log:
    * lib/mathn.rb: a hack to provide canonicalization.  This must be
      temporary, but this seems to be not bad for the time being.

  Modified files:
    trunk/ChangeLog
    trunk/lib/mathn.rb

Index: ChangeLog
===================================================================
--- ChangeLog	(revision 19599)
+++ ChangeLog	(revision 19600)
@@ -1,3 +1,8 @@
+Sun Sep 28 08:37:12 2008  Tadayoshi Funaba  <tadf@d...>
+
+	* lib/mathn.rb: a hack to provide canonicalization.  This must be
+	  temporary, but this seems to be not bad for the time being.
+
 Sat Sep 27 06:22:18 2008  Nobuyoshi Nakada  <nobu@r...>
 
 	* lib/rake.rb (Module#rake_extension, String#ext, String#pathmap): use
Index: lib/mathn.rb
===================================================================
--- lib/mathn.rb	(revision 19599)
+++ lib/mathn.rb	(revision 19600)
@@ -18,19 +18,76 @@
   Math = CMath
 end
 
+class Object
+
+  def canon
+    if Rational === self
+      if denominator == 1
+	return numerator
+      end
+    elsif Complex === self
+      if Integer === imag && imag == 0
+	return real
+      end
+    end
+    self
+  end
+
+  private :canon
+
+end
+
+class Numeric
+
+  class << self
+
+    def def_canon(*ids)
+      for id in ids
+	module_eval <<-"end;"
+	  alias_method :__#{id.object_id}__, :#{id.to_s}
+	  private :__#{id.object_id}__
+	  def #{id.to_s}(*args, &block)
+	    __#{id.object_id}__(*args, &block).__send__(:canon)
+	  end
+	end;
+      end
+    end
+
+  end
+
+end
+
 class Fixnum
   remove_method :/
   alias / quo
+
+  def_canon *(instance_methods - Object.methods - [:canon])
+
 end
 
 class Bignum
   remove_method :/
   alias / quo
+
+  def_canon *(instance_methods - Object.methods - [:canon])
+
 end
 
+alias RationalOrig Rational
+private :RationalOrig
+def Rational(*args) RationalOrig(*args).__send__(:canon) end
+
 class Rational
   Unify = true
 
+  class << self
+    alias convert_orig convert
+    private :convert_orig
+    def convert(*args) convert_orig(*args).__send__(:canon) end
+  end
+
+  def_canon *(instance_methods - Object.methods - [:canon])
+
   alias power! **
 
   def ** (other)
@@ -169,6 +226,39 @@
   module_function :rsqrt
 end
 
+alias ComplexOrig Complex
+private :ComplexOrig
+def Complex(*args) ComplexOrig(*args).__send__(:canon) end
+
 class Complex
   Unify = true
+
+  class << self
+    alias convert_orig convert
+    private :convert_orig
+    def convert(*args) convert_orig(*args).__send__(:canon) end
+  end
+
+  def_canon *(instance_methods - Object.methods - [:canon])
+
 end
+
+class NilClass
+
+  def to_r() 0 end
+  def to_c() 0 end
+
+end
+
+class Integer
+
+  def to_r() self end
+  def to_c() self end
+
+end
+
+class Float
+
+  def_canon *(instance_methods - Object.methods - [:canon])
+
+end

--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]