Exemplo n.º 1
0
 /**
  * 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;
 }