Created
October 17, 2016 21:11
-
-
Save Visgean/5e14d8613f6d516d2e6f4060895fb2c8 to your computer and use it in GitHub Desktop.
Matrix multiplication threesome
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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