-
Notifications
You must be signed in to change notification settings - Fork 0
/
quasilattice.js
164 lines (156 loc) · 4.6 KB
/
quasilattice.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
var BaseVertex = function (indices, representation) {
this.indices = indices.slice();
this.coords = M.mul(representation.BASIS, indices);
this.is_fundamental = representation.isFundamental(this.coords);
this.v_shrunk = null;
this.v_grown = null;
/* At minimum, compute this.weight */
};
BaseVertex.prototype = {
isAcceptableDirection: function () { return true; },
};
// The quasilattice
var QuasiLattice = function (representation, VertexType) {
this.rep = representation;
this.Vertex = VertexType;
this.verts = [];
this.border_verts = new Heap([], this.vertCmp);
this.vert_names = {};
this.directions = [];
this.start_index_shrink = 0;
this.start_index_grow = 0;
this.addVertSymmetric(V.zero(this.rep.DIMENSION));
this.addVertSymmetric(this.rep.CYCLIC_ELEMENT);
};
QuasiLattice.prototype = {
vertCmp: function (a, b) { return a.weight < b.weight; },
createVert: function (indices) {
nv = new this.Vertex(indices, this.rep);
this.verts.push(nv);
if (nv.is_fundamental) {
this.border_verts.push(nv);
}
return nv;
},
addVert: function (indices) {
var name = indices.join(' ');
var nv = this.vert_names[name];
if (nv) { return nv; }
nv = this.createVert(indices);
this.vert_names[name] = nv;
return nv;
},
addVertSymmetric: function (indices) {
if (this.vert_names[indices.join(' ')]) { return false; }
for (var i = 0; i < this.rep.GROUP.length; i++) {
this.addVert(this.rep.act(i, indices));
}
},
addVerts: function () {
var v = this.border_verts.pop();
if (!v) { return false; }
if (v.isAcceptableDirection()) {
for (var i = 0; i < this.rep.GROUP.length; i++) {
var nd = this.vert_names[this.rep.act(i, v.indices).join(' ')];
if (this.directions.indexOf(nd) < 0) {
this.directions.push(nd);
}
}
}
var c = this.verts.length;
for (var i = 1; i < this.directions.length; i++) {
this.addVertSymmetric(V.add(v.indices, this.directions[i].indices));
}
return this.verts.length - c;
},
addVertsShrink: function () {
if (!this.rep.actShrink) { return; }
var limit = this.verts.length;
for (var i = this.start_index_shrink; i < limit; i++) {
if (!this.verts[i].v_shrunk) {
this.verts[i].v_shrunk = this.addVert(this.rep.actShrink(this.verts[i].indices));
this.verts[i].v_shrunk.v_grown = this.verts[i];
}
}
this.start_index_shrink = limit;
},
addVertsGrow: function () {
var limit = this.verts.length;
if (!this.rep.actExpand) { return; }
for (var i = this.start_index_grow; i < limit; i++) {
if (!this.verts[i].v_grown) {
this.verts[i].v_grown = this.addVert(this.rep.actExpand(this.verts[i].indices));
this.verts[i].v_grown.v_shrunk = this.verts[i];
}
}
this.start_index_grow = limit;
},
};
var Representation = function (data) {
for (var key in data) { this[key] = data[key]; }
this.test();
return this;
};
Representation.prototype = {
EPSILON: 1e-9,
GROUP_IS_MATRIX_LIST: false,
SCALE_FACTORS: null,
test: function () {
var ortho_error = this.testBasis();
if (ortho_error) {
// If you want your representation to allow a non-orthogonal
// basis, set its testBasis property to a function that returns
// falsy values when everything is okay and an error message
// otherwise.
throw ortho_error;
}
if (this.GROUP_IS_MATRIX_LIST) {
var group_error = this.testGroupMatrices();
if (group_error) {
throw 'Symmetry check failed at ' + group_error.join(', ') + '.';
}
}
},
testBasis: function () {
// Test basis for orthogonality (but not length!)
var b = this.BASIS;
var EPSILON = this.EPSILON || 1e-9;
var NORM = this.BASIS_NORM_SQUARED || 1;
var id = function (i, j) {
if (i == j) { return NORM; } else { return 0; }
};
for (var i = 0; i < b.length; i++) {
for (var j = 0; j < b.length; j++) {
if (!(Math.abs(V.dot(b[i], b[j]) - id(i,j)) < EPSILON)) {
return 'Angle check failed at ' + i + ', ' + j + '.';
}
}
}
return false;
},
testGroupMatrices: function () {
// Test group for no duplicates and correct matrix sizes.
// To enable this test, set data.GROUP_IS_MATRIX_LIST = true.
var d = this.DIMENSION;
for (var i = 0; i < this.GROUP.length; i++) {
var a = this.GROUP[i];
// Test for matrix sizes
if (!a || (a.length != d)) { return i; }
for (var y = 0; y < d; y++) {
if (!a[y] || (a[y].length != d)) { return [i]; }
}
// Test for duplicates
for (var j = 0; j < i; j++) {
var b = this.GROUP[j];
var different = false;
for (var y = 0; y < d; y++) {
for (var x = 0; x < d; x++) {
if (a[y][x] != b[y][x]) { different = true; }
}
}
if (!different) { return [i, j]; }
}
}
return false;
},
};