summaryrefslogtreecommitdiff
path: root/packages/hmatrix/src/Numeric/GSL/IO.hs
diff options
context:
space:
mode:
Diffstat (limited to 'packages/hmatrix/src/Numeric/GSL/IO.hs')
-rw-r--r--packages/hmatrix/src/Numeric/GSL/IO.hs42
1 files changed, 42 insertions, 0 deletions
diff --git a/packages/hmatrix/src/Numeric/GSL/IO.hs b/packages/hmatrix/src/Numeric/GSL/IO.hs
new file mode 100644
index 0000000..0d6031a
--- /dev/null
+++ b/packages/hmatrix/src/Numeric/GSL/IO.hs
@@ -0,0 +1,42 @@
1-----------------------------------------------------------------------------
2-- |
3-- Module : Numeric.GSL.IO
4-- Copyright : (c) Alberto Ruiz 2007-14
5-- License : GPL
6-- Maintainer : Alberto Ruiz
7-- Stability : provisional
8--
9-----------------------------------------------------------------------------
10
11module Numeric.GSL.IO (
12 saveMatrix,
13 fwriteVector, freadVector, fprintfVector, fscanfVector,
14 fileDimensions, loadMatrix, fromFile
15) where
16
17import Data.Packed
18import Numeric.GSL.Vector
19import System.Process(readProcess)
20
21
22{- | obtains the number of rows and columns in an ASCII data file
23 (provisionally using unix's wc).
24-}
25fileDimensions :: FilePath -> IO (Int,Int)
26fileDimensions fname = do
27 wcres <- readProcess "wc" ["-w",fname] ""
28 contents <- readFile fname
29 let tot = read . head . words $ wcres
30 c = length . head . dropWhile null . map words . lines $ contents
31 if tot > 0
32 then return (tot `div` c, c)
33 else return (0,0)
34
35-- | Loads a matrix from an ASCII file formatted as a 2D table.
36loadMatrix :: FilePath -> IO (Matrix Double)
37loadMatrix file = fromFile file =<< fileDimensions file
38
39-- | Loads a matrix from an ASCII file (the number of rows and columns must be known in advance).
40fromFile :: FilePath -> (Int,Int) -> IO (Matrix Double)
41fromFile filename (r,c) = reshape c `fmap` fscanfVector filename (r*c)
42