diff options
Diffstat (limited to 'lib/Data/Packed/Matrix.hs')
-rw-r--r-- | lib/Data/Packed/Matrix.hs | 25 |
1 files changed, 23 insertions, 2 deletions
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 |