Math_Polynomial
[ class tree: Math_Polynomial ] [ index: Math_Polynomial ] [ all elements ]

Source for file PolynomialTest.php

Documentation is available at PolynomialTest.php

  1. <?php
  2.  
  3. require_once 'Math/Polynomial.php';
  4. require_once 'Math/PolynomialOp.php';
  5. require_once 'PHPUnit.php';
  6.  
  7. class Math_PolynomialTest extends PHPUnit_TestCase
  8. {
  9.     function Math_PolynomialTest($name)
  10.     {
  11.         $this->PHPUnit_TestCase($name);
  12.     }
  13.     
  14.     function setUp()
  15.     {
  16.         ;
  17.     }
  18.     
  19.     function tearDown()
  20.     {
  21.         ;
  22.     }
  23.     
  24.     function testObjectConstructor()
  25.     {
  26.         $p = new Math_Polynomial('3x + 1');
  27.         $q = new Math_Polynomial($p);
  28.         
  29.         $this->assertTrue($p->toString(== $q->toString());
  30.     }
  31.     
  32.     function testToString()
  33.     {
  34.         $str '4x^2 + 2x + 1';
  35.         $p = new Math_Polynomial($str);
  36.         
  37.         $this->assertTrue($str == $p->toString());
  38.     }
  39.     
  40.     function testAdd()
  41.     {
  42.         $p = new Math_Polynomial('3x + 1');
  43.         $res Math_PolynomialOp::add($p'4x^2 + 2x + 1');
  44.         
  45.         $this->assertEquals('4x^2 + 5x + 2'$res->toString());
  46.     }
  47.     
  48.     function testSubtract()
  49.     {
  50.         $p = new Math_Polynomial('3x^2 - 2x + 1');
  51.         $q = new Math_Polynomial('2x^2 + 2x');
  52.         
  53.         $res Math_PolynomialOp::sub($p$q);
  54.         
  55.         $this->assertEquals('x^2 - 4x + 1'$res->toString());
  56.     }
  57.     
  58.     function testMultiply()
  59.     {
  60.         $p = new Math_Polynomial('3x + 1');
  61.         $q = new Math_Polynomial('4x^2 + 2x + 1');
  62.         $res Math_PolynomialOp::mul($p$q);
  63.         
  64.         $this->assertEquals($res->toString()'12x^3 + 10x^2 + 5x + 1');
  65.     }
  66.     
  67.     function testDivide()
  68.     {
  69.         $p = new Math_Polynomial('4x^5 + 2x^2 + 3x + 1');
  70.         $q = new Math_Polynomial('3x^2 + 1');
  71.         
  72.         $remainder = new Math_Polynomial();
  73.         
  74.         $res Math_PolynomialOp::div($p$q$remainder);
  75.         
  76.         $this->assertEquals('1.33333333333x^3 - 0.444444444444x + 0.666666666667'$res->toString());
  77.     }
  78.     
  79.     function testFirstDerivative()
  80.     {
  81.         $p = new Math_Polynomial('12x^3 + 6x^2 + 2x + 4');
  82.         $first_der Math_PolynomialOp::getDerivative($p1);
  83.         
  84.         $this->assertEquals('36x^2 + 12x + 2'$first_der->toString());
  85.     }
  86.     
  87.     function testSecondDerivative()
  88.     {
  89.         $p = new Math_Polynomial('12x^3 + 6x^2 + 2x + 4');
  90.         $der Math_PolynomialOp::getDerivative($p2);
  91.         
  92.         $this->assertEquals('72x + 12'$der->toString());
  93.     }
  94.     
  95.     function testFromRoots()
  96.     {
  97.         $p Math_PolynomialOp::createFromRoots(array(03-3));
  98.         
  99.         $this->assertEquals('x^3 - 9x'$p->toString());
  100.     }
  101.     
  102.     function testGetRootsLinear()
  103.     {
  104.         $roots Math_PolynomialOp::getRoots('5x + 10');
  105.         
  106.         $this->assertEquals(array(-2)$roots);
  107.     }
  108.     
  109.     function testGetRootsQuadratic()
  110.     {
  111.         $p = new Math_Polynomial('2x^2 + 7x - 4');
  112.         
  113.         $roots Math_PolynomialOp::getRoots($p);
  114.         
  115.         $this->assertEquals(array(0.5-4)$roots);
  116.     }
  117.     
  118.     function testGetRootsCubic()
  119.     {
  120.         $p = new Math_Polynomial('x - 6');
  121.         $p Math_PolynomialOp::mul($p'x + 1');
  122.         $p Math_PolynomialOp::mul($p'x - 3');
  123.         
  124.         $roots Math_PolynomialOp::getRootsCubic($p);
  125.         
  126.         $this->assertEquals(array(6-13)$roots);
  127.     }
  128.     
  129.     function testRootsQuartic()
  130.     {
  131.         $p = new Math_Polynomial('3x^4 + 6x^3 - 123x^2 - 126X + 1080');
  132.         
  133.         $roots Math_PolynomialOp::getRootsQuartic($p);
  134.         
  135.         $this->assertEquals(array(53-4-6)$roots);
  136.     }
  137.     
  138.     function testLocalMaximums()
  139.     {
  140.         $p = new Math_Polynomial('x^4 - 44x^3 - 66x^2 + 187x + 210');
  141.         
  142.         $maxs Math_PolynomialOp::getLocalMaximums($p);
  143.         
  144.         $this->assertEquals(array(0.796920717957544155751747894100844860076904296875)$maxs);
  145.     }
  146.     
  147.     function testLocalMinimums()
  148.     {
  149.         $p = new Math_Polynomial('x^4 - 44x^3 - 66x^2 + 187x + 210');
  150.         
  151.         $maxs Math_PolynomialOp::getLocalMinimums($p);
  152.         
  153.         $this->assertEquals(array(33.9319316690521048940354376100003719329833984375-1.7288523870096472734303461038507521152496337890625)$maxs);
  154.     }
  155.     
  156.     function testTangent()
  157.     {
  158.         $p = new Math_Polynomial('3x^3 - 2x + 2');
  159.         
  160.         $tangent Math_PolynomialOp::getTangentAt($p0.85);
  161.         
  162.         $this->assertEquals('4.5025x - 1.68475'$tangent->toString());
  163.     }
  164.     
  165.     function testRemainder()
  166.     {
  167.         
  168.     }
  169.     
  170.     function testParamConstness()
  171.     {
  172.         $p1 = new Math_Polynomial('3x + 1');
  173.         $p2 '4x^2 + 2x + 1';
  174.         $res Math_PolynomialOp::add($p1$p2);
  175.         
  176.         $this->assertEquals('4x^2 + 2x + 1'$p2);
  177.     }
  178. }
  179.  
  180. header('Content-type: text/plain');
  181.  
  182. $suite  = new PHPUnit_TestSuite('Math_PolynomialTest');
  183. $result = PHPUnit::run($suite);
  184.  
  185. print($result -> toString());
  186.   
  187. ?>

Documentation generated on Mon, 11 Mar 2019 14:35:51 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.