[Haskell-cafe] vector recycling

Ben midfield at gmail.com
Fri Apr 16 23:32:23 EDT 2010


hello --

this is mostly a question for roman, or don, i guess.  suppose i have
a list of similarly-sized vectors, and i want to add them up (possibly
with coefficients), to yield a result vector.  something like

module Main where

import qualified Data.Vector.Generic as V
import qualified Data.Vector.Unboxed as UV

type Vec = UV.Vector Double

axpy :: Double -> Vec -> Vec -> Vec
axpy a x y = V.zipWith (+) (V.map (* a) x) y

sumVecs :: [(Double, Vec)] -> Vec
sumVecs axs =
    let (a, x) = head axs
    in foldl adder (V.map (* a) x) (tail axs)
        where adder :: Vec -> (Double, Vec) -> Vec
              adder v1 (a, x) = axpy a x v1

how to i write this in a way which ensures recycling / fusion, e.g.
in-place updates?

best regards, ben


More information about the Haskell-Cafe mailing list