Project

General

Profile

Bug #10855 ยป matrix_inverse_to_integer.patch

LitoNico (Lito Nicolai), 02/15/2015 07:02 PM

View differences:

ChangeLog
Mon Feb 16 03:37:45 2015 Lito Nicolai <lito.nicolai@gmail.com>
* lib/matrix.rb (inverse_from): return a matrix of integers
whenever possible
* test/matrix/test_matrix.rb (test_inverse): test for fix
Sat Feb 7 22:13:08 2015 Masaki Suketa <masaki.suketa@nifty.ne.jp>
* test/win32ole/test_win32ole_record.rb: remove test using .NET
lib/matrix.rb
@rows[k][j] = @rows[k][j].quo(akk)
end
end
self
integral_from_rational(self)
end
private :inverse_from
def integral_from_rational(src) # :nodoc:
n = Matrix.build(src.row_count) do |i , j|
elem = src[i, j]
if elem.is_a?(Rational) && elem.denominator == 1
elem.to_i
else
break
end
end
n.nil? ? src : n
end
private :integral_from_rational
#
# Matrix exponentiation.
# Equivalent to multiplying the matrix by itself N times.
test/matrix/test_matrix.rb
def test_inverse
assert_equal(Matrix.empty(0, 0), Matrix.empty.inverse)
assert_equal(Matrix[[-1, 1], [0, -1]], Matrix[[-1, -1], [0, -1]].inverse)
assert Matrix[[-1, 1], [0, -1]].eql? Matrix[[-1, -1], [0, -1]].inverse
assert Matrix[[1, 2], [3, 4]].inverse.eql?(
Matrix[[Rational(-2), Rational(1)],
[Rational(3, 2), Rational(-1, 2)]]
)
assert_raise(ExceptionForMatrix::ErrDimensionMismatch) { @m1.inverse }
assert_raise(ExceptionForMatrix::ErrNotRegular) {
Matrix[[0, 0], [0, 0]].inverse
}
end
def test_determinant
    (1-1/1)