diff options
Diffstat (limited to 'packages/hmatrix/src/Numeric/GSL/LinearAlgebra.hs')
-rw-r--r-- | packages/hmatrix/src/Numeric/GSL/LinearAlgebra.hs | 135 |
1 files changed, 0 insertions, 135 deletions
diff --git a/packages/hmatrix/src/Numeric/GSL/LinearAlgebra.hs b/packages/hmatrix/src/Numeric/GSL/LinearAlgebra.hs deleted file mode 100644 index 17e2258..0000000 --- a/packages/hmatrix/src/Numeric/GSL/LinearAlgebra.hs +++ /dev/null | |||
@@ -1,135 +0,0 @@ | |||
1 | ----------------------------------------------------------------------------- | ||
2 | -- | | ||
3 | -- Module : Numeric.GSL.LinearAlgebra | ||
4 | -- Copyright : (c) Alberto Ruiz 2007-14 | ||
5 | -- License : GPL | ||
6 | -- Maintainer : Alberto Ruiz | ||
7 | -- Stability : provisional | ||
8 | -- | ||
9 | ----------------------------------------------------------------------------- | ||
10 | |||
11 | module Numeric.GSL.LinearAlgebra ( | ||
12 | RandDist(..), randomVector, | ||
13 | saveMatrix, | ||
14 | fwriteVector, freadVector, fprintfVector, fscanfVector, | ||
15 | fileDimensions, loadMatrix, fromFile | ||
16 | ) where | ||
17 | |||
18 | import Data.Packed | ||
19 | import Numeric.GSL.Internal hiding (TV,TM,TCV,TCM) | ||
20 | |||
21 | import Foreign.Marshal.Alloc(free) | ||
22 | import Foreign.Ptr(Ptr) | ||
23 | import Foreign.C.Types | ||
24 | import Foreign.C.String(newCString) | ||
25 | import System.IO.Unsafe(unsafePerformIO) | ||
26 | import System.Process(readProcess) | ||
27 | |||
28 | fromei x = fromIntegral (fromEnum x) :: CInt | ||
29 | |||
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 | |||
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. | ||
37 | randomVector :: Int -- ^ seed | ||
38 | -> RandDist -- ^ distribution | ||
39 | -> Int -- ^ vector size | ||
40 | -> Vector Double | ||
41 | randomVector seed dist n = unsafePerformIO $ do | ||
42 | r <- createVector n | ||
43 | app1 (c_random_vector (fi seed) ((fi.fromEnum) dist)) vec r "randomVector" | ||
44 | return r | ||
45 | |||
46 | foreign import ccall unsafe "random_vector" c_random_vector :: CInt -> CInt -> TV | ||
47 | |||
48 | -------------------------------------------------------------------------------- | ||
49 | |||
50 | -- | Saves a matrix as 2D ASCII table. | ||
51 | saveMatrix :: FilePath | ||
52 | -> String -- ^ format (%f, %g, %e) | ||
53 | -> Matrix Double | ||
54 | -> IO () | ||
55 | saveMatrix filename fmt m = do | ||
56 | charname <- newCString filename | ||
57 | charfmt <- newCString fmt | ||
58 | let o = if orderOf m == RowMajor then 1 else 0 | ||
59 | app1 (matrix_fprintf charname charfmt o) mat m "matrix_fprintf" | ||
60 | free charname | ||
61 | free charfmt | ||
62 | |||
63 | foreign import ccall unsafe "matrix_fprintf" matrix_fprintf :: Ptr CChar -> Ptr CChar -> CInt -> TM | ||
64 | |||
65 | -------------------------------------------------------------------------------- | ||
66 | |||
67 | -- | Loads a vector from an ASCII file (the number of elements must be known in advance). | ||
68 | fscanfVector :: FilePath -> Int -> IO (Vector Double) | ||
69 | fscanfVector filename n = do | ||
70 | charname <- newCString filename | ||
71 | res <- createVector n | ||
72 | app1 (gsl_vector_fscanf charname) vec res "gsl_vector_fscanf" | ||
73 | free charname | ||
74 | return res | ||
75 | |||
76 | foreign import ccall unsafe "vector_fscanf" gsl_vector_fscanf:: Ptr CChar -> TV | ||
77 | |||
78 | -- | Saves the elements of a vector, with a given format (%f, %e, %g), to an ASCII file. | ||
79 | fprintfVector :: FilePath -> String -> Vector Double -> IO () | ||
80 | fprintfVector filename fmt v = do | ||
81 | charname <- newCString filename | ||
82 | charfmt <- newCString fmt | ||
83 | app1 (gsl_vector_fprintf charname charfmt) vec v "gsl_vector_fprintf" | ||
84 | free charname | ||
85 | free charfmt | ||
86 | |||
87 | foreign import ccall unsafe "vector_fprintf" gsl_vector_fprintf :: Ptr CChar -> Ptr CChar -> TV | ||
88 | |||
89 | -- | Loads a vector from a binary file (the number of elements must be known in advance). | ||
90 | freadVector :: FilePath -> Int -> IO (Vector Double) | ||
91 | freadVector filename n = do | ||
92 | charname <- newCString filename | ||
93 | res <- createVector n | ||
94 | app1 (gsl_vector_fread charname) vec res "gsl_vector_fread" | ||
95 | free charname | ||
96 | return res | ||
97 | |||
98 | foreign import ccall unsafe "vector_fread" gsl_vector_fread:: Ptr CChar -> TV | ||
99 | |||
100 | -- | Saves the elements of a vector to a binary file. | ||
101 | fwriteVector :: FilePath -> Vector Double -> IO () | ||
102 | fwriteVector filename v = do | ||
103 | charname <- newCString filename | ||
104 | app1 (gsl_vector_fwrite charname) vec v "gsl_vector_fwrite" | ||
105 | free charname | ||
106 | |||
107 | foreign import ccall unsafe "vector_fwrite" gsl_vector_fwrite :: Ptr CChar -> TV | ||
108 | |||
109 | type PD = Ptr Double -- | ||
110 | type TV = CInt -> PD -> IO CInt -- | ||
111 | type TM = CInt -> CInt -> PD -> IO CInt -- | ||
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 | |||