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

Haskell / repa - is there a way to pretty-print matrices?

在 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]) 

这对我有用。也许值得添加到 repa 或 repa-extras 包中?

{-# 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