[Haskell-cafe] "sum" in hmatrix and blas?

2008-06-07 Thread Xiao-Yong Jin
Hi, probably I am just being dumb, but what is the most efficient way to do a sum of every elements in a Vector of either hmatrix or blas? I know there is sum of absolute values from BLAS. So what about I want a plain "sum"? I can only think of the following two ways. 1. Using Data.List.foldl'

Re: [Haskell-cafe] "sum" in hmatrix and blas?

2008-06-08 Thread Tomas Andersson
Den Sunday 08 June 2008 00.45.01 skrev Xiao-Yong Jin: > Any delightful idea to convert my mind from a C shaped one > to a Haskell shaped one? > 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

Re: [Haskell-cafe] "sum" in hmatrix and blas?

2008-06-08 Thread Xiao-Yong Jin
Tomas Andersson <[EMAIL PROTECTED]> 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 produ

Re: [Haskell-cafe] "sum" in hmatrix and blas?

2008-06-08 Thread Don Stewart
xj2106: > Tomas Andersson <[EMAIL PROTECTED]> 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 programme

Re: [Haskell-cafe] "sum" in hmatrix and blas?

2008-06-08 Thread Alberto Ruiz
Xiao-Yong Jin wrote: Tomas Andersson <[EMAIL PROTECTED]> 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

Re: [Haskell-cafe] "sum" in hmatrix and blas?

2008-06-08 Thread Xiao-Yong Jin
Alberto Ruiz <[EMAIL PROTECTED]> writes: > My experience is that Haskell allocation time is very fast and usually > negligible in most non trivial matrix computations. > > A good thing about > > sum v = constant 1 (dim v) <.> v > > is that a constant vector is efficiently created internally (not f

Re: [Haskell-cafe] "sum" in hmatrix and blas?

2008-06-08 Thread Don Stewart
Below are some notes on this for Simon PJ and Alberto. In general, GHC is doing very well here, with only one small wibble preventing the recursive version running as fast as the C version. xj2106: > Alberto Ruiz <[EMAIL PROTECTED]> writes: > > > My experience is that Haskell allocation time i

Re: [Haskell-cafe] "sum" in hmatrix and blas?

2008-06-08 Thread Don Stewart
Problem solved. The Haskell loop now wins. > > After reading don's blog, I tried to make a test with both > > methods. The code is very simple, as following > > > > module Main where > > import System > > import Numeric.LinearAlgebra > > > > vsum1 :: Vector Double -> Double > > vsum1 v = const

Re: [Haskell-cafe] "sum" in hmatrix and blas?

2008-06-09 Thread Alberto Ruiz
Patch applied! I will also make the recommended changes in the Matrix type and prepare some benchmarks for this kind of Haskell loops. In fact, such excellent performance will also be very useful in image processing algorithms which must read and write a large number of C array elements. I n