MatrixCatalog.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. <?php
  2. namespace MathPHP\LinearAlgebra;
  3. use MathPHP\Number\ObjectArithmetic;
  4. /**
  5. * @template T
  6. */
  7. class MatrixCatalog
  8. {
  9. /** @var Matrix<T> transpose */
  10. private $Aᵀ;
  11. /** @var Matrix<T> inverse */
  12. private $A⁻¹;
  13. /** @var Reduction\RowEchelonForm */
  14. private $REF;
  15. /** @var Reduction\ReducedRowEchelonForm */
  16. private $RREF;
  17. /** @var Decomposition\LU */
  18. private $LU;
  19. /** @var Decomposition\QR */
  20. private $QR;
  21. /** @var Decomposition\Cholesky */
  22. private $cholesky;
  23. /** @var Decomposition\Crout */
  24. private $crout;
  25. /** @var Decomposition\SVD */
  26. private $SVD;
  27. /** @var int|float|ObjectArithmetic determinant */
  28. private $det;
  29. /**************************************************************************
  30. * DERIVED MATRICES
  31. * - transpose
  32. * - inverse
  33. **************************************************************************/
  34. // TRANSPOSE
  35. /**
  36. * @param Matrix<T> $Aᵀ
  37. */
  38. public function addTranspose(Matrix $Aᵀ): void
  39. {
  40. $this->Aᵀ = $Aᵀ;
  41. }
  42. /**
  43. * @return bool
  44. */
  45. public function hasTranspose(): bool
  46. {
  47. // @phpstan-ignore-next-line
  48. return isset($this->Aᵀ);
  49. }
  50. /**
  51. * @return Matrix<T>
  52. */
  53. public function getTranspose(): Matrix
  54. {
  55. return $this->Aᵀ;
  56. }
  57. // INVERSE
  58. /**
  59. * @param Matrix<T> $A⁻¹
  60. */
  61. public function addInverse(Matrix $A⁻¹): void
  62. {
  63. $this->A⁻¹ = $A⁻¹;
  64. }
  65. /**
  66. * @return bool
  67. */
  68. public function hasInverse(): bool
  69. {
  70. // @phpstan-ignore-next-line
  71. return isset($this->A⁻¹);
  72. }
  73. /**
  74. * @return Matrix<T>
  75. */
  76. public function getInverse(): Matrix
  77. {
  78. return $this->A⁻¹;
  79. }
  80. /**************************************************************************
  81. * MATRIX REDUCTIONS
  82. * - ref (row echelon form)
  83. * - rref (reduced row echelon form)
  84. **************************************************************************/
  85. // ROW ECHELON FORM
  86. /**
  87. * @param Reduction\RowEchelonForm $REF
  88. */
  89. public function addRowEchelonForm(Reduction\RowEchelonForm $REF): void
  90. {
  91. $this->REF = $REF;
  92. }
  93. /**
  94. * @return bool
  95. */
  96. public function hasRowEchelonForm(): bool
  97. {
  98. // @phpstan-ignore-next-line
  99. return isset($this->REF);
  100. }
  101. /**
  102. * @return Reduction\RowEchelonForm
  103. */
  104. public function getRowEchelonForm(): Reduction\RowEchelonForm
  105. {
  106. return $this->REF;
  107. }
  108. // REDUCED ROW ECHELON FORM
  109. /**
  110. * @param Reduction\ReducedRowEchelonForm $RREF
  111. */
  112. public function addReducedRowEchelonForm(Reduction\ReducedRowEchelonForm $RREF): void
  113. {
  114. $this->RREF = $RREF;
  115. }
  116. /**
  117. * @return bool
  118. */
  119. public function hasReducedRowEchelonForm(): bool
  120. {
  121. // @phpstan-ignore-next-line
  122. return isset($this->RREF);
  123. }
  124. /**
  125. * @return Reduction\ReducedRowEchelonForm
  126. */
  127. public function getReducedRowEchelonForm(): Reduction\ReducedRowEchelonForm
  128. {
  129. return $this->RREF;
  130. }
  131. /**************************************************************************
  132. * MATRIX DECOMPOSITIONS
  133. * - LU decomposition
  134. * - QR decomposition
  135. * - Cholesky decomposition
  136. * - Crout decomposition
  137. * - SVD
  138. **************************************************************************/
  139. // LU DECOMPOSITION
  140. /**
  141. * @param Decomposition\LU $LU
  142. */
  143. public function addLuDecomposition(Decomposition\LU $LU): void
  144. {
  145. $this->LU = $LU;
  146. }
  147. /**
  148. * @return bool
  149. */
  150. public function hasLuDecomposition(): bool
  151. {
  152. // @phpstan-ignore-next-line
  153. return isset($this->LU);
  154. }
  155. /**
  156. * @return Decomposition\LU
  157. */
  158. public function getLuDecomposition(): Decomposition\LU
  159. {
  160. return $this->LU;
  161. }
  162. // QR DECOMPOSITION
  163. /**
  164. * @param Decomposition\QR $QR
  165. */
  166. public function addQrDecomposition(Decomposition\QR $QR): void
  167. {
  168. $this->QR = $QR;
  169. }
  170. /**
  171. * @return bool
  172. */
  173. public function hasQrDecomposition(): bool
  174. {
  175. // @phpstan-ignore-next-line
  176. return isset($this->QR);
  177. }
  178. /**
  179. * @return Decomposition\QR
  180. */
  181. public function getQrDecomposition(): Decomposition\QR
  182. {
  183. return $this->QR;
  184. }
  185. // CHOLESKY DECOMPOSITION
  186. /**
  187. * @param Decomposition\Cholesky $cholesky
  188. */
  189. public function addCholeskyDecomposition(Decomposition\Cholesky $cholesky): void
  190. {
  191. $this->cholesky = $cholesky;
  192. }
  193. /**
  194. * @return bool
  195. */
  196. public function hasCholeskyDecomposition(): bool
  197. {
  198. // @phpstan-ignore-next-line
  199. return isset($this->cholesky);
  200. }
  201. /**
  202. * @return Decomposition\Cholesky
  203. */
  204. public function getCholeskyDecomposition(): Decomposition\Cholesky
  205. {
  206. return $this->cholesky;
  207. }
  208. // CROUT DECOMPOSITION
  209. /**
  210. * @param Decomposition\Crout $crout
  211. */
  212. public function addCroutDecomposition(Decomposition\Crout $crout): void
  213. {
  214. $this->crout = $crout;
  215. }
  216. /**
  217. * @return bool
  218. */
  219. public function hasCroutDecomposition(): bool
  220. {
  221. // @phpstan-ignore-next-line
  222. return isset($this->crout);
  223. }
  224. /**
  225. * @return Decomposition\Crout
  226. */
  227. public function getCroutDecomposition(): Decomposition\Crout
  228. {
  229. return $this->crout;
  230. }
  231. // SVD
  232. /**
  233. * @param Decomposition\SVD $SVD
  234. */
  235. public function addSVD(Decomposition\SVD $SVD): void
  236. {
  237. $this->SVD = $SVD;
  238. }
  239. /**
  240. * @return bool
  241. */
  242. public function hasSVD(): bool
  243. {
  244. // @phpstan-ignore-next-line
  245. return isset($this->SVD);
  246. }
  247. /**
  248. * @return Decomposition\SVD
  249. */
  250. public function getSVD(): Decomposition\SVD
  251. {
  252. return $this->SVD;
  253. }
  254. /**************************************************************************
  255. * DERIVED DATA
  256. * - determinant
  257. **************************************************************************/
  258. // DETERMINANT
  259. /**
  260. * @param int|float|ObjectArithmetic $det
  261. */
  262. public function addDeterminant($det): void
  263. {
  264. $this->det = $det;
  265. }
  266. /**
  267. * @return bool
  268. */
  269. public function hasDeterminant(): bool
  270. {
  271. // @phpstan-ignore-next-line
  272. return isset($this->det);
  273. }
  274. /**
  275. * @return int|float|ObjectArithmetic
  276. */
  277. public function getDeterminant()
  278. {
  279. return $this->det;
  280. }
  281. }