SetAxiomsTest.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731
  1. <?php
  2. namespace MathPHP\Tests\SetTheory;
  3. use MathPHP\SetTheory\Set;
  4. /**
  5. * Tests of Set axioms
  6. * These tests don't test specific functions,
  7. * but rather set axioms which in term make use of multiple functions.
  8. * If all the set logic is implemented properly, these tests should
  9. * all work out according to the axioms.
  10. *
  11. * Axioms tested:
  12. * - Subsets
  13. * - Ø ⊆ A
  14. * - A ⊆ A
  15. * - A = B iff A ⊆ B and B ⊆ A
  16. * - Union
  17. * - A ∪ B = B ∪ A
  18. * - A ∪ (B ∪ C) = (A ∪ B) ∪ C
  19. * - A ∪ (B ∩ C) = (A ∪ B) ∩ (A ∪ C)
  20. * - A ∪ (A ∩ B) = A
  21. * - A ⊆ (A ∪ B)
  22. * - A ∪ A = A
  23. * - A ∪ Ø = A
  24. * - |A ∪ B| = |A| + |B| - |A ∩ B|
  25. * - Intersection
  26. * - A ∩ B = B ∩ A
  27. * - A ∩ (B ∩ C) = (A ∩ B) ∩ C
  28. * - A ∩ (B ∪ C) = (A ∩ B) ∪ (A ∩ C)
  29. * - A ∩ (A ∪ B) = A
  30. * - (A ∩ B) ⊆ A
  31. * - A ∩ A = A
  32. * - A ∩ Ø = Ø
  33. * - Complement (difference)
  34. * - A ∖ B ≠ B ∖ A for A ≠ B
  35. * - A ∖ A = Ø
  36. * - Symmetric difference
  37. * - A Δ B = (A ∖ B) ∪ (B ∖ A)
  38. * - Cartesian product
  39. * - A × Ø = Ø
  40. * - A × (B ∪ C) = (A × B) ∪ (A × C)
  41. * - (A ∪ B) × C = (A × C) ∪ (B × C)
  42. * - |A × B| = |A| * |B|
  43. * - Power set
  44. * - |S| = n, then |P(S)| = 2ⁿ
  45. */
  46. class SetAxiomsTest extends \PHPUnit\Framework\TestCase
  47. {
  48. /**
  49. * @test Axiom: Ø ⊆ A
  50. * The empty set is a subset of every set
  51. * @dataProvider dataProviderForSingleSet
  52. */
  53. public function testEmptySetSubsetOfEverySet(Set $A)
  54. {
  55. // Given
  56. $Ø = new Set();
  57. // When
  58. $isSubset = $Ø->isSubset($A);
  59. // Then
  60. $this->assertTrue($isSubset);
  61. }
  62. /**
  63. * @test Axiom: A ⊆ A
  64. * Every set is a subset of itself
  65. * @dataProvider dataProviderForSingleSet
  66. */
  67. public function testSetIsSubsetOfItself(Set $A)
  68. {
  69. // When
  70. $isSubset = $A->isSubset($A);
  71. // Then
  72. $this->assertTrue($isSubset);
  73. }
  74. /**
  75. * @test Axiom: A = B iff A ⊆ B and B ⊆ A
  76. * Sets are equal if and only if they are both subsets of each other.
  77. * @dataProvider dataProviderForSingleSet
  78. */
  79. public function testEqualSetsAreSubsetsInBothDirections(Set $A)
  80. {
  81. // Given
  82. $B = $A;
  83. $this->assertEquals($A, $A);
  84. // Then
  85. $this->assertTrue($A->isSubset($B));
  86. $this->assertTrue($B->isSubset($A));
  87. }
  88. public function dataProviderForSingleSet(): array
  89. {
  90. return [
  91. [new Set([])],
  92. [new Set([0])],
  93. [new Set([1])],
  94. [new Set([5])],
  95. [new Set([-5])],
  96. [new Set([1, 2])],
  97. [new Set([1, 2, 3])],
  98. [new Set([1, -2, 3])],
  99. [new Set([1, 2, 3, 4, 5, 6])],
  100. [new Set([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])],
  101. [new Set([1, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2, 2.01, 2.001, 2.15])],
  102. [new Set(['a'])],
  103. [new Set(['a', 'b'])],
  104. [new Set(['a', 'b', 'c', 'd', 'e'])],
  105. [new Set([1, 2, 'a', 'b', 3.14, 'hello', 'goodbye'])],
  106. [new Set([1, 2, 3, new Set([1, 2]), 'a', 'b'])],
  107. [new Set(['a', 1, 'b', new Set([1, 'b']), new Set([3, 4, 5]), '4', 5])],
  108. [new Set(['a', 1, 'b', new Set([1, 'b']), new Set([3, 4, 5]), '4', 5, new Set([3, 4, 5, new Set([1, 2])])])],
  109. ];
  110. }
  111. /**
  112. * @test Axiom: A ∪ B = B ∪ A
  113. * Union is commutative
  114. *
  115. * @dataProvider dataProviderForTwoSets
  116. * @param Set $A
  117. * @param Set $B
  118. */
  119. public function testUnionCommutative(Set $A, Set $B)
  120. {
  121. // Given
  122. $A∪B = $A->union($B);
  123. $B∪A = $B->union($A);
  124. // Then
  125. $this->assertEquals($A∪B, $B∪A);
  126. $this->assertEquals($A∪B->asArray(), $B∪A->asArray());
  127. }
  128. public function dataProviderForTwoSets(): array
  129. {
  130. return [
  131. [
  132. new Set([]),
  133. new Set([]),
  134. ],
  135. [
  136. new Set([1]),
  137. new Set([]),
  138. ],
  139. [
  140. new Set([]),
  141. new Set([1]),
  142. ],
  143. [
  144. new Set([1]),
  145. new Set([1]),
  146. ],
  147. [
  148. new Set([1]),
  149. new Set([2]),
  150. ],
  151. [
  152. new Set([2]),
  153. new Set([1]),
  154. ],
  155. [
  156. new Set([1]),
  157. new Set([2]),
  158. ],
  159. [
  160. new Set([2]),
  161. new Set([1]),
  162. ],
  163. [
  164. new Set([1, 2, 3, 'a', 'b']),
  165. new Set([1, 'a', 'k']),
  166. ],
  167. [
  168. new Set([1, 2, 3, 'a', 'b', new Set([1, 2])]),
  169. new Set([1, 'a', 'k']),
  170. ],
  171. [
  172. new Set([1, 2, 3, 'a', 'b']),
  173. new Set([1, 'a', 'k', new Set([1, 2])]),
  174. ],
  175. [
  176. new Set([1, 2, 3, 'a', 'b', new Set()]),
  177. new Set([1, 'a', 'k', new Set([1, 2])]),
  178. ],
  179. [
  180. new Set([1, 2, 3, 'a', 'b', new Set([1, 2])]),
  181. new Set([1, 'a', 'k', -2, '2.4', 3.5, new Set([1, 2])]),
  182. ],
  183. ];
  184. }
  185. /**
  186. * @test Axiom: A ∪ (B ∪ C) = (A ∪ B) ∪ C
  187. * Unsion is associative
  188. *
  189. * @dataProvider dataProviderForThreeSets
  190. * @param Set $A
  191. * @param Set $B
  192. * @param Set $C
  193. */
  194. public function testUnsionAssociative(Set $A, Set $B, Set $C)
  195. {
  196. // Given
  197. $A∪⟮B∪C⟯ = $A->union($B->union($C));
  198. $⟮A∪B⟯∪C = $A->union($B)->union($C);
  199. // Then
  200. $this->assertEquals($A∪⟮B∪C⟯, $⟮A∪B⟯∪C);
  201. $this->assertEquals($A∪⟮B∪C⟯->asArray(), $⟮A∪B⟯∪C->asArray());
  202. }
  203. /**
  204. * @test Axiom: A ∪ (B ∩ C) = (A ∪ B) ∩ (A ∪ C)
  205. * Union is distributive
  206. *
  207. * @dataProvider dataProviderForThreeSets
  208. * @param Set $A
  209. * @param Set $B
  210. * @param Set $C
  211. */
  212. public function testUnionDistributive(Set $A, Set $B, Set $C)
  213. {
  214. // Given
  215. $A∪⟮B∩C⟯ = $A->union($B->intersect($C));
  216. $⟮A∪B⟯∩⟮A∪C⟯ = $A->union($B)->intersect($A->union($C));
  217. // Then
  218. $this->assertEquals($A∪⟮B∩C⟯, $⟮A∪B⟯∩⟮A∪C⟯);
  219. $this->assertEquals($A∪⟮B∩C⟯->asArray(), $⟮A∪B⟯∩⟮A∪C⟯->asArray());
  220. }
  221. /**
  222. * @test Axiom: A ∪ (A ∩ B) = A
  223. * Union absorbtion law
  224. *
  225. * @dataProvider dataProviderForTwoSets
  226. * @param Set $A
  227. * @param Set $B
  228. */
  229. public function testUnionAbsorbtion(Set $A, Set $B)
  230. {
  231. // Given
  232. $A∪⟮B∩C⟯ = $A->union($A->intersect($B));
  233. // Then
  234. $this->assertEquals($A, $A∪⟮B∩C⟯);
  235. $this->assertEquals($A->asArray(), $A∪⟮B∩C⟯->asArray());
  236. }
  237. /**
  238. * @test Axiom: A ⊆ (A ∪ B)
  239. * A is a subset of A union B
  240. *
  241. * @dataProvider dataProviderForTwoSets
  242. * @param Set $A
  243. * @param Set $B
  244. */
  245. public function testAIsSubsetOfAUnionB(Set $A, Set $B)
  246. {
  247. // Given
  248. $A∪B = $A->union($B);
  249. // Then
  250. $this->assertTrue($A->isSubset($A∪B));
  251. $this->assertTrue($B->isSubset($A∪B));
  252. }
  253. /**
  254. * @test Axiom: A ∪ A = A
  255. * A union A equals A
  256. *
  257. * @dataProvider dataProviderForSingleSet
  258. * @param Set $A
  259. */
  260. public function testAUnionAEqualsA(Set $A)
  261. {
  262. // Given
  263. $A∪A = $A->union($A);
  264. // Then
  265. $this->assertEquals($A, $A∪A);
  266. $this->assertEquals($A->asArray(), $A∪A->asArray());
  267. }
  268. /**
  269. * @test Axiom: A ∪ Ø = A
  270. * A union empty set is A
  271. *
  272. * @dataProvider dataProviderForSingleSet
  273. * @param Set $A
  274. */
  275. public function testAUnionEmptySetEqualsA(Set $A)
  276. {
  277. // Given
  278. $Ø = new Set();
  279. $A∪Ø = $A->union($Ø);
  280. // Then
  281. $this->assertEquals($A, $A∪Ø);
  282. $this->assertEquals($A->asArray(), $A∪Ø->asArray());
  283. }
  284. /**
  285. * @test Axiom: |A ∪ B| = |A| + |B| - |A ∩ B|
  286. * The cardinality (count) of unsion of A and B is equal to the cardinality of A + B minus the cardinality of A intersection B
  287. *
  288. * @dataProvider dataProviderForTwoSets
  289. * @param Set $A
  290. * @param Set $B
  291. */
  292. public function testCardinalityOfUnion(Set $A, Set $B)
  293. {
  294. // Given
  295. $A∪B = $A->union($B);
  296. $A∩B = $A->intersect($B);
  297. // Then
  298. $this->assertEquals(count($A) + count($B) - count($A∩B), count($A∪B));
  299. $this->assertEquals(count($A->asArray()) + count($B->asArray()) - count($A∩B->asArray()), count($A∪B->asArray()));
  300. }
  301. public function dataProviderForThreeSets(): array
  302. {
  303. return [
  304. [
  305. new Set([]),
  306. new Set([]),
  307. new Set([]),
  308. ],
  309. [
  310. new Set([1]),
  311. new Set([]),
  312. new Set([]),
  313. ],
  314. [
  315. new Set([]),
  316. new Set([]),
  317. new Set([1]),
  318. ],
  319. [
  320. new Set([1]),
  321. new Set([1]),
  322. new Set([1]),
  323. ],
  324. [
  325. new Set([1]),
  326. new Set([2]),
  327. new Set([2]),
  328. ],
  329. [
  330. new Set([2]),
  331. new Set([1]),
  332. new Set([1]),
  333. ],
  334. [
  335. new Set([1]),
  336. new Set([2]),
  337. new Set([3]),
  338. ],
  339. [
  340. new Set([2]),
  341. new Set([1]),
  342. new Set([1, 4]),
  343. ],
  344. [
  345. new Set([1, 2, 3, 'a', 'b']),
  346. new Set([1, 'a', 'k']),
  347. new Set([1, 9]),
  348. ],
  349. [
  350. new Set([1, 2, 3, 'a', 'b', new Set([1, 2])]),
  351. new Set([1, 'a', 'k']),
  352. new Set([34, 40]),
  353. ],
  354. [
  355. new Set([1, 2, 3, 'a', 'b']),
  356. new Set([1, 'a', 'k', new Set([1, 2])]),
  357. new Set([1, 9, 33]),
  358. ],
  359. [
  360. new Set([1, 2, 3, 'a', 'b', new Set()]),
  361. new Set([1, 'a', 'k', new Set([1, 2])]),
  362. new Set([1, new Set([1, 2])]),
  363. ],
  364. [
  365. new Set([1, 2, 3, 'a', 'b', new Set([1, 2])]),
  366. new Set([1, 'a', 'k', -2, '2.4', 3.5, new Set([1, 2])]),
  367. new Set([1, new Set([1, 2])], 99),
  368. ],
  369. ];
  370. }
  371. /**
  372. * @test Axiom: A ∩ B = B ∩ A
  373. * Intersection is commutative
  374. *
  375. * @dataProvider dataProviderForTwoSets
  376. * @param Set $A
  377. * @param Set $B
  378. */
  379. public function testIntersectionCommutative(Set $A, Set $B)
  380. {
  381. // Given
  382. $A∩B = $A->intersect($B);
  383. $B∩A = $B->intersect($A);
  384. // Then
  385. $this->assertEquals($A∩B, $B∩A);
  386. $this->assertEquals($A∩B->asArray(), $B∩A->asArray());
  387. }
  388. /**
  389. * @test Axiom: A ∩ (B ∩ C) = (A ∩ B) ∩ C
  390. * Intersection is associative
  391. *
  392. * @dataProvider dataProviderForThreeSets
  393. * @param Set $A
  394. * @param Set $B
  395. * @param Set $C
  396. */
  397. public function testIntersectionAssociative(Set $A, Set $B, Set $C)
  398. {
  399. // Given
  400. $A∩⟮B∩C⟯ = $A->intersect($B->intersect($C));
  401. $⟮A∩B⟯∩C = $A->intersect($B)->intersect($C);
  402. // Then
  403. $this->assertEquals($A∩⟮B∩C⟯, $⟮A∩B⟯∩C);
  404. $this->assertEquals($A∩⟮B∩C⟯->asArray(), $⟮A∩B⟯∩C->asArray());
  405. }
  406. /**
  407. * @test Axiom: A ∩ (B ∪ C) = (A ∩ B) ∪ (A ∩ C)
  408. * Intersection is distributive
  409. *
  410. * @dataProvider dataProviderForThreeSets
  411. * @param Set $A
  412. * @param Set $B
  413. * @param Set $C
  414. */
  415. public function testIntersectionDistributive(Set $A, Set $B, Set $C)
  416. {
  417. // Given
  418. $A∩⟮B∪C⟯ = $A->intersect($B->union($C));
  419. $⟮A∩B⟯∪⟮A∩C⟯ = $A->intersect($B)->union($A->intersect($C));
  420. // Then
  421. $this->assertEquals($A∩⟮B∪C⟯, $⟮A∩B⟯∪⟮A∩C⟯);
  422. $this->assertEquals($A∩⟮B∪C⟯->asArray(), $⟮A∩B⟯∪⟮A∩C⟯->asArray());
  423. }
  424. /**
  425. * @test Axiom: A ∩ (A ∪ B) = A
  426. * Intersection absorbtion law
  427. *
  428. * @dataProvider dataProviderForTwoSets
  429. * @param Set $A
  430. * @param Set $B
  431. */
  432. public function testIntersectionAbsorbtion(Set $A, Set $B)
  433. {
  434. // Given
  435. $A∩⟮B∪C⟯ = $A->intersect($A->union($B));
  436. // Then
  437. $this->assertEquals($A, $A∩⟮B∪C⟯);
  438. $this->assertEquals($A->asArray(), $A∩⟮B∪C⟯->asArray());
  439. }
  440. /**
  441. * @test Axiom: (A ∩ B) ⊆ A
  442. * A intersect B is a subset of A
  443. *
  444. * @dataProvider dataProviderForTwoSets
  445. * @param Set $A
  446. * @param Set $B
  447. */
  448. public function testAIntersectionBIsSubsetOfA(Set $A, Set $B)
  449. {
  450. // Given
  451. $A∩B = $A->intersect($B);
  452. // Then
  453. $this->assertTrue($A∩B->isSubset($A));
  454. $this->assertTrue($A∩B->isSubset($B));
  455. }
  456. /**
  457. * @test Axiom: A ∩ A = A
  458. * A intersection A equals A
  459. *
  460. * @dataProvider dataProviderForSingleSet
  461. * @param Set $A
  462. */
  463. public function testAIntersectionAEqualsA(Set $A)
  464. {
  465. // Given
  466. $A∩A = $A->intersect($A);
  467. // Then
  468. $this->assertEquals($A, $A∩A);
  469. $this->assertEquals($A->asArray(), $A∩A->asArray());
  470. }
  471. /**
  472. * @test Axiom: A ∩ Ø = Ø
  473. * A union empty set is A
  474. *
  475. * @dataProvider dataProviderForSingleSet
  476. * @param Set $A
  477. */
  478. public function testAIntersectionEmptySetIsEmptySet(Set $A)
  479. {
  480. // Given
  481. $Ø = new Set();
  482. $A∩Ø = $A->intersect($Ø);
  483. // Then
  484. $this->assertEquals($Ø, $A∩Ø);
  485. $this->assertEquals($Ø->asArray(), $A∩Ø->asArray());
  486. }
  487. /**
  488. * @test Axiom: A ∖ B ≠ B ∖ A for A ≠ B
  489. * A diff B does not equal B diff A if A and B are different sets
  490. *
  491. * @dataProvider dataProviderForTwoSetsDifferent
  492. * @param Set $A
  493. * @param Set $B
  494. */
  495. public function testADiffBDifferentFromBDiffAWhenNotEqual(Set $A, Set $B)
  496. {
  497. // Given
  498. $A∖B = $A->difference($B);
  499. $B∖A = $B->difference($A);
  500. // Then
  501. $this->assertNotEquals($A∖B, $B∖A);
  502. $this->assertNotEquals($A∖B->asArray(), $B∖A->asArray());
  503. }
  504. public function dataProviderForTwoSetsDifferent(): array
  505. {
  506. return [
  507. [
  508. new Set([1]),
  509. new Set([]),
  510. ],
  511. [
  512. new Set([]),
  513. new Set([1]),
  514. ],
  515. [
  516. new Set([1]),
  517. new Set([2]),
  518. ],
  519. [
  520. new Set([2]),
  521. new Set([1]),
  522. ],
  523. [
  524. new Set([1]),
  525. new Set([2]),
  526. ],
  527. [
  528. new Set([2]),
  529. new Set([1]),
  530. ],
  531. [
  532. new Set([1, 2, 3, 'a', 'b']),
  533. new Set([1, 'a', 'k']),
  534. ],
  535. [
  536. new Set([1, 2, 3, 'a', 'b', new Set([1, 2])]),
  537. new Set([1, 'a', 'k']),
  538. ],
  539. [
  540. new Set([1, 2, 3, 'a', 'b']),
  541. new Set([1, 'a', 'k', new Set([1, 2])]),
  542. ],
  543. [
  544. new Set([1, 2, 3, 'a', 'b', new Set()]),
  545. new Set([1, 'a', 'k', new Set([1, 2])]),
  546. ],
  547. [
  548. new Set([1, 2, 3, 'a', 'b', new Set([1, 2])]),
  549. new Set([1, 'a', 'k', -2, '2.4', 3.5, new Set([1, 2])]),
  550. ],
  551. ];
  552. }
  553. /**
  554. * @test Axiom: A ∖ A = Ø
  555. * A diff itself is the empty set
  556. *
  557. * @dataProvider dataProviderForSingleSet
  558. * @param Set $A
  559. */
  560. public function testADiffItselfIsEmptySet(Set $A)
  561. {
  562. // Given
  563. $Ø = new Set();
  564. $A∖A = $A->difference($A);
  565. // Then
  566. $this->assertEquals($Ø, $A∖A);
  567. $this->assertEquals($Ø->asArray(), $A∖A->asArray());
  568. }
  569. /**
  570. * @test Axiom: A Δ B = (A ∖ B) ∪ (B ∖ A)
  571. * A symmetric different B equals union of A diff B and B diff A
  572. *
  573. * @dataProvider dataProviderForTwoSets
  574. * @param Set $A
  575. * @param Set $B
  576. */
  577. public function testASymmetricDifferentBEqualsUnionADiffBAndBDiffA(Set $A, Set $B)
  578. {
  579. // Given
  580. $AΔB = $A->symmetricDifference($B);
  581. $A∖B = $A->difference($B);
  582. $B∖A = $B->difference($A);
  583. $⟮A∖B⟯∪⟮B∖A⟯ = $A∖B->union($B∖A);
  584. // Then
  585. $this->assertEquals($AΔB, $⟮A∖B⟯∪⟮B∖A⟯);
  586. $this->assertEquals($AΔB->asArray(), $⟮A∖B⟯∪⟮B∖A⟯->asArray());
  587. }
  588. /**
  589. * @test Axiom: A × Ø = Ø
  590. * A cartesian product with empty set is the empty set
  591. *
  592. * @dataProvider dataProviderForSingleSet
  593. * @param Set $A
  594. */
  595. public function testACartesianProductWithEmptySetIsEmptySet(Set $A)
  596. {
  597. // Given
  598. $Ø = new Set();
  599. $Aר = $A->cartesianProduct($Ø);
  600. // Then
  601. $this->assertEquals($Ø, $Aר);
  602. }
  603. /**
  604. * @test Axiom: A × (B ∪ C) = (A × B) ∪ (A × C)
  605. * A cross union of B and C is the union of A cross B and A cross C
  606. *
  607. * @dataProvider dataProviderForThreeSets
  608. * @param Set $A
  609. * @param Set $B
  610. * @param Set $C
  611. */
  612. public function testACrossUnionBCEqualsACrossBUnionACrossC(Set $A, Set $B, Set $C)
  613. {
  614. // Given
  615. $A×⟮B∪C⟯ = $A->cartesianProduct($B->union($C));
  616. $⟮A×B⟯∪⟮A×C⟯ = $A->cartesianProduct($B)->union($A->cartesianProduct($C));
  617. // Then
  618. $this->assertEquals($A×⟮B∪C⟯, $⟮A×B⟯∪⟮A×C⟯);
  619. $this->assertEquals($A×⟮B∪C⟯->asArray(), $⟮A×B⟯∪⟮A×C⟯->asArray());
  620. }
  621. /**
  622. * @test Axiom: (A ∪ B) × C = (A × C) ∪ (B × C)
  623. * A union B cross C is the union of A cross C and B cross C
  624. *
  625. * @dataProvider dataProviderForThreeSets
  626. * @param Set $A
  627. * @param Set $B
  628. * @param Set $C
  629. */
  630. public function testAUnionBCrossCEqualsUnsionOfACRossCAndBCrossC(Set $A, Set $B, Set $C)
  631. {
  632. // Given
  633. $⟮A∪B⟯×C = $A->union($B)->cartesianProduct($C);
  634. $⟮A×C⟯∪⟮B×C⟯ = $A->cartesianProduct($C)->union($B->cartesianProduct($C));
  635. // Then
  636. $this->assertEquals($⟮A∪B⟯×C, $⟮A×C⟯∪⟮B×C⟯);
  637. $this->assertEquals($⟮A∪B⟯×C->asArray(), $⟮A×C⟯∪⟮B×C⟯->asArray());
  638. }
  639. /**
  640. * @test Axiom: |A × B| - |A| * |B|
  641. * The cardinality (count) of the cartesian product is the product of the cardinality of A and B
  642. *
  643. * @dataProvider dataProviderForTwoSets
  644. * @param Set $A
  645. * @param Set $B
  646. */
  647. public function testCardinalityOfCartesianProduct(Set $A, Set $B)
  648. {
  649. // Given
  650. $A×B = $A->cartesianProduct($B);
  651. // Then
  652. $this->assertEquals(count($A) * count($B), count($A×B));
  653. $this->assertEquals(count($A->asArray()) * count($B->asArray()), count($A×B->asArray()));
  654. }
  655. /**
  656. * @test Axiom: |S| = n, then |P(S)| = 2ⁿ
  657. * The cardinality (count) of a power set of S is 2ⁿ if the cardinality of S is n.
  658. *
  659. * @dataProvider dataProviderForSingleSet
  660. * @param Set $A
  661. */
  662. public function testCardinalityOfPowerSet(Set $A)
  663. {
  664. // Given
  665. $P⟮S⟯ = $A->powerSet();
  666. $n = count($A);
  667. // Then
  668. $this->assertEquals(\pow(2, $n), count($P⟮S⟯));
  669. $this->assertEquals(\pow(2, $n), count($P⟮S⟯->asArray()));
  670. }
  671. }