From eae32133ae95c4594d416f30b8ac3602e52d77c3 Mon Sep 17 00:00:00 2001 From: guido Date: Tue, 16 Dec 2003 11:26:59 +0000 Subject: [PATCH] arithmetic with polynomials; monomials git-svn-id: https://svn.dealii.org/trunk@8266 0785d39b-7218-0410-832d-ea1e28bc413d --- deal.II/base/include/base/polynomial.h | 63 +++++++++++++++++++- deal.II/base/source/polynomial.cc | 79 ++++++++++++++++++++++++++ 2 files changed, 141 insertions(+), 1 deletion(-) diff --git a/deal.II/base/include/base/polynomial.h b/deal.II/base/include/base/polynomial.h index 4fa29bab0f..12b0983f6c 100644 --- a/deal.II/base/include/base/polynomial.h +++ b/deal.II/base/include/base/polynomial.h @@ -58,7 +58,12 @@ namespace Polynomials * minus one. */ Polynomial (const std::vector &coefficients); - + + /** + * Default constructor creating an illegal object. + */ + Polynomial (); + /** * Return the value of this * polynomial at the given point. @@ -144,6 +149,27 @@ namespace Polynomials template void shift (const number2 offset); + /** + * Compute the derivative of + * the polynomial. + */ + Polynomial derivative () const; + + /** + * Multiply with a scalar. + */ + Polynomial& operator *= (const double s); + + /** + * Add a second polynomial. + */ + Polynomial& operator += (const Polynomial& p); + + /** + * Subtract a second polynomial. + */ + Polynomial& operator -= (const Polynomial& p); + /** * Print coefficients. */ @@ -198,6 +224,36 @@ namespace Polynomials }; +/** + * @brief Monomial of degree n. + * + * Class generates Polynomial objects representing a monomial of + * degree n, that is, the function $x^n$. + * + * @author Guido Kanschat, 2004 + */ + template + class Monomial : + public Polynomial + { + public: + /** + * Constructor, taking the + * degree of the monomial and + * an optional coefficient as + * arguments. + */ + Monomial(const unsigned int n, + const double coefficient = 1.); + + private: + /** + * Needed by constructor. + */ + static std::vector make_vector(unsigned int n, + const double coefficient); + }; + /** * Lagrange polynomials with equidistant interpolation points in @@ -441,6 +497,11 @@ namespace Polynomials namespace Polynomials { + template + inline + Polynomial::Polynomial () + {} + template inline unsigned int diff --git a/deal.II/base/source/polynomial.cc b/deal.II/base/source/polynomial.cc index 9266f9f3bf..c6b4854c8d 100644 --- a/deal.II/base/source/polynomial.cc +++ b/deal.II/base/source/polynomial.cc @@ -156,6 +156,46 @@ namespace Polynomials + template + Polynomial& + Polynomial::operator *= (const double s) + { + for (typename std::vector::iterator c = coefficients.begin(); + c != coefficients.end(); ++c) + *c *= s; + return *this; + } + + + template + Polynomial& + Polynomial::operator += (const Polynomial& p) + { +//TODO:[GK] Is resize correct? + if (p.degree() > degree()) + coefficients.resize(p.coefficients.size()); + typename std::vector::const_iterator d = p.coefficients.begin(); + for (typename std::vector::iterator c = coefficients.begin(); + c != coefficients.end(); ++c, ++d) + *c += *d; + return *this; + } + + + template + Polynomial& + Polynomial::operator -= (const Polynomial& p) + { + if (p.degree() > degree()) + coefficients.resize(p.coefficients.size()); + typename std::vector::const_iterator d = p.coefficients.begin(); + for (typename std::vector::iterator c = coefficients.begin(); + c != coefficients.end(); ++c, ++d) + *c -= *d; + return *this; + } + + template template void @@ -236,6 +276,22 @@ namespace Polynomials } + + template + Polynomial + Polynomial::derivative () const + { + if (degree() == 0) + return Monomial(0, 0.); + + std::vector newcoefficients (coefficients.size()-1); + for (unsigned int i=1 ; i (newcoefficients); + } + + template void Polynomial::print (std::ostream& out) const @@ -248,7 +304,26 @@ namespace Polynomials } +// ------------------ class Monomial -------------------------- // + template + std::vector + Monomial::make_vector(unsigned int n, + double coefficient) + { + std::vector result(n+1, 0.); + result[n] = coefficient; + return result; + } + + + template + Monomial::Monomial (unsigned int n, + double coefficient) + : Polynomial(make_vector(n, coefficient)) + {} + + // ------------------ class LagrangeEquidistant --------------- // LagrangeEquidistant::LagrangeEquidistant (const unsigned int n, @@ -871,4 +946,8 @@ namespace Polynomials template void Polynomial::shift(const long double offset); template void Polynomial::shift(const long double offset); template void Polynomial::shift(const long double offset); + + template class Monomial; + template class Monomial; + template class Monomial; } -- 2.39.5