aboutsummaryrefslogtreecommitdiff
path: root/main.hs
blob: 951fedd9cebead46fc0a9ebccd239061675df87e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
--innerProduct :: [Int] -> [Int] -> Int
dotProduct [] [] = 0
dotProduct (x:xs) (y:ys) = x * y + dotProduct xs ys

--oneRow row [[], []] = []
oneRow row mat@(x:xs) = if null x
                  then []
                  else dotProduct row (map head mat) : oneRow row (map tail mat)

almostMatrixMult mat1 mat2 = if null mat1
                        then [[]]
                        else oneRow (head mat1) mat2 : almostMatrixMult (tail mat1) mat2

matrixMult mat1 mat2 = [a | a <- almostMatrixMult mat1 mat2, not $ null a]


dropNth xs n = map fst $ filter ((n/=) . snd) $ zip xs [1..]

minor mat i = dropNth (map tail mat) i

cofac mat i = ((-1) ^ (i+1)) * det (minor mat i)

expFac mat i = head (mat!!(i-1)) * cofac mat i

det [[a, b], [c, d]] = a * d - b * c
det mat = helper mat 1 where
            helper mat i = if i >= length mat
                                    then expFac mat i
                                    else expFac mat i + helper mat (i+1)