summaryrefslogtreecommitdiff
path: root/lib/Data
diff options
context:
space:
mode:
authorAlberto Ruiz <aruiz@um.es>2009-05-16 17:12:44 +0000
committerAlberto Ruiz <aruiz@um.es>2009-05-16 17:12:44 +0000
commit1412579714611555ae6263aed1bd8ffe71fa5961 (patch)
tree626ccf93c43418978e975f7a20c8604c8d76f6ff /lib/Data
parent37f71ca5188ba487d4e1c274873b187ddcb30576 (diff)
loadMatrix (based on wc)
Diffstat (limited to 'lib/Data')
-rw-r--r--lib/Data/Packed/Internal/Matrix.hs2
-rw-r--r--lib/Data/Packed/Matrix.hs25
2 files changed, 24 insertions, 3 deletions
diff --git a/lib/Data/Packed/Internal/Matrix.hs b/lib/Data/Packed/Internal/Matrix.hs
index 8a074a6..7678a12 100644
--- a/lib/Data/Packed/Internal/Matrix.hs
+++ b/lib/Data/Packed/Internal/Matrix.hs
@@ -351,7 +351,7 @@ fromComplex :: Vector (Complex Double) -> (Vector Double, Vector Double)
351fromComplex z = (r,i) where 351fromComplex z = (r,i) where
352 [r,i] = toColumns $ reshape 2 $ asReal z 352 [r,i] = toColumns $ reshape 2 $ asReal z
353 353
354-- | loads a matrix efficiently from formatted ASCII text file (the number of rows and columns must be known in advance). 354-- | loads a matrix from an ASCII file (the number of rows and columns must be known in advance).
355fromFile :: FilePath -> (Int,Int) -> IO (Matrix Double) 355fromFile :: FilePath -> (Int,Int) -> IO (Matrix Double)
356fromFile filename (r,c) = do 356fromFile filename (r,c) = do
357 charname <- newCString filename 357 charname <- newCString filename
diff --git a/lib/Data/Packed/Matrix.hs b/lib/Data/Packed/Matrix.hs
index 0b50d8a..32eb991 100644
--- a/lib/Data/Packed/Matrix.hs
+++ b/lib/Data/Packed/Matrix.hs
@@ -29,7 +29,9 @@ module Data.Packed.Matrix (
29 extractRows, 29 extractRows,
30 ident, diag, diagRect, takeDiag, 30 ident, diag, diagRect, takeDiag,
31 liftMatrix, liftMatrix2, 31 liftMatrix, liftMatrix2,
32 format, readMatrix, fromFile, fromArray2D 32 format,
33 loadMatrix, fromFile, fileDimensions,
34 readMatrix, fromArray2D
33) where 35) where
34 36
35import Data.Packed.Internal 37import Data.Packed.Internal
@@ -37,6 +39,7 @@ import qualified Data.Packed.ST as ST
37import Data.Packed.Vector 39import Data.Packed.Vector
38import Data.List(transpose,intersperse) 40import Data.List(transpose,intersperse)
39import Data.Array 41import Data.Array
42import System.Process(readProcess)
40 43
41-- | creates a matrix from a vertical list of matrices 44-- | creates a matrix from a vertical list of matrices
42joinVert :: Element t => [Matrix t] -> Matrix t 45joinVert :: Element t => [Matrix t] -> Matrix t
@@ -227,10 +230,28 @@ dispC :: Int -> Matrix (Complex Double) -> IO ()
227dispC d m = disp m (shfc d) 230dispC d m = disp m (shfc d)
228-} 231-}
229 232
230-- | creates a matrix from a table of numbers. 233-- | reads a matrix from a string containing a table of numbers.
231readMatrix :: String -> Matrix Double 234readMatrix :: String -> Matrix Double
232readMatrix = fromLists . map (map read). map words . filter (not.null) . lines 235readMatrix = fromLists . map (map read). map words . filter (not.null) . lines
233 236
237{- | obtains the number of rows and columns in an ASCII data file
238 (provisionally using unix's wc).
239-}
240fileDimensions :: FilePath -> IO (Int,Int)
241fileDimensions fname = do
242 wcres <- readProcess "wc" ["-w",fname] ""
243 contents <- readFile fname
244 let tot = read . head . words $ wcres
245 c = length . head . dropWhile null . map words . lines $ contents
246 if tot > 0
247 then return (tot `div` c, c)
248 else return (0,0)
249
250{- | loads a matrix from a formatted ASCII file.
251-}
252loadMatrix :: FilePath -> IO (Matrix Double)
253loadMatrix file = fromFile file =<< fileDimensions file
254
234-- | rearranges the rows of a matrix according to the order given in a list of integers. 255-- | rearranges the rows of a matrix according to the order given in a list of integers.
235extractRows :: Element t => [Int] -> Matrix t -> Matrix t 256extractRows :: Element t => [Int] -> Matrix t -> Matrix t
236extractRows l m = fromRows $ extract (toRows $ m) l 257extractRows l m = fromRows $ extract (toRows $ m) l