-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
607 lines (560 loc) · 22.5 KB
/
index.html
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
602
603
604
605
606
607
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>
RegularNGons
</title>
<style>
.emscripten {
padding-right: 0;
margin-left: auto;
margin-right: auto;
display: block;
}
textarea.emscripten {
font-family: monospace;
width: 80%;
}
.outer {
display: table;
position: absolute;
height: 100%;
width: 100%;
}
.middle {
display: table-cell;
vertical-align: middle;
}
.inner {
margin-left: auto;
margin-right: auto;
width: 960px;
}
.center {
text-align: center;
}
</style>
</head>
<body onload="go()">
<div class="outer">
<div class="middle">
<div class="inner">
<div class="center">
<span id="status">Status: Initializing</span>
<button id="start_stop" disabled="true" onclick="start_stop_button()">Pause computations</button>
</div>
<textarea class="emscripten" id="output" rows="10" cols="80">Welcome to RegularNGons (https://github.com/kovzol/RegularNGons)...
</textarea><br>
<div id="applet_container" class="emscripten"></div>
</div>
</div>
</div>
<script type="text/javascript" src="https://cdn.geogebra.org/apps/deployggb.js"></script>
<script type='text/javascript'>
async function reload() {
output("Attempt to restart page from this point again");
outputElapsedTime();
var url = window.location.href;
if (cont == "")
url += '&c=' + server_id;
await sleep(5000);
window.location.href = url;
}
function compute(inputString) { // compute a Giac command and return the output
var caseval = Module.cwrap('caseval', 'string', ['string']);
try {
return caseval(inputString);
} catch (err) {
output("Error during Giac computation on s=" + s);
reload();
}
}
// https://css-tricks.com/snippets/javascript/get-url-variables/
function getQueryVariable(variable) {
var query = window.location.search.substring(1);
var vars = query.split("&");
for (var i = 0; i < vars.length; i++) {
var pair = vars[i].split("=");
if (pair[0] == variable) {
return pair[1];
}
}
return (false);
}
/* Compute the xth element among \binom{n}{2}.
* (a,b) is a pair where 0<=a<b<N.
*/
function deserialize(x, N) {
var a = Math.floor(((2 * N - 1) - Math.sqrt((2 * N - 1) * (2 * N - 1) - 8 * x)) / 2);
var b = (2 * x + a * a - (2 * N - 3) * a) / 2 + 1;
// console.log("deserialize(" + x + "," + N + ")=" + a+ "," + b);
return {
"a": a,
"b": b
}
}
function go() {
n = getQueryVariable("n");
status_div = document.getElementById('status');
if (!n) errorOutput("Please append &n=... to the URL");
if (n < 3) errorOutput("Invalid n: " + n + " (please use a number >= 3)");
ab = n * (n - 1) / 2;
cd = (ab - 1) / 2;
abcd = ab * cd;
efgh = (abcd - 1) / 2;
smax = abcd * efgh;
// on startup
if (typeof s === 'undefined') {
// Setup GeoGebra
var parameters = {
"id": "ggbApplet",
"width": 960,
"height": 600,
"showToolBar": true,
"borderColor": null,
"showMenuBar": true,
"allowStyleBar": true,
"showAlgebraInput": false,
"enableLabelDrags": true,
"enableShiftDragZoom": true,
"capturingThreshold": null,
"showToolBarHelp": false,
"errorDialogsActive": true,
"showTutorialLink": true,
"showLogging": true,
"useBrowserForJS": false,
"perspective": "AG"
};
var applet = new GGBApplet(parameters, '5.0', 'applet_container');
applet.inject('applet_container');
s = getQueryVariable("s");
if (!s) {
output("Assuming s=0, please append &s=... to the URL to override");
s = 0;
}
output("Starting with n=" + n + ", s=" + s);
onlyX = getQueryVariable("u");
if (!onlyX) {
onlyX = 0;
}
if (onlyX != 0) {
output("Considering only results d=" + onlyX);
}
mindegree = getQueryVariable("m");
if (!mindegree) {
output("Assuming mindegree=1, please append &m=... to the URL to override");
mindegree = 1;
}
maxdegree = getQueryVariable("M");
if (!maxdegree) {
output("Assuming maxdegree=2, please append &M=... to the URL to override");
maxdegree = 2;
}
symm = getQueryVariable("S");
if (!symm) {
output("Not ignoring symmetry, please append &S=0 to the URL to ignore (this speeds up computation)");
symm = 1;
}
found = getQueryVariable("f");
if (!found) {
output("Not ignoring already found lengths, please append &f=1 to override");
found = 0;
}
approx = getQueryVariable("a");
bestapprox = Infinity;
if (!approx) {
approx = 0;
}
server = getQueryVariable("z");
cont = getQueryVariable("c");
if (!server) {
server = "";
} else if (!cont) {
var query = window.location.search.substring(1);
server_id = post("action=register¶meters=" + encodeURIComponent(query));
if (server_id.length != 7) {
errorOutput("Server communication to " + server + " seems not working");
}
output("Obtained job ID " + server_id);
}
if (!cont) {
cont = "";
} else {
cont_eval = post("action=continue&id=" + cont);
server_id = cont;
}
epsilon = getQueryVariable("E");
if (!epsilon) {
epsilon = Infinity;
output("Assuming epsilon=" + epsilon + ", please append &E=... to the URL to override");
}
sol = 0;
notchecked = 0;
startTime = new Date();
foundlist = [];
output("s can be incremented until " + smax);
e = getQueryVariable("e");
if (e) {
output("Computation will stop at " + e);
} else {
e = smax;
}
output("Waiting for the CAS...")
}
if (s < 0 || s >= smax) errorOutput("Invalid s: " + s);
var segment = deserialize(s, abcd);
var ABCD = segment['a'];
var EFGH = segment['b'];
var R = deserialize(ABCD, ab);
var AB = R['a'];
var CD = R['b'];
var A_B_ = deserialize(AB, n);
var C_D_ = deserialize(CD, n);
var S = deserialize(EFGH, ab);
var EF = S['a'];
var GH = S['b'];
var E_F_ = deserialize(EF, n);
var G_H_ = deserialize(GH, n);
A = A_B_['a'];
B = A_B_['b'];
C = C_D_['a'];
D = C_D_['b'];
E = E_F_['a'];
F = E_F_['b'];
G = G_H_['a'];
H = G_H_['b'];
// console.log("A = " + A + ", B = " + B + ", C = " + C + ", D = " + D + ", E = " + E + ", F = " + F + ", G = " + G + ", H = " + H);
}
function post(message) {
var xhttp = new XMLHttpRequest();
xhttp.open("POST", server, false);
xhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xhttp.send(message);
return xhttp.responseText;
}
function heartbeat() {
var changes = "s=" + s + ";";
changes += "foundlist=" + JSON.stringify(foundlist) + ";";
changes += "sol=" + sol + ";";
changes += "bestapprox=" + bestapprox + ";";
changes += "startTime=new Date(" + JSON.stringify(startTime) + ");";
post("action=heartbeat&id=" + server_id + "&changes=" + encodeURIComponent(changes));
}
function CASinitCommands() {
compute("restart;");
var factorsqrfree = "factorsqrfree(p):=begin local pf,r,ii; pf:=factor(p); if (sommet(pf)!='*') begin if (sommet(pf)=='^') return op(pf)[0]; else begin if (sommet(pf)!=sommet(-x)) return pf; else return factorsqrfree(-pf); end; end; opf:=op(pf); r:=1; for ii from 0 to size(opf)-1 do r:=r*factorsqrfree(opf[ii]); od return r end";
var cos2piOverNMinpoly = "cos2piOverNMinpoly(n):=begin local j, p, q, r; p:=simplify((tchebyshev1(n)-1)/(x-1)); for j from 1 to n/2 do q:=tchebyshev1(j)-1; r:=gcd(p,q); p:=simplify(p/r); od; return factorsqrfree(primpart(p)); end";
compute("caseval(\"init geogebra\");");
compute(factorsqrfree);
compute(cos2piOverNMinpoly);
}
function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
function start_stop_button() {
var buttontext = "Pause computations";
if (!running) {
running = true;
} else {
running = false;
buttontext = "Continue computations";
status_div.innerHTML = "Status: Paused while computing n=" + n + ", s=" + s;
}
var element = document.getElementById('start_stop');
element.innerHTML = buttontext;
}
async function doCAScomputation() {
running = true;
var element = document.getElementById('start_stop');
element.disabled = false;
CASinitCommands();
if (cont != "") {
output("Attempt to continue a previous run...");
eval(cont_eval);
ggb = ggb.replace(/ /g, "+");
ggbApplet.setBase64(ggb);
if (!(typeof fulllog === 'undefined')) {
element = document.getElementById('output');
var oldValue = element.value;
element.value = oldValue + fulllog;
element.scrollTop = 99999;
}
}
while (s < e - 1) {
if (symm == 1 || (A == 0 && 2 * B <= n)) {
CAScomputeCurrent();
await sleep(1);
while (!running) {
await sleep(300);
}
} else {
notchecked++;
}
s++;
go();
if (server != "" && s % 10000 == 0 && s > 0) {
output("Avoiding crash, normal restart...");
reload();
}
}
CAScomputeCurrent();
outputElapsedTime();
output("Finished after finding " + sol + " solutions");
output(notchecked + " cases were not checked to ignore symmetry");
hide_button();
}
function hide_button() {
var element = document.getElementById('start_stop');
element.hidden = true;
}
function output(text) {
console.log("OUTPUT: " + text);
var element = document.getElementById('output');
var oldValue = element.value;
element.value = oldValue + text + "\n";
element.scrollTop = 99999;
if (typeof ggbApplet != 'undefined' && server != "") {
post("action=update&id=" + server_id + "&update=" + text + "&ggb=" +
encodeURIComponent(ggbApplet.getBase64()));
}
}
function outputElapsedTime() {
var endTime = new Date();
var timeDiff = endTime - startTime;
timeDiff /= 1000;
var seconds = Math.round(timeDiff % 60);
timeDiff = Math.floor(timeDiff / 60);
var minutes = Math.round(timeDiff % 60);
timeDiff = Math.floor(timeDiff / 60);
var hours = Math.round(timeDiff % 24);
output("Elapsed time: " + hours + "h " + minutes + "m " + seconds + "s");
}
function errorOutput(text) {
output(text);
outputElapsedTime();
hide_button();
throw(text);
}
function CAScomputeCurrent() {
done = false;
// c=cos(2pi/n)
console.log("n=" + n + ", s=" + s);
status_div.innerHTML = "Status: Computing n=" + n + ", s=" + s;
if (server != "" && s > 0 && s % 100 == 0)
heartbeat();
var c = compute("subst(cos2piOverNMinpoly(" + n + "),[x=c]);");
// s=sin(2pi/n)
var s_ = "s^2+c^2-1";
var d = "0,"; // technical trick
// P0=(0,0), P1=(1,0)
var p = "p0x,p0y,p1x-1,p1y";
var maxindex = Math.max(B, C, D, E, F, G, H);
var v = "c,s,ax,ay,bx,b_y,cx,cy,dx,dy,ex,ey,fx,fy,gx,gy,hx,hy,qx,qy,rx,ry";
// defining vertex points and segment endpoints
for (var i = 0; i <= maxindex; ++i) {
var px = "p" + i + "x";
var py = "p" + i + "y";
v += "," + px + "," + py;
if (i > 1) {
var ppx = "p" + (i - 1) + "x";
var ppy = "p" + (i - 1) + "y";
var pppx = "p" + (i - 2) + "x";
var pppy = "p" + (i - 2) + "y";
p += ",-" + px + "-s*" + ppy + "+" + ppx + "+c*" + ppx + "+s*" + pppy + "-c*" + pppx;
p += ",-" + py + "+" + ppy + "+c*" + ppy + "+s*" + ppx + "-c*" + pppy + "-s*" + pppx;
}
if (A == i) d += ",ax-" + px + ",ay-" + py;
if (B == i) d += ",bx-" + px + ",b_y-" + py;
if (C == i) d += ",cx-" + px + ",cy-" + py;
if (D == i) d += ",dx-" + px + ",dy-" + py;
if (E == i) d += ",ex-" + px + ",ey-" + py;
if (F == i) d += ",fx-" + px + ",fy-" + py;
if (G == i) d += ",gx-" + px + ",gy-" + py;
if (H == i) d += ",hx-" + px + ",hy-" + py;
}
// AQB, CQD are collinear:
var q = "ax*qy+qx*b_y+bx*ay-ax*b_y-qx*ay-bx*qy,cx*qy+qx*dy+dx*cy-cx*dy-qx*cy-dx*qy";
// ERF, GRH are collinear:
var r = "ex*ry+rx*fy+fx*ey-ex*fy-rx*ey-fx*ry,gx*ry+rx*hy+hx*gy-gx*hy-rx*gy-hx*ry";
// the requested length is QR=d
var qr = "d^2-(qx-rx)^2-(qy-ry)^2";
var ideal = c + "," + s_ + "," + p + "," + d + "," + q + "," + r + "," + qr;
var eliminatecommand = "eliminate([" + ideal + "],[" + v + "]);";
var eliminate = compute(eliminatecommand);
var normresultcommand = "expand(" + eliminate + ")/lcoeff(" + eliminate + ",d);";
eliminate = compute(normresultcommand);
var factorscommand = "factors(" + eliminate + ");";
var factors = compute(factorscommand);
var factor1command = factors + "[0][0]";
var factor1 = compute(factor1command);
// don't consider d=0
if (factor1 != "d") {
var degreecommand = "degree(" + factor1 + ");";
var degree = compute(degreecommand);
var deg = Number(degree);
var accepted;
var factorcommand;
var factor;
var solvecommand;
var solve;
accepted = (deg >= mindegree && deg <= maxdegree);
if (accepted) {
factorcommand = "factor(" + eliminate + ");";
factor = compute(factorcommand);
solvecommand = "solve([" + factor + "=0,d>0])";
solve = compute(solvecommand);
}
if (accepted && onlyX != 0 && solve != "{{d=" + onlyX + "}}") {
accepted = false;
}
if (accepted && found == 1) {
var founditem = false;
for (var i = 0; i < foundlist.length && !founditem; ++i)
founditem = foundlist[i] == eliminate;
accepted = !founditem;
}
if (accepted && approx != 0) {
accepted = false;
var fsolvecommand = "fsolve(" + factor1 + ")";
var fsolve = compute(fsolvecommand);
var rootlist = fsolve.substr(1, fsolve.length - 2);
var roots = rootlist.split(",");
for (var i = 0; i < roots.length; ++i) {
var diff = Math.abs(Number(roots[i]) - approx);
if (diff <= Math.abs(bestapprox - approx) && Number(roots[i]) != approx) {
if (epsilon > diff) {
bestapprox = roots[i];
output("Found better approximation: " + bestapprox);
accepted = true;
}
}
}
}
if (accepted) {
output("n=" + n + ", s=" + s + ": A=" + A + ", B=" + B + ", C=" + C +
", D=" + D + ", E=" + E + ", F=" + F + ", G=" + G + ", H=" + H + ": " + (eliminate + ", " +
solve).replace(/d/g, "RS"));
foundlist.push(eliminate);
sol = sol + 1;
var lAB;
var lCD;
var lEF;
var lGH;
if (A < B) lAB = A + "," + B; else lAB = B + "," + A;
var ld = "l_{" + lAB + "}";
if (!ggbApplet.exists(ld)) {
ggbApplet.evalCommand(ld + "=Line(A_{" + A + "},A_{" + B + "})");
ggbApplet.setColor(ld, 192, 192, 192);
ggbApplet.setLabelVisible(ld, false);
}
if (C < D) lCD = C + "," + D; else lCD = D + "," + C;
var le = "l_{" + lCD + "}";
if (!ggbApplet.exists(le)) {
ggbApplet.evalCommand(le + "=Line(A_{" + C + "},A_{" + D + "})");
ggbApplet.setColor(le, 192, 192, 192);
ggbApplet.setLabelVisible(le, false);
}
if (E < F) lEF = E + "," + F; else lEF = E + "," + F;
var lf = "l_{" + lEF + "}";
if (!ggbApplet.exists(lf)) {
ggbApplet.evalCommand(lf + "=Line(A_{" + E + "},A_{" + F + "})");
ggbApplet.setColor(lf, 192, 192, 192);
ggbApplet.setLabelVisible(lf, false);
}
if (G < H) lGH = G + "," + H; else lGH = H + "," + H;
var lg = "l_{" + lGH + "}";
if (!ggbApplet.exists(lg)) {
ggbApplet.evalCommand(lg + "=Line(A_{" + G + "},A_{" + H + "})");
ggbApplet.setColor(lg, 192, 192, 192);
ggbApplet.setLabelVisible(lg, false);
}
var pR1 = "R_{" + lAB + "," + lCD + "}";
var pR2 = "R_{" + lCD + "," + lAB + "}";
var pR;
if (ggbApplet.exists(pR1)) pR = pR1;
else if (ggbApplet.exists(pR2)) pR = pR2;
else {
pR = pR1;
ggbApplet.evalCommand(pR + "=Intersect(" + ld + "," + le + ")");
ggbApplet.setVisible(pR, false);
}
var pS1 = "S_{" + lEF + "," + lGH + "}";
var pS2 = "S_{" + lGH + "," + lEF + "}";
var pS;
if (ggbApplet.exists(pS1)) pS = pS1;
else if (ggbApplet.exists(pS2)) pS = pS2;
else {
pS = pS1;
ggbApplet.evalCommand(pS + "=Intersect(" + lf + "," + lg + ")");
ggbApplet.setVisible(pS, false);
}
var sRS = "RS_{" + sol + "}";
ggbApplet.evalCommand(sRS + "=Segment(" + pR + "," + pS + ")");
ggbApplet.setLabelVisible(sRS, false);
if (accepted && approx != 0) {
var sRSlength = ggbApplet.getValue(sRS);
if (Math.abs(sRSlength - bestapprox) > 0.01) {
output("Geometrically non-matching case, consider looking for a star-regular variant");
ggbApplet.setColor(sRS, 64, 0, 0);
}
} else {
ggbApplet.setColor(sRS, 0, 0, 64);
}
}
}
done = true;
}
</script>
<script type='text/javascript'>
var Module = {
preRun: [],
postRun: [],
printErr: function (text) {
if (0) { // XXX disabled for safety typeof dump == 'function') {
dump(text + '\n'); // fast, straight to the real console
} else {
console.log(text);
}
},
setStatus: function (text) {
if (Module.setStatus.interval) clearInterval(Module.setStatus.interval);
var m = text.match(/([^(]+)\((\d+(\.\d+)?)\/(\d+)\)/);
if (m) text = m[1];
console.log("Giac status = " + text);
if (text == "") { // ready to go
outputElapsedTime();
output("...CAS is up and running");
output("Setting up GeoGebra applet");
ggbApplet.setCustomToolBar("0|14");
// ggbApplet.setAxesVisible(false,false);
ggbApplet.setAxesVisible(1, false, false, false);
ggbApplet.evalCommand("A=(0,0)");
ggbApplet.evalCommand("B=(1,0)");
ggbApplet.evalCommand("poly1=Polygon(A,B," + n + ")");
ggbApplet.setLabelVisible("poly1", false);
for (i = 0; i < n; ++i) {
var oldLabel = String.fromCharCode(65 + (i % 24));
if (oldLabel == 'X') oldLabel = 'Z';
var index = Math.floor(i / 24);
if (index > 0) {
oldLabel += "_" + index
}
var newLabel = "A_{" + i + "}";
ggbApplet.renameObject(oldLabel, newLabel);
ggbApplet.setVisible(newLabel, false);
}
doCAScomputation();
}
},
totalDependencies: 0,
monitorRunDependencies: function (left) {
this.totalDependencies = Math.max(this.totalDependencies, left);
Module.setStatus(left ? 'Preparing... (' + (this.totalDependencies - left) + '/' + this.totalDependencies + ')' : 'All downloads complete.');
}
};
Module.setStatus('Downloading...');
</script>
<script src="giac.js"></script>
</body>
</html>