Skip to content
Browse files

Add generic function for showing matrices

  • Loading branch information...
1 parent 8590a11 commit 94cc0f87aa67e257654aaf92d5b1321f115e0304 @Shimuuar committed Aug 28, 2012
Showing with 14 additions and 0 deletions.
  1. +14 −0 Data/Matrix/Generic.hs
View
14 Data/Matrix/Generic.hs
@@ -23,13 +23,16 @@ module Data.Matrix.Generic (
-- * Converions to/from mutable
, unsafeFreeze
, unsafeThaw
+ -- * Conversion to string
+ , showMatrixWith
-- * Newtype wrappers
, Transposed(..)
, Conjugated(..)
) where
import Control.Monad (liftM)
import Control.Monad.Primitive
+import Data.List (intercalate)
import Data.Complex (Complex,conjugate)
import qualified Data.Matrix.Generic.Mutable as M
import Data.Vector.Generic (Mutable)
@@ -107,6 +110,17 @@ unsafeThaw :: (PrimMonad m, IsMatrix mat a) => mat a -> m (Mutable mat (PrimStat
unsafeThaw = basicUnsafeThaw
+-- | Generic function for printing matrix. Mostly useful for debugging
+-- purposes.
+showMatrixWith :: IsMatrix m a => (a -> String) -> m a -> String
+showMatrixWith f m
+ = unlines
+ $ (show (rows m) ++ " >< " ++ show (cols m))
+ : [ intercalate "\t" [f $ unsafeIndex m (i,j) | j <- [0 .. cols m - 1]]
+ | i <- [0 .. rows m - 1]
+ ]
+
+
----------------------------------------------------------------
-- Newtype wrappers

0 comments on commit 94cc0f8

Please sign in to comment.
Something went wrong with that request. Please try again.