-
Notifications
You must be signed in to change notification settings - Fork 51
/
math.js
executable file
·2611 lines (2374 loc) · 76 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
/*
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.
Todo: Improve CAS 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.
*/
var MessageStrings={
"failinit":"Could not initalize",
"protected":"Read-Only variable",
"nonconstantconstant":"Expression not constant",
"parentheses":"Could not parse parentheses",
"functionchain":"Function composition is a binary operator",
"expchain":"^ is a binary operator.",
"nonvector":"Vector operation attempted on non-vector",
"unknownderivative":"The derivative of %s is not known",
"intfail":"Integration failed",
"inversefail":"Could not find inverse",
"lonedoperator":"Differential operator alone",
"loneioperator":"Integral operator alone",
"fnnamenotstring":"Invalid function name"
};
Number.prototype.format=function(digits) {
var num=Number(this);
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+=(10+~~(Math.random()*23)).toString(36)
//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;
//Hyperbolic functions
function cosh(x){
return 0.5*(exp(x)+exp(-x));
}
function sinh(x){
return 0.5*(exp(x)-exp(-x));
}
function tanh(x){
return (exp(x)-exp(-x))/(exp(x)+exp(-x));
}
function sech(x){
return 1/cosh(x);
}
function cosech(x){
return 1/sech(x);
}
function coth(x){
return 1/tanh(x);
}
//Inverse hyperbolic functions
function acosh(x){
return log(x+sqrt(x*x-1));
}
function asinh(x){
return log(x+sqrt(x*x+1));
}
function atanh(x){
return 0.5*log((1+x)/(1-x));
}
function ln_n(n,x){return pow(ln(x),n);}
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 cosh_n(n,x){return pow(cosh(x),n);}
function sinh_n(n,x){return pow(cosh(x),n);}
function tanh_n(n,x){return pow(cosh(x),n);}
function coth_n(n,x){return pow(coth(x),n);}
function sech_n(n,x){return pow(sech(x),n);}
function csch_n(n,x){return pow(csch(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 u_d(x){
//discrete unit step function
return (x>=0)?1: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 gammln(xx) {
var j;
var x,tmp,y,ser;
var cof=[57.1562356658629235,-59.5979603554754912,14.1360979747417471,-0.491913816097620199,0.339946499848118887e-4,0.465236289270485756e-4,-0.983744753048795646e-4,0.158088703224912494e-3,-0.210264441724104883e-3,0.217439618115212643e-3,-0.164318106536763890e-3,0.844182239838527433e-4,-0.261908384015814087e-4,0.368991826595316234e-5];
if (xx <= 0){
throw("bad arg in gammln");
}
y=x=xx;
tmp = x+5.24218750000000000;
tmp = (x+0.5)*log(tmp)-tmp;
ser = 0.999999999999997092;
for (j=0;j<14;j++){
ser += cof[j]/++y;
}
return tmp+log(2.5066282746310005*ser/x);
}
function Gamma(x){
if(x==0){
return Infinity;
}
if(x<0){
return -pi/(x*sin(pi*x)*Gamma(-x));
}
return exp(gammln(x));
}
function old_gamma_function(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();
}
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 doublefact(x){
return pow(2,(x/2-1/4*cos(pi*x)+1/4))*pow(pi,(1/4*cos(pi*x)-1/4))*Gamma(x/2+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|":"abs:(",
"right|":")",
"cosh":"cosh",
"sinh":"sinh",
"tanh":"tanh",
"coth":"coth",
"sech":"sech",
"csch":"csch",
"cosech":"cosech",
"sin":"sin:",
"cos":"cos:",
"tan":"tan:",
"times":"*",
"sec":"sec:",
"cosec":"cosec:",
"csc":"csc:",
"cotan":"cotan:",
"cot":"cot:",
"ln":"ln:",
"lg":"log:",
"log":"log:",
"det":"det:",
"dim":"dim:",
"max":"max:",
"min":"min:",
"mod":"mod:",
"lcm":"lcm:",
"gcd":"gcd:",
"gcf":"gcf:",
"hcf":"hcf:",
"lim":"lim:",
":":"",
"left(":"(",
"right)":")",
"left[":"[",
"right]":"]",
'ge':">=",
'lt':"<",
'le':"<=",
"infty":"∞",
"cdot":"*",
"text":"",
"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":"ϖ",
"pm":"±",
"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={};
//TODO: Maybe discretevector should be removed and assumed that all un .type'ed arrays are a discretevector by default.
// Yes, but we will keep it for now because it would be helpful in finding bugs where the type is not set.
var eqtype={"product":1,"sum":2,"number":3,"discretevector":6,"continuousvector":7,"power":8,"fn":9,"fraction":10,"derivative":11,"integral":12,"equality":13,"pm":14,"operatorfactor":15,"lessthan":16,"greaterthan":17,"range":18};
var __debug_parser=0;
var __debug_mode=1;
function __debug(x,y){
if(__debug_mode){
return x;
}
return y;
}
var spaces=" ";
var level=0;
function p(inp){
if(typeof inp=="number" || !isNaN(inp)){
return Number(inp);
}else if(typeof inp=="object"){
if(!isNaN(inp)){
app.ui.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) || app.ui.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
while(e.indexOf("xx")!=-1){
e=e.replace(/xx/g,"x*x");
}
//TODO: -- -> +
e=e.replace(/∞/g,"Infinity");
e=e.replace(/\.([^\d]|$)/g,"*$1");
e=e.replace(/([\d]+(\.[\d]+)?)([^\+\-\*\/\^\:\(\)\d\=\<\>\.!])/g,"$1*$3");
//TODO: Following line is a bit hacky. Specifications need be made to clear things up.
e=e.replace(/([xyzπϕ])([exyzπϕ])/g,"$1*$2");
e=e.replace(/\^([\d]+)\(/g,"^$1:(");
e=e.replace(/max\(/g,"(max)(");
e=e.replace(/([xyzπϕ\d∫])\(/g,"$1*(");
e=e.replace(/\(max\)/g,"max");
e=e.replace(/∫([^\*])/g,"∫*$1");
e=e.replace(/([xyzπϕ\d\.])∫/g,"$1*∫");
e=e.replace(/([^\+\-\*\/\^\:\(\)\d\=\<\>])\(/g,"$1:(");
e=e.replace(/\)([^\+\-\*\/\^\:\(\)\=\<\>!])/g,")*$1");
//multiplicative identity
e=e.replace(/\*([\)\=]|$)/g,"$1");
//Double factorial
e=e.replace(/!!/g,"‼");
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;
}else if(e.indexOf("<")!=-1){
var eq=e.split("<");
if(eq.length==2){
return [p(eq[0]),p(eq[1])].setType(eqtype.lessthan);
}
throw("Too many '<'s");
return;
}else if(e.indexOf(">")!=-1){
var eq=e.split(">");
if(eq.length==2){
return [p(eq[0]),p(eq[1])].setType(eqtype.greaterthan);
}
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 "aaaa"+h+"aaaa";
});
if(fail){
throw (MessageStrings.parentheses);
break;
}
}
var terms=[];
var last=0;
//---Sum parse
var term_op="+-";
var prod_op="*/";
if(e.indexOf(",")!=-1){
//__debug(!__debug_parser,0) || app.ui.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) ||app.ui.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) || app.ui.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){
//TODO: Fix this
//DONE: This was fixed March 16, 2011
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 if(e.indexOf("‼")!=-1){
//TODO: Fix this
//DONE: This was fixed March 16, 2011
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]=["doublefact",terms[terms.length-1]].setType(eqtype.fn);
}else{
terms.push(["doublefact",p(s)].setType(eqtype.fn));
}
last=i+1;
}
}
var final=e.substring(last,e.length);
if(final!=""){
terms.push(p(final));
}
}else if(e.indexOf(":")!=-1){
//__debug(!__debug_parser,0) || app.ui.console.log(spaces.substring(0,level)+"f>: "+e);
terms.type=eqtype.fn;
var be=e.split(":");
if(be.length!=2){
//alert(e);
throw (MessageStrings.functionchain);
//return;
}
var dmatch=/^([^\']+)(\'+)$/.exec(be[0]);
if(dmatch){
//console.log("found");
terms.type=eqtype.fn;
var b=[dmatch[1],be[1]].setType(eqtype.fn);
for(var count=dmatch[2].length;count--;count>0){
//console.log("diff");
b=["diff",b].setType(eqtype.fn);
}
terms=b;
}else{
var match=/^log_([\d\.\+\-e]+)$/.exec(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(fname.type==eqtype.power){
var basefn=fname[0].simplify();
var power=fname[1].simplify();
//if trig
if(power<0){
//find inverse
basefn="a"+basefn;
power=-power;
}
if( 1 || is_it_a_trig_function){
terms.type=eqtype.power;
terms.push([basefn,p(be[1])].setType(eqtype.fn));
terms.push(power);
}
}else 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){
//__debug(!__debug_parser,0) || app.ui.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 (MessageStrings.expchain);
return;
}
var base=p(be[0]);
terms.type=eqtype.power;
terms.push(base);
terms.push(p(be[1]));
}else{
var parsednumber=NaN;
if(!isNaN(parsednumber=Number(e))){
return parsednumber;
}else if(!/^aaaa[a-z\d]{20}aaaa$/.test(e)){
var match=/^([\d]+(\.[\d])?)([^\d]+)$/.exec(e);
if(match){
alert("old code: "+e);
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{
return e;
}
}
}else{
terms.type=eqtype.variable;
terms.push(e);
}
}
terms=terms.dreplace(/^aaaa[a-z\d]{20}aaaa$/,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(/^aaaa[a-z\d]{20}aaaa$/.test(terms[i])){
terms[i]=obj[terms[i].substring(4,24)];
//terms[i]="e";
}
}*/
//__debug(!__debug_parser,0) || app.ui.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){
//terms=terms.simplify();
if(terms.length==2 && terms.type==eqtype.fraction && terms[0].simplify()=="d" && terms[1].simplify()=="dx"){
return ["diff"].setType(eqtype.operatorfactor);
}
/*if(terms.length==2 && terms.type==eqtype.fraction && terms[0]=="d" && terms[1]=="dx"){
return ["diff"].setType(eqtype.operatorfactor);
}*/
if(terms[0].length==1 && terms[0]=="d" && terms[1].length==1 && terms[1]=="dx"){
return ["diff"].setType(eqtype.operatorfactor);
}
}
//console.log(terms.type+": "+terms.getString());
if(terms.type==eqtype.product){
var found=[];
for(var i=0;i<terms.length;i++){
if(terms[i].type==eqtype.operatorfactor){
var operation=terms.splice(i,1)[0][0];
found.push(operation);
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);
}
}else if(terms[i]=="∫"){
var operation=terms.splice(i,1)[0];
found.push(operation="int");
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 (MessageStrings.nonvector);
}
};
Array.prototype.dot=function(o){
if(o.type!=eqtype.discretevector || this.type!=eqtype.discretevector){
throw (MessageStrings.nonvector);
}
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;
};
String.prototype.dependence=function (x){
var self=this.toString();
if(typeof app.variables[self] =="function"){
return [self];
}
return [];
};
Array.prototype.dependence=function(){
var dep=[];
if(this.type==eqtype.fn || this.type==eqtype.equality){
if(this.type==eqtype.fn && !window[this[0]]){
dep.push(this[0]);
}
if(this[1].dependence){
var n=this[1].dependence();
for(var b=0;b<n.length;b++){
if(dep.indexOf(n[b])==-1){
dep.push(n[b]);
}
}
}
return dep;
}
for(var i=0;i<this.length;i++){