summaryrefslogtreecommitdiff
path: root/packages/hmatrix/src/Numeric/Container.hs
diff options
context:
space:
mode:
authorAlberto Ruiz <aruiz@um.es>2014-05-14 14:18:30 +0200
committerAlberto Ruiz <aruiz@um.es>2014-05-14 14:18:30 +0200
commitd46df392d07779fd83b01b0e9e71e5e17a36567a (patch)
tree605d0ea93101e11a72ce464825def7a499f852a6 /packages/hmatrix/src/Numeric/Container.hs
parent551cf7498c33bc0948bb4cb8444ae6f8af7278ea (diff)
undeprecate dot, fix <.>
Diffstat (limited to 'packages/hmatrix/src/Numeric/Container.hs')
-rw-r--r--packages/hmatrix/src/Numeric/Container.hs96
1 files changed, 37 insertions, 59 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