Repa提供了 fromListUnboxed
函数,可以使用值列表创建1维数组。但是如果我有一组相等长度的1维未打包数组列表,如何创建一个二维数组呢?
reshape
函数:reshape :: (Shape sh1,Shape sh2,Source r1 e) => sh2 -> Array r1 sh1 e -> Array D sh2 e
。这是编译时操作(没有运行时开销)。我也遇到了这个问题。我通过将数组列表转换为未装箱的向量,连接它们,然后再转换回repa数组来解决了这个问题。非常笨拙,但这是我能想到的方法。
import Data.Array.Repa as R
import Data.Vector.Unboxed as V
import Prelude as P
arrs = P.replicate 5 $ fromListUnboxed (ix1 10) [0..9 :: Int]
main = print concatenatedArrs
where vectors = P.map toUnboxed arrs
concatenatedVectors = V.concat vectors
concatenatedArrs = fromUnboxed (R.ix2 5 10) concatenatedVectors