forked from aantthony/graph.tk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
math.js
executable file
·1677 lines (1523 loc) · 49 KB
/
math.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
/*
Copyright (c) Anthony, Graph.tk and its Collaborators 2010
http://graph.tk/
graph.tk[/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/>.
Todo: use a function object array instead of just an array/object model.
Notes: addition/summation and multiplication should be considered an operation that takes 0 or more aguments.
There is no division.
Subtractional shall be dreplaced with unary negation.
Divion should be dreplaced with multiplication by the reciprocal.
i.e. x/y = x*y^(-1) , or prod{[x,pow{[y,-1]}]}
- should it?
TODO: -frac does not work.
*/
function format(num, digits) {
e = Math.E;
pi = Math.PI;
if (!num) {
return "0";
} else if (num == pi) {
return "π";
} else if (num == e) {
return "e";
} else if (num % (pi / 4) == 0) {
return (num / pi) + "π";
} else if (num % (1 / 3) == 0) {
return (num * 3) + "/3";
} else if (num % (e / 4) == 0) {
return (num / e) + "e";
} else if(num!=1){
if (( log(num) )%1 == 0) {
var exponent=log(num);
var exptext="⁰¹²³⁴⁵⁶⁷⁸⁹";
return "e"+((abs(exponent)<10)?((exponent<0?"⁻":"")+exptext[abs(exponent)]):"^"+exponent);
}
}
if (digits === undefined) {
return num.toString()
}
if (num.toPrecision) {
if (Math.abs(num) < 0.0000001) {
return "0.0000000";
}
return num.toPrecision(digits);
}
return num;
}
function random_hash(){
var s="";
for(var i=0;i<20;i++){
s+=(~~(Math.random()*16)).toString(16);
}
return s;
}
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 -> window (global)
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 delta(x){
if(x==0){
return Infinity;
}
return 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 cot(x){return 1 / (tan(x));}
var ctg = cot;
var ctn = cot;
var cosec=csc;
//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 psi(x) {
return random();
}
Γ=Gamma;
ψ=psi;
π=pi;
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;
}
}
// 'lvl'th derivative of g[ia](x) when x = 'x'
var difflevel = 0; //Used to prevent massive stacks in the recursive djkb()
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;
}
var latexchars={
'gt':">",
"left(":"(",
"right)":")",
"left[":"[",
"right]":"]",
'ge':">=",
'lt':"<",
'le':"<=",
"infty":"∞",
"cdot":"*",
"frac":"",
"backslash":"\\",
"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":"∫"
};
var obj={};
var eqtype={"product":1,"sum":2,"number":3,"constant":4,"variable":5,"discretevector":6,"continuousvector":7,"power":8,"fn":9,"fraction":10,"derivative":11,"integral":12,"equality":13,"pm":14,"operatorfactor":15};
var __debug_parser=0;
function __debug(x){
return x;
}
var spaces=" ";
var level=0;
function p(inp){
if(typeof inp=="number" || !isNaN(inp)){
return Number(inp);
}else if(__debug(1,0) && typeof inp=="object"){
if(!isNaN(inp)){
console.warn("this is returned somewhere instead of Number(this)");
return Number(inp);
}
return inp;
}
if(inp=="" || inp===undefined){
return 0;
}
//parses brackets recursively and returns an expression
//level++;
//if(level>15){throw("too recursive for debugging");return;}
__debug(!__debug_parser,0) || console.log(spaces.substring(0,level)+"p: "+inp);
var eq=[];
var e=inp.replace(/\s/g,"").replace(/\]/g,")").replace(/\[/g,"(").replace(/\)\(/g,")*(");
//TODO: known functions only, otherwise make it a product
//TODO: allow things like 2x
e=e.replace(/([^\+\-\*\/\^\:\(\)\d])\(/g,"$1:(");
e=e.replace(/([xe\d])\(/g,"$1*(");
e=e.replace(/\)([^\+\-\*\/\^\:\(\)])/g,")*$1");
if(e.indexOf("=")!=-1){
var eq=e.replace("==","[equals][equals]").split("=").map(function(e){return e.replace("[equals][equals]","==");});
if(eq.length==2){
return [p(eq[0]),p(eq[1])].setType(eqtype.equality);
}
throw("Too many '='s");
return;
}
//---Recursive Parentheses parse
while((e.indexOf("(")!=-1) && (e.indexOf(")")!=-1)){
var fail=true;
e=e.replace(/\([^\(\)]*\)/g,function(n){
fail=false;
var h=random_hash();
obj[h]=p(n.substring(1,n.length-1));
return "hash"+h+"hash";
});
if(fail){
throw ("Could not parse parentheses");
break;
}
}
var terms=[];
var last=0;
//---Sum parse
var term_op="+-";
var prod_op="*/";
if(e.indexOf(",")!=-1){
__debug(!__debug_parser,0) || console.log(spaces.substring(0,level)+"f>: "+e);
terms.type=eqtype.discretevector;
var be=e.split(",");
be.forEach(function(zz){
terms.push(p(zz));
});
}else if((e.indexOf("+")!=-1) || (e.indexOf("-")!=-1)){
__debug(!__debug_parser,0) ||console.log(spaces.substring(0,level)+"+>: "+e);
terms.type=eqtype.sum;
var nextisinverse=false;
for(var i=0;i<e.length;i++){
if(term_op.indexOf(e[i])!=-1){
var s=e.substring(last,i);
if(nextisinverse){
terms.push(p(s).multiply(-1));
nextisinverse=false;
}else{
terms.push(p(s));
}
if(e[i]=="-"){
nextisinverse=true;
}
last=i+1;
}
}
if(nextisinverse){
terms.push(p(e.substring(last,e.length)).multiply(-1));
}else{
terms.push(p(e.substring(last,e.length)));
}
}else if((e.indexOf("*")!=-1) || (e.indexOf("/")!=-1)){
__debug(!__debug_parser,0) || console.log(spaces.substring(0,level)+"*>: "+e);
terms.type=eqtype.product;
var denom=[];
denom.type=eqtype.product;
var nextisinverse=false;
//check for d/dx
for(var i=0;i<e.length;i++){
if(prod_op.indexOf(e[i])!=-1){
var s=e.substring(last,i);
if(nextisinverse){
denom.push(p(s));
nextisinverse=false;
}else{
terms.push(p(s));
}
if(e[i]=="/"){
nextisinverse=true;
}
last=i+1;
}
}
if(nextisinverse){
denom.push(p(e.substring(last,e.length)));
}else{
terms.push(p(e.substring(last,e.length)));
}
if(denom.length){
terms=[terms,denom];
terms.type=eqtype.fraction;
}
}else if(e.indexOf("^")!=-1){
__debug(!__debug_parser,0) || console.log(spaces.substring(0,level)+"^>: "+e);
var be=e.split("^");
//NOTE: for now
//^ is a BINARY operator that goes from right to left.
// 1^2^3 = 1^(2^(3))
if(be.length!=2){
throw ("^ is a binary operator");
return;
}
var base=p(be[0]);
if(base.type==eqtype.product){
terms.type=eqtype.product;
base[base.length-1]=[base[base.length-1],p(be[1])].setType(eqtype.power);
terms.push(base);
}else{
terms.type=eqtype.power;
terms.push(base);
terms.push(p(be[1]));
}
}else if(e.indexOf(":")!=-1){
__debug(!__debug_parser,0) || console.log(spaces.substring(0,level)+"f>: "+e);
terms.type=eqtype.fn;
var be=e.split(":");
if(be.length!=2){
throw ("Function composition is a binary operator");
return;
}
var match=/^log_([\d\.\+\-e]+)$/(be[0]);
if(match){
var fn_=["log",p(be[1])].setType(eqtype.fn);
terms.type=eqtype.fraction;
terms.push(fn_);
terms.push(["log", p(match[1])].setType(eqtype.fn));
}else{
var fname=p(be[0]);
if(typeof fname!="string"){
terms.type=eqtype.product;
terms.push(fname);
terms.push(p(be[1]));
}else{
terms.push(fname);
terms.push(p(be[1]));
}
}
}else if(e.indexOf("!")!=-1){
terms.type=eqtype.product;
var last=0;
for(var i=0;i<e.length;i++){
if(e[i]=="!"){
var s=e.substring(last,i);
if(s==""){
terms[terms.length-1]=["fact",terms[terms.length-1]].setType(eqtype.fn);
}else{
terms.push(["fact",p(s)].setType(eqtype.fn));
}
last=i+1;
}
}
var final=e.substring(last,e.length);
if(final!=""){
terms.push(p(final));
}
}else{
var parsednumber=NaN;
if(!isNaN(parsednumber=Number(e))){
return parsednumber;
}else if(!/^hash[a-z\d]{20}hash$/.test(e)){
var match=/^([\d](\.[\d])?)([^\d]+)$/(e);
if(match){
terms.type=eqtype.product;
terms.push(p(match[1]));
terms.push(match[3]);
}else{
var vars=e.split(".");
if(vars.length>1){
terms.type=eqtype.product;
vars.forEach(function(v){
terms.push(p(v));
});
}else{
if(0 && e.length>1 && e[0]=="d"){
terms.type=eqtype.product;
terms.push("d");
terms.push(p(e.substring(1)));
}else{
return e;
terms.type=eqtype.variable;
terms.push(e);
}
}
}
}else{
terms.type=eqtype.variable;
terms.push(e);
}
}
terms=terms.dreplace(/^hash[a-z\d]{20}hash$/,function(e){
var to_ret=obj[e.substring(4,24)];
delete obj[e.substring(4,24)];
return to_ret;
});
/*
for(var i=0;i<terms.length;i++){
if(/^hash[a-z\d]{20}hash$/.test(terms[i])){
terms[i]=obj[terms[i].substring(4,24)];
//terms[i]="e";
}
}*/
__debug(!__debug_parser,0) || console.log(spaces.substring(0,level)+"@>: "+JSON.stringify(terms));
level--;
while(typeof terms == "object" && terms.type==eqtype.variable){
terms=terms[0];
}
if(terms.length==2){
if(terms[0].length==1 && terms[0]=="d" && terms[1].length==1 && terms[1]=="dx"){
return ["diff"].setType(eqtype.operatorfactor);
}
}
if(terms.type==eqtype.product){
var found=0;
for(var i=0;i<terms.length;i++){
if(terms[i].type==eqtype.operatorfactor){
found++;
var operation=terms.splice(i,1)[0][0];
var subject=terms.splice(i).setType(eqtype.product);
if(terms.length){
return [operation,subject].setType(eqtype.fn).multiply(terms);
}else{
return [operation,subject].setType(eqtype.fn);
}
}
}
}
//while(typeof terms=="object" && terms.length==1){
// terms=terms[0];
//}
return terms;
}
/*
Random codes/gibberish to refer to the mathematics that I don't understand and have no-idea what to call.
[finding the inverse of a function, solving a relation]
Interoperability of the x's:
#zD-0 - One x
* Pop off parts from the right, (pushing to the left) until we have the identity on the right, (and the inverse on the left), or treat the function as a string of operations.
* Just go backwards
#zD-1 - x's of only hyper-[1] (+)
* Collect terms
* go to #zd-0
#zD-2 - x's of only hyper-[1,2] (+ *)
* Factorise, or expand?. or expand then factorise?
* Use formula for ax+bx^2+cx^3+dx^4
* Attempt to solve quintics with
* guessing
* A 4th/5th/3rd order Newton's method
* go to #zd-1
#zD-3 - x's of only hyper-[1,2,3] (+ * ^)
* Product log for x*e^x
* Product log for x+e^x
* Something for x+x*e^x
* go to #zd-2
#zD-4 - x's of only hyper-[1,2,3,4] (+ * ^ ....) (TODO)
* run away fast!
* go to #zd-3
Current status: not even #zD-0 - Jan 3 aanthony
*/
//Vectors:
function Vector(fn){
if((fn!==undefined) && (typeof fn == "function")){
var self=function(x){return this;};
self.type=eqtype.continuousvector;
self.f=fn;
return self;
}else{
var self=[];
self.type=eqtype.discretevector;
for(var i=0;i<arguments.length;i++){
self.push(p(arguments[i]));
}
return self;
}
};
Array.prototype.re=function(){
};
Array.prototype.im=function(){
};
Array.prototype.cross=function(o){
if(o.type!=eqtype.discretevector || this.type!=eqtype.discretevector){
throw ("I or it is not a vector!");
}
};
Array.prototype.dot=function(o){
if(o.type!=eqtype.discretevector || this.type!=eqtype.discretevector){
throw ("I or it is not a vector!");
}
var s=[];
s.type=eqtype.sum;
var lowest=min(o.length,this.length);
for(var i=0;i<lowest;i++){
s.add(this[i].multiply(o[i]));
}
return s;
};
Array.prototype.mag=function(){
if(this.type==eqtype.discretevector){
return this.dot(this.conjg()).pow((0.5));
}else{
return this.multiply(this.conjg()).pow((0.5));
}
};
Array.prototype.conjg=function(){
return this;
return this.dreplace(/i/g,p("-i"));
};
Number.prototype.search=function(x){
return this==x;
};
String.prototype.search=function (x){
return this==x;
};
Array.prototype.search=function (x){
var found=false;
for(var i=0;i<this.length;i++){
// if(this[i].search){
if(this[i].search(x)){
return true;
}
// }
}
return false;
};
String.prototype.dreplace=function(a,b){
if(a.test && a.test(this.toString())){
return b;
}
if(this.toString()===a){
return b;
}
return this.toString();
};
Number.prototype.dreplace=function(a,b){
return Number(this);
};
Array.prototype.dreplace=function(a,b){
var cp=[];
this.forEach(function(i){
if(typeof i=="string"){
if(a.test){
if(a.test(i)){
if(typeof b=="function"){
cp.push(b(i));
}else{
cp.push(b.dreplace(a,b));
}
}else{
cp.push(i);
}
}else{
if(typeof a=="string"){
cp.push(i.dreplace(a,b));
}
}
}else{
cp.push(i.dreplace(a,b));
}
});
cp.type=this.type;
return cp;
};
Number.prototype.divide=function(o){
var product=[Number(this),p(o)];
product.type=eqtype.fraction;
return product;
};
String.prototype.divide=function(o){
var product=[];
product.type=eqtype.fraction;
product.push(p(this.toString()));
product.push(p(o));
return product;
};
Array.prototype.divide=function(o){
if(this.type==eqtype.fraction){
this[1].multiply(p(o));
return this;
}
var product=[this,p(o)];
product.type=eqtype.fraction;
return product;
};
Array.prototype.power=function(o){
if(this.type==eqtype.power){
this[1]=this[1].multiply(p(o));
}
return [this,p(o)].setType(eqtype.power);
};
String.prototype.power=function(o){
return [p(this.toString()),p(o)].setType(eqtype.power);
};
Number.prototype.power=function(o){
return [Number(this),p(o)].setType(eqtype.power);
};
Array.prototype.multiply=function(o){
if(this.type==eqtype.fraction){
this[0]=this[0].multiply(p(o));
return this;
}
var other=p(o);
var oe=other.eval();
if(oe===0){
return 0;
}
else if(oe===1){
return this;
}
if(this.type==eqtype.product && other.type==eqtype.product){
var self=this;
other.forEach(function(e){self.push(e)});
return this;
}else if(this.type==eqtype.discretevector || this.type==eqtype.continuousvector){
return self.dot(o);
}
var sum=[this,o];
var oldtype=this.type;
this.push(this.splice(0,this.length));
this[0].type=oldtype;
this.push(p(o));
this.type=eqtype.product;
return this;
};
String.prototype.forEach=function(e){
e(this.toString());
};
Number.prototype.forEach=function(e){
e(Number(this));
};
String.prototype.add=function(o){
if(o==0){
return this.toString();
}
if(this.toString()=="0"){
return p(o);
}
return [p(this.toString())].setType(eqtype.sum).add(p(o));
};
Number.prototype.add=function(o){
var po=p(o);
if(po.eval()==0){
return Number(this);
}
var sum=[p(Number(this)),po];
sum.type=eqtype.sum;
return sum;
};
Number.prototype.eval=function(o){
return Number(this);
};
String.prototype.eval=function(){
if(this.toString()=="i"){
return "i";
}
if(!isNaN(this.toString())){
return Number(this.toString());
}
if(window[this.toString()]){
if(typeof window[this.toString()]!="function"){
return eval(this.toString());
}else{
return NaN;
}
}
return NaN;
};
Array.prototype.eval=function(){
if(!this.length){
console.warn("Empty: "+this.type);
}
if(this.canEval()){
return eval(this.getString(1,1));
}else{
return NaN;
}
};
String.prototype.multiply=function(o){
if(this.toString=="i"){
return -1;
}
if(o==1){
return p(this.toString());
}
if(o==0){
return 0;
}
var sum=[p(this.toString()),p(o)];
sum.type=eqtype.product;
return sum;
};
Number.prototype.multiply=function(o){
if(this==0){
if(p(o)==Infinity){
return undefined;
}
return 0;
}
if(o==1){
return Number(this);
}
if(o==0){
return 0;
}
var sum=[Number(this),p(o)];
sum.type=eqtype.product;
return sum;
};
Array.prototype.add=function(o){
if(this.type==eqtype.sum && o.type==eqtype.sum){
var self=this;
p(o).forEach(function(e){self.push(e)});
return this;
}
if(this.type==eqtype.sum){
this.push(p(o));
return this;
}
var sum=[this,o];
var oldtype=this.type;
this.push(this.splice(0,this.length));
this[0].type=oldtype;
this.push(p(o));
this.type=eqtype.sum;
return this;
};
Array.prototype.size=function(){
var _size=0;
for(var i=0;i<this.length;i++){
if(typeof this[i]=="string"){
_size+=this[i].length+6;
}else if(typeof this[i]=="number"){
_size+=5;
}else{
_size+=this[i].size?4+this[i].size():this[i].length;
}
}
return _size;
};
Array.prototype.setType=function(e){
this.type=e;
return this;
};
Number.prototype.differentiate=function(){
if(this==Infinity || this==-Infinity){
return undefined;
}
return 0;
};
String.prototype.differentiate=function(){
return Number(this.toString()=="x");
};
Array.prototype.differentiate=function(times){
times=p(times).eval() ||1;//double derivative etc. (1/2th derivative even)
if(times<0){
return this.integrate(-times);
}
var itg=[];//itg so it's similar to the integrate function
itg.type=eqtype.sum;
if(this.canEval && this.canEval()){
//I am not dependant on an independant variable.
return 0;
}
if(this.type==eqtype.sum){