diff options
Diffstat (limited to 'packages/hmatrix/src/Numeric')
-rw-r--r-- | packages/hmatrix/src/Numeric/Container.hs | 96 | ||||
-rw-r--r-- | packages/hmatrix/src/Numeric/ContainerBoot.hs | 2 | ||||
-rw-r--r-- | packages/hmatrix/src/Numeric/HMatrix.hs | 7 |
3 files changed, 42 insertions, 63 deletions
diff --git a/packages/hmatrix/src/Numeric/Container.hs b/packages/hmatrix/src/Numeric/Container.hs index 3a8dd94..be2347b 100644 --- a/packages/hmatrix/src/Numeric/Container.hs +++ b/packages/hmatrix/src/Numeric/Container.hs | |||
@@ -36,11 +36,11 @@ module Numeric.Container ( | |||
36 | -- * Generic operations | 36 | -- * Generic operations |
37 | Container(..), | 37 | Container(..), |
38 | -- * Matrix product | 38 | -- * Matrix product |
39 | Product(..), udot, | 39 | Product(..), udot, dot, (◇), |
40 | Mul(..), | 40 | Mul(..), |
41 | Contraction(..), | 41 | Contraction(..), |
42 | optimiseMult, | 42 | optimiseMult, |
43 | mXm,mXv,vXm,LSDiv(..), cdot, (·), dot, (<.>), | 43 | mXm,mXv,vXm,LSDiv(..), |
44 | outer, kronecker, | 44 | outer, kronecker, |
45 | -- * Random numbers | 45 | -- * Random numbers |
46 | RandDist(..), | 46 | RandDist(..), |
@@ -91,18 +91,12 @@ linspace 0 (a,b) = fromList[(a+b)/2] | |||
91 | linspace n (a,b) = addConstant a $ scale s $ fromList $ map fromIntegral [0 .. n-1] | 91 | linspace n (a,b) = addConstant a $ scale s $ fromList $ map fromIntegral [0 .. n-1] |
92 | where s = (b-a)/fromIntegral (n-1) | 92 | where s = (b-a)/fromIntegral (n-1) |
93 | 93 | ||
94 | -- | dot product: @cdot u v = 'udot' ('conj' u) v@ | ||
95 | cdot :: (Container Vector t, Product t) => Vector t -> Vector t -> t | ||
96 | cdot u v = udot (conj u) v | ||
97 | |||
98 | -------------------------------------------------------- | 94 | -------------------------------------------------------- |
99 | 95 | ||
100 | class Contraction a b c | a b -> c, c -> a b | 96 | class Contraction a b c | a b -> c |
101 | where | 97 | where |
102 | infixr 7 × | 98 | infixl 7 <.> |
103 | {- | Matrix-matrix product, matrix-vector product, and unconjugated dot product | 99 | {- | Matrix product, matrix vector product, and dot product |
104 | |||
105 | (unicode 0x00d7, multiplication sign) | ||
106 | 100 | ||
107 | Examples: | 101 | Examples: |
108 | 102 | ||
@@ -118,7 +112,7 @@ Examples: | |||
118 | 112 | ||
119 | matrix × matrix: | 113 | matrix × matrix: |
120 | 114 | ||
121 | >>> disp 2 (a × trans a) | 115 | >>> disp 2 (a <.> trans a) |
122 | 3x3 | 116 | 3x3 |
123 | 30 70 110 | 117 | 30 70 110 |
124 | 70 174 278 | 118 | 70 174 278 |
@@ -126,44 +120,38 @@ matrix × matrix: | |||
126 | 120 | ||
127 | matrix × vector: | 121 | matrix × vector: |
128 | 122 | ||
129 | >>> a × v | 123 | >>> a <.> v |
130 | fromList [3.0,11.0,19.0] | 124 | fromList [3.0,11.0,19.0] |
131 | 125 | ||
132 | unconjugated dot product: | 126 | dot product: |
133 | 127 | ||
134 | >>> fromList [1,i] × fromList[2*i+1,3] | 128 | >>> u <.> fromList[3,2,1::Double] |
135 | 1.0 :+ 5.0 | 129 | 10 |
136 | 130 | ||
137 | (×) is right associative, so we can write: | 131 | For complex vectors the first argument is conjugated: |
138 | 132 | ||
139 | >>> u × a × v | 133 | >>> fromList [1,i] <.> fromList[2*i+1,3] |
140 | 82.0 :: Double | 134 | 1.0 :+ (-1.0) |
141 | |||
142 | -} | ||
143 | (×) :: a -> b -> c | ||
144 | 135 | ||
145 | instance Product t => Contraction (Matrix t) (Vector t) (Vector t) where | 136 | >>> fromList [1,i,1-i] <.> complex a |
146 | (×) = mXv | 137 | fromList [10.0 :+ 4.0,12.0 :+ 4.0,14.0 :+ 4.0,16.0 :+ 4.0] |
147 | 138 | ||
148 | instance Product t => Contraction (Matrix t) (Matrix t) (Matrix t) where | 139 | -} |
149 | (×) = mXm | 140 | (<.>) :: a -> b -> c |
150 | 141 | ||
151 | instance Contraction (Vector Double) (Vector Double) Double where | ||
152 | (×) = udot | ||
153 | 142 | ||
154 | instance Contraction (Vector Float) (Vector Float) Float where | 143 | instance (Product t, Container Vector t) => Contraction (Vector t) (Vector t) t where |
155 | (×) = udot | 144 | u <.> v = conj u `udot` v |
156 | 145 | ||
157 | instance Contraction (Vector (Complex Double)) (Vector (Complex Double)) (Complex Double) where | 146 | instance Product t => Contraction (Matrix t) (Vector t) (Vector t) where |
158 | (×) = udot | 147 | (<.>) = mXv |
159 | 148 | ||
160 | instance Contraction (Vector (Complex Float)) (Vector (Complex Float)) (Complex Float) where | 149 | instance (Container Vector t, Product t) => Contraction (Vector t) (Matrix t) (Vector t) where |
161 | (×) = udot | 150 | (<.>) v m = (conj v) `vXm` m |
162 | 151 | ||
152 | instance Product t => Contraction (Matrix t) (Matrix t) (Matrix t) where | ||
153 | (<.>) = mXm | ||
163 | 154 | ||
164 | -- | alternative function for the matrix product (×) | ||
165 | mmul :: Contraction a b c => a -> b -> c | ||
166 | mmul = (×) | ||
167 | 155 | ||
168 | -------------------------------------------------------------------------------- | 156 | -------------------------------------------------------------------------------- |
169 | 157 | ||
@@ -194,23 +182,8 @@ instance LSDiv Vector where | |||
194 | instance LSDiv Matrix where | 182 | instance LSDiv Matrix where |
195 | (<\>) = linearSolveSVD | 183 | (<\>) = linearSolveSVD |
196 | 184 | ||
197 | -------------------------------------------------------- | ||
198 | |||
199 | {- | Dot product : @u · v = 'cdot' u v@ | ||
200 | |||
201 | (unicode 0x00b7, middle dot, Alt-Gr .) | ||
202 | |||
203 | >>> fromList [1,i] · fromList[2*i+1,3] | ||
204 | 1.0 :+ (-1.0) | ||
205 | |||
206 | -} | ||
207 | (·) :: (Container Vector t, Product t) => Vector t -> Vector t -> t | ||
208 | infixl 7 · | ||
209 | u · v = cdot u v | ||
210 | |||
211 | -------------------------------------------------------------------------------- | 185 | -------------------------------------------------------------------------------- |
212 | 186 | ||
213 | -- bidirectional type inference | ||
214 | class Konst e d c | d -> c, c -> d | 187 | class Konst e d c | d -> c, c -> d |
215 | where | 188 | where |
216 | -- | | 189 | -- | |
@@ -282,12 +255,17 @@ meanCov x = (med,cov) where | |||
282 | 255 | ||
283 | -------------------------------------------------------------------------------- | 256 | -------------------------------------------------------------------------------- |
284 | 257 | ||
285 | {-# DEPRECATED dot "use udot" #-} | 258 | {- | alternative operator for '(\<.\>)' |
286 | dot :: Product e => Vector e -> Vector e -> e | 259 | |
287 | dot = udot | 260 | x25c7, white diamond |
261 | |||
262 | -} | ||
263 | (◇) :: Contraction a b c => a -> b -> c | ||
264 | infixl 7 ◇ | ||
265 | (◇) = (<.>) | ||
266 | |||
267 | -- | dot product: @cdot u v = 'udot' ('conj' u) v@ | ||
268 | dot :: (Container Vector t, Product t) => Vector t -> Vector t -> t | ||
269 | dot u v = udot (conj u) v | ||
288 | 270 | ||
289 | -- | contraction operator, equivalent to (x) | ||
290 | infixr 7 <.> | ||
291 | (<.>) :: Contraction a b c => a -> b -> c | ||
292 | (<.>) = (×) | ||
293 | 271 | ||
diff --git a/packages/hmatrix/src/Numeric/ContainerBoot.hs b/packages/hmatrix/src/Numeric/ContainerBoot.hs index ef21763..bb65166 100644 --- a/packages/hmatrix/src/Numeric/ContainerBoot.hs +++ b/packages/hmatrix/src/Numeric/ContainerBoot.hs | |||
@@ -403,7 +403,7 @@ emptyVal f v = | |||
403 | else 0 | 403 | else 0 |
404 | 404 | ||
405 | -- FIXME remove unused C wrappers | 405 | -- FIXME remove unused C wrappers |
406 | -- | (unconjugated) dot product | 406 | -- | unconjugated dot product |
407 | udot :: Product e => Vector e -> Vector e -> e | 407 | udot :: Product e => Vector e -> Vector e -> e |
408 | udot u v | 408 | udot u v |
409 | | dim u == dim v = val (asRow u `multiply` asColumn v) | 409 | | dim u == dim v = val (asRow u `multiply` asColumn v) |
diff --git a/packages/hmatrix/src/Numeric/HMatrix.hs b/packages/hmatrix/src/Numeric/HMatrix.hs index 2e01454..c8742c4 100644 --- a/packages/hmatrix/src/Numeric/HMatrix.hs +++ b/packages/hmatrix/src/Numeric/HMatrix.hs | |||
@@ -39,7 +39,7 @@ module Numeric.HMatrix ( | |||
39 | -- | 39 | -- |
40 | 40 | ||
41 | -- * Products | 41 | -- * Products |
42 | (×), | 42 | (<.>), |
43 | 43 | ||
44 | -- | The matrix product is also implemented in the "Data.Monoid" instance for Matrix, where | 44 | -- | The matrix product is also implemented in the "Data.Monoid" instance for Matrix, where |
45 | -- single-element matrices (created from numeric literals or using 'scalar') | 45 | -- single-element matrices (created from numeric literals or using 'scalar') |
@@ -53,7 +53,8 @@ module Numeric.HMatrix ( | |||
53 | -- | 53 | -- |
54 | -- mconcat uses 'optimiseMult' to get the optimal association order. | 54 | -- mconcat uses 'optimiseMult' to get the optimal association order. |
55 | 55 | ||
56 | (·), outer, kronecker, cross, | 56 | (◇), |
57 | outer, kronecker, cross, | ||
57 | scale, | 58 | scale, |
58 | sumElements, prodElements, absSum, | 59 | sumElements, prodElements, absSum, |
59 | 60 | ||
@@ -123,7 +124,7 @@ module Numeric.HMatrix ( | |||
123 | rand, randn, RandDist(..), randomVector, gaussianSample, uniformSample, | 124 | rand, randn, RandDist(..), randomVector, gaussianSample, uniformSample, |
124 | 125 | ||
125 | -- * Misc | 126 | -- * Misc |
126 | meanCov, peps, relativeError, haussholder, optimiseMult, udot, cdot, (<.>) | 127 | meanCov, peps, relativeError, haussholder, optimiseMult, udot |
127 | ) where | 128 | ) where |
128 | 129 | ||
129 | import Numeric.HMatrix.Data | 130 | import Numeric.HMatrix.Data |