function testDivide() { $p = new Math_Polynomial('4x^5 + 2x^2 + 3x + 1'); $q = new Math_Polynomial('3x^2 + 1'); $remainder = new Math_Polynomial(); $res = Math_PolynomialOp::div($p, $q, $remainder); $this->assertEquals('1.33333333333x^3 - 0.444444444444x + 0.666666666667', $res->toString()); }
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 />";
include 'Math/PolynomialOp.php'; print "\n-- Algebra --\n"; $p = new Math_Polynomial('3x^2 + 2x'); $q = new Math_Polynomial('4x + 1'); print 'P is: ' . $p->toString() . "\n"; print 'Q is: ' . $q->toString() . "\n"; $mul = Math_PolynomialOp::mul($p, $q); // Multiply p by q print 'P multiplied by Q is: ' . $mul->toString() . "\n"; // Print string representation print 'The degree of that result is: ' . $mul->degree() . "\n"; print 'That result evaluated at x = 10 is: ' . number_format(Math_PolynomialOp::evaluate($mul, 10)) . "\n"; $sub = Math_PolynomialOp::sub($p, $q); print 'P minus Q is: ' . $sub->toString() . "\n"; $r = new Math_Polynomial('3x^3 - 5x^2 + 10x-3'); $s = new Math_Polynomial('3x+1'); $remainder = new Math_Polynomial(); print 'R is: ' . $r->toString() . "\n"; print 'S is: ' . $s->toString() . "\n"; $div = Math_PolynomialOp::div($r, $s, &$t); print 'R divided by S is: ' . $div->toString() . ' ( remainder of: ' . $remainder->toString() . ' )' . "\n"; print "\n-- Creating Polynomials --\n"; $roots = Math_PolynomialOp::createFromRoots(1, 2, -3); print 'Here is a polynomial with the roots 1, 2, and -3: ' . $roots->toString() . "\n"; print "\n-- Derivatives --\n"; print 'f(x) is: ' . $p->toString() . "\n"; $der1 = Math_PolynomialOp::getDerivative($p); print 'f\'(x) is: ' . $der1->toString() . ' (first derivative)' . "\n"; $der2 = Math_PolynomialOp::getDerivative($p, 2); print 'f\'\'(x) is: ' . $der2->toString() . ' (second derivative)' . "\n"; print "\n";
/** * Find and return the roots of a Quartic Polynomial (degree 4) with the Quartic formula * * @see Math_PolynomialOp::getRoots() * * @access public * * @param object $p * @return array */ function getRootsQuartic($p) { if (!is_a($p, 'Math_Polynomial')) { $p = new Math_Polynomial($p); } if ($p->degree() == 4) { $arr = array(); // Array of roots // Simplify it a bit first $a_term = $p->getTerm(0); $p = Math_PolynomialOp::div($p, $a_term->getCoefficient()); $a = 0; $b = 0; $c = 0; $d = 0; $e = 0; $num_terms = $p->numTerms(); for ($i = 0; $i < $num_terms; $i++) { $term = $p->getTerm($i); if ($term->getExponent() == 4) { $a = $term->getCoefficient(); } else { if ($term->getExponent() == 3) { $b = $term->getCoefficient(); } else { if ($term->getExponent() == 2) { $c = $term->getCoefficient(); } else { if ($term->getExponent() == 1) { $d = $term->getCoefficient(); } else { if ($term->getExponent() == 0) { $e = $term->getCoefficient(); } } } } } } $f = $c - 3 * $b * $b / 8; $g = $d + pow($b, 3) / 8 - $b * $c / 2; $h = $e - 3 * pow($b, 4) / 256 + $b * $b * ($c / 16) - $b * $d / 4; $cubic = new Math_Polynomial('x^3 + ' . $f / 2 . 'x^2 + ' . (pow($f, 2) - 4 * $h) / 16 . 'x - ' . pow($g, 2) / 64); $p = 0; $q = 0; foreach (Math_PolynomialOp::getRootsCubic($cubic) as $p_or_q) { if ($p == 0 && $p_or_q != 0) { $p = sqrt($p_or_q); } else { if ($q == 0 && $p_or_q != 0) { $q = sqrt($p_or_q); } } } if ($p != 0 && $q != 0) { $r = -1 * $g / (8 * $p * $q); $s = $b / (4 * $a); $arr[] = $p + $q + $r - $s; $arr[] = $p - $q - $r - $s; $arr[] = -1 * $p + $q - $r - $s; $arr[] = -1 * $p - $q + $r - $s; } return Math_PolynomialOp::_round($arr); } else { return PEAR::raiseError('Parameter to Math_PolynomialOp::getRootsQuartic() is not quartic.'); } }