-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmath.js
607 lines (570 loc) · 14.4 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
var m = {
PI:3.141592653589793,
Pi:3.141592653589793,
pi:3.141592653589793,
LN2:0.6931471805599453,
Ln2:0.6931471805599453,
ln2:0.6931471805599453,
e:2.718281828459045,
E:2.718281828459045,
toDecimal:function(givenArray){
return givenArray[0]/givenArray[1];
},
toFraction:function(givenDecimal){ // lmao todo.
var number1=givenDecimal;
var timesTen=0;
while(number1%1!==0){
number1*=10;
timesTen++;
}
console.log(number1);
console.log(timesTen);
},
VtoXY:function(magnitude,angle){
var toRe= {};
toRe.x = this.cos(angle)*magnitude;
toRe.y = this.sin(angle)*magnitude;
return toRe;
},
VtoMA:function(x,y){
var toRe= [];
toRe.mag = this.sqrt(this.pow(x,2)+this.pow(y,2));
toRe.angle = (Math.atan(y/x)*180)/this.pi;
if(x<0){toRe.angle+=180;}
if(toRe[1]<0){toRe.angle+=360}
return toRe;
},
simplify:function(givenArray){
var topFactors = m.factor(givenArray[0]);
var botFactors = m.factor(givenArray[1]);
var returnArray= givenArray;
for(var top=0;top<topFactors.length;top++){
var topFactor = topFactors[top];
for(var bot=0;bot<botFactors.length;bot++){
var botFactor = botFactors[bot];
if((topFactors[top]==botFactors[bot])&&(topFactors[top]!=1)){
returnArray[0]/=topFactors[top];
returnArray[1]/=botFactors[bot];
topFactors[top] =1;
botFactors[bot] =1;
}
}
}
return returnArray;
},
arithMean:function(array){
var total=0;
for(var i=0;i<array.length;i++){
total+=array[i];
}
return total/array.length;
},
geoMean:function(array){
var total=1;
for(var i=0;i<array.length;i++){
total*=array[i];
}
return m.rad(total,array.length);
},
geoArithMean:function(number1,number2){
return arithGeoMean(number1,number2)
},
// I have to use an array for this, realistically. I could get away with not, but it's not needed
arithGeoMean:function(number1,number2){
var arithBound=[m.arithMean([number1,number2])];
var geoBound=[m.geoMean([number1,number2])];
var meanInteger=0
while(arithBound[arithBound.length-1]!==geoBound[geoBound.length-1]){
arithBound[meanInteger+1]=m.arithMean([arithBound[meanInteger],geoBound[meanInteger]]);
geoBound[meanInteger+1]=m.geoMean([arithBound[meanInteger],geoBound[meanInteger]]);
meanInteger+=1;
}
return geoBound[geoBound.length-1]
},
factorial:function(number){
if(number%1==0){
return m.intFactorial(number);
}
},
intFactorial:function(number){
var factorialSum = 1;
for(var i=0;i<number;i++){
factorialSum*= number-i;
}
return factorialSum
},
synthetic:function(system,number){
var top=system;var mid=[];var bot=[];
mid[0]=0;
for(var i=0;i<system.length;i++){
bot[i]=(top[i]+mid[i]);
mid[i+1]=(bot[i]*number);
}
var remainder=bot[system.length-1]
return remainder;
},
// I should redo this to make more sense, but for now it works
factor:function(number){
var factored=new Array();
var temp;
if(number>=0){
for(var i=0;i<number/2+1;i++){
temp=(number/i);
if(temp%1==0){
factored[factored.length]=i;
}
}
}
if(number<0){
for(var i=0;i<m.abs(number);i++){
temp=(m.abs(number)/i);
if(temp%1==0){
factored[factored.length]=-i;
}
}
}
factored[factored.length]=number;
return factored;
},
// Almost works
primeFactor:function(number){
var factored=new Array();
var temp;
for(var i=2;i<number;i++){
if(number%i==0){
factored[factored.length]=i;
number/=i;
}
}
factored[factored.length]=number;
return factored;
},
abs:function(number){
if(number<0){
return (number*-1);
}return number;
},
floor:function(number){
var finalEnd=(number-(number%1))
return finalEnd
},
ceiling:function(number){
var finalEnd=((number-(number%1))+1)
return finalEnd
},
max:function(array){
},
pow:function(number,exponent){
if(exponent==0){
return 1
}
if(exponent%1==0){
return m.intPow(number,exponent);
}
var expandedExponent = exponent; // remaking the variable so we can modify it later
var timesToTen = 1; // Setting the base for the bottom of the fraction
var aftrDecStrng = exponent.toString(); // Changing the after decimal to a string
var splitAfterDec = aftrDecStrng.split("."); // Spliting up the string in order to acquire the part after the decimal
var onlyAfterDec = splitAfterDec[1].split("");
for(var i=0;i<onlyAfterDec.length;i++){
onlyAfterDec[i]=parseInt(onlyAfterDec[i]);
}
var beforeDecInit = m.intPow(number,parseInt(splitAfterDec[0]))
var newEnd=[];
for(var i=0;i<onlyAfterDec.length;i++){
var newNum = m.intPow(number,onlyAfterDec[i])
for(var idk=0;idk<(i+1);idk++){
newEnd[i]=m.intRad(newNum,10)
newNum = newEnd[i]
}
}
for(var i=0;i<newEnd.length;i++){
beforeDecInit*=newEnd[i]
}
return beforeDecInit;
},
// exponent must be a positive integer. FOR NOW
intPow:function(number,exponent){
var finalEnd=1;
if(exponent==0){
return 1;
}
for(var i=0;i<m.abs(exponent);i++){
finalEnd*=number;
}
if(exponent<0){
return (1/finalEnd);
}
return finalEnd;
},
// Has optional specs. You don't really need it.
// I should make it m.log(number) as soon as I figure out m.log
//sqrt:function(number,specs){
/*
Condensed version of Newton's Method, or the nth root alorithm for where n=2
The nth algorithm 1/n ( (n-1)x_k + A/X_k^n-1 )
Condensed, 1/2 ( (2-1)x_k + A/x_k^2-1)
1/2 (x_k + a/x_k)
That's all this does
*/
sqrt:function(number){
if(number<0){
return -Infinity;
}
if(number==0){
return 0;
}
if(number>0){
var lowerBound = 1;
var upperBound = number;
var averageBounds;
var sqrtInteger = 0;
var looper = [lowerBound,upperBound];
var checker= [lowerBound,upperBound];
while ((!(looper[0]==looper[1]))||(!(checker[0]==checker[1]))) {
averageBounds=(lowerBound+upperBound)/2;
lowerBound=averageBounds;
upperBound=number/lowerBound;
looper[sqrtInteger%2] = averageBounds;
checker[sqrtInteger%2]= upperBound;
sqrtInteger++;
}
return averageBounds;
}
else{
return NaN
}
},
rad:function(number,index){
if(number<0) {
return "cannot exist";
}
if(index%1==0){
return m.intRad(number,index);
}
return m.pow(number,1/index)
},
// Doesn't work with massive numbers :(
/*
A function to return the formula found on http://en.wikipedia.org/wiki/Nth_root_algorithm
Left is the inside left of the bracket
Right is the inside right of the bracket
the final return of it all being over root is the outside fraction 1/n simplified
(n-1)x_k = leftInsideIntRad
A / x_k ^ (n-1) = rightInsideIntRad
n = root
x_k = number, or the current number
x_k+1 = the returned number, or the next number in the series
A = number1, or the original number to be rooted
*/
intRad:function(number1,root){
var initGuess = 1;
var inside = function (number){
var leftInsideIntRad = (root-1)*number;
var rightInsideIntRad = number1/m.intPow(number,root-1);
return (leftInsideIntRad+rightInsideIntRad)/root;
}
var checker= [];
var looper = [];
checker[0] = 1;
checker[1] = inside(1);
looper[0]=1;
looper[1]=inside(1);
var intRadInteger=0;
while((!(checker[0]==checker[1]))&&(!(looper[1]==looper[0]))) {
intRadInteger+=1;
initGuess=inside(initGuess);
checker[intRadInteger%2]=initGuess
looper[intRadInteger%3] =initGuess
}
return initGuess;
},
// Currently only works with clean numbers
// Will work on even numbers
log:function(number,base){
if(isNaN(base)){
base = 10;
}
// Otherwise running ln of small numbers is slow
if(number<1){
return -1*m.ln(1/number)/m.ln(base)
}
return m.ln(number)/m.ln(base);
},
// It's broken
ln:function(number){
if(number<1){
return -1*m.logNatural(1/number)
}
return m.logNatural(number)
},
logNatural:function(numberBegin){
// I would use m.rad(number,2), except I need a clean number at the end
var number=numberBegin;
var totZer=0;
while(number>=2){
totZer++;
number/=2;
}
// Taylor series time!
var lnInt = 1;
var total = 0;
var loops = [1,0]
while(!(loops[1]==loops[0])){
total+= (((lnInt+1)%2==0)?1:-1)*m.pow((number-1),lnInt)/lnInt
loops[lnInt%2]=total;
lnInt+=1;
}
for(var ff=0;ff<totZer;ff++){
total+=m.LN2;
}
return total;
},
// Accepts it in degrees
sin:function(numberDegrees){
// Conditions
var number = numberDegrees%360;
if(number==0) {return 0}
if(number==30) {return m.intRad(1,2)/2}
if(number==45) {return m.intRad(2,2)/2}
if(number==60) {return m.intRad(3,2)/2}
if(number==90) {return 1}
if(number==120) {return m.intRad(3,2)/2}
if(number==135) {return m.intRad(2,2)/2}
if(number==150) {return m.intRad(1,2)/2}
if(number==180) {return 0}
if(number==210) {return -m.intRad(1,2)/2}
if(number==225) {return -m.intRad(2,2)/2}
if(number==240) {return -m.intRad(3,2)/2}
if(number==270) {return -1}
if(number==300) {return -m.intRad(3,2)/2}
if(number==315) {return -m.intRad(2,2)/2}
if(number==330) {return -m.intRad(1,2)/2}
number *= m.PI/180;
var checker = [];checker[0]=0;checker[1]=1;checker[2]=2;
var looper = [];looper[0]=0;looper[1]=1;looper[2]=2;looper[3]=3;
var top;
var bot;
var total=0;
var negative;
var sinInt=1;
while(((!(checker[0]==checker[1]))&&(!(checker[1]==checker[2])))&&((!(looper[0]==looper[1])) && (!(looper[1]==looper[2])) && (!(looper[2]==looper[3])))) {
if(sinInt%2==1){
top=m.pow(number,sinInt);
bot=m.factorial(sinInt);
if(sinInt%4==1){
negative=1
}
if(sinInt%4==3){
negative=-1
}
total+= negative*top/bot
checker[sinInt%3]=total;
looper[sinInt%4]=total;
}
sinInt+=1;
}
return checker[0]
},
sine:function(number){
return m.sin(number);
},
cos:function(numberDegrees){
// Conditions
return m.sin(numberDegrees+90);
/*
var number = numberDegrees%360;
if(number==0) {return 0}
if(number==30) {return 1/2}
if(number==90) {return 1}
if(number==150) {return 1/2}
if(number==180) {return 0}
if(number==210) {return -1/2}
if(number==270) {return -1}
if(number==330) {return -1/2}
var number = numberDegrees*m.PI/180;
var checker = [];checker[0]=0;checker[1]=1;checker[2]=2;
var looper = [];looper[0]=0;looper[1]=1;looper[2]=2;looper[3]=3;
var top;
var bot;
var total=0;
var negative;
var sinInt=0;
while(((!(checker[0]==checker[1]))&&(!(checker[1]==checker[2])))&&((!(looper[0]==looper[1])) && (!(looper[1]==looper[2])) && (!(looper[2]==looper[3])))) {
if(sinInt%2==0){
top=m.pow(number,sinInt);
bot=m.factorial(sinInt);
if(sinInt%4==0){
negative=1
}
if(sinInt%4==2){
negative=-1
}
total+= negative*top/bot
checker[sinInt%3]=total;
looper[sinInt%4]=total;
}
sinInt+=1;
}
return checker[0]*/
},
calcine:function(number){
return m.cos(number);
},
tan:function(number){
return m.sin(number)/m.cos(number);
},
tangent:function(number){
return m.tan(number);
},
csc:function(number){
return 1/m.sin(number);
},
cosecant:function(number){
return m.csc(number);
},
sec:function(number){
return 1/m.cos(number);
},
secant:function(number){
return m.sec(number);
},
cot:function(number){
return 1/m.tan(number);
},
cotangent:function(number){
return m.cot(number);
},
factorial:function(number){
var factorialEnd = 1;
for(var i=number;i>0;i--){
factorialEnd*= i;
}
return factorialEnd;
},
rrt:function(first,last){
var lastFactors =m.factor(last);
var firstFactors=m.factor(first);
var totalFactors=[];
for(var i=0;i<firstFactors.length;i++){
for(var j=0;j<lastFactors.length;j++){
totalFactors[totalFactors.length]=m.abs(lastFactors[i]/firstFactors[j]);
totalFactors[totalFactors.length]=-m.abs(lastFactors[i]/firstFactors[j]);
}
}
totalFactors = sort(totalFactors);
//Deletes all duplicates
var runs = 0;
while(runs<totalFactors.length-2){ // Why isn't this a for loop?
if(totalFactors[runs]===totalFactors[runs+1]){
totalFactors.splice(runs,1);
} else {
runs++;
}
}
return totalFactors;
},
sort:function(array){ // Delete this, unless you have a reason not to.
var length = array.length-1;
for(var i=0;i<array.length;i++) {
var sorted = true;
for(var j=0;j<length;j++) {
if(array[j]>array[j+1]) {
array.splice(j,2,array[j+1],array[j]);
sorted = false;
}
}
if(sorted) {
return array;
}
length--;
}
return array;
},
solvePolynomials:function(a){
var zeros = [];
// Defining length for later for loops
var length = a.length;
// Redefines length for end zeros, as if factoring x out of x^3+4x, since there's a zero it removes it
var origZeros = 0;
for(var i=0;i<length;i++){
if(a[length-1]==0){
origZeros++;
length-=1;
zeros[zeros.length]=0;
}
}
var beginFactors = m.factor(a[0])
var endFactors = m.factor(a[length-1])
// Checking the factors with synthetic division
var toSend
var tempLength
// First sends it twice, once for positive once for negative
// Has a problem with sending the same number in 2 different ways
// Eg. Sending -2/1 and -4/2
// Is effectively rational roots. What ever
for(var y=0;y<=beginFactors.length;y++){
for(var n=0;n<=endFactors.length;n++){
toSend=endFactors[n]/beginFactors[y];
if(0==m.synthetic(a,toSend)){
tempLength=zeros.length;
zeros[tempLength]=toSend;
};
};
}
for(var y=0;y<beginFactors.length;y++){
for(var n=0;n<endFactors.length;n++){
toSend=-1*endFactors[n]/beginFactors[y];
if(0==m.synthetic(a,toSend)){
tempLength=zeros.length;
zeros[tempLength]=toSend;
};
};
}
zeros.sort(
function(a,b){
return a-b;
}
);
var finalZeros = [];
if(zeros.length>0){
finalZeros.push(zeros[0]);
for(var i=1;i<zeros.length;i++){
if(zeros[i]!=finalZeros[finalZeros.length-1]){
finalZeros.push(zeros[i]);
}
}
}
return finalZeros
// Would be a good idea to add a repeat checker, but again
// What ever
},
// number theory stuff
euclidean:function( a, b ){
if( b == 1 || a == 1 ){
return 1;
}
var s = a + " = ";
// assuming b is the smaller
var total = 0;
while( a > b ){
a -= b;
total++;
}
s += total + "*" + b + " + " + a;
console.log( s );
return euclidean(b, a);
},
gcd:function( a, b ){
if( a == 0 ){
return b;
}
if( b == 0 ){
return a;
}
if( b == 1 ){
return 1;
}
return m.gcd( b, a%b );
}
};