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

ruby-changes:64824

From: Marc-Andr=C3=A9 <ko1@a...>
Date: Mon, 11 Jan 2021 06:21:32 +0900 (JST)
Subject: [ruby-changes:64824] d8c8b79d24 (master): [ruby/matrix] Fix 0-th power [Bug #17521] (#4047)

https://git.ruby-lang.org/ruby.git/commit/?id=d8c8b79d24

From d8c8b79d24bf0f3177535501ad9b801e552fb2ad Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Marc-Andr=C3=A9=20Lafortune?= <github@m...>
Date: Sun, 10 Jan 2021 16:21:10 -0500
Subject: [ruby/matrix] Fix 0-th power [Bug #17521] (#4047)


diff --git a/lib/matrix.rb b/lib/matrix.rb
index 32f1e23..8c636f6 100644
--- a/lib/matrix.rb
+++ b/lib/matrix.rb
@@ -1239,7 +1239,7 @@ class Matrix https://github.com/ruby/ruby/blob/trunk/lib/matrix.rb#L1239
     when Integer
       case
       when exp == 0
-        _make_sure_it_is_invertible = inverse
+        raise ErrDimensionMismatch unless square?
         self.class.identity(column_count)
       when exp < 0
         inverse.power_int(-exp)
diff --git a/spec/ruby/library/matrix/exponent_spec.rb b/spec/ruby/library/matrix/exponent_spec.rb
index 447c962..7eb478e 100644
--- a/spec/ruby/library/matrix/exponent_spec.rb
+++ b/spec/ruby/library/matrix/exponent_spec.rb
@@ -21,17 +21,30 @@ describe "Matrix#**" do https://github.com/ruby/ruby/blob/trunk/spec/ruby/library/matrix/exponent_spec.rb#L21
       -> { m ** 0 }.should raise_error(Matrix::ErrDimensionMismatch)
     end
 
-    describe "that is <= 0" do
+    describe "that is < 0" do
       it "returns the inverse of **(-n)" do
         m = Matrix[ [1, 1], [1, 2] ]
         (m ** -2).should == Matrix[ [5, -3], [-3, 2]]
         (m ** -4).should == (m.inverse ** 4)
       end
 
-      it "raises a ErrDimensionMismatch for irregular matrices" do
+      it "raises a ErrNotRegular for irregular matrices" do
         m = Matrix[ [1, 1], [1, 1] ]
         -> { m ** -2 }.should raise_error(Matrix::ErrNotRegular)
-        -> { m ** 0 }.should raise_error(Matrix::ErrNotRegular)
+      end
+    end
+
+    ruby_bug '#17521', ''..'3.0.0' do
+      describe "that is 0" do
+        it "returns the identity for square matrices" do
+          m = Matrix[ [1, 1], [1, 1] ]
+          (m ** 0).should == Matrix.identity(2)
+        end
+
+        it "raises an ErrDimensionMismatch for non-square matrices" do
+          m = Matrix[ [1, 1] ]
+          -> { m ** 0 }.should raise_error(Matrix::ErrDimensionMismatch)
+        end
       end
     end
   end
-- 
cgit v0.10.2


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

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