Method: Stick::Matrix#rank_e

Defined in:
lib/stick/matrix/core.rb

#rank_eObject

Returns the rank of the matrix. This method uses Euclidean algorism. If all elements are integer, really exact value. But, if an element is a float, can’t return exact value.

Matrix[[7,6], [3,9]].rank
  => 2


846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
# File 'lib/stick/matrix/core.rb', line 846

def rank_e
  a = to_a
  a_column_size = column_size
  a_row_size = row_size
  pi = 0
  (0 ... a_column_size).each do |j|
    if i = (pi ... a_row_size).find{|i0| !a[i0][j].zero?}
      if i != pi
        a[pi], a[i] = a[i], a[pi]
      end
      (pi + 1 ... a_row_size).each do |k|
        q = a[k][j].quo(a[pi][j])
        (pi ... a_column_size).each do |j0|
          a[k][j0] -= q * a[pi][j0]
        end
        if k > pi && !a[k][j].zero?
          a[k], a[pi] = a[pi], a[k]
          redo
        end
      end
      pi += 1
    end
  end
  pi
end