0
0

More than 5 years have passed since last update.

余因子展開

Last updated at Posted at 2014-01-20

余因子展開による行列式の計算をする関数det。

matrix.hs
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
type Matrix = [[Int]]
dropAt :: Int -> [a] -> [a]
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
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
0
0
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0