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

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