function testSubtract() { $p = new Math_Polynomial('3x^2 - 2x + 1'); $q = new Math_Polynomial('2x^2 + 2x'); $res = Math_PolynomialOp::sub($p, $q); $this->assertEquals('x^2 - 4x + 1', $res->toString()); }
//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); echo 'f\'(x) is: ' . $der1->toString() . ' (first derivative)' . "<br />";
/** * Divide one Polynomial by another, returning the result * * Divide the first polynomial by another polynomial object or a string * represention of another polynomial. Optionally, you can pass another * Polynomial object by reference to store the remainder of the division * operator. * * <code> * $a = new Polynomial("4x^2 + 2x"); * $b = new Polynomial("2x"); * $remainder = new Polynomial(); * $result = PolynomialOp::div($a, $b, $remainder); * print("A divided by B is: " . $result->toString() . " with a remainder of " . $remainder->toString() . "\n"); * </code> * * @access public * * @param object $p1 * @param object $p2 * @param object $rem * @return object */ function &div($p1, $p2, &$rem = null) { if (!is_a($p1, 'Math_Polynomial')) { $p1 = new Math_Polynomial($p1); } if (!is_a($p2, 'Math_Polynomial')) { $p2 = new Math_Polynomial($p2); } if (Math_PolynomialOp::isZero($p2)) { $err = PEAR::raiseError('Divide by zero error in Math_PolynomialOp::div().'); return $err; } if (is_null($rem)) { $remain = new Math_Polynomial(); } else { if (!is_a($rem, 'Math_Polynomial')) { $err = PEAR::raiseError('Reference remainder parameter must be a Math_Polynomial object.'); return $err; } else { $remain =& $rem; } } $res = new Math_Polynomial(); // Copy terms in $p1 over to the remainder Polynomial $count = $p1->numTerms(); for ($i = 0; $i < $count; $i++) { $remain->addTerm($p1->getTerm($i)); } /* * Method here is for each term in $p1, find out what we'd need to mult. * the first term in $p2 by to get $p1. That term goes into the result, * do the multiplication and subtract that from $p1. That gets rid of * the terms 1 by 1 until we're done. */ while (true) { $term_remain = $remain->getTerm(0); // This will change on each loop as we subtract stuff out of $remain $term_p2 = $p2->getTerm(0); // This should stay constant, we're not changing $p2 if ($term_p2->getExponent() > $remain->degree()) { // If the degree of b is larger than degree of a, there's no way b can be a factor... break; } // Next three lines could be shortened a bit... $div = new Math_PolynomialTerm($term_remain->getCoefficient() / $term_p2->getCoefficient(), $term_remain->getExponent() - $term_p2->getExponent()); $div_poly = new Math_Polynomial(); $div_poly->addTerm($div); $step_poly = Math_PolynomialOp::mul($p2, $div_poly); $remain = Math_PolynomialOp::sub($remain, $step_poly); $res->addTerm($div); if (Math_PolynomialOp::isZero($remain)) { break; } } return $res; }