summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--hmatrix.cabal11
-rw-r--r--lib/Data/Packed/Internal/Matrix.hs2
-rw-r--r--lib/Data/Packed/Matrix.hs25
3 files changed, 33 insertions, 5 deletions
diff --git a/hmatrix.cabal b/hmatrix.cabal
index 5265451..133b602 100644
--- a/hmatrix.cabal
+++ b/hmatrix.cabal
@@ -55,9 +55,9 @@ flag unsafe
55 55
56library 56library
57 if flag(splitBase) 57 if flag(splitBase)
58 build-depends: base >= 3, array, QuickCheck, HUnit, storable-complex 58 build-depends: base >= 3, array, QuickCheck, HUnit, storable-complex, process
59 else 59 else
60 build-depends: base < 3, QuickCheck, HUnit, storable-complex 60 build-depends: base < 3, QuickCheck, HUnit, storable-complex, process
61 61
62 Build-Depends: haskell98 62 Build-Depends: haskell98
63 Extensions: ForeignFunctionInterface, 63 Extensions: ForeignFunctionInterface,
@@ -121,6 +121,13 @@ library
121 Numeric.LinearAlgebra.Tests.Properties 121 Numeric.LinearAlgebra.Tests.Properties
122 C-sources: lib/Numeric/LinearAlgebra/LAPACK/lapack-aux.c, 122 C-sources: lib/Numeric/LinearAlgebra/LAPACK/lapack-aux.c,
123 lib/Numeric/GSL/gsl-aux.c 123 lib/Numeric/GSL/gsl-aux.c
124 extra-source-files: lib/Numeric/LinearAlgebra/LAPACK/lapack-aux.h,
125 lib/Numeric/LinearAlgebra/LAPACK/clapack.h,
126 lib/Numeric/GSL/gsl-aux.h
127
128 extra-source-files: lib/Numeric/GSL/Special/auto.hs,
129 lib/Numeric/GSL/Special/autoall.sh,
130 lib/Numeric/GSL/Special/replace.hs
124 131
125 ghc-prof-options: -auto-all 132 ghc-prof-options: -auto-all
126 133
diff --git a/lib/Data/Packed/Internal/Matrix.hs b/lib/Data/Packed/Internal/Matrix.hs
index 8a074a6..7678a12 100644
--- a/lib/Data/Packed/Internal/Matrix.hs
+++ b/lib/Data/Packed/Internal/Matrix.hs
@@ -351,7 +351,7 @@ fromComplex :: Vector (Complex Double) -> (Vector Double, Vector Double)
351fromComplex z = (r,i) where 351fromComplex z = (r,i) where
352 [r,i] = toColumns $ reshape 2 $ asReal z 352 [r,i] = toColumns $ reshape 2 $ asReal z
353 353
354-- | loads a matrix efficiently from formatted ASCII text file (the number of rows and columns must be known in advance). 354-- | loads a matrix from an ASCII file (the number of rows and columns must be known in advance).
355fromFile :: FilePath -> (Int,Int) -> IO (Matrix Double) 355fromFile :: FilePath -> (Int,Int) -> IO (Matrix Double)
356fromFile filename (r,c) = do 356fromFile filename (r,c) = do
357 charname <- newCString filename 357 charname <- newCString filename
diff --git a/lib/Data/Packed/Matrix.hs b/lib/Data/Packed/Matrix.hs
index 0b50d8a..32eb991 100644
--- a/lib/Data/Packed/Matrix.hs
+++ b/lib/Data/Packed/Matrix.hs
@@ -29,7 +29,9 @@ module Data.Packed.Matrix (
29 extractRows, 29 extractRows,
30 ident, diag, diagRect, takeDiag, 30 ident, diag, diagRect, takeDiag,
31 liftMatrix, liftMatrix2, 31 liftMatrix, liftMatrix2,
32 format, readMatrix, fromFile, fromArray2D 32 format,
33 loadMatrix, fromFile, fileDimensions,
34 readMatrix, fromArray2D
33) where 35) where
34 36
35import Data.Packed.Internal 37import Data.Packed.Internal
@@ -37,6 +39,7 @@ import qualified Data.Packed.ST as ST
37import Data.Packed.Vector 39import Data.Packed.Vector
38import Data.List(transpose,intersperse) 40import Data.List(transpose,intersperse)
39import Data.Array 41import Data.Array
42import System.Process(readProcess)
40 43
41-- | creates a matrix from a vertical list of matrices 44-- | creates a matrix from a vertical list of matrices
42joinVert :: Element t => [Matrix t] -> Matrix t 45joinVert :: Element t => [Matrix t] -> Matrix t
@@ -227,10 +230,28 @@ dispC :: Int -> Matrix (Complex Double) -> IO ()
227dispC d m = disp m (shfc d) 230dispC d m = disp m (shfc d)
228-} 231-}
229 232
230-- | creates a matrix from a table of numbers. 233-- | reads a matrix from a string containing a table of numbers.
231readMatrix :: String -> Matrix Double 234readMatrix :: String -> Matrix Double
232readMatrix = fromLists . map (map read). map words . filter (not.null) . lines 235readMatrix = fromLists . map (map read). map words . filter (not.null) . lines
233 236
237{- | obtains the number of rows and columns in an ASCII data file
238 (provisionally using unix's wc).
239-}
240fileDimensions :: FilePath -> IO (Int,Int)
241fileDimensions fname = do
242 wcres <- readProcess "wc" ["-w",fname] ""
243 contents <- readFile fname
244 let tot = read . head . words $ wcres
245 c = length . head . dropWhile null . map words . lines $ contents
246 if tot > 0
247 then return (tot `div` c, c)
248 else return (0,0)
249
250{- | loads a matrix from a formatted ASCII file.
251-}
252loadMatrix :: FilePath -> IO (Matrix Double)
253loadMatrix file = fromFile file =<< fileDimensions file
254
234-- | rearranges the rows of a matrix according to the order given in a list of integers. 255-- | rearranges the rows of a matrix according to the order given in a list of integers.
235extractRows :: Element t => [Int] -> Matrix t -> Matrix t 256extractRows :: Element t => [Int] -> Matrix t -> Matrix t
236extractRows l m = fromRows $ extract (toRows $ m) l 257extractRows l m = fromRows $ extract (toRows $ m) l