CombinatoricsAxiomsTest.php 5.34 KB
Newer Older
冯超鹏's avatar
冯超鹏 committed
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
<?php
namespace MathPHP\Tests\Probability;

use MathPHP\Probability\Combinatorics;

/**
 * Tests of Combinatorics axioms
 * These tests don't test specific functions,
 * but rather combinatorics axioms which in term make use of multiple functions.
 * If all the combinatorics math is implemented properly, these tests should
 * all work out according to the axioms.
 *
 * Axioms tested:
 *  - Lah numbers, rising and falling factorials
 *   - x⁽ⁿ⁾ = ∑ L⟮n,k⟯ x₍k₎
 *   - x₍n₎ = ∑ (-1)ⁿ⁻ᵏ L(n,k) x⁽ᵏ⁾
 *   - L(n,1) = n!
 *   - L(n,2) = (n - 1)n! / 2
 *   - L(n,n) = 1
 */
class CombinatoricsAxiomsTest extends \PHPUnit\Framework\TestCase
{
    /**
     * @testCase Axiom: x⁽ⁿ⁾ = L⟮n,k⟯ x₍k₎
     * Rising factorial can be represented as the summation of Lah numbers and falling factorials
     *
     * @dataProvider dataProivderForLahNumbers
     * @param        int $x
     * @param        int $n
     * @throws       \Exception
     */
    public function testRisingFactorialAsLahNumberAndFallingFactorial(int $x, int $n)
    {
        $x  = Combinatorics::risingFactorial($x, $n);

        $∑Lnkxk = 0;
        for ($k = 1; $k <= $n; $k++) {
            $xk        = Combinatorics::fallingFactorial($x, $k);
            $Lnk       = Combinatorics::lahNumber($n, $k);
            $∑Lnkxk += $Lnk * $xk;
        }
    
        $this->assertEquals($x, $∑Lnkxk);
    }

    /**
     * @testCase Axiom: x₍n₎ = ∑ (-1)ⁿ⁻ᵏ L(n,k) x⁽ᵏ⁾
     * Falling factorial can be represented as the summation of Lah numbers and rising factorials
     *
     * @dataProvider dataProivderForLahNumbers
     * @param        int $x
     * @param        int $n
     * @throws       \Exception
     */
    public function testFallingFactorialAsLahNumberAndRisingFactorial(int $x, int $n)
    {
        $xn = Combinatorics::fallingFactorial($x, $n);

        $∑⟮−1ᵏLnkxk = 0;
        for ($k = 1; $k <= $n; $k++) {
            $⟮−1            = (-1)**($n - $k);
            $Lnk             = Combinatorics::lahNumber($n, $k);
            $x              = Combinatorics::risingFactorial($x, $k);
            $∑⟮−1ᵏLnkxk += $⟮−1 * $Lnk * $x;
        }
    
        $this->assertEquals($xn, $∑⟮−1ᵏLnkxk);
    }

    /**
     * @testCase Axiom: L(n,1) = n!
     * Lah number identity when k is 1
     *
     * @dataProvider dataProivderForLahNumberIdentities
     * @param        int $n
     * @throws       \Exception
     */
    public function testLahNumberIdentityKEqualsOne(int $n)
    {
        $Ln1 = Combinatorics::lahNumber($n, 1);
        $n    = Combinatorics::factorial($n);

        $this->assertEquals($Ln1, $n);
    }

    /**
     * @testCase Axiom: L(n,2) = (n - 1)n! / 2
     * Lah number identity when k is 2
     *
     * @dataProvider dataProivderForLahNumberIdentitiesGreaterThanOne
     * @param        int $n
     * @throws       \Exception
     */
    public function testLahNumberIdentityKEqualsTwo(int $n)
    {
        $Ln1     = Combinatorics::lahNumber($n, 2);
        $⟮n1n!/2 = (($n - 1) * Combinatorics::factorial($n)) / 2;

        $this->assertEquals($Ln1, $⟮n1n!/2);
    }

    /**
     * @testCase Axiom: L(n,n) = 1
     * Lah number identity when n = n
     *
     * @dataProvider dataProivderForLahNumberIdentities
     * @param        int $n
     * @throws       \Exception
     */
    public function testLahNumberIdentityNNEqualsOne(int $n)
    {
        $Lnn = Combinatorics::lahNumber($n, $n);

        $this->assertEquals(1, $Lnn);
    }

    /**
     * @return array [n, k]
     */
    public function dataProivderForLahNumbers(): array
    {
        return [
            [1, 1],
            [5, 1],
            [5, 2],
            [5, 3],
            [4, 4],
            [3, 5],
            [2, 6],
            [1, 1],
            [2, 1],
            [2, 2],
            [3, 1],
            [3, 2],
            [3, 3],
            [4, 1],
            [4, 2],
            [4, 3],
            [4, 4],
            [5, 1],
            [5, 2],
            [5, 3],
            [5, 4],
            [5, 5],
            [6, 1],
            [6, 2],
            [6, 3],
            [6, 4],
            [6, 5],
            [6, 6],
            [12, 1],
            [12, 2],
            [12, 3],
            [12, 4],
            [12, 5],
            [12, 6],
            [12, 7],
            [12, 8],
            [12, 9],
            [12, 10],
            [12, 11],
            [12, 12],
        ];
    }

    /**
     * @return array [n]
     */
    public function dataProivderForLahNumberIdentities(): array
    {
        return [
            [1],
            [2],
            [3],
            [4],
            [5],
            [6],
            [7],
            [8],
            [9],
            [12],
        ];
    }

    /**
     * @return array [n]
     */
    public function dataProivderForLahNumberIdentitiesGreaterThanOne(): array
    {
        return [
            [2],
            [3],
            [4],
            [5],
            [6],
            [7],
            [8],
            [9],
            [12],
        ];
    }
}