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 /examples | |
parent | cd3daf7e12fa2a8c775a1d6516049bbc7e44da32 (diff) |
(<>) and (·)
use <> for general contraction, remove ×, use Alt Gr . for cdot
Diffstat (limited to 'examples')
-rw-r--r-- | examples/multiply.hs | 66 |
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 | ||
9 | import Numeric.LinearAlgebra hiding (Contraction(..)) | 9 | import Numeric.LinearAlgebra |
10 | 10 | ||
11 | class Scaling a b c | a b -> c where | 11 | class 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 | ||
16 | class Contraction a b c | a b -> c where | ||
17 | -- ^ 0x00D7 215 MULTIPLICATION SIGN ×, contraction | ||
18 | infixl 7 × | ||
19 | (×) :: a -> b -> c | ||
20 | |||
21 | class 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 | |||
29 | instance (Num t) => Scaling t t t where | 16 | instance (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 | ||
45 | instance Product t => Contraction (Vector t) (Vector t) t where | 32 | class 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 | |||
42 | instance Product t => Mul (Vector t) (Vector t) t where | ||
46 | (×) = udot | 43 | (×) = udot |
47 | 44 | ||
48 | instance Product t => Contraction (Matrix t) (Vector t) (Vector t) where | 45 | instance Product t => Mul (Matrix t) (Vector t) (Vector t) where |
49 | (×) = mXv | 46 | (×) = mXv |
50 | 47 | ||
51 | instance Product t => Contraction (Vector t) (Matrix t) (Vector t) where | 48 | instance Product t => Mul (Vector t) (Matrix t) (Vector t) where |
52 | (×) = vXm | 49 | (×) = vXm |
53 | 50 | ||
54 | instance Product t => Contraction (Matrix t) (Matrix t) (Matrix t) where | 51 | instance 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 | |||
63 | instance Product t => Outer (Vector t) (Vector t) (Matrix t) where | ||
64 | (⊗) = outer | ||
65 | 59 | ||
66 | instance Product t => Outer (Vector t) (Matrix t) (Matrix t) where | 60 | class 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 | ||
69 | instance Product t => Outer (Matrix t) (Vector t) (Matrix t) where | 68 | instance Outer Vector where |
70 | m ⊗ v = kronecker m (asRow v) | 69 | (⊗) = outer |
71 | 70 | ||
72 | instance Product t => Outer (Matrix t) (Matrix t) (Matrix t) where | 71 | instance Outer Matrix where |
73 | (⊗) = kronecker | 72 | (⊗) = kronecker |
74 | 73 | ||
75 | ----- | 74 | -------------------------------------------------------------------------------- |
76 | 75 | ||
77 | 76 | ||
78 | v = 3 |> [1..] :: Vector Double | 77 | v = 3 |> [1..] :: Vector Double |
@@ -83,18 +82,23 @@ s = 3 :: Double | |||
83 | 82 | ||
84 | a = s ⋅ v × m × m × v ⋅ s | 83 | a = s ⋅ v × m × m × v ⋅ s |
85 | 84 | ||
86 | b = (v ⊗ m) ⊗ (v ⊗ m) | 85 | --b = (v ⊗ m) ⊗ (v ⊗ m) |
87 | 86 | ||
88 | c = v ⊗ m ⊗ v ⊗ m | 87 | --c = v ⊗ m ⊗ v ⊗ m |
89 | 88 | ||
90 | d = s ⋅ (3 |> [10,20..] :: Vector Double) | 89 | d = s ⋅ (3 |> [10,20..] :: Vector Double) |
91 | 90 | ||
91 | u = fromList [3,0,5] | ||
92 | w = konst 1 (2,3) :: Matrix Double | ||
93 | |||
92 | main = do | 94 | main = 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 | ||