diff options
author | Alberto Ruiz <aruiz@um.es> | 2014-05-08 08:48:12 +0200 |
---|---|---|
committer | Alberto Ruiz <aruiz@um.es> | 2014-05-08 08:48:12 +0200 |
commit | 1925c123d7d8184a1d2ddc0a413e0fd2776e1083 (patch) | |
tree | fad79f909d9c3be53d68e6ebd67202650536d387 /lib/Numeric/GSL/Vector.hs | |
parent | eb3f702d065a4a967bb754977233e6eec408fd1f (diff) |
empty hmatrix-base
Diffstat (limited to 'lib/Numeric/GSL/Vector.hs')
-rw-r--r-- | lib/Numeric/GSL/Vector.hs | 328 |
1 files changed, 0 insertions, 328 deletions
diff --git a/lib/Numeric/GSL/Vector.hs b/lib/Numeric/GSL/Vector.hs deleted file mode 100644 index 6204b8e..0000000 --- a/lib/Numeric/GSL/Vector.hs +++ /dev/null | |||
@@ -1,328 +0,0 @@ | |||
1 | ----------------------------------------------------------------------------- | ||
2 | -- | | ||
3 | -- Module : Numeric.GSL.Vector | ||
4 | -- Copyright : (c) Alberto Ruiz 2007 | ||
5 | -- License : GPL-style | ||
6 | -- | ||
7 | -- Maintainer : Alberto Ruiz <aruiz@um.es> | ||
8 | -- Stability : provisional | ||
9 | -- Portability : portable (uses FFI) | ||
10 | -- | ||
11 | -- Low level interface to vector operations. | ||
12 | -- | ||
13 | ----------------------------------------------------------------------------- | ||
14 | |||
15 | module Numeric.GSL.Vector ( | ||
16 | sumF, sumR, sumQ, sumC, | ||
17 | prodF, prodR, prodQ, prodC, | ||
18 | dotF, dotR, dotQ, dotC, | ||
19 | FunCodeS(..), toScalarR, toScalarF, toScalarC, toScalarQ, | ||
20 | FunCodeV(..), vectorMapR, vectorMapC, vectorMapF, vectorMapQ, | ||
21 | FunCodeSV(..), vectorMapValR, vectorMapValC, vectorMapValF, vectorMapValQ, | ||
22 | FunCodeVV(..), vectorZipR, vectorZipC, vectorZipF, vectorZipQ, | ||
23 | RandDist(..), randomVector | ||
24 | ) where | ||
25 | |||
26 | import Data.Packed.Internal.Common | ||
27 | import Data.Packed.Internal.Signatures | ||
28 | import Data.Packed.Internal.Vector | ||
29 | |||
30 | import Data.Complex | ||
31 | import Foreign.Marshal.Alloc(free) | ||
32 | import Foreign.Marshal.Array(newArray) | ||
33 | import Foreign.Ptr(Ptr) | ||
34 | import Foreign.C.Types | ||
35 | import System.IO.Unsafe(unsafePerformIO) | ||
36 | import Control.Monad(when) | ||
37 | |||
38 | fromei x = fromIntegral (fromEnum x) :: CInt | ||
39 | |||
40 | data FunCodeV = Sin | ||
41 | | Cos | ||
42 | | Tan | ||
43 | | Abs | ||
44 | | ASin | ||
45 | | ACos | ||
46 | | ATan | ||
47 | | Sinh | ||
48 | | Cosh | ||
49 | | Tanh | ||
50 | | ASinh | ||
51 | | ACosh | ||
52 | | ATanh | ||
53 | | Exp | ||
54 | | Log | ||
55 | | Sign | ||
56 | | Sqrt | ||
57 | deriving Enum | ||
58 | |||
59 | data FunCodeSV = Scale | ||
60 | | Recip | ||
61 | | AddConstant | ||
62 | | Negate | ||
63 | | PowSV | ||
64 | | PowVS | ||
65 | deriving Enum | ||
66 | |||
67 | data FunCodeVV = Add | ||
68 | | Sub | ||
69 | | Mul | ||
70 | | Div | ||
71 | | Pow | ||
72 | | ATan2 | ||
73 | deriving Enum | ||
74 | |||
75 | data FunCodeS = Norm2 | ||
76 | | AbsSum | ||
77 | | MaxIdx | ||
78 | | Max | ||
79 | | MinIdx | ||
80 | | Min | ||
81 | deriving Enum | ||
82 | |||
83 | ------------------------------------------------------------------ | ||
84 | |||
85 | -- | sum of elements | ||
86 | sumF :: Vector Float -> Float | ||
87 | sumF x = unsafePerformIO $ do | ||
88 | r <- createVector 1 | ||
89 | app2 c_sumF vec x vec r "sumF" | ||
90 | return $ r @> 0 | ||
91 | |||
92 | -- | sum of elements | ||
93 | sumR :: Vector Double -> Double | ||
94 | sumR x = unsafePerformIO $ do | ||
95 | r <- createVector 1 | ||
96 | app2 c_sumR vec x vec r "sumR" | ||
97 | return $ r @> 0 | ||
98 | |||
99 | -- | sum of elements | ||
100 | sumQ :: Vector (Complex Float) -> Complex Float | ||
101 | sumQ x = unsafePerformIO $ do | ||
102 | r <- createVector 1 | ||
103 | app2 c_sumQ vec x vec r "sumQ" | ||
104 | return $ r @> 0 | ||
105 | |||
106 | -- | sum of elements | ||
107 | sumC :: Vector (Complex Double) -> Complex Double | ||
108 | sumC x = unsafePerformIO $ do | ||
109 | r <- createVector 1 | ||
110 | app2 c_sumC vec x vec r "sumC" | ||
111 | return $ r @> 0 | ||
112 | |||
113 | foreign import ccall unsafe "gsl-aux.h sumF" c_sumF :: TFF | ||
114 | foreign import ccall unsafe "gsl-aux.h sumR" c_sumR :: TVV | ||
115 | foreign import ccall unsafe "gsl-aux.h sumQ" c_sumQ :: TQVQV | ||
116 | foreign import ccall unsafe "gsl-aux.h sumC" c_sumC :: TCVCV | ||
117 | |||
118 | -- | product of elements | ||
119 | prodF :: Vector Float -> Float | ||
120 | prodF x = unsafePerformIO $ do | ||
121 | r <- createVector 1 | ||
122 | app2 c_prodF vec x vec r "prodF" | ||
123 | return $ r @> 0 | ||
124 | |||
125 | -- | product of elements | ||
126 | prodR :: Vector Double -> Double | ||
127 | prodR x = unsafePerformIO $ do | ||
128 | r <- createVector 1 | ||
129 | app2 c_prodR vec x vec r "prodR" | ||
130 | return $ r @> 0 | ||
131 | |||
132 | -- | product of elements | ||
133 | prodQ :: Vector (Complex Float) -> Complex Float | ||
134 | prodQ x = unsafePerformIO $ do | ||
135 | r <- createVector 1 | ||
136 | app2 c_prodQ vec x vec r "prodQ" | ||
137 | return $ r @> 0 | ||
138 | |||
139 | -- | product of elements | ||
140 | prodC :: Vector (Complex Double) -> Complex Double | ||
141 | prodC x = unsafePerformIO $ do | ||
142 | r <- createVector 1 | ||
143 | app2 c_prodC vec x vec r "prodC" | ||
144 | return $ r @> 0 | ||
145 | |||
146 | foreign import ccall unsafe "gsl-aux.h prodF" c_prodF :: TFF | ||
147 | foreign import ccall unsafe "gsl-aux.h prodR" c_prodR :: TVV | ||
148 | foreign import ccall unsafe "gsl-aux.h prodQ" c_prodQ :: TQVQV | ||
149 | foreign import ccall unsafe "gsl-aux.h prodC" c_prodC :: TCVCV | ||
150 | |||
151 | -- | dot product | ||
152 | dotF :: Vector Float -> Vector Float -> Float | ||
153 | dotF x y = unsafePerformIO $ do | ||
154 | r <- createVector 1 | ||
155 | app3 c_dotF vec x vec y vec r "dotF" | ||
156 | return $ r @> 0 | ||
157 | |||
158 | -- | dot product | ||
159 | dotR :: Vector Double -> Vector Double -> Double | ||
160 | dotR x y = unsafePerformIO $ do | ||
161 | r <- createVector 1 | ||
162 | app3 c_dotR vec x vec y vec r "dotR" | ||
163 | return $ r @> 0 | ||
164 | |||
165 | -- | dot product | ||
166 | dotQ :: Vector (Complex Float) -> Vector (Complex Float) -> Complex Float | ||
167 | dotQ x y = unsafePerformIO $ do | ||
168 | r <- createVector 1 | ||
169 | app3 c_dotQ vec x vec y vec r "dotQ" | ||
170 | return $ r @> 0 | ||
171 | |||
172 | -- | dot product | ||
173 | dotC :: Vector (Complex Double) -> Vector (Complex Double) -> Complex Double | ||
174 | dotC x y = unsafePerformIO $ do | ||
175 | r <- createVector 1 | ||
176 | app3 c_dotC vec x vec y vec r "dotC" | ||
177 | return $ r @> 0 | ||
178 | |||
179 | foreign import ccall unsafe "gsl-aux.h dotF" c_dotF :: TFFF | ||
180 | foreign import ccall unsafe "gsl-aux.h dotR" c_dotR :: TVVV | ||
181 | foreign import ccall unsafe "gsl-aux.h dotQ" c_dotQ :: TQVQVQV | ||
182 | foreign import ccall unsafe "gsl-aux.h dotC" c_dotC :: TCVCVCV | ||
183 | |||
184 | ------------------------------------------------------------------ | ||
185 | |||
186 | toScalarAux fun code v = unsafePerformIO $ do | ||
187 | r <- createVector 1 | ||
188 | app2 (fun (fromei code)) vec v vec r "toScalarAux" | ||
189 | return (r `at` 0) | ||
190 | |||
191 | vectorMapAux fun code v = unsafePerformIO $ do | ||
192 | r <- createVector (dim v) | ||
193 | app2 (fun (fromei code)) vec v vec r "vectorMapAux" | ||
194 | return r | ||
195 | |||
196 | vectorMapValAux fun code val v = unsafePerformIO $ do | ||
197 | r <- createVector (dim v) | ||
198 | pval <- newArray [val] | ||
199 | app2 (fun (fromei code) pval) vec v vec r "vectorMapValAux" | ||
200 | free pval | ||
201 | return r | ||
202 | |||
203 | vectorZipAux fun code u v = unsafePerformIO $ do | ||
204 | r <- createVector (dim u) | ||
205 | when (dim u > 0) $ app3 (fun (fromei code)) vec u vec v vec r "vectorZipAux" | ||
206 | return r | ||
207 | |||
208 | --------------------------------------------------------------------- | ||
209 | |||
210 | -- | obtains different functions of a vector: norm1, norm2, max, min, posmax, posmin, etc. | ||
211 | toScalarR :: FunCodeS -> Vector Double -> Double | ||
212 | toScalarR oper = toScalarAux c_toScalarR (fromei oper) | ||
213 | |||
214 | foreign import ccall unsafe "gsl-aux.h toScalarR" c_toScalarR :: CInt -> TVV | ||
215 | |||
216 | -- | obtains different functions of a vector: norm1, norm2, max, min, posmax, posmin, etc. | ||
217 | toScalarF :: FunCodeS -> Vector Float -> Float | ||
218 | toScalarF oper = toScalarAux c_toScalarF (fromei oper) | ||
219 | |||
220 | foreign import ccall unsafe "gsl-aux.h toScalarF" c_toScalarF :: CInt -> TFF | ||
221 | |||
222 | -- | obtains different functions of a vector: only norm1, norm2 | ||
223 | toScalarC :: FunCodeS -> Vector (Complex Double) -> Double | ||
224 | toScalarC oper = toScalarAux c_toScalarC (fromei oper) | ||
225 | |||
226 | foreign import ccall unsafe "gsl-aux.h toScalarC" c_toScalarC :: CInt -> TCVV | ||
227 | |||
228 | -- | obtains different functions of a vector: only norm1, norm2 | ||
229 | toScalarQ :: FunCodeS -> Vector (Complex Float) -> Float | ||
230 | toScalarQ oper = toScalarAux c_toScalarQ (fromei oper) | ||
231 | |||
232 | foreign import ccall unsafe "gsl-aux.h toScalarQ" c_toScalarQ :: CInt -> TQVF | ||
233 | |||
234 | ------------------------------------------------------------------ | ||
235 | |||
236 | -- | map of real vectors with given function | ||
237 | vectorMapR :: FunCodeV -> Vector Double -> Vector Double | ||
238 | vectorMapR = vectorMapAux c_vectorMapR | ||
239 | |||
240 | foreign import ccall unsafe "gsl-aux.h mapR" c_vectorMapR :: CInt -> TVV | ||
241 | |||
242 | -- | map of complex vectors with given function | ||
243 | vectorMapC :: FunCodeV -> Vector (Complex Double) -> Vector (Complex Double) | ||
244 | vectorMapC oper = vectorMapAux c_vectorMapC (fromei oper) | ||
245 | |||
246 | foreign import ccall unsafe "gsl-aux.h mapC" c_vectorMapC :: CInt -> TCVCV | ||
247 | |||
248 | -- | map of real vectors with given function | ||
249 | vectorMapF :: FunCodeV -> Vector Float -> Vector Float | ||
250 | vectorMapF = vectorMapAux c_vectorMapF | ||
251 | |||
252 | foreign import ccall unsafe "gsl-aux.h mapF" c_vectorMapF :: CInt -> TFF | ||
253 | |||
254 | -- | map of real vectors with given function | ||
255 | vectorMapQ :: FunCodeV -> Vector (Complex Float) -> Vector (Complex Float) | ||
256 | vectorMapQ = vectorMapAux c_vectorMapQ | ||
257 | |||
258 | foreign import ccall unsafe "gsl-aux.h mapQ" c_vectorMapQ :: CInt -> TQVQV | ||
259 | |||
260 | ------------------------------------------------------------------- | ||
261 | |||
262 | -- | map of real vectors with given function | ||
263 | vectorMapValR :: FunCodeSV -> Double -> Vector Double -> Vector Double | ||
264 | vectorMapValR oper = vectorMapValAux c_vectorMapValR (fromei oper) | ||
265 | |||
266 | foreign import ccall unsafe "gsl-aux.h mapValR" c_vectorMapValR :: CInt -> Ptr Double -> TVV | ||
267 | |||
268 | -- | map of complex vectors with given function | ||
269 | vectorMapValC :: FunCodeSV -> Complex Double -> Vector (Complex Double) -> Vector (Complex Double) | ||
270 | vectorMapValC = vectorMapValAux c_vectorMapValC | ||
271 | |||
272 | foreign import ccall unsafe "gsl-aux.h mapValC" c_vectorMapValC :: CInt -> Ptr (Complex Double) -> TCVCV | ||
273 | |||
274 | -- | map of real vectors with given function | ||
275 | vectorMapValF :: FunCodeSV -> Float -> Vector Float -> Vector Float | ||
276 | vectorMapValF oper = vectorMapValAux c_vectorMapValF (fromei oper) | ||
277 | |||
278 | foreign import ccall unsafe "gsl-aux.h mapValF" c_vectorMapValF :: CInt -> Ptr Float -> TFF | ||
279 | |||
280 | -- | map of complex vectors with given function | ||
281 | vectorMapValQ :: FunCodeSV -> Complex Float -> Vector (Complex Float) -> Vector (Complex Float) | ||
282 | vectorMapValQ oper = vectorMapValAux c_vectorMapValQ (fromei oper) | ||
283 | |||
284 | foreign import ccall unsafe "gsl-aux.h mapValQ" c_vectorMapValQ :: CInt -> Ptr (Complex Float) -> TQVQV | ||
285 | |||
286 | ------------------------------------------------------------------- | ||
287 | |||
288 | -- | elementwise operation on real vectors | ||
289 | vectorZipR :: FunCodeVV -> Vector Double -> Vector Double -> Vector Double | ||
290 | vectorZipR = vectorZipAux c_vectorZipR | ||
291 | |||
292 | foreign import ccall unsafe "gsl-aux.h zipR" c_vectorZipR :: CInt -> TVVV | ||
293 | |||
294 | -- | elementwise operation on complex vectors | ||
295 | vectorZipC :: FunCodeVV -> Vector (Complex Double) -> Vector (Complex Double) -> Vector (Complex Double) | ||
296 | vectorZipC = vectorZipAux c_vectorZipC | ||
297 | |||
298 | foreign import ccall unsafe "gsl-aux.h zipC" c_vectorZipC :: CInt -> TCVCVCV | ||
299 | |||
300 | -- | elementwise operation on real vectors | ||
301 | vectorZipF :: FunCodeVV -> Vector Float -> Vector Float -> Vector Float | ||
302 | vectorZipF = vectorZipAux c_vectorZipF | ||
303 | |||
304 | foreign import ccall unsafe "gsl-aux.h zipF" c_vectorZipF :: CInt -> TFFF | ||
305 | |||
306 | -- | elementwise operation on complex vectors | ||
307 | vectorZipQ :: FunCodeVV -> Vector (Complex Float) -> Vector (Complex Float) -> Vector (Complex Float) | ||
308 | vectorZipQ = vectorZipAux c_vectorZipQ | ||
309 | |||
310 | foreign import ccall unsafe "gsl-aux.h zipQ" c_vectorZipQ :: CInt -> TQVQVQV | ||
311 | |||
312 | ----------------------------------------------------------------------- | ||
313 | |||
314 | data RandDist = Uniform -- ^ uniform distribution in [0,1) | ||
315 | | Gaussian -- ^ normal distribution with mean zero and standard deviation one | ||
316 | deriving Enum | ||
317 | |||
318 | -- | Obtains a vector of pseudorandom elements from the the mt19937 generator in GSL, with a given seed. Use randomIO to get a random seed. | ||
319 | randomVector :: Int -- ^ seed | ||
320 | -> RandDist -- ^ distribution | ||
321 | -> Int -- ^ vector size | ||
322 | -> Vector Double | ||
323 | randomVector seed dist n = unsafePerformIO $ do | ||
324 | r <- createVector n | ||
325 | app1 (c_random_vector (fi seed) ((fi.fromEnum) dist)) vec r "randomVector" | ||
326 | return r | ||
327 | |||
328 | foreign import ccall unsafe "random_vector" c_random_vector :: CInt -> CInt -> TV | ||