diff options
-rw-r--r-- | lib/Data/Packed/Internal/Matrix.hs | 20 | ||||
-rw-r--r-- | lib/Data/Packed/Matrix.hs | 10 | ||||
-rw-r--r-- | lib/Numeric/GSL/gsl-aux.c | 25 |
3 files changed, 46 insertions, 9 deletions
diff --git a/lib/Data/Packed/Internal/Matrix.hs b/lib/Data/Packed/Internal/Matrix.hs index 01d2ccf..68547bd 100644 --- a/lib/Data/Packed/Internal/Matrix.hs +++ b/lib/Data/Packed/Internal/Matrix.hs | |||
@@ -23,6 +23,7 @@ import Data.Packed.Internal.Vector | |||
23 | import Foreign hiding (xor) | 23 | import Foreign hiding (xor) |
24 | import Complex | 24 | import Complex |
25 | import Foreign.C.Types | 25 | import Foreign.C.Types |
26 | import Foreign.C.String | ||
26 | 27 | ||
27 | ----------------------------------------------------------------- | 28 | ----------------------------------------------------------------- |
28 | 29 | ||
@@ -350,6 +351,19 @@ fromComplex :: Vector (Complex Double) -> (Vector Double, Vector Double) | |||
350 | fromComplex z = (r,i) where | 351 | fromComplex z = (r,i) where |
351 | [r,i] = toColumns $ reshape 2 $ asReal z | 352 | [r,i] = toColumns $ reshape 2 $ asReal z |
352 | 353 | ||
353 | -- | loads a matrix from an ASCII file (the number of rows and columns must be known in advance). | 354 | -------------------------------------------------------------------------- |
354 | fromFile :: FilePath -> (Int,Int) -> IO (Matrix Double) | 355 | |
355 | fromFile filename (r,c) = reshape c `fmap` fscanfVector filename (r*c) | 356 | -- | Saves a matrix as 2D ASCII table. |
357 | saveMatrix :: FilePath | ||
358 | -> String -- ^ format (%f, %g, %e) | ||
359 | -> Matrix Double | ||
360 | -> IO () | ||
361 | saveMatrix filename fmt m = do | ||
362 | charname <- newCString filename | ||
363 | charfmt <- newCString fmt | ||
364 | let o = if orderOf m == RowMajor then 1 else 0 | ||
365 | app1 (matrix_fprintf charname charfmt o) mat m "matrix_fprintf" | ||
366 | free charname | ||
367 | free charfmt | ||
368 | |||
369 | foreign import ccall "matrix_fprintf" matrix_fprintf :: Ptr CChar -> Ptr CChar -> CInt -> TM | ||
diff --git a/lib/Data/Packed/Matrix.hs b/lib/Data/Packed/Matrix.hs index 32eb991..ad0776e 100644 --- a/lib/Data/Packed/Matrix.hs +++ b/lib/Data/Packed/Matrix.hs | |||
@@ -30,7 +30,7 @@ module Data.Packed.Matrix ( | |||
30 | ident, diag, diagRect, takeDiag, | 30 | ident, diag, diagRect, takeDiag, |
31 | liftMatrix, liftMatrix2, | 31 | liftMatrix, liftMatrix2, |
32 | format, | 32 | format, |
33 | loadMatrix, fromFile, fileDimensions, | 33 | loadMatrix, saveMatrix, fromFile, fileDimensions, |
34 | readMatrix, fromArray2D | 34 | readMatrix, fromArray2D |
35 | ) where | 35 | ) where |
36 | 36 | ||
@@ -247,11 +247,15 @@ fileDimensions fname = do | |||
247 | then return (tot `div` c, c) | 247 | then return (tot `div` c, c) |
248 | else return (0,0) | 248 | else return (0,0) |
249 | 249 | ||
250 | {- | loads a matrix from a formatted ASCII file. | 250 | -- | Loads a matrix from an ASCII file formatted as a 2D table. |
251 | -} | ||
252 | loadMatrix :: FilePath -> IO (Matrix Double) | 251 | loadMatrix :: FilePath -> IO (Matrix Double) |
253 | loadMatrix file = fromFile file =<< fileDimensions file | 252 | loadMatrix file = fromFile file =<< fileDimensions file |
254 | 253 | ||
254 | -- | Loads a matrix from an ASCII file (the number of rows and columns must be known in advance). | ||
255 | fromFile :: FilePath -> (Int,Int) -> IO (Matrix Double) | ||
256 | fromFile filename (r,c) = reshape c `fmap` fscanfVector filename (r*c) | ||
257 | |||
258 | |||
255 | -- | rearranges the rows of a matrix according to the order given in a list of integers. | 259 | -- | rearranges the rows of a matrix according to the order given in a list of integers. |
256 | extractRows :: Element t => [Int] -> Matrix t -> Matrix t | 260 | extractRows :: Element t => [Int] -> Matrix t -> Matrix t |
257 | extractRows l m = fromRows $ extract (toRows $ m) l | 261 | extractRows l m = fromRows $ extract (toRows $ m) l |
diff --git a/lib/Numeric/GSL/gsl-aux.c b/lib/Numeric/GSL/gsl-aux.c index b3b524d..d129aeb 100644 --- a/lib/Numeric/GSL/gsl-aux.c +++ b/lib/Numeric/GSL/gsl-aux.c | |||
@@ -351,7 +351,7 @@ int polySolve(KRVEC(a), CVEC(z)) { | |||
351 | } | 351 | } |
352 | 352 | ||
353 | int vector_fscanf(char*filename, RVEC(a)) { | 353 | int vector_fscanf(char*filename, RVEC(a)) { |
354 | DEBUGMSG("gsl_matrix_fscanf"); | 354 | DEBUGMSG("gsl_vector_fscanf"); |
355 | DVVIEW(a); | 355 | DVVIEW(a); |
356 | FILE * f = fopen(filename,"r"); | 356 | FILE * f = fopen(filename,"r"); |
357 | CHECK(!f,BAD_FILE); | 357 | CHECK(!f,BAD_FILE); |
@@ -373,7 +373,7 @@ int vector_fprintf(char*filename, char*fmt, RVEC(a)) { | |||
373 | } | 373 | } |
374 | 374 | ||
375 | int vector_fread(char*filename, RVEC(a)) { | 375 | int vector_fread(char*filename, RVEC(a)) { |
376 | DEBUGMSG("gsl_matrix_fscanf"); | 376 | DEBUGMSG("gsl_vector_fread"); |
377 | DVVIEW(a); | 377 | DVVIEW(a); |
378 | FILE * f = fopen(filename,"r"); | 378 | FILE * f = fopen(filename,"r"); |
379 | CHECK(!f,BAD_FILE); | 379 | CHECK(!f,BAD_FILE); |
@@ -384,7 +384,7 @@ int vector_fread(char*filename, RVEC(a)) { | |||
384 | } | 384 | } |
385 | 385 | ||
386 | int vector_fwrite(char*filename, RVEC(a)) { | 386 | int vector_fwrite(char*filename, RVEC(a)) { |
387 | DEBUGMSG("gsl_vector_fprintf"); | 387 | DEBUGMSG("gsl_vector_fwrite"); |
388 | DVVIEW(a); | 388 | DVVIEW(a); |
389 | FILE * f = fopen(filename,"w"); | 389 | FILE * f = fopen(filename,"w"); |
390 | CHECK(!f,BAD_FILE); | 390 | CHECK(!f,BAD_FILE); |
@@ -394,6 +394,25 @@ int vector_fwrite(char*filename, RVEC(a)) { | |||
394 | OK | 394 | OK |
395 | } | 395 | } |
396 | 396 | ||
397 | int matrix_fprintf(char*filename, char*fmt, int ro, RMAT(m)) { | ||
398 | DEBUGMSG("matrix_fprintf"); | ||
399 | FILE * f = fopen(filename,"w"); | ||
400 | CHECK(!f,BAD_FILE); | ||
401 | int i,j,sr,sc; | ||
402 | if (ro==1) { sr = mc; sc = 1;} else { sr = 1; sc = mr;} | ||
403 | #define AT(M,r,c) (M##p[(r)*sr+(c)*sc]) | ||
404 | for (i=0; i<mr; i++) { | ||
405 | for (j=0; j<mc-1; j++) { | ||
406 | fprintf(f,fmt,AT(m,i,j)); | ||
407 | fprintf(f," "); | ||
408 | } | ||
409 | fprintf(f,fmt,AT(m,i,j)); | ||
410 | fprintf(f,"\n"); | ||
411 | } | ||
412 | fclose (f); | ||
413 | OK | ||
414 | } | ||
415 | |||
397 | //--------------------------------------------------------------- | 416 | //--------------------------------------------------------------- |
398 | 417 | ||
399 | typedef double Trawfun(int, double*); | 418 | typedef double Trawfun(int, double*); |