From 01832377377142696645a2ea800af2db3d0fac15 Mon Sep 17 00:00:00 2001 From: Alberto Ruiz Date: Thu, 12 Jun 2014 14:15:05 +0200 Subject: improved loadMatrix --- packages/base/src/C/vector-aux.c | 5 ++++- packages/base/src/Data/Packed/IO.hs | 12 ++++++++++-- packages/base/src/Numeric/LinearAlgebra/Data.hs | 2 +- packages/base/src/Numeric/LinearAlgebra/Util.hs | 5 +++-- packages/base/src/Numeric/Vectorized.hs | 2 +- 5 files changed, 19 insertions(+), 7 deletions(-) (limited to 'packages/base/src') diff --git a/packages/base/src/C/vector-aux.c b/packages/base/src/C/vector-aux.c index efb82af..2f47c8f 100644 --- a/packages/base/src/C/vector-aux.c +++ b/packages/base/src/C/vector-aux.c @@ -654,6 +654,9 @@ int zipQ(int code, KQVEC(a), KQVEC(b), QVEC(r)) { int vectorScan(char * file, int* n, double**pp){ FILE * fp; fp = fopen (file, "r"); + if(!fp) { + ERROR(BAD_FILE); + } int nbuf = 100*100; double * p = (double*)malloc(nbuf*sizeof(double)); int k=0; @@ -667,7 +670,7 @@ int vectorScan(char * file, int* n, double**pp){ if (k==nbuf) { nbuf = nbuf * 2; p = (double*)realloc(p,nbuf*sizeof(double)); - //printf("R\n"); + // printf("R\n"); } p[k++] = d; } diff --git a/packages/base/src/Data/Packed/IO.hs b/packages/base/src/Data/Packed/IO.hs index f7afa80..85f1b37 100644 --- a/packages/base/src/Data/Packed/IO.hs +++ b/packages/base/src/Data/Packed/IO.hs @@ -14,7 +14,7 @@ module Data.Packed.IO ( dispf, disps, dispcf, vecdisp, latexFormat, format, - readMatrix, fromArray2D, loadMatrix, saveMatrix + readMatrix, fromArray2D, loadMatrix, loadMatrix', saveMatrix ) where import Data.Packed @@ -155,5 +155,13 @@ loadMatrix :: FilePath -> IO (Matrix Double) loadMatrix f = do v <- vectorScan f c <- apparentCols f - return (reshape c v) + if (dim v `mod` c /= 0) + then + error $ printf "loadMatrix: %d elements and %d columns in file %s" + (dim v) c f + else + return (reshape c v) + + +loadMatrix' name = mbCatch (loadMatrix name) diff --git a/packages/base/src/Numeric/LinearAlgebra/Data.hs b/packages/base/src/Numeric/LinearAlgebra/Data.hs index d5ce32f..b1a31fc 100644 --- a/packages/base/src/Numeric/LinearAlgebra/Data.hs +++ b/packages/base/src/Numeric/LinearAlgebra/Data.hs @@ -55,7 +55,7 @@ module Numeric.LinearAlgebra.Data( -- * IO disp, - loadMatrix, saveMatrix, + loadMatrix, loadMatrix', saveMatrix, latexFormat, dispf, disps, dispcf, format, dispDots, dispBlanks, dispShort, diff --git a/packages/base/src/Numeric/LinearAlgebra/Util.hs b/packages/base/src/Numeric/LinearAlgebra/Util.hs index b6f8966..6bb9d15 100644 --- a/packages/base/src/Numeric/LinearAlgebra/Util.hs +++ b/packages/base/src/Numeric/LinearAlgebra/Util.hs @@ -416,7 +416,7 @@ c ~!~ msg = when c (error msg) formatSparse :: String -> String -> String -> Int -> Matrix Double -> String -formatSparse zeroI zeroF sep _ (approxInt -> Just m) = format sep f m +formatSparse zeroI _zeroF sep _ (approxInt -> Just m) = format sep f m where f 0 = zeroI f x = printf "%.0f" x @@ -424,7 +424,8 @@ formatSparse zeroI zeroF sep _ (approxInt -> Just m) = format sep f m formatSparse zeroI zeroF sep n m = format sep f m where f x | abs (x::Double) < 2*peps = zeroI++zeroF - | abs (fromIntegral (round x) - x) / abs x < 2*peps = printf ("%.0f."++replicate n ' ') x + | abs (fromIntegral (round x::Int) - x) / abs x < 2*peps + = printf ("%.0f."++replicate n ' ') x | otherwise = printf ("%."++show n++"f") x approxInt m diff --git a/packages/base/src/Numeric/Vectorized.hs b/packages/base/src/Numeric/Vectorized.hs index 936daba..6f0d240 100644 --- a/packages/base/src/Numeric/Vectorized.hs +++ b/packages/base/src/Numeric/Vectorized.hs @@ -294,7 +294,7 @@ vectorScan s = do cs <- newCString s ok <- c_vectorScan cs pn pp when (not (ok == 0)) $ - error ("vectorScan \"" ++ s ++"\"") + error ("vectorScan: file \"" ++ s ++"\" not found") n <- fromIntegral <$> peek pn p <- peek pp v <- createVector n -- cgit v1.2.3