Reference documentation for deal.II version 8.1.0
polynomial.h
1 // ---------------------------------------------------------------------
2 // @f$Id: polynomial.h 30036 2013-07-18 16:55:32Z maier @f$
3 //
4 // Copyright (C) 2000 - 2013 by the deal.II authors
5 //
6 // This file is part of the deal.II library.
7 //
8 // The deal.II library is free software; you can use it, redistribute
9 // it, and/or modify it under the terms of the GNU Lesser General
10 // Public License as published by the Free Software Foundation; either
11 // version 2.1 of the License, or (at your option) any later version.
12 // The full text of the license can be found in the file LICENSE at
13 // the top level of the deal.II distribution.
14 //
15 // ---------------------------------------------------------------------
16 
17 #ifndef __deal2__polynomial_h
18 #define __deal2__polynomial_h
19 
20 
21 
22 #include <deal.II/base/config.h>
24 #include <deal.II/base/subscriptor.h>
25 #include <deal.II/base/point.h>
26 #include <deal.II/base/std_cxx1x/shared_ptr.h>
27 
28 #include <vector>
29 
31 
41 namespace Polynomials
42 {
43 
53  template <typename number>
54  class Polynomial : public Subscriptor
55  {
56  public:
65  Polynomial (const std::vector<number> &coefficients);
66 
70  Polynomial (const unsigned int n);
71 
79  Polynomial (const std::vector<Point<1> > &lagrange_support_points,
80  const unsigned int evaluation_point);
81 
85  Polynomial ();
86 
93  number value (const number x) const;
94 
104  void value (const number x,
105  std::vector<number> &values) const;
106 
112  unsigned int degree () const;
113 
121  void scale (const number factor);
122 
138  template <typename number2>
139  void shift (const number2 offset);
140 
145 
150  Polynomial<number> primitive () const;
151 
155  Polynomial<number> &operator *= (const double s);
156 
161 
166 
171 
175  bool operator == (const Polynomial<number> &p) const;
176 
180  void print(std::ostream &out) const;
181 
186  template <class Archive>
187  void serialize (Archive &ar, const unsigned int version);
188 
189  protected:
190 
194  static void scale (std::vector<number> &coefficients,
195  const number factor);
196 
200  template <typename number2>
201  static void shift (std::vector<number> &coefficients,
202  const number2 shift);
203 
207  static void multiply (std::vector<number> &coefficients,
208  const number factor);
209 
216 
225  std::vector<number> coefficients;
226 
232 
237  std::vector<number> lagrange_support_points;
238 
244  };
245 
246 
253  template <typename number>
254  class Monomial : public Polynomial<number>
255  {
256  public:
263  Monomial(const unsigned int n,
264  const double coefficient = 1.);
265 
278  static
279  std::vector<Polynomial<number> >
280  generate_complete_basis (const unsigned int degree);
281 
282  private:
286  static std::vector<number> make_vector(unsigned int n,
287  const double coefficient);
288  };
289 
290 
309  class LagrangeEquidistant: public Polynomial<double>
310  {
311  public:
317  LagrangeEquidistant (const unsigned int n,
318  const unsigned int support_point);
319 
328  static
329  std::vector<Polynomial<double> >
330  generate_complete_basis (const unsigned int degree);
331 
332  private:
333 
338  static
339  void
340  compute_coefficients (const unsigned int n,
341  const unsigned int support_point,
342  std::vector<double> &a);
343  };
344 
345 
346 
353  std::vector<Polynomial<double> >
354  generate_complete_Lagrange_basis (const std::vector<Point<1> > &points);
355 
356 
357 
372  class Legendre : public Polynomial<double>
373  {
374  public:
379  Legendre (const unsigned int p);
380 
393  static
394  std::vector<Polynomial<double> >
395  generate_complete_basis (const unsigned int degree);
396 
397  private:
401  static std::vector<std_cxx1x::shared_ptr<const std::vector<double> > > shifted_coefficients;
402 
416  static std::vector<std_cxx1x::shared_ptr<const std::vector<double> > > recursive_coefficients;
417 
430  static void compute_coefficients (const unsigned int p);
431 
439  static const std::vector<double> &
440  get_coefficients (const unsigned int k);
441  };
442 
464  class Lobatto : public Polynomial<double>
465  {
466  public:
473  Lobatto (const unsigned int p = 0);
474 
482  static std::vector<Polynomial<double> >
483  generate_complete_basis (const unsigned int p);
484 
485  private:
489  std::vector<double> compute_coefficients (const unsigned int p);
490  };
491 
492 
493 
534  class Hierarchical : public Polynomial<double>
535  {
536  public:
543  Hierarchical (const unsigned int p);
544 
564  static
565  std::vector<Polynomial<double> >
566  generate_complete_basis (const unsigned int degree);
567 
568  private:
572  static void compute_coefficients (const unsigned int p);
573 
581  static const std::vector<double> &
582  get_coefficients (const unsigned int p);
583 
597  static std::vector<std_cxx1x::shared_ptr<const std::vector<double> > > recursive_coefficients;
598  };
599 
600 
632  class HermiteInterpolation : public Polynomial<double>
633  {
634  public:
642  HermiteInterpolation (const unsigned int p);
643 
652  static std::vector<Polynomial<double> >
653  generate_complete_basis (const unsigned int p);
654  };
655 }
656 
657 
660 /* -------------------------- inline functions --------------------- */
661 
662 namespace Polynomials
663 {
664  template <typename number>
665  inline
667  :
668  in_lagrange_product_form (false),
669  lagrange_weight (1.)
670  {}
671 
672 
673 
674  template <typename number>
675  inline
676  unsigned int
678  {
679  if (in_lagrange_product_form == true)
680  {
681  return lagrange_support_points.size();
682  }
683  else
684  {
685  Assert (coefficients.size()>0, ExcEmptyObject());
686  return coefficients.size() - 1;
687  }
688  }
689 
690 
691 
692  template <typename number>
693  inline
694  number
695  Polynomial<number>::value (const number x) const
696  {
697  if (in_lagrange_product_form == false)
698  {
699  Assert (coefficients.size() > 0, ExcEmptyObject());
700 
701  // Horner scheme
702  const unsigned int m=coefficients.size();
703  number value = coefficients.back();
704  for (int k=m-2; k>=0; --k)
705  value = value*x + coefficients[k];
706  return value;
707  }
708  else
709  {
710  // direct evaluation of Lagrange polynomial
711  const unsigned int m = lagrange_support_points.size();
712  number value = 1.;
713  for (unsigned int j=0; j<m; ++j)
714  value *= x-lagrange_support_points[j];
715  value *= lagrange_weight;
716  return value;
717  }
718  }
719 
720 
721 
722  template <typename number>
723  template <class Archive>
724  inline
725  void
726  Polynomial<number>::serialize (Archive &ar, const unsigned int)
727  {
728  // forward to serialization function in the base class.
729  ar &static_cast<Subscriptor &>(*this);
730  ar &coefficients;
731  ar &in_lagrange_product_form;
732  ar &lagrange_support_points;
733  ar &lagrange_weight;
734  }
735 
736 }
737 DEAL_II_NAMESPACE_CLOSE
738 
739 #endif
void serialize(Archive &ar, const unsigned int version)
Definition: polynomial.h:726
void scale(const number factor)
LagrangeEquidistant(const unsigned int n, const unsigned int support_point)
void shift(const number2 offset)
static std::vector< Polynomial< number > > generate_complete_basis(const unsigned int degree)
Legendre(const unsigned int p)
Polynomial< number > & operator-=(const Polynomial< number > &p)
static std::vector< std_cxx1x::shared_ptr< const std::vector< double > > > recursive_coefficients
Definition: polynomial.h:597
static void compute_coefficients(const unsigned int p)
Monomial(const unsigned int n, const double coefficient=1.)
Polynomial< number > primitive() const
bool operator==(const Polynomial< number > &p) const
number value(const number x) const
Definition: polynomial.h:695
Polynomial< number > & operator*=(const double s)
static std::vector< Polynomial< double > > generate_complete_basis(const unsigned int degree)
#define Assert(cond, exc)
Definition: exceptions.h:299
::ExceptionBase & ExcEmptyObject()
static std::vector< number > make_vector(unsigned int n, const double coefficient)
HermiteInterpolation(const unsigned int p)
std::vector< double > compute_coefficients(const unsigned int p)
Lobatto(const unsigned int p=0)
std::vector< Polynomial< double > > generate_complete_Lagrange_basis(const std::vector< Point< 1 > > &points)
Hierarchical(const unsigned int p)
static std::vector< Polynomial< double > > generate_complete_basis(const unsigned int degree)
static std::vector< std_cxx1x::shared_ptr< const std::vector< double > > > recursive_coefficients
Definition: polynomial.h:416
Polynomial< number > derivative() const
std::vector< number > coefficients
Definition: polynomial.h:225
static std::vector< Polynomial< double > > generate_complete_basis(const unsigned int degree)
void print(std::ostream &out) const
static const std::vector< double > & get_coefficients(const unsigned int k)
static void compute_coefficients(const unsigned int n, const unsigned int support_point, std::vector< double > &a)
std::vector< number > lagrange_support_points
Definition: polynomial.h:237
static void compute_coefficients(const unsigned int p)
static const std::vector< double > & get_coefficients(const unsigned int p)
unsigned int degree() const
Definition: polynomial.h:677
static std::vector< Polynomial< double > > generate_complete_basis(const unsigned int p)
static std::vector< std_cxx1x::shared_ptr< const std::vector< double > > > shifted_coefficients
Definition: polynomial.h:401
static void multiply(std::vector< number > &coefficients, const number factor)
Polynomial< number > & operator+=(const Polynomial< number > &p)
static std::vector< Polynomial< double > > generate_complete_basis(const unsigned int p)