inverse matrix by [1..100]>>=pen (not golf)

import List\x0d
import Ratio\x0d
inverse :: [[Rational]] -> [[Rational]]\x0d
inverse mat = sweep ([], zipWith (++) mat unit) where\x0d
  unit = map (take (length mat)) $ iterate (0:) (1:[0,0..])\x0d
  sweep (xss,[]) = xss\x0d
  sweep (xss,yss) = sweep (xss' ++ [ws], filter (any (/= 0)) yss') where\x0d
    Just (x:xs) = find ((/= 0).head) yss\x0d
    ws = map (/ x) xs\x0d
    [xss',yss'] = map (map f) [xss,yss]\x0d
    f (y:ys) = zipWith (\d e -> e - d*y) ws ys\x0d
main = interact $\x0d

Note that non-ascii characters in the above source code will be escaped (such as \x9f).


return to the top page