-
Notifications
You must be signed in to change notification settings - Fork 4
/
permutation-symbol.go
158 lines (128 loc) · 2.79 KB
/
permutation-symbol.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
/*
https://en.wikipedia.org/wiki/Levi-Civita_symbol
https://www.tau.ac.il/~tsirel/dump/Static/knowino.org/wiki/Levi-Civita_symbol.html
http://homepages.engineering.auckland.ac.nz/~pkel015/SolidMechanicsBooks/Part_III/Chapter_1_Vectors_Tensors/Vectors_Tensors_Complete.pdf
https://people.maths.bris.ac.uk/~maajh/afluids/VectorCalculus.pdf
https://en.wikipedia.org/wiki/Kronecker_delta
*/
package main
import "fmt"
func main() {
t_parity()
t_kronecker()
t_identities()
}
func assert(x bool) {
if !x {
panic("assertion failed")
}
}
/*
e_ijk*e_ipq = k_jp*k_kq - k_kp*k_jq
e_ijk*e_ijp = k_pk
ijkpq are any combination of indices with value [1, 2, 3]
ijk have to be independent of each other
*/
func t_identities() {
fmt.Println("Testing identities")
for i := 1; i <= 3; i++ {
for j := 1; j <= 3; j++ {
for k := 1; k <= 3; k++ {
if i == j || i == k || j == k {
continue
}
a := []int{i, j, k}
for p := 1; p <= 3; p++ {
for q := 1; q <= 3; q++ {
b := []int{i, p, q}
x := parity(a) * parity(b)
y := kronecker(j, p)*kronecker(k, q) - kronecker(k, p)*kronecker(j, q)
fmt.Println(i, j, k, k, q, "|", x, y)
assert(x == y)
b = []int{i, j, p}
x = parity(a) * parity(b)
y = kronecker(p, k)
fmt.Println(i, j, k, p, "|", x, y)
assert(x == y)
}
}
}
}
}
}
func t_parity() {
tab := [][]int{
{2, 3, 3},
{1, 1, 2},
{3, 2, 3},
{1, 1, 3},
{1, 2, 2},
{1, 3, 3},
{1, 2, 3},
{2, 3, 1},
{3, 1, 2},
{2, 1, 3},
{3, 2, 1},
{1, 3, 2},
}
fmt.Println("Testing Parity")
for i := range tab {
fmt.Println(tab[i], parity(tab[i]))
}
fmt.Println()
}
func t_kronecker() {
fmt.Println("Testing Kronecker")
for i := 0; i < 3; i++ {
for j := 0; j < 3; j++ {
fmt.Println(i, j, "|", kronecker(i, j))
}
}
fmt.Println()
}
/*
Also known as the permutation, alternating, or Levi-Civita symbol.
We can calculate the parity by seeing how many swaps we need to get to the standard permutation
Example:
(3 2 1) -> (1 2 3) would take 1 swap (odd number so parity is -1)
(2 3 1) -> (1 2 3) would take 2 swap (even number so parity is +1)
(2 2 1) -> (1 2 3) is impossible (parity is 0)
Common variable notion to permutation index:
i = x = 1
j = y = 2
k = z = 3
Example:
ijk = xyz = (1 2 3)
*/
func parity(a []int) int {
b := append([]int{}, a...)
p := 1
loop:
for i := 0; i < len(b); i++ {
for j := i; j < len(b); j++ {
if b[j]-1 == i && i != j {
b[i], b[j], p = b[j], b[i], -p
if b[i] == b[j] {
return 0
}
continue loop
}
}
if b[i]-1 != i {
return 0
}
}
return p
}
/*
A kronecker symbol is also commonly used as a index symbol
o_ij = 1 if i == j
o_ij = 0 if i != j
Represents the diagonal indices in the matrix
*/
func kronecker(i, j int) int {
if i == j {
return 1
}
return 0
}