function testAntiDerivative() { $p = new Math_Polynomial('12x^3 + 6x^2 + 2x + 4'); $first_der = Math_PolynomialOp::getDerivative($p); $anti_der = Math_PolynomialOp::getAntiDerivative($first_der, 1, 4); $this->assertTrue($anti_der->toString() == $p->toString()); }
/** * Get the nth anti-derivative of a Math_Polynomial * * Returns the nth anti-derivative of the Polynomial. An optional constant can * be passed in to have that appended as the x^0 term. * * @see PolynomialOp::getDerivative() * * @access public * @static * * @param object $p * @param integer $n * @param integer $c */ function &getAntiDerivative($p, $n = 1, $c = 0) { if (!is_a($p, 'Math_Polynomial')) { $p = new Math_Polynomial($p); } $result = new Math_Polynomial(); // This will store the Polynomial's derivative $count = $p->numTerms(); for ($i = 0; $i < $count; $i++) { // For each term, divide coefficient by exponent + 1, add one to exponent $term = $p->getTerm($i); $der_term = new Math_PolynomialTerm($term->getCoefficient() / ($term->getExponent() + 1), $term->getExponent() + 1); $result->addTerm($der_term); } for ($i = 0; $i < $n - 1; $i++) { // If we want other than the 1st derivative, keep going... $result = Math_PolynomialOp::getAntiDerivative($result); } if ($c) { $result->addTerm(new Math_PolynomialTerm($c, 0)); } return $result; }