12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337 |
- <?php
- namespace MathPHP\Tests\Expression;
- use MathPHP\Expression\Polynomial;
- use MathPHP\Exception;
- class PolynomialTest extends \PHPUnit\Framework\TestCase
- {
- /**
- * @test createZeroValue
- */
- public function testCreateZeroValue()
- {
- // Given
- $zero = Polynomial::createZeroValue();
- // Then
- $this->assertEquals([0], $zero->getCoefficients());
- }
- /**
- * @test String representation
- * @dataProvider dataProviderForString
- * @param array $coefficients
- * @param string $expected
- */
- public function testString(array $coefficients, string $expected)
- {
- // Given
- $polynomial = new Polynomial($coefficients);
- // When
- $string = \strval($polynomial);
- // Then
- $this->assertEquals($expected, $string);
- }
- /**
- * @return array (coefficients, string representation)
- */
- public function dataProviderForString(): array
- {
- return [
- [
- [1, 2, 3], // p(x) = x² + 2x + 3
- 'x² + 2x + 3',
- ],
- [
- [2, 3, 4], // p(x) = 2x² + 3x + 4
- '2x² + 3x + 4',
- ],
- [
- [-1, -2, -3], // p(x) = -x² - 2x - 3
- '-x² - 2x - 3',
- ],
- [
- [-2, -3, -4], // p(x) = -2x² - 3x - 4
- '-2x² - 3x - 4',
- ],
- [
- [0, 2, 3], // p(x) = 2x + 3
- '2x + 3',
- ],
- [
- [1, 0, 3], // p(x) = x² + 3
- 'x² + 3',
- ],
- [
- [1, 2, 0], // p(x) = x² + 2x
- 'x² + 2x',
- ],
- [
- [0, 0, 3], // p(x) = 3
- '3',
- ],
- [
- [1, 0, 0], // p(x) = x²
- 'x²',
- ],
- [
- [0, 2, 0], // p(x) = 2x
- '2x',
- ],
- [
- [0, -2, 3], // p(x) = -2x + 3
- '-2x + 3',
- ],
- [
- [-1, 0, 3], // p(x) = -x² + 3
- '-x² + 3',
- ],
- [
- [1, -2, 0], // p(x) = x² - 2x
- 'x² - 2x',
- ],
- [
- [0, 0, -3], // p(x) = -3
- '-3',
- ],
- [
- [-1, 0, 0], // p(x) = -x²
- '-x²',
- ],
- [
- [0, -2, 0], // p(x) = -2x
- '-2x',
- ],
- [
- [0, 0, 0], // p(x) = 0
- '0',
- ],
- [
- [0, 0, 1], // p(x) = 1
- '1',
- ],
- [
- [0, 0, 5], // p(x) = 5
- '5',
- ],
- [
- [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
- 'x¹¹ + 2x¹⁰ + 3x⁹ + 4x⁸ + 5x⁷ + 6x⁶ + 7x⁵ + 8x⁴ + 9x³ + 10x² + 11x + 12',
- ],
- ];
- }
- /**
- * @test Custom variable for string representation
- * @dataProvider dataProviderForVariable
- * @param array $args
- * @param string $expected
- */
- public function testVariable(array $args, string $expected)
- {
- // Given
- $coefficients = $args[0];
- $variable = $args[1] ?? "x";
- $polynomial = new Polynomial($coefficients, $variable);
- // When
- $string = \strval($polynomial);
- // Then
- $this->assertEquals($expected, $string);
- }
- /**
- * @return array (coefficients, string representation)
- */
- public function dataProviderForVariable(): array
- {
- return [
- [
- [[1, 2, 3]], // p(x) = x² + 2x + 3
- 'x² + 2x + 3',
- ],
- [
- [[2, 3, 4], "p"], // p(p) = 2p² + 3p + 4
- '2p² + 3p + 4',
- ],
- [
- [[-1, -2, -3], "q"], // p(q) = -q² - 2q - 3
- '-q² - 2q - 3',
- ],
- [
- [[-2, -3, -4], "a"], // p(a) = -2a² - 3a - 4
- '-2a² - 3a - 4',
- ],
- [
- [[0, 2, 3], "a"], // p(a) = 2a + 3
- '2a + 3',
- ],
- [
- [[1, 0, 3], "a"], // p(a) = a² + 3
- 'a² + 3',
- ],
- [
- [[1, 2, 0], "a"], // p(a) = a² + 2a
- 'a² + 2a',
- ],
- [
- [[0, 0, 3], "a"], // p(a) = 3
- '3',
- ],
- [
- [[1, 0, 0], "a"], // p(a) = a²
- 'a²',
- ],
- [
- [[0, 2, 0], "a"], // p(a) = 2a
- '2a',
- ],
- [
- [[0, -2, 3], "a"], // p(a) = -2a + 3
- '-2a + 3',
- ],
- [
- [[-1, 0, 3], "a"], // p(a) = -a² + 3
- '-a² + 3',
- ],
- [
- [[1, -2, 0], "a"], // p(a) = a² - 2a
- 'a² - 2a',
- ],
- [
- [[0, 0, -3], "a"], // p(a) = -3
- '-3',
- ],
- [
- [[-1, 0, 0], "a"], // p(a) = -a²
- '-a²',
- ],
- [
- [[0, -2, 0], "a"], // p(a) = -2a
- '-2a',
- ],
- [
- [[0, 0, 0], "a"], // p(a) = 0
- '0',
- ],
- [
- [[0, 0, 1], "a"], // p(a) = 1
- '1',
- ],
- [
- [[0, 0, 5], "a"], // p(a) = 5
- '5',
- ],
- [
- [[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
- 'a¹¹ + 2a¹⁰ + 3a⁹ + 4a⁸ + 5a⁷ + 6a⁶ + 7a⁵ + 8a⁴ + 9a³ + 10a² + 11a + 12',
- ],
- ];
- }
- /**
- * @test Evaluate the polynomial at some x
- * @dataProvider dataProviderForEval
- * @param array $coefficients
- * @param $x
- * @param $expected
- */
- public function testEval(array $coefficients, $x, $expected)
- {
- // Given
- $polynomial = new Polynomial($coefficients);
- // When
- $evaluated = $polynomial($x);
- // Then
- $this->assertEquals($expected, $evaluated);
- }
- /**
- * @return array (coefficients, x, y)
- */
- public function dataProviderForEval(): array
- {
- return [
- [
- [1, 2, 3], // p(x) = x² + 2x + 3
- 0, 3 // p(0) = 3
- ],
- [
- [1, 2, 3], // p(x) = x² + 2x + 3
- 1, 6 // p(1) = 6
- ],
- [
- [1, 2, 3], // p(x) = x² + 2x + 3
- 2, 11 // p(2) = 11
- ],
- [
- [1, 2, 3], // p(x) = x² + 2x + 3
- 3, 18 // p(3) = 18
- ],
- [
- [1, 2, 3], // p(x) = x² + 2x + 3
- 4, 27 // p(4) = 27
- ],
- [
- [1, 2, 3], // p(x) = x² + 2x + 3
- -1, 2 // p(-1) = 2
- ],
- [
- [0, 0, 0], // p(x) = 0
- 5, 0 // p(5) = 0
- ],
- ];
- }
- /**
- * @test Degree
- * @dataProvider dataProviderForGetDegree
- * @param array $coefficients
- * @param int $expected
- */
- public function testGetDegree(array $coefficients, int $expected)
- {
- // Given
- $polynomial = new Polynomial($coefficients);
- // When
- $degree = $polynomial->getDegree();
- // Then
- $this->assertEquals($expected, $degree);
- }
- /**
- * @return array (coefficients, degree)
- */
- public function dataProviderForGetDegree(): array
- {
- return [
- [
- [1, 2, 3], // p(x) = x² + 2x + 3
- 2,
- ],
- [
- [2, 3, 4], // p(x) = 2x² + 3x + 4
- 2
- ],
- [
- [-1, -2, -3], // p(x) = -x² - 2x - 3
- 2
- ],
- [
- [-2, -3, -4], // p(x) = -2x² - 3x - 4
- 2
- ],
- [
- [0, 2, 3], // p(x) = 2x + 3
- 1
- ],
- [
- [1, 0, 3], // p(x) = x² + 3
- 2
- ],
- [
- [1, 2, 0], // p(x) = x² + 2x
- 2
- ],
- [
- [0, 0, 3], // p(x) = 3
- 0
- ],
- [
- [1, 0, 0], // p(x) = x²
- 2
- ],
- [
- [0, 2, 0], // p(x) = 2x
- 1
- ],
- [
- [0, -2, 3], // p(x) = -2x + 3
- 1
- ],
- [
- [-1, 0, 3], // p(x) = -x² + 3
- 2
- ],
- [
- [1, -2, 0], // p(x) = x² - 2x
- 2
- ],
- [
- [0, 0, -3], // p(x) = -3
- 0
- ],
- [
- [-1, 0, 0], // p(x) = -x²
- 2
- ],
- [
- [0, -2, 0], // p(x) = -2x
- 1
- ],
- [
- [0, 0, 0], // p(x) = 0
- 0
- ],
- [
- [0, 0, 1], // p(x) = 1
- 0
- ],
- [
- [0, 0, 5], // p(x) = 5
- 0
- ],
- [
- [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
- 11
- ],
- ];
- }
- /**
- * @test coefficients
- * @dataProvider dataProviderForGetCoefficients
- * @param array $coefficients
- * @param array $expected
- */
- public function testGetCoefficients(array $coefficients, array $expected)
- {
- // Given
- $polynomial = new Polynomial($coefficients);
- // When
- $coefficients = $polynomial->getCoefficients();
- // Then
- $this->assertEquals($expected, $coefficients);
- }
- /**
- * @return array (coefficients, expected coefficients)
- */
- public function dataProviderForGetCoefficients(): array
- {
- return [
- [
- [1, 2, 3], // p(x) = x² + 2x + 3
- [1, 2, 3]
- ],
- [
- [2, 3, 4], // p(x) = 2x² + 3x + 4
- [2, 3, 4]
- ],
- [
- [-1, -2, -3], // p(x) = -x² - 2x - 3
- [-1, -2, -3]
- ],
- [
- [-2, -3, -4], // p(x) = -2x² - 3x - 4
- [-2, -3, -4]
- ],
- [
- [0, 2, 3], // p(x) = 2x + 3
- [2, 3]
- ],
- [
- [1, 0, 3], // p(x) = x² + 3
- [1, 0, 3]
- ],
- [
- [1, 2, 0], // p(x) = x² + 2x
- [1, 2, 0]
- ],
- [
- [0, 0, 3], // p(x) = 3
- [3]
- ],
- [
- [1, 0, 0], // p(x) = x²
- [1, 0, 0]
- ],
- [
- [0, 2, 0], // p(x) = 2x
- [2, 0]
- ],
- [
- [0, -2, 3], // p(x) = -2x + 3
- [-2, 3]
- ],
- [
- [-1, 0, 3], // p(x) = -x² + 3
- [-1, 0, 3]
- ],
- [
- [1, -2, 0], // p(x) = x² - 2x
- [1, -2, 0]
- ],
- [
- [0, 0, -3], // p(x) = -3
- [-3]
- ],
- [
- [-1, 0, 0], // p(x) = -x²
- [-1, 0, 0]
- ],
- [
- [0, -2, 0], // p(x) = -2x
- [-2, 0]
- ],
- [
- [0, 0, 0], // p(x) = 0
- [0]
- ],
- [
- [0, 0, 1], // p(x) = 1
- [1]
- ],
- [
- [0, 0, 5], // p(x) = 5
- [5]
- ],
- [
- [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
- [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
- ],
- ];
- }
- /**
- * @test Get variable
- * @dataProvider dataProviderForGetVariable
- * @param array $args
- * @param string $expected
- */
- public function testGetVariable(array $args, string $expected)
- {
- // Given
- $coefficients = $args[0];
- $variable = $args[1] ?? "x";
- $polynomial = new Polynomial($coefficients, $variable);
- // When
- $result = $polynomial->getVariable();
- // Then
- $this->assertEquals($expected, $result);
- }
- /**
- * @return array
- */
- public function dataProviderForGetVariable(): array
- {
- return [
- [
- [[1, 2, 3]], // p(x) = x² + 2x + 3
- 'x',
- ],
- [
- [[2, 3, 4], "p"], // p(p) = 2p² + 3p + 4
- 'p',
- ],
- [
- [[-1, -2, -3], "m"], // p(m) = -m² - 2m - 3
- 'm',
- ],
- [
- [[-2, -3, -4], "a"], // p(a) = -2a² - 3a - 4
- 'a',
- ],
- [
- [[0, 2, 3], "Δ"], // p(Δ) = 2Δ + 3
- 'Δ',
- ],
- [
- [[1, 0, 3], "Γ"], // p(Γ) = Γ² + 3
- 'Γ',
- ],
- [
- [[1, 2, 0], "Ψ"], // p(a) = Ψ² + 2Ψ
- 'Ψ',
- ],
- [
- [[0, 0, 3], "μ"], // p(μ) = 3
- 'μ',
- ],
- [
- [[1, 0, 0], "ξ"], // p(ξ) = ξ²
- 'ξ',
- ],
- [
- [[0, 2, 0], "aₙ"], // p(aₙ) = 2aₙ
- 'aₙ',
- ],
- [
- [[0, -2, 3], "aⁿ"], // p(aⁿ) = -2aⁿ + 3
- 'aⁿ',
- ],
- [
- [[-1, 0, 3], "a₍ₘ₎₍ₙ₎"], // p(a) = -a₍ₘ₎₍ₙ₎² + 3
- 'a₍ₘ₎₍ₙ₎',
- ],
- ];
- }
- /**
- * @test Set variable
- */
- public function testSetVariable()
- {
- // Given default variable: x
- $polynomial = new Polynomial([1, 1, 1, 1]);
- $expected = "x";
- $result = $polynomial->getVariable();
- $this->assertEquals($expected, $result);
- $expected = "x³ + x² + x + 1";
- $result = \strval($polynomial);
- $this->assertEquals($expected, $result);
- // Given we switch variable to Φ
- $polynomial->setVariable("Φ");
- $expected = "Φ";
- $result = $polynomial->getVariable();
- $this->assertEquals($expected, $result);
- $expected = "Φ³ + Φ² + Φ + 1";
- $result = \strval($polynomial);
- $this->assertEquals($expected, $result);
- // Given we switch variable back to x
- $polynomial->setVariable("x");
- $expected = "x";
- $result = $polynomial->getVariable();
- $this->assertEquals($expected, $result);
- $expected = "x³ + x² + x + 1";
- $result = \strval($polynomial);
- $this->assertEquals($expected, $result);
- }
- /**
- * @test Differentiate
- * @dataProvider dataProviderForDifferentiate
- * @param array $polynomial
- * @param array $expected
- */
- public function testDifferentiation(array $polynomial, array $expected)
- {
- // Given
- $polynomial = new Polynomial($polynomial);
- $expected = new Polynomial($expected);
- // When
- $derivative = $polynomial->differentiate();
- // Then
- $this->assertEquals($expected, $derivative);
- }
- /**
- * @return array (coefficients, derivative)
- */
- public function dataProviderForDifferentiate(): array
- {
- return [
- [
- [1, 2, 3], // p(x) = x² + 2x + 3
- [2, 2] // p'(x) = 2x + 2
- ],
- [
- [2, 3, 4], // p(x) = 2x² + 3x + 4
- [4, 3] // p'(x) = 4x + 3
- ],
- [
- [1, 0], // p(x) = x
- [1] // p'(x) = 1
- ],
- [
- [5, 0], // p(x) = 5x
- [5] // p'(x) = 5
- ],
- [
- [1, 0, 0], // p(x) = x²
- [2, 0] // p'(x) = 2x
- ],
- [
- [5], // p(x) = 5
- [0] // p'(x) = 0
- ],
- [
- [1], // p(x) = 1
- [0] // p'(x) = 0
- ],
- [
- [0], // p(x) = 0
- [0] // p'(x) = 0
- ],
- ];
- }
- /**
- * @test Integration
- * @dataProvider dataProviderForIntegrate
- * @param array $polynomial
- * @param array $expected_integral
- */
- public function testIntegration(array $polynomial, array $expected_integral)
- {
- // Given
- $polynomial = new Polynomial($polynomial);
- $expected = new Polynomial($expected_integral);
- // When
- $integral = $polynomial->integrate();
- // Then
- $this->assertEquals($expected, $integral);
- }
- /**
- * @return array (coefficients, integral)
- */
- public function dataProviderForIntegrate(): array
- {
- return [
- [
- [1, 2, 3], // f(x) = x² + 2x + 3
- [1 / 3, 1, 3, 0], // ∫f(x) = (1/3)x³ + x² + 3x
- ],
- [
- [5], // f(x) = 5
- [5, 0], // ∫f(x) = 5x
- ],
- [
- [0], // f(x) = 0
- [0, 0], // ∫f(x) = 0x
- ],
- [
- [1, 0], // f(x) = x
- [1 / 2, 0, 0], // ∫f(x) = (1/2)²
- ],
- ];
- }
- /**
- * @test Fundamental theorem of calculus
- */
- public function testFundamentalTheoremOfCalculus()
- {
- // Given p(x) = x² + 2x + 3
- $polynomial = new Polynomial([1, 2, 3]);
- $expected = $polynomial;
- // When
- $integral = $polynomial->integrate();
- $actual = $integral->differentiate();
- // Then
- $this->assertEquals($expected, $actual);
- }
- /**
- * @test Addition
- * @dataProvider dataProviderForAddition
- * @param array $polynomialA
- * @param array $polynomialB
- * @param array $expected_sum
- * @throws \Exception
- */
- public function testAddition(array $polynomialA, array $polynomialB, array $expected_sum)
- {
- // Given
- $polynomialA = new Polynomial($polynomialA);
- $polynomialB = new Polynomial($polynomialB);
- $expected = new Polynomial($expected_sum);
- // When
- $sum = $polynomialA->add($polynomialB);
- // Then
- $this->assertEquals($expected, $sum);
- }
- /**
- * @return array (p1, p2, sum)
- */
- public function dataProviderForAddition(): array
- {
- return [
- [
- [1, 2, 3], // f(x) = x² + 2x + 3
- [1, 2, 3], // g(x) = x² + 2x + 3
- [2, 4, 6], // f(x)+g(x) = 2x² + 4x + 6
- ],
- [
- [1, 2, 3], // f(x) = x² + 2x + 3
- [2, 3, 1], // g(x) = 2x² + 3x + 1
- [3, 5, 4], // f(x)+g(x) = 3x² + 5x + 4
- ],
- [
- [1, 2, 3, 4, 4], // f(x) = x⁴ + 2x³ + 3x² + 4x + 4
- [2, 3, 1], // g(x) = 2x² + 3x + 1
- [1, 2, 5, 7, 5], // f(x)+g(x) = x⁴ + 2x³ + 5x² + 7x + 5
- ],
- [
- [2, 3, 1], // f(x) = 2x² + 3x + 1
- [1, 2, 3, 4, 4], // g(x) = x⁴ + 2x³ + 3x² + 4x + 4
- [1, 2, 5, 7, 5], // f(x)+g(x) = x⁴ + 2x³ + 5x² + 7x + 5
- ],
- [
- [1, -8, 12, 3], // f(x) = x³ - 8x² + 12x + 3
- [1, -8, 12, 3], // g(x) = f(x)
- [2, -16, 24, 6], // f(x)+g(x) = 2x³ - 16x² + 24x + 6
- ],
- ];
- }
- /**
- * @test Subtraction
- * @dataProvider dataProviderForSubtraction
- * @param array $polynomialA
- * @param array $polynomialB
- * @param array $expected_sum
- * @throws \Exception
- */
- public function testSubtraction(array $polynomialA, array $polynomialB, array $expected_sum)
- {
- // Given
- $polynomialA = new Polynomial($polynomialA);
- $polynomialB = new Polynomial($polynomialB);
- $expected = new Polynomial($expected_sum);
- // When
- $difference = $polynomialA->subtract($polynomialB);
- // Then
- $this->assertEquals($expected, $difference);
- }
- /**
- * @return array (p1, p2, difference)
- */
- public function dataProviderForSubtraction(): array
- {
- return [
- [
- [1, 2, 3], // f(x) = x² + 2x + 3
- [1, 2, 3], // g(x) = x² + 2x + 3
- [0, 0, 0], // f(x)-g(x) = 0
- ],
- [
- [1, 2, 3], // f(x) = x² + 2x + 3
- [2, 3, 1], // g(x) = 2x² + 3x + 1
- [-1, -1, 2], // f(x)-g(x) = -x² - x + 2
- ],
- [
- [1, 2, 3, 4, 4], // f(x) = x⁴ + 2x³ + 3x² + 4x + 4
- [2, 3, 1], // g(x) = 2x² + 3x + 1
- [1, 2, 1, 1, 3], // f(x)-g(x) = x⁴ + 2x³ + x² + x + 3
- ],
- [
- [1, -8, 12, 3], // f(x) = x³ - 8x² + 12x + 3
- [1, -8, 12, 3], // g(x) = f(x)
- [0, 0, 0, 0], // f(x)-g(x) = 0
- ],
- ];
- }
- /**
- * @test Multiplication
- * @dataProvider dataProviderForMultiplication
- * @param array $polynomialA
- * @param array $polynomialB
- * @param array $expected_product
- * @throws \Exception
- */
- public function testMultiplication(array $polynomialA, array $polynomialB, array $expected_product)
- {
- // Given
- $polynomialA = new Polynomial($polynomialA);
- $polynomialB = new Polynomial($polynomialB);
- $expected = new Polynomial($expected_product);
- // When
- $product = $polynomialA->multiply($polynomialB);
- // Then
- $this->assertEquals($expected, $product);
- }
- /**
- * @return array (p1, p2, product)
- */
- public function dataProviderForMultiplication(): array
- {
- return [
- [
- [1, 2, 3], // f(x) = x² + 2x + 3
- [1, 2, 3], // g(x) = x² + 2x + 3
- [1, 4, 10, 12, 9], // f(x)*g(x) = x⁴ + 4x³ + 10x² + 12x + 9
- ],
- [
- [1, 2, 3], // f(x) = x² + 2x + 3
- [2, 3, 1], // g(x) = 2x² + 3x + 1
- [2, 7, 13, 11, 3], // f(x)*g(x) = 2x⁴ + 7x³ + 13x² + 11x + 3
- ],
- [
- [1, 2, 3, 4, 4], // f(x) = x⁴ + 2x³ + 3x² + 4x + 4
- [2, 3, 1], // g(x) = 2x² + 3x + 1
- [2, 7, 13, 19, 23, 16, 4], // f(x)*g(x) = 2x⁶ + 7x⁵ + 13x⁴ + 19x³ + 23x² + 16x + 4
- ],
- [
- [1, -8, 12, 3], // f(x) = x³ - 8x² + 12x + 3
- [1, -8, 12, 3], // g(x) = f(x)
- [1, -16, 88, -186, 96, 72, 9], // f(x)+g(x) = x⁶ - 16x⁵ + 88x⁴ - 186x³ + 96x² + 72x + 9
- ],
- ];
- }
- /**
- * @test Scalar addition
- * @dataProvider dataProviderForScalarAddition
- * @param array $polynomialA
- * @param int $scaler
- * @param array $expected_product
- * @throws \Exception
- */
- public function testScalarAddition(array $polynomialA, int $scaler, array $expected_product)
- {
- // Given
- $polynomialA = new Polynomial($polynomialA);
- $expected = new Polynomial($expected_product);
- // When
- $sum = $polynomialA->add($scaler);
- // Then
- $this->assertEquals($expected, $sum);
- }
- /**
- * @return array (p1, scalar, sum)
- */
- public function dataProviderForScalarAddition(): array
- {
- return [
- [
- [1, 2, 3], // f(x) = x² + 2x + 3
- 2,
- [1, 2, 5], // f(x)*c = x² + 2x + 5
- ],
- [
- [1, 2, 3, 4, 4], // f(x) = x⁴ + 2x³ + 3x² + 4x + 4
- -2,
- [1, 2, 3, 4, 2], // f(x)*c = 1x⁴ + 2x³ + 3x² + 4x + 2
- ],
- ];
- }
- /**
- * @test Scalar subtraction
- * @dataProvider dataProviderForScalarSubtraction
- * @param array $polynomialA
- * @param int $scaler
- * @param array $expected_product
- * @throws \Exception
- */
- public function testScalarSubtraction(array $polynomialA, int $scaler, array $expected_product)
- {
- // Given
- $polynomialA = new Polynomial($polynomialA);
- $expected = new Polynomial($expected_product);
- // When
- $difference = $polynomialA->subtract($scaler);
- // Then
- $this->assertEquals($expected, $difference);
- }
- /**
- * @return array (p1, scalar, difference)
- */
- public function dataProviderForScalarSubtraction(): array
- {
- return [
- [
- [1, 2, 3], // f(x) = x² + 2x + 3
- 2,
- [1, 2, 1], // f(x)*c = x² + 2x + 1
- ],
- [
- [1, 2, 3, 4, 4], // f(x) = x⁴ + 2x³ + 3x² + 4x + 4
- -2,
- [1, 2, 3, 4, 6], // f(x)*c = 1x⁴ + 2x³ + 3x² + 4x + 6
- ],
- ];
- }
- /**
- * @test Scalar multiplication
- * @dataProvider dataProviderForScalarMultiplication
- * @param array $polynomialA
- * @param int $scaler
- * @param array $expected_product
- * @throws \Exception
- */
- public function testScalarMultiplication(array $polynomialA, int $scaler, array $expected_product)
- {
- // Given
- $polynomialA = new Polynomial($polynomialA);
- $expected = new Polynomial($expected_product);
- // When
- $product = $polynomialA->multiply($scaler);
- // Then
- $this->assertEquals($expected, $product);
- }
- /**
- * @return array (p1, scalar, product)
- */
- public function dataProviderForScalarMultiplication(): array
- {
- return [
- [
- [1, 2, 3], // f(x) = x² + 2x + 3
- 2,
- [2, 4, 6], // f(x)*c = 2x² + 4x + 6
- ],
- [
- [1, 2, 3, 4, 4], // f(x) = x⁴ + 2x³ + 3x² + 4x + 4
- -2,
- [-2, -4, -6, -8, -8], // f(x)*c = -2x⁴ - 4x³ - 6x² - 8x - 8
- ],
- ];
- }
- /**
- * @test roots
- * @dataProvider dataProviderForRoots
- * @param array $polynomialA
- * @param array $expected_roots
- * @throws \Exception
- */
- public function testRoots(array $polynomialA, array $expected_roots)
- {
- // Given
- $polynomialA = new Polynomial($polynomialA);
- // When
- $roots = $polynomialA->roots();
- // Then
- $this->assertEqualsWithDelta($expected_roots, $roots, 0.00001);
- }
- /**
- * @return array
- */
- public function dataProviderForRoots(): array
- {
- return [
- // Degree 0
- [
- [0],
- [null],
- ],
- [
- [3],
- [null],
- ],
- [
- [0, -3],
- [null],
- ],
- // Degree 1
- [
- [1, -3],
- [3],
- ],
- [
- [2, 0],
- [0],
- ],
- // Degree 2
- [
- [1, -3, -4],
- [-1, 4],
- ],
- // Degree 3
- [
- [1, -6, 11, -6],
- [3, 1, 2],
- ],
- // Degree 4
- [
- [1, -10, 35, -50, 24],
- [4, 1, 3, 2],
- ],
- ];
- }
- /**
- * @test roots NAN - Closed form solutions don't exist for degrees 5 or higher - no implementation
- * @dataProvider dataProviderForRootsNAN
- * @param array $polynomialA
- * @throws \Exception
- */
- public function testRootsNAN(array $polynomialA)
- {
- // Given
- $polynomialA = new Polynomial($polynomialA);
- // When
- $roots = $polynomialA->roots();
- // Then
- $this->assertCount(1, $roots);
- $this->assertNan($roots[0]);
- }
- /**
- * @return array
- */
- public function dataProviderForRootsNAN(): array
- {
- return [
- 'degree 5' => [
- [1, -3, -4, 5, 5, 5],
- ],
- 'degree 6' => [
- [1, 2, 3, 4, 5, 6, 7],
- ],
- ];
- }
- /**
- * @test add - IncorrectTypeException if the argument is not numeric or a Polynomial
- * @throws \Exception
- */
- public function testException()
- {
- // Given
- $string = 'This is a string!';
- $poly = new Polynomial([1, 2]);
- // Then
- $this->expectException(Exception\IncorrectTypeException::class);
- // When
- $sum = $poly->add($string);
- }
- /**
- * @test checkNumericOrPolynomial returns a Polynomial for numeric and Polynomial inputs
- * @dataProvider dataProviderForCheckNumericOrPolynomial
- */
- public function testCheckNumericOrPolynomialNumericInput($input)
- {
- // Given
- $method = new \ReflectionMethod(Polynomial::class, 'checkNumericOrPolynomial');
- $method->setAccessible(true);
- // When
- $polynomial = $method->invokeArgs(new Polynomial([1]), [$input]);
- // Then
- $this->assertInstanceOf(Polynomial::class, $polynomial);
- }
- public function dataProviderForCheckNumericOrPolynomial(): array
- {
- return [
- [-1],
- [0],
- [1],
- [10],
- [2.45],
- ['3'],
- ['5.4'],
- [new Polynomial([4])],
- [new Polynomial([2, 3, 4])],
- ];
- }
- /**
- * @test checkNumericOrPolynomial throws an IncorrectTypeException if the input is not numeric or a Polynomial
- */
- public function testCheckNumericOrPolynomialException()
- {
- // Given
- $method = new \ReflectionMethod(Polynomial::class, 'checkNumericOrPolynomial');
- $method->setAccessible(true);
- // Then
- $this->expectException(Exception\IncorrectTypeException::class);
- // When
- $polynomial = $method->invokeArgs(new Polynomial([1]), ['not a number']);
- }
- /**
- * @test negate returns a Polynomial with every coefficient negated
- * @dataProvider dataProviderForNegate
- * @param array $polynomial
- * @param array $expected_negated_polynomial
- */
- public function testNegate(array $polynomial, array $expected_negated_polynomial)
- {
- // Given
- $polynomial = new Polynomial($polynomial);
- $expected = new Polynomial($expected_negated_polynomial);
- // When
- $negated = $polynomial->negate();
- // Then
- $this->assertEquals($expected, $negated);
- }
- /**
- * @return array
- */
- public function dataProviderForNegate(): array
- {
- return [
- [
- [],
- [],
- ],
- [
- [0],
- [0],
- ],
- [
- [1],
- [-1],
- ],
- [
- [-1],
- [1],
- ],
- [
- [1, 1],
- [-1, -1],
- ],
- [
- [-1, -1],
- [1, 1],
- ],
- [
- [1, -2, 3],
- [-1, 2, -3],
- ],
- [
- [5, 5, 5, -5, -5],
- [-5, -5, -5, 5, 5],
- ],
- [
- [23, 5, 65, 0, -4],
- [-23, -5, -65, 0, 4],
- ],
- [
- [-4, -3, 0, 0, 0],
- [4, 3, 0, 0, 0],
- ],
- [
- [-3, -4, 2, 1, 5, 5, 4, -3, 2],
- [3, 4, -2, -1, -5, -5, -4, 3, -2],
- ],
- [
- [1, 2, 3],
- [-1, -2, -3],
- ],
- ];
- }
- /**
- * @test Test that the proper companion matrix is calulated from a polynomial
- * @dataProvider dataProviderForTestCompanionMatrix
- * @param array $poly the polynomial
- * @param array $companion_matrix the expected companion matrix
- */
- public function testCompanionMatrix(array $poly, array $expected_matrix)
- {
- // Create a polynomial
- $poly = new Polynomial($poly);
- $companion = $poly->companionMatrix();
- $this->assertEqualsWithDelta($expected_matrix, $companion->getMatrix(), .0000001);
- }
- /**
- * Data cross referenced with numpy.polynomial.polynomial.polycompanion(c)
- * @return array[]
- */
- public function dataProviderForTestCompanionMatrix(): array
- {
- return [
- [
- [1, -21, 175, -735, 1624, -1764, 720],
- [
- [0, 0, 0, 0, 0, -720],
- [1, 0, 0, 0, 0, 1764],
- [0, 1, 0, 0, 0, -1624],
- [0, 0, 1, 0, 0, 735],
- [0, 0, 0, 1, 0, -175],
- [0, 0, 0, 0, 1, 21],
- ],
- ],
- [
- [2, -42, 350, -1470, 3248, -3528, 1440],
- [
- [0, 0, 0, 0, 0, -720],
- [1, 0, 0, 0, 0, 1764],
- [0, 1, 0, 0, 0, -1624],
- [0, 0, 1, 0, 0, 735],
- [0, 0, 0, 1, 0, -175],
- [0, 0, 0, 0, 1, 21],
- ],
- ],
- [
- [1, -1, -30],
- [
- [0, 30],
- [1, 1],
- ],
- ],
- [
- [1, 5, 0],
- [
- [0, 0],
- [1, -5],
- ],
- ],
- ];
- }
- /**
- * @test companionMatrix - OutOfBoundsException if the polynomial is degree 0.
- * @throws \Exception
- */
- public function testCompanionException()
- {
- // Given
- $poly = new Polynomial([2]);
- // Then
- $this->expectException(Exception\OutOfBoundsException::class);
- // When
- $matrix = $poly->companionMatrix();
- }
- }
|