MatrixGettersTest.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519
  1. <?php
  2. namespace MathPHP\Tests\LinearAlgebra\Matrix\Base;
  3. use MathPHP\LinearAlgebra\MatrixFactory;
  4. use MathPHP\LinearAlgebra\NumericMatrix;
  5. use MathPHP\LinearAlgebra\Vector;
  6. use MathPHP\Exception;
  7. class MatrixGettersTest extends \PHPUnit\Framework\TestCase
  8. {
  9. /** @var array */
  10. private $A;
  11. /** @var NumericMatrix */
  12. private $matrix;
  13. /**
  14. * @throws \Exception
  15. */
  16. public function setUp(): void
  17. {
  18. $this->A = [
  19. [1, 2, 3],
  20. [2, 3, 4],
  21. [4, 5, 6],
  22. ];
  23. $this->matrix = MatrixFactory::create($this->A);
  24. }
  25. /**
  26. * @test getMatrix returns the expected array representation of the matrix
  27. * @throws \Exception
  28. */
  29. public function testGetMatrix()
  30. {
  31. $this->assertEquals($this->A, $this->matrix->getMatrix());
  32. }
  33. /**
  34. * @test getM returns the number of rows
  35. * @dataProvider dataProviderForGetM
  36. * @param array $A
  37. * @param int $m
  38. * @throws \Exception
  39. */
  40. public function testGetM(array $A, int $m)
  41. {
  42. // Given
  43. $matrix = MatrixFactory::create($A);
  44. // Then
  45. $this->assertEquals($m, $matrix->getM());
  46. }
  47. public function dataProviderForGetM(): array
  48. {
  49. return [
  50. [
  51. [[1]], 1
  52. ],
  53. [
  54. [
  55. [1, 2],
  56. [2, 3],
  57. ], 2
  58. ],
  59. [
  60. [
  61. [1, 2],
  62. [2, 3],
  63. [3, 4],
  64. ], 3
  65. ],
  66. [
  67. [
  68. [1, 2],
  69. [2, 3],
  70. [3, 4],
  71. [4, 5],
  72. ], 4
  73. ],
  74. [
  75. [
  76. [1, 2, 0],
  77. [2, 3, 0],
  78. [3, 4, 0],
  79. [4, 5, 0],
  80. ], 4
  81. ],
  82. ];
  83. }
  84. /**
  85. * @test getN returns the number of columns
  86. * @dataProvider dataProviderForGetN
  87. * @param array $A
  88. * @param int $n
  89. * @throws \Exception
  90. */
  91. public function testGetN(array $A, int $n)
  92. {
  93. // Given
  94. $matrix = MatrixFactory::create($A);
  95. // Then
  96. $this->assertEquals($n, $matrix->getN());
  97. }
  98. public function dataProviderForGetN(): array
  99. {
  100. return [
  101. [
  102. [[1]], 1
  103. ],
  104. [
  105. [
  106. [1, 2],
  107. [2, 3],
  108. ], 2
  109. ],
  110. [
  111. [
  112. [1, 2],
  113. [2, 3],
  114. [3, 4],
  115. ], 2
  116. ],
  117. [
  118. [
  119. [1, 2],
  120. [2, 3],
  121. [3, 4],
  122. [4, 5],
  123. ], 2
  124. ],
  125. [
  126. [
  127. [1, 2, 0],
  128. [2, 3, 0],
  129. [3, 4, 0],
  130. [4, 5, 0],
  131. ], 3
  132. ],
  133. ];
  134. }
  135. /**
  136. * @test getRow returns the expected row as an array
  137. * @throws \Exception
  138. */
  139. public function testGetRow()
  140. {
  141. // Given
  142. $A = [
  143. [1, 2, 3],
  144. [2, 3, 4],
  145. [4, 5, 6],
  146. ];
  147. $matrix = MatrixFactory::create($A);
  148. // Then
  149. $this->assertEquals([1, 2, 3], $matrix->getRow(0));
  150. $this->assertEquals([2, 3, 4], $matrix->getRow(1));
  151. $this->assertEquals([4, 5, 6], $matrix->getRow(2));
  152. }
  153. /**
  154. * @test getRow throws Exception\MatrixException if the row does not exist
  155. * @throws \Exception
  156. */
  157. public function testGetRowException()
  158. {
  159. // Then
  160. $this->expectException(Exception\MatrixException::class);
  161. // When
  162. $this->matrix->getRow(8);
  163. }
  164. /**
  165. * @test getColumn returns the expected column as an array
  166. * @throws \Exception
  167. */
  168. public function testGetColumn()
  169. {
  170. // Given
  171. $A = [
  172. [1, 2, 3],
  173. [2, 3, 4],
  174. [4, 5, 6],
  175. ];
  176. $matrix = MatrixFactory::create($A);
  177. // Then
  178. $this->assertEquals([1, 2, 4], $matrix->getColumn(0));
  179. $this->assertEquals([2, 3, 5], $matrix->getColumn(1));
  180. $this->assertEquals([3, 4, 6], $matrix->getColumn(2));
  181. }
  182. /**
  183. * @test getColumn throws Exception\MatrixException if the column does not exist
  184. * @throws \Exception
  185. */
  186. public function testGetColumnException()
  187. {
  188. // Then
  189. $this->expectException(Exception\MatrixException::class);
  190. // When
  191. $this->matrix->getColumn(8);
  192. }
  193. /**
  194. * @test get returns the expected element as a scalar
  195. * @throws \Exception
  196. */
  197. public function testGet()
  198. {
  199. // Given
  200. $A = [
  201. [1, 2, 3],
  202. [2, 3, 4],
  203. [4, 5, 6],
  204. ];
  205. $matrix = MatrixFactory::create($A);
  206. // Then
  207. $this->assertEquals(1, $matrix->get(0, 0));
  208. $this->assertEquals(2, $matrix->get(0, 1));
  209. $this->assertEquals(3, $matrix->get(0, 2));
  210. $this->assertEquals(2, $matrix->get(1, 0));
  211. $this->assertEquals(3, $matrix->get(1, 1));
  212. $this->assertEquals(4, $matrix->get(1, 2));
  213. $this->assertEquals(4, $matrix->get(2, 0));
  214. $this->assertEquals(5, $matrix->get(2, 1));
  215. $this->assertEquals(6, $matrix->get(2, 2));
  216. }
  217. /**
  218. * @test get throws Exception\MatrixException if the row does not exist
  219. * @throws \Exception
  220. */
  221. public function testGetExceptionRow()
  222. {
  223. // Then
  224. $this->expectException(Exception\MatrixException::class);
  225. // When
  226. $this->matrix->get(8, 1);
  227. }
  228. /**
  229. * @test get throws Exception\MatrixException if the column does not exist
  230. * @throws \Exception
  231. */
  232. public function testGetExceptionColumn()
  233. {
  234. // Then
  235. $this->expectException(Exception\MatrixException::class);
  236. // When
  237. $this->matrix->get(1, 8);
  238. }
  239. /**
  240. * @test asVectors returns the matrix represented as an array of Vector objects
  241. * @throws \Exception
  242. */
  243. public function testAsVectors()
  244. {
  245. // Given
  246. $A = new NumericMatrix([
  247. [1, 2, 3],
  248. [4, 5, 6],
  249. [7, 8, 9],
  250. ]);
  251. $expected = [
  252. new Vector([1, 4, 7]),
  253. new Vector([2, 5, 8]),
  254. new Vector([3, 6, 9]),
  255. ];
  256. // Then
  257. $this->assertEquals($expected, $A->asVectors());
  258. }
  259. /**
  260. * @test asRowVectors returns the matrix represented as an array of Vector objects
  261. * @throws \Exception
  262. */
  263. public function testAsRowVectors()
  264. {
  265. // Given
  266. $A = new NumericMatrix([
  267. [1, 2, 3],
  268. [4, 5, 6],
  269. [7, 8, 9],
  270. ]);
  271. $expected = [
  272. new Vector([1, 2, 3]),
  273. new Vector([4, 5, 6]),
  274. new Vector([7, 8, 9]),
  275. ];
  276. // Then
  277. $this->assertEquals($expected, $A->asRowVectors());
  278. }
  279. /**
  280. * @test getDiagonalElements
  281. * @dataProvider dataProviderForGetDiagonalElements
  282. * @param array $A
  283. * @param array $R
  284. * @throws \Exception
  285. */
  286. public function testGetDiagonalElements(array $A, array $R)
  287. {
  288. // Given
  289. $A = MatrixFactory::create($A);
  290. // Then
  291. $this->assertEquals($R, $A->getDiagonalElements());
  292. }
  293. public function dataProviderForGetDiagonalElements(): array
  294. {
  295. return [
  296. [
  297. [
  298. [1, 2]
  299. ],
  300. [1],
  301. ],
  302. [
  303. [
  304. [1],
  305. [2],
  306. ],
  307. [1],
  308. ],
  309. [
  310. [[1]],
  311. [1],
  312. ],
  313. [
  314. [
  315. [1, 2],
  316. [2, 3],
  317. ],
  318. [1, 3],
  319. ],
  320. [
  321. [
  322. [1, 2, 3],
  323. [2, 3, 4],
  324. [3, 4, 5],
  325. ],
  326. [1, 3, 5],
  327. ],
  328. [
  329. [
  330. [1, 2, 3, 4],
  331. [2, 3, 4, 5],
  332. [3, 4, 5, 6],
  333. [4, 5, 6, 7],
  334. ],
  335. [1, 3, 5, 7],
  336. ],
  337. [
  338. [
  339. [1, 2, 3, 4],
  340. [2, 3, 4, 5],
  341. [3, 4, 5, 6],
  342. ],
  343. [1, 3, 5],
  344. ],
  345. [
  346. [
  347. [1, 2, 3],
  348. [2, 3, 4],
  349. [3, 4, 5],
  350. [4, 5, 6],
  351. ],
  352. [1, 3, 5],
  353. ],
  354. ];
  355. }
  356. /**
  357. * @test getSuperdiagonalElements
  358. * @dataProvider dataProviderForGetSuperdiagonalElements
  359. * @param array $A
  360. * @param array $R
  361. * @throws \Exception
  362. */
  363. public function testGetSuperdiagonalElements(array $A, array $R)
  364. {
  365. // Given
  366. $A = MatrixFactory::create($A);
  367. // Then
  368. $this->assertEquals($R, $A->getSuperdiagonalElements());
  369. }
  370. public function dataProviderForGetSuperdiagonalElements(): array
  371. {
  372. return [
  373. [
  374. [
  375. [1, 2]
  376. ],
  377. [],
  378. ],
  379. [
  380. [
  381. [1],
  382. [2],
  383. ],
  384. [],
  385. ],
  386. [
  387. [[1]],
  388. [],
  389. ],
  390. [
  391. [
  392. [1, 2],
  393. [4, 3],
  394. ],
  395. [2],
  396. ],
  397. [
  398. [
  399. [1, 2, 3],
  400. [2, 3, 4],
  401. [4, 5, 6],
  402. ],
  403. [2, 4],
  404. ],
  405. [
  406. [
  407. [1, 2, 3, 4],
  408. [2, 3, 4, 5],
  409. [4, 5, 6, 7],
  410. [3, 4, 5, 6],
  411. ],
  412. [2, 4, 7],
  413. ],
  414. ];
  415. }
  416. /**
  417. * @test getSubdiagonalElements
  418. * @dataProvider dataProviderForGetSubdiagonalElements
  419. * @param array $A
  420. * @param array $R
  421. * @throws \Exception
  422. */
  423. public function testGetSubdiagonalElements(array $A, array $R)
  424. {
  425. // Given
  426. $A = MatrixFactory::create($A);
  427. // Then
  428. $this->assertEquals($R, $A->getSubdiagonalElements());
  429. }
  430. public function dataProviderForGetSubdiagonalElements(): array
  431. {
  432. return [
  433. [
  434. [
  435. [1, 2]
  436. ],
  437. [],
  438. ],
  439. [
  440. [
  441. [1],
  442. [2],
  443. ],
  444. [],
  445. ],
  446. [
  447. [[1]],
  448. [],
  449. ],
  450. [
  451. [
  452. [1, 2],
  453. [4, 3],
  454. ],
  455. [4],
  456. ],
  457. [
  458. [
  459. [1, 2, 3],
  460. [2, 3, 4],
  461. [4, 5, 6],
  462. ],
  463. [2, 5],
  464. ],
  465. [
  466. [
  467. [1, 2, 3, 4],
  468. [2, 3, 4, 5],
  469. [4, 5, 6, 7],
  470. [3, 4, 5, 6],
  471. ],
  472. [2, 5, 5],
  473. ],
  474. ];
  475. }
  476. }