Cramer's rule: Difference between revisions

Line 1,116:
 
===Version 3===
<lang Haskell>import Data.List
import Data.List
 
determinant::(Fractional a, Ord a) => [[a]] -> a
Line 1,145 ⟶ 1,144:
matI n = [ [fromIntegral.fromEnum $ i == j | i <- [1..n]] | j <- [1..n]]
 
foldlZipWith::(a -> b -> c) -> (d -> c -> d) -> d -> [a] -> [b] -> d
foldlZipWith _ _ u [] _ = u
foldlZipWith _ _ u _ [] = u
foldlZipWith f g u (x:xs) (y:ys) = foldlZipWith f g (g u (f x y)) xs ys
foldl1ZipWith::(a -> b -> c) -> (c -> c -> c) -> [a] -> [b] -> c
foldl1ZipWith _ _ [] _ = error "First list is empty"
foldl1ZipWith _ _ _ [] = error "Second list is empty"
foldl1ZipWith f g (x:xs) (y:ys) = foldlZipWith f g (f x y) xs ys
multAdd::(a -> b -> c) -> (c -> c -> c) -> [[a]] -> [[b]] -> [[c]]
multAdd f g xs ys = map (\us -> foldl1ZipWith (\u vs -> map (f u) vs) (zipWith g) us ys) xs
mult:: Num a => [[a]] -> [[a]] -> [[a]]
mult uss vss = map (foldl (zipWith (+)) ts . zipWith (\vs u -> map (u*) vs) vss) uss
mult xs ys = multAdd (*) (+) xs ys
where ts = map (const 0).concat $ take 1 vss
 
task::[[Rational]] -> [[Rational]] -> IO()
678

edits