diff options
Diffstat (limited to 'packages/base/src/Numeric')
-rw-r--r-- | packages/base/src/Numeric/Vector.hs | 8 | ||||
-rw-r--r-- | packages/base/src/Numeric/Vectorized.hs | 105 |
2 files changed, 74 insertions, 39 deletions
diff --git a/packages/base/src/Numeric/Vector.hs b/packages/base/src/Numeric/Vector.hs index 1c16871..6cac5dc 100644 --- a/packages/base/src/Numeric/Vector.hs +++ b/packages/base/src/Numeric/Vector.hs | |||
@@ -32,6 +32,14 @@ adaptScalar f1 f2 f3 x y | |||
32 | 32 | ||
33 | ------------------------------------------------------------------ | 33 | ------------------------------------------------------------------ |
34 | 34 | ||
35 | instance Num (Vector CInt) where | ||
36 | (+) = adaptScalar addConstant add (flip addConstant) | ||
37 | negate = scale (-1) | ||
38 | (*) = adaptScalar scale mul (flip scale) | ||
39 | signum = vectorMapI Sign | ||
40 | abs = vectorMapI Abs | ||
41 | fromInteger = fromList . return . fromInteger | ||
42 | |||
35 | instance Num (Vector Float) where | 43 | instance Num (Vector Float) where |
36 | (+) = adaptScalar addConstant add (flip addConstant) | 44 | (+) = adaptScalar addConstant add (flip addConstant) |
37 | negate = scale (-1) | 45 | negate = scale (-1) |
diff --git a/packages/base/src/Numeric/Vectorized.hs b/packages/base/src/Numeric/Vectorized.hs index 405ae01..70bd48b 100644 --- a/packages/base/src/Numeric/Vectorized.hs +++ b/packages/base/src/Numeric/Vectorized.hs | |||
@@ -1,7 +1,7 @@ | |||
1 | ----------------------------------------------------------------------------- | 1 | ----------------------------------------------------------------------------- |
2 | -- | | 2 | -- | |
3 | -- Module : Numeric.Vectorized | 3 | -- Module : Numeric.Vectorized |
4 | -- Copyright : (c) Alberto Ruiz 2007-14 | 4 | -- Copyright : (c) Alberto Ruiz 2007-15 |
5 | -- License : BSD3 | 5 | -- License : BSD3 |
6 | -- Maintainer : Alberto Ruiz | 6 | -- Maintainer : Alberto Ruiz |
7 | -- Stability : provisional | 7 | -- Stability : provisional |
@@ -11,12 +11,12 @@ | |||
11 | ----------------------------------------------------------------------------- | 11 | ----------------------------------------------------------------------------- |
12 | 12 | ||
13 | module Numeric.Vectorized ( | 13 | module Numeric.Vectorized ( |
14 | sumF, sumR, sumQ, sumC, | 14 | sumF, sumR, sumQ, sumC, sumI, |
15 | prodF, prodR, prodQ, prodC, | 15 | prodF, prodR, prodQ, prodC, prodI, |
16 | FunCodeS(..), toScalarR, toScalarF, toScalarC, toScalarQ, | 16 | FunCodeS(..), toScalarR, toScalarF, toScalarC, toScalarQ, toScalarI, |
17 | FunCodeV(..), vectorMapR, vectorMapC, vectorMapF, vectorMapQ, | 17 | FunCodeV(..), vectorMapR, vectorMapC, vectorMapF, vectorMapQ, vectorMapI, |
18 | FunCodeSV(..), vectorMapValR, vectorMapValC, vectorMapValF, vectorMapValQ, | 18 | FunCodeSV(..), vectorMapValR, vectorMapValC, vectorMapValF, vectorMapValQ, vectorMapValI, |
19 | FunCodeVV(..), vectorZipR, vectorZipC, vectorZipF, vectorZipQ, | 19 | FunCodeVV(..), vectorZipR, vectorZipC, vectorZipF, vectorZipQ, vectorZipI, |
20 | vectorScan, saveMatrix, | 20 | vectorScan, saveMatrix, |
21 | Seed, RandDist(..), randomVector, | 21 | Seed, RandDist(..), randomVector, |
22 | sortVector, roundVector | 22 | sortVector, roundVector |
@@ -67,6 +67,8 @@ data FunCodeSV = Scale | |||
67 | | Negate | 67 | | Negate |
68 | | PowSV | 68 | | PowSV |
69 | | PowVS | 69 | | PowVS |
70 | | ModSV | ||
71 | | ModVS | ||
70 | deriving Enum | 72 | deriving Enum |
71 | 73 | ||
72 | data FunCodeVV = Add | 74 | data FunCodeVV = Add |
@@ -75,6 +77,7 @@ data FunCodeVV = Add | |||
75 | | Div | 77 | | Div |
76 | | Pow | 78 | | Pow |
77 | | ATan2 | 79 | | ATan2 |
80 | | Mod | ||
78 | deriving Enum | 81 | deriving Enum |
79 | 82 | ||
80 | data FunCodeS = Norm2 | 83 | data FunCodeS = Norm2 |
@@ -89,69 +92,67 @@ data FunCodeS = Norm2 | |||
89 | 92 | ||
90 | -- | sum of elements | 93 | -- | sum of elements |
91 | sumF :: Vector Float -> Float | 94 | sumF :: Vector Float -> Float |
92 | sumF x = unsafePerformIO $ do | 95 | sumF = sumg c_sumF |
93 | r <- createVector 1 | ||
94 | app2 c_sumF vec x vec r "sumF" | ||
95 | return $ r @> 0 | ||
96 | 96 | ||
97 | -- | sum of elements | 97 | -- | sum of elements |
98 | sumR :: Vector Double -> Double | 98 | sumR :: Vector Double -> Double |
99 | sumR x = unsafePerformIO $ do | 99 | sumR = sumg c_sumR |
100 | r <- createVector 1 | ||
101 | app2 c_sumR vec x vec r "sumR" | ||
102 | return $ r @> 0 | ||
103 | 100 | ||
104 | -- | sum of elements | 101 | -- | sum of elements |
105 | sumQ :: Vector (Complex Float) -> Complex Float | 102 | sumQ :: Vector (Complex Float) -> Complex Float |
106 | sumQ x = unsafePerformIO $ do | 103 | sumQ = sumg c_sumQ |
107 | r <- createVector 1 | ||
108 | app2 c_sumQ vec x vec r "sumQ" | ||
109 | return $ r @> 0 | ||
110 | 104 | ||
111 | -- | sum of elements | 105 | -- | sum of elements |
112 | sumC :: Vector (Complex Double) -> Complex Double | 106 | sumC :: Vector (Complex Double) -> Complex Double |
113 | sumC x = unsafePerformIO $ do | 107 | sumC = sumg c_sumC |
114 | r <- createVector 1 | 108 | |
115 | app2 c_sumC vec x vec r "sumC" | 109 | -- | sum of elements |
116 | return $ r @> 0 | 110 | sumI :: Vector CInt -> CInt |
111 | sumI = sumg c_sumI | ||
112 | |||
113 | sumg f x = unsafePerformIO $ do | ||
114 | r <- createVector 1 | ||
115 | app2 f vec x vec r "sum" | ||
116 | return $ r @> 0 | ||
117 | 117 | ||
118 | foreign import ccall unsafe "sumF" c_sumF :: TFF | 118 | foreign import ccall unsafe "sumF" c_sumF :: TFF |
119 | foreign import ccall unsafe "sumR" c_sumR :: TVV | 119 | foreign import ccall unsafe "sumR" c_sumR :: TVV |
120 | foreign import ccall unsafe "sumQ" c_sumQ :: TQVQV | 120 | foreign import ccall unsafe "sumQ" c_sumQ :: TQVQV |
121 | foreign import ccall unsafe "sumC" c_sumC :: TCVCV | 121 | foreign import ccall unsafe "sumC" c_sumC :: TCVCV |
122 | foreign import ccall unsafe "sumC" c_sumI :: CV CInt (CV CInt (IO CInt)) | ||
122 | 123 | ||
123 | -- | product of elements | 124 | -- | product of elements |
124 | prodF :: Vector Float -> Float | 125 | prodF :: Vector Float -> Float |
125 | prodF x = unsafePerformIO $ do | 126 | prodF = prodg c_prodF |
126 | r <- createVector 1 | ||
127 | app2 c_prodF vec x vec r "prodF" | ||
128 | return $ r @> 0 | ||
129 | 127 | ||
130 | -- | product of elements | 128 | -- | product of elements |
131 | prodR :: Vector Double -> Double | 129 | prodR :: Vector Double -> Double |
132 | prodR x = unsafePerformIO $ do | 130 | prodR = prodg c_prodR |
133 | r <- createVector 1 | ||
134 | app2 c_prodR vec x vec r "prodR" | ||
135 | return $ r @> 0 | ||
136 | 131 | ||
137 | -- | product of elements | 132 | -- | product of elements |
138 | prodQ :: Vector (Complex Float) -> Complex Float | 133 | prodQ :: Vector (Complex Float) -> Complex Float |
139 | prodQ x = unsafePerformIO $ do | 134 | prodQ = prodg c_prodQ |
140 | r <- createVector 1 | ||
141 | app2 c_prodQ vec x vec r "prodQ" | ||
142 | return $ r @> 0 | ||
143 | 135 | ||
144 | -- | product of elements | 136 | -- | product of elements |
145 | prodC :: Vector (Complex Double) -> Complex Double | 137 | prodC :: Vector (Complex Double) -> Complex Double |
146 | prodC x = unsafePerformIO $ do | 138 | prodC = prodg c_prodC |
147 | r <- createVector 1 | 139 | |
148 | app2 c_prodC vec x vec r "prodC" | 140 | -- | product of elements |
149 | return $ r @> 0 | 141 | prodI :: Vector CInt -> CInt |
142 | prodI = prodg c_prodI | ||
143 | |||
144 | |||
145 | prodg f x = unsafePerformIO $ do | ||
146 | r <- createVector 1 | ||
147 | app2 f vec x vec r "prod" | ||
148 | return $ r @> 0 | ||
149 | |||
150 | 150 | ||
151 | foreign import ccall unsafe "prodF" c_prodF :: TFF | 151 | foreign import ccall unsafe "prodF" c_prodF :: TFF |
152 | foreign import ccall unsafe "prodR" c_prodR :: TVV | 152 | foreign import ccall unsafe "prodR" c_prodR :: TVV |
153 | foreign import ccall unsafe "prodQ" c_prodQ :: TQVQV | 153 | foreign import ccall unsafe "prodQ" c_prodQ :: TQVQV |
154 | foreign import ccall unsafe "prodC" c_prodC :: TCVCV | 154 | foreign import ccall unsafe "prodC" c_prodC :: TCVCV |
155 | foreign import ccall unsafe "prodI" c_prodI :: CV CInt (CV CInt (IO CInt)) | ||
155 | 156 | ||
156 | ------------------------------------------------------------------ | 157 | ------------------------------------------------------------------ |
157 | 158 | ||
@@ -203,6 +204,12 @@ toScalarQ oper = toScalarAux c_toScalarQ (fromei oper) | |||
203 | 204 | ||
204 | foreign import ccall unsafe "toScalarQ" c_toScalarQ :: CInt -> TQVF | 205 | foreign import ccall unsafe "toScalarQ" c_toScalarQ :: CInt -> TQVF |
205 | 206 | ||
207 | -- | obtains different functions of a vector: norm1, norm2, max, min, posmax, posmin, etc. | ||
208 | toScalarI :: FunCodeS -> Vector CInt -> CInt | ||
209 | toScalarI oper = toScalarAux c_toScalarI (fromei oper) | ||
210 | |||
211 | foreign import ccall unsafe "toScalarI" c_toScalarI :: CInt -> CV CInt (CV CInt (IO CInt)) | ||
212 | |||
206 | ------------------------------------------------------------------ | 213 | ------------------------------------------------------------------ |
207 | 214 | ||
208 | -- | map of real vectors with given function | 215 | -- | map of real vectors with given function |
@@ -229,6 +236,12 @@ vectorMapQ = vectorMapAux c_vectorMapQ | |||
229 | 236 | ||
230 | foreign import ccall unsafe "mapQ" c_vectorMapQ :: CInt -> TQVQV | 237 | foreign import ccall unsafe "mapQ" c_vectorMapQ :: CInt -> TQVQV |
231 | 238 | ||
239 | -- | map of real vectors with given function | ||
240 | vectorMapI :: FunCodeV -> Vector CInt -> Vector CInt | ||
241 | vectorMapI = vectorMapAux c_vectorMapI | ||
242 | |||
243 | foreign import ccall unsafe "mapI" c_vectorMapI :: CInt -> CV CInt (CV CInt (IO CInt)) | ||
244 | |||
232 | ------------------------------------------------------------------- | 245 | ------------------------------------------------------------------- |
233 | 246 | ||
234 | -- | map of real vectors with given function | 247 | -- | map of real vectors with given function |
@@ -255,6 +268,13 @@ vectorMapValQ oper = vectorMapValAux c_vectorMapValQ (fromei oper) | |||
255 | 268 | ||
256 | foreign import ccall unsafe "mapValQ" c_vectorMapValQ :: CInt -> Ptr (Complex Float) -> TQVQV | 269 | foreign import ccall unsafe "mapValQ" c_vectorMapValQ :: CInt -> Ptr (Complex Float) -> TQVQV |
257 | 270 | ||
271 | -- | map of real vectors with given function | ||
272 | vectorMapValI :: FunCodeSV -> CInt -> Vector CInt -> Vector CInt | ||
273 | vectorMapValI oper = vectorMapValAux c_vectorMapValI (fromei oper) | ||
274 | |||
275 | foreign import ccall unsafe "mapValI" c_vectorMapValI :: CInt -> Ptr CInt -> CV CInt (CV CInt (IO CInt)) | ||
276 | |||
277 | |||
258 | ------------------------------------------------------------------- | 278 | ------------------------------------------------------------------- |
259 | 279 | ||
260 | -- | elementwise operation on real vectors | 280 | -- | elementwise operation on real vectors |
@@ -281,6 +301,13 @@ vectorZipQ = vectorZipAux c_vectorZipQ | |||
281 | 301 | ||
282 | foreign import ccall unsafe "zipQ" c_vectorZipQ :: CInt -> TQVQVQV | 302 | foreign import ccall unsafe "zipQ" c_vectorZipQ :: CInt -> TQVQVQV |
283 | 303 | ||
304 | -- | elementwise operation on CInt vectors | ||
305 | vectorZipI :: FunCodeVV -> Vector CInt -> Vector CInt -> Vector CInt | ||
306 | vectorZipI = vectorZipAux c_vectorZipI | ||
307 | |||
308 | foreign import ccall unsafe "zipI" c_vectorZipI :: CInt -> CV CInt (CV CInt (CV CInt (IO CInt))) | ||
309 | |||
310 | |||
284 | -------------------------------------------------------------------------------- | 311 | -------------------------------------------------------------------------------- |
285 | 312 | ||
286 | foreign import ccall unsafe "vectorScan" c_vectorScan | 313 | foreign import ccall unsafe "vectorScan" c_vectorScan |