diff options
Diffstat (limited to 'packages/hmatrix/src/Numeric/GSL/LinearAlgebra.hs')
-rw-r--r-- | packages/hmatrix/src/Numeric/GSL/LinearAlgebra.hs | 38 |
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 | ||
11 | module Numeric.GSL.LinearAlgebra ( | 11 | module 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 | ||
17 | import Data.Packed | 18 | import Data.Packed |
18 | import Numeric.LinearAlgebra.Base(RandDist(..)) | ||
19 | import Numeric.GSL.Internal hiding (TV,TM,TCV,TCM) | 19 | import Numeric.GSL.Internal hiding (TV,TM,TCV,TCM) |
20 | 20 | ||
21 | import Foreign.Marshal.Alloc(free) | 21 | import Foreign.Marshal.Alloc(free) |
@@ -23,11 +23,16 @@ import Foreign.Ptr(Ptr) | |||
23 | import Foreign.C.Types | 23 | import Foreign.C.Types |
24 | import Foreign.C.String(newCString) | 24 | import Foreign.C.String(newCString) |
25 | import System.IO.Unsafe(unsafePerformIO) | 25 | import System.IO.Unsafe(unsafePerformIO) |
26 | import System.Process(readProcess) | ||
26 | 27 | ||
27 | fromei x = fromIntegral (fromEnum x) :: CInt | 28 | fromei x = fromIntegral (fromEnum x) :: CInt |
28 | 29 | ||
29 | ----------------------------------------------------------------------- | 30 | ----------------------------------------------------------------------- |
30 | 31 | ||
32 | data 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. |
32 | randomVector :: Int -- ^ seed | 37 | randomVector :: Int -- ^ seed |
33 | -> RandDist -- ^ distribution | 38 | -> RandDist -- ^ distribution |
@@ -35,10 +40,10 @@ randomVector :: Int -- ^ seed | |||
35 | -> Vector Double | 40 | -> Vector Double |
36 | randomVector seed dist n = unsafePerformIO $ do | 41 | randomVector 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 | ||
41 | foreign import ccall unsafe "random_vector_GSL" c_random_vector_GSL :: CInt -> CInt -> TV | 46 | foreign 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 -- | |||
105 | type TV = CInt -> PD -> IO CInt -- | 110 | type TV = CInt -> PD -> IO CInt -- |
106 | type TM = CInt -> CInt -> PD -> IO CInt -- | 111 | type 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 | -} | ||
118 | fileDimensions :: FilePath -> IO (Int,Int) | ||
119 | fileDimensions 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. | ||
129 | loadMatrix :: FilePath -> IO (Matrix Double) | ||
130 | loadMatrix 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). | ||
133 | fromFile :: FilePath -> (Int,Int) -> IO (Matrix Double) | ||
134 | fromFile filename (r,c) = reshape c `fmap` fscanfVector filename (r*c) | ||
135 | |||