From 1b5e84ec979105d54fee51d19fccddb52026be69 Mon Sep 17 00:00:00 2001 From: Alberto Ruiz Date: Fri, 25 Apr 2014 20:45:36 +0200 Subject: (<>) and (·) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit use <> for general contraction, remove ×, use Alt Gr . for cdot --- examples/multiply.hs | 66 ++++++++++++++++++++++++++++------------------------ 1 file changed, 35 insertions(+), 31 deletions(-) (limited to 'examples') diff --git a/examples/multiply.hs b/examples/multiply.hs index fbfb9d7..572961c 100644 --- a/examples/multiply.hs +++ b/examples/multiply.hs @@ -6,26 +6,13 @@ -- , OverlappingInstances , UndecidableInstances #-} -import Numeric.LinearAlgebra hiding (Contraction(..)) +import Numeric.LinearAlgebra class Scaling a b c | a b -> c where -- ^ 0x22C5 8901 DOT OPERATOR, scaling infixl 7 ⋅ (⋅) :: a -> b -> c -class Contraction a b c | a b -> c where - -- ^ 0x00D7 215 MULTIPLICATION SIGN ×, contraction - infixl 7 × - (×) :: a -> b -> c - -class Outer a b c | a b -> c where - -- ^ 0x2297 8855 CIRCLED TIMES ⊗, outer product (not associative) - infixl 7 ⊗ - (⊗) :: a -> b -> c - - -------- - instance (Num t) => Scaling t t t where (⋅) = (*) @@ -42,37 +29,49 @@ instance Container Vector t => Scaling (Matrix t) t (Matrix t) where (⋅) = flip scale -instance Product t => Contraction (Vector t) (Vector t) t where +class Mul a b c | a b -> c, a c -> b, b c -> a where + -- ^ 0x00D7 215 MULTIPLICATION SIGN ×, contraction + infixl 7 × + (×) :: a -> b -> c + + +------- + + + +instance Product t => Mul (Vector t) (Vector t) t where (×) = udot -instance Product t => Contraction (Matrix t) (Vector t) (Vector t) where +instance Product t => Mul (Matrix t) (Vector t) (Vector t) where (×) = mXv -instance Product t => Contraction (Vector t) (Matrix t) (Vector t) where +instance Product t => Mul (Vector t) (Matrix t) (Vector t) where (×) = vXm -instance Product t => Contraction (Matrix t) (Matrix t) (Matrix t) where +instance Product t => Mul (Matrix t) (Matrix t) (Matrix t) where (×) = mXm --instance Scaling a b c => Contraction a b c where -- (×) = (⋅) ------ - -instance Product t => Outer (Vector t) (Vector t) (Matrix t) where - (⊗) = outer +-------------------------------------------------------------------------------- -instance Product t => Outer (Vector t) (Matrix t) (Matrix t) where - v ⊗ m = kronecker (asColumn v) m +class Outer a + where + infixl 7 ⊗ + -- | unicode 0x2297 8855 CIRCLED TIMES ⊗ + -- + -- vector outer product and matrix Kronecker product + (⊗) :: Product t => a t -> a t -> Matrix t -instance Product t => Outer (Matrix t) (Vector t) (Matrix t) where - m ⊗ v = kronecker m (asRow v) +instance Outer Vector where + (⊗) = outer -instance Product t => Outer (Matrix t) (Matrix t) (Matrix t) where +instance Outer Matrix where (⊗) = kronecker ------ +-------------------------------------------------------------------------------- v = 3 |> [1..] :: Vector Double @@ -83,18 +82,23 @@ s = 3 :: Double a = s ⋅ v × m × m × v ⋅ s -b = (v ⊗ m) ⊗ (v ⊗ m) +--b = (v ⊗ m) ⊗ (v ⊗ m) -c = v ⊗ m ⊗ v ⊗ m +--c = v ⊗ m ⊗ v ⊗ m d = s ⋅ (3 |> [10,20..] :: Vector Double) +u = fromList [3,0,5] +w = konst 1 (2,3) :: Matrix Double + main = do print $ (scale s v <> m) `udot` v print $ scale s v `udot` (m <> v) print $ s * ((v <> m) `udot` v) print $ s ⋅ v × m × v print a - print (b == c) +-- print (b == c) print d + print $ asColumn u ⊗ w + print $ w ⊗ asColumn u -- cgit v1.2.3