在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

关于Haskell/repa - 有没有办法漂亮地打印矩阵?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/34714306/

10-12 20:41