Commits

Sergey Astanin committed 73f013f

toCompleteList, include entries above the diagonal too.

Comments (0)

Files changed (2)

matrix-market-pure.cabal

 -- The package version. See the Haskell package versioning policy
 -- (http://www.haskell.org/haskellwiki/Package_versioning_policy) for
 -- standards guiding when and how versions should be incremented.
-Version:             0.1
+Version:             0.1.1
 
 -- A short (one-line) description of the package.
 Synopsis:            Pure and composable reader and writer of the Matrix Market format.

src/Data/MatrixMarket.hs

     -- * Utility functions
   , mm'rows, mm'cols, mm'shape
   , toList
+  , toCompleteList
+    -- * Re-exports
+  , Complex(..)
   )
   where
 
 import Data.Char (toLower)
-import Data.Complex (Complex(..))
+import Data.Complex (Complex(..), conjugate)
 import Data.Maybe (listToMaybe)
 import Control.Applicative ((<$>), (<*>))
 import Control.Monad (join)
     typename :: a -> String
     readval  :: [String] -> Maybe a
     showval  :: a -> String
+    conj     :: a -> a
 
 instance MValue Int where
   typename _ = "integer"
   readval [s] = maybeRead s
   readval _   = Nothing
   showval     = show
+  conj        = id
 
 instance MValue Double where
   typename _ = "real"
   readval [s] = maybeRead s
   readval _   = Nothing
   showval     = show
+  conj        = id
 
 instance MValue (Complex Double) where
   typename _  = "complex"
   readval [re,im] = (:+) <$> (maybeRead re) <*> (maybeRead im)
   readval _       = Nothing
   showval (re :+ im) = unwords [show re, show im]
+  conj            = conjugate
 
 maybeRead :: (Read a) => String -> Maybe a
 maybeRead s = listToMaybe (fst <$> reads s)
         ncs = am'cols am
     in  zip [ (r,c) | c<-[1..ncs], r<-[1..nrs] ] $ am'values am
 
+-- | Make a list of all non-zero entries (including symmetric entries).
+toCompleteList :: (MValue a) => Matrix a -> [((Int, Int), a)]
+toCompleteList m@(MM _ _ General _) = toList m
+toCompleteList m@(MM _ _ Symmetric _) = concatMap (insertSymmetric) (toList m)
+toCompleteList m@(MM _ _ SkewSymmetric _) = concatMap (insertSkew) (toList m)
+toCompleteList m@(MM _ _ Hermitian _) = concatMap (insertHermitian) (toList m)
+
+insertSymmetric p@((i,j),v)
+    | i /= j     = [p, ((j,i),v)]
+    | otherwise  = [p]
+insertSkew p@((i,j),v)
+    | i /= j     = [p, ((j,i),(-v))]
+    | otherwise  = [p]
+insertHermitian p@((i,j),v)
+    | i /= j     = [p, ((j,i),conj v)]
+    | otherwise  = [p]
+
 -- | Write Matrix Market format.
 dumpMM :: MValue a => Matrix a -> String
 dumpMM (MM md fld sy coms) = unlines $ header : (map ('%':) coms) ++ body