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