module Numeric.GSL.IO (
saveMatrix,
fwriteVector, freadVector, fprintfVector, fscanfVector,
fileDimensions, loadMatrix, fromFile
) where
import Numeric.LinearAlgebra.HMatrix hiding(saveMatrix, loadMatrix)
import Numeric.GSL.Vector
import System.Process(readProcess)
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)
loadMatrix :: FilePath -> IO (Matrix Double)
loadMatrix file = fromFile file =<< fileDimensions file
fromFile :: FilePath -> (Int,Int) -> IO (Matrix Double)
fromFile filename (r,c) = reshape c `fmap` fscanfVector filename (r*c)