[Haskell-cafe] "sum" in hmatrix and blas?
Xiao-Yong Jin
xj2106 at columbia.edu
Sun Jun 8 10:15:01 EDT 2008
Tomas Andersson <toman144 at student.liu.se> writes:
> You can never go wrong with a good old fashioned hand written tail recursion
> when you're in doubt, they are pretty much the closest thing to for-loops
> there is in haskell and should be easy to grok for Imperative programmers and
> usually produce really fast code.
>
> sum vect = let d = dim vect
> in sum' (d - 1) 0
> where sum' 0 s = s + (vect @> 0)
> sum' index s = sum' (index - 1) (s + (vect @> index))
>
Do I need the strict version of sum'? Put something like
sum' a b | a `seq` b `seq` False = undefined
Or ghc will optimize it automatically? I always don't know
when such optimization is useful.
>
>
> I don't know the hmatrix api very well, but if there is a function for
> computing the inner product between two vectors you could always do something
> like the following meta code:
>
> sum v = innerProduct v <1,1,1,1,1,1>
I just doubt the efficiency here. If v is a very large
vector, I guess the allocation time of that intermediate
vector [1,1..] is not small. But it is just my guess.
Xiao-Yong
--
c/* __o/*
<\ * (__
*/\ <
More information about the Haskell-Cafe
mailing list