Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1441 ariadna 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
    public static function PERMUT(mixed $numObjs, mixed $numInSet)
34
    {
35
        if (is_array($numObjs) || is_array($numInSet)) {
36
            return self::evaluateArrayArguments([self::class, __FUNCTION__], $numObjs, $numInSet);
37
        }
38
 
39
        try {
40
            $numObjs = StatisticalValidations::validateInt($numObjs);
41
            $numInSet = StatisticalValidations::validateInt($numInSet);
42
        } catch (Exception $e) {
43
            return $e->getMessage();
44
        }
45
 
46
        if ($numObjs < $numInSet) {
47
            return ExcelError::NAN();
48
        }
49
        /** @var float|int|string */
50
        $result1 = MathTrig\Factorial::fact($numObjs);
51
        if (is_string($result1)) {
52
            return $result1;
53
        }
54
        /** @var float|int|string */
55
        $result2 = MathTrig\Factorial::fact($numObjs - $numInSet);
56
        if (is_string($result2)) {
57
            return $result2;
58
        }
59
        $result = round($result1 / $result2);
60
 
61
        return IntOrFloat::evaluate($result);
62
    }
63
 
64
    /**
65
     * PERMUTATIONA.
66
     *
67
     * Returns the number of permutations for a given number of objects (with repetitions)
68
     *     that can be selected from the total objects.
69
     *
70
     * @param mixed $numObjs Integer number of different objects
71
     *                      Or can be an array of values
72
     * @param mixed $numInSet Integer number of objects in each permutation
73
     *                      Or can be an array of values
74
     *
75
     * @return array|float|int|string Number of permutations, or a string containing an error
76
     *         If an array of numbers is passed as an argument, then the returned result will also be an array
77
     *            with the same dimensions
78
     */
79
    public static function PERMUTATIONA(mixed $numObjs, mixed $numInSet)
80
    {
81
        if (is_array($numObjs) || is_array($numInSet)) {
82
            return self::evaluateArrayArguments([self::class, __FUNCTION__], $numObjs, $numInSet);
83
        }
84
 
85
        try {
86
            $numObjs = StatisticalValidations::validateInt($numObjs);
87
            $numInSet = StatisticalValidations::validateInt($numInSet);
88
        } catch (Exception $e) {
89
            return $e->getMessage();
90
        }
91
 
92
        if ($numObjs < 0 || $numInSet < 0) {
93
            return ExcelError::NAN();
94
        }
95
 
96
        $result = $numObjs ** $numInSet;
97
 
98
        return IntOrFloat::evaluate($result);
99
    }
100
}