determinant :: (Num a, Fractional a) => [[a]] -> a
determinant [[x]] = x
determinant mat =
sum [(-1)^i*x*(determinant (getRest i mat)) | (i, x) <- zip [0..] (head mat)]
So, in this code, the base case is a 1x1 matrix. The getRest
function simply returns the matrix without the head row (topmost) and without the \(i\)th column.The code and tests are available on my github.
No comments:
Post a Comment