summaryrefslogtreecommitdiff
path: root/lib/Numeric/Container.hs
diff options
context:
space:
mode:
authorAlberto Ruiz <aruiz@um.es>2010-09-13 18:21:15 +0000
committerAlberto Ruiz <aruiz@um.es>2010-09-13 18:21:15 +0000
commit9d54e7b9543c22d398ef5097e3ba619ca974820b (patch)
treea71b00a450eb22224b389ad501806b92a6a09864 /lib/Numeric/Container.hs
parent5323b5f9c5da53ab413202141667142e646897d4 (diff)
remove ComplexContainer
Diffstat (limited to 'lib/Numeric/Container.hs')
-rw-r--r--lib/Numeric/Container.hs103
1 files changed, 95 insertions, 8 deletions
diff --git a/lib/Numeric/Container.hs b/lib/Numeric/Container.hs
index aaa068f..e9a8f22 100644
--- a/lib/Numeric/Container.hs
+++ b/lib/Numeric/Container.hs
@@ -25,9 +25,10 @@ module Numeric.Container (
25 mXm,mXv,vXm, 25 mXm,mXv,vXm,
26 outer, kronecker, 26 outer, kronecker,
27 27
28 RealElement, --Precision, 28 Convert(..),
29 ComplexContainer(toComplex,fromComplex,comp,conj), 29 Complexable(),
30 Convert(..), --AutoReal(..), 30 RealElement(),
31
31 RealOf, ComplexOf, SingleOf, DoubleOf, 32 RealOf, ComplexOf, SingleOf, DoubleOf,
32 33
33 IndexOf, 34 IndexOf,
@@ -54,10 +55,11 @@ type instance IndexOf Matrix = (Int,Int)
54------------------------------------------------------------------- 55-------------------------------------------------------------------
55 56
56-- | Basic element-by-element functions for numeric containers 57-- | Basic element-by-element functions for numeric containers
57class (Element e) => Container c e where 58class (Complexable c, Element e) => Container c e where
58
59 -- | create a structure with a single element 59 -- | create a structure with a single element
60 scalar :: e -> c e 60 scalar :: e -> c e
61 -- | complex conjugate
62 conj :: c e -> c e
61 scale :: e -> c e -> c e 63 scale :: e -> c e -> c e
62 -- | scale the element by element reciprocal of the object: 64 -- | scale the element by element reciprocal of the object:
63 -- 65 --
@@ -75,7 +77,7 @@ class (Element e) => Container c e where
75 -- | cannot implement instance Functor because of Element class constraint 77 -- | cannot implement instance Functor because of Element class constraint
76 cmap :: (Element a, Element b) => (a -> b) -> c a -> c b 78 cmap :: (Element a, Element b) => (a -> b) -> c a -> c b
77 -- | constant structure of given size 79 -- | constant structure of given size
78 konst :: e -> IndexOf c -> c e 80 konst :: e -> IndexOf c -> c e
79 -- 81 --
80 -- | indexing function 82 -- | indexing function
81 atIndex :: c e -> IndexOf c -> e 83 atIndex :: c e -> IndexOf c -> e
@@ -110,6 +112,7 @@ instance Container Vector Float where
110 equal u v = dim u == dim v && maxElement (vectorMapF Abs (sub u v)) == 0.0 112 equal u v = dim u == dim v && maxElement (vectorMapF Abs (sub u v)) == 0.0
111 scalar x = fromList [x] 113 scalar x = fromList [x]
112 konst = constantD 114 konst = constantD
115 conj = conjugateD
113 cmap = mapVector 116 cmap = mapVector
114 atIndex = (@>) 117 atIndex = (@>)
115 minIndex = round . toScalarF MinIdx 118 minIndex = round . toScalarF MinIdx
@@ -130,6 +133,7 @@ instance Container Vector Double where
130 equal u v = dim u == dim v && maxElement (vectorMapR Abs (sub u v)) == 0.0 133 equal u v = dim u == dim v && maxElement (vectorMapR Abs (sub u v)) == 0.0
131 scalar x = fromList [x] 134 scalar x = fromList [x]
132 konst = constantD 135 konst = constantD
136 conj = conjugateD
133 cmap = mapVector 137 cmap = mapVector
134 atIndex = (@>) 138 atIndex = (@>)
135 minIndex = round . toScalarR MinIdx 139 minIndex = round . toScalarR MinIdx
@@ -150,6 +154,7 @@ instance Container Vector (Complex Double) where
150 equal u v = dim u == dim v && maxElement (mapVector magnitude (sub u v)) == 0.0 154 equal u v = dim u == dim v && maxElement (mapVector magnitude (sub u v)) == 0.0
151 scalar x = fromList [x] 155 scalar x = fromList [x]
152 konst = constantD 156 konst = constantD
157 conj = conjugateD
153 cmap = mapVector 158 cmap = mapVector
154 atIndex = (@>) 159 atIndex = (@>)
155 minIndex = minIndex . fst . fromComplex . (zipVectorWith (*) `ap` mapVector conjugate) 160 minIndex = minIndex . fst . fromComplex . (zipVectorWith (*) `ap` mapVector conjugate)
@@ -170,6 +175,7 @@ instance Container Vector (Complex Float) where
170 equal u v = dim u == dim v && maxElement (mapVector magnitude (sub u v)) == 0.0 175 equal u v = dim u == dim v && maxElement (mapVector magnitude (sub u v)) == 0.0
171 scalar x = fromList [x] 176 scalar x = fromList [x]
172 konst = constantD 177 konst = constantD
178 conj = conjugateD
173 cmap = mapVector 179 cmap = mapVector
174 atIndex = (@>) 180 atIndex = (@>)
175 minIndex = minIndex . fst . fromComplex . (zipVectorWith (*) `ap` mapVector conjugate) 181 minIndex = minIndex . fst . fromComplex . (zipVectorWith (*) `ap` mapVector conjugate)
@@ -192,6 +198,7 @@ instance (Container Vector a) => Container Matrix a where
192 equal a b = cols a == cols b && flatten a `equal` flatten b 198 equal a b = cols a == cols b && flatten a `equal` flatten b
193 scalar x = (1><1) [x] 199 scalar x = (1><1) [x]
194 konst v (r,c) = reshape c (konst v (r*c)) 200 konst v (r,c) = reshape c (konst v (r*c))
201 conj = liftMatrix conjugateD
195 cmap f = liftMatrix (mapVector f) 202 cmap f = liftMatrix (mapVector f)
196 atIndex = (@@>) 203 atIndex = (@@>)
197 minIndex m = let (r,c) = (rows m,cols m) 204 minIndex m = let (r,c) = (rows m,cols m)
@@ -208,7 +215,7 @@ instance (Container Vector a) => Container Matrix a where
208---------------------------------------------------- 215----------------------------------------------------
209 216
210 217
211-- | Linear algebraic properties of objects 218-- | Matrix product and related functions
212class Element e => Product e where 219class Element e => Product e where
213 -- | matrix product 220 -- | matrix product
214 multiply :: Matrix e -> Matrix e -> Matrix e 221 multiply :: Matrix e -> Matrix e -> Matrix e
@@ -309,4 +316,84 @@ kronecker a b = fromBlocks
309 . toRows 316 . toRows
310 $ flatten a `outer` flatten b 317 $ flatten a `outer` flatten b
311 318
312---------------------------------------------------------- 319-------------------------------------------------------------------
320
321
322class Convert t where
323 real :: Container c t => c (RealOf t) -> c t
324 complex :: Container c t => c t -> c (ComplexOf t)
325 single :: Container c t => c t -> c (SingleOf t)
326 double :: Container c t => c t -> c (DoubleOf t)
327 toComplex :: (Container c t, RealElement t) => (c t, c t) -> c (Complex t)
328 fromComplex :: (Container c t, RealElement t) => c (Complex t) -> (c t, c t)
329
330
331instance Convert Double where
332 real = id
333 complex = comp'
334 single = single'
335 double = id
336 toComplex = toComplex'
337 fromComplex = fromComplex'
338
339instance Convert Float where
340 real = id
341 complex = comp'
342 single = id
343 double = double'
344 toComplex = toComplex'
345 fromComplex = fromComplex'
346
347instance Convert (Complex Double) where
348 real = comp'
349 complex = id
350 single = single'
351 double = id
352 toComplex = toComplex'
353 fromComplex = fromComplex'
354
355instance Convert (Complex Float) where
356 real = comp'
357 complex = id
358 single = id
359 double = double'
360 toComplex = toComplex'
361 fromComplex = fromComplex'
362
363-------------------------------------------------------------------
364
365type family RealOf x
366
367type instance RealOf Double = Double
368type instance RealOf (Complex Double) = Double
369
370type instance RealOf Float = Float
371type instance RealOf (Complex Float) = Float
372
373type family ComplexOf x
374
375type instance ComplexOf Double = Complex Double
376type instance ComplexOf (Complex Double) = Complex Double
377
378type instance ComplexOf Float = Complex Float
379type instance ComplexOf (Complex Float) = Complex Float
380
381type family SingleOf x
382
383type instance SingleOf Double = Float
384type instance SingleOf Float = Float
385
386type instance SingleOf (Complex a) = Complex (SingleOf a)
387
388type family DoubleOf x
389
390type instance DoubleOf Double = Double
391type instance DoubleOf Float = Double
392
393type instance DoubleOf (Complex a) = Complex (DoubleOf a)
394
395type family ElementOf c
396
397type instance ElementOf (Vector a) = a
398type instance ElementOf (Matrix a) = a
399