packages feed

matrix-market-attoparsec 0.1.0.8 → 0.1.0.9

raw patch · 5 files changed

+40/−33 lines, 5 filesPVP ok

version bump matches the API change (PVP)

API changes (from Hackage documentation)

Files

CHANGELOG.markdown view
@@ -1,3 +1,3 @@-0.2.0.1+0.1.0.1  Added MatrixMarket module from `accelerate-examples` SMVM
CONTRIBUTORS.md view
@@ -1,3 +1,5 @@ Trevor McDonell (tmcdonell) and the `accelerate` contributors : Data.Matrix.MatrixMarket  Peter Simons (peti)++Gregory Schwartz (GregorySchwartz)
matrix-market-attoparsec.cabal view
@@ -1,5 +1,5 @@ name:                matrix-market-attoparsec-version:             0.1.0.8+version:             0.1.0.9 synopsis:            Parsing and serialization functions for the NIST Matrix Market format description:         Parsing and serialization functions for the NIST Matrix Market format. homepage:            https://github.com/ocramz/matrix-market-attoparsec@@ -17,7 +17,7 @@ data-files:          fidapm05.mtx                      fidapm05_rhs1.mtx                      cabal-version:       >=1.10-tested-with:         GHC == 8.0.1+tested-with:         GHC == 8.0.1, GHC == 8.6.5                        
src/Data/Matrix/MatrixMarket.hs view
@@ -8,14 +8,12 @@ -- Stability   :  experimental -- Portability :  portable ----- Attoparsec parser and serializer for the NIST MatrixMarket format. The parser logic originally appeared in `accelerate-examples` and it is reused here (courtesy of T.McDonell and the `accelerate` developers) with some amendments.--- --- In this version:+-- @attoparsec@-based parser and serializer for the NIST MatrixMarket format [1]. The parser logic originally appeared in @accelerate-examples@ and it is reused here (courtesy of T.McDonell and the @accelerate@ developers) with some amendments. ----- *) Numbers are represented with Scientific notation instead of floating point ----- *) Parsing rules are a bit relaxed to accommodate various whitespace corner cases+-- References : --+-- 1. https://math.nist.gov/MatrixMarket/ ----------------------------------------------------------------------------- module Data.Matrix.MatrixMarket        (-- * Load
src/Data/Matrix/MatrixMarket/Internal.hs view
@@ -10,15 +10,15 @@ -- Portability :  portable -- -- Attoparsec parser and serializer for the NIST MatrixMarket format. The parser logic originally appeared in `accelerate-examples` and it is reused here (courtesy of T.McDonell and the `accelerate` developers) with some amendments.--- +-- -- In this version: -- * Numbers are represented with Scientific notation instead of floating point -- * Parsing rules are a bit relaxed to accommodate various whitespace corner cases -- ----------------------------------------------------------------------------- module Data.Matrix.MatrixMarket.Internal-       (readMatrix, readArray,-        writeMatrix, writeArray,+       (readMatrix, readMatrix', readArray,+        writeMatrix, writeMatrix', writeArray,         Matrix(..), Array(..),         Format (Coordinate, Array), Structure (General, Symmetric, Hermitian, Skew),         nnz, dim, numDat,@@ -61,8 +61,8 @@ -- data Field  = R | C | I | P     deriving (Eq, Show)-              + -- | Specifies any special structure in the matrix.  For symmetric and hermitian -- matrices, only the lower-triangular part of the matrix is given. For skew -- matrices, only the entries below the diagonal are stored.@@ -147,7 +147,7 @@ line3 f = (,,) <$> integral                <*> integral                <*> f-               <*  endOfLine+               <*  (endOfLine <|> L.endOfInput)  skipSpace' :: Parser String skipSpace' = many' space@@ -164,7 +164,7 @@   if f /= Coordinate     then fail "matrix is not in Coordinate format"     else-    case t of +    case t of      R -> RMatrix (m,n) l s <$> many1 (line3 floating)      C -> CMatrix (m,n) l s <$> many1 (line3 ((:+) <$> floating <*> floating))      I -> IntMatrix     (m,n) l s <$> many1 (line3 integral)@@ -185,8 +185,11 @@  -- | Load a matrix (sparse, i.e. in Coordinate format) from file readMatrix :: FilePath -> IO (Matrix S.Scientific)-readMatrix file = do-  chunks <- L.readFile file+readMatrix file = L.readFile file >>= readMatrix'++-- | Load a matrix (sparse, i.e. in Coordinate format) from a bytestring.+readMatrix' :: L.ByteString -> IO (Matrix S.Scientific)+readMatrix' chunks =   case L.parse matrix chunks of     L.Fail _ _ msg      -> throwM (FileParseError "readMatrix" msg)     L.Done _ mtx        -> return mtx@@ -225,7 +228,7 @@   nl :: L.ByteString-nl = toLBS "\n" +nl = toLBS "\n"  showLines :: (a -> String) -> [a] -> L.ByteString showLines showf d = L.concat (L.pack . withNewline . showf <$> d) where@@ -235,13 +238,17 @@  -- | Serialize a sparse matrix in Coordinate format writeMatrix :: Show b => FilePath -> Matrix b -> IO ()-writeMatrix file mat = -  case mat of (RMatrix d nz s dat) -> -                L.writeFile file (matrixByteString d nz R s dat)-              (CMatrix d nz s dat) -> -                L.writeFile file (matrixByteString d nz C s dat)-              (IntMatrix d nz s dat) -> -                L.writeFile file (matrixByteString d nz I s dat)+writeMatrix file = L.writeFile file . writeMatrix'++-- | Serialize a sparse matrix in Coordinate format as a bytestring+writeMatrix' :: Show b => Matrix b -> L.ByteString+writeMatrix' mat =+  case mat of (RMatrix d nz s dat) ->+                matrixByteString d nz R s dat+              (CMatrix d nz s dat) ->+                matrixByteString d nz C s dat+              (IntMatrix d nz s dat) ->+                matrixByteString d nz I s dat               _ -> error "writeMatrix : PatternMatrix not implemented yet"      where        matrixByteString di nz t s d =@@ -252,11 +259,11 @@                    showLines sf3 d]          where          sf3 (i,j,x) = unwords [show i, show j, show x]-         headerSzMatrix (m,n) numz = B.pack $ unwords [show m, show n, show numz] +         headerSzMatrix (m,n) numz = B.pack $ unwords [show m, show n, show numz]  -- | Serialize a dense matrix in Array format-writeArray :: Show a => FilePath -> Array a -> IO ()  -writeArray file arr = +writeArray :: Show a => FilePath -> Array a -> IO ()+writeArray file arr =   case arr of (RArray d s dat) -> L.writeFile file (arrayByteString d R s dat)               (CArray d s dat) -> L.writeFile file (arrayByteString d C s dat)     where@@ -267,14 +274,14 @@             nl,             showLines show d] where         headerSzArray (m,n) = B.pack $ unwords [show m, show n]-  -        ++ -- | helpers  -- | Number of matrix nonzeros@@ -291,7 +298,7 @@                   (PatternMatrix d _ _ _) -> d                   (IntMatrix d _ _ _) -> d --- | Length of data vector internal to the Matrix; this is _not_ necessarily the actual number of matrix entries because symmetric entries are not stored +-- | Length of data vector internal to the Matrix; this is _not_ necessarily the actual number of matrix entries because symmetric entries are not stored numDat :: Matrix t -> Int numDat m = case m of (RMatrix _ _ _ d) -> length d                      (CMatrix _ _ _ d) -> length d@@ -304,10 +311,10 @@ dimArr a = case a of (RArray d _ _) -> d                      (CArray d _ _) -> d --- | Length of data vector internal to the Array; this is _not_ necessarily the actual number of matrix entries because symmetric entries are not stored +-- | Length of data vector internal to the Array; this is _not_ necessarily the actual number of matrix entries because symmetric entries are not stored numDatArr :: Array a -> Int-numDatArr a = case a of (RArray _ _ ll) -> length ll -                        (CArray _ _ ll) -> length ll            +numDatArr a = case a of (RArray _ _ ll) -> length ll+                        (CArray _ _ ll) -> length ll