PolynomialTest.php 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337
  1. <?php
  2. namespace MathPHP\Tests\Expression;
  3. use MathPHP\Expression\Polynomial;
  4. use MathPHP\Exception;
  5. class PolynomialTest extends \PHPUnit\Framework\TestCase
  6. {
  7. /**
  8. * @test createZeroValue
  9. */
  10. public function testCreateZeroValue()
  11. {
  12. // Given
  13. $zero = Polynomial::createZeroValue();
  14. // Then
  15. $this->assertEquals([0], $zero->getCoefficients());
  16. }
  17. /**
  18. * @test String representation
  19. * @dataProvider dataProviderForString
  20. * @param array $coefficients
  21. * @param string $expected
  22. */
  23. public function testString(array $coefficients, string $expected)
  24. {
  25. // Given
  26. $polynomial = new Polynomial($coefficients);
  27. // When
  28. $string = \strval($polynomial);
  29. // Then
  30. $this->assertEquals($expected, $string);
  31. }
  32. /**
  33. * @return array (coefficients, string representation)
  34. */
  35. public function dataProviderForString(): array
  36. {
  37. return [
  38. [
  39. [1, 2, 3], // p(x) = x² + 2x + 3
  40. 'x² + 2x + 3',
  41. ],
  42. [
  43. [2, 3, 4], // p(x) = 2x² + 3x + 4
  44. '2x² + 3x + 4',
  45. ],
  46. [
  47. [-1, -2, -3], // p(x) = -x² - 2x - 3
  48. '-x² - 2x - 3',
  49. ],
  50. [
  51. [-2, -3, -4], // p(x) = -2x² - 3x - 4
  52. '-2x² - 3x - 4',
  53. ],
  54. [
  55. [0, 2, 3], // p(x) = 2x + 3
  56. '2x + 3',
  57. ],
  58. [
  59. [1, 0, 3], // p(x) = x² + 3
  60. 'x² + 3',
  61. ],
  62. [
  63. [1, 2, 0], // p(x) = x² + 2x
  64. 'x² + 2x',
  65. ],
  66. [
  67. [0, 0, 3], // p(x) = 3
  68. '3',
  69. ],
  70. [
  71. [1, 0, 0], // p(x) = x²
  72. 'x²',
  73. ],
  74. [
  75. [0, 2, 0], // p(x) = 2x
  76. '2x',
  77. ],
  78. [
  79. [0, -2, 3], // p(x) = -2x + 3
  80. '-2x + 3',
  81. ],
  82. [
  83. [-1, 0, 3], // p(x) = -x² + 3
  84. '-x² + 3',
  85. ],
  86. [
  87. [1, -2, 0], // p(x) = x² - 2x
  88. 'x² - 2x',
  89. ],
  90. [
  91. [0, 0, -3], // p(x) = -3
  92. '-3',
  93. ],
  94. [
  95. [-1, 0, 0], // p(x) = -x²
  96. '-x²',
  97. ],
  98. [
  99. [0, -2, 0], // p(x) = -2x
  100. '-2x',
  101. ],
  102. [
  103. [0, 0, 0], // p(x) = 0
  104. '0',
  105. ],
  106. [
  107. [0, 0, 1], // p(x) = 1
  108. '1',
  109. ],
  110. [
  111. [0, 0, 5], // p(x) = 5
  112. '5',
  113. ],
  114. [
  115. [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12], // p(x) = x¹¹ + 2x¹⁰ + 3x⁹ + 4x⁸ + 5x⁷ + 6x⁶ + 7x⁵ + 8x⁴ + 9x³ + 10x² + 11x + 12
  116. 'x¹¹ + 2x¹⁰ + 3x⁹ + 4x⁸ + 5x⁷ + 6x⁶ + 7x⁵ + 8x⁴ + 9x³ + 10x² + 11x + 12',
  117. ],
  118. ];
  119. }
  120. /**
  121. * @test Custom variable for string representation
  122. * @dataProvider dataProviderForVariable
  123. * @param array $args
  124. * @param string $expected
  125. */
  126. public function testVariable(array $args, string $expected)
  127. {
  128. // Given
  129. $coefficients = $args[0];
  130. $variable = $args[1] ?? "x";
  131. $polynomial = new Polynomial($coefficients, $variable);
  132. // When
  133. $string = \strval($polynomial);
  134. // Then
  135. $this->assertEquals($expected, $string);
  136. }
  137. /**
  138. * @return array (coefficients, string representation)
  139. */
  140. public function dataProviderForVariable(): array
  141. {
  142. return [
  143. [
  144. [[1, 2, 3]], // p(x) = x² + 2x + 3
  145. 'x² + 2x + 3',
  146. ],
  147. [
  148. [[2, 3, 4], "p"], // p(p) = 2p² + 3p + 4
  149. '2p² + 3p + 4',
  150. ],
  151. [
  152. [[-1, -2, -3], "q"], // p(q) = -q² - 2q - 3
  153. '-q² - 2q - 3',
  154. ],
  155. [
  156. [[-2, -3, -4], "a"], // p(a) = -2a² - 3a - 4
  157. '-2a² - 3a - 4',
  158. ],
  159. [
  160. [[0, 2, 3], "a"], // p(a) = 2a + 3
  161. '2a + 3',
  162. ],
  163. [
  164. [[1, 0, 3], "a"], // p(a) = a² + 3
  165. 'a² + 3',
  166. ],
  167. [
  168. [[1, 2, 0], "a"], // p(a) = a² + 2a
  169. 'a² + 2a',
  170. ],
  171. [
  172. [[0, 0, 3], "a"], // p(a) = 3
  173. '3',
  174. ],
  175. [
  176. [[1, 0, 0], "a"], // p(a) = a²
  177. 'a²',
  178. ],
  179. [
  180. [[0, 2, 0], "a"], // p(a) = 2a
  181. '2a',
  182. ],
  183. [
  184. [[0, -2, 3], "a"], // p(a) = -2a + 3
  185. '-2a + 3',
  186. ],
  187. [
  188. [[-1, 0, 3], "a"], // p(a) = -a² + 3
  189. '-a² + 3',
  190. ],
  191. [
  192. [[1, -2, 0], "a"], // p(a) = a² - 2a
  193. 'a² - 2a',
  194. ],
  195. [
  196. [[0, 0, -3], "a"], // p(a) = -3
  197. '-3',
  198. ],
  199. [
  200. [[-1, 0, 0], "a"], // p(a) = -a²
  201. '-a²',
  202. ],
  203. [
  204. [[0, -2, 0], "a"], // p(a) = -2a
  205. '-2a',
  206. ],
  207. [
  208. [[0, 0, 0], "a"], // p(a) = 0
  209. '0',
  210. ],
  211. [
  212. [[0, 0, 1], "a"], // p(a) = 1
  213. '1',
  214. ],
  215. [
  216. [[0, 0, 5], "a"], // p(a) = 5
  217. '5',
  218. ],
  219. [
  220. [[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12], "a"], // p(a) = a¹¹ + 2a¹⁰ + 3a⁹ + 4a⁸ + 5a⁷ + 6a⁶ + 7a⁵ + 8a⁴ + 9a³ + 10a² + 11a + 12
  221. 'a¹¹ + 2a¹⁰ + 3a⁹ + 4a⁸ + 5a⁷ + 6a⁶ + 7a⁵ + 8a⁴ + 9a³ + 10a² + 11a + 12',
  222. ],
  223. ];
  224. }
  225. /**
  226. * @test Evaluate the polynomial at some x
  227. * @dataProvider dataProviderForEval
  228. * @param array $coefficients
  229. * @param $x
  230. * @param $expected
  231. */
  232. public function testEval(array $coefficients, $x, $expected)
  233. {
  234. // Given
  235. $polynomial = new Polynomial($coefficients);
  236. // When
  237. $evaluated = $polynomial($x);
  238. // Then
  239. $this->assertEquals($expected, $evaluated);
  240. }
  241. /**
  242. * @return array (coefficients, x, y)
  243. */
  244. public function dataProviderForEval(): array
  245. {
  246. return [
  247. [
  248. [1, 2, 3], // p(x) = x² + 2x + 3
  249. 0, 3 // p(0) = 3
  250. ],
  251. [
  252. [1, 2, 3], // p(x) = x² + 2x + 3
  253. 1, 6 // p(1) = 6
  254. ],
  255. [
  256. [1, 2, 3], // p(x) = x² + 2x + 3
  257. 2, 11 // p(2) = 11
  258. ],
  259. [
  260. [1, 2, 3], // p(x) = x² + 2x + 3
  261. 3, 18 // p(3) = 18
  262. ],
  263. [
  264. [1, 2, 3], // p(x) = x² + 2x + 3
  265. 4, 27 // p(4) = 27
  266. ],
  267. [
  268. [1, 2, 3], // p(x) = x² + 2x + 3
  269. -1, 2 // p(-1) = 2
  270. ],
  271. [
  272. [0, 0, 0], // p(x) = 0
  273. 5, 0 // p(5) = 0
  274. ],
  275. ];
  276. }
  277. /**
  278. * @test Degree
  279. * @dataProvider dataProviderForGetDegree
  280. * @param array $coefficients
  281. * @param int $expected
  282. */
  283. public function testGetDegree(array $coefficients, int $expected)
  284. {
  285. // Given
  286. $polynomial = new Polynomial($coefficients);
  287. // When
  288. $degree = $polynomial->getDegree();
  289. // Then
  290. $this->assertEquals($expected, $degree);
  291. }
  292. /**
  293. * @return array (coefficients, degree)
  294. */
  295. public function dataProviderForGetDegree(): array
  296. {
  297. return [
  298. [
  299. [1, 2, 3], // p(x) = x² + 2x + 3
  300. 2,
  301. ],
  302. [
  303. [2, 3, 4], // p(x) = 2x² + 3x + 4
  304. 2
  305. ],
  306. [
  307. [-1, -2, -3], // p(x) = -x² - 2x - 3
  308. 2
  309. ],
  310. [
  311. [-2, -3, -4], // p(x) = -2x² - 3x - 4
  312. 2
  313. ],
  314. [
  315. [0, 2, 3], // p(x) = 2x + 3
  316. 1
  317. ],
  318. [
  319. [1, 0, 3], // p(x) = x² + 3
  320. 2
  321. ],
  322. [
  323. [1, 2, 0], // p(x) = x² + 2x
  324. 2
  325. ],
  326. [
  327. [0, 0, 3], // p(x) = 3
  328. 0
  329. ],
  330. [
  331. [1, 0, 0], // p(x) = x²
  332. 2
  333. ],
  334. [
  335. [0, 2, 0], // p(x) = 2x
  336. 1
  337. ],
  338. [
  339. [0, -2, 3], // p(x) = -2x + 3
  340. 1
  341. ],
  342. [
  343. [-1, 0, 3], // p(x) = -x² + 3
  344. 2
  345. ],
  346. [
  347. [1, -2, 0], // p(x) = x² - 2x
  348. 2
  349. ],
  350. [
  351. [0, 0, -3], // p(x) = -3
  352. 0
  353. ],
  354. [
  355. [-1, 0, 0], // p(x) = -x²
  356. 2
  357. ],
  358. [
  359. [0, -2, 0], // p(x) = -2x
  360. 1
  361. ],
  362. [
  363. [0, 0, 0], // p(x) = 0
  364. 0
  365. ],
  366. [
  367. [0, 0, 1], // p(x) = 1
  368. 0
  369. ],
  370. [
  371. [0, 0, 5], // p(x) = 5
  372. 0
  373. ],
  374. [
  375. [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12], // p(x) = x¹¹ + 2x¹⁰ + 3x⁹ + 4x⁸ + 5x⁷ + 6x⁶ + 7x⁵ + 8x⁴ + 9x³ + 10x² + 11x + 12
  376. 11
  377. ],
  378. ];
  379. }
  380. /**
  381. * @test coefficients
  382. * @dataProvider dataProviderForGetCoefficients
  383. * @param array $coefficients
  384. * @param array $expected
  385. */
  386. public function testGetCoefficients(array $coefficients, array $expected)
  387. {
  388. // Given
  389. $polynomial = new Polynomial($coefficients);
  390. // When
  391. $coefficients = $polynomial->getCoefficients();
  392. // Then
  393. $this->assertEquals($expected, $coefficients);
  394. }
  395. /**
  396. * @return array (coefficients, expected coefficients)
  397. */
  398. public function dataProviderForGetCoefficients(): array
  399. {
  400. return [
  401. [
  402. [1, 2, 3], // p(x) = x² + 2x + 3
  403. [1, 2, 3]
  404. ],
  405. [
  406. [2, 3, 4], // p(x) = 2x² + 3x + 4
  407. [2, 3, 4]
  408. ],
  409. [
  410. [-1, -2, -3], // p(x) = -x² - 2x - 3
  411. [-1, -2, -3]
  412. ],
  413. [
  414. [-2, -3, -4], // p(x) = -2x² - 3x - 4
  415. [-2, -3, -4]
  416. ],
  417. [
  418. [0, 2, 3], // p(x) = 2x + 3
  419. [2, 3]
  420. ],
  421. [
  422. [1, 0, 3], // p(x) = x² + 3
  423. [1, 0, 3]
  424. ],
  425. [
  426. [1, 2, 0], // p(x) = x² + 2x
  427. [1, 2, 0]
  428. ],
  429. [
  430. [0, 0, 3], // p(x) = 3
  431. [3]
  432. ],
  433. [
  434. [1, 0, 0], // p(x) = x²
  435. [1, 0, 0]
  436. ],
  437. [
  438. [0, 2, 0], // p(x) = 2x
  439. [2, 0]
  440. ],
  441. [
  442. [0, -2, 3], // p(x) = -2x + 3
  443. [-2, 3]
  444. ],
  445. [
  446. [-1, 0, 3], // p(x) = -x² + 3
  447. [-1, 0, 3]
  448. ],
  449. [
  450. [1, -2, 0], // p(x) = x² - 2x
  451. [1, -2, 0]
  452. ],
  453. [
  454. [0, 0, -3], // p(x) = -3
  455. [-3]
  456. ],
  457. [
  458. [-1, 0, 0], // p(x) = -x²
  459. [-1, 0, 0]
  460. ],
  461. [
  462. [0, -2, 0], // p(x) = -2x
  463. [-2, 0]
  464. ],
  465. [
  466. [0, 0, 0], // p(x) = 0
  467. [0]
  468. ],
  469. [
  470. [0, 0, 1], // p(x) = 1
  471. [1]
  472. ],
  473. [
  474. [0, 0, 5], // p(x) = 5
  475. [5]
  476. ],
  477. [
  478. [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12], // p(x) = x¹¹ + 2x¹⁰ + 3x⁹ + 4x⁸ + 5x⁷ + 6x⁶ + 7x⁵ + 8x⁴ + 9x³ + 10x² + 11x + 12
  479. [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
  480. ],
  481. ];
  482. }
  483. /**
  484. * @test Get variable
  485. * @dataProvider dataProviderForGetVariable
  486. * @param array $args
  487. * @param string $expected
  488. */
  489. public function testGetVariable(array $args, string $expected)
  490. {
  491. // Given
  492. $coefficients = $args[0];
  493. $variable = $args[1] ?? "x";
  494. $polynomial = new Polynomial($coefficients, $variable);
  495. // When
  496. $result = $polynomial->getVariable();
  497. // Then
  498. $this->assertEquals($expected, $result);
  499. }
  500. /**
  501. * @return array
  502. */
  503. public function dataProviderForGetVariable(): array
  504. {
  505. return [
  506. [
  507. [[1, 2, 3]], // p(x) = x² + 2x + 3
  508. 'x',
  509. ],
  510. [
  511. [[2, 3, 4], "p"], // p(p) = 2p² + 3p + 4
  512. 'p',
  513. ],
  514. [
  515. [[-1, -2, -3], "m"], // p(m) = -m² - 2m - 3
  516. 'm',
  517. ],
  518. [
  519. [[-2, -3, -4], "a"], // p(a) = -2a² - 3a - 4
  520. 'a',
  521. ],
  522. [
  523. [[0, 2, 3], "Δ"], // p(Δ) = 2Δ + 3
  524. 'Δ',
  525. ],
  526. [
  527. [[1, 0, 3], "Γ"], // p(Γ) = Γ² + 3
  528. 'Γ',
  529. ],
  530. [
  531. [[1, 2, 0], "Ψ"], // p(a) = Ψ² + 2Ψ
  532. 'Ψ',
  533. ],
  534. [
  535. [[0, 0, 3], "μ"], // p(μ) = 3
  536. 'μ',
  537. ],
  538. [
  539. [[1, 0, 0], "ξ"], // p(ξ) = ξ²
  540. 'ξ',
  541. ],
  542. [
  543. [[0, 2, 0], "aₙ"], // p(aₙ) = 2aₙ
  544. 'aₙ',
  545. ],
  546. [
  547. [[0, -2, 3], "aⁿ"], // p(aⁿ) = -2aⁿ + 3
  548. 'aⁿ',
  549. ],
  550. [
  551. [[-1, 0, 3], "a₍ₘ₎₍ₙ₎"], // p(a) = -a₍ₘ₎₍ₙ₎² + 3
  552. 'a₍ₘ₎₍ₙ₎',
  553. ],
  554. ];
  555. }
  556. /**
  557. * @test Set variable
  558. */
  559. public function testSetVariable()
  560. {
  561. // Given default variable: x
  562. $polynomial = new Polynomial([1, 1, 1, 1]);
  563. $expected = "x";
  564. $result = $polynomial->getVariable();
  565. $this->assertEquals($expected, $result);
  566. $expected = "x³ + x² + x + 1";
  567. $result = \strval($polynomial);
  568. $this->assertEquals($expected, $result);
  569. // Given we switch variable to Φ
  570. $polynomial->setVariable("Φ");
  571. $expected = "Φ";
  572. $result = $polynomial->getVariable();
  573. $this->assertEquals($expected, $result);
  574. $expected = "Φ³ + Φ² + Φ + 1";
  575. $result = \strval($polynomial);
  576. $this->assertEquals($expected, $result);
  577. // Given we switch variable back to x
  578. $polynomial->setVariable("x");
  579. $expected = "x";
  580. $result = $polynomial->getVariable();
  581. $this->assertEquals($expected, $result);
  582. $expected = "x³ + x² + x + 1";
  583. $result = \strval($polynomial);
  584. $this->assertEquals($expected, $result);
  585. }
  586. /**
  587. * @test Differentiate
  588. * @dataProvider dataProviderForDifferentiate
  589. * @param array $polynomial
  590. * @param array $expected
  591. */
  592. public function testDifferentiation(array $polynomial, array $expected)
  593. {
  594. // Given
  595. $polynomial = new Polynomial($polynomial);
  596. $expected = new Polynomial($expected);
  597. // When
  598. $derivative = $polynomial->differentiate();
  599. // Then
  600. $this->assertEquals($expected, $derivative);
  601. }
  602. /**
  603. * @return array (coefficients, derivative)
  604. */
  605. public function dataProviderForDifferentiate(): array
  606. {
  607. return [
  608. [
  609. [1, 2, 3], // p(x) = x² + 2x + 3
  610. [2, 2] // p'(x) = 2x + 2
  611. ],
  612. [
  613. [2, 3, 4], // p(x) = 2x² + 3x + 4
  614. [4, 3] // p'(x) = 4x + 3
  615. ],
  616. [
  617. [1, 0], // p(x) = x
  618. [1] // p'(x) = 1
  619. ],
  620. [
  621. [5, 0], // p(x) = 5x
  622. [5] // p'(x) = 5
  623. ],
  624. [
  625. [1, 0, 0], // p(x) = x²
  626. [2, 0] // p'(x) = 2x
  627. ],
  628. [
  629. [5], // p(x) = 5
  630. [0] // p'(x) = 0
  631. ],
  632. [
  633. [1], // p(x) = 1
  634. [0] // p'(x) = 0
  635. ],
  636. [
  637. [0], // p(x) = 0
  638. [0] // p'(x) = 0
  639. ],
  640. ];
  641. }
  642. /**
  643. * @test Integration
  644. * @dataProvider dataProviderForIntegrate
  645. * @param array $polynomial
  646. * @param array $expected_integral
  647. */
  648. public function testIntegration(array $polynomial, array $expected_integral)
  649. {
  650. // Given
  651. $polynomial = new Polynomial($polynomial);
  652. $expected = new Polynomial($expected_integral);
  653. // When
  654. $integral = $polynomial->integrate();
  655. // Then
  656. $this->assertEquals($expected, $integral);
  657. }
  658. /**
  659. * @return array (coefficients, integral)
  660. */
  661. public function dataProviderForIntegrate(): array
  662. {
  663. return [
  664. [
  665. [1, 2, 3], // f(x) = x² + 2x + 3
  666. [1 / 3, 1, 3, 0], // ∫f(x) = (1/3)x³ + x² + 3x
  667. ],
  668. [
  669. [5], // f(x) = 5
  670. [5, 0], // ∫f(x) = 5x
  671. ],
  672. [
  673. [0], // f(x) = 0
  674. [0, 0], // ∫f(x) = 0x
  675. ],
  676. [
  677. [1, 0], // f(x) = x
  678. [1 / 2, 0, 0], // ∫f(x) = (1/2)²
  679. ],
  680. ];
  681. }
  682. /**
  683. * @test Fundamental theorem of calculus
  684. */
  685. public function testFundamentalTheoremOfCalculus()
  686. {
  687. // Given p(x) = x² + 2x + 3
  688. $polynomial = new Polynomial([1, 2, 3]);
  689. $expected = $polynomial;
  690. // When
  691. $integral = $polynomial->integrate();
  692. $actual = $integral->differentiate();
  693. // Then
  694. $this->assertEquals($expected, $actual);
  695. }
  696. /**
  697. * @test Addition
  698. * @dataProvider dataProviderForAddition
  699. * @param array $polynomialA
  700. * @param array $polynomialB
  701. * @param array $expected_sum
  702. * @throws \Exception
  703. */
  704. public function testAddition(array $polynomialA, array $polynomialB, array $expected_sum)
  705. {
  706. // Given
  707. $polynomialA = new Polynomial($polynomialA);
  708. $polynomialB = new Polynomial($polynomialB);
  709. $expected = new Polynomial($expected_sum);
  710. // When
  711. $sum = $polynomialA->add($polynomialB);
  712. // Then
  713. $this->assertEquals($expected, $sum);
  714. }
  715. /**
  716. * @return array (p1, p2, sum)
  717. */
  718. public function dataProviderForAddition(): array
  719. {
  720. return [
  721. [
  722. [1, 2, 3], // f(x) = x² + 2x + 3
  723. [1, 2, 3], // g(x) = x² + 2x + 3
  724. [2, 4, 6], // f(x)+g(x) = 2x² + 4x + 6
  725. ],
  726. [
  727. [1, 2, 3], // f(x) = x² + 2x + 3
  728. [2, 3, 1], // g(x) = 2x² + 3x + 1
  729. [3, 5, 4], // f(x)+g(x) = 3x² + 5x + 4
  730. ],
  731. [
  732. [1, 2, 3, 4, 4], // f(x) = x⁴ + 2x³ + 3x² + 4x + 4
  733. [2, 3, 1], // g(x) = 2x² + 3x + 1
  734. [1, 2, 5, 7, 5], // f(x)+g(x) = x⁴ + 2x³ + 5x² + 7x + 5
  735. ],
  736. [
  737. [2, 3, 1], // f(x) = 2x² + 3x + 1
  738. [1, 2, 3, 4, 4], // g(x) = x⁴ + 2x³ + 3x² + 4x + 4
  739. [1, 2, 5, 7, 5], // f(x)+g(x) = x⁴ + 2x³ + 5x² + 7x + 5
  740. ],
  741. [
  742. [1, -8, 12, 3], // f(x) = x³ - 8x² + 12x + 3
  743. [1, -8, 12, 3], // g(x) = f(x)
  744. [2, -16, 24, 6], // f(x)+g(x) = 2x³ - 16x² + 24x + 6
  745. ],
  746. ];
  747. }
  748. /**
  749. * @test Subtraction
  750. * @dataProvider dataProviderForSubtraction
  751. * @param array $polynomialA
  752. * @param array $polynomialB
  753. * @param array $expected_sum
  754. * @throws \Exception
  755. */
  756. public function testSubtraction(array $polynomialA, array $polynomialB, array $expected_sum)
  757. {
  758. // Given
  759. $polynomialA = new Polynomial($polynomialA);
  760. $polynomialB = new Polynomial($polynomialB);
  761. $expected = new Polynomial($expected_sum);
  762. // When
  763. $difference = $polynomialA->subtract($polynomialB);
  764. // Then
  765. $this->assertEquals($expected, $difference);
  766. }
  767. /**
  768. * @return array (p1, p2, difference)
  769. */
  770. public function dataProviderForSubtraction(): array
  771. {
  772. return [
  773. [
  774. [1, 2, 3], // f(x) = x² + 2x + 3
  775. [1, 2, 3], // g(x) = x² + 2x + 3
  776. [0, 0, 0], // f(x)-g(x) = 0
  777. ],
  778. [
  779. [1, 2, 3], // f(x) = x² + 2x + 3
  780. [2, 3, 1], // g(x) = 2x² + 3x + 1
  781. [-1, -1, 2], // f(x)-g(x) = -x² - x + 2
  782. ],
  783. [
  784. [1, 2, 3, 4, 4], // f(x) = x⁴ + 2x³ + 3x² + 4x + 4
  785. [2, 3, 1], // g(x) = 2x² + 3x + 1
  786. [1, 2, 1, 1, 3], // f(x)-g(x) = x⁴ + 2x³ + x² + x + 3
  787. ],
  788. [
  789. [1, -8, 12, 3], // f(x) = x³ - 8x² + 12x + 3
  790. [1, -8, 12, 3], // g(x) = f(x)
  791. [0, 0, 0, 0], // f(x)-g(x) = 0
  792. ],
  793. ];
  794. }
  795. /**
  796. * @test Multiplication
  797. * @dataProvider dataProviderForMultiplication
  798. * @param array $polynomialA
  799. * @param array $polynomialB
  800. * @param array $expected_product
  801. * @throws \Exception
  802. */
  803. public function testMultiplication(array $polynomialA, array $polynomialB, array $expected_product)
  804. {
  805. // Given
  806. $polynomialA = new Polynomial($polynomialA);
  807. $polynomialB = new Polynomial($polynomialB);
  808. $expected = new Polynomial($expected_product);
  809. // When
  810. $product = $polynomialA->multiply($polynomialB);
  811. // Then
  812. $this->assertEquals($expected, $product);
  813. }
  814. /**
  815. * @return array (p1, p2, product)
  816. */
  817. public function dataProviderForMultiplication(): array
  818. {
  819. return [
  820. [
  821. [1, 2, 3], // f(x) = x² + 2x + 3
  822. [1, 2, 3], // g(x) = x² + 2x + 3
  823. [1, 4, 10, 12, 9], // f(x)*g(x) = x⁴ + 4x³ + 10x² + 12x + 9
  824. ],
  825. [
  826. [1, 2, 3], // f(x) = x² + 2x + 3
  827. [2, 3, 1], // g(x) = 2x² + 3x + 1
  828. [2, 7, 13, 11, 3], // f(x)*g(x) = 2x⁴ + 7x³ + 13x² + 11x + 3
  829. ],
  830. [
  831. [1, 2, 3, 4, 4], // f(x) = x⁴ + 2x³ + 3x² + 4x + 4
  832. [2, 3, 1], // g(x) = 2x² + 3x + 1
  833. [2, 7, 13, 19, 23, 16, 4], // f(x)*g(x) = 2x⁶ + 7x⁵ + 13x⁴ + 19x³ + 23x² + 16x + 4
  834. ],
  835. [
  836. [1, -8, 12, 3], // f(x) = x³ - 8x² + 12x + 3
  837. [1, -8, 12, 3], // g(x) = f(x)
  838. [1, -16, 88, -186, 96, 72, 9], // f(x)+g(x) = x⁶ - 16x⁵ + 88x⁴ - 186x³ + 96x² + 72x + 9
  839. ],
  840. ];
  841. }
  842. /**
  843. * @test Scalar addition
  844. * @dataProvider dataProviderForScalarAddition
  845. * @param array $polynomialA
  846. * @param int $scaler
  847. * @param array $expected_product
  848. * @throws \Exception
  849. */
  850. public function testScalarAddition(array $polynomialA, int $scaler, array $expected_product)
  851. {
  852. // Given
  853. $polynomialA = new Polynomial($polynomialA);
  854. $expected = new Polynomial($expected_product);
  855. // When
  856. $sum = $polynomialA->add($scaler);
  857. // Then
  858. $this->assertEquals($expected, $sum);
  859. }
  860. /**
  861. * @return array (p1, scalar, sum)
  862. */
  863. public function dataProviderForScalarAddition(): array
  864. {
  865. return [
  866. [
  867. [1, 2, 3], // f(x) = x² + 2x + 3
  868. 2,
  869. [1, 2, 5], // f(x)*c = x² + 2x + 5
  870. ],
  871. [
  872. [1, 2, 3, 4, 4], // f(x) = x⁴ + 2x³ + 3x² + 4x + 4
  873. -2,
  874. [1, 2, 3, 4, 2], // f(x)*c = 1x⁴ + 2x³ + 3x² + 4x + 2
  875. ],
  876. ];
  877. }
  878. /**
  879. * @test Scalar subtraction
  880. * @dataProvider dataProviderForScalarSubtraction
  881. * @param array $polynomialA
  882. * @param int $scaler
  883. * @param array $expected_product
  884. * @throws \Exception
  885. */
  886. public function testScalarSubtraction(array $polynomialA, int $scaler, array $expected_product)
  887. {
  888. // Given
  889. $polynomialA = new Polynomial($polynomialA);
  890. $expected = new Polynomial($expected_product);
  891. // When
  892. $difference = $polynomialA->subtract($scaler);
  893. // Then
  894. $this->assertEquals($expected, $difference);
  895. }
  896. /**
  897. * @return array (p1, scalar, difference)
  898. */
  899. public function dataProviderForScalarSubtraction(): array
  900. {
  901. return [
  902. [
  903. [1, 2, 3], // f(x) = x² + 2x + 3
  904. 2,
  905. [1, 2, 1], // f(x)*c = x² + 2x + 1
  906. ],
  907. [
  908. [1, 2, 3, 4, 4], // f(x) = x⁴ + 2x³ + 3x² + 4x + 4
  909. -2,
  910. [1, 2, 3, 4, 6], // f(x)*c = 1x⁴ + 2x³ + 3x² + 4x + 6
  911. ],
  912. ];
  913. }
  914. /**
  915. * @test Scalar multiplication
  916. * @dataProvider dataProviderForScalarMultiplication
  917. * @param array $polynomialA
  918. * @param int $scaler
  919. * @param array $expected_product
  920. * @throws \Exception
  921. */
  922. public function testScalarMultiplication(array $polynomialA, int $scaler, array $expected_product)
  923. {
  924. // Given
  925. $polynomialA = new Polynomial($polynomialA);
  926. $expected = new Polynomial($expected_product);
  927. // When
  928. $product = $polynomialA->multiply($scaler);
  929. // Then
  930. $this->assertEquals($expected, $product);
  931. }
  932. /**
  933. * @return array (p1, scalar, product)
  934. */
  935. public function dataProviderForScalarMultiplication(): array
  936. {
  937. return [
  938. [
  939. [1, 2, 3], // f(x) = x² + 2x + 3
  940. 2,
  941. [2, 4, 6], // f(x)*c = 2x² + 4x + 6
  942. ],
  943. [
  944. [1, 2, 3, 4, 4], // f(x) = x⁴ + 2x³ + 3x² + 4x + 4
  945. -2,
  946. [-2, -4, -6, -8, -8], // f(x)*c = -2x⁴ - 4x³ - 6x² - 8x - 8
  947. ],
  948. ];
  949. }
  950. /**
  951. * @test roots
  952. * @dataProvider dataProviderForRoots
  953. * @param array $polynomialA
  954. * @param array $expected_roots
  955. * @throws \Exception
  956. */
  957. public function testRoots(array $polynomialA, array $expected_roots)
  958. {
  959. // Given
  960. $polynomialA = new Polynomial($polynomialA);
  961. // When
  962. $roots = $polynomialA->roots();
  963. // Then
  964. $this->assertEqualsWithDelta($expected_roots, $roots, 0.00001);
  965. }
  966. /**
  967. * @return array
  968. */
  969. public function dataProviderForRoots(): array
  970. {
  971. return [
  972. // Degree 0
  973. [
  974. [0],
  975. [null],
  976. ],
  977. [
  978. [3],
  979. [null],
  980. ],
  981. [
  982. [0, -3],
  983. [null],
  984. ],
  985. // Degree 1
  986. [
  987. [1, -3],
  988. [3],
  989. ],
  990. [
  991. [2, 0],
  992. [0],
  993. ],
  994. // Degree 2
  995. [
  996. [1, -3, -4],
  997. [-1, 4],
  998. ],
  999. // Degree 3
  1000. [
  1001. [1, -6, 11, -6],
  1002. [3, 1, 2],
  1003. ],
  1004. // Degree 4
  1005. [
  1006. [1, -10, 35, -50, 24],
  1007. [4, 1, 3, 2],
  1008. ],
  1009. ];
  1010. }
  1011. /**
  1012. * @test roots NAN - Closed form solutions don't exist for degrees 5 or higher - no implementation
  1013. * @dataProvider dataProviderForRootsNAN
  1014. * @param array $polynomialA
  1015. * @throws \Exception
  1016. */
  1017. public function testRootsNAN(array $polynomialA)
  1018. {
  1019. // Given
  1020. $polynomialA = new Polynomial($polynomialA);
  1021. // When
  1022. $roots = $polynomialA->roots();
  1023. // Then
  1024. $this->assertCount(1, $roots);
  1025. $this->assertNan($roots[0]);
  1026. }
  1027. /**
  1028. * @return array
  1029. */
  1030. public function dataProviderForRootsNAN(): array
  1031. {
  1032. return [
  1033. 'degree 5' => [
  1034. [1, -3, -4, 5, 5, 5],
  1035. ],
  1036. 'degree 6' => [
  1037. [1, 2, 3, 4, 5, 6, 7],
  1038. ],
  1039. ];
  1040. }
  1041. /**
  1042. * @test add - IncorrectTypeException if the argument is not numeric or a Polynomial
  1043. * @throws \Exception
  1044. */
  1045. public function testException()
  1046. {
  1047. // Given
  1048. $string = 'This is a string!';
  1049. $poly = new Polynomial([1, 2]);
  1050. // Then
  1051. $this->expectException(Exception\IncorrectTypeException::class);
  1052. // When
  1053. $sum = $poly->add($string);
  1054. }
  1055. /**
  1056. * @test checkNumericOrPolynomial returns a Polynomial for numeric and Polynomial inputs
  1057. * @dataProvider dataProviderForCheckNumericOrPolynomial
  1058. */
  1059. public function testCheckNumericOrPolynomialNumericInput($input)
  1060. {
  1061. // Given
  1062. $method = new \ReflectionMethod(Polynomial::class, 'checkNumericOrPolynomial');
  1063. $method->setAccessible(true);
  1064. // When
  1065. $polynomial = $method->invokeArgs(new Polynomial([1]), [$input]);
  1066. // Then
  1067. $this->assertInstanceOf(Polynomial::class, $polynomial);
  1068. }
  1069. public function dataProviderForCheckNumericOrPolynomial(): array
  1070. {
  1071. return [
  1072. [-1],
  1073. [0],
  1074. [1],
  1075. [10],
  1076. [2.45],
  1077. ['3'],
  1078. ['5.4'],
  1079. [new Polynomial([4])],
  1080. [new Polynomial([2, 3, 4])],
  1081. ];
  1082. }
  1083. /**
  1084. * @test checkNumericOrPolynomial throws an IncorrectTypeException if the input is not numeric or a Polynomial
  1085. */
  1086. public function testCheckNumericOrPolynomialException()
  1087. {
  1088. // Given
  1089. $method = new \ReflectionMethod(Polynomial::class, 'checkNumericOrPolynomial');
  1090. $method->setAccessible(true);
  1091. // Then
  1092. $this->expectException(Exception\IncorrectTypeException::class);
  1093. // When
  1094. $polynomial = $method->invokeArgs(new Polynomial([1]), ['not a number']);
  1095. }
  1096. /**
  1097. * @test negate returns a Polynomial with every coefficient negated
  1098. * @dataProvider dataProviderForNegate
  1099. * @param array $polynomial
  1100. * @param array $expected_negated_polynomial
  1101. */
  1102. public function testNegate(array $polynomial, array $expected_negated_polynomial)
  1103. {
  1104. // Given
  1105. $polynomial = new Polynomial($polynomial);
  1106. $expected = new Polynomial($expected_negated_polynomial);
  1107. // When
  1108. $negated = $polynomial->negate();
  1109. // Then
  1110. $this->assertEquals($expected, $negated);
  1111. }
  1112. /**
  1113. * @return array
  1114. */
  1115. public function dataProviderForNegate(): array
  1116. {
  1117. return [
  1118. [
  1119. [],
  1120. [],
  1121. ],
  1122. [
  1123. [0],
  1124. [0],
  1125. ],
  1126. [
  1127. [1],
  1128. [-1],
  1129. ],
  1130. [
  1131. [-1],
  1132. [1],
  1133. ],
  1134. [
  1135. [1, 1],
  1136. [-1, -1],
  1137. ],
  1138. [
  1139. [-1, -1],
  1140. [1, 1],
  1141. ],
  1142. [
  1143. [1, -2, 3],
  1144. [-1, 2, -3],
  1145. ],
  1146. [
  1147. [5, 5, 5, -5, -5],
  1148. [-5, -5, -5, 5, 5],
  1149. ],
  1150. [
  1151. [23, 5, 65, 0, -4],
  1152. [-23, -5, -65, 0, 4],
  1153. ],
  1154. [
  1155. [-4, -3, 0, 0, 0],
  1156. [4, 3, 0, 0, 0],
  1157. ],
  1158. [
  1159. [-3, -4, 2, 1, 5, 5, 4, -3, 2],
  1160. [3, 4, -2, -1, -5, -5, -4, 3, -2],
  1161. ],
  1162. [
  1163. [1, 2, 3],
  1164. [-1, -2, -3],
  1165. ],
  1166. ];
  1167. }
  1168. /**
  1169. * @test Test that the proper companion matrix is calulated from a polynomial
  1170. * @dataProvider dataProviderForTestCompanionMatrix
  1171. * @param array $poly the polynomial
  1172. * @param array $companion_matrix the expected companion matrix
  1173. */
  1174. public function testCompanionMatrix(array $poly, array $expected_matrix)
  1175. {
  1176. // Create a polynomial
  1177. $poly = new Polynomial($poly);
  1178. $companion = $poly->companionMatrix();
  1179. $this->assertEqualsWithDelta($expected_matrix, $companion->getMatrix(), .0000001);
  1180. }
  1181. /**
  1182. * Data cross referenced with numpy.polynomial.polynomial.polycompanion(c)
  1183. * @return array[]
  1184. */
  1185. public function dataProviderForTestCompanionMatrix(): array
  1186. {
  1187. return [
  1188. [
  1189. [1, -21, 175, -735, 1624, -1764, 720],
  1190. [
  1191. [0, 0, 0, 0, 0, -720],
  1192. [1, 0, 0, 0, 0, 1764],
  1193. [0, 1, 0, 0, 0, -1624],
  1194. [0, 0, 1, 0, 0, 735],
  1195. [0, 0, 0, 1, 0, -175],
  1196. [0, 0, 0, 0, 1, 21],
  1197. ],
  1198. ],
  1199. [
  1200. [2, -42, 350, -1470, 3248, -3528, 1440],
  1201. [
  1202. [0, 0, 0, 0, 0, -720],
  1203. [1, 0, 0, 0, 0, 1764],
  1204. [0, 1, 0, 0, 0, -1624],
  1205. [0, 0, 1, 0, 0, 735],
  1206. [0, 0, 0, 1, 0, -175],
  1207. [0, 0, 0, 0, 1, 21],
  1208. ],
  1209. ],
  1210. [
  1211. [1, -1, -30],
  1212. [
  1213. [0, 30],
  1214. [1, 1],
  1215. ],
  1216. ],
  1217. [
  1218. [1, 5, 0],
  1219. [
  1220. [0, 0],
  1221. [1, -5],
  1222. ],
  1223. ],
  1224. ];
  1225. }
  1226. /**
  1227. * @test companionMatrix - OutOfBoundsException if the polynomial is degree 0.
  1228. * @throws \Exception
  1229. */
  1230. public function testCompanionException()
  1231. {
  1232. // Given
  1233. $poly = new Polynomial([2]);
  1234. // Then
  1235. $this->expectException(Exception\OutOfBoundsException::class);
  1236. // When
  1237. $matrix = $poly->companionMatrix();
  1238. }
  1239. }