summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlberto Ruiz <aruiz@um.es>2015-05-23 12:31:32 +0200
committerAlberto Ruiz <aruiz@um.es>2015-05-23 12:31:32 +0200
commitb1b445697db31b1603a31747ca31151f97ee7263 (patch)
tree453bbe1e16e2abd0b605e7807a33de4eaba4a866
parent36379e29fe99b033313f42464897c32b9805559d (diff)
join SContainer and Container using Fractional contexts
-rw-r--r--packages/base/src/Data/Packed/Internal/Numeric.hs53
-rw-r--r--packages/base/src/Data/Packed/Numeric.hs13
-rw-r--r--packages/base/src/Numeric/Container.hs3
-rw-r--r--packages/base/src/Numeric/LinearAlgebra.hs2
-rw-r--r--packages/base/src/Numeric/LinearAlgebra/Static/Internal.hs8
-rw-r--r--packages/base/src/Numeric/Matrix.hs4
-rw-r--r--packages/base/src/Numeric/Vector.hs2
7 files changed, 33 insertions, 52 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
118class Element e => SContainer c e 118class 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
160class (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
175instance SContainer Vector CInt 171instance 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 201instance Container Vector Float
203instance 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
230instance 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
238instance SContainer Vector Double 233instance 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
265instance 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
272instance SContainer Vector (Complex Double) 264instance 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
300instance 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
306instance SContainer Vector (Complex Float) 294instance 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
333instance 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
341instance (Num a, Element a, SContainer Vector a) => SContainer Matrix a 326instance (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
371instance (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'
404scale :: Container c e => e -> c e -> c e 385scale :: Container c e => e -> c e -> c e
405scale = scale' 386scale = scale'
406 387
407arctan2 :: Container c e => c e -> c e -> c e 388arctan2 :: (Fractional e, Container c e) => c e -> c e -> c e
408arctan2 = arctan2' 389arctan2 = 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
757ctrans :: (SContainer Vector e, Element e) => Matrix e -> Matrix e 738ctrans :: (Container Vector e, Element e) => Matrix e -> Matrix e
758ctrans = liftMatrix conj' . trans 739ctrans = 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
813instance (SContainer Vector t) => Transposable (Matrix t) (Matrix t) 794instance (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-}
91linspace :: (Container Vector e) => Int -> (e, e) -> Vector e 91linspace :: (Fractional e, Container Vector e) => Int -> (e, e) -> Vector e
92linspace 0 _ = fromList[] 92linspace 0 _ = fromList[]
93linspace 1 (a,b) = fromList[(a+b)/2] 93linspace 1 (a,b) = fromList[(a+b)/2]
94linspace n (a,b) = addConstant a $ scale s $ fromList $ map fromIntegral [0 .. n-1] 94linspace 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
222instance SContainer Vector e => Konst e Int Vector 222instance Container Vector e => Konst e Int Vector
223 where 223 where
224 konst = konst' 224 konst = konst'
225 225
226instance (Num e, SContainer Vector e) => Konst e (Int,Int) Matrix 226instance (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
249instance SContainer Vector e => Build Int (e -> e) Vector e 249instance Container Vector e => Build Int (e -> e) Vector e
250 where 250 where
251 build = build' 251 build = build'
252 252
253instance SContainer Matrix e => Build (Int,Int) (e -> e -> e) Matrix e 253instance 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
299instance Numeric (Complex Double) 299instance Numeric (Complex Double)
300instance Numeric Float 300instance Numeric Float
301instance Numeric (Complex Float) 301instance Numeric (Complex Float)
302instance Numeric CInt
302 303
303 304
diff --git a/packages/base/src/Numeric/Container.hs b/packages/base/src/Numeric/Container.hs
index 5c1a28b..48291a1 100644
--- a/packages/base/src/Numeric/Container.hs
+++ b/packages/base/src/Numeric/Container.hs
@@ -7,8 +7,7 @@ module Numeric.Container(
7 diag, 7 diag,
8 ident, 8 ident,
9 ctrans, 9 ctrans,
10 SContainer(addConstant,add, sub, mul, equal), 10 Container(addConstant,add, sub, mul, equal,scaleRecip,divide),
11 Container(scaleRecip,divide),
12 scalar, 11 scalar,
13 conj, 12 conj,
14 scale, 13 scale,
diff --git a/packages/base/src/Numeric/LinearAlgebra.hs b/packages/base/src/Numeric/LinearAlgebra.hs
index 246c728..4ba0c98 100644
--- a/packages/base/src/Numeric/LinearAlgebra.hs
+++ b/packages/base/src/Numeric/LinearAlgebra.hs
@@ -137,7 +137,7 @@ module Numeric.LinearAlgebra (
137 meanCov, rowOuters, pairwiseD2, unitary, peps, relativeError, haussholder, optimiseMult, udot, nullspaceSVD, orthSVD, ranksv, 137 meanCov, rowOuters, pairwiseD2, unitary, peps, relativeError, haussholder, optimiseMult, udot, nullspaceSVD, orthSVD, ranksv,
138 ℝ,ℂ,iC, 138 ℝ,ℂ,iC,
139 -- * Auxiliary classes 139 -- * Auxiliary classes
140 Element, SContainer, Container, Product, Numeric, LSDiv, 140 Element, Container, Product, Numeric, LSDiv,
141 Complexable, RealElement, 141 Complexable, RealElement,
142 RealOf, ComplexOf, SingleOf, DoubleOf, 142 RealOf, ComplexOf, SingleOf, DoubleOf,
143 IndexOf, 143 IndexOf,
diff --git a/packages/base/src/Numeric/LinearAlgebra/Static/Internal.hs b/packages/base/src/Numeric/LinearAlgebra/Static/Internal.hs
index a5fc29b..7ecb132 100644
--- a/packages/base/src/Numeric/LinearAlgebra/Static/Internal.hs
+++ b/packages/base/src/Numeric/LinearAlgebra/Static/Internal.hs
@@ -322,12 +322,12 @@ instance forall n t . (Num (Vector t), Numeric t )=> Num (Dim n (Vector t))
322 negate = lift1F negate 322 negate = lift1F negate
323 fromInteger x = Dim (fromInteger x) 323 fromInteger x = Dim (fromInteger x)
324 324
325instance (Num (Vector t), Num (Matrix t), Numeric t) => Fractional (Dim n (Vector t)) 325instance (Num (Vector t), Num (Matrix t), Fractional t, Numeric t) => Fractional (Dim n (Vector t))
326 where 326 where
327 fromRational x = Dim (fromRational x) 327 fromRational x = Dim (fromRational x)
328 (/) = lift2F (/) 328 (/) = lift2F (/)
329 329
330instance (Floating (Vector t), Numeric t) => Floating (Dim n (Vector t)) where 330instance (Fractional t, Floating (Vector t), Numeric t) => Floating (Dim n (Vector t)) where
331 sin = lift1F sin 331 sin = lift1F sin
332 cos = lift1F cos 332 cos = lift1F cos
333 tan = lift1F tan 333 tan = lift1F tan
@@ -357,12 +357,12 @@ instance (Num (Matrix t), Numeric t) => Num (Dim m (Dim n (Matrix t)))
357 negate = (lift1F . lift1F) negate 357 negate = (lift1F . lift1F) negate
358 fromInteger x = Dim (Dim (fromInteger x)) 358 fromInteger x = Dim (Dim (fromInteger x))
359 359
360instance (Num (Vector t), Num (Matrix t), Numeric t) => Fractional (Dim m (Dim n (Matrix t))) 360instance (Num (Vector t), Num (Matrix t), Fractional t, Numeric t) => Fractional (Dim m (Dim n (Matrix t)))
361 where 361 where
362 fromRational x = Dim (Dim (fromRational x)) 362 fromRational x = Dim (Dim (fromRational x))
363 (/) = (lift2F.lift2F) (/) 363 (/) = (lift2F.lift2F) (/)
364 364
365instance (Num (Vector t), Floating (Matrix t), Numeric t) => Floating (Dim m (Dim n (Matrix t))) where 365instance (Num (Vector t), Floating (Matrix t), Fractional t, Numeric t) => Floating (Dim m (Dim n (Matrix t))) where
366 sin = (lift1F . lift1F) sin 366 sin = (lift1F . lift1F) sin
367 cos = (lift1F . lift1F) cos 367 cos = (lift1F . lift1F) cos
368 tan = (lift1F . lift1F) tan 368 tan = (lift1F . lift1F) tan
diff --git a/packages/base/src/Numeric/Matrix.hs b/packages/base/src/Numeric/Matrix.hs
index a9022c6..5f27652 100644
--- a/packages/base/src/Numeric/Matrix.hs
+++ b/packages/base/src/Numeric/Matrix.hs
@@ -37,7 +37,7 @@ import Numeric.Chain
37instance Container Matrix a => Eq (Matrix a) where 37instance Container Matrix a => Eq (Matrix a) where
38 (==) = equal 38 (==) = equal
39 39
40instance (Container Matrix a, Num (Vector a)) => Num (Matrix a) where 40instance (Container Matrix a, Num a, Num (Vector a)) => Num (Matrix a) where
41 (+) = liftMatrix2Auto (+) 41 (+) = liftMatrix2Auto (+)
42 (-) = liftMatrix2Auto (-) 42 (-) = liftMatrix2Auto (-)
43 negate = liftMatrix negate 43 negate = liftMatrix negate
@@ -48,7 +48,7 @@ instance (Container Matrix a, Num (Vector a)) => Num (Matrix a) where
48 48
49--------------------------------------------------- 49---------------------------------------------------
50 50
51instance (Container Vector a, Fractional (Vector a), Num (Matrix a)) => Fractional (Matrix a) where 51instance (Container Vector a, Fractional a, Fractional (Vector a), Num (Matrix a)) => Fractional (Matrix a) where
52 fromRational n = (1><1) [fromRational n] 52 fromRational n = (1><1) [fromRational n]
53 (/) = liftMatrix2Auto (/) 53 (/) = liftMatrix2Auto (/)
54 54
diff --git a/packages/base/src/Numeric/Vector.hs b/packages/base/src/Numeric/Vector.hs
index 28b453f..1c16871 100644
--- a/packages/base/src/Numeric/Vector.hs
+++ b/packages/base/src/Numeric/Vector.hs
@@ -66,7 +66,7 @@ instance Num (Vector (Complex Float)) where
66 66
67--------------------------------------------------- 67---------------------------------------------------
68 68
69instance (Container Vector a, Num (Vector a)) => Fractional (Vector a) where 69instance (Container Vector a, Num (Vector a), Fractional a) => Fractional (Vector a) where
70 fromRational n = fromList [fromRational n] 70 fromRational n = fromList [fromRational n]
71 (/) = adaptScalar f divide g where 71 (/) = adaptScalar f divide g where
72 r `f` v = scaleRecip r v 72 r `f` v = scaleRecip r v