Skip to content
Snippets Groups Projects
Commit 598efebf authored by Noric Couderc's avatar Noric Couderc
Browse files

Implemented new version of multiply

parent 786461a5
Branches
No related tags found
No related merge requests found
import Data.List
import Data.Ord
import Data.List
import qualified Data.Map
type CoordMat = [(Int, Int, Float)]
type IndexedRowMat = [(Int, [Float])]
-- UTILS
groupBy :: (Ord b) => (a -> b) -> [a] -> Data.Map.Map b [a]
groupBy f l = Data.Map.fromListWith (++) [(f v, [v]) | v <- l]
-- SPARSE MATRIX MULTIPLICATION
multiply :: CoordMat -> CoordMat -> CoordMat
multiply left right =
let join = [(k, i, v, j, w) | (k, i, v) <- left, (j, k1, w) <- right, k == k1]
in [(i, j, w * v) | (k, i, v, j, w) <- join]
let join = [((i, j), v * w) | (k, i, v) <- right, (j, k1, w) <- left, k == k1]
getValue (_, v) = v
in
map (\((i, j), v) -> (i, j, v)) $
Data.Map.toList $
Data.Map.map (sum) $
Data.Map.map (map snd) $
Main.groupBy (\(p, v) -> p) join
-- DENSE MATRIX MULTIPLICATION
......@@ -22,7 +35,7 @@ toRowMatrix =
-- [(Int, Int, Float)] -> [Float]
aggregateRow = map (\(i, j, v) -> v)
isZeroRow = all (== 0)
in zip [1..] . filter (not . isZeroRow) . map aggregateRow . groupBy equivalent . sortBy (comparing rowNum)
in zip [1..] . filter (not . isZeroRow) . map aggregateRow . Data.List.groupBy equivalent . sortBy (comparing rowNum)
transpose :: CoordMat -> CoordMat
transpose = map (\(i, j, v) -> (j, i, v))
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment