Matrix multiplication: Difference between revisions
→With List and without transpose: Applied hlint and hindent
(→With List and without transpose - shorter: Applied hlint and hindent.) |
(→With List and without transpose: Applied hlint and hindent) |
||
Line 2,232:
l = [((i,j), sum [x!(i,k) * y!(k,j) | k <- kr]) | i <- ir, j <- jr]</lang>
===With List and without transpose===
<lang Haskell>multiply :: Num a => [[a]] -> [[a]] -> [[a]]
multiply us vs = map (mult [] vs) us
mult xs [] _ = xs
mult xs _ [] = xs
mult [] (zs:zss) (y:ys) = mult (map (
mult xs (zs:zss) (y:ys) = mult (zipWith (\u v -> u + v * y) xs zs) zss ys
main :: IO ()
main = mapM_ print $ multiply [[1, 2], [3, 4]] [[-3, -8, 3], [-2,
{{out}}
<pre>[-7,-6,11]
[-
===With List and without transpose - shorter===
<lang Haskell>mult :: Num a => [[a]] -> [[a]] -> [[a]]
|