diff options
author | Alberto Ruiz <aruiz@um.es> | 2014-04-25 20:45:36 +0200 |
---|---|---|
committer | Alberto Ruiz <aruiz@um.es> | 2014-04-25 20:45:36 +0200 |
commit | 1b5e84ec979105d54fee51d19fccddb52026be69 (patch) | |
tree | fb3808253eb911f1b7a6882eafe6d45cbac0656d /lib/Numeric | |
parent | cd3daf7e12fa2a8c775a1d6516049bbc7e44da32 (diff) |
(<>) and (·)
use <> for general contraction, remove ×, use Alt Gr . for cdot
Diffstat (limited to 'lib/Numeric')
-rw-r--r-- | lib/Numeric/Container.hs | 99 |
1 files changed, 37 insertions, 62 deletions
diff --git a/lib/Numeric/Container.hs b/lib/Numeric/Container.hs index aac8c10..a612aa1 100644 --- a/lib/Numeric/Container.hs +++ b/lib/Numeric/Container.hs | |||
@@ -38,7 +38,7 @@ module Numeric.Container ( | |||
38 | Product(..), | 38 | Product(..), |
39 | Contraction(..), | 39 | Contraction(..), |
40 | optimiseMult, | 40 | optimiseMult, |
41 | mXm,mXv,vXm,Mul(..),LSDiv(..), cdot, (⋅), dot, (<.>), | 41 | mXm,mXv,vXm,LSDiv(..), cdot, (·), dot, (<.>), |
42 | outer, kronecker, | 42 | outer, kronecker, |
43 | -- * Random numbers | 43 | -- * Random numbers |
44 | RandDist(..), | 44 | RandDist(..), |
@@ -55,7 +55,7 @@ module Numeric.Container ( | |||
55 | 55 | ||
56 | IndexOf, | 56 | IndexOf, |
57 | module Data.Complex, | 57 | module Data.Complex, |
58 | -- * Input / Output | 58 | -- * IO |
59 | dispf, disps, dispcf, vecdisp, latexFormat, format, | 59 | dispf, disps, dispcf, vecdisp, latexFormat, format, |
60 | loadMatrix, saveMatrix, fromFile, fileDimensions, | 60 | loadMatrix, saveMatrix, fromFile, fileDimensions, |
61 | readMatrix, | 61 | readMatrix, |
@@ -101,19 +101,23 @@ cdot u v = udot (conj u) v | |||
101 | 101 | ||
102 | -------------------------------------------------------- | 102 | -------------------------------------------------------- |
103 | 103 | ||
104 | class Mul a b c | a b -> c where | 104 | class Contraction a b c | a b -> c, a c -> b, b c -> a |
105 | infixl 7 <> | 105 | where |
106 | -- | Matrix-matrix, matrix-vector, and vector-matrix products. | 106 | infixl 7 <> |
107 | (<>) :: Product t => a t -> b t -> c t | 107 | -- | matrix-matrix product, matrix-vector product, unconjugated dot product |
108 | (<>) :: a -> b -> c | ||
108 | 109 | ||
109 | instance Mul Matrix Matrix Matrix where | 110 | instance Product t => Contraction (Vector t) (Vector t) t where |
110 | (<>) = mXm | 111 | (<>) = udot |
111 | 112 | ||
112 | instance Mul Matrix Vector Vector where | 113 | instance Product t => Contraction (Matrix t) (Vector t) (Vector t) where |
113 | (<>) m v = flatten $ m <> asColumn v | 114 | (<>) = mXv |
114 | 115 | ||
115 | instance Mul Vector Matrix Vector where | 116 | instance Product t => Contraction (Vector t) (Matrix t) (Vector t) where |
116 | (<>) v m = flatten $ asRow v <> m | 117 | (<>) = vXm |
118 | |||
119 | instance Product t => Contraction (Matrix t) (Matrix t) (Matrix t) where | ||
120 | (<>) = mXm | ||
117 | 121 | ||
118 | -------------------------------------------------------- | 122 | -------------------------------------------------------- |
119 | 123 | ||
@@ -130,55 +134,12 @@ instance LSDiv Matrix Matrix where | |||
130 | 134 | ||
131 | -------------------------------------------------------- | 135 | -------------------------------------------------------- |
132 | 136 | ||
133 | -- | Compute mean vector and covariance matrix of the rows of a matrix. | 137 | -- | dot product : @u · v = 'cdot' u v@ |
134 | meanCov :: Matrix Double -> (Vector Double, Matrix Double) | 138 | -- |
135 | meanCov x = (med,cov) where | 139 | -- unicode 0x00b7, Alt-Gr . |
136 | r = rows x | 140 | (·) :: (Container Vector t, Product t) => Vector t -> Vector t -> t |
137 | k = 1 / fromIntegral r | 141 | infixl 7 · |
138 | med = konst k r `vXm` x | 142 | u · v = cdot u v |
139 | meds = konst 1 r `outer` med | ||
140 | xc = x `sub` meds | ||
141 | cov = scale (recip (fromIntegral (r-1))) (trans xc `mXm` xc) | ||
142 | |||
143 | -------------------------------------------------------------------------------- | ||
144 | |||
145 | -- | matrix-matrix product, matrix-vector product, unconjugated dot product, and scaling | ||
146 | class Contraction a b c | a b -> c | ||
147 | where | ||
148 | -- ^ 0x00d7 multiplication sign | ||
149 | infixl 7 × | ||
150 | (×) :: a -> b -> c | ||
151 | |||
152 | instance Product t => Contraction (Vector t) (Vector t) t where | ||
153 | (×) = udot | ||
154 | |||
155 | instance Product t => Contraction (Matrix t) (Vector t) (Vector t) where | ||
156 | (×) = mXv | ||
157 | |||
158 | instance Product t => Contraction (Vector t) (Matrix t) (Vector t) where | ||
159 | (×) = vXm | ||
160 | |||
161 | instance Product t => Contraction (Matrix t) (Matrix t) (Matrix t) where | ||
162 | (×) = mXm | ||
163 | |||
164 | instance Container Vector t => Contraction t (Vector t) (Vector t) where | ||
165 | (×) = scale | ||
166 | |||
167 | instance Container Vector t => Contraction (Vector t) t (Vector t) where | ||
168 | (×) = flip scale | ||
169 | |||
170 | instance Container Matrix t => Contraction t (Matrix t) (Matrix t) where | ||
171 | (×) = scale | ||
172 | |||
173 | instance Container Matrix t => Contraction (Matrix t) t (Matrix t) where | ||
174 | (×) = flip scale | ||
175 | |||
176 | -------------------------------------------------------------------------------- | ||
177 | |||
178 | -- | dot product (0x22C5): @u ⋅ v = 'cdot' u v@ | ||
179 | (⋅) :: (Container Vector t, Product t) => Vector t -> Vector t -> t | ||
180 | infixl 7 ⋅ | ||
181 | u ⋅ v = cdot u v | ||
182 | 143 | ||
183 | -------------------------------------------------------------------------------- | 144 | -------------------------------------------------------------------------------- |
184 | 145 | ||
@@ -195,6 +156,8 @@ instance Container Vector e => Konst e (Int,Int) Matrix | |||
195 | where | 156 | where |
196 | konst = konst' | 157 | konst = konst' |
197 | 158 | ||
159 | -------------------------------------------------------------------------------- | ||
160 | |||
198 | class Build d f c e | d -> c, c -> d, f -> e, f -> d, f -> c, c e -> f, d e -> f | 161 | class Build d f c e | d -> c, c -> d, f -> e, f -> d, f -> c, c e -> f, d e -> f |
199 | where | 162 | where |
200 | build :: d -> f -> c e | 163 | build :: d -> f -> c e |
@@ -209,11 +172,23 @@ instance Container Matrix e => Build (Int,Int) (e -> e -> e) Matrix e | |||
209 | 172 | ||
210 | -------------------------------------------------------------------------------- | 173 | -------------------------------------------------------------------------------- |
211 | 174 | ||
175 | -- | Compute mean vector and covariance matrix of the rows of a matrix. | ||
176 | meanCov :: Matrix Double -> (Vector Double, Matrix Double) | ||
177 | meanCov x = (med,cov) where | ||
178 | r = rows x | ||
179 | k = 1 / fromIntegral r | ||
180 | med = konst k r `vXm` x | ||
181 | meds = konst 1 r `outer` med | ||
182 | xc = x `sub` meds | ||
183 | cov = scale (recip (fromIntegral (r-1))) (trans xc `mXm` xc) | ||
184 | |||
185 | -------------------------------------------------------------------------------- | ||
186 | |||
212 | {-# DEPRECATED dot "use udot" #-} | 187 | {-# DEPRECATED dot "use udot" #-} |
213 | dot :: Product e => Vector e -> Vector e -> e | 188 | dot :: Product e => Vector e -> Vector e -> e |
214 | dot = udot | 189 | dot = udot |
215 | 190 | ||
216 | {-# DEPRECATED (<.>) "use udot or (×)" #-} | 191 | {-# DEPRECATED (<.>) "use udot or (<>)" #-} |
217 | infixl 7 <.> | 192 | infixl 7 <.> |
218 | (<.>) :: Product e => Vector e -> Vector e -> e | 193 | (<.>) :: Product e => Vector e -> Vector e -> e |
219 | (<.>) = udot | 194 | (<.>) = udot |