CombinatoricsTest.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787
  1. <?php
  2. namespace MathPHP\Tests\Probability;
  3. use MathPHP\Probability\Combinatorics;
  4. use MathPHP\Exception;
  5. class CombinatoricsTest extends \PHPUnit\Framework\TestCase
  6. {
  7. /**
  8. * @test factorial
  9. * @dataProvider dataProviderForFactorialPermutations
  10. * @param int $n
  11. * @param float $expected
  12. * @throws \Exception
  13. */
  14. public function testFactorial(int $n, float $expected)
  15. {
  16. // When
  17. $factorial = Combinatorics::factorial($n);
  18. // Then
  19. $this->assertEquals($expected, $factorial);
  20. }
  21. /**
  22. * @test factorial bounds exception
  23. * @throws \Exception
  24. */
  25. public function testFactorialBoundsException()
  26. {
  27. // Then
  28. $this->expectException(Exception\OutOfBoundsException::class);
  29. // When
  30. Combinatorics::factorial(-1);
  31. }
  32. /**
  33. * @test doubleFactorial
  34. * @dataProvider dataProviderForDoubleFactorial
  35. * @param int $n
  36. * @param float $expected
  37. * @throws \Exception
  38. */
  39. public function testDoubleFactorial(int $n, float $expected)
  40. {
  41. // When
  42. $doubleFactorial = Combinatorics::doubleFactorial($n);
  43. // Then
  44. $this->assertEquals($expected, $doubleFactorial);
  45. }
  46. /**
  47. * @return array [n, doubleFactorial]
  48. */
  49. public function dataProviderForDoubleFactorial(): array
  50. {
  51. return [
  52. [0, 1],
  53. [1, 1],
  54. [2, 2],
  55. [3, 3],
  56. [4, 8],
  57. [5, 15],
  58. [6, 48],
  59. [7, 105],
  60. [8, 384],
  61. [9, 945],
  62. [10, 3840],
  63. [11, 10395],
  64. [12, 46080],
  65. [13, 135135],
  66. [14, 645120],
  67. ];
  68. }
  69. /**
  70. * @test doubleFactorial n less than zero
  71. * @throws \Exception
  72. */
  73. public function testDoubleFactorialExceptionNLessThanZero()
  74. {
  75. // Then
  76. $this->expectException(Exception\OutOfBoundsException::class);
  77. // When
  78. Combinatorics::doubleFactorial(-1);
  79. }
  80. /**
  81. * @test risingFactorial
  82. * @dataProvider dataProviderForRisingFactorial
  83. * @param int $x
  84. * @param int $n
  85. * @param float $expected
  86. * @throws \Exception
  87. */
  88. public function testRisingFactorial(int $x, int $n, float $expected)
  89. {
  90. // When
  91. $risingFactorial = Combinatorics::risingFactorial($x, $n);
  92. // Then
  93. $this->assertEquals($expected, $risingFactorial);
  94. }
  95. /**
  96. * @return array [x, n, risingFactorial]
  97. */
  98. public function dataProviderForRisingFactorial(): array
  99. {
  100. return [
  101. [5, 0, 1],
  102. [5, 1, 5],
  103. [5, 2, 30],
  104. [5, 3, 210],
  105. [4, 4, 840],
  106. [3, 5, 2520],
  107. [2, 6, 5040],
  108. ];
  109. }
  110. /**
  111. * @test risingFactorial n less than zero
  112. * @throws \Exception
  113. */
  114. public function testRisingFactorialExceptionNLessThanZero()
  115. {
  116. // Then
  117. $this->expectException(Exception\OutOfBoundsException::class);
  118. // When
  119. Combinatorics::risingFactorial(5, -1);
  120. }
  121. /**
  122. * @test fallingFactorial
  123. * @dataProvider dataProviderForFallingFactorial
  124. * @param int $x
  125. * @param int $n
  126. * @param float $expected
  127. * @throws \Exception
  128. */
  129. public function testFallingFactorial(int $x, int $n, float $expected)
  130. {
  131. // When
  132. $fallingFactorial = Combinatorics::fallingFactorial($x, $n);
  133. // Then
  134. $this->assertEquals($expected, $fallingFactorial);
  135. }
  136. /**
  137. * @return array [x, n, fallingFactorial]
  138. */
  139. public function dataProviderForFallingFactorial(): array
  140. {
  141. return [
  142. [5, 0, 1],
  143. [5, 1, 5],
  144. [5, 2, 20],
  145. [5, 3, 60],
  146. [5, 4, 120],
  147. [5, 5, 120],
  148. [5, 6, 0],
  149. [4, 3, 24],
  150. [4, 4, 24],
  151. [4, 5, 0],
  152. [8, 5, 6720],
  153. [3, 5, 0],
  154. [2, 6, 0],
  155. ];
  156. }
  157. /**
  158. * @test fallingFactorial n less than zero
  159. * @throws \Exception
  160. */
  161. public function testFallingFactorialExceptionNLessThanZero()
  162. {
  163. // Then
  164. $this->expectException(Exception\OutOfBoundsException::class);
  165. // When
  166. Combinatorics::fallingFactorial(5, -1);
  167. }
  168. /**
  169. * @test subfactorial
  170. * @dataProvider dataProviderForSubfactorial
  171. * @param int $n
  172. * @param float $!n
  173. * @throws \Exception
  174. */
  175. public function testSubfactorial(int $n, float $!n)
  176. {
  177. // When
  178. $subfactorial = Combinatorics::subfactorial($n);
  179. // Then
  180. $this->assertEqualsWithDelta($!n, $subfactorial, 0.000000001);
  181. }
  182. /**
  183. * @return array [n, !n]
  184. */
  185. public function dataProviderForSubfactorial(): array
  186. {
  187. return [
  188. [0, 1],
  189. [1, 0],
  190. [2, 1],
  191. [3, 2],
  192. [4, 9],
  193. [5, 44],
  194. [6, 265],
  195. [7, 1854],
  196. [8, 14833],
  197. [9, 133496],
  198. [10, 1334961],
  199. ];
  200. }
  201. /**
  202. * @test subfactorial n less than zero
  203. * @throws \Exception
  204. */
  205. public function testSubactorialExceptionNLessThanZero()
  206. {
  207. // Then
  208. $this->expectException(Exception\OutOfBoundsException::class);
  209. // When
  210. Combinatorics::subfactorial(-1);
  211. }
  212. /**
  213. * @test permutations
  214. * @dataProvider dataProviderForFactorialPermutations
  215. * @param int $n
  216. * @param float $expected
  217. * @throws \Exception
  218. */
  219. public function testPermutations(int $n, float $expected)
  220. {
  221. // When
  222. $permutations = Combinatorics::permutations($n);
  223. // Then
  224. $this->assertEquals($expected, $permutations);
  225. }
  226. /**
  227. * @test permutations bounds exception
  228. * @throws \Exception
  229. */
  230. public function testPermutationsBoundsException()
  231. {
  232. // Then
  233. $this->expectException(Exception\OutOfBoundsException::class);
  234. // When
  235. Combinatorics::permutations(-1);
  236. }
  237. /**
  238. * @return array [n, permutations]
  239. */
  240. public function dataProviderForFactorialPermutations(): array
  241. {
  242. return [
  243. [1, 1],
  244. [2, 2],
  245. [3, 6],
  246. [4, 24],
  247. [5, 120],
  248. [6, 720],
  249. [7, 5040],
  250. [8, 40320],
  251. [9, 362880],
  252. [10, 3628800],
  253. [11, 39916800],
  254. [12, 479001600],
  255. [13, 6227020800],
  256. [14, 87178291200],
  257. [15, 1307674368000],
  258. [16, 20922789888000],
  259. [17, 355687428096000],
  260. [18, 6402373705728000],
  261. [19, 121645100408832000],
  262. [20, 2432902008176640000],
  263. ];
  264. }
  265. /**
  266. * @test permutations choose k
  267. * @dataProvider dataProviderForPermutationsChooseK
  268. * @param int $n
  269. * @param int $k
  270. * @param float $nPk
  271. * @throws \Exception
  272. */
  273. public function testPermutationsChooseK(int $n, int $k, float $nPk)
  274. {
  275. // When
  276. $permutations = Combinatorics::permutations($n, $k);
  277. // Then
  278. $this->assertEquals($nPk, $permutations);
  279. }
  280. /**
  281. * @return array [n, k, permutations]
  282. */
  283. public function dataProviderForPermutationsChooseK(): array
  284. {
  285. return [
  286. [10, 0, 1],
  287. [10, 1, 10],
  288. [10, 2, 90],
  289. [10, 3, 720],
  290. [10, 4, 5040],
  291. [10, 5, 30240],
  292. [10, 6, 151200],
  293. [10, 7, 604800],
  294. [10, 8, 1814400],
  295. [10, 9, 3628800],
  296. [10, 10, 3628800],
  297. [ 5, 3, 60],
  298. [ 6, 4, 360],
  299. [16, 3, 3360],
  300. [20, 3, 6840],
  301. [23, 5, 4037880],
  302. ];
  303. }
  304. /**
  305. * @test permutations choose k bounds exception
  306. * @throws \Exception
  307. */
  308. public function testPermutationsChooseKBoundsException()
  309. {
  310. // Then
  311. $this->expectException(Exception\OutOfBoundsException::class);
  312. // When
  313. Combinatorics::permutations(-1, 3);
  314. }
  315. /**
  316. * @test permutations choose k - k greater than n exception
  317. * @throws \Exception
  318. */
  319. public function testPermutationsChooseKKGreaterThanNException()
  320. {
  321. // Then
  322. $this->expectException(Exception\OutOfBoundsException::class);
  323. // When
  324. Combinatorics::permutations(3, 4);
  325. }
  326. /**
  327. * @test combinations
  328. * @dataProvider dataProviderForCombinations
  329. * @param int $n
  330. * @param int $r
  331. * @param float $expected
  332. * @throws \Exception
  333. */
  334. public function testCombinations(int $n, int $r, float $expected)
  335. {
  336. // When
  337. $combinations = Combinatorics::combinations($n, $r);
  338. // Then
  339. $this->assertEquals($expected, $combinations);
  340. }
  341. /**
  342. * Test data produced with Python scipy.special.comb(n, k, exact=True, repetition=False)
  343. * @return array [n, r, combinations]
  344. */
  345. public function dataProviderForCombinations(): array
  346. {
  347. return [
  348. [10, 0, 1],
  349. [10, 1, 10],
  350. [10, 2, 45],
  351. [10, 3, 120],
  352. [10, 4, 210],
  353. [10, 5, 252],
  354. [10, 6, 210],
  355. [10, 7, 120],
  356. [10, 8, 45],
  357. [10, 9, 10],
  358. [10, 10, 1],
  359. [ 5, 3, 10],
  360. [ 6, 4, 15],
  361. [16, 3, 560],
  362. [20, 3, 1140],
  363. [35, 20, 3247943160],
  364. [35, 25, 183579396],
  365. ];
  366. }
  367. /**
  368. * @test combinations with large floating point overflow result
  369. * @dataProvider dataProviderForCombinationsWithLargeFloatingPointOverflowResult
  370. * @param int $n
  371. * @param int $r
  372. * @param float $expected
  373. * @param float ε
  374. * @throws \Exception
  375. */
  376. public function testCombinationsWithLargeFloatingPointOverflowResult(int $n, int $r, float $expected, float $ε)
  377. {
  378. // When
  379. $combinations = Combinatorics::combinations($n, $r);
  380. // Then
  381. $this->assertEqualsWithDelta($expected, $combinations, $ε);
  382. }
  383. /**
  384. * Test data produced with Python scipy.special.comb(n, k, exact=False, repetition=False)
  385. * @return array [n, r, combinations, ε]
  386. */
  387. public function dataProviderForCombinationsWithLargeFloatingPointOverflowResult(): array
  388. {
  389. return [
  390. [70, 30, 5.534774005814348e+19, 0],
  391. [100, 50, 1.0089134454556415e+29, 1e14],
  392. ];
  393. }
  394. /**
  395. * @test combinations n less than zero
  396. * @throws \Exception
  397. */
  398. public function testCombinationsExceptionNLessThanZero()
  399. {
  400. // Then
  401. $this->expectException(Exception\OutOfBoundsException::class);
  402. // When
  403. Combinatorics::combinations(-1, 2);
  404. }
  405. /**
  406. * @test combinations r larger than n
  407. * @throws \Exception
  408. */
  409. public function testCombinationsExceptionRLargerThanN()
  410. {
  411. // Then
  412. $this->expectException(Exception\OutOfBoundsException::class);
  413. // When
  414. Combinatorics::combinations(1, 2);
  415. }
  416. /**
  417. * @test combinations with repetition
  418. * @dataProvider dataProviderForCombinationsWithRepetition
  419. * @param int $n
  420. * @param int $r
  421. * @param float $expected
  422. * @throws \Exception
  423. */
  424. public function testCombinationsWithRepetition(int $n, int $r, float $expected)
  425. {
  426. // When
  427. $combinations = Combinatorics::combinations($n, $r, Combinatorics::REPETITION);
  428. // Then
  429. $this->assertEquals($expected, $combinations);
  430. }
  431. /**
  432. * @test combinations with repetition bounds exception
  433. * @throws \Exception
  434. */
  435. public function testCombinationsWithRepetitionBoundsException()
  436. {
  437. // Then
  438. $this->expectException(Exception\OutOfBoundsException::class);
  439. // When
  440. Combinatorics::combinations(-1, 3, Combinatorics::REPETITION);
  441. }
  442. /**
  443. * Test data produced with Python scipy.special.comb(n, k, exact=True, repetition=True)
  444. * @return array [n, r, combinations]
  445. */
  446. public function dataProviderForCombinationsWithRepetition(): array
  447. {
  448. return [
  449. [10, 0, 1],
  450. [10, 1, 10],
  451. [10, 2, 55],
  452. [10, 3, 220],
  453. [10, 4, 715],
  454. [10, 5, 2002],
  455. [10, 6, 5005],
  456. [10, 7, 11440],
  457. [10, 8, 24310],
  458. [10, 9, 48620],
  459. [10, 10, 92378],
  460. [5, 3, 35],
  461. [5, 7, 330],
  462. [6, 4, 126],
  463. [16, 3, 816],
  464. [20, 3, 1540],
  465. [21, 20, 137846528820],
  466. [35, 25, 30284005485024837],
  467. ];
  468. }
  469. /**
  470. * @test combinations with repetition with large floating point overflow result
  471. * @dataProvider dataProviderForCombinationsWithRepetitionWithLargeFloatingPointOverflowResult
  472. * @param int $n
  473. * @param int $r
  474. * @param float $expected
  475. * @param float ε
  476. * @throws \Exception
  477. */
  478. public function testCombinationsWithRepetitionWithLargeFloatingPointOverflowResult(int $n, int $r, float $expected, float $ε)
  479. {
  480. // When
  481. $combinations = Combinatorics::combinations($n, $r, Combinatorics::REPETITION);
  482. // Then
  483. $this->assertEqualsWithDelta($expected, $combinations, $ε);
  484. }
  485. /**
  486. * Test data produced with Python scipy.special.comb(n, k, exact=False, repetition=True)
  487. * @return array [n, r, combinations, ε]
  488. */
  489. public function dataProviderForCombinationsWithRepetitionWithLargeFloatingPointOverflowResult(): array
  490. {
  491. return [
  492. [70, 30, 2.0560637875127662e+25, 1e10],
  493. [100, 50, 1.341910727315462e+40, 1e25],
  494. ];
  495. }
  496. /**
  497. * @test centralBinomialCoefficient
  498. * @dataProvider dataProviderForCentralBinomialCoefficient
  499. * @param int $n
  500. * @param float $!n
  501. * @throws \Exception
  502. */
  503. public function testCentralBinomialCoefficient(int $n, float $!n)
  504. {
  505. // When
  506. $binomial = Combinatorics::centralBinomialCoefficient($n);
  507. // Then
  508. $this->assertEqualsWithDelta($!n, $binomial, 0.000000001);
  509. }
  510. /**
  511. * @return array [n, !n]
  512. */
  513. public function dataProviderForCentralBinomialCoefficient(): array
  514. {
  515. return [
  516. [0, 1],
  517. [1, 2],
  518. [2, 6],
  519. [3, 20],
  520. [4, 70],
  521. [5, 252],
  522. [6, 924],
  523. [7, 3432],
  524. [8, 12870],
  525. [9, 48620],
  526. [10, 184756],
  527. ];
  528. }
  529. /**
  530. * @test centralBinomialCoefficient n less than zero
  531. * @throws \Exception
  532. */
  533. public function testCentralBinomialCoefficientExceptionNLessThanZero()
  534. {
  535. // Then
  536. $this->expectException(Exception\OutOfBoundsException::class);
  537. // When
  538. Combinatorics::centralBinomialCoefficient(-1);
  539. }
  540. /**
  541. * @test catalanNumber
  542. * @dataProvider dataProviderForCatalanNumber
  543. * @param int $n
  544. * @param float $!n
  545. * @throws \Exception
  546. */
  547. public function testCatalanNumber(int $n, float $!n)
  548. {
  549. // When
  550. $catalanNumber = Combinatorics::catalanNumber($n);
  551. // Then
  552. $this->assertEqualsWithDelta($!n, $catalanNumber, 0.000000001);
  553. }
  554. /**
  555. * @return array [n, !n]
  556. */
  557. public function dataProviderForCatalanNumber(): array
  558. {
  559. return [
  560. [0, 1],
  561. [1, 1],
  562. [2, 2],
  563. [3, 5],
  564. [4, 14],
  565. [5, 42],
  566. [6, 132],
  567. [7, 429],
  568. [8, 1430],
  569. [9, 4862],
  570. [10, 16796],
  571. [11, 58786],
  572. ];
  573. }
  574. /**
  575. * @test catalanNumber n less than zero
  576. * @throws \Exception
  577. */
  578. public function testCatalanNumberExceptionNLessThanZero()
  579. {
  580. // Then
  581. $this->expectException(Exception\OutOfBoundsException::class);
  582. // When
  583. Combinatorics::catalanNumber(-1);
  584. }
  585. /**
  586. * @test multinomial
  587. * @dataProvider dataProviderForMultinomialTheorem
  588. * @param array $groups
  589. * @param int $expected
  590. * @throws \Exception
  591. */
  592. public function testMultinomialTheorem(array $groups, int $expected)
  593. {
  594. // When
  595. $divisions = Combinatorics::multinomial($groups);
  596. // Then
  597. $this->assertEquals($expected, $divisions);
  598. }
  599. /**
  600. * @return array [groups, divisions]
  601. */
  602. public function dataProviderForMultinomialTheorem(): array
  603. {
  604. return [
  605. [[2, 0, 1], 3],
  606. [[1, 1, 1], 6],
  607. [[ 5, 2, 3 ], 2520],
  608. [[ 5, 5 ], 252],
  609. [[ 1, 4, 4, 2 ], 34650],
  610. [[3, 4, 5, 8], 3491888400],
  611. ];
  612. }
  613. /**
  614. * @test lahNumber
  615. * @dataProvider dataProviderForLahNumber
  616. * @param int $k
  617. * @param int $n
  618. * @param float $expected
  619. * @throws \Exception
  620. */
  621. public function testLahNumber(int $k, int $n, float $expected)
  622. {
  623. // When
  624. $lahNumber = Combinatorics::lahNumber($k, $n);
  625. // Then
  626. $this->assertEquals($expected, $lahNumber);
  627. }
  628. /**
  629. * @return array [k, n, lah]
  630. */
  631. public function dataProviderForLahNumber(): array
  632. {
  633. return [
  634. [1, 1, 1],
  635. [2, 1, 2],
  636. [2, 2, 1],
  637. [3, 1, 6],
  638. [3, 2, 6],
  639. [3, 3, 1],
  640. [4, 1, 24],
  641. [4, 2, 36],
  642. [4, 3, 12],
  643. [4, 4, 1],
  644. [5, 1, 120],
  645. [5, 2, 240],
  646. [5, 3, 120],
  647. [5, 4, 20],
  648. [5, 5, 1],
  649. [6, 1, 720],
  650. [6, 2, 1800],
  651. [6, 3, 1200],
  652. [6, 4, 300],
  653. [6, 5, 30],
  654. [6, 6, 1],
  655. [12, 1, 479001600],
  656. [12, 2, 2634508800],
  657. [12, 3, 4390848000],
  658. [12, 4, 3293136000],
  659. [12, 5, 1317254400],
  660. [12, 6, 307359360],
  661. [12, 7, 43908480],
  662. [12, 8, 3920400],
  663. [12, 9, 217800],
  664. [12, 10, 7260],
  665. [12, 11, 132],
  666. [12, 12, 1],
  667. ];
  668. }
  669. /**
  670. * @test lahNumber n or k less than one
  671. * @dataProvider dataProviderForLahNumberExceptionNOrKLessThanOne
  672. * @param int $n
  673. * @param int $k
  674. * @throws \Exception
  675. */
  676. public function testLahNumberExceptionNOrKLessThanOne(int $n, int $k)
  677. {
  678. // Then
  679. $this->expectException(Exception\OutOfBoundsException::class);
  680. // When
  681. Combinatorics::lahNumber($n, $k);
  682. }
  683. /**
  684. * @return array [n, k]
  685. */
  686. public function dataProviderForLahNumberExceptionNOrKLessThanOne(): array
  687. {
  688. return [
  689. [-1, 2],
  690. [2, -2],
  691. [-3, -3],
  692. ];
  693. }
  694. /**
  695. * @test lahNumber n less than k
  696. * @throws \Exception
  697. */
  698. public function testLahNumberExceptionNLessThanK()
  699. {
  700. // Given
  701. $k = 4;
  702. $n = 2;
  703. // Then
  704. $this->expectException(Exception\OutOfBoundsException::class);
  705. // When
  706. Combinatorics::lahNumber($n, $k);
  707. }
  708. }