Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for Determinant by Bibiche
def minor(matr, i):
return [[matr[k][j] for j in range(1, len(matr))] for k in range(len(matr)) if k != i]
def checkio(matr):
'determinant value'
N = len(matr)
if N == 1:
return matr[0][0]
elif N == 2:
return matr[0][0] * matr[1][1] - matr[1][0] * matr[0][1]
else:
return sum((-1)**i * matr[i][0] * checkio(minor(matr, i)) for i in range(N))
if __name__ == '__main__':
assert checkio([[4,3], [6,3]]) == -6, 'First'
assert checkio([[1, 3, 2],
[1, 1, 4],
[2, 2, 1]]) == 14, 'Second'
May 24, 2013
Comments: