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