module Gauss ( gaussianReduce ) where type Row = [Double] type Column = [Double] type RMatrix = [Row] type CMatrix = [Column] stringToDouble = read :: String -> Double stringsToDoubles :: [String] -> [Double] stringsToDoubles = map stringToDouble gaussianReduce :: RMatrix -> RMatrix gaussianReduce matrix = foldl reducerow matrix [0..length matrix-1] where reducerow :: RMatrix -> Int -> RMatrix reducerow m r = let row = m !! r p = row !! r row' = map (/ p) row reduceonerow nrow = let nr = nrow !! r in zipWith (\a b -> nr*a - b) row' nrow nextrows = map reduceonerow (drop (r+1) m) in take r m ++ [row'] ++ nextrows -- gaussianSolve :: RMatrix -> [Double] -- Your code goes here main :: IO () main = do raw <- getContents let rows = lines raw let rows' = map words rows let matrix = map stringsToDoubles rows' print $ gaussianReduce matrix