Sergey Astanin avatar Sergey Astanin committed 51d55c5

Added support of the array format. More handy representation types.

Comments (0)

Files changed (1)

 import Control.Applicative ((<$>), (<*>))
 import Control.Monad (join)
 
--- | A Matrix Market matrix.
+-- | Matrix Market format representation.
 data Matrix = MM
   { mm'data :: MatrixData
   , mm'symmetry :: Symmetry
   } deriving (Show)
 
 -- | Matrix' data block.
-data MatrixData = CoordinateM CM | ArrayM AM
+data MatrixData = CoordinateM { coords'm :: CM }
+                | ArrayM { array'm :: AM }
   deriving (Show)
 
 -- | Coordinate format (sparse matrix).
 data Symmetry = General | Symmetric | SkewSymmetric | Hermitian
   deriving (Show, Eq)
 
---- | Parsing errors.
+-- | Parsing errors.
 data MMError
   = NotAMatrixMarketFormat
   | InvalidHeader String
   deriving (Show, Eq)
 
 -- | Values allowed in the Matrix Market files.
-data MValue = I Int | R Double | C (Complex Double) | NotAValue
+data MValue = I { unI :: Int}
+            | R { unR :: Double}
+            | C { unC :: (Complex Double)}
+            | NotAValue
   deriving (Show, Eq)
 
--- | Helper to unwrap MValue.
-asInt :: MValue -> Maybe Int
-asInt (I i) = Just i
-asInt _ = Nothing
-
--- | Helper to unwrap MValue.
-asDouble :: MValue -> Maybe Double
-asDouble (R d) = Just d
-asDouble _ = Nothing
-
--- | Helper to unwrap MValue.
-asComplex :: MValue -> Maybe (Complex Double)
-asComplex (C c) = Just c
-asComplex _ = Nothing
-
 -- | Parse Matrix Market format.
 readMM :: String -> Either MMError Matrix
 readMM mtx
                   _             -> NoParse
         _ -> Left $ InvalidHeader hdr
   --
-  parsers = [ ("coordinate", readCoords)]
+  parsers = [ ("coordinate", readCoords)
+            , ("array",      readArray)]
   readers = [ ("real",    (1, readReal))
             , ("integer", (1, readInt))
             , ("complex", (2, readComplex))
     readVal _ _ = Nothing
 readCoords _ _ _ = Nothing
 
+-- | Read matrix in array format.
+readArray :: FormatReader
+readArray n reader (n1:n2:toks) =
+    let nrows = mbInt n1
+        ncols = mbInt n2
+        vals = sequence $ map (reader . (:[])) toks
+        am = AM <$> nrows <*> ncols <*> vals ::Maybe AM
+    in  ArrayM <$> am
+readArray _ _ _ = Nothing
+
 startsWith :: (Eq a) => [a] -> [a] -> Bool
 s `startsWith` prefix = all id $ zipWith (==) s prefix
 
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.