summaryrefslogtreecommitdiff
path: root/packages/base/src/Numeric/Vectorized.hs
diff options
context:
space:
mode:
Diffstat (limited to 'packages/base/src/Numeric/Vectorized.hs')
-rw-r--r--packages/base/src/Numeric/Vectorized.hs365
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
13module 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
25import Data.Packed.Internal.Common
26import Data.Packed.Internal.Signatures
27import Data.Packed.Internal.Vector
28import Data.Packed.Internal.Matrix
29
30import Data.Complex
31import Foreign.Marshal.Alloc(free,malloc)
32import Foreign.Marshal.Array(newArray,copyArray)
33import Foreign.Ptr(Ptr)
34import Foreign.Storable(peek)
35import Foreign.C.Types
36import Foreign.C.String
37import System.IO.Unsafe(unsafePerformIO)
38
39import Control.Monad(when)
40import Control.Applicative((<$>))
41
42
43
44fromei x = fromIntegral (fromEnum x) :: CInt
45
46data 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
65data FunCodeSV = Scale
66 | Recip
67 | AddConstant
68 | Negate
69 | PowSV
70 | PowVS
71 deriving Enum
72
73data FunCodeVV = Add
74 | Sub
75 | Mul
76 | Div
77 | Pow
78 | ATan2
79 deriving Enum
80
81data FunCodeS = Norm2
82 | AbsSum
83 | MaxIdx
84 | Max
85 | MinIdx
86 | Min
87 deriving Enum
88
89------------------------------------------------------------------
90
91-- | sum of elements
92sumF :: Vector Float -> Float
93sumF 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
99sumR :: Vector Double -> Double
100sumR 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
106sumQ :: Vector (Complex Float) -> Complex Float
107sumQ 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
113sumC :: Vector (Complex Double) -> Complex Double
114sumC x = unsafePerformIO $ do
115 r <- createVector 1
116 app2 c_sumC vec x vec r "sumC"
117 return $ r @> 0
118
119foreign import ccall unsafe "sumF" c_sumF :: TFF
120foreign import ccall unsafe "sumR" c_sumR :: TVV
121foreign import ccall unsafe "sumQ" c_sumQ :: TQVQV
122foreign import ccall unsafe "sumC" c_sumC :: TCVCV
123
124-- | product of elements
125prodF :: Vector Float -> Float
126prodF 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
132prodR :: Vector Double -> Double
133prodR 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
139prodQ :: Vector (Complex Float) -> Complex Float
140prodQ 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
146prodC :: Vector (Complex Double) -> Complex Double
147prodC x = unsafePerformIO $ do
148 r <- createVector 1
149 app2 c_prodC vec x vec r "prodC"
150 return $ r @> 0
151
152foreign import ccall unsafe "prodF" c_prodF :: TFF
153foreign import ccall unsafe "prodR" c_prodR :: TVV
154foreign import ccall unsafe "prodQ" c_prodQ :: TQVQV
155foreign import ccall unsafe "prodC" c_prodC :: TCVCV
156
157------------------------------------------------------------------
158
159toScalarAux 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
164vectorMapAux fun code v = unsafePerformIO $ do
165 r <- createVector (dim v)
166 app2 (fun (fromei code)) vec v vec r "vectorMapAux"
167 return r
168
169vectorMapValAux 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
176vectorZipAux 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.
184toScalarR :: FunCodeS -> Vector Double -> Double
185toScalarR oper = toScalarAux c_toScalarR (fromei oper)
186
187foreign import ccall unsafe "toScalarR" c_toScalarR :: CInt -> TVV
188
189-- | obtains different functions of a vector: norm1, norm2, max, min, posmax, posmin, etc.
190toScalarF :: FunCodeS -> Vector Float -> Float
191toScalarF oper = toScalarAux c_toScalarF (fromei oper)
192
193foreign import ccall unsafe "toScalarF" c_toScalarF :: CInt -> TFF
194
195-- | obtains different functions of a vector: only norm1, norm2
196toScalarC :: FunCodeS -> Vector (Complex Double) -> Double
197toScalarC oper = toScalarAux c_toScalarC (fromei oper)
198
199foreign import ccall unsafe "toScalarC" c_toScalarC :: CInt -> TCVV
200
201-- | obtains different functions of a vector: only norm1, norm2
202toScalarQ :: FunCodeS -> Vector (Complex Float) -> Float
203toScalarQ oper = toScalarAux c_toScalarQ (fromei oper)
204
205foreign import ccall unsafe "toScalarQ" c_toScalarQ :: CInt -> TQVF
206
207------------------------------------------------------------------
208
209-- | map of real vectors with given function
210vectorMapR :: FunCodeV -> Vector Double -> Vector Double
211vectorMapR = vectorMapAux c_vectorMapR
212
213foreign import ccall unsafe "mapR" c_vectorMapR :: CInt -> TVV
214
215-- | map of complex vectors with given function
216vectorMapC :: FunCodeV -> Vector (Complex Double) -> Vector (Complex Double)
217vectorMapC oper = vectorMapAux c_vectorMapC (fromei oper)
218
219foreign import ccall unsafe "mapC" c_vectorMapC :: CInt -> TCVCV
220
221-- | map of real vectors with given function
222vectorMapF :: FunCodeV -> Vector Float -> Vector Float
223vectorMapF = vectorMapAux c_vectorMapF
224
225foreign import ccall unsafe "mapF" c_vectorMapF :: CInt -> TFF
226
227-- | map of real vectors with given function
228vectorMapQ :: FunCodeV -> Vector (Complex Float) -> Vector (Complex Float)
229vectorMapQ = vectorMapAux c_vectorMapQ
230
231foreign import ccall unsafe "mapQ" c_vectorMapQ :: CInt -> TQVQV
232
233-------------------------------------------------------------------
234
235-- | map of real vectors with given function
236vectorMapValR :: FunCodeSV -> Double -> Vector Double -> Vector Double
237vectorMapValR oper = vectorMapValAux c_vectorMapValR (fromei oper)
238
239foreign import ccall unsafe "mapValR" c_vectorMapValR :: CInt -> Ptr Double -> TVV
240
241-- | map of complex vectors with given function
242vectorMapValC :: FunCodeSV -> Complex Double -> Vector (Complex Double) -> Vector (Complex Double)
243vectorMapValC = vectorMapValAux c_vectorMapValC
244
245foreign import ccall unsafe "mapValC" c_vectorMapValC :: CInt -> Ptr (Complex Double) -> TCVCV
246
247-- | map of real vectors with given function
248vectorMapValF :: FunCodeSV -> Float -> Vector Float -> Vector Float
249vectorMapValF oper = vectorMapValAux c_vectorMapValF (fromei oper)
250
251foreign import ccall unsafe "mapValF" c_vectorMapValF :: CInt -> Ptr Float -> TFF
252
253-- | map of complex vectors with given function
254vectorMapValQ :: FunCodeSV -> Complex Float -> Vector (Complex Float) -> Vector (Complex Float)
255vectorMapValQ oper = vectorMapValAux c_vectorMapValQ (fromei oper)
256
257foreign import ccall unsafe "mapValQ" c_vectorMapValQ :: CInt -> Ptr (Complex Float) -> TQVQV
258
259-------------------------------------------------------------------
260
261-- | elementwise operation on real vectors
262vectorZipR :: FunCodeVV -> Vector Double -> Vector Double -> Vector Double
263vectorZipR = vectorZipAux c_vectorZipR
264
265foreign import ccall unsafe "zipR" c_vectorZipR :: CInt -> TVVV
266
267-- | elementwise operation on complex vectors
268vectorZipC :: FunCodeVV -> Vector (Complex Double) -> Vector (Complex Double) -> Vector (Complex Double)
269vectorZipC = vectorZipAux c_vectorZipC
270
271foreign import ccall unsafe "zipC" c_vectorZipC :: CInt -> TCVCVCV
272
273-- | elementwise operation on real vectors
274vectorZipF :: FunCodeVV -> Vector Float -> Vector Float -> Vector Float
275vectorZipF = vectorZipAux c_vectorZipF
276
277foreign import ccall unsafe "zipF" c_vectorZipF :: CInt -> TFFF
278
279-- | elementwise operation on complex vectors
280vectorZipQ :: FunCodeVV -> Vector (Complex Float) -> Vector (Complex Float) -> Vector (Complex Float)
281vectorZipQ = vectorZipAux c_vectorZipQ
282
283foreign import ccall unsafe "zipQ" c_vectorZipQ :: CInt -> TQVQVQV
284
285--------------------------------------------------------------------------------
286
287foreign import ccall unsafe "vectorScan" c_vectorScan
288 :: CString -> Ptr CInt -> Ptr (Ptr Double) -> IO CInt
289
290vectorScan :: FilePath -> IO (Vector Double)
291vectorScan 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
310foreign import ccall unsafe "saveMatrix" c_saveMatrix
311 :: CString -> CString -> TM
312
313{- | save a matrix as a 2D ASCII table
314-}
315saveMatrix
316 :: FilePath
317 -> String -- ^ \"printf\" format (e.g. \"%.2f\", \"%g\", etc.)
318 -> Matrix Double
319 -> IO ()
320saveMatrix 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
330type Seed = Int
331
332data 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).
337randomVector :: Seed
338 -> RandDist -- ^ distribution
339 -> Int -- ^ vector size
340 -> Vector Double
341randomVector 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
346foreign import ccall unsafe "random_vector" c_random_vector :: CInt -> CInt -> TV
347
348--------------------------------------------------------------------------------
349
350sortVector v = unsafePerformIO $ do
351 r <- createVector (dim v)
352 app2 c_sort_values vec v vec r "sortVector"
353 return r
354
355foreign import ccall unsafe "sort_values" c_sort_values :: TVV
356
357--------------------------------------------------------------------------------
358
359roundVector v = unsafePerformIO $ do
360 r <- createVector (dim v)
361 app2 c_round_vector vec v vec r "roundVector"
362 return r
363
364foreign import ccall unsafe "round_vector" c_round_vector :: TVV
365