-
Notifications
You must be signed in to change notification settings - Fork 6
/
toddcoxeter.js
603 lines (483 loc) · 20.1 KB
/
toddcoxeter.js
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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
function Table(name, genList) {
this.name = name;
this.genList = genList;
this.rows = [];
this.rowCosets = [];
this.firstEmpty = [];
this.lastEmpty = [];
this.rowForCoset = new Map();
this.firstNonClosed = 0;
}
Table.prototype = {
constructor: Relations,
toHTML: function (symbolMapper) {
var s = "<div id='table-scroll'><table class='blueTable'><thead><tr><th>" + this.name + "</th>";
for (var i = 0; i < this.genList.length; i++) {
s += "<th>" + symbolMapper(this.genList[i]) + "</th>";
}
s += "</tr></thead>";
for (var i = 0; i < this.rows.length; i++) {
s += "<tr><td>" + this.rowCosets[i] + "</td>";
for (var j = 0; j < this.genList.length; j++) {
var o = this.rows[i][j];
s += "<td>" + (o == undefined ? "" : o) + "</td>";
}
s += "</tr>";
}
s += "</table></div>";
return s;
},
getFirstNonClosed: function () {
while (this.firstEmpty[this.firstNonClosed] == -1) {
this.firstNonClosed++;
}
return this.firstNonClosed;
},
setAsCosetTable: function () {
this.generatorCosetLookup = new Map();
},
setCosetEntry: function (cosetRow, g, newCoset) {
this.rows[cosetRow][g] = newCoset;
this.generatorCosetLookup.set(g * 100000 + newCoset, cosetRow);
},
// Check whether the coset table has an entry for generator 'g', which points to 'coset'.
findRowPointingToCoset: function (g, coset) {
return this.generatorCosetLookup.get(g * 100000 + coset);
},
addRow: function (coset) {
this.rowCosets.push(coset);
this.rowForCoset.set(coset, this.rowCosets.length - 1);
this.rows.push(new Array(this.genList.length));
},
addRelationRow: function (coset) {
if (this.isSubgroup) return;
this.addRow(coset);
var r = this.rows[this.rows.length - 1];
r[r.length - 1] = coset;
this.firstEmpty.push(0);
this.lastEmpty.push(r.length - 2);
},
getCosetRow: function (coset) {
return this.rowForCoset.get(coset);
},
set: function (row, col, value) {
this.rows[row][col] = value;
var closed = false;
if (this.firstEmpty[row] == col) {
if (this.rows[row][col + 1] == undefined) {
this.firstEmpty[row] = col + 1;
} else {
this.firstEmpty[row] = -1;
this.lastEmpty[row] = -1;
closed = true;
}
}
if (this.lastEmpty[row] == col) {
if (col == 0) {
throw "Unexpected";
}
if (this.rows[row][col - 1] == undefined) {
this.lastEmpty[row] = col - 1;
} else {
this.firstEmpty[row] = -1;
this.lastEmpty[row] = -1;
closed = true;
}
}
return closed;
}
}
function parseString(generatorMap, string) {
var rules = string.replace(/\s/g, ''); // strip whitespace
rules = rules.split(",");
var list = [];
rules.forEach(function (r) {
var rel = [];
for (var i = 0; i < r.length; i++) {
var genIndex = generatorMap.get(r[i]);
rel.push(genIndex);
}
list.push(rel);
});
return list;
}
function Relations(relationString) {
relationString = relationString.replace(/\s/g, ''); // strip whitespace
// console.log("Relations:", relationString);
this.generators = [];
var self = this;
var set = new Set();
var stripped = relationString.replace(/,/g, '');
for (var i = 0; i < stripped.length; i++) set.add(stripped[i]);
set.forEach(function (v) { self.generators.push(v) });
this.genMap = new Map();
for (var i = 0; i < this.generators.length; i++) {
this.genMap.set(this.generators[i], i);
}
// console.log("Generators:", this.generators);
this.list = parseString(this.genMap, relationString);
//this.printRelations();
}
Relations.prototype = {
constructor: Relations,
build: function () { },
printRelations: function () {
var self = this;
this.list.forEach(function (r) {
console.log(self.toSymbols(r));
});
},
toSymbols: function (list) {
var self = this;
return list.reduce(function (s, e) { return s + self.generators[e] }, "");
}
}
function ToddCoxeter(relationString, subgroupString) {
relationString = relationString.replace(/;/g, ',');
this.rels = new Relations(relationString);
var gs = [];
for (var i = 0; i < this.rels.generators.length; i++) gs.push(i);
this.cosetTable = new Table("Coset", gs);
this.cosetTable.setAsCosetTable();
this.cosetTable.addRow(0);
this.newInformation = [];
this.relationTables = [];
for (var i = 0; i < this.rels.list.length; i++) {
var t = new Table("R" + i, this.rels.list[i]);
t.addRelationRow(0);
this.relationTables.push(t);
}
if (subgroupString != undefined) {
subgroupString = subgroupString.replace(/;/g, ',');
// console.log("subgroupString:", subgroupString);
var subgroupList = parseString(this.rels.genMap, subgroupString);
for (var i = 0; i < subgroupList.length; i++) {
var subgroup = subgroupList[i];
if (subgroup.length == 1) {
// No need to create a subgroup table - we will just fill in the coset table to begin with
this.cosetTable.rows[0][subgroup[0]] = 0;
} else {
var t = new Table("Subgroup " + i, subgroup);
t.addRelationRow(0);
t.isSubgroup = true;
this.relationTables.push(t);
}
}
}
//this.debugState();
}
ToddCoxeter.prototype = {
constructor: ToddCoxeter,
debugState: function () {
var self = this;
var mapper = function (e) { return self.rels.generators[e]; };
var out = this.cosetTable.toHTML(mapper);
for (var i = 0; i < this.relationTables.length; i++) {
out += this.relationTables[i].toHTML(mapper);
}
document.getElementById('tables').innerHTML = out;
},
getTables: function () {
var self = this;
var mapper = function (e) { return self.rels.generators[e]; };
var out = this.cosetTable.toHTML(mapper);
for (var i = 0; i < self.relationTables.length; i++) {
out += self.relationTables[i].toHTML(mapper);
}
return out;
},
addRow: function (coset) {
this.rowCosets.push(coset);
this.rows.push(new Array(this.genList.length));
},
solve: function () {
var startTime = Date.now();
var doIteration = this.initSolver();
while (doIteration()) { };
console.log("Elapsed: " + (Date.now() - startTime) + " ms. Cosets: " + this.cosetTable.rows.length);
this.cosetCounts = this.cosetTable.rows.length;
return this.cosetCounts;
},
initSolver: function () {
var cosetCounter = 1; // Initially we have one coset.
var self = this;
var firstEmptyCosetEntryX = 0;
// Returns (row, coset, generatorIndex)
function firstEmptyCosetEntry() {
for (var row = firstEmptyCosetEntryX; row < self.cosetTable.rows.length; row++) {
var coset = self.cosetTable.rowCosets[row];
for (var col = 0; col < self.cosetTable.rows[row].length; col++) {
if (self.cosetTable.rows[row][col] == undefined) {
firstEmptyCosetEntryX = row;
return [row, coset, col];
}
}
}
return undefined;
}
function updateRelationTable2() {
for (var i = 0; i < self.relationTables.length; i++) {
var rt = self.relationTables[i];
for (var row = rt.getFirstNonClosed(); row < rt.rows.length; row++) {
var changes = true;
while (changes) {
changes = false;
// Check from left
var firstEmpty = rt.firstEmpty[row];
if (firstEmpty == -1) continue;
if (rt.rows[row][firstEmpty] !== undefined) throw "Inconsistent state";
var coset = (firstEmpty == 0 ? rt.rowCosets[row] : rt.rows[row][firstEmpty - 1]);
var gen = rt.genList[firstEmpty];
var cTableRow = self.cosetTable.getCosetRow(coset);
var newCoset = self.cosetTable.rows[cTableRow][gen];
if (newCoset != undefined) {
var closed = rt.set(row, firstEmpty, newCoset);
if (closed) {
// New information: newCoset * rt.genList[firstEmpty+1] = rt.rows[row][firstEmpty+1]
self.newInformation.push([newCoset, rt.genList[firstEmpty + 1], rt.rows[row][firstEmpty + 1]]);
} else {
changes = true;
}
}
// Check from right
if (rt.lastEmpty[row] == -1) continue;
var lastEmpty = rt.lastEmpty[row];
//if (rt.rows[row][lastEmpty] !== undefined) throw "Inconsistent state";
var newCoset = rt.rows[row][lastEmpty + 1];
var gen = rt.genList[lastEmpty + 1];
var ctRow = self.cosetTable.findRowPointingToCoset(gen, newCoset);
if (ctRow != undefined) {
var closed = rt.set(row, lastEmpty, self.cosetTable.rowCosets[ctRow]);
if (closed) {
var coset = (lastEmpty > 0 ? rt.rows[row][lastEmpty - 1] : rt.rowCosets[row]);
if (coset == undefined) {
debugger;
}
self.newInformation.push(
[coset, rt.genList[lastEmpty], self.cosetTable.rowCosets[ctRow]]);
} else {
changes = true;
}
}
}
}
}
}
function addCoset(newCoset) {
for (var i = 0; i < self.relationTables.length; i++) {
var rt = self.relationTables[i];
rt.addRelationRow(newCoset);
}
self.cosetTable.addRow(newCoset);
}
var f = function doIteration() {
if (cosetCounter > 15000) {
return false;
}
if (self.newInformation.length == 0) {
// Find first empty entry in coset table.
var entry = firstEmptyCosetEntry();
if (entry == undefined) return false;
var newCoset = cosetCounter++;
var row = entry[0];
var coset = entry[1];
var g = entry[2];
self.cosetTable.setCosetEntry(row, g, newCoset);
//this.cosetTable.rows[row][g] = newCoset;
addCoset(newCoset);
// The reverse relation must be present as well.
var newCosetRow = self.cosetTable.getCosetRow(newCoset);
//this.cosetTable.rows[newCosetRow][g] = coset;
self.cosetTable.setCosetEntry(newCosetRow, g, coset);
} else {
while (self.newInformation.length > 0) {
var i = self.newInformation.pop();
// coset*g = newCoset
var coset = i[0];
var g = i[1];
var newCoset = i[2];
var cosetRow = self.cosetTable.getCosetRow(coset);
var val = self.cosetTable.rows[cosetRow][g];
if (val == undefined) {
//this.cosetTable.rows[cosetRow][g] = newCoset;
self.cosetTable.setCosetEntry(cosetRow, g, newCoset);
} else {
if (val != newCoset) {
debugger;
}
}
// per symmetry: newCoset*g = coset
var cosetRow = self.cosetTable.getCosetRow(newCoset);
var val = self.cosetTable.rows[cosetRow][g];
if (val == undefined) {
//this.cosetTable.rows[cosetRow][g] = coset;
self.cosetTable.setCosetEntry(cosetRow, g, coset);
} else {
if (val != coset) {
debugger;
}
}
}
}
updateRelationTable2();
// this.debugState();
// debugger;
return true;
}
return f;
},
// Return first element in each coset.
getRepresentiveForCoset: function (cosetIndex) {
// Note: representatives are stored in reverse order, e.g. for generators r,g,b, the list [0,1,2,2] means bbrgH
if (this.cosetTable.reps == undefined) {
this.cosetTable.reps = new Array(this.cosetTable.rows.length);
this.cosetTable.reps[0] = [];
for (var i = 0; i < this.cosetTable.rows.length; i++) {
if (this.cosetTable.reps[i] == undefined) { throw "unexpected"; }
for (var j = 0; j < this.cosetTable.rows[i].length; j++) {
var coset = this.cosetTable.rows[i][j];
if (this.cosetTable.reps[coset] == undefined) {
var newRep = this.cosetTable.reps[i].slice(0);
newRep.push(j);
this.cosetTable.reps[coset] = newRep;
// console.log("Settings coset " + coset + " to " + newRep + " = " + this.getRepresentiveString(newRep));
}
}
};
};
// debugger;
return this.cosetTable.reps[cosetIndex];
},
getRepresentivesForCosets: function () {
var l = [];
for (var i = 0; i < this.cosetTable.rows.length; i++) {
l.push(this.getRepresentiveForCoset(i));
}
return l;
},
getRepresentiveString: function (list) {
var s = "";
for (var i = list.length - 1; i >= 0; i--) {
s += this.rels.generators[list[i]];
}
return s;
},
// Note: representatives are stored in reverse order, e.g. for generators r,g,b, the list [0,1,2,2] means bbrgH
getCosetForRepresentive: function (list, initial) {
var coset = (initial == undefined ? 0 : initial);
for (var i = 0; i < list.length; i++) {
coset = this.cosetTable.rows[coset][list[i]];
}
return coset;
},
getCosetsForRepresentives: function (list) {
var cosetList = [];
for (var i = 0; i < list.length; i++) {
cosetList.push(this.getCosetForRepresentive(list[i]));
}
return cosetList;
}
,
getCosetCounts: function () {
return this.cosetCounts;
},
// If this instance use subset of symbols, get a map from subset to containing set.
translate: function (listOfWords, newToddCoxeter) {
var mapping = new Array(this.rels.generators.length);
for (var i = 0; i < this.rels.generators.length; i++) {
var newIndex = newToddCoxeter.rels.genMap.get(this.rels.generators[i]);
if (newIndex == undefined) throw "Failed to find symbol " + this.rels.generators[i] + " in larger set";
mapping[i] = newIndex;
}
var out = new Array(listOfWords.length);
for (var i = 0; i < listOfWords.length; i++) {
var newWord = new Array(listOfWords[i].length);
for (var j = 0; j < listOfWords[i].length; j++)
newWord[j] = mapping[listOfWords[i][j]];
out[i] = newWord;
}
return out;
},
apply: function (wordList, cosetList) {
var out = new Array(wordList.length);
for (var i = 0; i < wordList.length; i++) {
var transformedCosetList = new Array(cosetList.length);
for (var j = 0; j < cosetList.length; j++) {
transformedCosetList[j] = this.getCosetForRepresentive(wordList[i], cosetList[j]);
}
out[i] = transformedCosetList;
}
return out;
},
// Find vertices
// Returns { vertexOperators, edgeList, faceList };
getStructure: function () {
var vertices = 0;
var edges = 0;
var faces = 0;
for (var i = 0; i < this.cosetTable.extraColumns[0].length; i++) {
vertices = Math.max(this.cosetTable.extraColumns[0][i] + 1, vertices);
edges = Math.max(this.cosetTable.extraColumns[0][i] + 1, edges);
faces = Math.max(this.cosetTable.extraColumns[0][i] + 1, faces);
}
var vxs = new Array(vertices);
var ms = new Array(vertices);
// Vertices and reflection matrices
var current = 0;
for (var i = 0; i < this.cosetTable.extraColumns[0].length; i++) {
var vertex = this.cosetTable.extraColumns[0][i];
if (vxs[vertex] == undefined) {
vxs[vertex] = this.getRepresentiveForCoset(i);
// console.log("Setting vertex " + vertex + " to " + vxs[vertex]);
}
}
var edgeList = new Array(edges);
var faceList = new Array(faces);
// Edges and faces
for (var i = 0; i < this.cosetTable.extraColumns[1].length; i++) {
var v = this.cosetTable.extraColumns[0][i];
var e = this.cosetTable.extraColumns[1][i];
var f = this.cosetTable.extraColumns[2][i];
if (edgeList[e] == undefined) edgeList[e] = [];
if (edgeList[e].indexOf(v) == -1) edgeList[e].push(v);
if (faceList[f] == undefined) faceList[f] = [];
if (faceList[f].indexOf(e) == -1) faceList[f].push(e);
}
return { vertexOperators: vxs, edgeList: edgeList, faceList: faceList };
}
}
function getCoxeterGroup(rgPower, gbPower, rbPower) {
function copyString(string, copies) {
var s = "";
for (var i = 0; i < copies; i++) {
s += string;
}
return s;
}
function findSub(subgroupRelation, subgroupGenerators, name) {
var tc = new ToddCoxeter(subgroupRelation);
var total = tc.solve();
var subgroupMembers = freeGroup.getCosetsForRepresentives(tc.translate(tc.getRepresentivesForCosets(), freeGroup));
var tc = new ToddCoxeter(relations, subgroupGenerators);
var total = tc.solve();
var cosetActions = tc.translate(tc.getRepresentivesForCosets(), freeGroup);
var subsets = freeGroup.apply(cosetActions, subgroupMembers);
var vLabel = new Array(tc.getCosetCounts());
for (var i = 0; i < subsets.length; i++) {
for (var j = 0; j < subsets[i].length; j++) {
vLabel[subsets[i][j]] = i;
}
}
return vLabel;
}
var relations = copyString("rg", rgPower) + "," + copyString("gb", gbPower) + "," + copyString("rb", rbPower);
var freeGroup = new ToddCoxeter(relations);
var total = freeGroup.solve();
freeGroup.cosetTable.extraColumns = [];
freeGroup.cosetTable.extraColumnNames = [["Vertices", "V"], ["Edges", "E"], ["Faces", "F"], ["Cells", "C"]];
freeGroup.cosetTable.extraColumns.push(findSub(copyString("gb", gbPower), "g,b", "V"));
freeGroup.cosetTable.extraColumns.push(findSub(copyString("rb", rbPower), "r,b", "E"));
freeGroup.cosetTable.extraColumns.push(findSub(copyString("rg", rgPower), "r,g", "F"));
freeGroup.powers = [rgPower, gbPower, rbPower];
return freeGroup;
}