Project

General

Profile

Bug #10855 ยป matrix_inverse_to_integer.patch

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

View differences:

ChangeLog
1
Mon Feb 16 03:37:45 2015  Lito Nicolai <lito.nicolai@gmail.com>
2

  
3
      * lib/matrix.rb (inverse_from): return a matrix of integers
4
      whenever possible
5

  
6
      * test/matrix/test_matrix.rb (test_inverse): test for fix
7

  
1 8
Sat Feb  7 22:13:08 2015  Masaki Suketa <masaki.suketa@nifty.ne.jp>
2 9

  
3 10
	* test/win32ole/test_win32ole_record.rb: remove test using .NET
lib/matrix.rb
1103 1103
        @rows[k][j] = @rows[k][j].quo(akk)
1104 1104
      end
1105 1105
    end
1106
    self
1106
    integral_from_rational(self)
1107 1107
  end
1108 1108
  private :inverse_from
1109 1109

  
1110
  def integral_from_rational(src) # :nodoc:
1111
    n = Matrix.build(src.row_count) do |i , j|
1112
      elem = src[i, j]
1113
      if elem.is_a?(Rational) && elem.denominator == 1
1114
         elem.to_i
1115
      else
1116
        break
1117
      end
1118
    end
1119

  
1120
    n.nil? ? src : n
1121
  end
1122
  private :integral_from_rational
1123

  
1110 1124
  #
1111 1125
  # Matrix exponentiation.
1112 1126
  # Equivalent to multiplying the matrix by itself N times.
test/matrix/test_matrix.rb
174 174

  
175 175
  def test_inverse
176 176
    assert_equal(Matrix.empty(0, 0), Matrix.empty.inverse)
177
    assert_equal(Matrix[[-1, 1], [0, -1]], Matrix[[-1, -1], [0, -1]].inverse)
177
    assert Matrix[[-1, 1], [0, -1]].eql? Matrix[[-1, -1], [0, -1]].inverse
178
    assert Matrix[[1, 2], [3, 4]].inverse.eql?(
179
      Matrix[[Rational(-2), Rational(1)],
180
             [Rational(3, 2), Rational(-1, 2)]]
181
    )
178 182
    assert_raise(ExceptionForMatrix::ErrDimensionMismatch) { @m1.inverse }
183
    assert_raise(ExceptionForMatrix::ErrNotRegular) {
184
      Matrix[[0, 0], [0, 0]].inverse
185
    }
179 186
  end
180 187

  
181 188
  def test_determinant