MatrixColumnOperationsTest.php 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  1. <?php
  2. namespace MathPHP\Tests\LinearAlgebra\Matrix\Numeric;
  3. use MathPHP\LinearAlgebra\MatrixFactory;
  4. use MathPHP\Exception;
  5. use MathPHP\LinearAlgebra\Vector;
  6. class MatrixColumnOperationsTest extends \PHPUnit\Framework\TestCase
  7. {
  8. /**
  9. * @test columnMultiply
  10. * @dataProvider dataProviderForColumnMultiply
  11. * @param array $A
  12. * @param int $nᵢ
  13. * @param float $k
  14. * @param array $expectedMatrix
  15. * @throws \Exception
  16. */
  17. public function testColumnMultiply(array $A, int $nᵢ, float $k, array $expectedMatrix)
  18. {
  19. // Given
  20. $A = MatrixFactory::create($A);
  21. $expectedMatrix = MatrixFactory::create($expectedMatrix);
  22. // When
  23. $R = $A->columnMultiply($nᵢ, $k);
  24. // Then
  25. $this->assertEqualsWithDelta($expectedMatrix, $R, 0.00001);
  26. }
  27. /**
  28. * @return array
  29. */
  30. public function dataProviderForColumnMultiply(): array
  31. {
  32. return [
  33. [
  34. [
  35. [1, 2, 3],
  36. [2, 3, 4],
  37. [3, 4, 5],
  38. ], 0, 5,
  39. [
  40. [5, 2, 3],
  41. [10, 3, 4],
  42. [15, 4, 5],
  43. ]
  44. ],
  45. [
  46. [
  47. [1, 2, 3],
  48. [2, 3, 4],
  49. [3, 4, 5],
  50. ], 1, 4,
  51. [
  52. [1, 8, 3],
  53. [2, 12, 4],
  54. [3, 16, 5],
  55. ]
  56. ],
  57. [
  58. [
  59. [1, 2, 3],
  60. [2, 3, 4],
  61. [3, 4, 5],
  62. ], 2, 8,
  63. [
  64. [1, 2, 24],
  65. [2, 3, 32],
  66. [3, 4, 40],
  67. ]
  68. ],
  69. [
  70. [
  71. [1, 2, 3],
  72. [2, 3, 4],
  73. [3, 4, 5],
  74. ], 0, 5.1,
  75. [
  76. [5.1, 2, 3],
  77. [10.2, 3, 4],
  78. [15.3, 4, 5],
  79. ]
  80. ],
  81. [
  82. [
  83. [1, 2, 3],
  84. [2, 3, 4],
  85. [3, 4, 5],
  86. ], 0, 0,
  87. [
  88. [0, 2, 3],
  89. [0, 3, 4],
  90. [0, 4, 5],
  91. ]
  92. ],
  93. ];
  94. }
  95. /**
  96. * @test columnMultiply column greater than n
  97. * @throws \Exception
  98. */
  99. public function testColumnMultiplyExceptionColumnGreaterThanN()
  100. {
  101. // Given
  102. $A = MatrixFactory::create([
  103. [1, 2, 3],
  104. [2, 3, 4],
  105. [3, 4, 5],
  106. ]);
  107. // Then
  108. $this->expectException(Exception\MatrixException::class);
  109. // When
  110. $A->columnMultiply(4, 5);
  111. }
  112. /**
  113. * @test columnAdd
  114. * @dataProvider dataProviderForColumnAdd
  115. * @param array $A
  116. * @param int $nᵢ
  117. * @param int $nⱼ
  118. * @param float $k
  119. * @param array $expectedMatrix
  120. * @throws \Exception
  121. */
  122. public function testColumnAdd(array $A, int $nᵢ, int $nⱼ, float $k, array $expectedMatrix)
  123. {
  124. // Given
  125. $A = MatrixFactory::create($A);
  126. $expectedMatrix = MatrixFactory::create($expectedMatrix);
  127. // When
  128. $R = $A->columnAdd($nᵢ, $nⱼ, $k);
  129. // Then
  130. $this->assertEqualsWithDelta($expectedMatrix, $R, 0.00001);
  131. }
  132. /**
  133. * @return array
  134. */
  135. public function dataProviderForColumnAdd(): array
  136. {
  137. return [
  138. [
  139. [
  140. [1, 2, 3],
  141. [2, 3, 4],
  142. [3, 4, 5],
  143. ], 0, 1, 2,
  144. [
  145. [1, 4, 3],
  146. [2, 7, 4],
  147. [3, 10, 5],
  148. ]
  149. ],
  150. [
  151. [
  152. [1, 2, 3],
  153. [2, 3, 4],
  154. [3, 4, 5],
  155. ], 1, 2, 3,
  156. [
  157. [1, 2, 9],
  158. [2, 3, 13],
  159. [3, 4, 17],
  160. ]
  161. ],
  162. [
  163. [
  164. [1, 2, 3],
  165. [2, 3, 4],
  166. [3, 4, 5],
  167. ], 0, 2, 4,
  168. [
  169. [1, 2, 7],
  170. [2, 3, 12],
  171. [3, 4, 17],
  172. ]
  173. ],
  174. [
  175. [
  176. [1, 2, 3],
  177. [2, 3, 4],
  178. [3, 4, 5],
  179. ], 0, 1, 2.2,
  180. [
  181. [1, 4.2, 3],
  182. [2, 7.4, 4],
  183. [3, 10.6, 5],
  184. ]
  185. ],
  186. ];
  187. }
  188. /**
  189. * @test columnAdd row greater than n
  190. * @throws \Exception
  191. */
  192. public function testColumnAddExceptionRowGreaterThanN()
  193. {
  194. // Given
  195. $A = MatrixFactory::create([
  196. [1, 2, 3],
  197. [2, 3, 4],
  198. [3, 4, 5],
  199. ]);
  200. // Then
  201. $this->expectException(Exception\MatrixException::class);
  202. // When
  203. $A->columnAdd(4, 5, 2);
  204. }
  205. /**
  206. * @test columnAdd k is zero
  207. * @throws \Exception
  208. */
  209. public function testColumnAddExceptionKIsZero()
  210. {
  211. // Given
  212. $A = MatrixFactory::create([
  213. [1, 2, 3],
  214. [2, 3, 4],
  215. [3, 4, 5],
  216. ]);
  217. // Then
  218. $this->expectException(Exception\BadParameterException::class);
  219. // When
  220. $A->columnAdd(1, 2, 0);
  221. }
  222. /**
  223. * @test columnAddVector
  224. * @dataProvider dataProviderForColumnAddVector
  225. * @param array $A
  226. * @param int $nᵢ
  227. * @param array $vector
  228. * @param array $expectedMatrix
  229. * @throws \Exception
  230. */
  231. public function testColumnAddVector(array $A, int $nᵢ, array $vector, array $expectedMatrix)
  232. {
  233. // Given
  234. $A = MatrixFactory::createNumeric($A);
  235. $V = new Vector($vector);
  236. $expectedMatrix = MatrixFactory::create($expectedMatrix);
  237. // When
  238. $R = $A->columnAddVector($nᵢ, $V);
  239. // Then
  240. $this->assertEquals($expectedMatrix, $R);
  241. }
  242. /**
  243. * @return array
  244. */
  245. public function dataProviderForColumnAddVector(): array
  246. {
  247. return [
  248. [
  249. [
  250. [1],
  251. ], 0, [2],
  252. [
  253. [3],
  254. ]
  255. ],
  256. [
  257. [
  258. [1],
  259. [2],
  260. ], 0, [2, 5],
  261. [
  262. [3],
  263. [7],
  264. ]
  265. ],
  266. [
  267. [
  268. [1, 2],
  269. ], 0, [2],
  270. [
  271. [3, 2],
  272. ]
  273. ],
  274. [
  275. [
  276. [1, 2],
  277. ], 1, [2],
  278. [
  279. [1, 4],
  280. ]
  281. ],
  282. [
  283. [
  284. [1, 2],
  285. [3, 4],
  286. ], 0, [2, 5],
  287. [
  288. [3, 2],
  289. [8, 4],
  290. ]
  291. ],
  292. [
  293. [
  294. [1, 2],
  295. [3, 4],
  296. ], 1, [2, 5],
  297. [
  298. [1, 4],
  299. [3, 9],
  300. ]
  301. ],
  302. [
  303. [
  304. [1, 2, 3],
  305. [2, 3, 4],
  306. [3, 4, 5],
  307. ], 0, [1,2,3],
  308. [
  309. [2, 2, 3],
  310. [4, 3, 4],
  311. [6, 4, 5],
  312. ]
  313. ],
  314. [
  315. [
  316. [1, 2, 3],
  317. [2, 3, 4],
  318. [3, 4, 5],
  319. ], 2, [6,9,12],
  320. [
  321. [1, 2, 9],
  322. [2, 3, 13],
  323. [3, 4, 17],
  324. ]
  325. ],
  326. [
  327. [
  328. [1, 2, 3],
  329. [2, 3, 4],
  330. [3, 4, 5],
  331. ], 2, [4,8,12],
  332. [
  333. [1, 2, 7],
  334. [2, 3, 12],
  335. [3, 4, 17],
  336. ]
  337. ],
  338. [
  339. [
  340. [1, 2, 3],
  341. [2, 3, 4],
  342. [3, 4, 5],
  343. ], 1, [2.2,3.3,4.4],
  344. [
  345. [1, 4.2, 3],
  346. [2, 6.3, 4],
  347. [3, 8.4, 5],
  348. ]
  349. ],
  350. ];
  351. }
  352. /**
  353. * @test columnAddVector test column n exists
  354. * @throws \Exception
  355. */
  356. public function testColumnAddVectorExceptionColumnExists()
  357. {
  358. // Given
  359. $A = MatrixFactory::createNumeric([
  360. [1, 2, 3],
  361. [2, 3, 4],
  362. [3, 4, 5],
  363. ]);
  364. $b = new Vector([1,2,3]);
  365. // Then
  366. $this->expectException(Exception\MatrixException::class);
  367. // When
  368. $A->columnAddVector(4, $b);
  369. }
  370. /**
  371. * @test columnAddVector test Vector->count() === matrix->m
  372. * @throws \Exception
  373. */
  374. public function testColumnAddVectorExceptionElementMismatch()
  375. {
  376. // Given
  377. $A = MatrixFactory::createNumeric([
  378. [1, 2, 3],
  379. [2, 3, 4],
  380. [3, 4, 5],
  381. ]);
  382. $b = new Vector([1,2,3,4]);
  383. // Then
  384. $this->expectException(Exception\BadParameterException::class);
  385. // When
  386. $A->columnAddVector(1, $b);
  387. }
  388. }