summaryrefslogtreecommitdiff
path: root/lib/Numeric/GSL/Vector.hs
diff options
context:
space:
mode:
authorAlberto Ruiz <aruiz@um.es>2014-05-08 08:48:12 +0200
committerAlberto Ruiz <aruiz@um.es>2014-05-08 08:48:12 +0200
commit1925c123d7d8184a1d2ddc0a413e0fd2776e1083 (patch)
treefad79f909d9c3be53d68e6ebd67202650536d387 /lib/Numeric/GSL/Vector.hs
parenteb3f702d065a4a967bb754977233e6eec408fd1f (diff)
empty hmatrix-base
Diffstat (limited to 'lib/Numeric/GSL/Vector.hs')
-rw-r--r--lib/Numeric/GSL/Vector.hs328
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
15module 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
26import Data.Packed.Internal.Common
27import Data.Packed.Internal.Signatures
28import Data.Packed.Internal.Vector
29
30import Data.Complex
31import Foreign.Marshal.Alloc(free)
32import Foreign.Marshal.Array(newArray)
33import Foreign.Ptr(Ptr)
34import Foreign.C.Types
35import System.IO.Unsafe(unsafePerformIO)
36import Control.Monad(when)
37
38fromei x = fromIntegral (fromEnum x) :: CInt
39
40data 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
59data FunCodeSV = Scale
60 | Recip
61 | AddConstant
62 | Negate
63 | PowSV
64 | PowVS
65 deriving Enum
66
67data FunCodeVV = Add
68 | Sub
69 | Mul
70 | Div
71 | Pow
72 | ATan2
73 deriving Enum
74
75data FunCodeS = Norm2
76 | AbsSum
77 | MaxIdx
78 | Max
79 | MinIdx
80 | Min
81 deriving Enum
82
83------------------------------------------------------------------
84
85-- | sum of elements
86sumF :: Vector Float -> Float
87sumF 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
93sumR :: Vector Double -> Double
94sumR 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
100sumQ :: Vector (Complex Float) -> Complex Float
101sumQ 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
107sumC :: Vector (Complex Double) -> Complex Double
108sumC x = unsafePerformIO $ do
109 r <- createVector 1
110 app2 c_sumC vec x vec r "sumC"
111 return $ r @> 0
112
113foreign import ccall unsafe "gsl-aux.h sumF" c_sumF :: TFF
114foreign import ccall unsafe "gsl-aux.h sumR" c_sumR :: TVV
115foreign import ccall unsafe "gsl-aux.h sumQ" c_sumQ :: TQVQV
116foreign import ccall unsafe "gsl-aux.h sumC" c_sumC :: TCVCV
117
118-- | product of elements
119prodF :: Vector Float -> Float
120prodF 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
126prodR :: Vector Double -> Double
127prodR 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
133prodQ :: Vector (Complex Float) -> Complex Float
134prodQ 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
140prodC :: Vector (Complex Double) -> Complex Double
141prodC x = unsafePerformIO $ do
142 r <- createVector 1
143 app2 c_prodC vec x vec r "prodC"
144 return $ r @> 0
145
146foreign import ccall unsafe "gsl-aux.h prodF" c_prodF :: TFF
147foreign import ccall unsafe "gsl-aux.h prodR" c_prodR :: TVV
148foreign import ccall unsafe "gsl-aux.h prodQ" c_prodQ :: TQVQV
149foreign import ccall unsafe "gsl-aux.h prodC" c_prodC :: TCVCV
150
151-- | dot product
152dotF :: Vector Float -> Vector Float -> Float
153dotF 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
159dotR :: Vector Double -> Vector Double -> Double
160dotR 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
166dotQ :: Vector (Complex Float) -> Vector (Complex Float) -> Complex Float
167dotQ 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
173dotC :: Vector (Complex Double) -> Vector (Complex Double) -> Complex Double
174dotC x y = unsafePerformIO $ do
175 r <- createVector 1
176 app3 c_dotC vec x vec y vec r "dotC"
177 return $ r @> 0
178
179foreign import ccall unsafe "gsl-aux.h dotF" c_dotF :: TFFF
180foreign import ccall unsafe "gsl-aux.h dotR" c_dotR :: TVVV
181foreign import ccall unsafe "gsl-aux.h dotQ" c_dotQ :: TQVQVQV
182foreign import ccall unsafe "gsl-aux.h dotC" c_dotC :: TCVCVCV
183
184------------------------------------------------------------------
185
186toScalarAux 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
191vectorMapAux fun code v = unsafePerformIO $ do
192 r <- createVector (dim v)
193 app2 (fun (fromei code)) vec v vec r "vectorMapAux"
194 return r
195
196vectorMapValAux 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
203vectorZipAux 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.
211toScalarR :: FunCodeS -> Vector Double -> Double
212toScalarR oper = toScalarAux c_toScalarR (fromei oper)
213
214foreign 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.
217toScalarF :: FunCodeS -> Vector Float -> Float
218toScalarF oper = toScalarAux c_toScalarF (fromei oper)
219
220foreign import ccall unsafe "gsl-aux.h toScalarF" c_toScalarF :: CInt -> TFF
221
222-- | obtains different functions of a vector: only norm1, norm2
223toScalarC :: FunCodeS -> Vector (Complex Double) -> Double
224toScalarC oper = toScalarAux c_toScalarC (fromei oper)
225
226foreign import ccall unsafe "gsl-aux.h toScalarC" c_toScalarC :: CInt -> TCVV
227
228-- | obtains different functions of a vector: only norm1, norm2
229toScalarQ :: FunCodeS -> Vector (Complex Float) -> Float
230toScalarQ oper = toScalarAux c_toScalarQ (fromei oper)
231
232foreign import ccall unsafe "gsl-aux.h toScalarQ" c_toScalarQ :: CInt -> TQVF
233
234------------------------------------------------------------------
235
236-- | map of real vectors with given function
237vectorMapR :: FunCodeV -> Vector Double -> Vector Double
238vectorMapR = vectorMapAux c_vectorMapR
239
240foreign import ccall unsafe "gsl-aux.h mapR" c_vectorMapR :: CInt -> TVV
241
242-- | map of complex vectors with given function
243vectorMapC :: FunCodeV -> Vector (Complex Double) -> Vector (Complex Double)
244vectorMapC oper = vectorMapAux c_vectorMapC (fromei oper)
245
246foreign import ccall unsafe "gsl-aux.h mapC" c_vectorMapC :: CInt -> TCVCV
247
248-- | map of real vectors with given function
249vectorMapF :: FunCodeV -> Vector Float -> Vector Float
250vectorMapF = vectorMapAux c_vectorMapF
251
252foreign import ccall unsafe "gsl-aux.h mapF" c_vectorMapF :: CInt -> TFF
253
254-- | map of real vectors with given function
255vectorMapQ :: FunCodeV -> Vector (Complex Float) -> Vector (Complex Float)
256vectorMapQ = vectorMapAux c_vectorMapQ
257
258foreign import ccall unsafe "gsl-aux.h mapQ" c_vectorMapQ :: CInt -> TQVQV
259
260-------------------------------------------------------------------
261
262-- | map of real vectors with given function
263vectorMapValR :: FunCodeSV -> Double -> Vector Double -> Vector Double
264vectorMapValR oper = vectorMapValAux c_vectorMapValR (fromei oper)
265
266foreign import ccall unsafe "gsl-aux.h mapValR" c_vectorMapValR :: CInt -> Ptr Double -> TVV
267
268-- | map of complex vectors with given function
269vectorMapValC :: FunCodeSV -> Complex Double -> Vector (Complex Double) -> Vector (Complex Double)
270vectorMapValC = vectorMapValAux c_vectorMapValC
271
272foreign import ccall unsafe "gsl-aux.h mapValC" c_vectorMapValC :: CInt -> Ptr (Complex Double) -> TCVCV
273
274-- | map of real vectors with given function
275vectorMapValF :: FunCodeSV -> Float -> Vector Float -> Vector Float
276vectorMapValF oper = vectorMapValAux c_vectorMapValF (fromei oper)
277
278foreign import ccall unsafe "gsl-aux.h mapValF" c_vectorMapValF :: CInt -> Ptr Float -> TFF
279
280-- | map of complex vectors with given function
281vectorMapValQ :: FunCodeSV -> Complex Float -> Vector (Complex Float) -> Vector (Complex Float)
282vectorMapValQ oper = vectorMapValAux c_vectorMapValQ (fromei oper)
283
284foreign import ccall unsafe "gsl-aux.h mapValQ" c_vectorMapValQ :: CInt -> Ptr (Complex Float) -> TQVQV
285
286-------------------------------------------------------------------
287
288-- | elementwise operation on real vectors
289vectorZipR :: FunCodeVV -> Vector Double -> Vector Double -> Vector Double
290vectorZipR = vectorZipAux c_vectorZipR
291
292foreign import ccall unsafe "gsl-aux.h zipR" c_vectorZipR :: CInt -> TVVV
293
294-- | elementwise operation on complex vectors
295vectorZipC :: FunCodeVV -> Vector (Complex Double) -> Vector (Complex Double) -> Vector (Complex Double)
296vectorZipC = vectorZipAux c_vectorZipC
297
298foreign import ccall unsafe "gsl-aux.h zipC" c_vectorZipC :: CInt -> TCVCVCV
299
300-- | elementwise operation on real vectors
301vectorZipF :: FunCodeVV -> Vector Float -> Vector Float -> Vector Float
302vectorZipF = vectorZipAux c_vectorZipF
303
304foreign import ccall unsafe "gsl-aux.h zipF" c_vectorZipF :: CInt -> TFFF
305
306-- | elementwise operation on complex vectors
307vectorZipQ :: FunCodeVV -> Vector (Complex Float) -> Vector (Complex Float) -> Vector (Complex Float)
308vectorZipQ = vectorZipAux c_vectorZipQ
309
310foreign import ccall unsafe "gsl-aux.h zipQ" c_vectorZipQ :: CInt -> TQVQVQV
311
312-----------------------------------------------------------------------
313
314data 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.
319randomVector :: Int -- ^ seed
320 -> RandDist -- ^ distribution
321 -> Int -- ^ vector size
322 -> Vector Double
323randomVector 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
328foreign import ccall unsafe "random_vector" c_random_vector :: CInt -> CInt -> TV