stdlib/matrix.rb
#
# Private. Use Matrix#determinant
#
# Returns the determinant of the matrix, using
# Bareiss' multistep integer-preserving gaussian elimination.
# It has the same computational cost order O(n^3) as standard Gaussian elimination.
# Intermediate results are fraction free and of lower complexity.
# A matrix of Integers will have thus intermediate results that are also Integers,
# with smaller bignums (if any), while a matrix of Float will usually have
# intermediate results with better precision.
#
Private. Use Matrix#determinant
Returns the determinant of the matrix, using
Bareiss' multistep integer-preserving gaussian elimination.
It has the same computational cost order O(n^3) as standard Gaussian elimination.
Intermediate results are fraction free and of lower complexity.
A matrix of Integers will have thus intermediate results that are also Integers,
with smaller bignums (if any), while a matrix of Float will usually have
intermediate results with better precision.
Private . Use Matrix#determinant Returns the determinant of the matrix using Bareiss ' multistep integer - preserving gaussian elimination . It has the same computational cost order O ( n^3 ) as standard Gaussian elimination . Intermediate results are fraction free and of lower complexity . A matrix of Integers will have thus intermediate results that are also Integers with smaller bignums ( if any ) while a matrix of Float will usually have intermediate results with better precision .
def determinant_bareiss
    size = row_count
    last = size - 1
    a = to_a
    no_pivot = Proc.new{ return 0 }
    sign = +1
    pivot = 1
    size.times do |k|
      previous_pivot = pivot
      if (pivot = a[k][k]) == 0
        switch = (k+1 ... size).find(no_pivot) {|row|
          a[row][k] != 0
        }
        a[switch], a[k] = a[k], a[switch]
        pivot = a[k][k]
        sign = -sign
      end
      (k+1).upto(last) do |i|
        ai = a[i]
        (k+1).upto(last) do |j|
          ai[j] =  (pivot * ai[j] - ai[k] * a[k][j]) / previous_pivot
        end
      end
    end
    sign * pivot
  end
def determinant_bareiss size = row_count last = size - 1 a = to_a no_pivot = Proc . new { return 0 } sign = + 1 pivot = 1 size . times do | k | previous_pivot = pivot if ( pivot = a [ k ] [ k ] ) == 0 switch = ( k + 1 ... size ) . find ( no_pivot ) { | row | a [ row ] [ k ] != 0 } a [ switch ] , a [ k ] = a [ k ] , a [ switch ] pivot = a [ k ] [ k ] sign = - sign end ( k + 1 ) . upto ( last ) do | i | ai = a [ i ] ( k + 1 ) . upto ( last ) do | j | ai [ j ] = ( pivot * ai [ j ] - ai [ k ] * a [ k ] [ j ] ) / previous_pivot end end end sign * pivot end
Private.
Private .