This repository has been archived by the owner on Jul 30, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
calc.html
executable file
·1807 lines (1717 loc) · 69.3 KB
/
calc.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
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE HTML>
<html>
<head>
<title>Calc</title>
<meta charset="UTF-8"/>
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1" />
<style type="text/css">
<!--
html, body { margin:0; padding:0; height:100%; width:100%;}
body { background:#fff; }
#cont { height:100%; width:100%; display:table; text-align:center; }
#cont > div { width:100%; margin:0 auto; vertical-align:top; }
#incont2 { position:relative; width:40.5em; margin:0 auto; background-color:#f8f8f8; padding:0;}
#in {
position:absolute; resize:none; outline:none; padding:0.2em;
-webkit-appearance:none; -moz-appearance:none; appearance:none;
text-align:center; font: 1em sans-serif; color:#888;
box-shadow:0 0 0.3em #bbb; -webkit-box-shadow:0 0 0.3em #bbb; -moz-box-shadow:0 0 0.3em #bbb;
background: transparent; top:0; left:0; z-index:1000; margin:0; line-height:1.2em;
width:40em; border:none;
}
#fake1, #fake2 { position:absolute; resize:none; outline:none; appearance:none;
border:none; visibility:hidden; font: 1em sans-serif; line-height:1.2em; width:0;}
#inbg { position:absolute; left:0; top:0; z-index:0; background-color:#f8f8f8;
padding:0.2em; width:40em;}
.inbg { float:left; height:1.2em; z-index:1; padding:0; margin:0; width:100%; border:none;}
#out { padding:1em 0; margin:0; font:1.2em serif; color:#777; line-height:1.5em;}
#out font.clickable { cursor:pointer }
#outcont, #hcont { width:40em; padding:0.2em; margin:0 auto; }
#outcont { min-height:5em; background-color:none;}
#hcont { min-height:1.2em; background-color:#eee; box-shadow: 0 0 0.3em #bbb;
-webkit-box-shadow: 0 0 0.3em #bbb; -moz-box-shadow: 0 0 0.3em #bbb;
margin-bottom:1em}
#help1, #help2, #help3, #help4 {
float:left; font:1em serif; color:#777; cursor:pointer;
border-bottom:0.2em solid transparent;
-webkit-user-select: none; -moz-user-select: none;
-khtml-user-select: none; -ms-user-select: none;
width:25%; margin:0;
}
#sep {clear:both; width:100%; height:0.1em; line-height:0.1em;
border-top:0.1em solid #aaa; display:none }
#help1c, #help2c, #help3c, #help4c {
clear:both; width:100%; height:21em;
display:none; overflow:auto; font:1em serif; color:#777;
}
#hintbox {
position:absolute; left:51em; display:none;
font:0.8em sans-serif; color:#888; white-space: nowrap;
}
.t1 { width:100%; }
.t1 tr td { padding:.5em 0; }
.t1 tr:nth-child(even) { background-color:#e5e5e5; }
.t2 { border:0; border-collapse:collapse; }
.t2 td:nth-child(1) { width:15em; padding-right:2em; text-align:right }
.t2 td:nth-child(2) { max-width:20em; padding-left:2em; text-align:left; font-style:italic }
.t3 { width:100%; border:0; border-collapse:collapse; }
.t3 td:nth-child(1) { width:15em; text-align:right; }
.t3a td:nth-child(1) { font-style:italic }
.t3 td:nth-child(2) { width:6em; text-align:center; }
.t3 td:nth-child(3) { width:15em; text-align:left; font-style:italic}
#examples tr{ cursor:pointer; }
#examples tr:hover { background-color:rgba(0,0,0,0.1); }
.graphcont{ position:relative; width:100%; height:20em; padding:0;
margin:0 auto; border:0.1em solid #dddddd; }
.graphcont svg { width:100%; height:100% }
#button { display:none; border:0; position:relative; z-index:2000; height:2.8em; margin-top:0.2em;}
.rangeMenu{ position:absolute; height:5.2em; display:table;
top:3em; left:1em; background-color:#eee;
line-height:1.3em; padding:0.4em; border:0.1em solid #777; }
.menuItem { display:table-row; }
.menuItem input {
-webkit-appearance:none; -moz-appearance:none; appearance:none;
border:1px solid #ccc; background-color:#f9f9f9; color:#777;
width:13em; text-align:center; margin:0.2em 0.4em;
}
.menuItem > * { display:table-cell; }
@media only screen and (max-width:42em){
#outcont, #hcont, #incont2 { width:100%; margin:0; font-size:0.9em; }
#in, #inbg { width:100%; margin:0; padding:0.2em 0;}
#fake1, #fake2 { margin:0; padding:0.2em 0;}
.inbg { width:100%; }
#help1, #help2, #help3, #help4 { font-size:0.9em;}
#help1c, #help2c, #help3c, #help4c { font-size:0.9em; height:auto;}
#out { font-size:0.9em; line-height:1.1em;}
#hcont { min-height:0.9em; }
#outcont, #hcont { padding:0.2em 0; }
#hintbox { display:none; }
.t2 td:nth-child(1) { width:auto; min-width:3em; padding-right:0.9em; }
.t2 td:nth-child(2) { padding-left:0.9em; }
.t3 td:nth-child(1) { width:auto; }
.t3 td:nth-child(2) { width:3em; }
.t3 td:nth-child(3) { width:auto; }
.graphcont { border-left:0; border-right:0; }
#button { display:block; }
}
-->
</style>
<link rel="shortcut icon" href="data:image/x-icon;base64,AAABAAEAEBACAAEAAQCwAAAAFgAAACgAAAAQAAAAIAAAAAEAAQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABYWFgAAAAAAP8AAAD/AAAA//8AAP//AAD//AAA//gAAD/4AAAf+AAAH/gAAB/8AAAf/wAAP/8AAP//AAD//wAAAP8AAAD/AAD/AAAA/wAAAP//AAD//wAA//wAAP/4AAA/+AAAH/gAAB/4AAAf/AAAH/8AAD//AAD//wAA//8AAAD/AAAA/wAA"/>
<script language="javascript">
<!--
var svgns="http://www.w3.org/2000/svg";
// Add functions to SVG elements
SVGElement.prototype.sets = function(att) { // set multiple attributes at once
for(var key in att) { this.setAttribute(key, att[key]); }
}
SVGElement.prototype.Group = function() {
var e=document.createElementNS(svgns,"g");
return this.appendChild(e);
}
SVGElement.prototype.Line = function(x1,y1,x2,y2,stroke,thickness,dash) {
var e=document.createElementNS(svgns,"line");
if(!dash) { dash="none"; }
e.sets({"x1":""+x1, "y1":""+y1, "x2":""+x2, "y2":""+y2, "stroke-dasharray":dash,
"vector-effect":"non-scaling-stroke", "stroke-width":thickness, "stroke":stroke });
return this.appendChild(e);
};
SVGElement.prototype.Rect = function(x,y,w,h,fill,stroke,thickness) {
var e=document.createElementNS(svgns,"rect");
e.sets({"x":""+x, "width":""+w, "y":""+y, "height":""+h, "stroke":stroke,
"stroke-width":thickness, "fill":fill, "vector-effect":"non-scaling-stroke" });
return this.appendChild(e);
};
SVGElement.prototype.Polyline = function(points,stroke,thickness,dash) {
var e=document.createElementNS(svgns,"polyline");
if(!dash) { dash="none"; }
e.sets({"points":points, "vector-effect":"non-scaling-stroke",
"fill":"none", "stroke":stroke, "stroke-width":thickness, "stroke-dasharray":dash });
return this.appendChild(e);
};
SVGElement.prototype.Path = function(pathdata,stroke,thickness,dash) {
var e=document.createElementNS(svgns,"path");
if(!dash) { dash="none"; }
e.sets({"d":pathdata, "vector-effect":"non-scaling-stroke",
"fill":"none", "stroke":stroke, "stroke-width":thickness, "stroke-dasharray":dash });
return this.appendChild(e);
};
SVGElement.prototype.Text = function(str,x,y,fontsize,color,align,rot) {
var t=document.createElementNS(svgns,"text");
t.sets({"x":x+"","y":y+"","fill":color});
if(fontsize) { t.setAttribute("font-size",fontsize+""); }
t.textContent = str;
this.appendChild(t);
t.move(x,y,align,rot);
return t;
};
SVGTextElement.prototype.move = function(x,y,align,rot) {
// 'align' can contain NW, NE, NC, SW, SE, SC, MW, ME, or MC
var b=this.getBBox(); var tw=parseFloat(b.width), th=parseFloat(b.height)*0.5;
if (align==undefined) {align='SW';}
tw = tw*[0,.5,1]["WCE".indexOf(align[1])];
th = th*[0,.5,1]["SMN".indexOf(align[0])];
this.sets({"x":x-tw, "y":y+th});
// Now rotate
if (rot==undefined) { rot=""; }
if (rot.length>0) { this.setAttribute("transform","rotate("+rot+","+x+","+y+")"); }
};
SVGTextElement.prototype.Tspan = function(str,dy) {
var t=document.createElementNS(svgns,"tspan");
t.setAttribute("dy",dy);
t.textContent = str;
return this.appendChild(t);
};
// Functions controling the SVG plot
function findBestBounds(f) { // Find the best plot limits automatically
var xm,xM,ym,yM;
var m=Math.log(1e-50),M=Math.log(1e20);
var i,j,k,kk,lnx,x,y,prevx,dxm,dxM,dx,dy,okm=false,okM=false, prec;
// The first step is to find a range containing finite values of the function
for(k=1000; k<=100000; k*=10) { // several attemps, refining the resolution each time
dx=(M-m)/k;
// First, negative (positive) numbers for finding left (right) boundary
for(lnx=M; lnx>=m; lnx-=dx) {
if(okm && okM) { break; }
x=Math.exp(lnx);
if(!okm && Math.abs(f(-x))<1e30) { xm=[-(prevx||x),-x]; okm=true; }
if(!okM && Math.abs(f( x))<1e30) { xM=[ x, prevx||x ]; okM=true; }
prevx=x;
}
// Second, positive (negative) numbers for finding left (right) boundary
for(lnx=m; lnx<=M; lnx+=dx) {
if(okm && okM) { break; }
x=Math.exp(lnx);
if(!okm && Math.abs(f( x))<1e30) { xm=[ prevx||x , x]; okm=true; }
if(!okM && Math.abs(f(-x))<1e30) { xM=[-x,-(prevx||x)]; okM=true; }
prevx=x;
}
if(okm && okM) { break; }
}
// If could not find finite numbers, then error
if(!okm || !okM) { return false; }
// Refine the xm and xM locations
okm=false; okM=false; prec=1e-10; // prec is precision
dxm=(xm[1]-xm[0])*prec; dxM=(xM[1]-xM[0])*prec;
while(!okm || !okM) {
if(xm[1]-xm[0]>0) { dxm=Math.min((xM[1]-xm[0])*prec,dxm); }
if(xM[1]-xM[0]>0) { dxM=Math.min((xM[1]-xm[0])*prec,dxM); }
if(!okm && xm[1]-xm[0]<=dxm) { okm=true; }
if(!okM && xM[1]-xM[0]<=dxM) { okM=true; }
if(!okm) {
x=(xm[0]+xm[1])*.5;
if(Math.abs(f(x))<1e100) { xm[1]=x; } else { xm[0]=x; }
}
if(!okM) {
x=(xM[0]+xM[1])*.5;
if(Math.abs(f(x))<1e100) { xM[0]=x; } else { xM[1]=x; }
}
}
xm=xm[1]; xM=xM[0]; // Now we have the global x boundaries
//alert(xm+" "+xM);
// Adaptatively find best x range
var adaptative = {};
adaptative.x_=new Array(5); adaptative.y_=new Array(5);
adaptative.curveEq = f;
// First, we define a function for adaptative refinement
adaptative.refinement = function(x0,x1,depth,eps) {
if(depth>=this.maxdepth) {return depth;}
this.dx=(x1-x0)*0.25;
this.x_[0]=x0; this.x_[1]=x0+this.dx; this.x_[2]=x0+2*this.dx; this.x_[3]=x0+3*this.dx; this.x_[4]=x1;
this.y0=Number.MAX_VALUE; this.y1=-Number.MAX_VALUE;
for(this.k=0;this.k<5;this.k+=1) {
this.y_[this.k]=this.curveEq(this.x_[this.k]);
if(this.y_[this.k]<this.y0) { this.y0=this.y_[this.k]; } // find local min
if(this.y_[this.k]>this.y1) { this.y1=this.y_[this.k]; } // find local max
}
if(this.y0*this.y1>0) { // pb of numerical precision
if(Math.abs(this.y1-this.y0)<1e-6*Math.max(Math.abs(this.y0),Math.abs(this.y1))) {return depth;}
}
this.k=1; this.i=this.y_[1]-this.y_[0];
while(this.k<4) {
this.j=this.y_[this.k+1]-this.y_[this.k];
if(this.i*this.j<0) {break;}
this.i=this.j; this.k+=1;
}
this.refine=false;
if(this.k<4) { // if oscillating, refine
this.refine=true;
} else { // if not oscillating, check if smooth enough
this.q1=this.y_[0]-5.*this.y_[1]+19.*this.y_[2]+9.*this.y_[3]; // first quadrature
this.q2=10.*this.y_[2]+16.*this.y_[3]-2.*this.y_[4]; // second quadrature
if(Math.abs(this.q2-this.q1)>eps*Math.abs(this.q2)) { this.refine=true; }
}
if(this.refine) {
return Math.max(
this.refinement(x0,this.x_[2],depth+1,eps*2),
this.refinement(this.x_[2],x1,depth+1,eps*2) );
}
return depth;
}
// Define initial x values
x=new Array(100); var u=new Array(99);
x.linspace(xm,xM);
// First refinement
var ok=false, kmax=100, i0, i1, xlim=[xm,xM], xmax, xmM;
adaptative.maxdepth=1;
for(k=0; k<kmax; k+=1) {
for(i=0; i<99; i+=1) { u[i]=adaptative.refinement(x[i],x[i+1],0,1e-6); }
i0=u.indexOf(1);
if(i0<0) {
if(!ok) { x.shrinkTowards0(0.02); }
else { dx=(x[50]-x[49])*0.3; x.linspace(x[0]+dx,x[99]+dx); }
} else {
ok=true; i1=u.lastIndexOf(1);
if(i0==0 && i1==98) { break; }
x.linspace(x[i0],x[i1+1]);
}
xmax=Math.max(Math.abs(x[99]),Math.abs(x[0]));
if(Math.abs(x[99]-x[0])<Math.max(1e-20, 0.01*xmax)) { k=kmax; break; }
}
xmM=adjustWith0(x[0], x[99], k>=kmax-1);
xm=Math.max(xm,xmM[0]); xM=Math.min(xM,xmM[1]);
//alert(xm+" "+xM);
// Second refinement
if(k<kmax-1) {
x.linspace(xm,xM);
adaptative.maxdepth=4; kmax=40; k=0; ok=false;
var utot, umax, v0, v1, vtot, j0, j1, v=new Array(100);
while(k<kmax) {
k+=1;
if(k>kmax-5 && adaptative.maxdepth<9) { k=1; adaptative.maxdepth+=1; }
utot=0; umax=0; v[99]=0;
for(i=0; i<99; i+=1) {
u[i]=adaptative.refinement(x[i],x[i+1],0,4e-7);
if(u[i]>umax) { umax=u[i]; }
v[i]=(u[i]>=adaptative.maxdepth)?1:0;
if(v[i]) { utot+=1; }
}
//alert(u);
// utot is the number of maximal refinement locations
if(utot>1) {
i0=v.indexOf(1); i1=v.lastIndexOf(1);
// If there are several places where max-refinement has been found,
// we calculate vtot, the number of "switches" from maximal to non-maximal refinement
v0=0; vtot=0;
for(j=0; j<100; j+=1) {
v1=v[j];
if(v0!=v1) { vtot+=1; v[j]=1; } else { v[j]=0; }
v0=v1;
}
// now j0 the second switch, j1 the penultimate switch
if(vtot<=3 || vtot>20) {
if(utot>=99) {
x.shrinkTowards0(Math.max(0.1,Math.min(0.9,Math.pow(2,-adaptative.maxdepth*(utot/100)))));
} else {
if(i0==1 && i1==98) { break; }
x.linspace(x[i0],x[i1+1]);
if(utot<50 && adaptative.maxdepth<9) { k=1; adaptative.maxdepth+=1; }
}
} else if(vtot>6) {
// If a bit too many switches, we remove the first and last ones
j0=v.indexOf(1); j0=v.indexOf(1,j0+1); // second switch
j1=v.lastIndexOf(1); j1=v.lastIndexOf(1,j1-1); // penultimate switch
x.linspace(x[j0],x[j1+1]);
} else break;
} else {
// If zero or one max-refinement locations found,
// then consider reducing the range to the half-width
umax*=0.5;
for(i=0; i<99; i+=1) { u[i]=(u[i]>=umax)?1:0; }
i0=u.indexOf(1); i1=u.lastIndexOf(1);
if(i0==0 && i1==98) {
if(!ok) { ok=true; x.shrinkTowards0(1.001); continue; }
break;
}
x.linspace(x[i0],x[i1+1]);
}
ok=false;
xmax=Math.max(Math.abs(x[99]),Math.abs(x[0]));
if(Math.abs(x[99]-x[0])<Math.max(1e-20, 0.01*xmax)) { k=kmax; break; }
}
xmM=adjustWith0(x[0], x[99], k>=kmax-1);
xm=Math.max(xm,xmM[0]); xM=Math.min(xM,xmM[1]);
}
// If bug, then reset to default
if(k==1 || !isFinite(xm) || !isFinite(xM) || Math.abs(xM-xm)/Math.max(Math.abs(xm),Math.abs(xM))<1e-6) {
xlim.shrinkTowards0(4/(xlim[1]-xlim[0]));
xm=xlim[0]; xM=xlim[1];
}
//alert([xm,xM]);
// X range is now found.
// Find best Y range
y=[]; dx=(xM-xm)/999.;
var l;
for(k=0;k<999;k+=1) { l=f(xm+dx*k); if(isFinite(l)) { y.push(l); } }
l=f(xM); if(isFinite(l)) { y.push(l); }
y.sort(function(a,b) {return a-b;});
//alert(y);
l=y.length;
ym=y[0]; yM=y[l-1];
var yh=new Array(11), dk, hmax;
for(i=0;i<30;i+=1) {
dy=(yM-ym)*0.1;
// compute histogram yh in 10 bins
yh[0]=0; yh[10]=l-1; dk=100; hmax=0;
for(k=1;k<10;k+=1) {
if(yh[k-1]>=l-1) { yh[k]=l-1; continue; }
dk=Math.ceil((l-yh[k-1])/(11-k));
yh[k]=yh[k-1]; y0=ym+dy*k;
while(dk>1) {
if(yh[k]+dk>l-2 || y[yh[k]+dk]>y0) { dk=Math.floor(dk*0.5); }
else { yh[k]+=dk; }
}
}
for(k=0;k<10;k+=1) {
yh[k]=yh[k+1]-yh[k];
if(yh[k]>hmax) {hmax=yh[k];}
}
// Reduce where histogram contains more than 99% of data
hmax*=0.01;
for(k=0;k<10;k+=1) { yh[k]=(yh[k]>hmax)?1:0; }
i0=yh.indexOf(1); i1=yh.lastIndexOf(1);
if(i0==0 && i1>=9) { break; }
ym=ym+dy*i0; yM=ym+dy*(i1+1);
}
// Adjust y range
if(Math.abs(yM+ym)<yM*0.5) { yM=(yM-ym)*0.5; ym=-yM; }
if(yM>0 && ym>0 && ym<yM/5) { ym=0; }
if(yM<0 && ym<0 && yM>ym/5) { yM=0; }
// Adjust x range
k=-1;
do { k+=1; y0=f(xm+dx*k); } while(y0<ym || y0>yM)
xm=xm+dx*k;
k=-1;
do { k+=1; y0=f(xM-dx*k); } while(y0<ym || y0>yM)
xM=xM-dx*k;
// Final adjustment
dx=(xM-xm)/10; xm-=dx; xM+=dx;
dy=(yM-ym)/10; ym-=dy; yM+=dy;
//alert([xm,xM,ym,yM]);
return [xm,xM,ym,yM];
}
// Main class
function Plot(container, bounds) {
var i;
var gcont=document.createElement("div");
var g=document.createElementNS(svgns,"svg");
gcont.appendChild(g);
container.appendChild(gcont);
gcont.className="graphcont";
g.setAttribute("width" ,"100%");
g.setAttribute("height","100%");
this.plot = g.Group();
this.w = gcont.clientWidth;
this.h = gcont.clientHeight;
this.resize(bounds[0],bounds[1],bounds[2],bounds[3]);
// min and max zone to display things (even out of the viewport)
this.m=[0,0];
this.M=[0,0];
// Create axes
this.ax=this.plot.Line(0,0,0,0,"#777",1);
this.ay=this.plot.Line(0,0,0,0,"#777",1);
// Create ticks
this.ticks=[this.plot.Group(), this.plot.Group()]; // x and y
this.nt=[22, 16]; // approximate number of ticks
for(i=0; i<2*this.nt[0]; i+=1) { this.ticks[0].Line(0,0,0,0,"#777",0); }
for(i=0; i<2*this.nt[1]; i+=1) { this.ticks[1].Line(0,0,0,0,"#777",0); }
// Create tick labels with an optional "tspan" for powers.
this.tickL=[this.plot.Group(), this.plot.Group()];
for(i=0; i<2*this.nt[0]; i+=1) { this.tickL[0].Text(" ",0,0,12,"#777",'NC').Tspan("",-5/this.s[0]).setAttribute("dy",-5); }
for(i=0; i<2*this.nt[1]; i+=1) { this.tickL[1].Text(" ",0,0,12,"#777",'MW').Tspan("",-5/this.s[0]).setAttribute("dy",-5); }
// Create curve
this.ncurves=0;
this.quickCurve=[];
this.curve=[];
this.curveEq=[];
this.np=1000;
this.points=new Array(2*this.np);
// Create a transparent overlay for detecting mousedown
var rect=g.Rect(0,0,"100%","100%","transparent","transparent",0);
rect.onmousedown = moveBegin;
rect.plot = this;
this.rect = rect;
// Create zoom buttons
var x=5,y=5,w=14,h=14;
this.ZoomButton(g,x,y ,w,h,"+").z=[1.1,1.1];
this.ZoomButton(g,x,y+h ,w,h,"-").z=[1/1.1,1/1.1];
x+=w*2; w*=2/3; h*=2/3;
this.ZoomButton(g,x,y ,w,h,"+").z=[1,1.1];
this.ZoomButton(g,x,y+2*h,w,h,"-").z=[1,1/1.1];
x-=w; y+=h;
this.ZoomButton(g,x+2*w,y,w,h,"+").z=[1.1,1];
this.ZoomButton(g,x ,y,w,h,"-").z=[1/1.1,1];
// Create range button
x+=3.5*w; y-=.5*h; w*=2; h*=2;
var button=g.Group();
button.svg=g;
button.cont=gcont;
button.plot=this;
button.onmousedown=chooseRange;
button.setAttribute("cursor","pointer");
button.Rect(x,y,w,h,"#eee","#ccc",1);
button.Line(x+w,y,x,y+h,"#777",1.);
var lineLength=Math.sqrt(w*w+h*h), lineAngle=Math.atan2(h,w);
var arrowLength = 0.2*lineLength, arrowAngle = 0.4;
var u0 = arrowLength*Math.cos(lineAngle-arrowAngle), v0 = arrowLength*Math.sin(lineAngle-arrowAngle);
var u1 = arrowLength*Math.cos(lineAngle+arrowAngle), v1 = arrowLength*Math.sin(lineAngle+arrowAngle);
button.Line(x+w,y,x+w-u0,y+v0,"#777",1.);
button.Line(x+w,y,x+w-u1,y+v1,"#777",1.);
button.Line(x,y+h,x+u0,y+h-v0,"#777",1.);
button.Line(x,y+h,x+u1,y+h-v1,"#777",1.);
// Create cursor
this.cursor = false;
this.cur = this.plot.Group();
this.curx = this.cur.Line(0,0,0,0,"#777",1,"1% 1%");
this.cury = this.cur.Line(0,0,0,0,"#777",1,"1% 1%");
this.cur.Rect(5,-34,60,30,"#eee","#ccc",1);
this.curt = [this.cur.Text(" ",7,-21,12,"#777","NC",0),
this.cur.Text(" ",7, -8,12,"#777","NC",0) ];
this.cur.setAttribute("visibility","hidden");
}
// resizer
Plot.prototype.resize = function(x1,x2,y1,y2) {
// Define, or reset plot limits
if(x1) this.x1=Math.min(this.x1,x1) || x1;
if(x2) this.x2=Math.max(this.x2,x2) || x2;
if(y1) this.y1=Math.min(this.y1,y1) || y1;
if(y2) this.y2=Math.max(this.y2,y2) || y2;
this.s=[this.w/(this.x2-this.x1), this.h/(this.y1-this.y2)]; // scalings
this.t=[-this.x1*this.s[0], -this.y2*this.s[1]]; // translations
this.plot.setAttribute("transform","translate("+this.t[0]+","+this.t[1]+")");
this.b=[this.w,this.h]; // box size
};
// Add a curve
Plot.prototype.addCurve = function(f,color) {
this.curve .push( this.plot.Path ("" ,color,2) );
this.quickCurve.push( this.plot.Polyline([0,0],color,2) );
this.curveEq .push( f );
this.ncurves += 1;
this.plot.appendChild(this.cur); // put cursor on top
};
// replotting manager after scrolling/zooming
Plot.prototype.update = function() {
var z,r,i,j,t,s,n,z1,z2,k,kk,zz,al,p=0,q=0;
for(k=0; k<this.ncurves; k+=1) {this.curve[k].setAttribute("d",""); }
this.x1=-this.t[0]/this.s[0]; this.x2=this.b[0]/this.s[0]+this.x1;
this.y2=-this.t[1]/this.s[1]; this.y1=this.b[1]/this.s[1]+this.y2;
this.m[0]=2*this.x1-this.x2; this.m[1]=2*this.y1-this.y2;
this.M[0]=2*this.x2-this.x1; this.M[1]=2*this.y2-this.y1;
// Update axes
this.ax.sets({"x1":""+this.m[0]*this.s[0], "x2":""+this.M[0]*this.s[0]});
this.ay.sets({"y1":""+this.m[1]*this.s[1], "y2":""+this.M[1]*this.s[1]});
this.curx.sets({"x1":""+this.m[0]*this.s[0], "x2":""+this.M[0]*this.s[0]});
this.cury.sets({"y1":""+this.m[1]*this.s[1], "y2":""+this.M[1]*this.s[1]});
// Update ticks and labels
al=['NC','MW']; zz=[1,1]; // tick label alignment
if(this.b[1]<2*this.t[1]) { al[0]='SC'; zz[1]=-1; } // reverse tick alignment if out of box
if(this.b[0]<2*this.t[0]) { al[1]='ME'; zz[0]=-1; } // depending on axes position
for(k=0; k<2; k+=1) { // x then y
kk=(k+1)%2;
z1=[ (this.x2-this.x1)*0.005,-(this.y2-this.y1)*0.01]; // small offsets for ticks
z2=[-(this.x2-this.x1)*0.005, (this.y2-this.y1)*0.01];
t = this.ticks[k].childNodes; l=this.tickL[k].childNodes; // get all ticks
p=(this.M[k]-this.m[k])/this.nt[k]; r=Math.pow(10.,ndec(p));
q=Math.floor(Math.sqrt(1.5*(p/r-1))); q=(1+q*q)*r; // now q is tick separation
p=Math.max(ndec(this.m[k]),ndec(this.M[k]))-1; r=Math.pow(10.,p); // p is the power
this.ticks[k].p=p; this.ticks[k].q=q; this.ticks[k].r=r;
i=0;
for(z=this.m[k]-this.m[k]%q;z<this.M[k];z+=q) { // for each tick position
z1[k]=z; z2[k]=z;
t[i].sets({"x1":""+z1[0]*this.s[0],"x2":""+z2[0]*this.s[0],"y1":""+z1[1]*this.s[1],"y2":""+z2[1]*this.s[1],
"stroke-width":z?1:0});
s=""; n="";
if(p*p>9) { // if too long number, display with power of 10
s=tickformat(z/r,q/r);
switch(s) {
case "0": s=""; break;
case "1": s="10"; n=""+p; break;
case "-1": s="-10"; n=""+p; break;
default: s+="·10";""; n=""+p;
}
} else {
s=tickformat(z,q);
if(s=="0") { s=""; }
}
l[i].childNodes[0].textContent=s;
l[i].childNodes[1].textContent=n;
l[i].move((kk+2*zz[0]*k)*z1[0]*this.s[0],-(-zz[1]*2.5*kk-k)*z1[1]*this.s[1],al[k]);
i+=1;
}
for(j=i; j<2*this.nt[k]; j+=1) { //hide unused ticks
t[j].setAttribute("stroke-width",0);
l[j].childNodes[0].textContent=""; l[j].childNodes[1].textContent="";
}
}
};
// Replotting while scrolling/zooming
Plot.prototype.quickUpdate = function() {
this.update();
// Update curve with some points
var l=(this.M[0]-this.m[0])/this.np;
var i,y,k;
for(k=0; k<this.ncurves; k+=1) {
i=0;
for(var x=this.m[0]; x<this.M[0]; x+=l) {
y=this.curveEq[k](x);
if(isFinite(y)) {
this.points[i*2]=x*this.s[0];
this.points[i*2+1]=Math.min(Math.max(y,this.m[1]),this.M[1])*this.s[1];
i+=1;
}
}
this.quickCurve[k].setAttribute("points",this.points.slice(0,2*i));
}
};
// Replotting AFTER scrolling/zooming
Plot.prototype.fullUpdate = function() {
this.update();
// Update curve with as many points as necessary
this.maxdepth=5; // depth for adaptative finding of min/max
var l=(this.M[0]-this.m[0])/this.np; // step in x
var x,x0,x1,y,y0,y1,t,r,discont,dx,dy,k,i;
for(k=0; k<this.ncurves; k+=1) {
this.d="M "+(this.m[0]*this.s[0])+" 0 "; // start svg path
this.pathState="M";
for(x=this.m[0]; x<this.M[0]; x+=l) { // loop over x
y=this.curveEq[k](x);
this.addMorePoints(x,y); // add point to svg path
x0=x; x1=x+l; y0=y; y1=this.curveEq[k](x1); // calculate next point
if(y1<y0) { t=x0; x0=x1; x1=t; t=y0; y0=y1; y1=t; } // sort the two points (min then max)
r=this.adaptativeMinMax(x,x+l,x0,y0,x1,y1,0,k); // find min and max of function between them
if(r[1]<y0) { this.addMorePoints(r[0],r[1]); } // add min if necessary
if(r[1]<y0 || r[3]>y1) { // if min or max encountered, check whether discontinuity.
if(Math.abs(r[2]-r[0])>1e-6*Math.max(Math.abs(r[2]),Math.abs(r[0]))) { // if numerical precision is ok
dx=(r[2]-r[0])*0.1; dy=Math.abs(r[3]-r[1])*0.3; t=0.5*(r[3]+r[1]);
for(i=0; i<10; i+=1) { // search for intermediate points
y=this.curveEq[k](r[0]+i*dx);
if(Math.abs(y-t)<dy) { break; } // if intermediate point, then not discontinuous
}
if(k>8) { this.pathState=""; } // discontinuity
}
}
if(r[3]>y1) { this.addMorePoints(r[2],r[3]); } // add max if necessary
}
this.quickCurve[k].setAttribute("points",[0,0]);
this.curve[k].setAttribute("d",this.d);
}
// Update global bounds
graph_bounds[this.igraph] = [this.x1, this.x2, this.y1, this.y2];
};
Plot.prototype.addMorePoints = function(x,y) {
if(isFinite(y)) {
if(this.pathState=="") { this.d+="M "; this.pathState="M"; }
else if(this.pathState=="M") { this.d+="L "; this.pathState="L"; }
this.d+=(x*this.s[0])+" "+(Math.min(Math.max(y,this.m[1]),this.M[1])*this.s[1])+" ";
} else {
this.pathState="";
}
};
// returns approximate min and max points between x0 and x1, in the form [xm,ym,xM,yM]
Plot.prototype.adaptativeMinMax = function(x0,x1,xm,ym,xM,yM,depth,k) {
if(depth>=this.maxdepth) {return [xm,ym,xM,yM];}
this.yy=this.curveEq[k]((x0+x1)*0.5);
if(this.yy>yM) { xM=(x0+x1)*0.5; yM=this.yy; }
else if(this.yy<ym) { xm=(x0+x1)*0.5; ym=this.yy; }
else if(depth>1) { return [xm,ym,xM,yM]; }
var r1=this.adaptativeMinMax(x0,(x0+x1)*0.5,xm,ym,xM,yM,depth+1,k);
var r2=this.adaptativeMinMax((x0+x1)*0.5,x1,xm,ym,xM,yM,depth+1,k);
if(r2[1]<r1[1]) { r1[0]=r2[0]; r1[1]=r2[1]; }
if(r2[3]>r1[3]) { r1[2]=r2[2]; r1[3]=r2[3]; }
return r1;
};
// Create one zoom button
Plot.prototype.ZoomButton = function(g,x,y,w,h,type) {
var zoom=g.Group();
zoom.Rect(x,y,w,h,"#eee","#ccc",1);
zoom.plot=this;
zoom.onmousedown=zoomBegin;
zoom.setAttribute("cursor","pointer");
if(type=="+") {zoom.Line(x+w/2,y+h/5,x+w/2,y+4*h/5,"#777",1.5);}
zoom.Line(x+w/5,y+h/2,x+4*w/5,y+h/2,"#777",1.5);
return zoom;
};
// Toggle visibility of cursor
Plot.prototype.toggleCursor = function() {
this.cursor = !this.cursor;
if( this.cursor ) {
this.rect.addEventListener("mousemove", moveCursor, false);
this.cur.setAttribute("visibility","visible");
} else {
this.rect.removeEventListener("mousemove", moveCursor, false);
this.cur.setAttribute("visibility","hidden");
}
};
// Display menu for changing plot range
function chooseRange(evt) {
var gbutton = evt.target.parentNode
var plot=gbutton.plot;
var cont=gbutton.cont;
var svg =gbutton.svg;
var w = svg.getAttribute("width"), h = svg.getAttribute("height");
// Create a gray overlay
var rect=svg.Rect(0,0,w,h,"rgba(0,0,0,0.2)","transparent",0);
// Create the menu
var menu=document.createElement("div");
menu.className = "rangeMenu";
// Create each item of the plot-range menu
function createTextItem(menu, title, value) {
var item = document.createElement("div");
item.className = "menuItem";
var name = document.createElement("div");
name.innerHTML = title;
item.appendChild(name);
var input = document.createElement("input");
input.type = "text";
input.value = value;
input.id = title;
item.appendChild(input);
menu.appendChild(item);
return input;
}
plot.exmin = createTextItem(menu, "x-min", ""+plot.x1);
plot.exmax = createTextItem(menu, "x-max", ""+plot.x2);
plot.eymin = createTextItem(menu, "y-min", ""+plot.y1);
plot.eymax = createTextItem(menu, "y-max", ""+plot.y2);
cont.appendChild(menu);
// Make overlay clickable to quit the menu
rect.plot=plot;
rect.menu=menu;
rect.onclick=setRange;
}
// Change the plot range and remove menu
function setRange(evt) {
var plot = evt.target.plot;
// Apply the range
var x1=plot.exmin.value;
var x2=plot.exmax.value;
var y1=plot.eymin.value;
var y2=plot.eymax.value;
plot.s[0] = plot.b[0] / (x2-x1);
plot.s[1] = plot.b[1] / (y1-y2);
plot.t[0] = -plot.s[0]*x1;
plot.t[1] = -plot.s[1]*y2;
plot.plot.setAttribute("transform","translate("+plot.t[0]+","+plot.t[1]+")");
plot.fullUpdate();
// remove the menu
evt.target.menu.parentNode.removeChild(evt.target.menu);
// remove the gray overlay
evt.target.parentNode.removeChild(evt.target);
};
// Display cursor when click
function moveCursor(evt) {
var plot=evt.target.plot;
var t=plot.plot.getAttribute("transform").match(/-?[0-9.]+[eE\+\-0-9]*/g);
var x = [evt.offsetX-t[0], evt.offsetY-t[1]], a, s, n, s1;
plot.cur.setAttribute("transform","translate("+x[0]+","+x[1]+")");
for( var i=0; i<2; i+=1 ) {
a = x[i]/plot.s[i];
s = parseFloat(a.toExponential(5)).toFixed(5);
s1 = parseFloat(s);
if( s.length>8 || s1==0 || parseFloat(s1.toFixed(3))==0 ) {
n=5;
do { n--; s = a.toExponential(n); } while( n>0 && s.length>10 )
}
plot.curt[i].textContent = s;
}
}
// Make SVG plots movable
var moving=undefined, movu, movx, mov;
function moveBegin(evt) {
evt.preventDefault(); // prevent the default svg dragging
document.onmouseup=moveEnd;
moving = evt.target.plot;
moving.c10=evt.clientX; moving.c20=evt.clientY; // initial coordinates
moving.quickUpdate();
evt.target.addEventListener("mousemove", moveContinue, false);
}
function moveContinue(evt) {
if(!moving) return;
movx = [evt.clientX-moving.c10+moving.t[0], evt.clientY-moving.c20+moving.t[1]];
// move the plot
moving.plot.setAttribute("transform","translate("+movx[0]+","+movx[1]+")");
// make sure that the ticks don't move further than the plot box
movu = 2*movx[0]/moving.b[0]-1; movu=movu*(Math.min(1,1/Math.abs(movu))-1)*moving.b[0]/2;
moving.ticks[1].setAttribute("transform","translate("+movu+",0)");
moving.tickL[1].setAttribute("transform","translate("+movu+",0)");
movu = 2*movx[1]/moving.b[1]-1; movu=movu*(Math.min(1,1/Math.abs(movu))-1)*moving.b[1]/2;
moving.ticks[0].setAttribute("transform","translate(0,"+movu+")");
moving.tickL[0].setAttribute("transform","translate(0,"+movu+")");
}
function moveEnd(evt) {
document.onmouseup=null;
evt.target.removeEventListener("mousemove", moveContinue);
if(!moving) return;
mov=moving; moving=undefined;
var t=mov.plot.getAttribute("transform").match(/-?[0-9.]+[eE\+\-0-9]*/g);
if( mov.t[0]==parseFloat(t[0]) && mov.t[1]==parseFloat(t[1]) ) {
mov.toggleCursor();
moveCursor(evt);
} else {
mov.t[0]=parseFloat(t[0]); mov.t[1]=parseFloat(t[1]);
}
mov.fullUpdate();
mov=undefined;
}
// Make SVG plots zoomable
var zooming=undefined, zoomtimer=undefined, zbutton=undefined, zoom;
function zoomBegin(evt) {
evt.preventDefault(); // prevent the default svg dragging
document.onmouseup=zoomEnd;
zbutton=evt.target.parentNode;
zooming=zbutton.plot;
zbutton.firstChild.setAttribute("fill","#ddd");
setTimeout(zoomContinue, 50);
}
function zoomContinue() {
if(!zooming) return;
zooming.s[0]*=zbutton.z[0];
zooming.s[1]*=zbutton.z[1];
zooming.t[0]=zooming.b[0]/2-zbutton.z[0]*(zooming.b[0]/2-zooming.t[0]);
zooming.t[1]=zooming.b[1]/2-zbutton.z[1]*(zooming.b[1]/2-zooming.t[1]);
zooming.plot.setAttribute("transform","translate("+zooming.t[0]+","+zooming.t[1]+")");
zooming.quickUpdate();
setTimeout(zoomContinue, 50);
}
function zoomEnd(evt) {
//if(zoomtimer) {clearInterval(zoomtimer);}
document.onmouseup=null;
if(!zooming) return;
zoom=zooming; zooming=undefined;
zbutton.firstChild.setAttribute("fill","#eee");
zoom.fullUpdate();
zoom=undefined; zplot=undefined;
}
// number of decimal places
function ndec(num) {return Math.floor(Math.log(Math.abs(num))/Math.log(10)); }
// Function for testing equal arrays (must not have subarrays)
function arrayequal(a,b) {
var na=a.length, nb=b.length;
if(na!=nb) {return false;}
for(var i=0;i<na;i+=1) {if(a[i]!=b[i]){return false;}}
return true;
}
// Linear spaced array
Array.prototype.linspace = function(xmin,xmax) {
var dx=(xmax-xmin)/(this.length-1.);
for(var k=0;k<this.length-1;k+=1) { this[k]=xmin+dx*k; }
this[this.length-1]=xmax;
}
// Shrink array towards 0
Array.prototype.shrinkTowards0 = function(coeff) {
if(this[0]*this[this.length-1]<0) {
for(var k=0;k<this.length;k+=1) { this[k]*=coeff; }
} else if(this[0]>=0) {
for(var k=1;k<this.length;k+=1) { this[k]=this[0]+(this[k]-this[0])*coeff; }
} else {
for(var k=0;k<this.length-2;k+=1) { this[k]=this[this.length-1]+(this[k]-this[this.length-1])*coeff; }
}
}
// Adjust xmin and xmax near 0 to make it nice
function adjustWith0(xmin,xmax,undetermined) {
var xm=xmin, xM=xmax;
if(undetermined) {
var xmean=(xmin+xmax)*0.5, defaultx=4;
if(xm*xM>0 && Math.abs(xmean)<defaultx/5 && xM-xm<Math.abs(xmean)/10) {
xm=Math.min(0, 2*xmean);
xM=Math.max(0, 2*xmean);
} else {
xm=xmean-defaultx;
xM=xmean+defaultx;
}
}
if(Math.abs(xM+xm)<xM*0.5) { xm=-xM; }
var dx=(xM-xm)*0.5;
if( (xm>0 && xm<dx) || (xM<0 && xM>-dx)) { xm-=dx; xM+=dx; }
return [xm,xM];
}
// Function to make tick labels nice
function tickformat(x,q) {
var l,z,zm=x+"",m=zm.length;
for(var y=0;y<=q;y+=q) {
z=(x+y).toExponential().split("e");
l = z[0].length-1;
if(l>14) { z[0]=eval(z[0].substring(0,l)).toFixed(10); }
z = (eval(z.join("e"))-y)+"";
if(z.length<m) {m=z.length; zm=z;}
}
return zm;
}
-->
</script>
<script language="javascript">
<!--
// constants
var co=[], re_co=[];
co.push('299792458. m*s^(-1)'); re_co.push(/(\W)c(?!\w)/g); // c
co.push('6.67384e-11 m^3*kg^(-1)*s^(-2)'); re_co.push(/(\W)G(?!\w)/g); // G
co.push('6.62606957e-34 J*s'); re_co.push(/(\W)h(?!\w)/g); // h
co.push('1.256637061e-6 N*A^(-2)'); re_co.push(/(\W)mu_?0(?!\w)/g); // mu0
co.push('8.854187817e-12 F*m^(-1)'); re_co.push(/(\W)e(?:ps)?_?0(?!\w)/g); // eps0
co.push('1.602176565e-19 C'); re_co.push(/([^\w\.])(?:q_?)?e(?!\w)/g); // q_e
co.push('5.2917721092e-11 m'); re_co.push(/(\W)a_?0(?!\w)/g); // a_0
co.push('2.8179403267e-15 m'); re_co.push(/(\W)r_?e(?!\w)/g); // r_e
co.push('9.10938215e-31 kg'); re_co.push(/(\W)m_?e(?!\w)/g); // m_e
co.push('1.672621777e-27 kg'); re_co.push(/(\W)m_?p(?!\w)/g); // m_p
co.push('6.02214129e23 mol^(-1)'); re_co.push(/(\W)N_?A(?!\w)/g); // N_A
co.push('1.3806488e-23 J*K^(-1)'); re_co.push(/(\W)k_?B(?!\w)/g); // k_B
co.push('3.141592653589793'); re_co.push(/(\W)pi(?!\w)/gi); // pi
co.push('7.2973525698e-3'); re_co.push(/(\W)alpha(?!\w)/g); // alpha
// SI units or similar
var un=["m","kg","s","A","K","mol","cd","rad"];
var re_u1=[], re_u2=[], rf=[], ru=[];
re_u1.push(/[Mm]eters?$/);re_u2.push(/m$/); ru.push([1,0,0,0,0,0,0,0]); rf.push(1);
re_u1.push(/[Gg]rams?$/); re_u2.push(/g$/); ru.push([0,1,0,0,0,0,0,0]); rf.push(1e-3);
re_u1.push(/[Ss]econds?$/); re_u2.push(/s$/); ru.push([0,0,1,0,0,0,0,0]); rf.push(1);
re_u1.push(/[Aa]mperes?$/); re_u2.push(/A$/); ru.push([0,0,0,1,0,0,0,0]); rf.push(1);
re_u1.push(/[Kk]elvins?$/); re_u2.push(/K$/); ru.push([0,0,0,0,1,0,0,0]); rf.push(1);
re_u1.push(/[Mm]oles?$/); re_u2.push(/mol$/); ru.push([0,0,0,0,0,1,0,0]); rf.push(1);
re_u1.push(/[Cc]andelas?$/); re_u2.push(/cd$/); ru.push([0,0,0,0,0,0,1,0]); rf.push(1);
re_u1.push(/[Rr]adians?$/); re_u2.push(/rad$/); ru.push([0,0,0,0,0,0,0,1]); rf.push(1);
re_u1.push(/[Hh]ertz$/); re_u2.push(/Hz$/);ru.push([0,0,-1,0,0,0,0,0]); rf.push(1);
re_u1.push(/[Nn]ewtons?$/); re_u2.push(/N$/);ru.push([1,1,-2,0,0,0,0,0]); rf.push(1);
re_u1.push(/[Pp]ascals?$/);re_u2.push(/Pa$/);ru.push([-1,1,-2,0,0,0,0,0]); rf.push(1);
re_u1.push(/[Jj]oules?$/); re_u2.push(/J$/);ru.push([2,1,-2,0,0,0,0,0]); rf.push(1);
re_u1.push(/[Ww]atts?$/); re_u2.push(/W$/);ru.push([2,1,-3,0,0,0,0,0]); rf.push(1);
re_u1.push(/[Cc]oulombs?$/);re_u2.push(/C$/); ru.push([0,0,1,1,0,0,0,0]); rf.push(1);
re_u1.push(/[Vv]olts?$/); re_u2.push(/V$/);ru.push([2,1,-3,-1,0,0,0,0]); rf.push(1);
re_u1.push(/[Ff]arads?$/); re_u2.push(/F$/);ru.push([-2,-1,4,2,0,0,0,0]); rf.push(1);
re_u1.push(/[Oo]hms?$/); re_u2.push(/ohm$/);ru.push([2,1,-3,-2,0,0,0,0]); rf.push(1);
re_u1.push(/[Ss]iemens$/); re_u2.push(/S$/);ru.push([-2,-1,3,2,0,0,0,0]); rf.push(1);
re_u1.push(/[Ww]ebers?$/); re_u2.push(/Wb$/);ru.push([2,1,-2,-1,0,0,0,0]); rf.push(1);
re_u1.push(/[Tt]eslas?$/); re_u2.push(/T$/);ru.push([0,1,-2,-1,0,0,0,0]); rf.push(1);
re_u1.push(/[Gg]auss$/); re_u2.push(/ /);ru.push([0,1,-2,-1,0,0,0,0]); rf.push(1e-4);
re_u1.push(/[Hh]enrys?$/); re_u2.push(/H$/);ru.push([2,1,-2,-2,0,0,0,0]); rf.push(1);
re_u1.push(/[Ll]itres?$/);re_u2.push(/[lL]$/);ru.push([3,0,0,0,0,0,0,0]); rf.push(1e-3);
re_u1.push(/[Tt]onnes?$/); re_u2.push(/ /); ru.push([0,1,0,0,0,0,0,0]); rf.push(1e3);
re_u1.push(/[Ee]lectron[Vv]olts?$/);re_u2.push(/eV$/);ru.push([2,1,-2,0,0,0,0,0]); rf.push(1.602176565e-19);
re_u1.push(/bars?$/); re_u2.push(/bar$/);ru.push([-1,1,-2,0,0,0,0,0]); rf.push(1e5);
var nu=ru.length;
// SI prefixes
var re_pr1=[], re_pr2=[], pr=[];
re_pr1.push(/^[Dd]eca$/); re_pr2.push(/^da$/); pr.push(1e1);
re_pr1.push(/^[Hh]ecto$/); re_pr2.push(/^h$/); pr.push(1e2);
re_pr1.push(/^[Kk]ilo$/); re_pr2.push(/^k$/); pr.push(1e3);
re_pr1.push(/^[Mm]ega$/); re_pr2.push(/^M$/); pr.push(1e6);
re_pr1.push(/^[Gg]iga$/); re_pr2.push(/^G$/); pr.push(1e9);
re_pr1.push(/^[Tt]era$/); re_pr2.push(/^T$/); pr.push(1e12);
re_pr1.push(/^[Pp]eta$/); re_pr2.push(/^P$/); pr.push(1e15);
re_pr1.push(/^[Ee]xa$/); re_pr2.push(/^E$/); pr.push(1e18);
re_pr1.push(/^[Zz]etta$/); re_pr2.push(/^Z$/); pr.push(1e21);
re_pr1.push(/^[Yy]otta$/); re_pr2.push(/^Y$/); pr.push(1e24);
re_pr1.push(/^[Dd]eci$/); re_pr2.push(/^d$/); pr.push(1e-1);
re_pr1.push(/^[Cc]enti$/); re_pr2.push(/^c$/); pr.push(1e-2);
re_pr1.push(/^[Mm]illi$/); re_pr2.push(/^m$/); pr.push(1e-3);
re_pr1.push(/^[Mm]icro$/); re_pr2.push(/^[µu]$/); pr.push(1e-6);
re_pr1.push(/^[Nn]ano$/); re_pr2.push(/^n$/); pr.push(1e-9);
re_pr1.push(/^[Pp]ico$/); re_pr2.push(/^p$/); pr.push(1e-12);
re_pr1.push(/^[Ff]emto$/); re_pr2.push(/^f$/); pr.push(1e-15);
re_pr1.push(/^[Aa]tto$/); re_pr2.push(/^a$/); pr.push(1e-18);
re_pr1.push(/^[Zz]epto$/); re_pr2.push(/^z$/); pr.push(1e-21);
re_pr1.push(/^[Yy]octo$/); re_pr2.push(/^y$/); pr.push(1e-24);
var npr=pr.length;
// Other units
re_u1.push(/^[Hh]ectares?$|^ha$/); ru.push([2,0,0,0,0,0,0,0]); rf.push(1e4);
re_u1.push(/^[Aa]res?$/); ru.push([2,0,0,0,0,0,0,0]); rf.push(100);
re_u1.push(/^[Dd]egrees?$|^deg$/); ru.push([0,0,0,0,0,0,0,1]); rf.push(0.017453292519943295);
re_u1.push(/^[Mm]inutes?$|^min$/); ru.push([0,0,1,0,0,0,0,0]); rf.push(60);
re_u1.push(/^[Hh]ours?$/); ru.push([0,0,1,0,0,0,0,0]); rf.push(3600);
re_u1.push(/^[Dd]ays?$/); ru.push([0,0,1,0,0,0,0,0]); rf.push(86400);
re_u1.push(/^[Aa]ngstroms?$/); ru.push([1,0,0,0,0,0,0,0]); rf.push(1e-10);
re_u1.push(/^amu$/); ru.push([0,1,0,0,0,0,0,0]); rf.push(1.66053886e-27);
re_u1.push(/^[Ii]nch(?:es)?$$/); ru.push([1,0,0,0,0,0,0,0]); rf.push(0.0254);
re_u1.push(/^[Ff]oot$|^[Ff]eet$/); ru.push([1,0,0,0,0,0,0,0]); rf.push(0.3048);
re_u1.push(/^[Yy]ards?$/); ru.push([1,0,0,0,0,0,0,0]); rf.push(0.9144);
re_u1.push(/^[Mm]iles?$|^mi$/); ru.push([1,0,0,0,0,0,0,0]); rf.push(1609.344);
re_u1.push(/^[Aa]cres?$/); ru.push([2,0,0,0,0,0,0,0]); rf.push(4046.8564224);
re_u1.push(/barns?$/); ru.push([2,0,0,0,0,0,0,0]); rf.push(1e-28);
re_u1.push(/^[Pp]arsecs?$/); ru.push([1,0,0,0,0,0,0,0]); rf.push(3.085677581491e+16);
re_u1.push(/^[Ll]ight-?[Yy]ears?$|^ly$/); ru.push([1,0,0,0,0,0,0,0]); rf.push(9460730472580800);
re_u1.push(/^[Ee]rgs?$/); ru.push([2,1,-2,0,0,0,0,0]); rf.push(1e-7);
re_u1.push(/^atmospheres?$|^atm$/); ru.push([-1,1,-2,0,0,0,0,0]); rf.push(101325.);
var nu1=ru.length;
// other regular expressions
var re_err=/[ a-zA-Z0-9=+\-^*_\/().,\n]/g, re_in=/ in /, re_eq=/(.+)=/,
re_ep =/[Ee]\+?(?=[0-9])/g, re_em=/[Ee]-(?=[0-9])/g,
re_ep1=/#/g, re_em1=/@/g,
re_pm=/([^\^])-/g, re_s=/\s/g, re_ss=/\(\+/g, re_par=/^\s*\((.+)\)\s*$/g,
re_unit1=/([0-9\).])\s*([A-Za-z]+)/g,
re_unit2=/[A-Za-z]/, re_unit3=/([A-Za-z]+)/g;
-->
</script>
</head>
<body>
<div id="cont">
<div>
<div style="height:36px">
<svg width="100" height="36"><g transform="translate(50,18)">
<g fill="#fafafa" stroke="none">
<rect width="20" height="4" x="20" y="-12"/><circle cx="30" cy="-2" r="4"/><circle cx="30" cy="-18" r="4"/>
<rect width="20" height="4" x="-40" y="8"/><circle cx="-30" cy="2" r="4"/><circle cx="-30" cy="18" r="4"/>
</g>
<g fill="#f2f2f2" stroke="none">
<rect width="20" height="4" x="-20" y="-12"/><circle cx="-10" cy="-2" r="4"/><circle cx="-10" cy="-18" r="4"/>
<rect width="20" height="4" x="0" y="8"/><circle cx="10" cy="2" r="4"/><circle cx="10" cy="18" r="4"/>
</g>
<g fill="#666666" stroke="none">
<rect width="10" height="2" x="-10" y="-10"/>
<rect width="10" height="2" x="0" y="8"/>
<path d="M-10 2 A 4 4 0 1 0 -10 -6 Z"/>
<path d="M10 -2 A 4 4 0 1 0 10 6 Z"/>
</g>
</g></svg>
</div>
<div id="incont">
<div id="incont2">
<div id="inbg"></div>
<div id="hintbox">⇧ + ↵</div>
<textarea wrap="off" id="in" rows="1" oninput="inputEvent()" onkeydown="typeKey(event);" onkeyup="releaseKey();">me*c^2 in keV</textarea>
<textarea wrap="off" id="fake1" rows="1"></textarea>
<textarea wrap="off" id="fake2" rows="2"></textarea>
</div>
<div id="button">
<svg width="4em" height="2.8em" viewBox="-1 0 2 1.4">
<g fill="#f2f2f2" stroke="none">
<path d="M -.7 0 L 0 1.4 L .7 0"/>
</g>
<g fill="#ddd" stroke="none">