summaryrefslogtreecommitdiff
path: root/packages/hmatrix/src/Numeric/LinearAlgebra/IO.hs
diff options
context:
space:
mode:
authorAlberto Ruiz <aruiz@um.es>2014-05-16 20:10:57 +0200
committerAlberto Ruiz <aruiz@um.es>2014-05-16 20:10:57 +0200
commitd4d9082a8d7d3eed6cb5f188fc3b476847dcac27 (patch)
tree0a19bc18be429454fd59c16fd46e29e1e7bae722 /packages/hmatrix/src/Numeric/LinearAlgebra/IO.hs
parent51f4cc7b4b301142b8df73568ffaa448f9e6dd50 (diff)
GSL.LinearAlgebra reorganized
Diffstat (limited to 'packages/hmatrix/src/Numeric/LinearAlgebra/IO.hs')
-rw-r--r--packages/hmatrix/src/Numeric/LinearAlgebra/IO.hs42
1 files changed, 42 insertions, 0 deletions
diff --git a/packages/hmatrix/src/Numeric/LinearAlgebra/IO.hs b/packages/hmatrix/src/Numeric/LinearAlgebra/IO.hs
new file mode 100644
index 0000000..d2278ee
--- /dev/null
+++ b/packages/hmatrix/src/Numeric/LinearAlgebra/IO.hs
@@ -0,0 +1,42 @@
1-----------------------------------------------------------------------------
2-- |
3-- Module : Numeric.LinearAlgebra.IO
4-- Copyright : (c) Alberto Ruiz 2007-14
5-- License : GPL
6-- Maintainer : Alberto Ruiz
7-- Stability : provisional
8--
9-----------------------------------------------------------------------------
10
11module Numeric.LinearAlgebra.IO (
12 saveMatrix,
13 fwriteVector, freadVector, fprintfVector, fscanfVector,
14 fileDimensions, loadMatrix, fromFile
15) where
16
17import Data.Packed
18import Numeric.GSL.LinearAlgebra
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