Skip to content

Instantly share code, notes, and snippets.

@nnabeyang
Created January 20, 2014 00:26

Revisions

  1. nnabeyang revised this gist Jan 30, 2014. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion matrix.hs
    Original file line number Diff line number Diff line change
    @@ -6,7 +6,7 @@ cofactor i j m = [dropAt j v| (v, k) <- zip m [0..], i /= k]
    det :: Matrix -> Int
    type Matrix = [[Int]]
    dropAt :: Int -> [a] -> [a]
    dropAt i xs = [x| (x, j) <- zip xs [0..], i /= j]
    dropAt i xs = as ++ bs where (as, _ :bs) = splitAt i xs
    cofactor :: Int -> Int -> Matrix -> Matrix
    cofactor i j m = [dropAt j v| (v, k) <- zip m [0..], i /= k]
    det :: Matrix -> Int
  2. nnabeyang revised this gist Jan 21, 2014. No changes.
  3. nnabeyang revised this gist Jan 21, 2014. 1 changed file with 6 additions and 0 deletions.
    6 changes: 6 additions & 0 deletions matrix.hs
    Original file line number Diff line number Diff line change
    @@ -14,3 +14,9 @@ det m = case m of
    [[e]] -> e
    (xs:xss) -> sum [(-1) ^ i * e * (det . cofactor 0 i) m |
    (e, i) <- zip xs [0..]]


    ex :: Int
    ex = det [[1, 2],
    [3, 4]]
    -- 1*4 - 2 * 3 = 4 - 6 = -2
  4. nnabeyang revised this gist Jan 21, 2014. 1 changed file with 10 additions and 7 deletions.
    17 changes: 10 additions & 7 deletions matrix.hs
    Original file line number Diff line number Diff line change
    @@ -4,10 +4,13 @@ dropAt i xs = [x| (x, j) <- zip xs [0..], i /= j]
    cofactor :: Int -> Int -> Matrix -> Matrix
    cofactor i j m = [dropAt j v| (v, k) <- zip m [0..], i /= k]
    det :: Matrix -> Int
    det m
    | n == 1 = m !! 0 !! 0
    | n > 1 = sum [(-1) ^ i * e * (det . cofactor 0 i) m |
    (e, i) <- zip v [0..]]
    where
    v = m !! 0
    n = length m
    type Matrix = [[Int]]
    dropAt :: Int -> [a] -> [a]
    dropAt i xs = [x| (x, j) <- zip xs [0..], i /= j]
    cofactor :: Int -> Int -> Matrix -> Matrix
    cofactor i j m = [dropAt j v| (v, k) <- zip m [0..], i /= k]
    det :: Matrix -> Int
    det m = case m of
    [[e]] -> e
    (xs:xss) -> sum [(-1) ^ i * e * (det . cofactor 0 i) m |
    (e, i) <- zip xs [0..]]
  5. nnabeyang created this gist Jan 20, 2014.
    13 changes: 13 additions & 0 deletions matrix.hs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,13 @@
    type Matrix = [[Int]]
    dropAt :: Int -> [a] -> [a]
    dropAt i xs = [x| (x, j) <- zip xs [0..], i /= j]
    cofactor :: Int -> Int -> Matrix -> Matrix
    cofactor i j m = [dropAt j v| (v, k) <- zip m [0..], i /= k]
    det :: Matrix -> Int
    det m
    | n == 1 = m !! 0 !! 0
    | n > 1 = sum [(-1) ^ i * e * (det . cofactor 0 i) m |
    (e, i) <- zip v [0..]]
    where
    v = m !! 0
    n = length m