Пример #1
0
 /**
  * Create a Polynomial object which has roots (zeros) provided as parameters
  * 
  * The roots can be passed in as either a variable length parameter list or 
  * a single array of float values.  
  * 
  * @access public
  * 
  * @param array $arr An array of roots
  * @return object
  */
 function &createFromRoots($arr)
 {
     if (is_array($arr)) {
         // An array of floats/integers
         $count = count($arr);
         if ($count > 0) {
             $res = new Math_Polynomial('x - ' . (double) $arr[0]);
             // Create the initial Polynomial with 0th index
         }
         for ($i = 1; $i < $count; $i++) {
             // Start at 1, 0th index already used to create Polynomial
             $res = Math_PolynomialOp::mul($res, 'x - ' . (double) $arr[$i]);
         }
     } else {
         // Its a variable length parameter list of values
         $res = new Math_Polynomial('x - ' . (double) $arr);
         for ($i = 1; $i < func_num_args(); $i++) {
             $arg = (double) func_get_arg($i);
             $res = Math_PolynomialOp::mul($res, 'x - ' . $arg);
         }
     }
     return $res;
 }
<?php

//header('Content-type: text/plain');
include 'Math/Polynomial.php';
include 'Math/PolynomialOp.php';
echo "<br />-- Algebra --<br />";
$p = new Math_Polynomial('3x^2 + 2x');
$q = new Math_Polynomial('4x + 1');
echo 'P is: ' . $p->toString() . "<br />";
echo 'Q is: ' . $q->toString() . "<br />";
$mul = Math_PolynomialOp::mul($p, $q);
// Multiply p by q
echo 'P multiplied by Q is: ' . $mul->toString() . "<br />";
// Print string representation
echo 'The degree of that result is: ' . $mul->degree() . "<br />";
echo 'That result evaluated at x = 10 is: ' . number_format(Math_PolynomialOp::evaluate($mul, 10)) . "<br />";
$sub = Math_PolynomialOp::sub($p, $q);
echo 'P minus Q is: ' . $sub->toString() . "<br />";
$r = new Math_Polynomial('3x^3 - 5x^2 + 10x-3');
$s = new Math_Polynomial('3x+1');
$remainder = new Math_Polynomial();
echo 'R is: ' . $r->toString() . "<br />";
echo 'S is: ' . $s->toString() . "<br />";
$div = Math_PolynomialOp::div($r, $s, $remainder);
echo 'R divided by S is: ' . $div->toString() . ' ( remainder of: ' . $remainder->toString() . ' )' . "<br />";
echo "<br />-- Creating Polynomials --<br />";
$roots = Math_PolynomialOp::createFromRoots(1, 2, -3);
echo 'Here is a polynomial with the roots 1, 2, and -3: ' . $roots->toString() . "<br />";
echo "<br />-- Derivatives --<br />";
echo 'f(x) is: ' . $p->toString() . "<br />";
$der1 = Math_PolynomialOp::getDerivative($p);
Пример #3
0
 function testGetRootsCubic()
 {
     $p = new Math_Polynomial('x - 6');
     $p = Math_PolynomialOp::mul($p, 'x + 1');
     $p = Math_PolynomialOp::mul($p, 'x - 3');
     $roots = Math_PolynomialOp::getRootsCubic($p);
     $this->assertEquals(array(6, -1, 3), $roots);
 }