deepSeqArray of a single precision array

让人想犯罪 __ 提交于 2019-12-10 18:53:22

问题


I have a vector of Float elements created by the getVectorFloat function. In order to make some measurements, I need to use deepSeqArray. However, I don't manage to do it.

Here is my example:

import Data.Array.Repa as R
import Data.Array.Repa.Algorithms.Randomish

len :: Int
len = 3000000

main = do
    ws <- getVectorFloat len
    ws `deepSeqArray` return()

getVectorFloat :: Int -> Array DIM1 Float
getVectorFloat len = R.map (toFloat) (getVectorDouble len)

toFloat :: Double -> Float
toFloat a = realToFrac a

getVectorDouble :: Int -> Array DIM1 Double
getVectorDouble len = randomishDoubleArray (Z :. len) (-100) 100 1

And the error I get:

haskell.hs:9:9:
    Couldn't match expected type `Array sh0 a0'
                with actual type `Float'
    In the first argument of `deepSeqArray', namely `ws'
    In the expression: ws `deepSeqArray` return ()
    In the expression:
      do { ws <- getVectorFloat len;
             ws `deepSeqArray` return () }

I don't understand why Array sh0 a0 can't be matched to Array Dim1 Float.

Thank you for your help


回答1:


The problemo comes, from the do which doesn't do what you think. When you do ws <- getVectorFloat len

ws is not set with the Array, but called for each value of it. ie the do notation for an array , is similar to a map. Consider the following example

prelude > do x <- [1,2,3]; return (x*10)
[10, 20, 30]

or more fun ;-)

Prelude> do <-[1,2,3];y<-[10,20,30]; return(x,y)                                                                                                                                         
[(1,10),(1,20),(1,30),(2,10),(2,20),(2,30),(3,10),(3,20),(3,30)]   

So I'm not sure your usage of do is appropriate.



来源:https://stackoverflow.com/questions/5619837/deepseqarray-of-a-single-precision-array

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!