* minus one.
*/
Polynomial (const std::vector<number> &coefficients);
-
+
+ /**
+ * Default constructor creating an illegal object.
+ */
+ Polynomial ();
+
/**
* Return the value of this
* polynomial at the given point.
template <typename number2>
void shift (const number2 offset);
+ /**
+ * Compute the derivative of
+ * the polynomial.
+ */
+ Polynomial<number> derivative () const;
+
+ /**
+ * Multiply with a scalar.
+ */
+ Polynomial<number>& operator *= (const double s);
+
+ /**
+ * Add a second polynomial.
+ */
+ Polynomial<number>& operator += (const Polynomial<number>& p);
+
+ /**
+ * Subtract a second polynomial.
+ */
+ Polynomial<number>& operator -= (const Polynomial<number>& p);
+
/**
* Print coefficients.
*/
};
+/**
+ * @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 <typename number>
+ class Monomial :
+ public Polynomial<number>
+ {
+ 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<number> make_vector(unsigned int n,
+ const double coefficient);
+ };
+
/**
* Lagrange polynomials with equidistant interpolation points in
namespace Polynomials
{
+ template <typename number>
+ inline
+ Polynomial<number>::Polynomial ()
+ {}
+
template <typename number>
inline
unsigned int
+ template <typename number>
+ Polynomial<number>&
+ Polynomial<number>::operator *= (const double s)
+ {
+ for (typename std::vector<number>::iterator c = coefficients.begin();
+ c != coefficients.end(); ++c)
+ *c *= s;
+ return *this;
+ }
+
+
+ template <typename number>
+ Polynomial<number>&
+ Polynomial<number>::operator += (const Polynomial<number>& p)
+ {
+//TODO:[GK] Is resize correct?
+ if (p.degree() > degree())
+ coefficients.resize(p.coefficients.size());
+ typename std::vector<number>::const_iterator d = p.coefficients.begin();
+ for (typename std::vector<number>::iterator c = coefficients.begin();
+ c != coefficients.end(); ++c, ++d)
+ *c += *d;
+ return *this;
+ }
+
+
+ template <typename number>
+ Polynomial<number>&
+ Polynomial<number>::operator -= (const Polynomial<number>& p)
+ {
+ if (p.degree() > degree())
+ coefficients.resize(p.coefficients.size());
+ typename std::vector<number>::const_iterator d = p.coefficients.begin();
+ for (typename std::vector<number>::iterator c = coefficients.begin();
+ c != coefficients.end(); ++c, ++d)
+ *c -= *d;
+ return *this;
+ }
+
+
template <typename number>
template <typename number2>
void
}
+
+ template <typename number>
+ Polynomial<number>
+ Polynomial<number>::derivative () const
+ {
+ if (degree() == 0)
+ return Monomial<number>(0, 0.);
+
+ std::vector<number> newcoefficients (coefficients.size()-1);
+ for (unsigned int i=1 ; i<coefficients.size() ; ++i)
+ newcoefficients[i-1] = i * coefficients[i];
+
+ return Polynomial<number> (newcoefficients);
+ }
+
+
template <typename number>
void
Polynomial<number>::print (std::ostream& out) const
}
+// ------------------ class Monomial -------------------------- //
+ template <typename number>
+ std::vector<number>
+ Monomial<number>::make_vector(unsigned int n,
+ double coefficient)
+ {
+ std::vector<number> result(n+1, 0.);
+ result[n] = coefficient;
+ return result;
+ }
+
+
+ template <typename number>
+ Monomial<number>::Monomial (unsigned int n,
+ double coefficient)
+ : Polynomial<number>(make_vector(n, coefficient))
+ {}
+
+
// ------------------ class LagrangeEquidistant --------------- //
LagrangeEquidistant::LagrangeEquidistant (const unsigned int n,
template void Polynomial<long double>::shift(const long double offset);
template void Polynomial<float>::shift(const long double offset);
template void Polynomial<double>::shift(const long double offset);
+
+ template class Monomial<float>;
+ template class Monomial<double>;
+ template class Monomial<long double>;
}