diff options
author | Alberto Ruiz <aruiz@um.es> | 2014-05-16 20:10:57 +0200 |
---|---|---|
committer | Alberto Ruiz <aruiz@um.es> | 2014-05-16 20:10:57 +0200 |
commit | d4d9082a8d7d3eed6cb5f188fc3b476847dcac27 (patch) | |
tree | 0a19bc18be429454fd59c16fd46e29e1e7bae722 /packages/hmatrix/src/Numeric/GSL/LinearAlgebra.hs | |
parent | 51f4cc7b4b301142b8df73568ffaa448f9e6dd50 (diff) |
GSL.LinearAlgebra reorganized
Diffstat (limited to 'packages/hmatrix/src/Numeric/GSL/LinearAlgebra.hs')
-rw-r--r-- | packages/hmatrix/src/Numeric/GSL/LinearAlgebra.hs | 160 |
1 files changed, 160 insertions, 0 deletions
diff --git a/packages/hmatrix/src/Numeric/GSL/LinearAlgebra.hs b/packages/hmatrix/src/Numeric/GSL/LinearAlgebra.hs new file mode 100644 index 0000000..43e46c5 --- /dev/null +++ b/packages/hmatrix/src/Numeric/GSL/LinearAlgebra.hs | |||
@@ -0,0 +1,160 @@ | |||
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 | ) where | ||
16 | |||
17 | import Data.Packed | ||
18 | import Numeric.GSL.Internal hiding (TV,TM,TCV,TCM) | ||
19 | |||
20 | import Data.Complex | ||
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 | |||
27 | fromei x = fromIntegral (fromEnum x) :: CInt | ||
28 | |||
29 | ----------------------------------------------------------------------- | ||
30 | |||
31 | data RandDist = Uniform -- ^ uniform distribution in [0,1) | ||
32 | | Gaussian -- ^ normal distribution with mean zero and standard deviation one | ||
33 | deriving Enum | ||
34 | |||
35 | -- | 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 | randomVector :: Int -- ^ seed | ||
37 | -> RandDist -- ^ distribution | ||
38 | -> Int -- ^ vector size | ||
39 | -> Vector Double | ||
40 | randomVector seed dist n = unsafePerformIO $ do | ||
41 | r <- createVector n | ||
42 | app1 (c_random_vector (fi seed) ((fi.fromEnum) dist)) vec r "randomVector" | ||
43 | return r | ||
44 | |||
45 | foreign import ccall unsafe "random_vector" c_random_vector :: CInt -> CInt -> TV | ||
46 | |||
47 | -------------------------------------------------------------------------------- | ||
48 | |||
49 | -- | Saves a matrix as 2D ASCII table. | ||
50 | saveMatrix :: FilePath | ||
51 | -> String -- ^ format (%f, %g, %e) | ||
52 | -> Matrix Double | ||
53 | -> IO () | ||
54 | saveMatrix filename fmt m = do | ||
55 | charname <- newCString filename | ||
56 | charfmt <- newCString fmt | ||
57 | let o = if orderOf m == RowMajor then 1 else 0 | ||
58 | app1 (matrix_fprintf charname charfmt o) mat m "matrix_fprintf" | ||
59 | free charname | ||
60 | free charfmt | ||
61 | |||
62 | foreign import ccall unsafe "matrix_fprintf" matrix_fprintf :: Ptr CChar -> Ptr CChar -> CInt -> TM | ||
63 | |||
64 | -------------------------------------------------------------------------------- | ||
65 | |||
66 | -- | Loads a vector from an ASCII file (the number of elements must be known in advance). | ||
67 | fscanfVector :: FilePath -> Int -> IO (Vector Double) | ||
68 | fscanfVector filename n = do | ||
69 | charname <- newCString filename | ||
70 | res <- createVector n | ||
71 | app1 (gsl_vector_fscanf charname) vec res "gsl_vector_fscanf" | ||
72 | free charname | ||
73 | return res | ||
74 | |||
75 | foreign import ccall unsafe "vector_fscanf" gsl_vector_fscanf:: Ptr CChar -> TV | ||
76 | |||
77 | -- | Saves the elements of a vector, with a given format (%f, %e, %g), to an ASCII file. | ||
78 | fprintfVector :: FilePath -> String -> Vector Double -> IO () | ||
79 | fprintfVector filename fmt v = do | ||
80 | charname <- newCString filename | ||
81 | charfmt <- newCString fmt | ||
82 | app1 (gsl_vector_fprintf charname charfmt) vec v "gsl_vector_fprintf" | ||
83 | free charname | ||
84 | free charfmt | ||
85 | |||
86 | foreign import ccall unsafe "vector_fprintf" gsl_vector_fprintf :: Ptr CChar -> Ptr CChar -> TV | ||
87 | |||
88 | -- | Loads a vector from a binary file (the number of elements must be known in advance). | ||
89 | freadVector :: FilePath -> Int -> IO (Vector Double) | ||
90 | freadVector filename n = do | ||
91 | charname <- newCString filename | ||
92 | res <- createVector n | ||
93 | app1 (gsl_vector_fread charname) vec res "gsl_vector_fread" | ||
94 | free charname | ||
95 | return res | ||
96 | |||
97 | foreign import ccall unsafe "vector_fread" gsl_vector_fread:: Ptr CChar -> TV | ||
98 | |||
99 | -- | Saves the elements of a vector to a binary file. | ||
100 | fwriteVector :: FilePath -> Vector Double -> IO () | ||
101 | fwriteVector filename v = do | ||
102 | charname <- newCString filename | ||
103 | app1 (gsl_vector_fwrite charname) vec v "gsl_vector_fwrite" | ||
104 | free charname | ||
105 | |||
106 | foreign import ccall unsafe "vector_fwrite" gsl_vector_fwrite :: Ptr CChar -> TV | ||
107 | |||
108 | type PF = Ptr Float -- | ||
109 | type PD = Ptr Double -- | ||
110 | type PQ = Ptr (Complex Float) -- | ||
111 | type PC = Ptr (Complex Double) -- | ||
112 | type TF = CInt -> PF -> IO CInt -- | ||
113 | type TFF = CInt -> PF -> TF -- | ||
114 | type TFV = CInt -> PF -> TV -- | ||
115 | type TVF = CInt -> PD -> TF -- | ||
116 | type TFFF = CInt -> PF -> TFF -- | ||
117 | type TV = CInt -> PD -> IO CInt -- | ||
118 | type TVV = CInt -> PD -> TV -- | ||
119 | type TVVV = CInt -> PD -> TVV -- | ||
120 | type TFM = CInt -> CInt -> PF -> IO CInt -- | ||
121 | type TFMFM = CInt -> CInt -> PF -> TFM -- | ||
122 | type TFMFMFM = CInt -> CInt -> PF -> TFMFM -- | ||
123 | type TM = CInt -> CInt -> PD -> IO CInt -- | ||
124 | type TMM = CInt -> CInt -> PD -> TM -- | ||
125 | type TVMM = CInt -> PD -> TMM -- | ||
126 | type TMVMM = CInt -> CInt -> PD -> TVMM -- | ||
127 | type TMMM = CInt -> CInt -> PD -> TMM -- | ||
128 | type TVM = CInt -> PD -> TM -- | ||
129 | type TVVM = CInt -> PD -> TVM -- | ||
130 | type TMV = CInt -> CInt -> PD -> TV -- | ||
131 | type TMMV = CInt -> CInt -> PD -> TMV -- | ||
132 | type TMVM = CInt -> CInt -> PD -> TVM -- | ||
133 | type TMMVM = CInt -> CInt -> PD -> TMVM -- | ||
134 | type TCM = CInt -> CInt -> PC -> IO CInt -- | ||
135 | type TCVCM = CInt -> PC -> TCM -- | ||
136 | type TCMCVCM = CInt -> CInt -> PC -> TCVCM -- | ||
137 | type TMCMCVCM = CInt -> CInt -> PD -> TCMCVCM -- | ||
138 | type TCMCMCVCM = CInt -> CInt -> PC -> TCMCVCM -- | ||
139 | type TCMCM = CInt -> CInt -> PC -> TCM -- | ||
140 | type TVCM = CInt -> PD -> TCM -- | ||
141 | type TCMVCM = CInt -> CInt -> PC -> TVCM -- | ||
142 | type TCMCMVCM = CInt -> CInt -> PC -> TCMVCM -- | ||
143 | type TCMCMCM = CInt -> CInt -> PC -> TCMCM -- | ||
144 | type TCV = CInt -> PC -> IO CInt -- | ||
145 | type TCVCV = CInt -> PC -> TCV -- | ||
146 | type TCVCVCV = CInt -> PC -> TCVCV -- | ||
147 | type TCVV = CInt -> PC -> TV -- | ||
148 | type TQV = CInt -> PQ -> IO CInt -- | ||
149 | type TQVQV = CInt -> PQ -> TQV -- | ||
150 | type TQVQVQV = CInt -> PQ -> TQVQV -- | ||
151 | type TQVF = CInt -> PQ -> TF -- | ||
152 | type TQM = CInt -> CInt -> PQ -> IO CInt -- | ||
153 | type TQMQM = CInt -> CInt -> PQ -> TQM -- | ||
154 | type TQMQMQM = CInt -> CInt -> PQ -> TQMQM -- | ||
155 | type TCMCV = CInt -> CInt -> PC -> TCV -- | ||
156 | type TVCV = CInt -> PD -> TCV -- | ||
157 | type TCVM = CInt -> PC -> TM -- | ||
158 | type TMCVM = CInt -> CInt -> PD -> TCVM -- | ||
159 | type TMMCVM = CInt -> CInt -> PD -> TMCVM -- | ||
160 | |||