summaryrefslogtreecommitdiff
path: root/packages/hmatrix/src/Numeric/GSL/LinearAlgebra.hs
diff options
context:
space:
mode:
Diffstat (limited to 'packages/hmatrix/src/Numeric/GSL/LinearAlgebra.hs')
-rw-r--r--packages/hmatrix/src/Numeric/GSL/LinearAlgebra.hs38
1 files changed, 33 insertions, 5 deletions
diff --git a/packages/hmatrix/src/Numeric/GSL/LinearAlgebra.hs b/packages/hmatrix/src/Numeric/GSL/LinearAlgebra.hs
index 8bd0cc2..17e2258 100644
--- a/packages/hmatrix/src/Numeric/GSL/LinearAlgebra.hs
+++ b/packages/hmatrix/src/Numeric/GSL/LinearAlgebra.hs
@@ -9,13 +9,13 @@
9----------------------------------------------------------------------------- 9-----------------------------------------------------------------------------
10 10
11module Numeric.GSL.LinearAlgebra ( 11module Numeric.GSL.LinearAlgebra (
12 randomVector, 12 RandDist(..), randomVector,
13 saveMatrix, 13 saveMatrix,
14 fwriteVector, freadVector, fprintfVector, fscanfVector 14 fwriteVector, freadVector, fprintfVector, fscanfVector,
15 fileDimensions, loadMatrix, fromFile
15) where 16) where
16 17
17import Data.Packed 18import Data.Packed
18import Numeric.LinearAlgebra.Base(RandDist(..))
19import Numeric.GSL.Internal hiding (TV,TM,TCV,TCM) 19import Numeric.GSL.Internal hiding (TV,TM,TCV,TCM)
20 20
21import Foreign.Marshal.Alloc(free) 21import Foreign.Marshal.Alloc(free)
@@ -23,11 +23,16 @@ import Foreign.Ptr(Ptr)
23import Foreign.C.Types 23import Foreign.C.Types
24import Foreign.C.String(newCString) 24import Foreign.C.String(newCString)
25import System.IO.Unsafe(unsafePerformIO) 25import System.IO.Unsafe(unsafePerformIO)
26import System.Process(readProcess)
26 27
27fromei x = fromIntegral (fromEnum x) :: CInt 28fromei x = fromIntegral (fromEnum x) :: CInt
28 29
29----------------------------------------------------------------------- 30-----------------------------------------------------------------------
30 31
32data RandDist = Uniform -- ^ uniform distribution in [0,1)
33 | Gaussian -- ^ normal distribution with mean zero and standard deviation one
34 deriving Enum
35
31-- | Obtains a vector of pseudorandom elements from the the mt19937 generator in GSL, with a given seed. Use randomIO to get a random seed. 36-- | Obtains a vector of pseudorandom elements from the the mt19937 generator in GSL, with a given seed. Use randomIO to get a random seed.
32randomVector :: Int -- ^ seed 37randomVector :: Int -- ^ seed
33 -> RandDist -- ^ distribution 38 -> RandDist -- ^ distribution
@@ -35,10 +40,10 @@ randomVector :: Int -- ^ seed
35 -> Vector Double 40 -> Vector Double
36randomVector seed dist n = unsafePerformIO $ do 41randomVector seed dist n = unsafePerformIO $ do
37 r <- createVector n 42 r <- createVector n
38 app1 (c_random_vector_GSL (fi seed) ((fi.fromEnum) dist)) vec r "randomVectorGSL" 43 app1 (c_random_vector (fi seed) ((fi.fromEnum) dist)) vec r "randomVector"
39 return r 44 return r
40 45
41foreign import ccall unsafe "random_vector_GSL" c_random_vector_GSL :: CInt -> CInt -> TV 46foreign import ccall unsafe "random_vector" c_random_vector :: CInt -> CInt -> TV
42 47
43-------------------------------------------------------------------------------- 48--------------------------------------------------------------------------------
44 49
@@ -105,3 +110,26 @@ type PD = Ptr Double --
105type TV = CInt -> PD -> IO CInt -- 110type TV = CInt -> PD -> IO CInt --
106type TM = CInt -> CInt -> PD -> IO CInt -- 111type TM = CInt -> CInt -> PD -> IO CInt --
107 112
113--------------------------------------------------------------------------------
114
115{- | obtains the number of rows and columns in an ASCII data file
116 (provisionally using unix's wc).
117-}
118fileDimensions :: FilePath -> IO (Int,Int)
119fileDimensions fname = do
120 wcres <- readProcess "wc" ["-w",fname] ""
121 contents <- readFile fname
122 let tot = read . head . words $ wcres
123 c = length . head . dropWhile null . map words . lines $ contents
124 if tot > 0
125 then return (tot `div` c, c)
126 else return (0,0)
127
128-- | Loads a matrix from an ASCII file formatted as a 2D table.
129loadMatrix :: FilePath -> IO (Matrix Double)
130loadMatrix file = fromFile file =<< fileDimensions file
131
132-- | Loads a matrix from an ASCII file (the number of rows and columns must be known in advance).
133fromFile :: FilePath -> (Int,Int) -> IO (Matrix Double)
134fromFile filename (r,c) = reshape c `fmap` fscanfVector filename (r*c)
135