/** * Get the nth derivative of a Math_Polynomial * * Returns the nth derivative of the Polynomial. Derivatives are commonly * used in calculus as they represent slopes or acceleration. To get the * first derivative, the second parameter should be a 1. For the second * derivative parameter should be a two, etc. etc. * * @see PolynomialOp::getAntiDerivative() * * @access public * * @param object $p The Polynomial object * @param integer $der_num The derivative you want (1 = 1st, 2 = 2nd, etc.) * * @return object A polynomial object representing the nth derivative */ function &getDerivative($p, $n = 1) { 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, multiply coefficient by exponent, subtract 1 from exponent $term = $p->getTerm($i); $der_term = new Math_PolynomialTerm($term->getCoefficient() * $term->getExponent(), $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::getDerivative($result); if (Math_PolynomialOp::isZero($result)) { // If derivative is ever zero, every derivative thereafter is also 0 return $result; } } return $result; }
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 />"; $der2 = Math_PolynomialOp::getDerivative($p, 2); echo 'f\'\'(x) is: ' . $der2->toString() . ' (second derivative)' . "<br />"; echo "<br />";
function testParamConstness() { $p1 = new Math_Polynomial('3x + 1'); $p2 = '4x^2 + 2x + 1'; $res = Math_PolynomialOp::add($p1, $p2); $this->assertEquals('4x^2 + 2x + 1', $p2); }