Ich möchte Matrizen (voll oder spärlich) effizient mit Haskells Vektorbibliothek manipulieren.Unboxing, (spärlich) Matrizen und Haskell-Vektorbibliothek
Hier ist eine Matrix-Typ
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
Wie Sie sehen können, wobei die Matrix ein Vektor von unboxed Vektoren ist. Nun würde ich gerne ein Punktprodukt zwischen einem Vektor und einer Matrix machen. Es ist ziemlich einfach zu tun, indem man eine Summe, einen Reißverschluss und eine Karte kombiniert.
Aber wenn ich das tue, weil ich durch die Reihen der Matrix abbilde, ist das Ergebnis ein eingerahmter Vektor, obwohl es ungepackt sein könnte.
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)
Wie kann ich einen unboxed Vektor als Ergebnis effizient erhalten?
Was ist die Definition von Field? –