/** * Estimate and return the roots of a high-degree Polynomial ( degree 5 or greater ) * * This function uses Newton's method using the Math_Numerical_RootFinding * PEAR package to estimate the real roots of high-degree Polynomials. If * you already have estimates of where the roots might be, you can pass in * an array of guesses. Otherwise, the method will try to calculate some * good initial guesses for you. * * You must have the Math_Numerical_RootFinding package installed for this * method to work! * * @see Math_PolynomialOp::getRoots(), Math_Numerical_RootFinding * * @access public * * @param object $m * @param array $guesses * @return array */ function getRootsHighDegree($p, $guesses = array()) { require_once 'Math/Numerical/RootFinding.php'; if (!is_a($p, 'Math_Polynomial')) { $p = new Math_Polynomial($p); } $arr = array(); // Array to store the roots $fx = Math_PolynomialOp::createFunction($p); $dx = Math_PolynomialOp::createFunction(Math_PolynomialOp::getDerivative($p)); $newton = Math_Numerical_RootFinding::factory('Newtonraphson'); if (PEAR::isError($newton)) { return PEAR::raiseError('Math_Numeric_RootFinding could not be instantiated, message was: ' . $newton->toString()); } // We need to find some initial guesses for finding the roots if (count($guesses)) { // User has provided guesses for us foreach ($guesses as $guess) { $arr[] = $newton->compute($fx, $dx, $guess); } } else { // We need to find the guesses ourselves... yuck. $criticals = Math_PolynomialOp::getCriticalPoints($p); $arr[] = $newton->compute($fx, $dx, $criticals[0] - 0.1); $count = count($criticals); for ($i = 1; $i < $count; $i++) { $arr[] = $newton->compute($fx, $dx, ($criticals[$i - 1] + $criticals[$i]) / 2); } $arr[] = $newton->compute($fx, $dx, end($criticals) + 0.1); $arr = array_unique($arr); } return Math_PolynomialOp::_round($arr); }