diff options
author | Alberto Ruiz <aruiz@um.es> | 2015-05-23 12:31:32 +0200 |
---|---|---|
committer | Alberto Ruiz <aruiz@um.es> | 2015-05-23 12:31:32 +0200 |
commit | b1b445697db31b1603a31747ca31151f97ee7263 (patch) | |
tree | 453bbe1e16e2abd0b605e7807a33de4eaba4a866 /packages/base/src/Data | |
parent | 36379e29fe99b033313f42464897c32b9805559d (diff) |
join SContainer and Container using Fractional contexts
Diffstat (limited to 'packages/base/src/Data')
-rw-r--r-- | packages/base/src/Data/Packed/Internal/Numeric.hs | 53 | ||||
-rw-r--r-- | packages/base/src/Data/Packed/Numeric.hs | 13 |
2 files changed, 24 insertions, 42 deletions
diff --git a/packages/base/src/Data/Packed/Internal/Numeric.hs b/packages/base/src/Data/Packed/Internal/Numeric.hs index 2e36de2..00ec70c 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 | SContainer(..), Container(..), | 23 | 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, |
@@ -115,7 +115,7 @@ m ¿¿ ec = trans (trans m ?? ec) | |||
115 | 115 | ||
116 | 116 | ||
117 | -- | Basic element-by-element functions for numeric containers | 117 | -- | Basic element-by-element functions for numeric containers |
118 | class Element e => SContainer c e | 118 | class Element e => Container c e |
119 | where | 119 | where |
120 | conj' :: c e -> c e | 120 | conj' :: c e -> c e |
121 | size' :: c e -> IndexOf c | 121 | size' :: c e -> IndexOf c |
@@ -155,24 +155,20 @@ class Element e => SContainer c e | |||
155 | -> [(IndexOf c, e)] -- ^ association list | 155 | -> [(IndexOf c, e)] -- ^ association list |
156 | -> c e -- ^ result | 156 | -> c e -- ^ result |
157 | 157 | ||
158 | |||
159 | -- | Basic element-by-element functions for numeric containers | ||
160 | class (Fractional e, SContainer c e) => Container c e | ||
161 | where | ||
162 | -- | scale the element by element reciprocal of the object: | 158 | -- | scale the element by element reciprocal of the object: |
163 | -- | 159 | -- |
164 | -- @scaleRecip 2 (fromList [5,i]) == 2 |> [0.4 :+ 0.0,0.0 :+ (-2.0)]@ | 160 | -- @scaleRecip 2 (fromList [5,i]) == 2 |> [0.4 :+ 0.0,0.0 :+ (-2.0)]@ |
165 | scaleRecip :: e -> c e -> c e | 161 | scaleRecip :: Fractional e => e -> c e -> c e |
166 | -- | element by element division | 162 | -- | element by element division |
167 | divide :: c e -> c e -> c e | 163 | divide :: Fractional e => c e -> c e -> c e |
168 | -- | 164 | -- |
169 | -- element by element inverse tangent | 165 | -- element by element inverse tangent |
170 | arctan2' :: c e -> c e -> c e | 166 | arctan2' :: Fractional e => c e -> c e -> c e |
171 | 167 | ||
172 | 168 | ||
173 | -------------------------------------------------------------------------- | 169 | -------------------------------------------------------------------------- |
174 | 170 | ||
175 | instance SContainer Vector CInt | 171 | instance Container Vector CInt |
176 | where | 172 | where |
177 | conj' = id | 173 | conj' = id |
178 | size' = dim | 174 | size' = dim |
@@ -198,9 +194,11 @@ instance SContainer Vector CInt | |||
198 | assoc' = assocV | 194 | assoc' = assocV |
199 | accum' = accumV | 195 | accum' = accumV |
200 | -- cond' = condV condI | 196 | -- cond' = condV condI |
197 | scaleRecip = undefined -- cannot match | ||
198 | divide = undefined | ||
199 | arctan2' = undefined | ||
201 | 200 | ||
202 | 201 | instance Container Vector Float | |
203 | instance SContainer Vector Float | ||
204 | where | 202 | where |
205 | conj' = id | 203 | conj' = id |
206 | size' = dim | 204 | size' = dim |
@@ -226,16 +224,13 @@ instance SContainer Vector Float | |||
226 | assoc' = assocV | 224 | assoc' = assocV |
227 | accum' = accumV | 225 | accum' = accumV |
228 | cond' = condV condF | 226 | cond' = condV condF |
229 | |||
230 | instance Container Vector Float | ||
231 | where | ||
232 | scaleRecip = vectorMapValF Recip | 227 | scaleRecip = vectorMapValF Recip |
233 | divide = vectorZipF Div | 228 | divide = vectorZipF Div |
234 | arctan2' = vectorZipF ATan2 | 229 | arctan2' = vectorZipF ATan2 |
235 | 230 | ||
236 | 231 | ||
237 | 232 | ||
238 | instance SContainer Vector Double | 233 | instance Container Vector Double |
239 | where | 234 | where |
240 | conj' = id | 235 | conj' = id |
241 | size' = dim | 236 | size' = dim |
@@ -261,15 +256,12 @@ instance SContainer Vector Double | |||
261 | assoc' = assocV | 256 | assoc' = assocV |
262 | accum' = accumV | 257 | accum' = accumV |
263 | cond' = condV condD | 258 | cond' = condV condD |
264 | |||
265 | instance Container Vector Double | ||
266 | where | ||
267 | scaleRecip = vectorMapValR Recip | 259 | scaleRecip = vectorMapValR Recip |
268 | divide = vectorZipR Div | 260 | divide = vectorZipR Div |
269 | arctan2' = vectorZipR ATan2 | 261 | arctan2' = vectorZipR ATan2 |
270 | 262 | ||
271 | 263 | ||
272 | instance SContainer Vector (Complex Double) | 264 | instance Container Vector (Complex Double) |
273 | where | 265 | where |
274 | conj' = conjugateC | 266 | conj' = conjugateC |
275 | size' = dim | 267 | size' = dim |
@@ -295,15 +287,11 @@ instance SContainer Vector (Complex Double) | |||
295 | assoc' = assocV | 287 | assoc' = assocV |
296 | accum' = accumV | 288 | accum' = accumV |
297 | cond' = undefined -- cannot match | 289 | cond' = undefined -- cannot match |
298 | |||
299 | |||
300 | instance Container Vector (Complex Double) | ||
301 | where | ||
302 | scaleRecip = vectorMapValC Recip | 290 | scaleRecip = vectorMapValC Recip |
303 | divide = vectorZipC Div | 291 | divide = vectorZipC Div |
304 | arctan2' = vectorZipC ATan2 | 292 | arctan2' = vectorZipC ATan2 |
305 | 293 | ||
306 | instance SContainer Vector (Complex Float) | 294 | instance Container Vector (Complex Float) |
307 | where | 295 | where |
308 | conj' = conjugateQ | 296 | conj' = conjugateQ |
309 | size' = dim | 297 | size' = dim |
@@ -329,16 +317,13 @@ instance SContainer Vector (Complex Float) | |||
329 | assoc' = assocV | 317 | assoc' = assocV |
330 | accum' = accumV | 318 | accum' = accumV |
331 | cond' = undefined -- cannot match | 319 | cond' = undefined -- cannot match |
332 | |||
333 | instance Container Vector (Complex Float) | ||
334 | where | ||
335 | scaleRecip = vectorMapValQ Recip | 320 | scaleRecip = vectorMapValQ Recip |
336 | divide = vectorZipQ Div | 321 | divide = vectorZipQ Div |
337 | arctan2' = vectorZipQ ATan2 | 322 | arctan2' = vectorZipQ ATan2 |
338 | 323 | ||
339 | --------------------------------------------------------------- | 324 | --------------------------------------------------------------- |
340 | 325 | ||
341 | instance (Num a, Element a, SContainer Vector a) => SContainer Matrix a | 326 | instance (Num a, Element a, Container Vector a) => Container Matrix a |
342 | where | 327 | where |
343 | conj' = liftMatrix conj' | 328 | conj' = liftMatrix conj' |
344 | size' = size | 329 | size' = size |
@@ -366,10 +351,6 @@ instance (Num a, Element a, SContainer Vector a) => SContainer Matrix a | |||
366 | assoc' = assocM | 351 | assoc' = assocM |
367 | accum' = accumM | 352 | accum' = accumM |
368 | cond' = condM | 353 | cond' = condM |
369 | |||
370 | |||
371 | instance (Fractional a, Container Vector a) => Container Matrix a | ||
372 | where | ||
373 | scaleRecip x = liftMatrix (scaleRecip x) | 354 | scaleRecip x = liftMatrix (scaleRecip x) |
374 | divide = liftMatrix2 divide | 355 | divide = liftMatrix2 divide |
375 | arctan2' = liftMatrix2 arctan2' | 356 | arctan2' = liftMatrix2 arctan2' |
@@ -404,7 +385,7 @@ conj = conj' | |||
404 | scale :: Container c e => e -> c e -> c e | 385 | scale :: Container c e => e -> c e -> c e |
405 | scale = scale' | 386 | scale = scale' |
406 | 387 | ||
407 | arctan2 :: Container c e => c e -> c e -> c e | 388 | arctan2 :: (Fractional e, Container c e) => c e -> c e -> c e |
408 | arctan2 = arctan2' | 389 | arctan2 = arctan2' |
409 | 390 | ||
410 | -- | like 'fmap' (cannot implement instance Functor because of Element class constraint) | 391 | -- | like 'fmap' (cannot implement instance Functor because of Element class constraint) |
@@ -754,7 +735,7 @@ buildV n f = fromList [f k | k <- ks] | |||
754 | 735 | ||
755 | -------------------------------------------------------- | 736 | -------------------------------------------------------- |
756 | -- | conjugate transpose | 737 | -- | conjugate transpose |
757 | ctrans :: (SContainer Vector e, Element e) => Matrix e -> Matrix e | 738 | ctrans :: (Container Vector e, Element e) => Matrix e -> Matrix e |
758 | ctrans = liftMatrix conj' . trans | 739 | ctrans = liftMatrix conj' . trans |
759 | 740 | ||
760 | -- | Creates a square matrix with a given diagonal. | 741 | -- | Creates a square matrix with a given diagonal. |
@@ -810,7 +791,7 @@ class Transposable m mt | m -> mt, mt -> m | |||
810 | -- | (conjugate) transpose | 791 | -- | (conjugate) transpose |
811 | tr :: m -> mt | 792 | tr :: m -> mt |
812 | 793 | ||
813 | instance (SContainer Vector t) => Transposable (Matrix t) (Matrix t) | 794 | instance (Container Vector t) => Transposable (Matrix t) (Matrix t) |
814 | where | 795 | where |
815 | tr = ctrans | 796 | tr = ctrans |
816 | 797 | ||
diff --git a/packages/base/src/Data/Packed/Numeric.hs b/packages/base/src/Data/Packed/Numeric.hs index 4d66f27..d11ecf9 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 | SContainer(..), Container(..), Numeric, | 34 | 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, |
@@ -88,7 +88,7 @@ Logarithmic spacing can be defined as follows: | |||
88 | 88 | ||
89 | @logspace n (a,b) = 10 ** linspace n (a,b)@ | 89 | @logspace n (a,b) = 10 ** linspace n (a,b)@ |
90 | -} | 90 | -} |
91 | linspace :: (Container Vector e) => Int -> (e, e) -> Vector e | 91 | linspace :: (Fractional e, Container Vector e) => Int -> (e, e) -> Vector e |
92 | linspace 0 _ = fromList[] | 92 | linspace 0 _ = fromList[] |
93 | linspace 1 (a,b) = fromList[(a+b)/2] | 93 | linspace 1 (a,b) = fromList[(a+b)/2] |
94 | linspace n (a,b) = addConstant a $ scale s $ fromList $ map fromIntegral [0 .. n-1] | 94 | linspace n (a,b) = addConstant a $ scale s $ fromList $ map fromIntegral [0 .. n-1] |
@@ -219,11 +219,11 @@ class Konst e d c | d -> c, c -> d | |||
219 | -- | 219 | -- |
220 | konst :: e -> d -> c e | 220 | konst :: e -> d -> c e |
221 | 221 | ||
222 | instance SContainer Vector e => Konst e Int Vector | 222 | instance Container Vector e => Konst e Int Vector |
223 | where | 223 | where |
224 | konst = konst' | 224 | konst = konst' |
225 | 225 | ||
226 | instance (Num e, SContainer Vector e) => Konst e (Int,Int) Matrix | 226 | instance (Num e, Container Vector e) => Konst e (Int,Int) Matrix |
227 | where | 227 | where |
228 | konst = konst' | 228 | konst = konst' |
229 | 229 | ||
@@ -246,11 +246,11 @@ class Build d f c e | d -> c, c -> d, f -> e, f -> d, f -> c, c e -> f, d e -> f | |||
246 | -- | 246 | -- |
247 | build :: d -> f -> c e | 247 | build :: d -> f -> c e |
248 | 248 | ||
249 | instance SContainer Vector e => Build Int (e -> e) Vector e | 249 | instance Container Vector e => Build Int (e -> e) Vector e |
250 | where | 250 | where |
251 | build = build' | 251 | build = build' |
252 | 252 | ||
253 | instance SContainer Matrix e => Build (Int,Int) (e -> e -> e) Matrix e | 253 | instance Container Matrix e => Build (Int,Int) (e -> e -> e) Matrix e |
254 | where | 254 | where |
255 | build = build' | 255 | build = build' |
256 | 256 | ||
@@ -299,5 +299,6 @@ instance Numeric Double | |||
299 | instance Numeric (Complex Double) | 299 | instance Numeric (Complex Double) |
300 | instance Numeric Float | 300 | instance Numeric Float |
301 | instance Numeric (Complex Float) | 301 | instance Numeric (Complex Float) |
302 | instance Numeric CInt | ||
302 | 303 | ||
303 | 304 | ||