diff options
Diffstat (limited to 'packages/base/src/Numeric/Vectorized.hs')
-rw-r--r-- | packages/base/src/Numeric/Vectorized.hs | 365 |
1 files changed, 0 insertions, 365 deletions
diff --git a/packages/base/src/Numeric/Vectorized.hs b/packages/base/src/Numeric/Vectorized.hs deleted file mode 100644 index 6f0d240..0000000 --- a/packages/base/src/Numeric/Vectorized.hs +++ /dev/null | |||
@@ -1,365 +0,0 @@ | |||
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 | vectorScan, saveMatrix, | ||
21 | Seed, RandDist(..), randomVector, | ||
22 | sortVector, roundVector | ||
23 | ) where | ||
24 | |||
25 | import Data.Packed.Internal.Common | ||
26 | import Data.Packed.Internal.Signatures | ||
27 | import Data.Packed.Internal.Vector | ||
28 | import Data.Packed.Internal.Matrix | ||
29 | |||
30 | import Data.Complex | ||
31 | import Foreign.Marshal.Alloc(free,malloc) | ||
32 | import Foreign.Marshal.Array(newArray,copyArray) | ||
33 | import Foreign.Ptr(Ptr) | ||
34 | import Foreign.Storable(peek) | ||
35 | import Foreign.C.Types | ||
36 | import Foreign.C.String | ||
37 | import System.IO.Unsafe(unsafePerformIO) | ||
38 | |||
39 | import Control.Monad(when) | ||
40 | import Control.Applicative((<$>)) | ||
41 | |||
42 | |||
43 | |||
44 | fromei x = fromIntegral (fromEnum x) :: CInt | ||
45 | |||
46 | data FunCodeV = Sin | ||
47 | | Cos | ||
48 | | Tan | ||
49 | | Abs | ||
50 | | ASin | ||
51 | | ACos | ||
52 | | ATan | ||
53 | | Sinh | ||
54 | | Cosh | ||
55 | | Tanh | ||
56 | | ASinh | ||
57 | | ACosh | ||
58 | | ATanh | ||
59 | | Exp | ||
60 | | Log | ||
61 | | Sign | ||
62 | | Sqrt | ||
63 | deriving Enum | ||
64 | |||
65 | data FunCodeSV = Scale | ||
66 | | Recip | ||
67 | | AddConstant | ||
68 | | Negate | ||
69 | | PowSV | ||
70 | | PowVS | ||
71 | deriving Enum | ||
72 | |||
73 | data FunCodeVV = Add | ||
74 | | Sub | ||
75 | | Mul | ||
76 | | Div | ||
77 | | Pow | ||
78 | | ATan2 | ||
79 | deriving Enum | ||
80 | |||
81 | data FunCodeS = Norm2 | ||
82 | | AbsSum | ||
83 | | MaxIdx | ||
84 | | Max | ||
85 | | MinIdx | ||
86 | | Min | ||
87 | deriving Enum | ||
88 | |||
89 | ------------------------------------------------------------------ | ||
90 | |||
91 | -- | sum of elements | ||
92 | sumF :: Vector Float -> Float | ||
93 | sumF x = unsafePerformIO $ do | ||
94 | r <- createVector 1 | ||
95 | app2 c_sumF vec x vec r "sumF" | ||
96 | return $ r @> 0 | ||
97 | |||
98 | -- | sum of elements | ||
99 | sumR :: Vector Double -> Double | ||
100 | sumR x = unsafePerformIO $ do | ||
101 | r <- createVector 1 | ||
102 | app2 c_sumR vec x vec r "sumR" | ||
103 | return $ r @> 0 | ||
104 | |||
105 | -- | sum of elements | ||
106 | sumQ :: Vector (Complex Float) -> Complex Float | ||
107 | sumQ x = unsafePerformIO $ do | ||
108 | r <- createVector 1 | ||
109 | app2 c_sumQ vec x vec r "sumQ" | ||
110 | return $ r @> 0 | ||
111 | |||
112 | -- | sum of elements | ||
113 | sumC :: Vector (Complex Double) -> Complex Double | ||
114 | sumC x = unsafePerformIO $ do | ||
115 | r <- createVector 1 | ||
116 | app2 c_sumC vec x vec r "sumC" | ||
117 | return $ r @> 0 | ||
118 | |||
119 | foreign import ccall unsafe "sumF" c_sumF :: TFF | ||
120 | foreign import ccall unsafe "sumR" c_sumR :: TVV | ||
121 | foreign import ccall unsafe "sumQ" c_sumQ :: TQVQV | ||
122 | foreign import ccall unsafe "sumC" c_sumC :: TCVCV | ||
123 | |||
124 | -- | product of elements | ||
125 | prodF :: Vector Float -> Float | ||
126 | prodF x = unsafePerformIO $ do | ||
127 | r <- createVector 1 | ||
128 | app2 c_prodF vec x vec r "prodF" | ||
129 | return $ r @> 0 | ||
130 | |||
131 | -- | product of elements | ||
132 | prodR :: Vector Double -> Double | ||
133 | prodR x = unsafePerformIO $ do | ||
134 | r <- createVector 1 | ||
135 | app2 c_prodR vec x vec r "prodR" | ||
136 | return $ r @> 0 | ||
137 | |||
138 | -- | product of elements | ||
139 | prodQ :: Vector (Complex Float) -> Complex Float | ||
140 | prodQ x = unsafePerformIO $ do | ||
141 | r <- createVector 1 | ||
142 | app2 c_prodQ vec x vec r "prodQ" | ||
143 | return $ r @> 0 | ||
144 | |||
145 | -- | product of elements | ||
146 | prodC :: Vector (Complex Double) -> Complex Double | ||
147 | prodC x = unsafePerformIO $ do | ||
148 | r <- createVector 1 | ||
149 | app2 c_prodC vec x vec r "prodC" | ||
150 | return $ r @> 0 | ||
151 | |||
152 | foreign import ccall unsafe "prodF" c_prodF :: TFF | ||
153 | foreign import ccall unsafe "prodR" c_prodR :: TVV | ||
154 | foreign import ccall unsafe "prodQ" c_prodQ :: TQVQV | ||
155 | foreign import ccall unsafe "prodC" c_prodC :: TCVCV | ||
156 | |||
157 | ------------------------------------------------------------------ | ||
158 | |||
159 | toScalarAux fun code v = unsafePerformIO $ do | ||
160 | r <- createVector 1 | ||
161 | app2 (fun (fromei code)) vec v vec r "toScalarAux" | ||
162 | return (r `at` 0) | ||
163 | |||
164 | vectorMapAux fun code v = unsafePerformIO $ do | ||
165 | r <- createVector (dim v) | ||
166 | app2 (fun (fromei code)) vec v vec r "vectorMapAux" | ||
167 | return r | ||
168 | |||
169 | vectorMapValAux fun code val v = unsafePerformIO $ do | ||
170 | r <- createVector (dim v) | ||
171 | pval <- newArray [val] | ||
172 | app2 (fun (fromei code) pval) vec v vec r "vectorMapValAux" | ||
173 | free pval | ||
174 | return r | ||
175 | |||
176 | vectorZipAux fun code u v = unsafePerformIO $ do | ||
177 | r <- createVector (dim u) | ||
178 | app3 (fun (fromei code)) vec u vec v vec r "vectorZipAux" | ||
179 | return r | ||
180 | |||
181 | --------------------------------------------------------------------- | ||
182 | |||
183 | -- | obtains different functions of a vector: norm1, norm2, max, min, posmax, posmin, etc. | ||
184 | toScalarR :: FunCodeS -> Vector Double -> Double | ||
185 | toScalarR oper = toScalarAux c_toScalarR (fromei oper) | ||
186 | |||
187 | foreign import ccall unsafe "toScalarR" c_toScalarR :: CInt -> TVV | ||
188 | |||
189 | -- | obtains different functions of a vector: norm1, norm2, max, min, posmax, posmin, etc. | ||
190 | toScalarF :: FunCodeS -> Vector Float -> Float | ||
191 | toScalarF oper = toScalarAux c_toScalarF (fromei oper) | ||
192 | |||
193 | foreign import ccall unsafe "toScalarF" c_toScalarF :: CInt -> TFF | ||
194 | |||
195 | -- | obtains different functions of a vector: only norm1, norm2 | ||
196 | toScalarC :: FunCodeS -> Vector (Complex Double) -> Double | ||
197 | toScalarC oper = toScalarAux c_toScalarC (fromei oper) | ||
198 | |||
199 | foreign import ccall unsafe "toScalarC" c_toScalarC :: CInt -> TCVV | ||
200 | |||
201 | -- | obtains different functions of a vector: only norm1, norm2 | ||
202 | toScalarQ :: FunCodeS -> Vector (Complex Float) -> Float | ||
203 | toScalarQ oper = toScalarAux c_toScalarQ (fromei oper) | ||
204 | |||
205 | foreign import ccall unsafe "toScalarQ" c_toScalarQ :: CInt -> TQVF | ||
206 | |||
207 | ------------------------------------------------------------------ | ||
208 | |||
209 | -- | map of real vectors with given function | ||
210 | vectorMapR :: FunCodeV -> Vector Double -> Vector Double | ||
211 | vectorMapR = vectorMapAux c_vectorMapR | ||
212 | |||
213 | foreign import ccall unsafe "mapR" c_vectorMapR :: CInt -> TVV | ||
214 | |||
215 | -- | map of complex vectors with given function | ||
216 | vectorMapC :: FunCodeV -> Vector (Complex Double) -> Vector (Complex Double) | ||
217 | vectorMapC oper = vectorMapAux c_vectorMapC (fromei oper) | ||
218 | |||
219 | foreign import ccall unsafe "mapC" c_vectorMapC :: CInt -> TCVCV | ||
220 | |||
221 | -- | map of real vectors with given function | ||
222 | vectorMapF :: FunCodeV -> Vector Float -> Vector Float | ||
223 | vectorMapF = vectorMapAux c_vectorMapF | ||
224 | |||
225 | foreign import ccall unsafe "mapF" c_vectorMapF :: CInt -> TFF | ||
226 | |||
227 | -- | map of real vectors with given function | ||
228 | vectorMapQ :: FunCodeV -> Vector (Complex Float) -> Vector (Complex Float) | ||
229 | vectorMapQ = vectorMapAux c_vectorMapQ | ||
230 | |||
231 | foreign import ccall unsafe "mapQ" c_vectorMapQ :: CInt -> TQVQV | ||
232 | |||
233 | ------------------------------------------------------------------- | ||
234 | |||
235 | -- | map of real vectors with given function | ||
236 | vectorMapValR :: FunCodeSV -> Double -> Vector Double -> Vector Double | ||
237 | vectorMapValR oper = vectorMapValAux c_vectorMapValR (fromei oper) | ||
238 | |||
239 | foreign import ccall unsafe "mapValR" c_vectorMapValR :: CInt -> Ptr Double -> TVV | ||
240 | |||
241 | -- | map of complex vectors with given function | ||
242 | vectorMapValC :: FunCodeSV -> Complex Double -> Vector (Complex Double) -> Vector (Complex Double) | ||
243 | vectorMapValC = vectorMapValAux c_vectorMapValC | ||
244 | |||
245 | foreign import ccall unsafe "mapValC" c_vectorMapValC :: CInt -> Ptr (Complex Double) -> TCVCV | ||
246 | |||
247 | -- | map of real vectors with given function | ||
248 | vectorMapValF :: FunCodeSV -> Float -> Vector Float -> Vector Float | ||
249 | vectorMapValF oper = vectorMapValAux c_vectorMapValF (fromei oper) | ||
250 | |||
251 | foreign import ccall unsafe "mapValF" c_vectorMapValF :: CInt -> Ptr Float -> TFF | ||
252 | |||
253 | -- | map of complex vectors with given function | ||
254 | vectorMapValQ :: FunCodeSV -> Complex Float -> Vector (Complex Float) -> Vector (Complex Float) | ||
255 | vectorMapValQ oper = vectorMapValAux c_vectorMapValQ (fromei oper) | ||
256 | |||
257 | foreign import ccall unsafe "mapValQ" c_vectorMapValQ :: CInt -> Ptr (Complex Float) -> TQVQV | ||
258 | |||
259 | ------------------------------------------------------------------- | ||
260 | |||
261 | -- | elementwise operation on real vectors | ||
262 | vectorZipR :: FunCodeVV -> Vector Double -> Vector Double -> Vector Double | ||
263 | vectorZipR = vectorZipAux c_vectorZipR | ||
264 | |||
265 | foreign import ccall unsafe "zipR" c_vectorZipR :: CInt -> TVVV | ||
266 | |||
267 | -- | elementwise operation on complex vectors | ||
268 | vectorZipC :: FunCodeVV -> Vector (Complex Double) -> Vector (Complex Double) -> Vector (Complex Double) | ||
269 | vectorZipC = vectorZipAux c_vectorZipC | ||
270 | |||
271 | foreign import ccall unsafe "zipC" c_vectorZipC :: CInt -> TCVCVCV | ||
272 | |||
273 | -- | elementwise operation on real vectors | ||
274 | vectorZipF :: FunCodeVV -> Vector Float -> Vector Float -> Vector Float | ||
275 | vectorZipF = vectorZipAux c_vectorZipF | ||
276 | |||
277 | foreign import ccall unsafe "zipF" c_vectorZipF :: CInt -> TFFF | ||
278 | |||
279 | -- | elementwise operation on complex vectors | ||
280 | vectorZipQ :: FunCodeVV -> Vector (Complex Float) -> Vector (Complex Float) -> Vector (Complex Float) | ||
281 | vectorZipQ = vectorZipAux c_vectorZipQ | ||
282 | |||
283 | foreign import ccall unsafe "zipQ" c_vectorZipQ :: CInt -> TQVQVQV | ||
284 | |||
285 | -------------------------------------------------------------------------------- | ||
286 | |||
287 | foreign import ccall unsafe "vectorScan" c_vectorScan | ||
288 | :: CString -> Ptr CInt -> Ptr (Ptr Double) -> IO CInt | ||
289 | |||
290 | vectorScan :: FilePath -> IO (Vector Double) | ||
291 | vectorScan s = do | ||
292 | pp <- malloc | ||
293 | pn <- malloc | ||
294 | cs <- newCString s | ||
295 | ok <- c_vectorScan cs pn pp | ||
296 | when (not (ok == 0)) $ | ||
297 | error ("vectorScan: file \"" ++ s ++"\" not found") | ||
298 | n <- fromIntegral <$> peek pn | ||
299 | p <- peek pp | ||
300 | v <- createVector n | ||
301 | free pn | ||
302 | free cs | ||
303 | unsafeWith v $ \pv -> copyArray pv p n | ||
304 | free p | ||
305 | free pp | ||
306 | return v | ||
307 | |||
308 | -------------------------------------------------------------------------------- | ||
309 | |||
310 | foreign import ccall unsafe "saveMatrix" c_saveMatrix | ||
311 | :: CString -> CString -> TM | ||
312 | |||
313 | {- | save a matrix as a 2D ASCII table | ||
314 | -} | ||
315 | saveMatrix | ||
316 | :: FilePath | ||
317 | -> String -- ^ \"printf\" format (e.g. \"%.2f\", \"%g\", etc.) | ||
318 | -> Matrix Double | ||
319 | -> IO () | ||
320 | saveMatrix name format m = do | ||
321 | cname <- newCString name | ||
322 | cformat <- newCString format | ||
323 | app1 (c_saveMatrix cname cformat) mat m "saveMatrix" | ||
324 | free cname | ||
325 | free cformat | ||
326 | return () | ||
327 | |||
328 | -------------------------------------------------------------------------------- | ||
329 | |||
330 | type Seed = Int | ||
331 | |||
332 | data RandDist = Uniform -- ^ uniform distribution in [0,1) | ||
333 | | Gaussian -- ^ normal distribution with mean zero and standard deviation one | ||
334 | deriving Enum | ||
335 | |||
336 | -- | Obtains a vector of pseudorandom elements (use randomIO to get a random seed). | ||
337 | randomVector :: Seed | ||
338 | -> RandDist -- ^ distribution | ||
339 | -> Int -- ^ vector size | ||
340 | -> Vector Double | ||
341 | randomVector seed dist n = unsafePerformIO $ do | ||
342 | r <- createVector n | ||
343 | app1 (c_random_vector (fi seed) ((fi.fromEnum) dist)) vec r "randomVector" | ||
344 | return r | ||
345 | |||
346 | foreign import ccall unsafe "random_vector" c_random_vector :: CInt -> CInt -> TV | ||
347 | |||
348 | -------------------------------------------------------------------------------- | ||
349 | |||
350 | sortVector v = unsafePerformIO $ do | ||
351 | r <- createVector (dim v) | ||
352 | app2 c_sort_values vec v vec r "sortVector" | ||
353 | return r | ||
354 | |||
355 | foreign import ccall unsafe "sort_values" c_sort_values :: TVV | ||
356 | |||
357 | -------------------------------------------------------------------------------- | ||
358 | |||
359 | roundVector v = unsafePerformIO $ do | ||
360 | r <- createVector (dim v) | ||
361 | app2 c_round_vector vec v vec r "roundVector" | ||
362 | return r | ||
363 | |||
364 | foreign import ccall unsafe "round_vector" c_round_vector :: TVV | ||
365 | |||