[Haskell-cafe] zip, map and zipWith for arrays
axel.gerstenberger at gmx.de
Sun Sep 9 06:05:54 EDT 2007
thanks again for all the responses on the "imperative loop" question.
Here is another questions:
I am used to work with map, zip and zipWith, when working with lists,
however, I could not find such functions for Arrays.
For example, in my Finite Element prototype, I have a function
> type Dof = Double
> type TimeInc = Double
> predictU :: Double -> TimeInc -> Dof -> Dof -> Dof -> Dof
> predictU beta dt u u_t u_tt = u + dt*u_t + ((dt**2.0)/2.0)*(1.0 -
2.0*beta ) * u_tt
Given 3 equal sized lists un, vn and an of double values [Dof], I apply
> u_est = zipWith3 (predictU beta dt) un vn an
I like it's conciseness, but is that efficient?
The lists can be very long and correspond to a plain C++ vectors in
corresponding C++ codes.
my own "map" for a vector I defined based on the Array documention
> mapVec f vec = vec//[ (i, f(vec!i)) |i<-[a..b]]
> (a,b) = bounds vec
I guess, I could implement zip and zipWith in a similar manner, but
using list comprehension seems weird and inefficient here. I wonder if
these standard functions are already defined somewhere.
I only found "amap" for IArrays
but no zip, zipWith or even zipWith3. The whole point of using (unboxed)
Arrays instead of lists is memory performce and speead of random access.
Any hints on how to do zipWith on arrays is highly appreciated.
Henning Thielemann schrieb:
> On Thu, 6 Sep 2007, Axel Gerstenberger wrote:
>> Thanks to all of you. The suggestions work like a charm. Very nice.
>> I still need to digest the advices, but have already one further
>> question: How would I compute the new value based on the 2 (or even
>> more) last values instead of only the last one?
>> [ 2, 3 , f 3 2, f((f 3 2) 3), f ( f((f 3 2) 3) f 3 2)), ...]
>> (background: I am doing explicit time stepping for some physical
>> problem, where higher order time integration schemes are interesting.
>> You advance in time by extrapolating based on the old time step values.)
> You might be interested in some ideas on how to solve differential
> equations numerically in an elegant way:
More information about the Haskell-Cafe