diff options
author | Alberto Ruiz <aruiz@um.es> | 2009-05-16 17:12:44 +0000 |
---|---|---|
committer | Alberto Ruiz <aruiz@um.es> | 2009-05-16 17:12:44 +0000 |
commit | 1412579714611555ae6263aed1bd8ffe71fa5961 (patch) | |
tree | 626ccf93c43418978e975f7a20c8604c8d76f6ff /lib/Data | |
parent | 37f71ca5188ba487d4e1c274873b187ddcb30576 (diff) |
loadMatrix (based on wc)
Diffstat (limited to 'lib/Data')
-rw-r--r-- | lib/Data/Packed/Internal/Matrix.hs | 2 | ||||
-rw-r--r-- | lib/Data/Packed/Matrix.hs | 25 |
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) | |||
351 | fromComplex z = (r,i) where | 351 | fromComplex 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). |
355 | fromFile :: FilePath -> (Int,Int) -> IO (Matrix Double) | 355 | fromFile :: FilePath -> (Int,Int) -> IO (Matrix Double) |
356 | fromFile filename (r,c) = do | 356 | fromFile 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 | ||
35 | import Data.Packed.Internal | 37 | import Data.Packed.Internal |
@@ -37,6 +39,7 @@ import qualified Data.Packed.ST as ST | |||
37 | import Data.Packed.Vector | 39 | import Data.Packed.Vector |
38 | import Data.List(transpose,intersperse) | 40 | import Data.List(transpose,intersperse) |
39 | import Data.Array | 41 | import Data.Array |
42 | import 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 |
42 | joinVert :: Element t => [Matrix t] -> Matrix t | 45 | joinVert :: Element t => [Matrix t] -> Matrix t |
@@ -227,10 +230,28 @@ dispC :: Int -> Matrix (Complex Double) -> IO () | |||
227 | dispC d m = disp m (shfc d) | 230 | dispC 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. |
231 | readMatrix :: String -> Matrix Double | 234 | readMatrix :: String -> Matrix Double |
232 | readMatrix = fromLists . map (map read). map words . filter (not.null) . lines | 235 | readMatrix = 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 | -} | ||
240 | fileDimensions :: FilePath -> IO (Int,Int) | ||
241 | fileDimensions 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 | -} | ||
252 | loadMatrix :: FilePath -> IO (Matrix Double) | ||
253 | loadMatrix 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. |
235 | extractRows :: Element t => [Int] -> Matrix t -> Matrix t | 256 | extractRows :: Element t => [Int] -> Matrix t -> Matrix t |
236 | extractRows l m = fromRows $ extract (toRows $ m) l | 257 | extractRows l m = fromRows $ extract (toRows $ m) l |