Skip to content

Instantly share code, notes, and snippets.

@Visgean
Created October 17, 2016 21:11
Show Gist options
  • Save Visgean/5e14d8613f6d516d2e6f4060895fb2c8 to your computer and use it in GitHub Desktop.
Save Visgean/5e14d8613f6d516d2e6f4060895fb2c8 to your computer and use it in GitHub Desktop.
Matrix multiplication threesome
dot :: [Int] -> [Int] -> Int
dot r1 r2 = sum (zipWith (*) r1 r2)
n_chunks :: Int -> [Int] -> Matrix
n_chunks _ [] = []
n_chunks n xs = take n xs: n_chunks n (drop n xs)
timesM :: Matrix -> Matrix -> Matrix
timesM a b | not (valid a) || not (valid b) = error "Po hube dostanes!"
| otherwise = n_chunks (length a) [ra `dot` cb | ra <- a, cb <- (transpose b) ]
-- Rosetta code:
mmult :: Num a => [[a]] -> [[a]] -> [[a]]
mmult a b = [
[
sum (zipWith (*) ar bc) | bc <- (transpose b)
]
| ar <- a
]
-- Curry
timesMap :: Matrix -> Matrix -> Matrix
timesMap _ [] = []
timesMap [] _ = []
timesMap a b = [map (dot ra) tb | ra <- a]
where tb = transpose b
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment