1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

[ruby/matrix] Fix 0-th power [Bug #17521] (#4047)

This commit is contained in:
Marc-André Lafortune 2021-01-10 16:21:10 -05:00 committed by GitHub
parent 8187228de0
commit d8c8b79d24
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
Notes: git 2021-01-11 06:21:37 +09:00
Merged-By: marcandre <github@marc-andre.ca>
2 changed files with 17 additions and 4 deletions

View file

@ -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)

View file

@ -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