summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
Diffstat (limited to 'lib')
-rw-r--r--lib/Numeric/Container.hs99
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
104class Mul a b c | a b -> c where 104class 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
109instance Mul Matrix Matrix Matrix where 110instance Product t => Contraction (Vector t) (Vector t) t where
110 (<>) = mXm 111 (<>) = udot
111 112
112instance Mul Matrix Vector Vector where 113instance Product t => Contraction (Matrix t) (Vector t) (Vector t) where
113 (<>) m v = flatten $ m <> asColumn v 114 (<>) = mXv
114 115
115instance Mul Vector Matrix Vector where 116instance Product t => Contraction (Vector t) (Matrix t) (Vector t) where
116 (<>) v m = flatten $ asRow v <> m 117 (<>) = vXm
118
119instance 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@
134meanCov :: Matrix Double -> (Vector Double, Matrix Double) 138--
135meanCov 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 141infixl 7 ·
138 med = konst k r `vXm` x 142u · 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
146class Contraction a b c | a b -> c
147 where
148 -- ^ 0x00d7 multiplication sign
149 infixl 7 ×
150 (×) :: a -> b -> c
151
152instance Product t => Contraction (Vector t) (Vector t) t where
153 (×) = udot
154
155instance Product t => Contraction (Matrix t) (Vector t) (Vector t) where
156 (×) = mXv
157
158instance Product t => Contraction (Vector t) (Matrix t) (Vector t) where
159 (×) = vXm
160
161instance Product t => Contraction (Matrix t) (Matrix t) (Matrix t) where
162 (×) = mXm
163
164instance Container Vector t => Contraction t (Vector t) (Vector t) where
165 (×) = scale
166
167instance Container Vector t => Contraction (Vector t) t (Vector t) where
168 (×) = flip scale
169
170instance Container Matrix t => Contraction t (Matrix t) (Matrix t) where
171 (×) = scale
172
173instance 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
180infixl 7 ⋅
181u ⋅ 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
198class Build d f c e | d -> c, c -> d, f -> e, f -> d, f -> c, c e -> f, d e -> f 161class 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.
176meanCov :: Matrix Double -> (Vector Double, Matrix Double)
177meanCov 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" #-}
213dot :: Product e => Vector e -> Vector e -> e 188dot :: Product e => Vector e -> Vector e -> e
214dot = udot 189dot = udot
215 190
216{-# DEPRECATED (<.>) "use udot or (×)" #-} 191{-# DEPRECATED (<.>) "use udot or (<>)" #-}
217infixl 7 <.> 192infixl 7 <.>
218(<.>) :: Product e => Vector e -> Vector e -> e 193(<.>) :: Product e => Vector e -> Vector e -> e
219(<.>) = udot 194(<.>) = udot