summaryrefslogtreecommitdiff
path: root/lib/Numeric/ContainerBoot.hs
diff options
context:
space:
mode:
Diffstat (limited to 'lib/Numeric/ContainerBoot.hs')
-rw-r--r--lib/Numeric/ContainerBoot.hs51
1 files changed, 36 insertions, 15 deletions
diff --git a/lib/Numeric/ContainerBoot.hs b/lib/Numeric/ContainerBoot.hs
index d61633e..a333489 100644
--- a/lib/Numeric/ContainerBoot.hs
+++ b/lib/Numeric/ContainerBoot.hs
@@ -66,6 +66,11 @@ type instance ArgOf Matrix a = a -> a -> a
66-- | Basic element-by-element functions for numeric containers 66-- | Basic element-by-element functions for numeric containers
67class (Complexable c, Fractional e, Element e) => Container c e where 67class (Complexable c, Fractional e, Element e) => Container c e where
68 -- | create a structure with a single element 68 -- | create a structure with a single element
69 --
70 -- >>> let v = fromList [1..3::Double]
71 -- >>> v / scalar (norm2 v)
72 -- fromList [0.2672612419124244,0.5345224838248488,0.8017837257372732]
73 --
69 scalar :: e -> c e 74 scalar :: e -> c e
70 -- | complex conjugate 75 -- | complex conjugate
71 conj :: c e -> c e 76 conj :: c e -> c e
@@ -114,8 +119,9 @@ class (Complexable c, Fractional e, Element e) => Container c e where
114 119
115 -- | A more efficient implementation of @cmap (\\x -> if x>0 then 1 else 0)@ 120 -- | A more efficient implementation of @cmap (\\x -> if x>0 then 1 else 0)@
116 -- 121 --
117 -- @> step $ linspace 5 (-1,1::Double) 122 -- >>> step $ linspace 5 (-1,1::Double)
118 -- 5 |> [0.0,0.0,0.0,1.0,1.0]@ 123 -- 5 |> [0.0,0.0,0.0,1.0,1.0]
124 --
119 125
120 step :: RealElement e => c e -> c e 126 step :: RealElement e => c e -> c e
121 127
@@ -123,11 +129,12 @@ class (Complexable c, Fractional e, Element e) => Container c e where
123 -- 129 --
124 -- Arguments with any dimension = 1 are automatically expanded: 130 -- Arguments with any dimension = 1 are automatically expanded:
125 -- 131 --
126 -- @> cond ((1>\<4)[1..]) ((3>\<1)[1..]) 0 100 ((3>\<4)[1..]) :: Matrix Double 132 -- >>> cond ((1><4)[1..]) ((3><1)[1..]) 0 100 ((3><4)[1..]) :: Matrix Double
127 -- (3><4) 133 -- (3><4)
128 -- [ 100.0, 2.0, 3.0, 4.0 134 -- [ 100.0, 2.0, 3.0, 4.0
129 -- , 0.0, 100.0, 7.0, 8.0 135 -- , 0.0, 100.0, 7.0, 8.0
130 -- , 0.0, 0.0, 100.0, 12.0 ]@ 136 -- , 0.0, 0.0, 100.0, 12.0 ]
137 --
131 138
132 cond :: RealElement e 139 cond :: RealElement e
133 => c e -- ^ a 140 => c e -- ^ a
@@ -139,16 +146,22 @@ class (Complexable c, Fractional e, Element e) => Container c e where
139 146
140 -- | Find index of elements which satisfy a predicate 147 -- | Find index of elements which satisfy a predicate
141 -- 148 --
142 -- @> find (>0) (ident 3 :: Matrix Double) 149 -- >>> find (>0) (ident 3 :: Matrix Double)
143 -- [(0,0),(1,1),(2,2)]@ 150 -- [(0,0),(1,1),(2,2)]
151 --
144 152
145 find :: (e -> Bool) -> c e -> [IndexOf c] 153 find :: (e -> Bool) -> c e -> [IndexOf c]
146 154
147 -- | Create a structure from an association list 155 -- | Create a structure from an association list
148 -- 156 --
149 -- @> assoc 5 0 [(2,7),(1,3)] :: Vector Double 157 -- >>> assoc 5 0 [(3,7),(1,4)] :: Vector Double
150 -- 5 |> [0.0,3.0,7.0,0.0,0.0]@ 158 -- fromList [0.0,4.0,0.0,7.0,0.0]
151 159 --
160 -- >>> assoc (2,3) 0 [((0,2),7),((1,0),2*i-3)] :: Matrix (Complex Double)
161 -- (2><3)
162 -- [ 0.0 :+ 0.0, 0.0 :+ 0.0, 7.0 :+ 0.0
163 -- , (-3.0) :+ 2.0, 0.0 :+ 0.0, 0.0 :+ 0.0 ]
164 --
152 assoc :: IndexOf c -- ^ size 165 assoc :: IndexOf c -- ^ size
153 -> e -- ^ default value 166 -> e -- ^ default value
154 -> [(IndexOf c, e)] -- ^ association list 167 -> [(IndexOf c, e)] -- ^ association list
@@ -156,13 +169,19 @@ class (Complexable c, Fractional e, Element e) => Container c e where
156 169
157 -- | Modify a structure using an update function 170 -- | Modify a structure using an update function
158 -- 171 --
159 -- @> accum (ident 5) (+) [((1,1),5),((0,3),3)] :: Matrix Double 172 -- >>> accum (ident 5) (+) [((1,1),5),((0,3),3)] :: Matrix Double
160 -- (5><5) 173 -- (5><5)
161 -- [ 1.0, 0.0, 0.0, 3.0, 0.0 174 -- [ 1.0, 0.0, 0.0, 3.0, 0.0
162 -- , 0.0, 6.0, 0.0, 0.0, 0.0 175 -- , 0.0, 6.0, 0.0, 0.0, 0.0
163 -- , 0.0, 0.0, 1.0, 0.0, 0.0 176 -- , 0.0, 0.0, 1.0, 0.0, 0.0
164 -- , 0.0, 0.0, 0.0, 1.0, 0.0 177 -- , 0.0, 0.0, 0.0, 1.0, 0.0
165 -- , 0.0, 0.0, 0.0, 0.0, 1.0 ]@ 178 -- , 0.0, 0.0, 0.0, 0.0, 1.0 ]
179 --
180 -- computation of histogram:
181 --
182 -- >>> accum (konst 0 7) (+) (map (flip (,) 1) [4,5,4,1,5,2,5]) :: Vector Double
183 -- fromList [0.0,1.0,1.0,0.0,2.0,3.0,0.0]
184 --
166 185
167 accum :: c e -- ^ initial structure 186 accum :: c e -- ^ initial structure
168 -> (e -> e -> e) -- ^ update function 187 -> (e -> e -> e) -- ^ update function
@@ -382,11 +401,12 @@ vXm v m = flatten $ (asRow v) `mXm` m
382 401
383{- | Outer product of two vectors. 402{- | Outer product of two vectors.
384 403
385@\> 'fromList' [1,2,3] \`outer\` 'fromList' [5,2,3] 404>>> fromList [1,2,3] `outer` fromList [5,2,3]
386(3><3) 405(3><3)
387 [ 5.0, 2.0, 3.0 406 [ 5.0, 2.0, 3.0
388 , 10.0, 4.0, 6.0 407 , 10.0, 4.0, 6.0
389 , 15.0, 6.0, 9.0 ]@ 408 , 15.0, 6.0, 9.0 ]
409
390-} 410-}
391outer :: (Product t) => Vector t -> Vector t -> Matrix t 411outer :: (Product t) => Vector t -> Vector t -> Matrix t
392outer u v = asColumn u `multiply` asRow v 412outer u v = asColumn u `multiply` asRow v
@@ -402,7 +422,7 @@ m2=(4><3)
402 , 7.0, 8.0, 9.0 422 , 7.0, 8.0, 9.0
403 , 10.0, 11.0, 12.0 ]@ 423 , 10.0, 11.0, 12.0 ]@
404 424
405@\> kronecker m1 m2 425>>> kronecker m1 m2
406(8><9) 426(8><9)
407 [ 1.0, 2.0, 3.0, 2.0, 4.0, 6.0, 0.0, 0.0, 0.0 427 [ 1.0, 2.0, 3.0, 2.0, 4.0, 6.0, 0.0, 0.0, 0.0
408 , 4.0, 5.0, 6.0, 8.0, 10.0, 12.0, 0.0, 0.0, 0.0 428 , 4.0, 5.0, 6.0, 8.0, 10.0, 12.0, 0.0, 0.0, 0.0
@@ -411,7 +431,8 @@ m2=(4><3)
411 , 0.0, 0.0, 0.0, -1.0, -2.0, -3.0, 3.0, 6.0, 9.0 431 , 0.0, 0.0, 0.0, -1.0, -2.0, -3.0, 3.0, 6.0, 9.0
412 , 0.0, 0.0, 0.0, -4.0, -5.0, -6.0, 12.0, 15.0, 18.0 432 , 0.0, 0.0, 0.0, -4.0, -5.0, -6.0, 12.0, 15.0, 18.0
413 , 0.0, 0.0, 0.0, -7.0, -8.0, -9.0, 21.0, 24.0, 27.0 433 , 0.0, 0.0, 0.0, -7.0, -8.0, -9.0, 21.0, 24.0, 27.0
414 , 0.0, 0.0, 0.0, -10.0, -11.0, -12.0, 30.0, 33.0, 36.0 ]@ 434 , 0.0, 0.0, 0.0, -10.0, -11.0, -12.0, 30.0, 33.0, 36.0 ]
435
415-} 436-}
416kronecker :: (Product t) => Matrix t -> Matrix t -> Matrix t 437kronecker :: (Product t) => Matrix t -> Matrix t -> Matrix t
417kronecker a b = fromBlocks 438kronecker a b = fromBlocks