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

ruby-changes:20304

From: marcandre <ko1@a...>
Date: Fri, 1 Jul 2011 15:21:30 +0900 (JST)
Subject: [ruby-changes:20304] marcandRe: r32352 (trunk): * lib/matrix.rb: Allow non integer exponents for Matrix#**

marcandre	2011-07-01 15:21:24 +0900 (Fri, 01 Jul 2011)

  New Revision: 32352

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

  Log:
    * lib/matrix.rb: Allow non integer exponents for Matrix#**

  Modified files:
    trunk/ChangeLog
    trunk/NEWS
    trunk/lib/matrix.rb

Index: ChangeLog
===================================================================
--- ChangeLog	(revision 32351)
+++ ChangeLog	(revision 32352)
@@ -1,3 +1,7 @@
+Fri Jul  1 15:21:14 2011  Marc-Andre Lafortune  <ruby-core@m...>
+
+	* lib/matrix.rb: Allow non integer exponents for Matrix#**
+
 Fri Jul  1 15:13:25 2011  Marc-Andre Lafortune  <ruby-core@m...>
 
 	* lib/matrix: Add Eigenvalue Decomposition
Index: lib/matrix.rb
===================================================================
--- lib/matrix.rb	(revision 32351)
+++ lib/matrix.rb	(revision 32352)
@@ -956,8 +956,10 @@
   private :inverse_from
 
   #
-  # Matrix exponentiation.  Currently implemented for integer powers only.
+  # Matrix exponentiation.
   # Equivalent to multiplying the matrix by itself N times.
+  # Non integer exponents will be handled by diagonalizing the matrix.
+  #
   #   Matrix[[7,6], [3,9]] ** 2
   #     => 67 96
   #        48 99
@@ -977,8 +979,9 @@
         return z if (other >>= 1).zero?
         x *= x
       end
-    when Float, Rational
-      Matrix.Raise ErrOperationNotImplemented, "**", self.class, other.class
+    when Numeric
+      v, d, v_inv = eigensystem
+      v * Matrix.diagonal(*d.each(:diagonal).map{|e| e ** other}) * v_inv
     else
       Matrix.Raise ErrOperationNotDefined, "**", self.class, other.class
     end
Index: NEWS
===================================================================
--- NEWS	(revision 32351)
+++ NEWS	(revision 32352)
@@ -178,6 +178,7 @@
     * Matrix#each and #each_with_index can iterate on a subset of the elements
     * Matrix#find_index returns [row, column] and can iterate on a subset
       of the elements
+    * Matrix#** implements Numeric exponents (using the eigensystem)
     * Matrix.zero can build rectangular matrices
 
 * net/http

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

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