EigenvalueTest.php 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867
  1. <?php
  2. namespace MathPHP\Tests\LinearAlgebra\Eigen;
  3. use MathPHP\Exception;
  4. use MathPHP\LinearAlgebra\MatrixFactory;
  5. use MathPHP\LinearAlgebra\Eigenvalue;
  6. class EigenvalueTest extends \PHPUnit\Framework\TestCase
  7. {
  8. /**
  9. * @test closedFormPolynomialRootMethod returns the expected eigenvalues
  10. * @dataProvider dataProviderForEigenvalues
  11. * @param array $A
  12. * @param array $S
  13. * @throws \Exception
  14. */
  15. public function testClosedFormPolynomialRootMethod(array $A, array $S)
  16. {
  17. // Given
  18. $A = MatrixFactory::create($A);
  19. // When
  20. $eigenvalues = Eigenvalue::closedFormPolynomialRootMethod($A);
  21. // Then
  22. $this->assertEqualsWithDelta($S, $eigenvalues, 0.0001);
  23. $this->assertEqualsWithDelta($S, $A->eigenvalues(Eigenvalue::CLOSED_FORM_POLYNOMIAL_ROOT_METHOD), 0.0001);
  24. }
  25. /**
  26. * @test Matrix eigenvalues using closedFormPolynomialRootMethod returns the expected eigenvalues
  27. * @dataProvider dataProviderForEigenvalues
  28. * @param array $A
  29. * @param array $S
  30. * @throws \Exception
  31. */
  32. public function testClosedFormPolynomialRootMethodViaMatrix(array $A, array $S)
  33. {
  34. // Given
  35. $A = MatrixFactory::create($A);
  36. // When
  37. $eigenvalues = $A->eigenvalues(Eigenvalue::CLOSED_FORM_POLYNOMIAL_ROOT_METHOD);
  38. // Then
  39. $this->assertEqualsWithDelta($S, $eigenvalues, 0.0001);
  40. }
  41. /**
  42. * @test jacobiMethod returns the expected eigenvalues
  43. * @dataProvider dataProviderForSymmetricEigenvalues
  44. * @dataProvider dataProviderForSymmetricEigenvalueEdgeCases
  45. * @param array $A
  46. * @param array $S
  47. * @throws \Exception
  48. */
  49. public function testJacobiMethod(array $A, array $S)
  50. {
  51. // Given
  52. $A = MatrixFactory::create($A);
  53. // When
  54. $eigenvalues = Eigenvalue::jacobiMethod($A);
  55. // Then
  56. $this->assertEqualsWithDelta($S, $eigenvalues, 0.0001);
  57. }
  58. /**
  59. * @test Matrix eigenvalues using jacobiMethod returns the expected eigenvalues
  60. * @dataProvider dataProviderForSymmetricEigenvalues
  61. * @dataProvider dataProviderForSymmetricEigenvalueEdgeCases
  62. * @param array $A
  63. * @param array $S
  64. * @throws \Exception
  65. */
  66. public function testJacobiMethodViaMatrix(array $A, array $S)
  67. {
  68. // Given
  69. $A = MatrixFactory::create($A);
  70. // When
  71. $eigenvalues = $A->eigenvalues(Eigenvalue::JACOBI_METHOD);
  72. // Then
  73. $this->assertEqualsWithDelta($S, $eigenvalues, 0.0001);
  74. }
  75. /**
  76. * @test powerIterationMethod returns the expected eigenvalues
  77. * @dataProvider dataProviderForEigenvalues
  78. * @dataProvider dataProviderForLargeMatrixEigenvalues
  79. * @dataProvider dataProviderForSymmetricEigenvalues
  80. * @param array $A
  81. * @param array $S
  82. * @param float $max_abs_eigenvalue maximum absolute eigenvalue
  83. * @throws \Exception
  84. */
  85. public function testPowerIteration(array $A, array $S, float $max_abs_eigenvalue)
  86. {
  87. // Given
  88. $A = MatrixFactory::create($A);
  89. // When
  90. $eigenvalues = Eigenvalue::powerIteration($A);
  91. // Then
  92. $this->assertEqualsWithDelta([$max_abs_eigenvalue], $eigenvalues, 0.0001);
  93. }
  94. /**
  95. * @test Matrix eigenvalues using powerIterationMethod returns the expected eigenvalues
  96. * @dataProvider dataProviderForEigenvalues
  97. * @dataProvider dataProviderForLargeMatrixEigenvalues
  98. * @dataProvider dataProviderForSymmetricEigenvalues
  99. * @param array $A
  100. * @param array $S
  101. * @param float $max_abs_eigenvalue maximum absolute eigenvalue
  102. * @throws \Exception
  103. */
  104. public function testPowerIterationViaMatrix(array $A, array $S, float $max_abs_eigenvalue)
  105. {
  106. // Given
  107. $A = MatrixFactory::create($A);
  108. // When
  109. $eigenvalues = $A->eigenvalues(Eigenvalue::POWER_ITERATION);
  110. // Then
  111. $this->assertEqualsWithDelta([$max_abs_eigenvalue], $eigenvalues, 0.0001);
  112. }
  113. /**
  114. * @return array (matrix, eigenvalues, dominant eigenvalue)
  115. */
  116. public function dataProviderForEigenvalues(): array
  117. {
  118. return [
  119. [
  120. [
  121. [0, 0],
  122. [0, 0],
  123. ],
  124. [0, 0],
  125. 0,
  126. ],
  127. [
  128. [
  129. [0, 1],
  130. [-2, -3],
  131. ],
  132. [-2, -1],
  133. -2,
  134. ],
  135. [
  136. [
  137. [6, -1],
  138. [2, 3],
  139. ],
  140. [5, 4],
  141. 5,
  142. ],
  143. [
  144. [
  145. [1, -2],
  146. [-2, 0],
  147. ],
  148. [(1 + \sqrt(17)) / 2, (1 - \sqrt(17)) / 2],
  149. (1 + \sqrt(17)) / 2,
  150. ],
  151. [
  152. [
  153. [2, -12],
  154. [1, -5],
  155. ],
  156. [-2, -1],
  157. -2,
  158. ],
  159. [
  160. [
  161. [-2, -4, 2],
  162. [-2, 1, 2],
  163. [4, 2, 5],
  164. ],
  165. [6, -5, 3],
  166. 6,
  167. ],
  168. [
  169. [
  170. [2, 0, 0],
  171. [1, 2, 1],
  172. [-1, 0, 1],
  173. ],
  174. [2, 2, 1],
  175. 2,
  176. ],
  177. [
  178. [
  179. [1, 2, 1],
  180. [6, -1, 0],
  181. [-1, -2, -1],
  182. ],
  183. [-4, 3, 0],
  184. -4,
  185. ],
  186. [
  187. [
  188. [1, 2, 3],
  189. [4, 5, 6],
  190. [7, 8, 9],
  191. ],
  192. [(3 * (5 + \sqrt(33))) / 2, (-3 * (sqrt(33) - 5)) / 2, 0],
  193. 3 * (5 + \sqrt(33)) / 2,
  194. ],
  195. [
  196. [
  197. [8, -6, 2],
  198. [-6, 7, -4],
  199. [2, -4, -3],
  200. ],
  201. [14.528807, -4.404176, 1.875369],
  202. 14.528807,
  203. ],
  204. [
  205. [
  206. [0, 11, -5],
  207. [-2, 17, -7],
  208. [-4, 26, -10],
  209. ],
  210. [4, 2, 1],
  211. 4,
  212. ],
  213. ];
  214. }
  215. /**
  216. * @return array (matrix, eigenvalues, dominant eiganvalue)
  217. */
  218. public function dataProviderForLargeMatrixEigenvalues(): array
  219. {
  220. return [
  221. [
  222. [
  223. [ 87, 270, -12, -49, -276, 40],
  224. [-14, -45, 6, 10, 46, -4],
  225. [-50, -156, 4, 25, 162, -25],
  226. [ 94, 294, -5, -47, -306, 49],
  227. [ 1, 1, 3, 1, 0, 2],
  228. [ 16, 48, 1, -6, -48, 8],
  229. ],
  230. [4, 3, 2, -2, 1, -1],
  231. 4,
  232. ]
  233. ];
  234. }
  235. /**
  236. * @test closedFormPolynomialRootMethod throws a BadDataException if the matrix is not the correct size (2x2 or 3x3)
  237. * @dataProvider dataProviderForEigenvalueException
  238. * @param array $A
  239. * @throws \Exception
  240. */
  241. public function testClosedFormPolynomialRootMethodExceptionMatrixNotCorrectSize(array $A)
  242. {
  243. // Given
  244. $A = MatrixFactory::create($A);
  245. // Then
  246. $this->expectException(Exception\BadDataException::class);
  247. // When
  248. Eigenvalue::closedFormPolynomialRootMethod($A);
  249. }
  250. /**
  251. * @return array
  252. */
  253. public function dataProviderForEigenvalueException(): array
  254. {
  255. return [
  256. '1x1' => [
  257. [
  258. [1],
  259. ],
  260. ],
  261. '5x5' => [
  262. [
  263. [1, 2, 3, 4, 5],
  264. [2, 3, 4, 5, 6],
  265. [3, 4, 5, 6, 7],
  266. [4, 5, 6, 7, 8],
  267. [5, 6, 7, 8, 9],
  268. ]
  269. ],
  270. 'not_square' => [
  271. [
  272. [1, 2, 3],
  273. [2, 3, 4],
  274. ],
  275. ],
  276. ];
  277. }
  278. /**
  279. * @return array (matrix, eigenvalues, dominant eigenvalue)
  280. */
  281. public function dataProviderForSymmetricEigenvalues(): array
  282. {
  283. return [
  284. [
  285. [
  286. [1, 4],
  287. [4, 1],
  288. ],
  289. [5.000, -3.000],
  290. 5,
  291. ],
  292. [
  293. [
  294. [1, 1, 1],
  295. [1, 2, 1],
  296. [1, 1, 1],
  297. ],
  298. [2 + M_SQRT2, 2 - M_SQRT2, 0.00],
  299. 2 + M_SQRT2,
  300. ],
  301. [
  302. [
  303. [1, 1, 1],
  304. [1, 1, 1],
  305. [1, 1, 1],
  306. ],
  307. [3, 8.881784e-16, 0],
  308. 3,
  309. ],
  310. [
  311. [
  312. [4, -30, 60, -35],
  313. [-30, 300, -675, 420],
  314. [60, -675, 1620, -1050],
  315. [-35, 420, -1050, 700],
  316. ],
  317. [2585.2538, 37.10149, 1.47805, .166642],
  318. 2585.2538,
  319. ],
  320. [
  321. [
  322. [1, 2],
  323. [2, 3],
  324. ],
  325. [4.236068, -0.236068],
  326. 4.236068,
  327. ],
  328. [
  329. [
  330. [1, 2],
  331. [2, 1],
  332. ],
  333. [3, -1],
  334. 3,
  335. ],
  336. [
  337. [
  338. [4, 1],
  339. [1, -2],
  340. ],
  341. [4.162278, -2.162278],
  342. 4.162278,
  343. ],
  344. [
  345. [
  346. [4, -1],
  347. [-1, 9],
  348. ],
  349. [9.192582, 3.807418],
  350. 9.192582,
  351. ],
  352. [
  353. [
  354. [1, 2, 3],
  355. [2, 6, 4],
  356. [3, 4, 5],
  357. ],
  358. [10.784062, 1.825499, -0.609561],
  359. 10.784062,
  360. ],
  361. [
  362. [
  363. [1, 7, 3],
  364. [7, 4, -5],
  365. [3, -5, 6],
  366. ],
  367. [10.971983, 7.035946, -7.007929],
  368. 10.971983,
  369. ],
  370. [
  371. [
  372. [5, 6, 7],
  373. [6, 3, 2],
  374. [7, 2, 1],
  375. ],
  376. [13.7082039, -5.0000000, 0.2917961],
  377. 13.7082039,
  378. ],
  379. [
  380. [
  381. [4, -1, -1, -1],
  382. [-1, 4, -1, -1],
  383. [-1, -1, 4, -1],
  384. [-1, -1, -1, 4],
  385. ],
  386. [5, 5, 5, 1],
  387. 5,
  388. ],
  389. [
  390. [
  391. [2, 7, 3],
  392. [7, 9, 4],
  393. [3, 4, 7],
  394. ],
  395. [16.065129, 4.287057, -2.352186],
  396. 16.065129,
  397. ],
  398. [
  399. [
  400. [1, 5, 6, 8],
  401. [5, 2, 7, 9],
  402. [6, 7, 3, 10],
  403. [8, 9, 10, 4],
  404. ],
  405. [25.527715, -7.381045, -4.652925, -3.493745],
  406. 25.527715,
  407. ],
  408. [
  409. [
  410. [1, 7, 3, 6],
  411. [7, 4, -5, 3],
  412. [3, -5, 6, 2],
  413. [6, 3, 2, 4],
  414. ],
  415. [13.6856756, 9.5813577, -7.2742130, -0.9928203],
  416. 13.6856756,
  417. ],
  418. [
  419. [
  420. [-12, -16, -18, 11, 11, 13, -20, 1],
  421. [-16, -18, 10, -1, -18, 18, -16, 6],
  422. [-18, 10, 4, -17, 2, -14, -11, -16],
  423. [11, -1, -17, -1, -19, 5, 8, -20],
  424. [11, -18, 2, -19, -13, 8, 5, -4],
  425. [13, 18, -14, 5, 8, 10, -19, 19],
  426. [-20, -16, -11, 8, 5, -19, 1, -3],
  427. [1, 6, -16, -20, -4, 19, -3, 15],
  428. ],
  429. [53.85777, -49.65359, -48.21567, 35.73664, -33.18637, 22.86811, 15.47171, -10.87861],
  430. 53.85777,
  431. ],
  432. [
  433. [
  434. [3, -2, 4],
  435. [-2, 6, 2],
  436. [4, 2, 3],
  437. ],
  438. [7, 7, -2],
  439. 7,
  440. ],
  441. [
  442. [
  443. [1 / 2, 0, 0],
  444. [0, 1 / 3, 0],
  445. [0, 0, 1 / 4],
  446. ],
  447. [1 / 2, 1 / 3, 1 / 4],
  448. 1 / 2,
  449. ],
  450. [
  451. [
  452. [1, 4, 5],
  453. [4, -3, 0],
  454. [5, 0, 7],
  455. ],
  456. [10.150897, -6.089238, 0.938341],
  457. 10.150897,
  458. ],
  459. [
  460. [
  461. [2, 4, 5],
  462. [4, 5, 1],
  463. [5, 1, 3],
  464. ],
  465. [10.055486, -3.259280, 3.203794],
  466. 10.055486,
  467. ],
  468. [
  469. [
  470. [1, -1, 5],
  471. [-1, 2, 1],
  472. [5, 1, 3],
  473. ],
  474. [7.102976, -3.461768, 2.358792],
  475. 7.102976,
  476. ],
  477. [
  478. [
  479. [3, 0, 0, 0],
  480. [0, 1, 0, 1],
  481. [0, 0, 2, 0],
  482. [0, 1, 0, 1],
  483. ],
  484. [3, 2, 2, 0],
  485. 3,
  486. ],
  487. [
  488. [
  489. [4, -14, -12],
  490. [-14, 10, 13],
  491. [-12, 13, 1],
  492. ],
  493. [31.535690, -9.643665, -6.892025],
  494. 31.535690,
  495. ],
  496. [
  497. [
  498. [9, 13, 3, 6],
  499. [13, 11, 7, 6],
  500. [3, 7, 4, 7],
  501. [6, 6, 7, 10],
  502. ],
  503. [30.6854034, 7.1478692, -4.5592669, 0.7259942],
  504. 30.6854034,
  505. ],
  506. [
  507. [
  508. [1, 3, 8],
  509. [3, 8, -4],
  510. [8, -4, 6],
  511. ],
  512. [12.531500, 8.945111, -6.476611],
  513. 12.531500,
  514. ],
  515. [
  516. [
  517. [1, 0, 0],
  518. [0, 2, 0],
  519. [0, 0, 4],
  520. ],
  521. [4, 2, 1],
  522. 4,
  523. ],
  524. [
  525. [
  526. [8, -6, 2],
  527. [-6, 7, -4],
  528. [2, -4, -3],
  529. ],
  530. [14.528807, -4.404176, 1.875369],
  531. 14.528807,
  532. ],
  533. [
  534. [
  535. [1, 0, 0],
  536. [0, 1, 0],
  537. [0, 0, 1],
  538. ],
  539. [1, 1, 1],
  540. 1,
  541. ],
  542. [
  543. [
  544. [1, 1, 1],
  545. [1, 2, 2],
  546. [1, 2, 3],
  547. ],
  548. [5.0489173, 0.6431041, 0.3079785],
  549. 5.0489173,
  550. ],
  551. [
  552. [
  553. [0, 3, 4],
  554. [3, 0, 5],
  555. [4, 5, 0],
  556. ],
  557. [8.055810, -5.180268, -2.875543],
  558. 8.055810,
  559. ],
  560. [
  561. [
  562. [4, 0, 2, -2],
  563. [0, 9, -6, 3],
  564. [2, -6, 5, -3],
  565. [-2, 3, -3, 2],
  566. ],
  567. [15, 5, 0, 0],
  568. 15,
  569. ],
  570. [
  571. [
  572. [2, -3 / 2, -3 / 2],
  573. [-3 / 2, 3, 1],
  574. [-3 / 2, 1, -3],
  575. ],
  576. [4.468627, -3.468627, 1.000000],
  577. 4.468627,
  578. ],
  579. ];
  580. }
  581. /**
  582. * @return array (matrix, eigenvalues, dominant eigenvalue)
  583. */
  584. public function dataProviderForSymmetricEigenvalueEdgeCases(): array
  585. {
  586. return [
  587. [
  588. [
  589. [0, 0, 0],
  590. [0, 0, 0],
  591. [0, 0, 0],
  592. ],
  593. [0, 0, 0],
  594. 0,
  595. ],
  596. ];
  597. }
  598. /**
  599. * @test Matrix eigenvalues throws a MatrixException if the eigenvalue method is not valid
  600. * @throws \Exception
  601. */
  602. public function testMatrixEigenvalueInvalidMethodException()
  603. {
  604. // Given
  605. $A = MatrixFactory::create([
  606. [1, 2, 3],
  607. [2, 3, 4],
  608. [3, 4, 5],
  609. ]);
  610. $invalidMethod = 'SecretMethod';
  611. // Then
  612. $this->expectException(Exception\MatrixException::class);
  613. // When
  614. $A->eigenvalues($invalidMethod);
  615. }
  616. /**
  617. * @test JacobiMethod throws a BadDataException if the matrix is not the correct size.
  618. * @dataProvider dataProviderForSymmetricException
  619. * @param array $A
  620. * @throws \Exception
  621. */
  622. public function testJacobiExceptionMatrixNotCorrectSize(array $A)
  623. {
  624. // Given
  625. $A = MatrixFactory::create($A);
  626. // Then
  627. $this->expectException(Exception\BadDataException::class);
  628. // When
  629. Eigenvalue::jacobiMethod($A);
  630. }
  631. /**
  632. * @return array
  633. */
  634. public function dataProviderForSymmetricException(): array
  635. {
  636. return [
  637. '1x1' => [
  638. [
  639. [1],
  640. ],
  641. ],
  642. 'not_symetric' => [
  643. [
  644. [1, 2, 3, 4, 6],
  645. [2, 3, 4, 5, 6],
  646. [3, 4, 5, 6, 7],
  647. [4, 5, 6, 7, 8],
  648. [5, 6, 7, 8, 9],
  649. ]
  650. ],
  651. 'not_square' => [
  652. [
  653. [1, 2, 3],
  654. [2, 3, 4],
  655. ]
  656. ],
  657. ];
  658. }
  659. /**
  660. * @test Power Iteration throws exception if number of iterations is exceeded
  661. * @dataProvider dataProviderForIterationFailure
  662. * @param array $A
  663. * @throws \Exception
  664. */
  665. public function testPowerIterationFail(array $A)
  666. {
  667. // Given
  668. $A = MatrixFactory::create($A);
  669. // Then
  670. $this->expectException(Exception\FunctionFailedToConvergeException::class);
  671. // When
  672. Eigenvalue::powerIteration($A, 0);
  673. }
  674. /**
  675. * @return array
  676. */
  677. public function dataProviderForIterationFailure(): array
  678. {
  679. return [
  680. [
  681. [
  682. [4, -30, 60, -35],
  683. [-30, 300, -675, 420],
  684. [60, -675, 1620, -1050],
  685. [-35, 420, -1050, 700],
  686. ],
  687. ],
  688. ];
  689. }
  690. /**
  691. * @test that a variety of matrix types can have eigenvalues calulated
  692. * @dataProvider dataProviderForSymmetricEigenvalues
  693. * @dataProvider dataProviderForEigenvalues
  694. * @dataProvider dataProviderForTriangularEigenvalues
  695. * @throws \Exception
  696. */
  697. public function testSmartEigenvalues(array $A, array $S)
  698. {
  699. // Given
  700. $A = MatrixFactory::create($A);
  701. // When
  702. $eigenvalues = $A->eigenvalues();
  703. // Then
  704. $this->assertEqualsWithDelta($S, $eigenvalues, 0.0001);
  705. }
  706. /**
  707. * @return array
  708. */
  709. public function dataProviderForTriangularEigenvalues(): array
  710. {
  711. return [
  712. [
  713. [
  714. [2, 0, 0, 0, 0, 0],
  715. [4, 3, 0, 0, 0, 0],
  716. [8, 2, 3, 0, 0, 0],
  717. [1, 7, 3, 9, 0, 0],
  718. [5, 4, 3, 2, 1, 0],
  719. [1, 6, 2, 9, 3, 6],
  720. ],
  721. [9, 6, 3, 3, 2, 1],
  722. ],
  723. [
  724. [
  725. [1, 0, 0, 1, 0, 0],
  726. [0, 2, 0, 0, 1, 0],
  727. [0, 0, 3, 0, 0, 1],
  728. [0, 0, 0, 4, 0, 0],
  729. [0, 0, 0, 0, 5, 0],
  730. [0, 0, 0, 0, 0, 6],
  731. ],
  732. [6, 5, 4, 3, 2, 1],
  733. ],
  734. [
  735. [[6]],
  736. [6],
  737. ],
  738. ];
  739. }
  740. /**
  741. * @test the function fails appropriately
  742. * @dataProvider dataProviderForEigenvalueFailure
  743. * @throws \Exception
  744. */
  745. public function testSmartEigenvalueFailure(array $A)
  746. {
  747. // Given
  748. $A = MatrixFactory::create($A);
  749. // Then
  750. $this->expectException(Exception\MatrixException::class);
  751. // When
  752. $A->eigenvalues();
  753. }
  754. /**
  755. * @return array
  756. */
  757. public function dataProviderForEigenvalueFailure(): array
  758. {
  759. return [
  760. [ // Not Square
  761. [[1, 2]],
  762. ],
  763. [ // Can Not Solve (yet)
  764. [
  765. [1, 2, 3, 4, 5],
  766. [2, 3, 4, 5, 1],
  767. [3, 4, 5, 1, 2],
  768. [4, 5, 1, 2, 3],
  769. [6, 1, 2, 3, 4],
  770. ],
  771. ],
  772. ];
  773. }
  774. /**
  775. * @test Bug Issue 414 - Jacobi method does not converge on highly correlated data and goes into infinite loop
  776. * Test for eigenvalues
  777. * @link https://github.com/markrogoyski/math-php/issues/414
  778. *
  779. * Test data from Python Numpy
  780. * > import numpy
  781. * > A = [[11090.868109438, 2292930.5298083], [2292930.5298083, 474044636.63249]]
  782. * > eig = numpy.linalg.eig(A)
  783. * > eig
  784. * (array([7.62112141e-02, 4.74055727e+08]),
  785. * array([[-0.9999883 , -0.00483689],
  786. * [ 0.00483689, -0.9999883 ]]))
  787. *
  788. * For reference, R result:
  789. * > A = rbind(c(11090.868109438, 2292930.5298083), c(2292930.5298083, 474044636.63249))
  790. * > ev <- eigen(A)
  791. * > ev
  792. * eigen() decomposition
  793. * $values
  794. * [1] 4.740557e+08 7.621119e-02
  795. *
  796. * $vectors
  797. * [,1] [,2]
  798. * [1,] 0.004836894 -0.999988302
  799. * [2,] 0.999988302 0.004836894
  800. */
  801. public function testJocobiMethodBugIssue414Eigenvalues()
  802. {
  803. // Given
  804. $A = MatrixFactory::createNumeric([
  805. [11090.868109438, 2292930.5298083],
  806. [2292930.5298083, 474044636.63249],
  807. ]);
  808. // And
  809. $expected = [4.74055727e+08, 7.62112141e-02];
  810. // When
  811. $eigenvalues = Eigenvalue::jacobiMethod($A);
  812. // Then
  813. $this->assertEqualsWithDelta($expected[0], $eigenvalues[0], 0.5);
  814. $this->assertEqualsWithDelta($expected[1], $eigenvalues[1], 0.0000001);
  815. }
  816. }