summaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
authorAlberto Ruiz <aruiz@um.es>2014-04-25 20:45:36 +0200
committerAlberto Ruiz <aruiz@um.es>2014-04-25 20:45:36 +0200
commit1b5e84ec979105d54fee51d19fccddb52026be69 (patch)
treefb3808253eb911f1b7a6882eafe6d45cbac0656d /examples
parentcd3daf7e12fa2a8c775a1d6516049bbc7e44da32 (diff)
(<>) and (·)
use <> for general contraction, remove ×, use Alt Gr . for cdot
Diffstat (limited to 'examples')
-rw-r--r--examples/multiply.hs66
1 files changed, 35 insertions, 31 deletions
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 @@
6-- , OverlappingInstances 6-- , OverlappingInstances
7 , UndecidableInstances #-} 7 , UndecidableInstances #-}
8 8
9import Numeric.LinearAlgebra hiding (Contraction(..)) 9import Numeric.LinearAlgebra
10 10
11class Scaling a b c | a b -> c where 11class Scaling a b c | a b -> c where
12 -- ^ 0x22C5 8901 DOT OPERATOR, scaling 12 -- ^ 0x22C5 8901 DOT OPERATOR, scaling
13 infixl 7 ⋅ 13 infixl 7 ⋅
14 (⋅) :: a -> b -> c 14 (⋅) :: a -> b -> c
15 15
16class Contraction a b c | a b -> c where
17 -- ^ 0x00D7 215 MULTIPLICATION SIGN ×, contraction
18 infixl 7 ×
19 (×) :: a -> b -> c
20
21class Outer a b c | a b -> c where
22 -- ^ 0x2297 8855 CIRCLED TIMES ⊗, outer product (not associative)
23 infixl 7 ⊗
24 (⊗) :: a -> b -> c
25
26
27-------
28
29instance (Num t) => Scaling t t t where 16instance (Num t) => Scaling t t t where
30 (⋅) = (*) 17 (⋅) = (*)
31 18
@@ -42,37 +29,49 @@ instance Container Vector t => Scaling (Matrix t) t (Matrix t) where
42 (⋅) = flip scale 29 (⋅) = flip scale
43 30
44 31
45instance Product t => Contraction (Vector t) (Vector t) t where 32class Mul a b c | a b -> c, a c -> b, b c -> a where
33 -- ^ 0x00D7 215 MULTIPLICATION SIGN ×, contraction
34 infixl 7 ×
35 (×) :: a -> b -> c
36
37
38-------
39
40
41
42instance Product t => Mul (Vector t) (Vector t) t where
46 (×) = udot 43 (×) = udot
47 44
48instance Product t => Contraction (Matrix t) (Vector t) (Vector t) where 45instance Product t => Mul (Matrix t) (Vector t) (Vector t) where
49 (×) = mXv 46 (×) = mXv
50 47
51instance Product t => Contraction (Vector t) (Matrix t) (Vector t) where 48instance Product t => Mul (Vector t) (Matrix t) (Vector t) where
52 (×) = vXm 49 (×) = vXm
53 50
54instance Product t => Contraction (Matrix t) (Matrix t) (Matrix t) where 51instance Product t => Mul (Matrix t) (Matrix t) (Matrix t) where
55 (×) = mXm 52 (×) = mXm
56 53
57 54
58--instance Scaling a b c => Contraction a b c where 55--instance Scaling a b c => Contraction a b c where
59-- (×) = (⋅) 56-- (×) = (⋅)
60 57
61----- 58--------------------------------------------------------------------------------
62
63instance Product t => Outer (Vector t) (Vector t) (Matrix t) where
64 (⊗) = outer
65 59
66instance Product t => Outer (Vector t) (Matrix t) (Matrix t) where 60class Outer a
67 v ⊗ m = kronecker (asColumn v) m 61 where
62 infixl 7 ⊗
63 -- | unicode 0x2297 8855 CIRCLED TIMES ⊗
64 --
65 -- vector outer product and matrix Kronecker product
66 (⊗) :: Product t => a t -> a t -> Matrix t
68 67
69instance Product t => Outer (Matrix t) (Vector t) (Matrix t) where 68instance Outer Vector where
70 m v = kronecker m (asRow v) 69 () = outer
71 70
72instance Product t => Outer (Matrix t) (Matrix t) (Matrix t) where 71instance Outer Matrix where
73 (⊗) = kronecker 72 (⊗) = kronecker
74 73
75----- 74--------------------------------------------------------------------------------
76 75
77 76
78v = 3 |> [1..] :: Vector Double 77v = 3 |> [1..] :: Vector Double
@@ -83,18 +82,23 @@ s = 3 :: Double
83 82
84a = s ⋅ v × m × m × v ⋅ s 83a = s ⋅ v × m × m × v ⋅ s
85 84
86b = (v ⊗ m) ⊗ (v ⊗ m) 85--b = (v ⊗ m) ⊗ (v ⊗ m)
87 86
88c = v ⊗ m ⊗ v ⊗ m 87--c = v ⊗ m ⊗ v ⊗ m
89 88
90d = s ⋅ (3 |> [10,20..] :: Vector Double) 89d = s ⋅ (3 |> [10,20..] :: Vector Double)
91 90
91u = fromList [3,0,5]
92w = konst 1 (2,3) :: Matrix Double
93
92main = do 94main = do
93 print $ (scale s v <> m) `udot` v 95 print $ (scale s v <> m) `udot` v
94 print $ scale s v `udot` (m <> v) 96 print $ scale s v `udot` (m <> v)
95 print $ s * ((v <> m) `udot` v) 97 print $ s * ((v <> m) `udot` v)
96 print $ s ⋅ v × m × v 98 print $ s ⋅ v × m × v
97 print a 99 print a
98 print (b == c) 100-- print (b == c)
99 print d 101 print d
102 print $ asColumn u ⊗ w
103 print $ w ⊗ asColumn u
100 104