QuaternionAxiomsTest.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. <?php
  2. namespace MathPHP\Tests\Number;
  3. use MathPHP\Number\Quaternion;
  4. /**
  5. * Tests of quaternion number axioms
  6. * These tests don't test specific functions,
  7. * but rather quaternion number axioms which in term make use of multiple functions.
  8. * If all the quaternion number math is implemented properly, these tests should
  9. * all work out according to the axioms.
  10. *
  11. * Axioms tested:
  12. * - Identity
  13. * - z + 0 = z
  14. * - z * 1 = z
  15. * - Inverse
  16. * - (∀a)(∃b) a + b = 0
  17. */
  18. class QuaternionAxiomsTest extends \PHPUnit\Framework\TestCase
  19. {
  20. /**
  21. * @test Axiom: z + 0 = z
  22. * Additive identity
  23. * @dataProvider dataProviderForOneQuaternionNumber
  24. * @param int $r
  25. * @param int $i
  26. * @throws \Exception
  27. */
  28. public function testAdditiveIdentity(int $r, int $i, int $j, int $k)
  29. {
  30. // Given
  31. $z = new Quaternion($r, $i, $j, $k);
  32. // When
  33. $z+0 = $z->add(0);
  34. $this->assertTrue($z+0->equals($z));
  35. $this->assertTrue($z->equals($z+0));
  36. $this->assertEquals($z->r, $z+0->r);
  37. $this->assertEquals($z->i, $z+0->i);
  38. $this->assertEquals($z->j, $z+0->j);
  39. $this->assertEquals($z->k, $z+0->k);
  40. }
  41. /**
  42. * @test Axiom: z * 1 = z
  43. * Multiplicative identity
  44. * @dataProvider dataProviderForOneQuaternionNumber
  45. * @param int $r
  46. * @param int $i
  47. * @throws \Exception
  48. */
  49. public function testMultiplicativeIdentity(int $r, int $i, int $j, int $k)
  50. {
  51. // Given
  52. $z = new Quaternion($r, $i, $j, $k);
  53. // When
  54. $z1 = $z->multiply(1);
  55. $this->assertTrue($z1->equals($z));
  56. $this->assertTrue($z->equals($z1));
  57. $this->assertEquals($z->r, $z1->r);
  58. $this->assertEquals($z->i, $z1->i);
  59. $this->assertEquals($z->j, $z1->j);
  60. $this->assertEquals($z->k, $z1->k);
  61. }
  62. /**
  63. * @test Axiom: (∀a)(∃b) a + b = 0
  64. * Additive inverse.
  65. * @dataProvider dataProviderForOneQuaternionNumber
  66. * @param int $r
  67. * @param int $i
  68. * @throws \Exception
  69. */
  70. public function testAdditiveInverse(int $r, int $i, int $j, int $k)
  71. {
  72. // Given
  73. $a = new Quaternion($r, $i, $j, $k);
  74. $b = new Quaternion(-$r, -$i, -$j, -$k);
  75. // When
  76. $a+b = $a->add($b);
  77. $this->assertEquals(0, $a+b->r);
  78. $this->assertEquals(0, $a+b->i);
  79. $this->assertEquals(0, $a+b->j);
  80. $this->assertEquals(0, $a+b->k);
  81. }
  82. public function dataProviderForOneQuaternionNumber(): array
  83. {
  84. return [
  85. [0, 0, 0, 0],
  86. [0, 0, 0, 1],
  87. [0, 0, 1, 0],
  88. [0, 1, 0, 0],
  89. [1, 0, 0, 0],
  90. [1, 0, 0, 1],
  91. [1, 0, 1, 0],
  92. [1, 1, 0, 0],
  93. [1, 1, 0, 1],
  94. [1, 1, 1, 0],
  95. [1, 1, 1, 1],
  96. [2, 3, 4, 5],
  97. [4, 5, 3, 7],
  98. [7, 4, 5, 1],
  99. [-5, 2, 7, 2],
  100. [3, -6, -5, 3],
  101. [-3, -5, -2, -7],
  102. [4, 5, -6, -3],
  103. [3, 6, -4, 43],
  104. [12, 65, 32, -32],
  105. [54, -4, 43, -96],
  106. [-3, 34, 12, -4],
  107. ];
  108. }
  109. }