ComplexMatrixAxiomsTest.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. <?php
  2. namespace MathPHP\Tests\LinearAlgebra\Matrix\Complex;
  3. use MathPHP\LinearAlgebra\ComplexMatrix;
  4. use MathPHP\Number\Complex;
  5. use MathPHP\Tests\LinearAlgebra\Fixture\MatrixDataProvider;
  6. /**
  7. * Complex Matrix Axioms
  8. * - Conjugate Transpose
  9. * - (A + B)ᴴ = Aᴴ + Bᴴ
  10. * - (zA)ᴴ = z‾Aᴴ
  11. * - (AB)ᴴ = BᴴAᴴ
  12. * - (Aᴴ)ᴴ = A
  13. * - det(Aᴴ) = ‾det(A)‾
  14. */
  15. class ComplexMatrixAxiomsTest extends \PHPUnit\Framework\TestCase
  16. {
  17. use MatrixDataProvider;
  18. /**
  19. * @test (A + B)ᴴ = Aᴴ + Bᴴ
  20. * A and B must be the same dimensions, where ᴴ is the conjugate transpose
  21. */
  22. public function testConjugateTransposeAddition()
  23. {
  24. // Given
  25. $A = new ComplexMatrix([
  26. [new Complex(1, 0), new Complex(-2, -1)],
  27. [new Complex(1, 1), new Complex(0, 1)],
  28. ]);
  29. $B = new ComplexMatrix([
  30. [new Complex(2, 2), new Complex(2, -1)],
  31. [new Complex(1, 4), new Complex(3, -2)],
  32. ]);
  33. // When
  34. $Aᴴ + Bᴴ = $A->conjugateTranspose()->add($B->conjugateTranspose());
  35. $⟮A + B⟯ᴴ = $A->add($B)->conjugateTranspose();
  36. // Then
  37. $this->assertEquals($⟮A + B⟯ᴴ->getMatrix(), $Aᴴ + Bᴴ->getMatrix());
  38. }
  39. /**
  40. * @test (zA)ᴴ = z‾Aᴴ
  41. * z is a complex number and z‾ is the conjugate
  42. */
  43. public function testConjugateTransposeScalarMultiplication()
  44. {
  45. // Given
  46. $A = new ComplexMatrix([
  47. [new Complex(1, 0), new Complex(-2, -1)],
  48. [new Complex(1, 1), new Complex(0, 1)],
  49. ]);
  50. $z = new Complex(3, -2);
  51. // When
  52. $⟮zA⟯ᴴ = $A->scalarMultiply($z)->conjugateTranspose();
  53. $z‾Aᴴ = $A->conjugateTranspose()->scalarMultiply($z->complexConjugate());
  54. // Then
  55. $this->assertEquals($⟮zA⟯ᴴ->getMatrix(), $z‾Aᴴ->getMatrix());
  56. }
  57. /**
  58. * @test (AB)ᴴ = BᴴAᴴ
  59. */
  60. public function testConjugateTransposeMultiplication()
  61. {
  62. // Given
  63. $A = new ComplexMatrix([
  64. [new Complex(1, 0), new Complex(-2, -1)],
  65. [new Complex(1, 1), new Complex(0, 1)],
  66. ]);
  67. $B = new ComplexMatrix([
  68. [new Complex(2, 2), new Complex(2, -1)],
  69. [new Complex(1, 4), new Complex(3, -2)],
  70. ]);
  71. // When
  72. $⟮AB⟯ᴴ = $A->multiply($B)->conjugateTranspose();
  73. $BᴴAᴴ = $B->conjugateTranspose()->multiply($A->conjugateTranspose());
  74. // Then
  75. $this->assertEquals($⟮AB⟯ᴴ->getMatrix(), $BᴴAᴴ->getMatrix());
  76. }
  77. /**
  78. * @test (Aᴴ)ᴴ = A
  79. * Hermitian transposition is an involution
  80. */
  81. public function testConjugateTransposeHermitianTranspositionIsanInvolution()
  82. {
  83. // Given
  84. $A = new ComplexMatrix([
  85. [new Complex(1, 0), new Complex(-2, -1)],
  86. [new Complex(1, 1), new Complex(0, 1)],
  87. ]);
  88. // When
  89. $⟮Aᴴ⟯ᴴ = $A->conjugateTranspose()->conjugateTranspose();
  90. // Then
  91. $this->assertEquals($⟮Aᴴ⟯ᴴ->getMatrix(), $A->getMatrix());
  92. }
  93. /**
  94. * @test det(Aᴴ) = ‾det(A)‾
  95. * If A is a square matrix, then determinant of conjugate transpose is the same as the complex conjugate of the determinant
  96. *
  97. * @dataProvider dataProviderForComplexSquareObjectMatrix
  98. * @array $A
  99. */
  100. public function testConjugateTransposeDeterminant(array $A)
  101. {
  102. // Given
  103. $A = new ComplexMatrix($A);
  104. // When
  105. $det⟮Aᴴ⟯ = $A->conjugateTranspose()->det();
  106. $‾det⟮A⟯‾ = $A->det()->complexConjugate();
  107. // Then
  108. $this->assertEquals($‾det⟮A⟯‾, $det⟮Aᴴ⟯);
  109. }
  110. /**
  111. * @test tr(Aᴴ) = ‾tr(A)‾
  112. * If A is a square matrix, then trace of conjugate transpose is the same as the complex conjugate of the trace
  113. *
  114. * @dataProvider dataProviderForComplexSquareObjectMatrix
  115. * @array $A
  116. */
  117. public function testConjugateTransposeTrace(array $A)
  118. {
  119. // Given
  120. $A = new ComplexMatrix($A);
  121. // When
  122. $tr⟮Aᴴ⟯ = $A->conjugateTranspose()->trace();
  123. $‾tr⟮A⟯‾ = $A->trace()->complexConjugate();
  124. // Then
  125. $this->assertEquals($‾tr⟮A⟯‾, $tr⟮Aᴴ⟯);
  126. }
  127. }