diff options
author | Alberto Ruiz <aruiz@um.es> | 2015-05-21 21:22:00 +0200 |
---|---|---|
committer | Alberto Ruiz <aruiz@um.es> | 2015-05-21 21:22:00 +0200 |
commit | dd595ffe3fe6c710adba253696e455ffd98877c5 (patch) | |
tree | 2460a2f84d1a9172d0570e29716fcac20b080ba3 /packages/base/src/Data/Packed | |
parent | 46178222d272a85220bc86b221aa3166edd5bd4a (diff) |
SContainer
Diffstat (limited to 'packages/base/src/Data/Packed')
-rw-r--r-- | packages/base/src/Data/Packed/Internal/Numeric.hs | 107 | ||||
-rw-r--r-- | packages/base/src/Data/Packed/Numeric.hs | 2 |
2 files changed, 67 insertions, 42 deletions
diff --git a/packages/base/src/Data/Packed/Internal/Numeric.hs b/packages/base/src/Data/Packed/Internal/Numeric.hs index 7a4dd29..7359433 100644 --- a/packages/base/src/Data/Packed/Internal/Numeric.hs +++ b/packages/base/src/Data/Packed/Internal/Numeric.hs | |||
@@ -20,7 +20,7 @@ module Data.Packed.Internal.Numeric ( | |||
20 | -- * Basic functions | 20 | -- * Basic functions |
21 | ident, diag, ctrans, | 21 | ident, diag, ctrans, |
22 | -- * Generic operations | 22 | -- * Generic operations |
23 | Container(..), | 23 | SContainer(..), Container(..), |
24 | scalar, conj, scale, arctan2, cmap, | 24 | scalar, conj, scale, arctan2, cmap, |
25 | atIndex, minIndex, maxIndex, minElement, maxElement, | 25 | atIndex, minIndex, maxIndex, minElement, maxElement, |
26 | sumElements, prodElements, | 26 | sumElements, prodElements, |
@@ -67,27 +67,17 @@ type instance ArgOf Matrix a = a -> a -> a | |||
67 | ------------------------------------------------------------------- | 67 | ------------------------------------------------------------------- |
68 | 68 | ||
69 | -- | Basic element-by-element functions for numeric containers | 69 | -- | Basic element-by-element functions for numeric containers |
70 | class (Complexable c, Fractional e, Element e) => Container c e | 70 | class Element e => SContainer c e |
71 | where | 71 | where |
72 | size' :: c e -> IndexOf c | 72 | size' :: c e -> IndexOf c |
73 | scalar' :: e -> c e | 73 | scalar' :: e -> c e |
74 | conj' :: c e -> c e | ||
75 | scale' :: e -> c e -> c e | 74 | scale' :: e -> c e -> c e |
76 | -- | scale the element by element reciprocal of the object: | ||
77 | -- | ||
78 | -- @scaleRecip 2 (fromList [5,i]) == 2 |> [0.4 :+ 0.0,0.0 :+ (-2.0)]@ | ||
79 | scaleRecip :: e -> c e -> c e | ||
80 | addConstant :: e -> c e -> c e | 75 | addConstant :: e -> c e -> c e |
81 | add :: c e -> c e -> c e | 76 | add :: c e -> c e -> c e |
82 | sub :: c e -> c e -> c e | 77 | sub :: c e -> c e -> c e |
83 | -- | element by element multiplication | 78 | -- | element by element multiplication |
84 | mul :: c e -> c e -> c e | 79 | mul :: c e -> c e -> c e |
85 | -- | element by element division | ||
86 | divide :: c e -> c e -> c e | ||
87 | equal :: c e -> c e -> Bool | 80 | equal :: c e -> c e -> Bool |
88 | -- | ||
89 | -- element by element inverse tangent | ||
90 | arctan2' :: c e -> c e -> c e | ||
91 | cmap' :: (Element b) => (e -> b) -> c e -> c b | 81 | cmap' :: (Element b) => (e -> b) -> c e -> c b |
92 | konst' :: e -> IndexOf c -> c e | 82 | konst' :: e -> IndexOf c -> c e |
93 | build' :: IndexOf c -> (ArgOf c e) -> c e | 83 | build' :: IndexOf c -> (ArgOf c e) -> c e |
@@ -116,24 +106,35 @@ class (Complexable c, Fractional e, Element e) => Container c e | |||
116 | -> [(IndexOf c, e)] -- ^ association list | 106 | -> [(IndexOf c, e)] -- ^ association list |
117 | -> c e -- ^ result | 107 | -> c e -- ^ result |
118 | 108 | ||
109 | |||
110 | -- | Basic element-by-element functions for numeric containers | ||
111 | class (Complexable c, Fractional e, SContainer c e) => Container c e | ||
112 | where | ||
113 | conj' :: c e -> c e | ||
114 | -- | scale the element by element reciprocal of the object: | ||
115 | -- | ||
116 | -- @scaleRecip 2 (fromList [5,i]) == 2 |> [0.4 :+ 0.0,0.0 :+ (-2.0)]@ | ||
117 | scaleRecip :: e -> c e -> c e | ||
118 | -- | element by element division | ||
119 | divide :: c e -> c e -> c e | ||
120 | -- | ||
121 | -- element by element inverse tangent | ||
122 | arctan2' :: c e -> c e -> c e | ||
123 | |||
119 | -------------------------------------------------------------------------- | 124 | -------------------------------------------------------------------------- |
120 | 125 | ||
121 | instance Container Vector Float | 126 | instance SContainer Vector Float |
122 | where | 127 | where |
123 | size' = dim | 128 | size' = dim |
124 | scale' = vectorMapValF Scale | 129 | scale' = vectorMapValF Scale |
125 | scaleRecip = vectorMapValF Recip | ||
126 | addConstant = vectorMapValF AddConstant | 130 | addConstant = vectorMapValF AddConstant |
127 | add = vectorZipF Add | 131 | add = vectorZipF Add |
128 | sub = vectorZipF Sub | 132 | sub = vectorZipF Sub |
129 | mul = vectorZipF Mul | 133 | mul = vectorZipF Mul |
130 | divide = vectorZipF Div | ||
131 | equal u v = dim u == dim v && maxElement (vectorMapF Abs (sub u v)) == 0.0 | 134 | equal u v = dim u == dim v && maxElement (vectorMapF Abs (sub u v)) == 0.0 |
132 | arctan2' = vectorZipF ATan2 | ||
133 | scalar' x = fromList [x] | 135 | scalar' x = fromList [x] |
134 | konst' = constantD | 136 | konst' = constantD |
135 | build' = buildV | 137 | build' = buildV |
136 | conj' = id | ||
137 | cmap' = mapVector | 138 | cmap' = mapVector |
138 | atIndex' = (@>) | 139 | atIndex' = (@>) |
139 | minIndex' = emptyErrorV "minIndex" (round . toScalarF MinIdx) | 140 | minIndex' = emptyErrorV "minIndex" (round . toScalarF MinIdx) |
@@ -148,22 +149,26 @@ instance Container Vector Float | |||
148 | accum' = accumV | 149 | accum' = accumV |
149 | cond' = condV condF | 150 | cond' = condV condF |
150 | 151 | ||
151 | instance Container Vector Double | 152 | instance Container Vector Float |
153 | where | ||
154 | scaleRecip = vectorMapValF Recip | ||
155 | divide = vectorZipF Div | ||
156 | arctan2' = vectorZipF ATan2 | ||
157 | conj' = id | ||
158 | |||
159 | |||
160 | instance SContainer Vector Double | ||
152 | where | 161 | where |
153 | size' = dim | 162 | size' = dim |
154 | scale' = vectorMapValR Scale | 163 | scale' = vectorMapValR Scale |
155 | scaleRecip = vectorMapValR Recip | ||
156 | addConstant = vectorMapValR AddConstant | 164 | addConstant = vectorMapValR AddConstant |
157 | add = vectorZipR Add | 165 | add = vectorZipR Add |
158 | sub = vectorZipR Sub | 166 | sub = vectorZipR Sub |
159 | mul = vectorZipR Mul | 167 | mul = vectorZipR Mul |
160 | divide = vectorZipR Div | ||
161 | equal u v = dim u == dim v && maxElement (vectorMapR Abs (sub u v)) == 0.0 | 168 | equal u v = dim u == dim v && maxElement (vectorMapR Abs (sub u v)) == 0.0 |
162 | arctan2' = vectorZipR ATan2 | ||
163 | scalar' x = fromList [x] | 169 | scalar' x = fromList [x] |
164 | konst' = constantD | 170 | konst' = constantD |
165 | build' = buildV | 171 | build' = buildV |
166 | conj' = id | ||
167 | cmap' = mapVector | 172 | cmap' = mapVector |
168 | atIndex' = (@>) | 173 | atIndex' = (@>) |
169 | minIndex' = emptyErrorV "minIndex" (round . toScalarR MinIdx) | 174 | minIndex' = emptyErrorV "minIndex" (round . toScalarR MinIdx) |
@@ -178,22 +183,25 @@ instance Container Vector Double | |||
178 | accum' = accumV | 183 | accum' = accumV |
179 | cond' = condV condD | 184 | cond' = condV condD |
180 | 185 | ||
181 | instance Container Vector (Complex Double) | 186 | instance Container Vector Double |
187 | where | ||
188 | scaleRecip = vectorMapValR Recip | ||
189 | divide = vectorZipR Div | ||
190 | arctan2' = vectorZipR ATan2 | ||
191 | conj' = id | ||
192 | |||
193 | instance SContainer Vector (Complex Double) | ||
182 | where | 194 | where |
183 | size' = dim | 195 | size' = dim |
184 | scale' = vectorMapValC Scale | 196 | scale' = vectorMapValC Scale |
185 | scaleRecip = vectorMapValC Recip | ||
186 | addConstant = vectorMapValC AddConstant | 197 | addConstant = vectorMapValC AddConstant |
187 | add = vectorZipC Add | 198 | add = vectorZipC Add |
188 | sub = vectorZipC Sub | 199 | sub = vectorZipC Sub |
189 | mul = vectorZipC Mul | 200 | mul = vectorZipC Mul |
190 | divide = vectorZipC Div | ||
191 | equal u v = dim u == dim v && maxElement (mapVector magnitude (sub u v)) == 0.0 | 201 | equal u v = dim u == dim v && maxElement (mapVector magnitude (sub u v)) == 0.0 |
192 | arctan2' = vectorZipC ATan2 | ||
193 | scalar' x = fromList [x] | 202 | scalar' x = fromList [x] |
194 | konst' = constantD | 203 | konst' = constantD |
195 | build' = buildV | 204 | build' = buildV |
196 | conj' = conjugateC | ||
197 | cmap' = mapVector | 205 | cmap' = mapVector |
198 | atIndex' = (@>) | 206 | atIndex' = (@>) |
199 | minIndex' = emptyErrorV "minIndex" (minIndex' . fst . fromComplex . (mul <*> conj')) | 207 | minIndex' = emptyErrorV "minIndex" (minIndex' . fst . fromComplex . (mul <*> conj')) |
@@ -208,22 +216,27 @@ instance Container Vector (Complex Double) | |||
208 | accum' = accumV | 216 | accum' = accumV |
209 | cond' = undefined -- cannot match | 217 | cond' = undefined -- cannot match |
210 | 218 | ||
211 | instance Container Vector (Complex Float) | 219 | |
220 | instance Container Vector (Complex Double) | ||
221 | where | ||
222 | scaleRecip = vectorMapValC Recip | ||
223 | divide = vectorZipC Div | ||
224 | arctan2' = vectorZipC ATan2 | ||
225 | conj' = conjugateC | ||
226 | |||
227 | |||
228 | instance SContainer Vector (Complex Float) | ||
212 | where | 229 | where |
213 | size' = dim | 230 | size' = dim |
214 | scale' = vectorMapValQ Scale | 231 | scale' = vectorMapValQ Scale |
215 | scaleRecip = vectorMapValQ Recip | ||
216 | addConstant = vectorMapValQ AddConstant | 232 | addConstant = vectorMapValQ AddConstant |
217 | add = vectorZipQ Add | 233 | add = vectorZipQ Add |
218 | sub = vectorZipQ Sub | 234 | sub = vectorZipQ Sub |
219 | mul = vectorZipQ Mul | 235 | mul = vectorZipQ Mul |
220 | divide = vectorZipQ Div | ||
221 | equal u v = dim u == dim v && maxElement (mapVector magnitude (sub u v)) == 0.0 | 236 | equal u v = dim u == dim v && maxElement (mapVector magnitude (sub u v)) == 0.0 |
222 | arctan2' = vectorZipQ ATan2 | ||
223 | scalar' x = fromList [x] | 237 | scalar' x = fromList [x] |
224 | konst' = constantD | 238 | konst' = constantD |
225 | build' = buildV | 239 | build' = buildV |
226 | conj' = conjugateQ | ||
227 | cmap' = mapVector | 240 | cmap' = mapVector |
228 | atIndex' = (@>) | 241 | atIndex' = (@>) |
229 | minIndex' = emptyErrorV "minIndex" (minIndex' . fst . fromComplex . (mul <*> conj')) | 242 | minIndex' = emptyErrorV "minIndex" (minIndex' . fst . fromComplex . (mul <*> conj')) |
@@ -238,24 +251,28 @@ instance Container Vector (Complex Float) | |||
238 | accum' = accumV | 251 | accum' = accumV |
239 | cond' = undefined -- cannot match | 252 | cond' = undefined -- cannot match |
240 | 253 | ||
254 | instance Container Vector (Complex Float) | ||
255 | where | ||
256 | scaleRecip = vectorMapValQ Recip | ||
257 | divide = vectorZipQ Div | ||
258 | arctan2' = vectorZipQ ATan2 | ||
259 | conj' = conjugateQ | ||
260 | |||
261 | |||
241 | --------------------------------------------------------------- | 262 | --------------------------------------------------------------- |
242 | 263 | ||
243 | instance (Fractional a, Element a, Container Vector a) => Container Matrix a | 264 | instance (Num a, Element a, SContainer Vector a) => SContainer Matrix a |
244 | where | 265 | where |
245 | size' = size | 266 | size' = size |
246 | scale' x = liftMatrix (scale' x) | 267 | scale' x = liftMatrix (scale' x) |
247 | scaleRecip x = liftMatrix (scaleRecip x) | ||
248 | addConstant x = liftMatrix (addConstant x) | 268 | addConstant x = liftMatrix (addConstant x) |
249 | add = liftMatrix2 add | 269 | add = liftMatrix2 add |
250 | sub = liftMatrix2 sub | 270 | sub = liftMatrix2 sub |
251 | mul = liftMatrix2 mul | 271 | mul = liftMatrix2 mul |
252 | divide = liftMatrix2 divide | ||
253 | equal a b = cols a == cols b && flatten a `equal` flatten b | 272 | equal a b = cols a == cols b && flatten a `equal` flatten b |
254 | arctan2' = liftMatrix2 arctan2' | ||
255 | scalar' x = (1><1) [x] | 273 | scalar' x = (1><1) [x] |
256 | konst' v (r,c) = matrixFromVector RowMajor r c (konst' v (r*c)) | 274 | konst' v (r,c) = matrixFromVector RowMajor r c (konst' v (r*c)) |
257 | build' = buildM | 275 | build' = buildM |
258 | conj' = liftMatrix conj' | ||
259 | cmap' f = liftMatrix (mapVector f) | 276 | cmap' f = liftMatrix (mapVector f) |
260 | atIndex' = (@@>) | 277 | atIndex' = (@@>) |
261 | minIndex' = emptyErrorM "minIndex of Matrix" $ | 278 | minIndex' = emptyErrorM "minIndex of Matrix" $ |
@@ -264,15 +281,23 @@ instance (Fractional a, Element a, Container Vector a) => Container Matrix a | |||
264 | \m -> divMod (maxIndex' $ flatten m) (cols m) | 281 | \m -> divMod (maxIndex' $ flatten m) (cols m) |
265 | minElement' = emptyErrorM "minElement of Matrix" (atIndex' <*> minIndex') | 282 | minElement' = emptyErrorM "minElement of Matrix" (atIndex' <*> minIndex') |
266 | maxElement' = emptyErrorM "maxElement of Matrix" (atIndex' <*> maxIndex') | 283 | maxElement' = emptyErrorM "maxElement of Matrix" (atIndex' <*> maxIndex') |
267 | sumElements' = sumElements . flatten | 284 | sumElements' = sumElements' . flatten |
268 | prodElements' = prodElements . flatten | 285 | prodElements' = prodElements' . flatten |
269 | step' = liftMatrix step | 286 | step' = liftMatrix step' |
270 | find' = findM | 287 | find' = findM |
271 | assoc' = assocM | 288 | assoc' = assocM |
272 | accum' = accumM | 289 | accum' = accumM |
273 | cond' = condM | 290 | cond' = condM |
274 | 291 | ||
275 | 292 | ||
293 | instance (Fractional a, Container Vector a) => Container Matrix a | ||
294 | where | ||
295 | scaleRecip x = liftMatrix (scaleRecip x) | ||
296 | divide = liftMatrix2 divide | ||
297 | arctan2' = liftMatrix2 arctan2' | ||
298 | conj' = liftMatrix conj' | ||
299 | |||
300 | |||
276 | emptyErrorV msg f v = | 301 | emptyErrorV msg f v = |
277 | if dim v > 0 | 302 | if dim v > 0 |
278 | then f v | 303 | then f v |
@@ -682,7 +707,7 @@ accumM m0 f xs = ST.runSTMatrix $ do | |||
682 | 707 | ||
683 | ---------------------------------------------------------------------- | 708 | ---------------------------------------------------------------------- |
684 | 709 | ||
685 | condM a b l e t = matrixFromVector RowMajor (rows a'') (cols a'') $ cond a' b' l' e' t' | 710 | condM a b l e t = matrixFromVector RowMajor (rows a'') (cols a'') $ cond' a' b' l' e' t' |
686 | where | 711 | where |
687 | args@(a'':_) = conformMs [a,b,l,e,t] | 712 | args@(a'':_) = conformMs [a,b,l,e,t] |
688 | [a', b', l', e', t'] = map flatten args | 713 | [a', b', l', e', t'] = map flatten args |
diff --git a/packages/base/src/Data/Packed/Numeric.hs b/packages/base/src/Data/Packed/Numeric.hs index 6d62f22..ae5fad7 100644 --- a/packages/base/src/Data/Packed/Numeric.hs +++ b/packages/base/src/Data/Packed/Numeric.hs | |||
@@ -31,7 +31,7 @@ module Data.Packed.Numeric ( | |||
31 | diag, ident, | 31 | diag, ident, |
32 | ctrans, | 32 | ctrans, |
33 | -- * Generic operations | 33 | -- * Generic operations |
34 | Container(..), Numeric, | 34 | SContainer(..), Container(..), Numeric, |
35 | -- add, mul, sub, divide, equal, scaleRecip, addConstant, | 35 | -- add, mul, sub, divide, equal, scaleRecip, addConstant, |
36 | scalar, conj, scale, arctan2, cmap, | 36 | scalar, conj, scale, arctan2, cmap, |
37 | atIndex, minIndex, maxIndex, minElement, maxElement, | 37 | atIndex, minIndex, maxIndex, minElement, maxElement, |