arrays - unboxing, (sparse) matrices, and haskell vector library -


i manipulate matrices (full or sparse) efficiently haskell's vector library.

here matrix type

import qualified data.vector.unboxed u import qualified data.vector v  data link = full (v.vector (u.vector a))     | sparse (v.vector (u.vector (int,a)))  type vector = u.vector 

as can see, matrix vector of unboxed vectors. now, dot product between vector , matrix. simple combining sum, zip , map.

but if that, because i'm mapping through rows of matrix, result boxed vector, though unboxed.

propagates output (field src) (full weights) = v.map (sum out) weights     out     = u.map output src           sum s w = u.sum $ zipwithfull (*) w s  propagates output (field src) (sparse weights) = v.map (sum out) weights     out     = u.map output src           sum s w = u.sum $ zipwithsparse (*) w s  zipwithfull = u.zipwith  zipwithsparse f x y = u.map f' x     f' (i,v) = f v (y u.! i) 

how can unboxed vector result efficiently ?

i don't know field type is, don't quite understand second snippet.

but if represent matrix boxed vector, intermediate results boxed vectors. if want have unboxed result, need convert types explicitly u.fromlist . v.tolist. example dense matrix type (i omitted sparse case brevity):

import qualified data.vector.unboxed u import qualified data.vector v  -- assuming row-major order data matrix = full (v.vector (u.vector a))  type vector = u.vector  -- matrix vector dot product dot :: (u.unbox a, num a) => (matrix a) -> (vector a) -> (vector a) (full rows) `dot` x =   let mx = v.map (vdot x) rows   in u.fromlist . v.tolist $ mx  -- unboxing, o(n)  -- vector vector dot product vdot :: (u.unbox a, num a) => vector -> vector -> vdot x y = u.sum $ u.zipwith (*) x y  instance (show a, u.unbox a) => show (matrix a)   show (full rows) = show $ v.tolist $ v.map u.tolist rows  showv = show . u.tolist  main =   let m = full $ v.fromlist $ map u.fromlist ([[1,2],[3,4]] :: [[int]])       x = u.fromlist ([5,6] :: [int])       mx = m `dot` x   in putstrln $ (show m) ++ " × " ++ (showv x) ++ " = " ++ (showv mx) 

output:

 [[1,2],[3,4]] × [5,6] = [17,39] 

i not sure performance of approach. better store whole matrix single unboxed vector , access elements index according storage model. way don't need boxed vectors.

take @ new repa library , index operation.


Comments

Popular posts from this blog

javascript - Enclosure Memory Copies -

php - Replacing tags in braces, even nested tags, with regex -