forked from aantthony/graph.tk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
1744 lines (1588 loc) · 65.9 KB
/
main.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
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
//jquery and mathquill must be available first before including this script.
/*
Copyright © Anthony 2010
http://graph.tk/
nospamant[/at/ ]gmail[ /dot/ ]com
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
var version="GIT_VERSION"; //filled in by ./compile.sh
var g = [];
//E is the function that returns dy/dx for the differential equation
var E = function (x, y, dx, dy) {
return g[0](x);
};
var ready = false;
var drag = false;//Are you dragging the graph
//Physical Constants
var c = 299792458;
var G = 6.67300e-11;
var m_e = 5.9742e24;
var m_m = 7.36e22;
var m_s = 1.98892e30;
var R_E = 6378100;
var r_e = 6378100;
var h = 6.626068e-34;
var log2pi = 1.8378770664093453;
var e = Math.E;
var pi = Math.PI;
var phi = (1 + Math.sqrt(5)) / 2;
var epsilon_0 = 8.85418782e-12;
var en = ["Massless void", "Hydrogen", "Helium", "Lithium", "Beryllium", "Boron", "Carbon", "Nitrogen", "Oxygen", "Fluorine", "Neon", "Sodium", "Magnesium", "aluminium", "Silicon", "Phosphorus", "Sulphur", "Chlorine", "Argon", "Potassium", "Calcium", "Scandium", "Titanium", "Vanadium", "Chromium", "Manganese", "Iron", "Cobalt", "Nickel", "Copper", "Zinc", "Gallium", "Germanium", "Arsenic", "Selenium", "Bromine", "Krypton", "Rubidium", "Strontium", "Yttrium", "Zirkonium", "Niobium", "Molybdaenum", "Technetium", "Ruthenium", "Rhodium", "Palladium", "Silver", "Cadmium", "Indium", "Tin", "Antimony", "Tellurium", "Iodine", "Xenon", "Cesium", "Barium", "Lanthanum", "Cerium", "Praseodymium", "Neodymium", "Promethium", "Samarium", "Europium", "Gadolinium", "Terbium", "Dysprosium", "Holmium", "Erbium", "Thulium", "Ytterbium", "Lutetium", "Hafnium", "Tantalum", "Tungsten", "Rhenium", "Osmium", "Iridium", "Platinum", "Gold", "Hydrargyrum", "Thallium", "Lead", "Bismuth", "Polonium", "Astatine", "Radon", "Francium", "Radium", "Actinium", "Thorium", "Protactinium", "Uranium", "Neptunium", "Plutonium", "Americium", "Curium", "Berkelium", "Californium", "Einsteinium", "Fermium", "Mendelevium", "Nobelium", "Lawrencium", "Rutherfordium", "Dubnium", "Seaborgium", "Bohrium", "Hassium", "Meitnerium", "Ununnilium", "Unununium"];
var M = [0.0, 1.00794, 4.002602, 6.941, 9.012182, 10.811, 12.0107, 14.0067, 15.9994, 18.9994, 20.1797, 22.98976928, 24.305, 26.9815386, 28.0855, 30.973762, 32.065, 35.453, 39.948, 39.0983, 40.078, 44.955912, 47.867, 50.9415, 51.9961, 54.938045, 55.845, 58.933195, 58.6934, 63.546, 65.38, 69.723, 72.64, 74.9216, 78.96, 79.904, 83.798, 85.4678, 87.62, 88.90585, 91.224, 92.90638, 95.96, 98, 101.07, 102.9055, 106.42, 107.8682, 112.411, 114.818, 118.71, 121.76, 127.6, 126.90447, 131.293, 132.9054519, 137.327, 138.90547, 140.116, 140.90765, 144.242, 145, 150.36, 151.964, 157.25, 158.92535, 162.5001, 164.93032, 167.259, 168.93421, 173.054, 174.9668, 178.49, 180.94788, 183.84, 186.207, 190.23, 192.217, 192.084, 196.966569, 200.59, 204.3833, 207.2, 208.980401, 210, 210, 220, 223, 226, 227, 232.03806, 231.03588, 238.02891, 237, 244, 243, 247, 247, 251, 252, 257, 258, 259, 262, 261, 262, 266, 264, 277, 268, 271, 272];
var symbol = ["Zero", "H", "He", "Li", "Be", "B", "C", "N", "O", "F", "Ne", "Na", "Mg", "Al", "Si", "P", "S", "Cl", "Ar", "K", "Ca", "Sc", "Ti", "V", "Cr", "Mn", "Fe", "Co", "Ni", "Cu", "Zn", "Ga", "Ge", "As", "Se", "Br", "Kr", "Rb", "Sr", "Y", "Zr", "Nb", "Mo", "Te", "Ru", "Rh", "Pd", "Ag", "Cd", "In", "Sn", "Sb", "Te", "I", "Xe", "Cs", "Ba", "La", "Ce", "Pr", "Nd", "Pm", "Sm", "Eu", "Gd", "Tb", "Dy", "Ho", "Er", "Tm", "Yb", "Lu", "Hf", "Ta", "W", "Re", "Os", "Ir", "Pt", "Au", "Hg", "Tl", "Pb", "Bi", "Po", "At", "Rn", "Fr", "Ra", "Ac", "Th", "Pa", "U", "Np", "Pu", "Am", "Cm", "Bk", "Cf", "Es", "Fm", "Md", "No", "Lr", "Rf", "Db", "Sg", "Bh", "Hs", "Mt", "Ds", "Rg"];
//Make the periodic table global
for (var index = 0; index < symbol.length; index++) {
window[symbol[index]] = M[index];
}
//Basic math functions
var sin = Math.sin;
var cos = Math.cos;
var tan = Math.tan;
var tg = Math.tan;
var exp = Math.exp;
var log = Math.log;
var ln = Math.log;
var abs = Math.abs;
var acos = Math.acos;
var asin = Math.asin;
var atan = Math.atan;
var atan2 = Math.atan2;
var ceil = Math.ceil;
var floor = Math.floor;
var max = Math.max;
var min = Math.min;
var random = Math.random;
var round = Math.round;
var sqrt = Math.sqrt;
var pow = Math.pow;
//sin^n (x)
function sin_n(n,x){return pow(sin(x),n);}
function cos_n(n,x){return pow(cos(x),n);}
function tan_n(n,x){return pow(tan(x),n);}
function cot_n(n,x){return pow(cot(x),n);}
function sec_n(n,x){return pow(sec(x),n);}
function csc_n(n,x){return pow(csc(x),n);}
function log_n(n,x){return pow(log(x),n);}
function ln_n(n,x){return pow(ln(x),n);}
function logb(b, v) {
return ln(v) / ln(b);
}
function u(x) {
//unit step function
return (x>=0)?(x?1:0.5):(0);
}
function signum(x){
return 2*u(x)-1;
}
function piecewise(cond, val, other) {
if (cond) {
return val;
}
return other;
}
function sinc(x) {
if (x === 0) {
return 1;
}
return sin(pi * x) / (pi * x);
}
function sec(x) {return 1 / (cos(x));}
function csc(x) {return 1 / (sin(x));}
function cosec(x) {return 1 / (sin(x));}
function cot(x) {return 1 / (tan(x));}
var ctg = cot;
var ctn = cot;
//Not so basic math
//Bell numbers
var blln = [1, 1, 2, 5, 15, 52, 203, 877, 4140, 21147, 115975, 678570, 4213597, 27644437, 190899322, 1382958545, 10480142147, 82864869804, 682076806159, 5832742205057, 51724158235372, 474869816156751, 4506715738447323];
//Riemann zeta function
function zeta(x) {
if (x === 0) {
return -0.5;
} else if (x == 1) {
return Infinity;
} else if (x == 2) {
return pi * pi / 6;
} else if (x == 4) {
return pi * pi * pi * pi / 90;
} else if (x < 1) {
return Infinity;
}
var sum = 4.4 * pow(x, -5.1);
for (var npw = 1; npw < 10; npw++) {
sum += pow(npw, -x);
}
return sum;
}
function Gamma(x) {
if (x > 1.0) {
return (exp(x * (ln(x) - 1) + 0.5 * (-ln(x) + log2pi) + 1 / (12 * x) - 1 / (360 * (x * x * x)) + 1 / (1260 * pow(x, 5)) - 1 / (1680 * pow(x, 7))));
}
if (x > -0.5) {
return (1.0 + 0.150917639897307 * x + 0.24425221666910216 * pow(x, 2)) / (x + 0.7281333047988399 * pow(x, 2) - 0.3245138289924575 * pow(x, 3));
}
if (x < 0) {
if (x == ~~x) {
return;
} else {
return Math.PI / (Math.sin(Math.PI * x) * Gamma((1 - x)));
}
} else {
return pow(x - 1, x - 1) * Math.sqrt(2 * Math.PI * (x - 1)) * exp(1 - x + 1 / (12 * (x - 1) + 2 / (5 * (x - 1) + 53 / (42 * (x - 1)))));
}
}
function fact(ff) {
if (ff === 0 || ff == 1) {
return 1;
} else if (ff > 0 && ff == ~~ff && ff < 15) {
var s = 1;
for (var nns = 1; nns <= ff; nns++) {
s *= nns;
}
return~~s;
} else if (ff != (~~ff) || ff < 0) {
return Gamma(ff + 1);
}
}
function bellb(x) {
if (x == ~~x && x < blln.length) {
return blln[x];
} else {
var sum = 0;
for (var inj = 0; inj < 5; inj++) {
sum += pow(inj, x) / fact(inj);
}
return sum / e;
}
}
//Draw a dot instead of a line.
function pt(vx, vy) {
if (vy === undefined) {
return vx;
}
return {
"x": vx,
"y": vy
};
}
// 'lvl'th derivative of g[ia](x) when x = 'x'
function djkb(ia, lvl, x) {
difflevel++;
var res;
if (difflevel > 8) {
difflevel -= 1;
return 0;
}
var h = 0.0001;
if (lvl > 0) {
res = (djkb(ia, lvl - 1, x + h) - djkb(ia, lvl - 1, x - h)) / (2 * h);
difflevel -= 1;
return res;
}
res = g[ia](x);
difflevel -= 1;
return res;
}
//Varibles below this point will be privately encapsulated in the to be graph object.
//This way the user can have many variable names, without affecting any of the below code.
//But for debugging, it's easier to have direct access.
var difflevel = 0; //Used to prevent massive stacks in the recursive djkb()
var loaded = false;
var fofy = 0; //Current y in solving of diffeq
var showp = 0;//index for newtonian solver at the bottom left.
var ctx;//canvas context (2d)
var iphone = /iPhone/.test(window.navigator.userAgent);
var kinput="span";//span for mathquill, input for form input
if (window.parent.length) {
kinput="input";
}
if(iphone){kinput="input"}
//Visible region on screen
var boundleft = -10;
var boundright = 10;
var boundtop = 10;
var boundbottom = -10;
//for google analytics tracking of events
function _ga_track_event(n) {
if (window.pageTracker) {
setTimeout(function () {
pageTracker._trackEvent("Graph", n)
}, 20)
}
};
//begin plotting from an (x,y) point
function plotf(px, py) {
if (!isNaN(py)) {
if (py > boundtop + 2) {
ctx.moveTo(px, -boundtop - 2);
return;
} else if (py < boundbottom - 2) {
ctx.moveTo(px, -boundbottom + 2);
return;
}
ctx.moveTo(px, -py);
}
}
//plot an (x,y) point
function plot(px, py) {
if (!isNaN(py)) {
if (py > boundtop + 2) {
ctx.lineTo(px, -boundtop - 2);
return;
} else if (py < boundbottom - 2) {
ctx.lineTo(px, -boundbottom + 2);
return;
}
ctx.lineTo(px, -py);
}
}
var dotprod = "⋅";
if (/Windows/.test(navigator.userAgent) && /Safari/.test(navigator.userAgent)) {
//Safari leaves a massive gap on windows for the other one
dotprod = "∙";
}
function safeeval(z) {
//use safeeval instead of eval()
var naughty = "eval,document,window,location,cookie,alert,comfirm,prompt,this,parent,child,xml,xmlhttp,clip,draw,getfunction,get2dfunction,extrafunc,calcnextframe,nextframe,canvas".split(",");
for (var nau_g = 0; nau_g < naughty.length; nau_g++) {
if (z.indexOf(naughty[nau_g]) != -1) {
throw ("Unsafe Code: " + naughty[nau_g])
}
}
return eval(z);
}
var randfuncs = "x^2~f'(x)-1~2e^-x~2x+3~{λ:λ=3}~e^(-λ*x)~(0.5,0.5)~∑[1...∞,sin(nx)/n]~m:H_2SO_4~|x^2-4|+2~1/x~x^-2~x!~lnx~∑[1,infinity,(x^n)/n!]~sinx~e^x:[−2,2]~tan(x)~(x+2)(x-3)^2~diff(0,2,2x)~(x-2)^2~∑[1,∞,sin((2n−1)x)/(2n−1)]~~∏[1,5,(x-n)]~∑[0,5,n]~x^x~gamma(x)~(x!)/(3!-x)~x%3~(x>3)?2x:-3~fact(x)~phi/x~(x>=0)?m_e*G/(r_e+100000x)^2:undefined~g[0]'(2x)~g[0](x)+1~sqrt(x)".split("~");
if(kinput=="span"){
randfuncs = "x^2 f'\\left(x\\right)-1 2e^{-x} 2x+3 \\lambda=3 e^{-\\lambda*x} \\left(0.5,0.5\\right) \\sum_{n=1}^{\\infinity}\\frac{\\sin\\left(nx\\right)}n \\prod_{1}^{4}x-n m:H_2SO_4 \\left|x^2-4\\right|+2 \\frac1x x^{-2} x! \\ln x \\sum_{n=1}^{\\infinity}\\frac{x^n}{n} \\sin x e^x:\\left[−2,2\\right] \\tan\\left(x\\right) \\left(x+2\\right)\\left(x-3\\right)^2 diff\\left(0,2,2x\\right) \\left(x-2\\right)^2 \\sum_{n=1}^{\\infinity}\\frac{\\sin\\left(\\left(2n−1\\right)x\\right)}{2n−1} \\prod_{n=1}^5\\left(x-n\\right) \\sum_{n=0}^5n x^x \\Gamma\\left(x\\right) \\frac{x!}{3!-x} x%3 \\left(x>3\\right)?2x:-3 \\fact\\left(x\\right) \\frac\\phi x \\left(x>=0\\right)?m_e*G/\\left(r_e+100000x\\right)^2:undefined g\\left[0\\right]'\\left(2x\\right) g\\left[0\\right]\\left(x\\right)+1 \\sqrt x".split(" "); //four spaces
}
var randomfi = 0;
//Not actually random.
function randfunc() {
return randfuncs[(randomfi++) % randfuncs.length];
}
if (window.location.hash != "") {
//index.html#y=x^2+2
window.shouldload = false;
if (window.location.hash[2] == "=") {
window.jsonfunc = window.location.hash.substring(3);
} else {
window.jsondata = unescape(window.location.hash.substring(6));
}
}
var all = document.getElementById("all");
//It is much faster to cache the entire page in a javascript.
//It is also insane.
all.innerHTML = "<img style=\"display:none\" src=\"grabbing.gif\" height=\"16\" width=\"16\"><canvas id=\"canvas\" width=\"800\" height=\"600\"><div style=\"margin:16px;font-family:sans-serif\"><h1>Error</h1>Your internet browser is way too old for this!. <a href=\"about\">About this page.</a><br />Download google chrome, firefox or safari<br><br>Choose one of these browsers, or find another one.<br><br><a href=\"http://www.google.com/chrome/\"><img id=\"i_chrome\" alt=\"Google Chrome\"></a> <a href=\"http://www.google.com/chrome/\">Get Google Chrome</a><br><a href=\"http://www.mozilla.com/firefox\"><img id=\"i_firefox\" alt=\"Firefox\"></a> <a href=\"http://www.mozilla.com/firefox\">Get Firefox</a><br><a href=\"http://www.apple.com/safari\"><img id=\"i_safari\" alt=\"Safari\"></a> <a href=\"http://www.apple.com/safari\">Get Safari</a><br><a href=\"http://www.opera.com/download/\"><img align=\"middle\" id=\"i_opera\" alt=\"Opera\"></a> <a href=\"http://www.opera.com/download/\">Get Opera</a><br></div></canvas><div id=\"ptd\" style=\"position:fixed;z-Index:80000;color:black;opacity:0.6;left:8px;bottom:8px;border:none;background:white;-webkit-transition:opacity 0.5s ease-in-out;-moz-transition:opacity 0.5s ease-in-out;-o-transition:opacity 0.5s ease-in-out;transition: opacity 0.5s ease-in-out;font:10pt 'Menlo','Andale Mono','Consolas','DejaVu Sans Mono','Droid Sans Mono','Lucida Console','Monaco','monofur',monospace\">(0,0)</div><div id=\"con\" style=\"display:none\" class=\"overlay\"><div id=\"logt\"><div>graph.tk v1.2 (<a href=\"https://github.com/aantthony/graph.tk/commit/"+version+"\">"+version+"</a>) © 2010 Anthony<br /><br />License: <a href=\"http://www.gnu.org/licenses/lgpl.html\" title=\"GNU Lesser General Public License\" target=\"_blank\">GNU LGPL</a><br />Source code: <a href=\"http://github.com/aantthony/graph.tk\" target=\"_blank\">graph.tk on GitHub</a><br /><div style=\"font-family:sans-serif;font-size:x-small\">This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.<br /><br>Notes:<br>Sometimes x^n will not be x to the power of n, but x XOR n, which is the normal javascript meaning. I have made modifications that allow some easier equations. A dot after the expression means it was evaluated in a non-javascript-standard way. Use size() to change size.<br></div><br>Some cool things to type:<br> Fe, m:H_2O, g(0), g[1](0), en[26], M[26], symbol[26]</div></div><br><input type=\"text\" style=\"width:100%\" id=\"conin\" onkeydown=\"if(event.which==13){consoleex(this)};if(event.which==38){this.value=last}\"></div><div id=\"funcs\" class=\"overlay\"><ul class=\"f\" id=\"flist\"><li id=\"prototype\"><div class=\"b\" style=\"background:#07c\"></div><span class=\"matheditor\"></span><span class=\"delete\" onmouseup=\"delfunc(this);\"></span></li></ul><input type=\"button\" value=\"+\" id=\"pb\" onclick=\"newfunc();_ga_track_event('New')\"><a href=\"javascript:void(showcon())\" style=\"font-size:small;margin:8px\" id=\"shc\">Console</a><a href=\"javascript:void(tdiff())\" style=\"font-size:small;margin:8px\" id=\"sde\">Diff Eq</a><a href=\"javascript:void(scren())\" style=\"font-size:small;margin:8px\" id=\"tss\">Screenshot</a><small id=\"nosave\"></small><div style=\"float:right\"><a href=\"http://graph.tk/about\" target=\"_blank\"><input type=\"button\" value=\"Info\" onclick=\"ldocation='http://graph.tk/about'\"></a></div></div><div id=\"overlay\" style=\"display:none;bottom:0;right:0\" class=\"overlay\"><table><tbody><tr><td style=\"width:100px\"><input type=\"button\" value=\"f ' (x)\" onclick=\"if(this.value=='f \\' (x)'){this.value='f \\' \\' (x)';second=true;}else{this.value='f \\' (x)';second=false;}\"> = </td><td><input type=\"text\" id=\"nnn\" value=\"g(x)\" onchange=\"this.onkeydown()\" onkeydown=\"getg(this)\" onkeyup=\"this.onkeydown()\"></td></tr><tr><td>x<sub>0</sub> = </td><td><input type=\"text\" value=\"0\" id=\"x0\" onchange=\"this.onkeydown()\" onkeydown=\"valiad(this)\" onkeyup=\"this.onkeydown()\"></td></tr><tr><td>y<sub>0</sub> = </td><td><input type=\"text\" value=\"1\" id=\"y0\" onchange=\"this.onkeydown()\" onkeydown=\"valiad(this)\" onkeyup=\"this.onkeydown()\"></td></tr><tr><td> </td><td><input type=\"button\" value=\"Solve\" id=\"stopper\" onclick=\"if(solving){solving=false;}else{dosolve(document.getElementById('x0').value,document.getElementById('y0').value)}\"></td></tr></tbody></table></div>";
var ptd = document.getElementById("ptd");
var canvas = document.getElementById("canvas");
var stopper = document.getElementById("stopper");//The solve diffeq button.
var overlay = document.getElementById("overlay");//the diffeq overlay
var width, height, draw;
//Mouse coordinates
var mx = 400;
var my = 300;
//Last mouse coordinates
var lmx;
var lmy;
var scalex = 1;
var scaley = scalex; //this always holds
var panx = 0;
var pany = 0;
//initial conditions
var ix = 1;
var iy = 1;
//Current conditions
var sx = 1;
var sy = 1;
var dx = 0;
var dy = 0;
//Last conditions
var lx = 1;
var ly = 1;
//Location of canvas on screen. While dragging this changes.
var cx = 0;
var cy = 0;
//Solving a diffeq?
var solving = false;
//diffeq step size
var step = 0.001;
var con = document.getElementById("con");
var proto = document.getElementById("prototype").cloneNode(true);
proto.removeAttribute("id");
//JSON parse/stringify
if(!this.JSON){this.JSON={}}(function(){function f(n){return n<10?'0'+n:n}if(typeof Date.prototype.toJSON!=='function'){Date.prototype.toJSON=function(key){return isFinite(this.valueOf())?this.getUTCFullYear()+'-'+f(this.getUTCMonth()+1)+'-'+f(this.getUTCDate())+'T'+f(this.getUTCHours())+':'+f(this.getUTCMinutes())+':'+f(this.getUTCSeconds())+'Z':null};String.prototype.toJSON=Number.prototype.toJSON=Boolean.prototype.toJSON=function(key){return this.valueOf()}}var cx=/[\u0000\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]/g,escapable=/[\\\"\x00-\x1f\x7f-\x9f\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]/g,gap,indent,meta={'\b':'\\b','\t':'\\t','\n':'\\n','\f':'\\f','\r':'\\r','"':'\\"','\\':'\\\\'},rep;function quote(string){escapable.lastIndex=0;return escapable.test(string)?'"'+string.replace(escapable,function(a){var c=meta[a];return typeof c==='string'?c:'\\u'+('0000'+a.charCodeAt(0).toString(16)).slice(-4)})+'"':'"'+string+'"'}function str(key,holder){var i,k,v,length,mind=gap,partial,value=holder[key];if(value&&typeof value==='object'&&typeof value.toJSON==='function'){value=value.toJSON(key)}if(typeof rep==='function'){value=rep.call(holder,key,value)}switch(typeof value){case'string':return quote(value);case'number':return isFinite(value)?String(value):'null';case'boolean':case'null':return String(value);case'object':if(!value){return'null'}gap+=indent;partial=[];if(Object.prototype.toString.apply(value)==='[object Array]'){length=value.length;for(i=0;i<length;i+=1){partial[i]=str(i,value)||'null'}v=partial.length===0?'[]':gap?'[\n'+gap+partial.join(',\n'+gap)+'\n'+mind+']':'['+partial.join(',')+']';gap=mind;return v}if(rep&&typeof rep==='object'){length=rep.length;for(i=0;i<length;i+=1){k=rep[i];if(typeof k==='string'){v=str(k,value);if(v){partial.push(quote(k)+(gap?': ':':')+v)}}}}else{for(k in value){if(Object.hasOwnProperty.call(value,k)){v=str(k,value);if(v){partial.push(quote(k)+(gap?': ':':')+v)}}}}v=partial.length===0?'{}':gap?'{\n'+gap+partial.join(',\n'+gap)+'\n'+mind+'}':'{'+partial.join(',')+'}';gap=mind;return v}}if(typeof JSON.stringify!=='function'){JSON.stringify=function(value,replacer,space){var i;gap='';indent='';if(typeof space==='number'){for(i=0;i<space;i+=1){indent+=' '}}else if(typeof space==='string'){indent=space}rep=replacer;if(replacer&&typeof replacer!=='function'&&(typeof replacer!=='object'||typeof replacer.length!=='number')){throw new Error('JSON.stringify');}return str('',{'':value})}}if(typeof JSON.parse!=='function'){JSON.parse=function(text,reviver){var j;function walk(holder,key){var k,v,value=holder[key];if(value&&typeof value==='object'){for(k in value){if(Object.hasOwnProperty.call(value,k)){v=walk(value,k);if(v!==undefined){value[k]=v}else{delete value[k]}}}}return reviver.call(holder,key,value)}text=String(text);cx.lastIndex=0;if(cx.test(text)){text=text.replace(cx,function(a){return'\\u'+('0000'+a.charCodeAt(0).toString(16)).slice(-4)})}if(/^[\],:{}\s]*$/.test(text.replace(/\\(?:["\\\/bfnrt]|u[0-9a-fA-F]{4})/g,'@').replace(/"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g,']').replace(/(?:^|:|,)(?:\s*\[)+/g,''))){j=eval('('+text+')');return typeof reviver==='function'?walk({'':j},''):j}throw new SyntaxError('JSON.parse');}}}());
var colorss = "#f08,#8f0,#80f,#f08,#880,#088,#808,#0ff,#f80,#f0f,#04f,#0a0,#f00,#07c".split(",");
var colors_in_use = new Array(0);
function col(n) {
return colorss[n % (colorss.length)];
}
//function list ul
var flist = document.getElementById("flist");
//log textbox
var logt = document.getElementById("logt");
var latexchars={
'gt':">",
"left":"",
"right":"",
'ge':">=",
'lt':"<",
'le':"<=",
"infty":"∞",
"cdot":"*",
"frac":"",
"alpha":"α",
"beta":"β",
'gamma':"γ",
'delta':"δ",
'zeta':"ζ",
'eta':"η",
'theta':"θ",
'iota':"ι",
'kappa':"κ",
'mu':"μ",
'nu':"ν",
'xi':"ξ",
'omicron':"ο",
'rho':"ρ",
'sigma':"σ",
'tau':"τ",
'upsilon':"υ",
'chi':"χ",
'psi':"ψ",
'omega':"ω",
'phi':"ϕ",
"phiv":"φ",
"varphi":"φ",
"epsilon":"ϵ",
"epsiv":"ε",
"varepsilon":"ε",
"sigmaf":"ς",
"sigmav":"ς",
"gammad":"ϝ",
"Gammad":"ϝ",
"digamma":"ϝ",
"kappav":"ϰ",
"varkappa":"ϰ",
"piv":"ϖ",
"varpi":"ϖ",
"rhov":"ϱ",
"varrho":"ϱ",
"thetav":"ϑ",
"vartheta":"ϑ",
"pi":"π",
"lambda":"λ",
'Gamma':"Γ",
'Delta':"Δ",
'Theta':"Θ",
'Lambda':"Λ",
'Xi':"Ξ",
'Pi':"Π",
'Sigma':"Σ",
'Upsilon':"Υ",
'Phi':"Φ",
'Psi':"Ψ",
'Omega':"Ω",
"perp":"⊥",
",":" ",
"nabla":"∇",
"forall":"∀",
"sum":"∑",
"summation":"∑",
"prod":"∏",
"product":"∏",
"coprod":"∐",
"coproduct":"∐",
"int":"∫",
"integral":"∫"
};
function getlatexpart(match, submatch)
{
if(submatch == ",")
return "";
if(latexchars[submatch] !== undefined)
return latexchars[submatch];
return submatch;
}
//gets the value of a mathquill textbox (HTMLElement obj)
function getstr(obj, latex)
{
if(obj.getAttribute("class")=="matheditor"){
consolelog("Error: matheditor is not a mathquill","error");
}
if(kinput=="span"){
if(latex)
return $(obj).mathquill("latex");
return (
$(obj).mathquill("latex")
.replace(/}{/g, ")/(") //so far only fractions have >1 MathBlock's
.replace(/\\([a-zA-Z\.\,]+)/g, getlatexpart) //LaTeX symbols => special characters
.replace(/{/g, "(") //LaTeX blocks {} usually correspond to () in text
.replace(/}/g, ")")
.replace(/\\/g, "") //leftover backslashes
);
}else {
return obj.value;
}
}
//sets the value of a mathquill textbox.
function setstr(obj, val) {
if(kinput=="span"){
$(obj).mathquill("latex", val)
}else{
obj.value=val;
}
}
function save() {
if (!ready) {
return;
}
if (window.localStorage) {
var od = {
"status": "ok",
g: []
};
for (var n = 0; n < flist.childNodes.length; n++) {
od.g.push(getstr(flist.childNodes[n].getElementsByClassName("matheditor")[0].getElementsByTagName(kinput)[0],true));
}
var out = JSON.stringify(od);
window.localStorage.setItem("fn", out);
}
}
function newfunc(funcval) {
var newone = proto.cloneNode(true);
var inputbox=document.createElement(kinput);
var inputbox_container=newone.getElementsByClassName("matheditor")[0];
inputbox_container.appendChild(inputbox);
if(kinput=="span"){
inputbox.appendChild(document.createTextNode(funcval || randfunc()));
$(inputbox).mathquill('editable');
}else {
inputbox.value=funcval || randfunc();
inputbox.onkeydown=inputbox.onkeyup=function(){this.onchange()};
}
g.push(function (x) {
return 0;
});
var currentnodeslength = flist.childNodes.length;
inputbox.onchange = function(){getf(undefined,currentnodeslength);};
//getf(inputbox, flist.childNodes.length, true);
if (newone.getElementsByClassName) {
var bs = newone.getElementsByClassName("b");
colors_in_use.push(colorss.pop());
if (bs.length > 0) {
bs[0].style.background = colors_in_use[flist.childNodes.length];
}
}
flist.appendChild(newone);
if(kinput=="span"){
$(inputbox).mathquill("redraw");
if(!funcval){
$(inputbox).trigger({ type: "keydown", ctrlKey: true, which: 65 });
}
}else{
inputbox.onchange();
}
if (loaded) {
inputbox.focus();
//inputbox.select();
save();
draw();
}
}
function delfunc() {
if (flist.childNodes.length > 1) {
g.pop();
flist.removeChild(flist.lastChild);
flist.lastChild.getElementsByClassName("matheditor")[0].getElementsByTagName(kinput)[0].focus();
save();
}
}
function delfunc(delete_button_node){
this_node=delete_button_node.parentNode;
index=0;
other_node=flist.firstChild;
while(other_node!=null){
if(other_node==this_node){
break;
index++;
}
other_node=other_node.nextSibling;
}
g.splice(index,1);
colorss.push(colors_in_use.splice(index,1)[0]);
flist.removeChild(this_node);
save();
};
var second = false;
function extrafunc(string, jjq) {
string = string.replace(/\^\(\)/g,"");
string = string.replace(/X/g, "x");
string = string.replace(/ /g, "");
string = string.replace(/√/g, "sqrt");
string = string.replace(/[•⋅∙]/g, "*");
string = string.replace(/[Ii]nfinity/g, "∞");
string = string.replace(/[Ii]nf/g, "∞");
string = string.replace(/⁻ⁱ/g, "^-1").replace(/ⁿ/g, "^n").replace(/⁻²/g, "^-2").replace(/⁻?⁰/g, "^0").replace(/⁻³/g, "^-3").replace(/⁻⁴/g, "^-4").replace(/⁻⁵/g, "^-5").replace(/⁻⁶/g, "^-6").replace(/⁻⁷/g, "^-7").replace(/⁻⁸/g, "^-8").replace(/⁻⁹/g, "^-9");
string = string.replace(/(sin|cos|tan)\^\(\-1\)/g, "a$1"); //inverse trig functions written as sin^(-1)(x)
string = string.replace(/(sin|cos|tan|sec|csc|cot|log|ln)\^([\daex])\(/g, "$1_n($2,");
string = string.replace(/(sin|cos|tan|sec|csc|cot|log|ln)\^([\daex])([^\(])/g, "$1_n($2,$3)");
string = string.replace(/log_([\daex])\(/g, "logb($1,");
string = string.replace(/log_([\daex])([^\(])/g, "logb($1,$2)");
for(i in latexchars){
if(i.length>1){//don't replace "," with space
string=string.replace(i,latexchars[i]);
}
}
string = string.replace(/(∑|∏)([^\[^\_^\^^\{][^\[]*)$/,"$1[1,∞,$2]");
string = string.replace(/(∑|∏)_\(([^\)]+)\)\^\(([^\)]+)\)(.+)$/,"$1[$2,$3,$4]");
string = string.replace(/(∑|∏)_([\d]+|.)\^([\d]+|.)(.+)$/,"$1[$2,$3,$4]");
string = string.replace(/(∑|∏)_\(([^\)]+)\)\^([\d]+|.)(.+)$/,"$1[$2,$3,$4]");
string = string.replace(/(∑|∏)_([\d]+|.)\^\(([^\)]+)\)(.+)$/,"$1[$2,$3,$4]");
string = string.replace(/[÷∕⁄]/g, "/").replace(/−/g, "-").replace(/′/g, "'").replace(/sum/g, "∑").replace(/¼/g, "0.25").replace(/½/g, "0.5").replace(/¾/g, "0.75").replace(/⅓/g, "(1/3)").replace(/⅔/g, "(2/3)").replace(/⅕/g, "0.2").replace(/⅖/g, "0.4").replace(/⅗/g, "0.6").replace(/⅘/g, "0.8").replace(/⅙/g, "(1/6)").replace(/⅚/g, "(5/6)").replace(/⅛/g, "0.125").replace(/⅜/g, "0.375").replace(/⅝/g, "0.625").replace(/⅞/g, "0.875").replace(/nx/g, "n*x").replace(/diff\(/g, "djkb(");
if (jjq===true) {
//Generic Function
string = string.replace(/[gfy]'\[([\d]+)\]\(/g, "djkb($1,1,");
string = string.replace(/[gfy]\[([\d]+)\]'\(/g, "djkb($1,1,");
string = string.replace(/[gfy]'\(/g, "djkb(0,1,");
string = string.replace(/[gfy](''|")\[([\d]+)\]\(/g, "djkb($2,2,");
string = string.replace(/[gfy]\[([\d]+)\](''|")\(/g, "djkb($1,2,");
string = string.replace(/[gfy](''|")\(/g, "djkb(0,2,");
string = string.replace(/[gfy]'''\[([\d]+)\]\(/g, "djkb($1,3,");
string = string.replace(/[gfy]\[([\d]+)\]'''\(/g, "djkb($1,3,");
string = string.replace(/[gfy]'''\(/g, "djkb(0,3,");
string = string.replace(/[gfy]''''\[([\d]+)\]\(/g, "djkb($1,4,");
string = string.replace(/[gfy]\[([\d]+)\]''''\(/g, "djkb($1,4,");
string = string.replace(/[gfy]''''\(/g, "djkb(0,4,");
string = string.replace(/f\(/g, "g(");
} else {
//Differential Equation
string = string.replace(/[gy]'\[([\d]+)\]\(/g, "djkb($1,1,");
string = string.replace(/[gy]\[([\d]+)\]'\(/g, "djkb($1,1,");
string = string.replace(/[gy]'\(/g, "djkb(0,1,");
string = string.replace(/[gy](''|")\[([\d]+)\]\(/g, "djkb($2,2,");
string = string.replace(/[gy]\[([\d]+)\](''|")\(/g, "djkb($1,2,");
string = string.replace(/[gy](''|")\(/g, "djkb(0,2,");
string = string.replace(/[gy]'''\[([\d]+)\]\(/g, "djkb($1,3,");
string = string.replace(/[gy]\[([\d]+)\]'''\(/g, "djkb($1,3,");
string = string.replace(/[gy]'''\(/g, "djkb(0,3,");
string = string.replace(/[gy]''''\[([\d]+)\]\(/g, "djkb($1,4,");
string = string.replace(/[gy]\[([\d]+)\]''''\(/g, "djkb($1,4,");
string = string.replace(/[gy]''''\(/g, "djkb(0,4,");
string = string.replace(/f'\(x\)/g, "dy");
string = string.replace(/f'x/g, "dy");
string = string.replace(/fx/g, "y");
string = string.replace(/dx/g, "1");
}
var sargs, ext, enn, iad;
string = string.replace(/≥/g, ">=").replace(/≤/g, "<=").replace(/\++/g, "+").replace(/(\-\-)+/g, "+").replace(/\-(\-\-)+/,"-").replace(/Γ/g, "γ").replace(/γ\(/g, "fact(-1+").replace(/²/g, "^2").replace(/³/g, "^3").replace(/⁴/g, "^4").replace(/⁵/g, "^5").replace(/⁶/g, "^6").replace(/⁷/g, "^7").replace(/⁸/g, "^8").replace(/⁹/g, "^9").replace(/xxx/g, "x*x*x").replace(/xx/g, "x*x").replace(/([\d\.]+|[a-zπ])\!/g, "fact($1)").replace(/\(([^\(^\)]+)\)\!/g, "fact($1)").replace(/([^o^t^a-z^A-Z])g\(/g, "$1g[0](").replace(/^g\(/, "g[0](").replace(/\|([^\|]+)\|/g, "abs($1)").replace(/f\(x\)/g, "y").replace(/x\(/g, "x*(").replace(/x\^-1/g, "(1/x)").replace(/e\^(-[\d\.xy])/g, "exp($1)").replace(/e\^\(/g, "exp(").replace(/([^\(\)\^\]\[\,\.])\^\(/g,"pow($1,").replace(/\(([^\)\(\[\]\.\^\,]+)\)\^\(/g,"pow($1,").replace(/₀/g, "_0").replace(/₁/g, "_1").replace(/₂/g, "_2").replace(/₃/g, "_3").replace(/₄/g, "_4").replace(/₅/g, "_5").replace(/₆/g, "_6").replace(/₇/g, "_7").replace(/₈/g, "_8").replace(/₉/g, "_9").replace(/ₐ/g, "_a").replace(/ₑ/g, "_e").replace(/ₓ/g, "_x");
if (string.indexOf("_") != -1 && /(mass|m\:)/.test(string)) {
string = string.replace(/([a-zA-Z])_([\d])/g, "$1*$2+");
string = string.replace(/([A-Z])([A-Z])/g, "$1+$2");
string = string.replace(/^(mass\:|m\:)/, "");
}
if(!jjq){string=string.replace(/θ/g, "theta").replace(/theta/g, "(atan(y/x))")};
string = string.replace(/([^\)]|[\d]+|[\d]+\.[\d]+)\^([\d]|[^\(^-])/g, "(pow($1,$2))").replace(/([^\)]|[\d]+|[\d]+\.[\d]+)\^-([\d]+|[^\(^-])/g, "(pow($1,-$2))").replace(/([a-z])\^\(([^\)]+)\)/g, "(pow($1,$2))").replace(/\(([^\(^\)]+)\)\^\(([^\(^\)]+)\)/g, "(pow($1,$2))").replace(/\(([^\(^\)]+)\)\^([^\(^\)])/g, "(pow($1,$2))").replace(/ r /g, "(sqrt(x*x+y*y))").replace(/([\d]+)([^\+^%^\-^\*^\/^\d^\.^\}^\)^\:^>^<^\[^\]^\(^\{^\,])/g, "$1*$2").replace(/([^_^a-z^0-9][\d]+)\(/g, "$1*(").replace(/^([\d]+)\(/, "$1*(").replace(/\)pow/g, ")*pow").replace("γ(n+1)", "n!").replace("1/(n*n)", "n^-2").replace(/\)\(/g, ")*(").replace(/\(\+?pow\(([a-z]),([a-z])\)\)/g, "(pow($1,$2))");
var hassum = string.indexOf("∑") != -1;
string = string.replace(/([ail])n/g, "$1é");
string = string.replace(/([∑∏])\[n=/g, "$1[");
string = string.replace(/([∑∏])\[([^,]+)\.\.\./g, "$1[$2,");
if (hassum) {
for (var nnnd = 0; nnnd < 6; nnnd++) {
string = string.replace(/∑\[([^,]+),([^,]+),([^\]^\+^\-^n^\(]+)\*([^\]^\+^\-]+)\]/g, "($3)*∑[$1,$2,$4]");
string = string.replace(/∑\[([^,]+),([^,]+),([^\]^\+^\-]+)\*([^\]^\+^\-^n^\)]+)\]/g, "($4)*∑[$1,$2,$3]");
string = string.replace(/∑\[([^,]+),([^,]+),([^\]^\+^\-^n^\(]+\([^\]^\+^\-^n^\(]+\)[^\]^\+^\-^n^\(]+)\*([^\]^\+^\-]+)\]/g, "($3)*∑[$1,$2,$4]");
string = string.replace(/∑\[([^,]+),([^,]+),([^\]^\+^\-]+)\*([^\]^\+^\-^n^\(]+\([^\]^\+^\-^n^\(]+\)[^\]^\+^\-^n^\(]+)\]/g, "($4)*∑[$1,$2,$3]");
}
string = string.replace(/∑\[([^,]+),∞,([^n^\]]+)\/(fact\(n\)|\(fact\(n\)\))\]/g, "((e*$2)-∑[0,$1-1,$2/fact(n)])");
string = string.replace(/∑\[([^,]+),∞,pow\(([\d]+|[^\(^\)]),n\)\/(fact\(n\)|\(fact\(n\)\))\]/g, "(exp($2)-∑[0,$1-1,pow($2,n)])");
string = string.replace(/∑\[([^,]+),∞,pow\(([\d]+),n\)\/(fact\(n\)|\(fact\(n\)\))\]/g, "(exp($2)-∑[0,$1-1,pow($2,n)])");
string = string.replace(/1\/pow\(([^\)^,]+),([^\(^\))]+)\)/g, "pow($1,-$2)");
var args = /∑\[([^,]+),∞,pow\(n,([^\)]+)\)\]/.exec(string);
if (args != null) {
if (/0+/.test(args[1])) {
string = string.replace(args[0], "∞");
} else {
var reparg;
if (!/x/.test(args[2])) {
reparg = zeta(-args[2]);
} else {
reparg = "zeta(-" + args[2] + ")";
}
string = string.replace(args[0], "((" + reparg + ")-∑[1," + args[1] + "-1,pow(n," + args[2] + ")])");
}
}
string = string.replace(/∑\[([^,]+),([^,]+),n\]/g, "(-0.5*(-1+($1)-($2))*(($1)+($2)))").replace(/∑\[0*[01],∞,pow\(n,([^n^\(^\)]+)\)\/\(?fact\(n\)\)?\]/g, "(e*bellb($1))").replace(/∑\[0*2,∞,pow\(n,([^n^\(^\)]+)\)\/\(?fact\(n\)\)?\]/g, "(e*bellb($1)-1)").replace(/∑\[0*3,∞,pow\(n,([^n^\(^\)]+)\)\/\(?fact\(n\)\)?\]/g, "(e*bellb($1)-1-pow(2,(-1+$1)))").replace(/∑\[0*[01],∞,n\/fact\(n\)\]/g, "(e)").replace(/∑\[0*2,∞,n\/fact\(n\)\]/g, "(e-1)").replace(/∑\[0*3,∞,n\/fact\(n\)\]/g, "(e-2)").replace(/∑\[0*4,∞,n\/fact\(n\)\]/g, "(0.5*(2*e-5))").replace(/∑\[0*5,∞,n\/fact\(n\)\]/g, "((1/3)*(3*e-8))").replace(/∑\[([^,]+),∞,pow\(([^\,]+),-n\)\]/g, "((pow($2,1-$1))/(-1+($2)))").replace(/∑\[([^,]+),([^,]+),pow\(([^\,]+),-n\)\]/g, "(-((pow($3,-$1-$2))*(pow($3,$1)-pow($3,1+$2)))/(-1+($3)))").replace(/∑\[([^,]+),∞,pow\(([^\,]+),n\)\]/g, "((abs($2)<1)?pow($2,$1)/(1-$2):undefined)").replace(/∏\[0,x,n\]/g, "(0)");
string=string.replace(/∑\[([\d\*\+\-a-wyz]+),([\d\*\+\-a-wyz]+|∞),([^\]]+)\]/,function(all,a,b,c){
if (c.indexOf("n") == -1) {
return "((" + c + ")*(1+(" + b + ")-(" + a + ")))";
} else {
ext = "";
if (b == "∞") {
enn = 20;
} else {
enn = safeeval(b);
}
for (iad = safeeval(a); iad <= enn; iad++) {
ext += "+" + c.replace(/n/g, iad);
}
return "(" + ext + ")";
}
});
}
string=string.replace(/ζ\(([\d\.]+)\)/g,function(x,z){return zeta(z);});
string = string.replace(/ζ/g, "zeta").replace(/≠/g, "!=").replace(/∏\[[12],([^,]+),n\]/g, "fact($1)").replace(/∏\[([\d\*\+\-a-wyz]+),([\d\*\+\-a-wyz]+),n\]/, "(fact($2)/fact(($1)-1))");
sargs = /∏\[([\d\*\+\-a-wyz]+),([\d\*\+\-a-wyz]+|∞),([^\]]+)\]/.exec(string);
if (sargs != null) {
if (sargs[3].indexOf("n") == -1) {
string = string.replace(sargs[0], "pow(" + sargs[3] + ",1+(" + sargs[2] + ")-(" + sargs[1] + "))");
} else {
ext = "1";
if (sargs[2] == "∞") {
enn = 20;
} else {
enn = safeeval(sargs[2]);
}
for (iad = safeeval(sargs[1]); iad <= enn; iad++) {
ext += "*(" + sargs[3].replace(/n/g, iad) + ")";
}
string = string.replace(sargs[0], "(" + ext + ")");
}
}
if (string[0] == "(" && string.indexOf(",") != -1) {
string = "pt" + string;
}
string = string.replace(/^pow\(x,2\)\+pow\(y,2\)=(.+)/, "sqrt(($1)-x*x)").replace(/^pow\(x,2\)\+([\d\/e\*]+)\*pow\(y,2\)=(.+)$/, "sqrt(($2-x*x)/($1))").replace(/^([0-9e\/\*\+\-\(\)]+)\*pow\(x,2\)\+pow\(y,2\)=(.+)$/, "sqrt(($2-($1*x*x)))").replace(/^([0-9e\/\*\+\-\(\)]+)\*pow\(x,2\)\+([0-9e\/\*\+\-\(\)]+)\*pow\(y,2\)=(.+)$/, "sqrt(($3-($1*x*x))/($2))").replace(/^[yfg]=/, "").replace(/^([\da-z\.\*\/]+)\*[yfg]=(.+)$/, "($2)/($1)").replace(/∞/g, "Infinity").replace(/¯/g, ",").replace(/pow\((.),4\)/g, "($1*$1*$1*$1)").replace(/pow\((.),3\)/g, "($1*$1*$1)").replace(/pow\((.),2\)/g, "($1*$1)").replace(/pow\((.),1\)/g, "($1)").replace(/pow\((.),0\)/g, "(1)").replace(/pow\(e,([^\)^\(]+)\)/g, "exp($1)").replace(/\(\)/g, "(0)").replace(/\)([a-z])/g, ")*$1");
if (jjq != null) {
string = string.replace(/fx/g, "(g[0](x))");
}
string = string.replace(/é/g, "n");
string = string.replace(/(sin|cos|tan|ln|log|abs|floor|ceil|sec|csc|tg|cot|exp)\*?x/g, "$1(x)");
string = string.replace(/pow\(([\d\.]+),([\d\.]+)\)/, function (d, x, y) {
return pow(x, y);
});
string = string.replace(/pow\(([\d\.^,]+),([\d\.^,\+\-]+)\)/, function (d, x, y) {
return pow(x, safeeval(y));
});
string = "(" + string + ")";
string = string.replace(/([\+\*\/\-])\(0\)/g, "$10");
string = string.replace(/\)\-0\)/g, "))");
string = string.replace(/é/g, "n");
string = string.replace(/(sin|cos|tan|ln|log|abs|floor|ceil|sec|csc|tg|cot|exp)x/g, "$1(x)");
string = string.replace(/\(\(([\d\.]+)\)\)/g, "($1)");
string = string.replace(/\(\(([\d\.]+)\)\)/g, "($1)");
string = string.replace(/\(\(([\d\.]+)\)\)/g, "($1)");
string = string.replace(/\(\(([\d\.]+)\)\)/g, "($1)");
string = string.replace(/([\+\*\/\-])\(([\d\.]+)\)/g, "$1$2");
string = string.replace(/\(([\d]+[\*\+\-\/][\d]+)\)/, function (d, x, y) {
return safeeval(x);
});
string = string.replace(/\+\)/g, ")");
if (string == "()") {
string = ""
}
string = string.replace(/\)([\d])/g, ")*$1");
if (string[0] == "(" && string[string.length - 1] == ")") {
string = string.substring(1, string.length - 1);
}
string = string.replace(/^(.+)[\|\:]\[([\d\-\.\+]+),([\d\-\.\+]+)\]$/, "((x>($2))&&(x<($3)))?($1):undefined");
string=string.replace(/α/g,"alpha").replace(/β/g,"beta").replace(/γ/g,"gamma").replace(/δ/g,"delta").replace(/ζ/g,"zeta").replace(/η/g,"eta").replace(/θ/g,"theta").replace(/ι/g,"iota").replace(/κ/g,"kappa").replace(/μ/g,"mu").replace(/ν/g,"nu").replace(/ξ/g,"xi").replace(/ο/g,"omicron").replace(/ρ/g,"rho").replace(/σ/g,"sigma").replace(/τ/g,"tau").replace(/υ/g,"upsilon").replace(/χ/g,"chi").replace(/ψ/g,"psi").replace(/ω/g,"omega").replace(/ϕ/g,"phi").replace(/φ/g,"phiv").replace(/ϵ/g,"epsilon").replace(/ε/g,"epsiv").replace(/ς/g,"sigmaf").replace(/ϝ/g,"gammad").replace(/ϰ/g,"kappav").replace(/ϖ/g,"piv").replace(/ϱ/g,"rhov").replace(/ϑ/g,"thetav").replace(/π/g,"pi").replace(/λ/g,"lambda").replace(/Γ/g,"Gamma").replace(/Δ/g,"Delta").replace(/Θ/g,"Theta").replace(/Λ/g,"Lambda").replace(/Ξ/g,"Xi").replace(/Π/g,"Pi").replace(/Σ/g,"Sigma").replace(/Υ/g,"Upsilon").replace(/Φ/g,"Phi").replace(/Ψ/g,"Psi").replace(/Ω/g,"Omega").replace(/⊥/g,"perp").replace(/∇/g,"nabla").replace(/∀/g,"forall").replace(/∐/g,"coprod").replace(/∫/g,"int");
string=string.replace(/\)Math/g,")*Math");
//there should be no "^"s
if(/\^/.test(string)){
throw("xor");
}
return string;
}
//function compiler
function getfunction(string) {
try {
var dddd = extrafunc(string, true);
var func = safeeval("(function (x) { with(Math){ return " + dddd + ";}})");
func(2.4324234215125);
return func;
} catch (ex) {
return false;
}
}
function scren() {
window.location = canvas.toDataURL("image/png");
}
//diffeq function compiler
function get2dfunction(string) {
try {
var func = safeeval("(function(x,y,dx,dy){ with(Math) { return " + extrafunc(string) + ";} } )");
func(2.4324234215123);
return func;
} catch (ex) {
return false;
}
}
//validate
function valiad(obj) {
try {
safeeval(extrafunc(obj.value));
obj.style.background = "white";
} catch (ex) {
obj.style.background = "red";
}
}
//Console methods
var big = false;
function size() {
big = !big;
logt.style.width = big ? "500px" : "331px";
logt.style.height = big ? "500px" : "117px";
return "OK";
}
function showcon() {
con.style.display = (con.style.display == 'none' ? 'block' : 'none');
document.getElementById("conin").focus();
_ga_track_event("Console " + con.style.display)
}
function consolelog(vla, question) {
vla = JSON.stringify(vla);
var right = document.createElement("div");
var left = document.createElement("b");
left.style.textAlign = "left";
right.style.textAlign = "right";
right.style.borderBottom = "1px solid #444";
var rtext = document.createTextNode(vla.toString());
right.appendChild(rtext);
var ltext = document.createTextNode(question);
left.appendChild(ltext);
var item = document.createElement("div");
item.appendChild(left);
item.appendChild(right);
logt.appendChild(item);
logt.scrollTop = logt.scrollHeight
}
var ans = null;
var last = "";
function consoleex(val) {
last = val.value;
if (val.value == "") {
return;
}
if (/^[\*\+\/]/.test(val.value)) {
val.value = "ans" + val.value
}
var obj;
try {
var done = false;
obj = safeeval("(" + (val.value) + ")");
ans = obj;
consolelog(obj, val.value);
done = true;
} catch (ex) {
try {
var ddsddd = extrafunc(val.value);
obj = safeeval("(" + ddsddd + ")");
ans = obj;
consolelog(obj, val.value + "=" + ddsddd + " .");
} catch (xbb) {
consolelog(xbb.message, "!" + val.value);
}
}
val.value = "";
}
function clear() {
logt.innerHTML = "";
return "Log Cleared";
}
function getf(obj, idd, force) {
//a function has been modified. get new one.
if(obj===undefined)
{
obj=flist.childNodes[idd].getElementsByClassName("matheditor")[0].getElementsByTagName(kinput)[0];
}
if (idd > (g.length - 1)) {
draw();
return;
}
var strdata=getstr(obj);
if ((idd != 0) && (idd == (flist.childNodes.length - 1)) && (strdata == "")) {
delfunc();
draw();
return;
}
//consolelog("getf: "+getstr(obj));
var func = getfunction(getstr(obj));
if (func) {
obj.style.background = "white";
g[idd] = func;
draw();
} else {
obj.style.background = "red";
}
save();
}
var good = false;
function getg(obj) {
if (obj.value == "") {
good = false;
obj.style.background = "white";
return draw();
}
var func = get2dfunction(obj.value);
if (func) {
obj.style.background = "white";
E = func;
good = true;
} else {
good = false;
obj.style.background = "red";
}
draw();
}
window.onresize = function () {
if (window.innerWidth) {
width = window.innerWidth;
height = window.innerHeight;
}
if (!width) {
width = document.body.clientWidth;
}
if (!height) {
height = document.body.clientHeight;
}
if (!height) {
height = 120;
}
canvas.width = width;
canvas.height = height;
if (draw && ctx) {
draw()
};