From 3815bc25f62124063e02af83fe3c907336dc86f5 Mon Sep 17 00:00:00 2001 From: Alberto Ruiz Date: Fri, 28 Sep 2007 12:25:56 +0000 Subject: algorithms interface reorganized --- lib/Data/Packed/Internal/Matrix.hs | 12 ++++++++++++ lib/Data/Packed/Internal/aux.h | 2 ++ lib/Data/Packed/Matrix.hs | 8 +++++++- 3 files changed, 21 insertions(+), 1 deletion(-) (limited to 'lib/Data/Packed') diff --git a/lib/Data/Packed/Internal/Matrix.hs b/lib/Data/Packed/Internal/Matrix.hs index 63ebddf..e76500b 100644 --- a/lib/Data/Packed/Internal/Matrix.hs +++ b/lib/Data/Packed/Internal/Matrix.hs @@ -24,6 +24,8 @@ import Complex import Control.Monad(when) import Data.List(transpose,intersperse) import Data.Maybe(fromJust) +import Foreign.C.String +import Foreign.C.Types ----------------------------------------------------------------- @@ -371,6 +373,16 @@ fromComplex z = (r,i) where comp :: Vector Double -> Vector (Complex Double) comp v = toComplex (v,constant 0 (dim v)) +-- | loads a matrix efficiently from formatted ASCII text file (the number of rows and columns must be known in advance). +fromFile :: FilePath -> (Int,Int) -> IO (Matrix Double) +fromFile filename (r,c) = do + charname <- newCString filename + res <- createMatrix RowMajor r c + c_gslReadMatrix charname // mat dat res // check "gslReadMatrix" [] + --free charname -- TO DO: free the auxiliary CString + return res +foreign import ccall "aux.h matrix_fscanf" c_gslReadMatrix:: Ptr CChar -> TM + ------------------------------------------------------------------------- -- Generic definitions diff --git a/lib/Data/Packed/Internal/aux.h b/lib/Data/Packed/Internal/aux.h index 83111e5..73334e3 100644 --- a/lib/Data/Packed/Internal/aux.h +++ b/lib/Data/Packed/Internal/aux.h @@ -26,3 +26,5 @@ int diagR(KRVEC(d),RMAT(r)); int diagC(KCVEC(d),CMAT(r)); const char * gsl_strerror (const int gsl_errno); + +int matrix_fscanf(char*filename, RMAT(a)); diff --git a/lib/Data/Packed/Matrix.hs b/lib/Data/Packed/Matrix.hs index 404fde7..2b93348 100644 --- a/lib/Data/Packed/Matrix.hs +++ b/lib/Data/Packed/Matrix.hs @@ -25,9 +25,10 @@ module Data.Packed.Matrix ( fromBlocks, flipud, fliprl, subMatrix, takeRows, dropRows, takeColumns, dropColumns, + extractRows, ident, diag, diagRect, takeDiag, liftMatrix, liftMatrix2, - format, dispR, readMatrix, fromArray2D + format, dispR, readMatrix, fromFile, fromArray2D ) where import Data.Packed.Internal @@ -209,3 +210,8 @@ dispC d m = disp m (shfc d) -- | creates a matrix from a table of numbers. readMatrix :: String -> Matrix Double readMatrix = fromLists . map (map read). map words . filter (not.null) . lines + +-- | rearranges the rows of a matrix according to the order given in a list of integers. +extractRows :: Field t => [Int] -> Matrix t -> Matrix t +extractRows l m = fromRows $ extract (toRows $ m) l + where extract l is = [l!!i |i<-is] -- cgit v1.2.3