我想使用 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
如您所见,矩阵是未装箱向量的向量。现在,我想做一个向量和矩阵之间的点积。通过组合 sum、zip 和 map 来实现相当简单。
但是如果我这样做,因为我正在映射矩阵的行,结果是一个装箱的向量,即使它可以被取消装箱。
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)
如何有效地获得未装箱的矢量?