1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PhpOffice\PhpSpreadsheet\Calculation\Statistical; |
4
|
|
|
|
5
|
|
|
use PhpOffice\PhpSpreadsheet\Calculation\ArrayEnabled; |
6
|
|
|
use PhpOffice\PhpSpreadsheet\Calculation\Exception; |
7
|
|
|
use PhpOffice\PhpSpreadsheet\Calculation\Information\ExcelError; |
8
|
|
|
use PhpOffice\PhpSpreadsheet\Calculation\MathTrig; |
9
|
|
|
use PhpOffice\PhpSpreadsheet\Shared\IntOrFloat; |
10
|
|
|
|
11
|
|
|
class Permutations |
12
|
|
|
{ |
13
|
|
|
use ArrayEnabled; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* PERMUT. |
17
|
|
|
* |
18
|
|
|
* Returns the number of permutations for a given number of objects that can be |
19
|
|
|
* selected from number objects. A permutation is any set or subset of objects or |
20
|
|
|
* events where internal order is significant. Permutations are different from |
21
|
|
|
* combinations, for which the internal order is not significant. Use this function |
22
|
|
|
* for lottery-style probability calculations. |
23
|
|
|
* |
24
|
|
|
* @param mixed $numObjs Integer number of different objects |
25
|
|
|
* Or can be an array of values |
26
|
|
|
* @param mixed $numInSet Integer number of objects in each permutation |
27
|
|
|
* Or can be an array of values |
28
|
|
|
* |
29
|
|
|
* @return array|float|int|string Number of permutations, or a string containing an error |
30
|
|
|
* If an array of numbers is passed as an argument, then the returned result will also be an array |
31
|
|
|
* with the same dimensions |
32
|
|
|
*/ |
33
|
24 |
|
public static function PERMUT($numObjs, $numInSet) |
34
|
|
|
{ |
35
|
24 |
|
if (is_array($numObjs) || is_array($numInSet)) { |
36
|
23 |
|
return self::evaluateArrayArguments([self::class, __FUNCTION__], $numObjs, $numInSet); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
try { |
40
|
24 |
|
$numObjs = StatisticalValidations::validateInt($numObjs); |
41
|
24 |
|
$numInSet = StatisticalValidations::validateInt($numInSet); |
42
|
1 |
|
} catch (Exception $e) { |
43
|
1 |
|
return $e->getMessage(); |
44
|
|
|
} |
45
|
|
|
|
46
|
23 |
|
if ($numObjs < $numInSet) { |
47
|
2 |
|
return ExcelError::NAN(); |
48
|
|
|
} |
49
|
21 |
|
$result1 = MathTrig\Factorial::fact($numObjs); |
50
|
21 |
|
if (is_string($result1)) { |
51
|
|
|
return $result1; |
52
|
|
|
} |
53
|
21 |
|
$result2 = MathTrig\Factorial::fact($numObjs - $numInSet); |
54
|
21 |
|
if (is_string($result2)) { |
55
|
|
|
return $result2; |
56
|
|
|
} |
57
|
|
|
// phpstan thinks result1 and result2 can be arrays; they can't. |
58
|
21 |
|
$result = round($result1 / $result2); // @phpstan-ignore-line |
59
|
|
|
|
60
|
21 |
|
return IntOrFloat::evaluate($result); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* PERMUTATIONA. |
65
|
|
|
* |
66
|
|
|
* Returns the number of permutations for a given number of objects (with repetitions) |
67
|
|
|
* that can be selected from the total objects. |
68
|
|
|
* |
69
|
|
|
* @param mixed $numObjs Integer number of different objects |
70
|
|
|
* Or can be an array of values |
71
|
|
|
* @param mixed $numInSet Integer number of objects in each permutation |
72
|
|
|
* Or can be an array of values |
73
|
|
|
* |
74
|
|
|
* @return array|float|int|string Number of permutations, or a string containing an error |
75
|
|
|
* If an array of numbers is passed as an argument, then the returned result will also be an array |
76
|
|
|
* with the same dimensions |
77
|
|
|
*/ |
78
|
17 |
|
public static function PERMUTATIONA($numObjs, $numInSet) |
79
|
|
|
{ |
80
|
17 |
|
if (is_array($numObjs) || is_array($numInSet)) { |
81
|
17 |
|
return self::evaluateArrayArguments([self::class, __FUNCTION__], $numObjs, $numInSet); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
try { |
85
|
17 |
|
$numObjs = StatisticalValidations::validateInt($numObjs); |
86
|
16 |
|
$numInSet = StatisticalValidations::validateInt($numInSet); |
87
|
2 |
|
} catch (Exception $e) { |
88
|
2 |
|
return $e->getMessage(); |
89
|
|
|
} |
90
|
|
|
|
91
|
15 |
|
if ($numObjs < 0 || $numInSet < 0) { |
92
|
2 |
|
return ExcelError::NAN(); |
93
|
|
|
} |
94
|
|
|
|
95
|
13 |
|
$result = $numObjs ** $numInSet; |
96
|
|
|
|
97
|
13 |
|
return IntOrFloat::evaluate($result); |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|