-
Notifications
You must be signed in to change notification settings - Fork 23
/
cayley.jl
287 lines (261 loc) · 7.62 KB
/
cayley.jl
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
module cayley
using Grassmann
using Formatting
# this file is about euclidean 3-space
@basis "+++"
# NB using the Grassmann algebra we easily get the binary groups too
# (but how do we visualize these?)
# using AbstractAlgebra
# using DataStructures
# A group element (relative to a generating set and representation)
# is a word in generators and their inverses and the representing clifford elt
CliffordElt = MultiVector{V,Float64,8}
ThreeVector = Chain{V,1,Float64,3}
GroupElt = Tuple{String, CliffordElt}
Edge = Tuple{ThreeVector, ThreeVector}
Face = Vector{ThreeVector}
# a group is a vector of group elements
Group = Vector{GroupElt}
function printgroup(G::Group)
for (w, g) in G
println("word $w represents $g")
end
end
function getelt(default::Function, G::Group, g::CliffordElt)
for (w, h) in G
if h ≈ g || h ≈ -g
return w::String
end
end
return default()
end
function getvertex(default::Function, L::Vector{ThreeVector}, P::ThreeVector)
for Q in L
if (0.0v + P) ≈ (0.0 + Q)
return Q::ThreeVector
end
end
return default()
end
Base.isapprox(e::Edge, f::Edge) = (
((0.0v + e[1]) ≈ (0.0v + f[1]) && (0.0v + e[2] ≈ 0.0v + f[2]))
|| ((0.0v + e[1]) ≈ (0.0v + f[2]) && (0.0v + e[2] ≈ 0.0v + f[1])))
function getedge(default::Function, L::Vector{Edge}, e::Edge)
for f in L
if e ≈ f
return f::Edge
end
end
return default()
end
Base.isapprox(X::Face, Y::Face) =
let N=length(X)
((N == length(Y))
&& (any(i ->
all(j -> (0.0v + X[j]) ≈ (0.0v + Y[1+(i+j)%N]), 1:N), 1:N)))
end
function getface(default::Function, L::Vector{Face}, X::Face)
for Y in L
if X ≈ Y
return Y::Face
end
end
return default()
end
function mkeuclgp(a::CliffordElt, b::CliffordElt;
bound::Int=10) :: Group
worklist::Group = [("", 1.0v+0.0v₁)]
G::Group = []
A = ~a # we assume clifford elements are normal
B = ~b
while ! isempty(worklist)
@inbounds (w, g) = popfirst!(worklist)
if length(w) > bound
continue
end
getelt(G, g) do
push!(G, (w, g))
push!(worklist, ('a'*w, a*g), ('b'*w, b*g), ('A'*w, A*g), ('B'*w, B*g))
end
end
return G
end
function rotor(degree::Float64, vector::ThreeVector)::CliffordElt
return exp(-π/degree*(⋆(vector/norm(vector))))
end
function apply(a::CliffordElt, v::ThreeVector)::ThreeVector
return (a*v*(~a))(1)
end
function showvertex(v::ThreeVector; precision::Int=5)::String
fs = FormatSpec(".$precision"*'f')
return ("(" * fmt(fs, getindex(v,1)) * ", "
* fmt(fs, getindex(v,2)) * ", "
* fmt(fs, getindex(v,3)) * ")")
end
## Some example: a cyclic and a dihedral group
turn = rotor(5.0, 0.0v₁+1.0v₃)
flip = rotor(2.0, 1.0v₁+0.0v₃)
C5 = mkeuclgp(turn, turn)
D5 = mkeuclgp(turn, flip)
# generators of A5
# 1/5 turn around P
# 1/2 turn around edge PQ
ϕ = .5*(sqrt(5)+1.0) # golden ratio
P = +1.0v₂ + ϕ*v₃
Q = -1.0v₂ + ϕ*v₃
a = rotor(5.0, P)
b = rotor(2.0, P+Q)
A5 = mkeuclgp(a,b)
# cube and tetrahedron
N = 1.0v₁ + 1.0v₂ + 1.0v₃
M = -1.0v₁ - 1.0v₂ + 1.0v₃
K = -1.0v₁ + 1.0v₂ + 1.0v₃
d = rotor(3.0,N)
c = rotor(2.0,N+K)
A4 = mkeuclgp(d,b)
S4 = mkeuclgp(d,c)
"""
Make a TikzPicture of a platonic solid
P and Q are two adjecent vertices,
degree is the number of faces around a vertex
"""
function drawplatonic(degree::Float64, P::ThreeVector, Q::ThreeVector)
a = rotor(degree, P)
b = rotor(2.0, P+Q)
G = mkeuclgp(a,b)
vertices::Vector{ThreeVector} = [P, Q]
edges::Vector{Edge} = [(P, Q)]
face1::Face = [P, Q]
# complete the first face, iterating b*a
for i in 1:10
R = apply(b*a,last(face1))
getvertex(face1, R) do
push!(face1, R)
end
end
faces::Vector{Face} = [face1]
# find all vertices, edges and faces, iterating over G
for (w, g) in G
P₁ = apply(g,P)
Q₁ = apply(g,Q)
getvertex(vertices, P₁) do
push!(vertices, P₁)
end
getedge(edges, (P₁,Q₁)) do
push!(edges, (P₁,Q₁))
end
newface = apply.(Ref(g),face1)
getface(faces, newface) do
push!(faces, newface)
end
end
tikzstring::String = ""
for X in faces
tikzstring *= "\\fill "
for R in X
tikzstring *= showvertex(R) * " -- "
end
tikzstring *= "cycle;\n"
end
for (P₁,Q₁) in edges
tikzstring *= "\\draw " * showvertex(P₁) * " -- " * showvertex(Q₁) * ";\n"
end
return tikzstring
end
"""
Make a TikzPicture of an icocahedron with a true cross
P and Q are two adjecent vertices (also specifying the cross),
D is a viewing direction
This function emits the faces in order as seen from D
"""
function drawicocross(P::ThreeVector, Q::ThreeVector, D::ThreeVector)
a = rotor(5.0, P)
b = rotor(2.0, P+Q)
A = ~a
B = ~b
G = mkeuclgp(a,b)
edge1::Face = [P, Q]
face1::Face = [P, Q]
# complete the first face, iterating b*a
for i in 1:10
R = apply(b*a,last(face1))
getvertex(face1, R) do
push!(face1, R)
end
end
faces::Vector{Face} = [face1]
# find all edges and faces, iterating over G
# (here we conside edges as degenerate faces
for (w, g) in G
#newface = apply.(Ref(g),face1)
#getface(faces, newface) do
# push!(faces, newface)
#end
newedge = apply.(Ref(g),edge1)
getface(faces, newedge) do
push!(faces, newedge)
end
end
# add the three golden rectangles (each split in four quarters)
qrect::Face = [0.0P, 0.5(P+Q), P, 0.5(P-Q)]
# we want the orbit of qrect under the stabilizing A4
# note that a*b*A*A = rotor(3.0, v₁+v₂+v₃)
# (12 elements for 3 × 4 quarter rectangles)
A4 = mkeuclgp(a*b*A*A, b)
for (w, g) in A4
newrect = apply.(Ref(g),qrect)
getface(faces, newrect) do
push!(faces, newrect)
end
end
# sort the faces
sort!(faces; by=(X -> (Grassmann.mean(X) ⋅ D)[1]))
tikzstring::String = ""
for X in faces
if length(X) == 2 # edge
tikzstring *= ("\\draw " * showvertex(X[1])
* " -- " * showvertex(X[2]) * ";\n")
else # rectangle/face
tikzstring *= "\\fill[" * (length(X) == 3 ? "red" : "blue") * "] "
for R in X
tikzstring *= showvertex(R) * " -- "
end
tikzstring *= "cycle;\n"
end
end
return tikzstring
end
## Some examples to try
# drawplatonic(3.0, N, M) # tetrahedron
# drawplatonic(3.0, N, K) # cube
# print(drawplatonic(5.0, P, Q)) # icosahedron
print(drawicocross(P, Q, N))
"""
Make a TikZpicture of a cayley diagram of a platonic solid
P and Q are two adjecent vertices,
degree is the number of faces around a vertex
"""
function drawcayley(degree::Float64, P::ThreeVector, Q::ThreeVector)
tikzstring::String = ""
a = rotor(degree, P)
b = rotor(2.0, P+Q)
A = ~a
B = ~b
G = mkeuclgp(a,b)
for (w, g) in G
tikzstring *= ("\\node[vertex] (n" * w * ") at "
* showvertex(apply(g,3.0/4.0*P+1.0/4.0*Q)) * " {};\n")
end
for (w, g) in G
aw = getelt(G, g*A) do
"error"
end
bw = getelt(G, g*B) do
"error"
end
tikzstring *= "\\draw[gena] (n" * w * ") -- (n" * aw * ");\n"
tikzstring *= "\\draw[genb] (n" * w * ") -- (n" * bw * ");\n"
end
return tikzstring
end
end