[Haskell-cafe] trying to go from FFT type to Vector.Storable

Leza Morais Lutonda leza.ml at fecrd.cujae.edu.cu
Thu May 14 06:31:55 UTC 2015


On 05/14/2015 12:51 AM, briand at aracnet.com wrote:
> Well what I was_really_  trying to do was to get to a Vector of (Complex Double).
>
> Interestingly, the following, where ys is type Vector.Storable (Complex Double),
>
>
>    ys_array = listArray (0, V.length ys -1) (V.toList ys)
>    y = V.fromList $ map (\z -> realPart z :+ imagPart z) (elems (dft ys_array))
>
> worked just fine, but it's not obvious to me that it should have.  Seems like the realPart/imagPart should have failed to work because the argument is not Complex Double it's Complex FFTWReal, so I should have ended up with (Complex FFTWReal) when I really wanted (Complex Double) and fail.
>
> BTW, it seems to me that it would be awfully nice if FFT delivered results as Vectors instead of the Array thingy.  It looked to me like V.Storable is something which is supposed to ease the marshalling to and from the C world.
>
> Would it be a whole lot of work to change the interface to Vector.Storable ?
>
> Among other advantages, it makes interfacing to HMatrix easier.
Hi Brian,

I think the problem is in the way of seeing the types and type classes.
First, the type of `ys` is not `Vector.Storable (Complex Double)`, it is 
Vector (Complex Double).
`FFTWReal` is not a type, it is a type class. It is used as a constraint 
for types, for example, the type of `dft`:

dft :: (Ix i, Shapable i, FFTWReal r) => CArray i (Complex r) -> CArray 
i (Complex r)
this means that the type `r` must be instance o FFTWReal type class.

So, the type of `dft ys_array` is `CArray i (Complex Double)`, since the 
type of `ys_array` is `CArray i (Complex Double)`, that means that the 
`r` in the type of `dft` is `Double`.
So, the `map` is unnecessary. You can try:

```
vectorToArray v = listArray (0, V.length v - 1) (V.toList v)
arrayToVector = V.fromList . elems

fft :: FFTWReal r => V.Vector (Complex r) -> V.Vector (Complex r)
fft = arrayToVector . dft . vectorToArray
```

Hope it can help!

-- 
Leza Morais Lutonda, Lemol-C
Electronics & Telecommunications Engineer
Software Development and Architecture Enthusiast
http://lemol.github.io



50 Aniversario de la Cujae. Inaugurada por Fidel el 2 de diciembre de 1964  http://cujae.edu.cu




More information about the Haskell-Cafe mailing list