Haskell / repa - 有没有办法漂亮地打印矩阵?

时间:2016-01-11 04:42:46

标签: haskell matrix repa

在haskell中使用repa,是否有一种打印矩阵的方法,因此它们的格式很好,矩阵行位于不同的行上(大多数数值计算环境的默认设置,如R或matlab)?

我可以写一个,但它似乎已经存在。

这种类型的输出难以与之交互:

λ> foo
AUnboxed ((Z :. 3) :. 5) (fromList [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15]) 

1 个答案:

答案 0 :(得分:2)

这对我有用。也许值得添加到维修或修理额外包裹?

{-# LANGUAGE FlexibleInstances #-}

module PrettyPrint where

import Data.Array.Repa
import Data.Array.Repa.Algorithms.Matrix

import Text.PrettyPrint
import Text.PrettyPrint.HughesPJClass

instance (Source t a, Pretty a) => Pretty (Array t DIM1 a) where
 pPrint a = brackets $ hcat $ punctuate (comma <> space) elems
    where
     elems = [ pPrint (a!j) | i <- [0..n-1], let j = Z :. i ]
     Z :. n = extent a

instance (Source t a, Pretty a) => Pretty (Array t DIM2 a) where
 pPrint a = vcat elems
   where
     elems = [ pPrint (slice a j) | i <- [0..n-1], let j = Any :. i :. All]
     Z :. n :. _m = extent a