rank()
public
Show source
def rank
if column_size > row_size
a = transpose.to_a
a_column_size = row_size
a_row_size = column_size
else
a = to_a
a_column_size = column_size
a_row_size = row_size
end
rank = 0
k = 0
begin
if (akk = a[k][k]) == 0
i = k
exists = true
loop do
if (i += 1) > a_row_size - 1
exists = false
break
end
break unless a[i][k] == 0
end
if exists
a[i], a[k] = a[k], a[i]
akk = a[k][k]
else
i = k
exists = true
loop do
if (i += 1) > a_column_size - 1
exists = false
break
end
break unless a[k][i] == 0
end
if exists
k.upto(a_row_size - 1) do |j|
a[j][k], a[j][i] = a[j][i], a[j][k]
end
akk = a[k][k]
else
next
end
end
end
for i in (k + 1)..(a_row_size - 1)
q = a[i][k].quo(akk)
for j in (k + 1)..(a_column_size - 1)
a[i][j] -= a[k][j] * q
end
end
rank += 1
end while (k += 1) <= a_column_size - 1
return rank
end