From d0fc6c7192badfa6f03baf0e02e0cf2a73c3906b Mon Sep 17 00:00:00 2001 From: Alberto Ruiz Date: Tue, 20 May 2014 17:35:40 +0200 Subject: loadMatrix, saveMatrix --- packages/base/src/Data/Packed/IO.hs | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) (limited to 'packages/base/src/Data') diff --git a/packages/base/src/Data/Packed/IO.hs b/packages/base/src/Data/Packed/IO.hs index dbb2943..db03d5f 100644 --- a/packages/base/src/Data/Packed/IO.hs +++ b/packages/base/src/Data/Packed/IO.hs @@ -14,7 +14,7 @@ module Data.Packed.IO ( dispf, disps, dispcf, vecdisp, latexFormat, format, - readMatrix, fromArray2D + readMatrix, fromArray2D, loadMatrix, saveMatrix ) where import Data.Packed @@ -22,6 +22,8 @@ import Data.Packed.Development import Text.Printf(printf) import Data.List(intersperse) import Data.Complex +import Numeric.Vectorized(vectorScan,saveMatrix) +import Control.Applicative((<$>)) {- | Creates a string from a matrix given a separator and a function to show each entry. Using this function the user can easily define any desired display function: @@ -139,3 +141,17 @@ dispcf d m = sdims m ++ "\n" ++ format " " (showComplex d) m readMatrix :: String -> Matrix Double readMatrix = fromLists . map (map read). map words . filter (not.null) . lines +-------------------------------------------------------------------------------- + +apparentCols :: FilePath -> IO Int +apparentCols s = f . dropWhile null . map words . lines <$> readFile s + where + f [] = 0 + f (x:_) = length x + +loadMatrix :: FilePath -> IO (Matrix Double) +loadMatrix f = do + v <- vectorScan f + c <- apparentCols f + return (reshape c v) + -- cgit v1.2.3