1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PhpOffice\PhpSpreadsheet\Calculation\MathTrig; |
4
|
|
|
|
5
|
|
|
use PhpOffice\PhpSpreadsheet\Calculation\Exception; |
6
|
|
|
use PhpOffice\PhpSpreadsheet\Calculation\Functions; |
7
|
|
|
use PhpOffice\PhpSpreadsheet\Calculation\Statistical; |
8
|
|
|
|
9
|
|
|
class Factorial |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* FACT. |
13
|
|
|
* |
14
|
|
|
* Returns the factorial of a number. |
15
|
|
|
* The factorial of a number is equal to 1*2*3*...* number. |
16
|
|
|
* |
17
|
|
|
* Excel Function: |
18
|
|
|
* FACT(factVal) |
19
|
|
|
* |
20
|
|
|
* @param float $factVal Factorial Value |
21
|
|
|
* |
22
|
|
|
* @return float|int|string Factorial, or a string containing an error |
23
|
|
|
*/ |
24
|
115 |
|
public static function fact($factVal) |
25
|
|
|
{ |
26
|
|
|
try { |
27
|
115 |
|
$factVal = Helpers::validateNumericNullBool($factVal); |
28
|
113 |
|
Helpers::validateNotNegative($factVal); |
29
|
4 |
|
} catch (Exception $e) { |
30
|
4 |
|
return $e->getMessage(); |
31
|
|
|
} |
32
|
|
|
|
33
|
111 |
|
$factLoop = floor($factVal); |
34
|
111 |
|
if ($factVal > $factLoop) { |
35
|
5 |
|
if (Functions::getCompatibilityMode() == Functions::COMPATIBILITY_GNUMERIC) { |
36
|
2 |
|
return Statistical\Distributions\Gamma::gammaValue($factVal + 1); |
37
|
|
|
} |
38
|
|
|
} |
39
|
|
|
|
40
|
109 |
|
$factorial = 1; |
41
|
109 |
|
while ($factLoop > 1) { |
42
|
101 |
|
$factorial *= $factLoop--; |
43
|
|
|
} |
44
|
|
|
|
45
|
109 |
|
return $factorial; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* FACTDOUBLE. |
50
|
|
|
* |
51
|
|
|
* Returns the double factorial of a number. |
52
|
|
|
* |
53
|
|
|
* Excel Function: |
54
|
|
|
* FACTDOUBLE(factVal) |
55
|
|
|
* |
56
|
|
|
* @param float $factVal Factorial Value |
57
|
|
|
* |
58
|
|
|
* @return float|int|string Double Factorial, or a string containing an error |
59
|
|
|
*/ |
60
|
9 |
|
public static function factDouble($factVal) |
61
|
|
|
{ |
62
|
|
|
try { |
63
|
9 |
|
$factVal = Helpers::validateNumericNullSubstitution($factVal, 0); |
64
|
8 |
|
Helpers::validateNotNegative($factVal); |
65
|
2 |
|
} catch (Exception $e) { |
66
|
2 |
|
return $e->getMessage(); |
67
|
|
|
} |
68
|
|
|
|
69
|
7 |
|
$factLoop = floor($factVal); |
70
|
7 |
|
$factorial = 1; |
71
|
7 |
|
while ($factLoop > 1) { |
72
|
6 |
|
$factorial *= $factLoop; |
73
|
6 |
|
$factLoop -= 2; |
74
|
|
|
} |
75
|
|
|
|
76
|
7 |
|
return $factorial; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* MULTINOMIAL. |
81
|
|
|
* |
82
|
|
|
* Returns the ratio of the factorial of a sum of values to the product of factorials. |
83
|
|
|
* |
84
|
|
|
* @param mixed[] $args An array of mixed values for the Data Series |
85
|
|
|
* |
86
|
|
|
* @return float|string The result, or a string containing an error |
87
|
|
|
*/ |
88
|
8 |
|
public static function multinomial(...$args) |
89
|
|
|
{ |
90
|
8 |
|
$summer = 0; |
91
|
8 |
|
$divisor = 1; |
92
|
|
|
|
93
|
|
|
try { |
94
|
|
|
// Loop through arguments |
95
|
8 |
|
foreach (Functions::flattenArray($args) as $argx) { |
96
|
8 |
|
$arg = Helpers::validateNumericNullSubstitution($argx, null); |
97
|
8 |
|
Helpers::validateNotNegative($arg); |
98
|
8 |
|
$arg = (int) $arg; |
99
|
8 |
|
$summer += $arg; |
100
|
8 |
|
$divisor *= self::fact($arg); |
101
|
|
|
} |
102
|
4 |
|
} catch (Exception $e) { |
103
|
4 |
|
return $e->getMessage(); |
104
|
|
|
} |
105
|
|
|
|
106
|
4 |
|
$summer = self::fact($summer); |
107
|
|
|
|
108
|
4 |
|
return $summer / $divisor; |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|