/** * Main program. * * @param array $args Command-line arguments. * @return integer Zero on success; non-zero on failure. */ public static function main($args) { printf("Application program number 3.\n"); $status = 0; $p1 = new PolynomialAsOrderedList(); $p1->add(new Term(4.5, 5)); $p1->add(new Term(3.2, 14)); printf("%s\n", str($p1)); $p1->differentiate(); printf("%s\n", str($p1)); $p2 = new PolynomialAsSortedList(); $p2->add(new Term(7.8, 0)); $p2->add(new Term(1.6, 14)); $p2->add(new Term(9.999000000000001, 27)); printf("%s\n", str($p2)); $p2->differentiate(); printf("%s\n", str($p2)); $p3 = new PolynomialAsSortedList(); $p3->add(new Term(0.6, 13)); $p3->add(new Term(-269.973, 26)); $p3->add(new Term(1000, 1000)); printf("%s\n", str($p3)); printf("%s\n", str($p2->plus($p3))); return $status; }
/** * Main program. * * @param array $args Command-line arguments. * @return integer Zero on success; non-zero on failure. */ public static function main($args) { printf("Application program number 4.\n"); $status = 0; $p1 = new PolynomialAsSortedList(); $p1->add(new Term(4.5, 5)); $p1->add(new Term(3.2, 14)); printf("%s\n", str($p1)); $p2 = new PolynomialAsSortedList(); $p2->add(new Term(7.8, 3)); $p2->add(new Term(1.6, 14)); $p2->add(new Term(9.999000000000001, 27)); printf("%s\n", str($p2)); $p3 = $p1->plus($p2); printf("%s\n", str($p3)); return $status; }
/** * Returns the sum of this polynomial and the specified polynomial. * This method is not implemented. * * @param object IPolynomial $poly * The polynomial to be added to this polynomial. * @return object IPolynomial * $he sum of this polynomial and the specified polynomial. */ public function plus(IPolynomial $poly) { $result = new PolynomialAsSortedList(); $p1 = $this->list->getIterator(); $p2 = $poly->list->getIterator(); $term1 = NULL; $term2 = NULL; while ($p1->valid() && $p2->valid()) { if ($term1 === NULL) { $term1 = $p1->succ(); } if ($term2 === NULL) { $term2 = $p2->succ(); } if ($term1->getExponent() < $term2->getExponent()) { $result->add(clone $term1); $term1 = NULL; } elseif ($term1->getExponent() > $term2->getExponent()) { $result->add(clone $term2); $term2 = NULL; } else { $sum = $term1->plus($term2); if ($sum->getCoefficient() != 0) { $result->add($sum); } $term1 = NULL; $term2 = NULL; } } while ($term1 !== NULL || $p1->valid()) { if ($term1 === NULL) { $term1 = $p1->succ(); } $result->add(clone $term1); $term1 = NULL; } while ($term2 !== NULL || $p2->valid()) { if ($term2 === NULL) { $term2 = $p2->succ(); } $result->add(clone $term2); $term2 = NULL; } return $result; }