ComplexAxiomsTest.php 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. <?php
  2. namespace MathPHP\Tests\Number;
  3. use MathPHP\Number\Complex;
  4. /**
  5. * Tests of complex number axioms
  6. * These tests don't test specific functions,
  7. * but rather complex number axioms which in term make use of multiple functions.
  8. * If all the complex number math is implemented properly, these tests should
  9. * all work out according to the axioms.
  10. *
  11. * Axioms tested:
  12. * - Commutativity
  13. * - z + w = w + z
  14. * - zw = wz
  15. * - Associativity
  16. * - z + (u + v) = (z + u) + v
  17. * - z(uv) = (zu)v
  18. * - Distributed Law
  19. * - z(u + v) = zu + zv
  20. * - Identity
  21. * - z + 0 = z
  22. * - z * 1 = z
  23. * - Inverse
  24. * - (∀a)(∃b) a + b = 0
  25. */
  26. class ComplexAxiomsTest extends \PHPUnit\Framework\TestCase
  27. {
  28. /**
  29. * @test Axiom: z + w = w + z
  30. * Commutativity of addition.
  31. * @dataProvider dataProviderForTwoComplexNumbers
  32. * @param int $r₁
  33. * @param int $i₁
  34. * @param int $r₂
  35. * @param int $i₂
  36. * @throws \Exception
  37. */
  38. public function testCommutativityOfAddition(int $r₁, int $i₁, int $r₂, int $i₂)
  39. {
  40. // Given
  41. $z = new Complex($r₁, $i₁);
  42. $w = new Complex($r₂, $i₂);
  43. // When
  44. $z+w = $z->add($w);
  45. $w+z = $w->add($z);
  46. $this->assertTrue($z+w->equals($w+z));
  47. $this->assertTrue($w+z->equals($z+w));
  48. $this->assertEquals($z+w->r, $w+z->r);
  49. $this->assertEquals($z+w->i, $w+z->i);
  50. }
  51. /**
  52. * @test Axiom: zw = wz
  53. * Commutativity of multiplication.
  54. * @dataProvider dataProviderForTwoComplexNumbers
  55. * @param int $r₁
  56. * @param int $i₁
  57. * @param int $r₂
  58. * @param int $i₂
  59. * @throws \Exception
  60. */
  61. public function testCommutativityOfMultiplication(int $r₁, int $i₁, int $r₂, int $i₂)
  62. {
  63. // Given
  64. $z = new Complex($r₁, $i₁);
  65. $w = new Complex($r₂, $i₂);
  66. // When
  67. $zw = $z->multiply($w);
  68. $wz = $w->multiply($z);
  69. $this->assertTrue($zw->equals($wz));
  70. $this->assertTrue($wz->equals($zw));
  71. $this->assertEquals($zw->r, $wz->r);
  72. $this->assertEquals($zw->i, $wz->i);
  73. }
  74. /**
  75. * @test Axiom: z + (u + v) = (z + u) + v
  76. * Associativity of Addition.
  77. * @dataProvider dataProviderForThreeComplexNumbers
  78. * @param int $r₁
  79. * @param int $i₁
  80. * @param int $r₂
  81. * @param int $i₂
  82. * @param int $r₃
  83. * @param int $i₃
  84. * @throws \Exception
  85. */
  86. public function testAssociativityOfAddition(int $r₁, int $i₁, int $r₂, int $i₂, int $r₃, int $i₃)
  87. {
  88. // Given
  89. $z = new Complex($r₁, $i₁);
  90. $u = new Complex($r₂, $i₂);
  91. $v = new Complex($r₃, $i₃);
  92. // When
  93. $z⟮u + v⟯ = $z->add($u->add($v));
  94. $⟮z + u⟯v = $z->add($u)->add($v);
  95. $this->assertTrue($z⟮u + v⟯->equals($⟮z + u⟯v));
  96. $this->assertTrue($⟮z + u⟯v->equals($z⟮u + v⟯));
  97. $this->assertEquals($z⟮u + v⟯->r, $⟮z + u⟯v->r);
  98. $this->assertEquals($z⟮u + v⟯->i, $⟮z + u⟯v->i);
  99. }
  100. /**
  101. * @test Axiom: z(uv) = (zu)v
  102. * Associativity of Multiplication.
  103. * @dataProvider dataProviderForThreeComplexNumbers
  104. * @param int $r₁
  105. * @param int $i₁
  106. * @param int $r₂
  107. * @param int $i₂
  108. * @param int $r₃
  109. * @param int $i₃
  110. * @throws \Exception
  111. */
  112. public function testAssociativityOfMultiplication(int $r₁, int $i₁, int $r₂, int $i₂, int $r₃, int $i₃)
  113. {
  114. // Given
  115. $z = new Complex($r₁, $i₁);
  116. $u = new Complex($r₂, $i₂);
  117. $v = new Complex($r₃, $i₃);
  118. // When
  119. $z⟮uv⟯ = $z->multiply($u->multiply($v));
  120. $⟮zu⟯v = $z->multiply($u)->multiply($v);
  121. $this->assertTrue($z⟮uv⟯->equals($⟮zu⟯v));
  122. $this->assertTrue($⟮zu⟯v->equals($z⟮uv⟯));
  123. $this->assertEquals($z⟮uv⟯->r, $⟮zu⟯v->r);
  124. $this->assertEquals($z⟮uv⟯->i, $⟮zu⟯v->i);
  125. }
  126. /**
  127. * @test Axiom: z(u + v) = zu + zv
  128. * Distributed Law.
  129. * @dataProvider dataProviderForThreeComplexNumbers
  130. * @param int $r₁
  131. * @param int $i₁
  132. * @param int $r₂
  133. * @param int $i₂
  134. * @param int $r₃
  135. * @param int $i₃
  136. * @throws \Exception
  137. */
  138. public function testDistributedLaw(int $r₁, int $i₁, int $r₂, int $i₂, int $r₃, int $i₃)
  139. {
  140. // Given
  141. $z = new Complex($r₁, $i₁);
  142. $u = new Complex($r₂, $i₂);
  143. $v = new Complex($r₃, $i₃);
  144. // When
  145. $z⟮u + v⟯ = $z->multiply($u->add($v));
  146. $zu + zv = $z->multiply($u)->add($z->multiply($v));
  147. $this->assertTrue($z⟮u + v⟯->equals($zu + zv));
  148. $this->assertTrue($zu + zv->equals($z⟮u + v⟯));
  149. $this->assertEquals($z⟮u + v⟯->r, $zu + zv->r);
  150. $this->assertEquals($z⟮u + v⟯->i, $zu + zv->i);
  151. }
  152. /**
  153. * @test Axiom: z + 0 = z
  154. * Additive identity
  155. * @dataProvider dataProviderForOneComplexNumber
  156. * @param int $r
  157. * @param int $i
  158. * @throws \Exception
  159. */
  160. public function testAdditiveIdentity(int $r, int $i)
  161. {
  162. // Given
  163. $z = new Complex($r, $i);
  164. // When
  165. $z+0 = $z->add(0);
  166. $this->assertTrue($z+0->equals($z));
  167. $this->assertTrue($z->equals($z+0));
  168. $this->assertEquals($z->r, $z+0->r);
  169. $this->assertEquals($z->i, $z+0->i);
  170. }
  171. /**
  172. * @test Axiom: z * 1 = z
  173. * Multiplicative identity
  174. * @dataProvider dataProviderForOneComplexNumber
  175. * @param int $r
  176. * @param int $i
  177. * @throws \Exception
  178. */
  179. public function testMultiplicativeIdentity(int $r, int $i)
  180. {
  181. // Given
  182. $z = new Complex($r, $i);
  183. // When
  184. $z1 = $z->multiply(1);
  185. $this->assertTrue($z1->equals($z));
  186. $this->assertTrue($z->equals($z1));
  187. $this->assertEquals($z->r, $z1->r);
  188. $this->assertEquals($z->i, $z1->i);
  189. }
  190. /**
  191. * @test Axiom: (∀a)(∃b) a + b = 0
  192. * Additive inverse.
  193. * @dataProvider dataProviderForOneComplexNumber
  194. * @param int $r
  195. * @param int $i
  196. * @throws \Exception
  197. */
  198. public function testAdditiveInverse(int $r, int $i)
  199. {
  200. // Given
  201. $a = new Complex($r, $i);
  202. $b = new Complex(-$r, -$i);
  203. // When
  204. $a+b = $a->add($b);
  205. $this->assertEquals(0, $a+b->r);
  206. $this->assertEquals(0, $a+b->i);
  207. }
  208. public function dataProviderForOneComplexNumber(): array
  209. {
  210. return [
  211. [0, 0],
  212. [0, 0],
  213. [0, 0],
  214. [0, 1],
  215. [1, 0],
  216. [1, 0],
  217. [1, 0],
  218. [1, 1],
  219. [1, 1],
  220. [1, 1],
  221. [1, 1],
  222. [2, 3],
  223. [4, 5],
  224. [7, 4],
  225. [-5, 2],
  226. [3, -6],
  227. [-3, -5],
  228. [4, 5],
  229. [3, 6],
  230. [12, 65],
  231. [54, -4],
  232. [-3, 34],
  233. ];
  234. }
  235. public function dataProviderForTwoComplexNumbers(): array
  236. {
  237. return [
  238. [0, 0, 0, 0],
  239. [0, 0, 0, 1],
  240. [0, 0, 1, 0],
  241. [0, 1, 0, 0],
  242. [1, 0, 0, 0],
  243. [1, 0, 0, 1],
  244. [1, 0, 1, 0],
  245. [1, 1, 0, 0],
  246. [1, 1, 0, 1],
  247. [1, 1, 1, 0],
  248. [1, 1, 1, 1],
  249. [2, 3, 4, 5],
  250. [4, 5, 3, 7],
  251. [7, 4, 5, 1],
  252. [-5, 2, 7, 2],
  253. [3, -6, -5, 3],
  254. [-3, -5, -2, -7],
  255. [4, 5, -6, -3],
  256. [3, 6, -4, 43],
  257. [12, 65, 32, -32],
  258. [54, -4, 43, -96],
  259. [-3, 34, 12, -4],
  260. ];
  261. }
  262. public function dataProviderForThreeComplexNumbers(): array
  263. {
  264. return [
  265. [0, 0, 0, 0, 0, 0],
  266. [0, 0, 0, 1, 0, 0],
  267. [0, 0, 1, 0, 0, 0],
  268. [0, 1, 0, 0, 0, 0],
  269. [1, 0, 0, 0, 1, 0],
  270. [1, 0, 0, 1, 0, 1],
  271. [1, 0, 1, 0, 0, 0],
  272. [1, 1, 0, 0, 0, 0],
  273. [1, 1, 0, 1, 1, 1],
  274. [1, 1, 1, 0, 0, 0],
  275. [1, 1, 1, 1, 0, 0],
  276. [2, 3, 4, 5, 0, 0],
  277. [4, 5, 3, 7, 0, 1],
  278. [7, 4, 5, 1, 1, 0],
  279. [-5, 2, 7, 2, 5, 3],
  280. [3, -6, -5, 3, 5, 3],
  281. [-3, -5, -2, -7, 5, 3],
  282. [4, 5, -6, -3, 5, 3],
  283. [3, 6, -4, 43, 5, 3],
  284. [12, 65, 32, -32, 5, 3],
  285. [54, -4, 43, -96, 5, 3],
  286. [-3, 34, 12, -4, 5, 3],
  287. [1, 2, 3, 4, 5, 6],
  288. [6, 5, 4, 3, 2, 1],
  289. [1, -2, 3, -4, 5, -6],
  290. [-6, 5, -4, 3, -2, 1],
  291. [345, 765, 235, 123, 765, 456],
  292. ];
  293. }
  294. }