Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Gaussian elimination solution in Uncategorized category for Determinant by Yoha
def checkio(data):
def perm(i,j):
n = len(data)
for k in range(n):
t = data[i][k]
data[i][k] = data[j][k]
data[j][k] = t
def linear(i,j,a):
n = len(data)
for k in range(n):
data[i][k] += a * data[j][k]
def aux(k):
n = len(data)
if k == n:
return 1
p = -1
for i in range(k,n):
if data[i][k] != 0:
p = i
break
if p == -1:
return 0
perm(k,p)
x = data[k][k]
if x == 0:
return 0
for j in range(k,n):
data[k][j] /= x
for i in range(k+1,n):
linear(i,k,-data[i][k])
if (p-k) % 2 == 1:
x = -x
return x*aux(k+1)
return round(aux(0))
#These "asserts" using only for self-checking and not necessary for auto-testing
if __name__ == '__main__':
assert checkio([[4,3], [6,3]]) == -6, 'First example'
assert checkio([[1, 3, 2],
[1, 1, 4],
[2, 2, 1]]) == 14, 'Second example'
July 5, 2013