diff options
author | Marc-André Lafortune <github@marc-andre.ca> | 2021-01-10 16:21:10 -0500 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-01-10 16:21:10 -0500 |
commit | d8c8b79d24bf0f3177535501ad9b801e552fb2ad (patch) | |
tree | 7ec8bca59d51ebacdba899fbbf733736772f76ae | |
parent | 8187228de0142d3ac7950b7d977c2849e934c637 (diff) |
[ruby/matrix] Fix 0-th power [Bug #17521] (#4047)
Notes
Notes:
Merged-By: marcandre <github@marc-andre.ca>
-rw-r--r-- | lib/matrix.rb | 2 | ||||
-rw-r--r-- | spec/ruby/library/matrix/exponent_spec.rb | 19 |
2 files changed, 17 insertions, 4 deletions
diff --git a/lib/matrix.rb b/lib/matrix.rb index 32f1e233ce..8c636f61e8 100644 --- a/lib/matrix.rb +++ b/lib/matrix.rb @@ -1239,7 +1239,7 @@ class Matrix 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 447c962967..7eb478e1a7 100644 --- a/spec/ruby/library/matrix/exponent_spec.rb +++ b/spec/ruby/library/matrix/exponent_spec.rb @@ -21,17 +21,30 @@ describe "Matrix#**" do -> { 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 |