summaryrefslogtreecommitdiff
path: root/lib/Numeric
diff options
context:
space:
mode:
authorAlberto Ruiz <aruiz@um.es>2010-09-23 08:04:33 +0000
committerAlberto Ruiz <aruiz@um.es>2010-09-23 08:04:33 +0000
commit778e63dcc2b348914a4f6975f5328ff7fd25638a (patch)
treef52df4d9e0ad86ab037cd6124e1cfed2b2454d0f /lib/Numeric
parent0c739613926ac44254a9de9ea81351a974805c45 (diff)
experimental build, build', konst'
Diffstat (limited to 'lib/Numeric')
-rw-r--r--lib/Numeric/Container.hs67
1 files changed, 63 insertions, 4 deletions
diff --git a/lib/Numeric/Container.hs b/lib/Numeric/Container.hs
index 3b590de..1c542b8 100644
--- a/lib/Numeric/Container.hs
+++ b/lib/Numeric/Container.hs
@@ -36,6 +36,8 @@ module Numeric.Container (
36 36
37 IndexOf, 37 IndexOf,
38 module Data.Complex, 38 module Data.Complex,
39 -- * Experimental
40 build', konst',
39 -- * Deprecated 41 -- * Deprecated
40 (.*),(*/),(<|>),(<->), 42 (.*),(*/),(<|>),(<->),
41 vectorMax,vectorMin, 43 vectorMax,vectorMin,
@@ -61,6 +63,11 @@ type family IndexOf c
61type instance IndexOf Vector = Int 63type instance IndexOf Vector = Int
62type instance IndexOf Matrix = (Int,Int) 64type instance IndexOf Matrix = (Int,Int)
63 65
66type family ArgOf c a
67
68type instance ArgOf Vector a = a -> a
69type instance ArgOf Matrix a = a -> a -> a
70
64------------------------------------------------------------------- 71-------------------------------------------------------------------
65 72
66-- | Basic element-by-element functions for numeric containers 73-- | Basic element-by-element functions for numeric containers
@@ -87,6 +94,13 @@ class (Complexable c, Fractional e, Element e) => Container c e where
87 cmap :: (Element a, Element b) => (a -> b) -> c a -> c b 94 cmap :: (Element a, Element b) => (a -> b) -> c a -> c b
88 -- | constant structure of given size 95 -- | constant structure of given size
89 konst :: e -> IndexOf c -> c e 96 konst :: e -> IndexOf c -> c e
97 -- | create a structure using a function
98 --
99 -- Hilbert matrix of order N:
100 --
101 -- @hilb n = build (n,n) (\\i j -> 1/(i+j+1))@
102 build :: IndexOf c -> (ArgOf c e) -> c e
103 --build :: BoundsOf f -> f -> (ContainerOf f) e
90 -- 104 --
91 -- | indexing function 105 -- | indexing function
92 atIndex :: c e -> IndexOf c -> e 106 atIndex :: c e -> IndexOf c -> e
@@ -104,10 +118,6 @@ class (Complexable c, Fractional e, Element e) => Container c e where
104 -- | the product of elements (faster than using @fold@) 118 -- | the product of elements (faster than using @fold@)
105 prodElements :: c e -> e 119 prodElements :: c e -> e
106 120
107-- -- | Basic element-by-element functions.
108-- class (Element e, Container c e) => Linear c e where
109
110
111-------------------------------------------------------------------------- 121--------------------------------------------------------------------------
112 122
113instance Container Vector Float where 123instance Container Vector Float where
@@ -121,6 +131,7 @@ instance Container Vector Float where
121 equal u v = dim u == dim v && maxElement (vectorMapF Abs (sub u v)) == 0.0 131 equal u v = dim u == dim v && maxElement (vectorMapF Abs (sub u v)) == 0.0
122 scalar x = fromList [x] 132 scalar x = fromList [x]
123 konst = constantD 133 konst = constantD
134 build = buildV
124 conj = id 135 conj = id
125 cmap = mapVector 136 cmap = mapVector
126 atIndex = (@>) 137 atIndex = (@>)
@@ -142,6 +153,7 @@ instance Container Vector Double where
142 equal u v = dim u == dim v && maxElement (vectorMapR Abs (sub u v)) == 0.0 153 equal u v = dim u == dim v && maxElement (vectorMapR Abs (sub u v)) == 0.0
143 scalar x = fromList [x] 154 scalar x = fromList [x]
144 konst = constantD 155 konst = constantD
156 build = buildV
145 conj = id 157 conj = id
146 cmap = mapVector 158 cmap = mapVector
147 atIndex = (@>) 159 atIndex = (@>)
@@ -163,6 +175,7 @@ instance Container Vector (Complex Double) where
163 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
164 scalar x = fromList [x] 176 scalar x = fromList [x]
165 konst = constantD 177 konst = constantD
178 build = buildV
166 conj = conjugateC 179 conj = conjugateC
167 cmap = mapVector 180 cmap = mapVector
168 atIndex = (@>) 181 atIndex = (@>)
@@ -184,6 +197,7 @@ instance Container Vector (Complex Float) where
184 equal u v = dim u == dim v && maxElement (mapVector magnitude (sub u v)) == 0.0 197 equal u v = dim u == dim v && maxElement (mapVector magnitude (sub u v)) == 0.0
185 scalar x = fromList [x] 198 scalar x = fromList [x]
186 konst = constantD 199 konst = constantD
200 build = buildV
187 conj = conjugateQ 201 conj = conjugateQ
188 cmap = mapVector 202 cmap = mapVector
189 atIndex = (@>) 203 atIndex = (@>)
@@ -207,6 +221,7 @@ instance (Container Vector a) => Container Matrix a where
207 equal a b = cols a == cols b && flatten a `equal` flatten b 221 equal a b = cols a == cols b && flatten a `equal` flatten b
208 scalar x = (1><1) [x] 222 scalar x = (1><1) [x]
209 konst v (r,c) = reshape c (konst v (r*c)) 223 konst v (r,c) = reshape c (konst v (r*c))
224 build = buildM
210 conj = liftMatrix conj 225 conj = liftMatrix conj
211 cmap f = liftMatrix (mapVector f) 226 cmap f = liftMatrix (mapVector f)
212 atIndex = (@@>) 227 atIndex = (@@>)
@@ -500,3 +515,47 @@ vectorMaxIndex = round . toScalarR MaxIdx
500vectorMinIndex :: Vector Double -> Int 515vectorMinIndex :: Vector Double -> Int
501vectorMinIndex = round . toScalarR MinIdx 516vectorMinIndex = round . toScalarR MinIdx
502 517
518-----------------------------------------------------
519
520class Build f where
521 build' :: BoundsOf f -> f -> ContainerOf f
522
523type family BoundsOf x
524
525type instance BoundsOf (a->a) = Int
526type instance BoundsOf (a->a->a) = (Int,Int)
527
528type family ContainerOf x
529
530type instance ContainerOf (a->a) = Vector a
531type instance ContainerOf (a->a->a) = Matrix a
532
533instance (Element a, Num a) => Build (a->a) where
534 build' = buildV
535
536instance (Element a, Num a) => Build (a->a->a) where
537 build' = buildM
538
539buildM (rc,cc) f = fromLists [ [f r c | c <- cs] | r <- rs ]
540 where rs = map fromIntegral [0 .. (rc-1)]
541 cs = map fromIntegral [0 .. (cc-1)]
542
543buildV n f = fromList [f k | k <- ks]
544 where ks = map fromIntegral [0 .. (n-1)]
545
546----------------------------------------------------
547-- experimental
548
549class Konst s where
550 konst' :: Element e => e -> s -> ContainerOf' s e
551
552type family ContainerOf' x y
553
554type instance ContainerOf' Int a = Vector a
555type instance ContainerOf' (Int,Int) a = Matrix a
556
557instance Konst Int where
558 konst' = constantD
559
560instance Konst (Int,Int) where
561 konst' k (r,c) = reshape c $ konst' k (r*c)