8
我想用haskell的矢量庫有效地處理矩陣(完全或稀疏)。拆箱,(稀疏)矩陣和haskell矢量庫
這裏是一個矩陣型
import qualified Data.Vector.Unboxed as U
import qualified Data.Vector as V
data Link a = Full (V.Vector (U.Vector a))
| Sparse (V.Vector (U.Vector (Int,a)))
type Vector a = U.Vector a
正如你可以看到,基體是裝箱向量的向量。現在,我想在矢量和矩陣之間做點積。通過合併金額,郵編和地圖來做相當簡單。
但如果我這樣做,因爲我通過矩陣的行映射,結果是盒裝的載體,即使它可能被拆箱。
propagateS output (Field src) (Full weights) = V.map (sum out) weights
where out = U.map output src
sum s w = U.sum $ zipWithFull (*) w s
propagateS output (Field src) (Sparse weights) = V.map (sum out) weights
where out = U.map output src
sum s w = U.sum $ zipWithSparse (*) w s
zipWithFull = U.zipWith
zipWithSparse f x y = U.map f' x
where f' (i,v) = f v (y U.! i)
如何有效地得到一個取消裝箱的向量?
Field的定義是什麼? – 2010-04-30 19:49:20