diff options
author | Alberto Ruiz <aruiz@um.es> | 2014-05-14 19:46:44 +0200 |
---|---|---|
committer | Alberto Ruiz <aruiz@um.es> | 2014-05-14 19:46:44 +0200 |
commit | 0f9575462eb37a7c9985583ca33ae315f6e6431d (patch) | |
tree | c8f4c6519c1a4b6969e4ae80fed9e410b852f7d7 /packages/base/src/Numeric/Vectorized.hs | |
parent | 494fe8f3e66f52244c4e0aedfd00eb15b9caceef (diff) |
vectorized operations in base
Diffstat (limited to 'packages/base/src/Numeric/Vectorized.hs')
-rw-r--r-- | packages/base/src/Numeric/Vectorized.hs | 273 |
1 files changed, 273 insertions, 0 deletions
diff --git a/packages/base/src/Numeric/Vectorized.hs b/packages/base/src/Numeric/Vectorized.hs new file mode 100644 index 0000000..3814579 --- /dev/null +++ b/packages/base/src/Numeric/Vectorized.hs | |||
@@ -0,0 +1,273 @@ | |||
1 | ----------------------------------------------------------------------------- | ||
2 | -- | | ||
3 | -- Module : Numeric.Vectorized | ||
4 | -- Copyright : (c) Alberto Ruiz 2007-14 | ||
5 | -- License : BSD3 | ||
6 | -- Maintainer : Alberto Ruiz | ||
7 | -- Stability : provisional | ||
8 | -- | ||
9 | -- Low level interface to vector operations. | ||
10 | -- | ||
11 | ----------------------------------------------------------------------------- | ||
12 | |||
13 | module Numeric.Vectorized ( | ||
14 | sumF, sumR, sumQ, sumC, | ||
15 | prodF, prodR, prodQ, prodC, | ||
16 | FunCodeS(..), toScalarR, toScalarF, toScalarC, toScalarQ, | ||
17 | FunCodeV(..), vectorMapR, vectorMapC, vectorMapF, vectorMapQ, | ||
18 | FunCodeSV(..), vectorMapValR, vectorMapValC, vectorMapValF, vectorMapValQ, | ||
19 | FunCodeVV(..), vectorZipR, vectorZipC, vectorZipF, vectorZipQ | ||
20 | ) where | ||
21 | |||
22 | import Data.Packed.Internal.Common | ||
23 | import Data.Packed.Internal.Signatures | ||
24 | import Data.Packed.Internal.Vector | ||
25 | |||
26 | import Data.Complex | ||
27 | import Foreign.Marshal.Alloc(free) | ||
28 | import Foreign.Marshal.Array(newArray) | ||
29 | import Foreign.Ptr(Ptr) | ||
30 | import Foreign.C.Types | ||
31 | import System.IO.Unsafe(unsafePerformIO) | ||
32 | |||
33 | fromei x = fromIntegral (fromEnum x) :: CInt | ||
34 | |||
35 | data FunCodeV = Sin | ||
36 | | Cos | ||
37 | | Tan | ||
38 | | Abs | ||
39 | | ASin | ||
40 | | ACos | ||
41 | | ATan | ||
42 | | Sinh | ||
43 | | Cosh | ||
44 | | Tanh | ||
45 | | ASinh | ||
46 | | ACosh | ||
47 | | ATanh | ||
48 | | Exp | ||
49 | | Log | ||
50 | | Sign | ||
51 | | Sqrt | ||
52 | deriving Enum | ||
53 | |||
54 | data FunCodeSV = Scale | ||
55 | | Recip | ||
56 | | AddConstant | ||
57 | | Negate | ||
58 | | PowSV | ||
59 | | PowVS | ||
60 | deriving Enum | ||
61 | |||
62 | data FunCodeVV = Add | ||
63 | | Sub | ||
64 | | Mul | ||
65 | | Div | ||
66 | | Pow | ||
67 | | ATan2 | ||
68 | deriving Enum | ||
69 | |||
70 | data FunCodeS = Norm2 | ||
71 | | AbsSum | ||
72 | | MaxIdx | ||
73 | | Max | ||
74 | | MinIdx | ||
75 | | Min | ||
76 | deriving Enum | ||
77 | |||
78 | ------------------------------------------------------------------ | ||
79 | |||
80 | -- | sum of elements | ||
81 | sumF :: Vector Float -> Float | ||
82 | sumF x = unsafePerformIO $ do | ||
83 | r <- createVector 1 | ||
84 | app2 c_sumF vec x vec r "sumF" | ||
85 | return $ r @> 0 | ||
86 | |||
87 | -- | sum of elements | ||
88 | sumR :: Vector Double -> Double | ||
89 | sumR x = unsafePerformIO $ do | ||
90 | r <- createVector 1 | ||
91 | app2 c_sumR vec x vec r "sumR" | ||
92 | return $ r @> 0 | ||
93 | |||
94 | -- | sum of elements | ||
95 | sumQ :: Vector (Complex Float) -> Complex Float | ||
96 | sumQ x = unsafePerformIO $ do | ||
97 | r <- createVector 1 | ||
98 | app2 c_sumQ vec x vec r "sumQ" | ||
99 | return $ r @> 0 | ||
100 | |||
101 | -- | sum of elements | ||
102 | sumC :: Vector (Complex Double) -> Complex Double | ||
103 | sumC x = unsafePerformIO $ do | ||
104 | r <- createVector 1 | ||
105 | app2 c_sumC vec x vec r "sumC" | ||
106 | return $ r @> 0 | ||
107 | |||
108 | foreign import ccall unsafe "sumF" c_sumF :: TFF | ||
109 | foreign import ccall unsafe "sumR" c_sumR :: TVV | ||
110 | foreign import ccall unsafe "sumQ" c_sumQ :: TQVQV | ||
111 | foreign import ccall unsafe "sumC" c_sumC :: TCVCV | ||
112 | |||
113 | -- | product of elements | ||
114 | prodF :: Vector Float -> Float | ||
115 | prodF x = unsafePerformIO $ do | ||
116 | r <- createVector 1 | ||
117 | app2 c_prodF vec x vec r "prodF" | ||
118 | return $ r @> 0 | ||
119 | |||
120 | -- | product of elements | ||
121 | prodR :: Vector Double -> Double | ||
122 | prodR x = unsafePerformIO $ do | ||
123 | r <- createVector 1 | ||
124 | app2 c_prodR vec x vec r "prodR" | ||
125 | return $ r @> 0 | ||
126 | |||
127 | -- | product of elements | ||
128 | prodQ :: Vector (Complex Float) -> Complex Float | ||
129 | prodQ x = unsafePerformIO $ do | ||
130 | r <- createVector 1 | ||
131 | app2 c_prodQ vec x vec r "prodQ" | ||
132 | return $ r @> 0 | ||
133 | |||
134 | -- | product of elements | ||
135 | prodC :: Vector (Complex Double) -> Complex Double | ||
136 | prodC x = unsafePerformIO $ do | ||
137 | r <- createVector 1 | ||
138 | app2 c_prodC vec x vec r "prodC" | ||
139 | return $ r @> 0 | ||
140 | |||
141 | foreign import ccall unsafe "prodF" c_prodF :: TFF | ||
142 | foreign import ccall unsafe "prodR" c_prodR :: TVV | ||
143 | foreign import ccall unsafe "prodQ" c_prodQ :: TQVQV | ||
144 | foreign import ccall unsafe "prodC" c_prodC :: TCVCV | ||
145 | |||
146 | ------------------------------------------------------------------ | ||
147 | |||
148 | toScalarAux fun code v = unsafePerformIO $ do | ||
149 | r <- createVector 1 | ||
150 | app2 (fun (fromei code)) vec v vec r "toScalarAux" | ||
151 | return (r `at` 0) | ||
152 | |||
153 | vectorMapAux fun code v = unsafePerformIO $ do | ||
154 | r <- createVector (dim v) | ||
155 | app2 (fun (fromei code)) vec v vec r "vectorMapAux" | ||
156 | return r | ||
157 | |||
158 | vectorMapValAux fun code val v = unsafePerformIO $ do | ||
159 | r <- createVector (dim v) | ||
160 | pval <- newArray [val] | ||
161 | app2 (fun (fromei code) pval) vec v vec r "vectorMapValAux" | ||
162 | free pval | ||
163 | return r | ||
164 | |||
165 | vectorZipAux fun code u v = unsafePerformIO $ do | ||
166 | r <- createVector (dim u) | ||
167 | app3 (fun (fromei code)) vec u vec v vec r "vectorZipAux" | ||
168 | return r | ||
169 | |||
170 | --------------------------------------------------------------------- | ||
171 | |||
172 | -- | obtains different functions of a vector: norm1, norm2, max, min, posmax, posmin, etc. | ||
173 | toScalarR :: FunCodeS -> Vector Double -> Double | ||
174 | toScalarR oper = toScalarAux c_toScalarR (fromei oper) | ||
175 | |||
176 | foreign import ccall unsafe "toScalarR" c_toScalarR :: CInt -> TVV | ||
177 | |||
178 | -- | obtains different functions of a vector: norm1, norm2, max, min, posmax, posmin, etc. | ||
179 | toScalarF :: FunCodeS -> Vector Float -> Float | ||
180 | toScalarF oper = toScalarAux c_toScalarF (fromei oper) | ||
181 | |||
182 | foreign import ccall unsafe "toScalarF" c_toScalarF :: CInt -> TFF | ||
183 | |||
184 | -- | obtains different functions of a vector: only norm1, norm2 | ||
185 | toScalarC :: FunCodeS -> Vector (Complex Double) -> Double | ||
186 | toScalarC oper = toScalarAux c_toScalarC (fromei oper) | ||
187 | |||
188 | foreign import ccall unsafe "toScalarC" c_toScalarC :: CInt -> TCVV | ||
189 | |||
190 | -- | obtains different functions of a vector: only norm1, norm2 | ||
191 | toScalarQ :: FunCodeS -> Vector (Complex Float) -> Float | ||
192 | toScalarQ oper = toScalarAux c_toScalarQ (fromei oper) | ||
193 | |||
194 | foreign import ccall unsafe "toScalarQ" c_toScalarQ :: CInt -> TQVF | ||
195 | |||
196 | ------------------------------------------------------------------ | ||
197 | |||
198 | -- | map of real vectors with given function | ||
199 | vectorMapR :: FunCodeV -> Vector Double -> Vector Double | ||
200 | vectorMapR = vectorMapAux c_vectorMapR | ||
201 | |||
202 | foreign import ccall unsafe "mapR" c_vectorMapR :: CInt -> TVV | ||
203 | |||
204 | -- | map of complex vectors with given function | ||
205 | vectorMapC :: FunCodeV -> Vector (Complex Double) -> Vector (Complex Double) | ||
206 | vectorMapC oper = vectorMapAux c_vectorMapC (fromei oper) | ||
207 | |||
208 | foreign import ccall unsafe "mapC" c_vectorMapC :: CInt -> TCVCV | ||
209 | |||
210 | -- | map of real vectors with given function | ||
211 | vectorMapF :: FunCodeV -> Vector Float -> Vector Float | ||
212 | vectorMapF = vectorMapAux c_vectorMapF | ||
213 | |||
214 | foreign import ccall unsafe "mapF" c_vectorMapF :: CInt -> TFF | ||
215 | |||
216 | -- | map of real vectors with given function | ||
217 | vectorMapQ :: FunCodeV -> Vector (Complex Float) -> Vector (Complex Float) | ||
218 | vectorMapQ = vectorMapAux c_vectorMapQ | ||
219 | |||
220 | foreign import ccall unsafe "mapQ" c_vectorMapQ :: CInt -> TQVQV | ||
221 | |||
222 | ------------------------------------------------------------------- | ||
223 | |||
224 | -- | map of real vectors with given function | ||
225 | vectorMapValR :: FunCodeSV -> Double -> Vector Double -> Vector Double | ||
226 | vectorMapValR oper = vectorMapValAux c_vectorMapValR (fromei oper) | ||
227 | |||
228 | foreign import ccall unsafe "mapValR" c_vectorMapValR :: CInt -> Ptr Double -> TVV | ||
229 | |||
230 | -- | map of complex vectors with given function | ||
231 | vectorMapValC :: FunCodeSV -> Complex Double -> Vector (Complex Double) -> Vector (Complex Double) | ||
232 | vectorMapValC = vectorMapValAux c_vectorMapValC | ||
233 | |||
234 | foreign import ccall unsafe "mapValC" c_vectorMapValC :: CInt -> Ptr (Complex Double) -> TCVCV | ||
235 | |||
236 | -- | map of real vectors with given function | ||
237 | vectorMapValF :: FunCodeSV -> Float -> Vector Float -> Vector Float | ||
238 | vectorMapValF oper = vectorMapValAux c_vectorMapValF (fromei oper) | ||
239 | |||
240 | foreign import ccall unsafe "mapValF" c_vectorMapValF :: CInt -> Ptr Float -> TFF | ||
241 | |||
242 | -- | map of complex vectors with given function | ||
243 | vectorMapValQ :: FunCodeSV -> Complex Float -> Vector (Complex Float) -> Vector (Complex Float) | ||
244 | vectorMapValQ oper = vectorMapValAux c_vectorMapValQ (fromei oper) | ||
245 | |||
246 | foreign import ccall unsafe "mapValQ" c_vectorMapValQ :: CInt -> Ptr (Complex Float) -> TQVQV | ||
247 | |||
248 | ------------------------------------------------------------------- | ||
249 | |||
250 | -- | elementwise operation on real vectors | ||
251 | vectorZipR :: FunCodeVV -> Vector Double -> Vector Double -> Vector Double | ||
252 | vectorZipR = vectorZipAux c_vectorZipR | ||
253 | |||
254 | foreign import ccall unsafe "zipR" c_vectorZipR :: CInt -> TVVV | ||
255 | |||
256 | -- | elementwise operation on complex vectors | ||
257 | vectorZipC :: FunCodeVV -> Vector (Complex Double) -> Vector (Complex Double) -> Vector (Complex Double) | ||
258 | vectorZipC = vectorZipAux c_vectorZipC | ||
259 | |||
260 | foreign import ccall unsafe "zipC" c_vectorZipC :: CInt -> TCVCVCV | ||
261 | |||
262 | -- | elementwise operation on real vectors | ||
263 | vectorZipF :: FunCodeVV -> Vector Float -> Vector Float -> Vector Float | ||
264 | vectorZipF = vectorZipAux c_vectorZipF | ||
265 | |||
266 | foreign import ccall unsafe "zipF" c_vectorZipF :: CInt -> TFFF | ||
267 | |||
268 | -- | elementwise operation on complex vectors | ||
269 | vectorZipQ :: FunCodeVV -> Vector (Complex Float) -> Vector (Complex Float) -> Vector (Complex Float) | ||
270 | vectorZipQ = vectorZipAux c_vectorZipQ | ||
271 | |||
272 | foreign import ccall unsafe "zipQ" c_vectorZipQ :: CInt -> TQVQVQV | ||
273 | |||