summaryrefslogtreecommitdiff
path: root/packages/hmatrix/src
diff options
context:
space:
mode:
Diffstat (limited to 'packages/hmatrix/src')
-rw-r--r--packages/hmatrix/src/Numeric/Container.hs96
-rw-r--r--packages/hmatrix/src/Numeric/ContainerBoot.hs2
-rw-r--r--packages/hmatrix/src/Numeric/HMatrix.hs7
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]
91linspace n (a,b) = addConstant a $ scale s $ fromList $ map fromIntegral [0 .. n-1] 91linspace 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@
95cdot :: (Container Vector t, Product t) => Vector t -> Vector t -> t
96cdot u v = udot (conj u) v
97
98-------------------------------------------------------- 94--------------------------------------------------------
99 95
100class Contraction a b c | a b -> c, c -> a b 96class 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
107Examples: 101Examples:
108 102
@@ -118,7 +112,7 @@ Examples:
118 112
119matrix × matrix: 113matrix × matrix:
120 114
121>>> disp 2 (a × trans a) 115>>> disp 2 (a <.> trans a)
1223x3 1163x3
123 30 70 110 117 30 70 110
124 70 174 278 118 70 174 278
@@ -126,44 +120,38 @@ matrix × matrix:
126 120
127matrix × vector: 121matrix × vector:
128 122
129>>> a × v 123>>> a <.> v
130fromList [3.0,11.0,19.0] 124fromList [3.0,11.0,19.0]
131 125
132unconjugated dot product: 126dot product:
133 127
134>>> fromList [1,i] × fromList[2*i+1,3] 128>>> u <.> fromList[3,2,1::Double]
1351.0 :+ 5.0 12910
136 130
137(×) is right associative, so we can write: 131For complex vectors the first argument is conjugated:
138 132
139>>> u × a × v 133>>> fromList [1,i] <.> fromList[2*i+1,3]
14082.0 :: Double 1341.0 :+ (-1.0)
141
142-}
143 (×) :: a -> b -> c
144 135
145instance Product t => Contraction (Matrix t) (Vector t) (Vector t) where 136>>> fromList [1,i,1-i] <.> complex a
146 (×) = mXv 137fromList [10.0 :+ 4.0,12.0 :+ 4.0,14.0 :+ 4.0,16.0 :+ 4.0]
147 138
148instance Product t => Contraction (Matrix t) (Matrix t) (Matrix t) where 139-}
149 (×) = mXm 140 (<.>) :: a -> b -> c
150 141
151instance Contraction (Vector Double) (Vector Double) Double where
152 (×) = udot
153 142
154instance Contraction (Vector Float) (Vector Float) Float where 143instance (Product t, Container Vector t) => Contraction (Vector t) (Vector t) t where
155 (×) = udot 144 u <.> v = conj u `udot` v
156 145
157instance Contraction (Vector (Complex Double)) (Vector (Complex Double)) (Complex Double) where 146instance Product t => Contraction (Matrix t) (Vector t) (Vector t) where
158 (×) = udot 147 (<.>) = mXv
159 148
160instance Contraction (Vector (Complex Float)) (Vector (Complex Float)) (Complex Float) where 149instance (Container Vector t, Product t) => Contraction (Vector t) (Matrix t) (Vector t) where
161 (×) = udot 150 (<.>) v m = (conj v) `vXm` m
162 151
152instance Product t => Contraction (Matrix t) (Matrix t) (Matrix t) where
153 (<.>) = mXm
163 154
164-- | alternative function for the matrix product (×)
165mmul :: Contraction a b c => a -> b -> c
166mmul = (×)
167 155
168-------------------------------------------------------------------------------- 156--------------------------------------------------------------------------------
169 157
@@ -194,23 +182,8 @@ instance LSDiv Vector where
194instance LSDiv Matrix where 182instance 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]
2041.0 :+ (-1.0)
205
206-}
207(·) :: (Container Vector t, Product t) => Vector t -> Vector t -> t
208infixl 7 ·
209u · v = cdot u v
210
211-------------------------------------------------------------------------------- 185--------------------------------------------------------------------------------
212 186
213-- bidirectional type inference
214class Konst e d c | d -> c, c -> d 187class 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 '(\<.\>)'
286dot :: Product e => Vector e -> Vector e -> e 259
287dot = udot 260x25c7, white diamond
261
262-}
263(◇) :: Contraction a b c => a -> b -> c
264infixl 7 ◇
265(◇) = (<.>)
266
267-- | dot product: @cdot u v = 'udot' ('conj' u) v@
268dot :: (Container Vector t, Product t) => Vector t -> Vector t -> t
269dot u v = udot (conj u) v
288 270
289-- | contraction operator, equivalent to (x)
290infixr 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
407udot :: Product e => Vector e -> Vector e -> e 407udot :: Product e => Vector e -> Vector e -> e
408udot u v 408udot 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
129import Numeric.HMatrix.Data 130import Numeric.HMatrix.Data