Matrix multiplication: Difference between revisions

m (→‎{{header|Tailspin}}: syntax update)
Line 2,248:
[-17,-20,25]
</pre>
===With List and without transpose - Shorter===
<lang Haskell>
foldlZipWith<lang Haskell>mult::(a ->Num ba -=> c) -> (d -> c -> d) -> d[[a]] -> [[a]] -> [b[a]] -> d
mult uss vss = map (foldl (zipWith (+)) ts . zipWith (\vs u -> map (u*) vs) vss) $ uss
foldlZipWith _ _ u [] _ = u
where ts = map (const 0).concat $ take 1 vss
foldlZipWith _ _ u _ [] = u
foldlZipWith f g u (x:xs) (y:ys) = foldlZipWith f g (g u (f x y)) xs ys
 
foldl1ZipWith::(a -> b -> c) -> (c -> c -> c) -> [a] -> [b] -> c
foldl1ZipWith _ _ [] _ = error "First list is empty"
foldl1ZipWith _ _ _ [] = error "Second list is empty"
foldl1ZipWith f g (x:xs) (y:ys) = foldlZipWith f g (f x y) xs ys
 
multAdd::(a -> b -> c) -> (c -> c -> c) -> [[a]] -> [[b]] -> [[c]]
multAdd f g xs ys = map (\us -> foldl1ZipWith (\u vs -> map (f u) vs) (zipWith g) us ys) xs
 
mult:: Num a => [[a]] -> [[a]] -> [[a]]
mult xs ys = multAdd (*) (+) xs ys
 
test a b = do
Line 2,273 ⟶ 2,261:
putStrLn "c = a * b = mult a b ="
mapM_ print c
 
main = test [[1, 2],[3, 4]] [[-3, -8, 3],[-2, 1, 4]]</lang>
</lang>
{{out}}
<pre>
678

edits