summaryrefslogtreecommitdiff
path: root/packages/base/src/Numeric/Container.hs
diff options
context:
space:
mode:
Diffstat (limited to 'packages/base/src/Numeric/Container.hs')
-rw-r--r--packages/base/src/Numeric/Container.hs37
1 files changed, 22 insertions, 15 deletions
diff --git a/packages/base/src/Numeric/Container.hs b/packages/base/src/Numeric/Container.hs
index 264a619..0633640 100644
--- a/packages/base/src/Numeric/Container.hs
+++ b/packages/base/src/Numeric/Container.hs
@@ -32,11 +32,11 @@ module Numeric.Container (
32 diag, ident, 32 diag, ident,
33 ctrans, 33 ctrans,
34 -- * Generic operations 34 -- * Generic operations
35 Container(..), 35 Container(..), Transposable(..), Linear(..),
36 -- * Matrix product 36 -- * Matrix product
37 Product(..), udot, dot, (◇), 37 Product(..), udot, dot, (◇),
38 Mul(..), 38 Mul(..),
39 Contraction(..), 39 Contraction(..),(<.>),
40 optimiseMult, 40 optimiseMult,
41 mXm,mXv,vXm,LSDiv(..), 41 mXm,mXv,vXm,LSDiv(..),
42 outer, kronecker, 42 outer, kronecker,
@@ -55,7 +55,9 @@ module Numeric.Container (
55 IndexOf, 55 IndexOf,
56 module Data.Complex, 56 module Data.Complex,
57 -- * IO 57 -- * IO
58 module Data.Packed.IO 58 module Data.Packed.IO,
59 -- * Misc
60 Testable(..)
59) where 61) where
60 62
61import Data.Packed hiding (stepD, stepF, condD, condF, conjugateC, conjugateQ) 63import Data.Packed hiding (stepD, stepF, condD, condF, conjugateC, conjugateQ)
@@ -87,10 +89,9 @@ linspace n (a,b) = addConstant a $ scale s $ fromList $ map fromIntegral [0 .. n
87 89
88-------------------------------------------------------- 90--------------------------------------------------------
89 91
90class Contraction a b c | a b -> c 92{- | Matrix product, matrix - vector product, and dot product (equivalent to 'contraction')
91 where 93
92 infixl 7 <.> 94(This operator can also be written using the unicode symbol ◇ (25c7).)
93 {- | Matrix product, matrix - vector product, and dot product
94 95
95Examples: 96Examples:
96 97
@@ -129,22 +130,28 @@ For complex vectors the first argument is conjugated:
129 130
130>>> fromList [1,i,1-i] <.> complex a 131>>> fromList [1,i,1-i] <.> complex a
131fromList [10.0 :+ 4.0,12.0 :+ 4.0,14.0 :+ 4.0,16.0 :+ 4.0] 132fromList [10.0 :+ 4.0,12.0 :+ 4.0,14.0 :+ 4.0,16.0 :+ 4.0]
132
133-} 133-}
134 (<.>) :: a -> b -> c 134infixl 7 <.>
135(<.>) :: Contraction a b c => a -> b -> c
136(<.>) = contraction
135 137
136 138
139class Contraction a b c | a b -> c
140 where
141 -- | Matrix product, matrix - vector product, and dot product
142 contraction :: a -> b -> c
143
137instance (Product t, Container Vector t) => Contraction (Vector t) (Vector t) t where 144instance (Product t, Container Vector t) => Contraction (Vector t) (Vector t) t where
138 u <.> v = conj u `udot` v 145 u `contraction` v = conj u `udot` v
139 146
140instance Product t => Contraction (Matrix t) (Vector t) (Vector t) where 147instance Product t => Contraction (Matrix t) (Vector t) (Vector t) where
141 (<.>) = mXv 148 contraction = mXv
142 149
143instance (Container Vector t, Product t) => Contraction (Vector t) (Matrix t) (Vector t) where 150instance (Container Vector t, Product t) => Contraction (Vector t) (Matrix t) (Vector t) where
144 (<.>) v m = (conj v) `vXm` m 151 contraction v m = (conj v) `vXm` m
145 152
146instance Product t => Contraction (Matrix t) (Matrix t) (Matrix t) where 153instance Product t => Contraction (Matrix t) (Matrix t) (Matrix t) where
147 (<.>) = mXm 154 contraction = mXm
148 155
149 156
150-------------------------------------------------------------------------------- 157--------------------------------------------------------------------------------
@@ -229,10 +236,10 @@ instance Container Matrix e => Build (Int,Int) (e -> e -> e) Matrix e
229 236
230-------------------------------------------------------------------------------- 237--------------------------------------------------------------------------------
231 238
232-- | alternative unicode symbol (25c7) for the contraction operator '(\<.\>)' 239-- | alternative unicode symbol (25c7) for 'contraction'
233(◇) :: Contraction a b c => a -> b -> c 240(◇) :: Contraction a b c => a -> b -> c
234infixl 7 ◇ 241infixl 7 ◇
235(◇) = (<.>) 242(◇) = contraction
236 243
237-- | dot product: @cdot u v = 'udot' ('conj' u) v@ 244-- | dot product: @cdot u v = 'udot' ('conj' u) v@
238dot :: (Container Vector t, Product t) => Vector t -> Vector t -> t 245dot :: (Container Vector t, Product t) => Vector t -> Vector t -> t