-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmath.php
761 lines (675 loc) · 17.9 KB
/
math.php
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
<?php
/**
**********************************************
* We do not handle all type of things *
* knowing that the one who will use *
* this script is a programmer and *
* has a bit knowledge of math *
* and uses it in a good way *
**********************************************
*/
/**
* PHP Math Library
*/
class math
{
const M_PI = 3.14159265358979323846;
const M_E = 2.7182818284590452354;
const M_LOG2E = 1.4426950408889634074;
const M_LOG10E = 0.43429448190325182765;
const M_LN2 = 0.69314718055994530942;
const M_LN10 = 2.30258509299404568402;
const M_PI_2 = 1.57079632679489661923;
const M_PI_4 = 0.78539816339744830962;
const M_1_PI = 0.31830988618379067154;
const M_2_PI = 0.63661977236758134308;
const M_2_SQRTPI = 1.12837916709551257390;
const M_SQRT2 = 1.41421356237309504880;
const M_SQRT1_2 = 0.70710678118654752440;
public static function add($x, $y) // addition
{
return $x + $y;
}
public static function subtract($x, $y) // subtraction
{
return $x - $y;
}
public static function multiply($x, $y) // multiplication
{
return $x * $y;
}
public static function divide($x, $y) // divition
{
return $x / $y;
}
public static function modulo($x, $y) // modulo
{
return $x % $y;
}
public static function AddMany($data) // add many in array
{
if (!is_array($data)) {
return 'Please Give an array of numbers';
}
$EndData = 0;
foreach ($data as $key => $value) {
$EndData += $value;
}
return $EndData;
}
public static function subtractMany($data) // subtract many in array
{
if (!is_array($data)) {
return 'Please Give an array of numbers';
}
$EndData = 0;
foreach ($data as $key => $value) {
$EndData -= $value;
}
return $EndData;
}
public static function multiplyMany($data) // multiply many in array
{
if (!is_array($data)) {
return 'Please Give an array of numbers';
}
$EndData = 1;
foreach ($data as $key => $value) {
$EndData *= $value;
}
return $EndData;
}
public static function divideMany($data) // divide many in array
{
if (!is_array($data)) {
return 'Please Give an array of numbers';
}
$EndData = 1;
foreach ($data as $key => $value) {
$EndData /= $value;
}
return $EndData;
}
public static function gcd($num1, $num2) // finding gcd of 2 numbers
{
while ($num2 != 0)
{
$m = $num1 % $num2;
$num1 = $num2;
$num2 = $m;
}
return abs($num1);
}
public static function gcdMany($numbers) // finding gcd of many munbers
{
if (!is_array($numbers)) {
return 'Please Give an array of numbers';
}
$gcd = self::gcd($numbers[0], $numbers[1]);
for ($i = 2; $i < count($numbers); $i++) {
$gcd = self::gcd($gcd, $numbers[$i]);
}
return abs($gcd);
}
// this 2 functions binomail() and binomailRecursive() is from
// https://github.com/powder96/numbers.php/blob/master/lib/NumbersPHP/Basic.php
// we just copy pasted it here
public static function binomial($n, $k)
{
$array = array();
return self::binomialRecursive($array, $n, $k);
}
private static function binomialRecursive(&$array, $n, $k)
{
if ($n >= 0 && $k == 0) {
return 1;
}
if ($n == 0 && $k > 0) {
return 0;
}
if (isset($array[$n]) && isset($array[$n][$k]) && $array[$n][$k] > 0) {
return $array[$n][$k];
}
if (!isset($array[$n])) {
$array[$n] = array();
}
$left = self::binomialRecursive($array, $n - 1, $k - 1);
$right = self::binomialRecursive($array, $n - 1, $k);
return $array[$n][$k] = $left + $right;
}
public static function lcm($num1, $num2) // lcm of 2 numbers
{
return (abs($num1 * $num2) / self::gcd($num1, $num2));
}
public static function lcmMany($numbers) // lcm of many numbers
{
if (!is_array($number)) {
return 'Please Give an array of numbers';
}
$lcm = self::lcm($numbers[0], $numbers[1]);
for ($i = 2; $i < count($numbers); $i++) {
$lcm = self::lcm($lcm, $numbers[$i]);
}
return abs($lcm);
}
public static function AreaOfCircleFromRadius($radius) // area of circle
{
$data = array();
$data['area'] = pi() * $radius * $radius;
$data['perimeter'] = 2 - pi() - $radius;
return $data;
}
public static function AreaOfCircleFromAngle($angle, $radius) // area of a cirlce from its angle
{
$data = array();
$data['area'] = ($angle / 360 - pi() - pow($radius, 2));
$data['perimeter'] = ($angle / 180 - pi() - $radius);
return $data;
}
public static function AreaOfParm($l, $w, $h) // area of parallelogram
{
return $l * $w * $h;
}
public static function AreaOfEquilateralTraingle($a) // area of an equilateral traingle
{
$data = array();
$data['area'] = ((sqrt(3) / 4) - pow($a, 2));
$data['RadiusOfCircle'] = ((sqrt(3) / 6) - $a);
$data['RadiusOfCircumscribedCircle'] = ((sqrt(3) / 3) - $a);
return $data;
}
public static function AreaOfSquare($a)
{
$data = array();
$data['area'] = (pow($a, 2));
$data['RadiusOfCircle'] = (1/2 - $a);
$data['RadiusOfCircumscribedCircle'] = ((sqrt(2) / 2) - $a);
return $data;
}
public static function AreaOfPentagon($a)
{
$data = array();
$data['area'] = (1/4 - (sqrt(25 + (10 * sqrt(5)))) - pow($a, 2));
$data['RadiusOfCircle'] = (1 / (sqrt(20 - (8 * sqrt(5)))) - $a);
$data['RadiusOfCircumscribedCircle'] = (2 / (sqrt(10 - (2 * sqrt(5)))) - $a);
return $data;
}
public static function AreaOfhexagon($a)
{
$data = array();
$data['area'] = (((3 * sqrt(3)) / 2) - pow($a, 2));
$data['RadiusOfCircle'] = ((sqrt(3) / 2) - $a);
$data['RadiusOfCircumscribedCircle'] = ($a);
return $data;
}
public static function AreaOfOctagon($a)
{
$data = array();
$data['area'] = (2 * (1 + sqrt(2)) * pow($a, 2));
$data['RadiusOfCircle'] = ((1 + (sqrt(2) / 2)) - $a);
$data['RadiusOfCircumscribedCircle'] = (sqrt(1 + (sqrt(2) / 2) - $a));
return $data;
}
public static function AreaOfRectangle($w, $h)
{
$area = $h * $w;
$perimeter = 2 * ($h + $w);
$allData = array('area' => $area, 'perimeter' => $perimeter);
return $allData;
}
public static function mean($number = array())
{
$sum = 0;
$total = count($number);
foreach ($number as $addThem) {
$sum = $sum + $addThem;
}
return ($sum / $total);
}
public static function pythagoras($num1, $num2, $num3) // pythagoras theorem
{
if ($num1 == 'find') {
return sqrt(pow($num2, 2) + pow($num3, 2));
} else if ($num2 == 'find') {
return sqrt(pow($num1, 2) - pow($num3, 2));
} else if ($num3 == 'find') {
return sqrt(pow($num2, 2) - pow($num2, 2));
} else {
if (sqrt(pow($num1, 2)) != (sqrt(pow($num2, 2) + pow($num3, 2)))) {
return false;
} else {
return true;
}
}
}
public static function root($root) // square root
{
// this is the theory of how to find the square root but we will use its build in function for speed
// if ($root < 0) {
// return NAN;
// } else {
// $x1 = (1/2) * (600.000 + ($root / 600.000));
// $x2 = (1/2) * ($x1 + ($root / $x1));
// while ($x1 != $x2) {
// $x1 = (1/2) * ($x2 + ($root / $x2));
// $x2 = (1/2) * ($x1 + ($root / $x1));
// }
// return $x2;
// }
return sqrt($root);
}
public static function power($base, $exp) // power
{
// this whole code is commented and not in use because it can calculate 5^5 but canno't calculate 5^5.5
// so if you have an idea of how to do it message us and we will add it with a credits to you
// $sum = 1;
// $base = trim($base);
// $exp = trim(floatval($exp));
// $NumOfExp = explode('.', $exp);
// if ($exp > 0) {
// while ($NumOfExp[0] > 0) {
// $sum = $sum * $base;
// $NumOfExp[0]--;
// }
// $sum = $sum + ($base * ($NumOfExp[1] / 10));
// } else {
// while ($NumOfExp[0] < 0) {
// $sum = $sum / $base;
// $NumOfExp[0]++;
// }
// $sum = $sum + ($base * ($NumOfExp[1] / 10));
// }
// return $sum;
return pow($base, $exp);
}
public static function str2hex($string) // string to hex
{
$hexstr = unpack('H*', $string);
return array_shift($hexstr);
}
public static function hex2str($hexstr) // hex to string
{
$hexstr = str_replace(' ', '', $hexstr);
$hexstr = str_replace('\x', '', $hexstr);
$retstr = pack('H*', $hexstr);
return $retstr;
}
public static function SinInDeg($ang) // sine in degree
{
$ang = $ang * pi() / 180;
// return ($ang - (pow($ang, 3) / 6) + (pow($ang, 5) / 120) - (pow($ang, 7) / 5040));
return sin($ang);
}
public static function CosInDeg($ang) // cosine in degree
{
$ang = $ang * pi() / 180;
// return (1 - (pow($ang, 2) / 2) + (pow($ang, 4) / 24) - (pow($ang, 6) / 720));
return cos($ang);
}
public static function TanInDeg($ang) // tangent in degree
{
$ang = $ang * pi() / 180;
// return ($ang + (pow($ang, 3) / 3) + (2 * (pow($ang, 5)) / 15) + (17 * (pow($ang, 7)) / 315));
return tan($ang);
}
public static function SinInRad($ang) // sine in radians
{
// $ang = $ang * 180 / pi();
// return ($ang - (pow($ang, 3) / 6) + (pow($ang, 5) / 120) - (pow($ang, 7) / 5040));
return sin($ang);
}
public static function CosInRad($ang) // cosine in radians
{
// $ang = $ang * 180 / pi();
// return (1 - (pow($ang, 2) / 2) + (pow($ang, 4) / 24) - (pow($ang, 6) / 720));
return cos($ang);
}
public static function TanInRad($ang) // tangent in radians
{
// $ang = $ang * 180 / pi();
// return ($ang + (pow($ang, 3) / 3) + (2 * (pow($ang, 5)) / 15) + (17 * (pow($ang, 7)) / 315));
return tan($ang);
}
public static function SinhInDeg($ang) // hyperbolic sine in degree
{
$ang = $ang * pi() / 180;
return sinh($ang);
}
public static function CoshInDeg($ang) // hyperbolic cosine in degree
{
$ang = $ang * pi() / 180;
return cosh($ang);
}
public static function TanhInDeg($ang) // hyperbolic tangent in degree
{
$ang = $ang * pi() / 180;
return tanh($ang);
}
public static function SinhInRad($ang) // hyperbolic sine in radians
{
return sinh($ang);
}
public static function CoshInRad($ang) // hyperbolic cosine in radians
{
return cosh($ang);
}
public static function TanhInRad($ang) // hyperbolic tangent in radians
{
return tanh($ang);
}
public static function aSinInDeg($ang) // inverse sine in degree
{
$ang = $ang * pi() / 180;
return asin($ang);
}
public static function aCosInDeg($ang) // inverse cosine in degree
{
$ang = $ang * pi() / 180;
return acos($ang);
}
public static function aTanInDeg($ang) // inverse tangent in degree
{
$ang = $ang * pi() / 180;
return atan($ang);
}
public static function aSinInRad($ang) // inverse sine in radians
{
return asin($ang);
}
public static function aCosInRad($ang) // inverse cosine in radians
{
return acos($ang);
}
public static function aTanInRad($ang) // inverse tangent in radians
{
return atan($ang);
}
public static function aSinhInDeg($ang) // inverse hyperbolic sine in degree
{
$ang = $ang * pi() / 180;
return asinh($ang);
}
public static function aCoshInDeg($ang) // inverse hyperbolic cosine in degree
{
$ang = $ang * pi() / 180;
return acosh($ang);
}
public static function aTanhInDeg($ang) // inverse hyperbolic tangent in degree
{
$ang = $ang * pi() / 180;
return atanh($ang);
}
public static function aSinhInRad($ang) // inverse hyperbolic sine in radians
{
return asinh($ang);
}
public static function aCoshInRad($ang) // inverse hyperbolic cosine in radians
{
return acosh($ang);
}
public static function aTanhInRad($ang) // inverse hyperbolic tangent in radians
{
return atanh($ang);
}
public static function Rad2Deg($number) // radians to degree
{
return ($number * 180 / pi());
}
public static function Deg2Rad($number) // degree to radians
{
return ($number * pi() / 180);
}
public static function absolute($number) // absolute
{
if ($number < 0)
{
return (- $number);
} else {
return ($number);
}
}
public static function SecInDeg($number) // secant
{
return (1 / SELF::CosInDeg($number));
}
public static function CscInDeg($number) // cosecant
{
return (1 / SELF::SinInDeg($number));
}
public static function CotInDeg($number) // cotangent
{
return (1 / SELF::TanInDeg($number));
}
public static function SecInRad($number) // secant
{
return (1 / SELF::CosInRad($number));
}
public static function CscInRad($number) // cosecant
{
return (1 / SELF::SinInRad($number));
}
public static function CotInRad($number) // cotangent
{
return (1 / SELF::TanInRad($number));
}
public static function exponent($r) // exponent
{
// return (1 + $r + (pow($r, 2) / 2) + (pow($r, 3) / 6) + (pow($r, 4) / 24) + (pow($r, 5) / 120));
return exp($r);
}
public static function is_leap($year) // returns true if it is leap year and false if not
{
if ( (($year % 400) == 0) || (($year % 4) == 0) )
{
return true;
} else {
return false;
}
}
public static function is_armstrong($number) // returns true if it is armstrong number and false if not
{
$b;
$t = $number;
while ($number > 0) {
$a = $number % 10;
$b = $b + $a * $a * $a;
$number = $number / 10;
}
if ($b == $t)
{
return true;
} else {
return false;
}
}
public static function is_palindrome($number) // returns true if it is palindrome number and false if not
{
$temp = $number;
$revrs = 0;
while ($temp > 0) {
$d = $temp % 10;
$temp /= 10;
$revrs = $revrs * 10 + $d;
}
if ($revrs == $number)
{
return true;
} else {
return false;
}
}
public static function FindMaxInArray($array) // finds the maximum number in a given array
{
if (!is_array($array)) {
return 'Please Give an array of numbers';
}
return max($array);
}
public static function FindMinInArray($array) // finds the minimum number in a given array
{
if (!is_array($array)) {
return 'Please Give an array of numbers';
}
return min($array);
}
public static function range($start = 0, $stop = null, $step = 1)
{
if ($stop === null) {
$stop = $start;
}
if ($stop < $start) {
$step = -abs($step);
}
$array = array();
$length = max(ceil(($stop - $start) / $step + 1), 0);
for ($i = 0; $i < $length; ++$i) {
$array[$i] = $start;
$start += $step;
}
$data = '['.$array[0].',';
for ($i=1; $i < count($array)-2; $i++) {
$data .= $array[$i].',';
}
$data .= $array[count($array)-1];
return $data.']';
}
public static function isInt($number) // check if it is int
{
if (is_numeric($number)) {
return (int)$number == (float)$number;
} else {
return false;
}
}
public static function isInf($number) // check if it is infinite
{
if (is_infinite($number))
{
return $number;
} else {
return false;
}
}
public static function IncreasingOrder($array) // order numbers in increasing order
{
if (!is_array($array))
{
return 'Please Give an array of numbers';
}
$order = array();
$count = count($array);
for ($i=0; $i < $count; $i++) {
$min = array_search(min($array), $array);
$order[] = $array[$min];
unset($array[$min]);
}
$data = '['.$order[0].',';
for ($j=1; $j < $count-1; $j++) {
$data .= $order[$j].',';
}
$data .= $order[count($order)-1];
return $data.']';
}
public static function DecreasingOrder($array) // order numbers in decreasing order
{
if (!is_array($array))
{
return 'Please Give an array of numbers';
}
$order = array();
$count = count($array);
for ($i=0; $i < $count; $i++) {
$max = array_search(max($array), $array);
$order[] = $array[$max];
unset($array[$max]);
}
$data = '['.$order[0].',';
for ($j=1; $j < $count-1; $j++) {
$data .= $order[$j].',';
}
$data .= $order[count($order)-1];
return $data.']';
}
public static function log($a) // calculate logarithm to eumuls number
{
return log($a);
}
public static function log10($a) // calculate logarithm to base 10
{
return log10($a);
}
public static function log1p($a) // calculate logarithm to eumuls number with plus 1
{
return log1p($a);
}
public static function CalculateVectorRecursive($vector) // Recursive Vector Calculator use CalculateVector(); instead
{
$vector = strtoupper(trim($vector));
$vector = preg_replace('/\s+/', '', $vector);
$imploded1 = explode("+", $vector);
foreach ($imploded1 as $key => $value) {
if (preg_match("/(-)/", $value)) {
$imploded2 = explode("-", $value);
if (!empty($imploded2[0])) {
$imploded1[] = $imploded2[0];
}
for ($i=1; $i < sizeof($imploded2); $i++) {
$imploded1[] = strrev($imploded2[$i]);
}
unset($imploded1[$key]);
}
}
foreach ($imploded1 as $key1 => $value1) {
$MyVectors[] = $value1;
}
$size = sizeof($MyVectors);
for ($i=0; $i < $size; $i++) {
for ($j=0; $j < $size; $j++) {
if (substr($MyVectors[$i], -1) == substr($MyVectors[$j], -strlen($MyVectors[$j]), 1))
{
$MyNewVectors[] = substr($MyVectors[$i], 0, -1).substr($MyVectors[$j], 1);
unset($MyVectors[$i]);
unset($MyVectors[$j]);
$MyVectors[$i] = '';
$MyVectors[$j] = '';
break;
}
}
}
foreach ($MyVectors as $key2 => $value2) {
if (!empty(trim($value2))) {
$MyNewVectors[] = $value2;
}
}
$AllVectors = array();
foreach ($MyNewVectors as $key3 => $value3) {
if (!empty(trim($value3))) {
$AllVectors[] = $value3;
}
}
if (sizeof($AllVectors) == 1) {
$output = $AllVectors[0];
} else if (sizeof($AllVectors) == 2) {
$output = $AllVectors[0]."+".$AllVectors[1];
} else {
$output = $AllVectors[0]."+";
for ($k=1; $k < sizeof($AllVectors)-1; $k++) {
$output .= $AllVectors[$k]."+";
}
$output .= $AllVectors[sizeof($AllVectors)-1];
}
return $output;
}
public static function CalculateVector($data) // calculates vector such as AB+BC+CD = AD
{
$result0 = self::CalculateVectorRecursive(trim($data));
$result1 = self::CalculateVectorRecursive(trim($result0));
while ($result0 != $result1) {
$result0 = self::CalculateVectorRecursive(trim($result1));
$result1 = self::CalculateVectorRecursive(trim($result0));
}
return $result1;
}
}
echo math::CalculateVector('Ab+BC-DC');
?>