| 1441 |
ariadna |
1 |
<?php
|
|
|
2 |
|
|
|
3 |
namespace PhpOffice\PhpSpreadsheet\Calculation\MathTrig;
|
|
|
4 |
|
|
|
5 |
use PhpOffice\PhpSpreadsheet\Calculation\ArrayEnabled;
|
|
|
6 |
use PhpOffice\PhpSpreadsheet\Calculation\Exception;
|
|
|
7 |
|
|
|
8 |
class Combinations
|
|
|
9 |
{
|
|
|
10 |
use ArrayEnabled;
|
|
|
11 |
|
|
|
12 |
/**
|
|
|
13 |
* COMBIN.
|
|
|
14 |
*
|
|
|
15 |
* Returns the number of combinations for a given number of items. Use COMBIN to
|
|
|
16 |
* determine the total possible number of groups for a given number of items.
|
|
|
17 |
*
|
|
|
18 |
* Excel Function:
|
|
|
19 |
* COMBIN(numObjs,numInSet)
|
|
|
20 |
*
|
|
|
21 |
* @param mixed $numObjs Number of different objects, or can be an array of numbers
|
|
|
22 |
* @param mixed $numInSet Number of objects in each combination, or can be an array of numbers
|
|
|
23 |
*
|
|
|
24 |
* @return array|float|string Number of combinations, or a string containing an error
|
|
|
25 |
* If an array of numbers is passed as the argument, then the returned result will also be an array
|
|
|
26 |
* with the same dimensions
|
|
|
27 |
*/
|
|
|
28 |
public static function withoutRepetition(mixed $numObjs, mixed $numInSet): array|string|float
|
|
|
29 |
{
|
|
|
30 |
if (is_array($numObjs) || is_array($numInSet)) {
|
|
|
31 |
return self::evaluateArrayArguments([self::class, __FUNCTION__], $numObjs, $numInSet);
|
|
|
32 |
}
|
|
|
33 |
|
|
|
34 |
try {
|
|
|
35 |
$numObjs = Helpers::validateNumericNullSubstitution($numObjs, null);
|
|
|
36 |
$numInSet = Helpers::validateNumericNullSubstitution($numInSet, null);
|
|
|
37 |
Helpers::validateNotNegative($numInSet);
|
|
|
38 |
Helpers::validateNotNegative($numObjs - $numInSet);
|
|
|
39 |
} catch (Exception $e) {
|
|
|
40 |
return $e->getMessage();
|
|
|
41 |
}
|
|
|
42 |
|
|
|
43 |
/** @var float */
|
|
|
44 |
$quotient = Factorial::fact($numObjs);
|
|
|
45 |
/** @var float */
|
|
|
46 |
$divisor1 = Factorial::fact($numObjs - $numInSet);
|
|
|
47 |
/** @var float */
|
|
|
48 |
$divisor2 = Factorial::fact($numInSet);
|
|
|
49 |
|
|
|
50 |
return round($quotient / ($divisor1 * $divisor2));
|
|
|
51 |
}
|
|
|
52 |
|
|
|
53 |
/**
|
|
|
54 |
* COMBINA.
|
|
|
55 |
*
|
|
|
56 |
* Returns the number of combinations for a given number of items. Use COMBIN to
|
|
|
57 |
* determine the total possible number of groups for a given number of items.
|
|
|
58 |
*
|
|
|
59 |
* Excel Function:
|
|
|
60 |
* COMBINA(numObjs,numInSet)
|
|
|
61 |
*
|
|
|
62 |
* @param mixed $numObjs Number of different objects, or can be an array of numbers
|
|
|
63 |
* @param mixed $numInSet Number of objects in each combination, or can be an array of numbers
|
|
|
64 |
*
|
|
|
65 |
* @return array|float|int|string Number of combinations, or a string containing an error
|
|
|
66 |
* If an array of numbers is passed as the argument, then the returned result will also be an array
|
|
|
67 |
* with the same dimensions
|
|
|
68 |
*/
|
|
|
69 |
public static function withRepetition(mixed $numObjs, mixed $numInSet): array|int|string|float
|
|
|
70 |
{
|
|
|
71 |
if (is_array($numObjs) || is_array($numInSet)) {
|
|
|
72 |
return self::evaluateArrayArguments([self::class, __FUNCTION__], $numObjs, $numInSet);
|
|
|
73 |
}
|
|
|
74 |
|
|
|
75 |
try {
|
|
|
76 |
$numObjs = Helpers::validateNumericNullSubstitution($numObjs, null);
|
|
|
77 |
$numInSet = Helpers::validateNumericNullSubstitution($numInSet, null);
|
|
|
78 |
Helpers::validateNotNegative($numInSet);
|
|
|
79 |
Helpers::validateNotNegative($numObjs);
|
|
|
80 |
$numObjs = (int) $numObjs;
|
|
|
81 |
$numInSet = (int) $numInSet;
|
|
|
82 |
// Microsoft documentation says following is true, but Excel
|
|
|
83 |
// does not enforce this restriction.
|
|
|
84 |
//Helpers::validateNotNegative($numObjs - $numInSet);
|
|
|
85 |
if ($numObjs === 0) {
|
|
|
86 |
Helpers::validateNotNegative(-$numInSet);
|
|
|
87 |
|
|
|
88 |
return 1;
|
|
|
89 |
}
|
|
|
90 |
} catch (Exception $e) {
|
|
|
91 |
return $e->getMessage();
|
|
|
92 |
}
|
|
|
93 |
|
|
|
94 |
/** @var float */
|
|
|
95 |
$quotient = Factorial::fact($numObjs + $numInSet - 1);
|
|
|
96 |
/** @var float */
|
|
|
97 |
$divisor1 = Factorial::fact($numObjs - 1);
|
|
|
98 |
/** @var float */
|
|
|
99 |
$divisor2 = Factorial::fact($numInSet);
|
|
|
100 |
|
|
|
101 |
return round($quotient / ($divisor1 * $divisor2));
|
|
|
102 |
}
|
|
|
103 |
}
|