Reference documentation for deal.II version 8.5.1
polynomial.h
1 // ---------------------------------------------------------------------
2 //
3 // Copyright (C) 2000 - 2017 by the deal.II authors
4 //
5 // This file is part of the deal.II library.
6 //
7 // The deal.II library is free software; you can use it, redistribute
8 // it, and/or modify it under the terms of the GNU Lesser General
9 // Public License as published by the Free Software Foundation; either
10 // version 2.1 of the License, or (at your option) any later version.
11 // The full text of the license can be found in the file LICENSE at
12 // the top level of the deal.II distribution.
13 //
14 // ---------------------------------------------------------------------
15 
16 #ifndef dealii__polynomial_h
17 #define dealii__polynomial_h
18 
19 
20 
21 #include <deal.II/base/config.h>
22 #include <deal.II/base/exceptions.h>
23 #include <deal.II/base/subscriptor.h>
24 #include <deal.II/base/point.h>
25 #include <deal.II/base/std_cxx11/shared_ptr.h>
26 
27 #include <vector>
28 
29 DEAL_II_NAMESPACE_OPEN
30 
40 namespace Polynomials
41 {
42 
61  template <typename number>
62  class Polynomial : public Subscriptor
63  {
64  public:
73  Polynomial (const std::vector<number> &coefficients);
74 
78  Polynomial (const unsigned int n);
79 
87  Polynomial (const std::vector<Point<1> > &lagrange_support_points,
88  const unsigned int evaluation_point);
89 
93  Polynomial ();
94 
102  number value (const number x) const;
103 
114  void value (const number x,
115  std::vector<number> &values) const;
116 
128  void value (const number x,
129  const unsigned int n_derivatives,
130  number *values) const;
131 
137  unsigned int degree () const;
138 
146  void scale (const number factor);
147 
163  template <typename number2>
164  void shift (const number2 offset);
165 
170 
175  Polynomial<number> primitive () const;
176 
180  Polynomial<number> &operator *= (const double s);
181 
186 
191 
196 
200  bool operator == (const Polynomial<number> &p) const;
201 
205  void print(std::ostream &out) const;
206 
211  template <class Archive>
212  void serialize (Archive &ar, const unsigned int version);
213 
214  protected:
215 
219  static void scale (std::vector<number> &coefficients,
220  const number factor);
221 
225  template <typename number2>
226  static void shift (std::vector<number> &coefficients,
227  const number2 shift);
228 
232  static void multiply (std::vector<number> &coefficients,
233  const number factor);
234 
241 
250  std::vector<number> coefficients;
251 
257 
262  std::vector<number> lagrange_support_points;
263 
269  };
270 
271 
278  template <typename number>
279  class Monomial : public Polynomial<number>
280  {
281  public:
286  Monomial(const unsigned int n,
287  const double coefficient = 1.);
288 
295  static
296  std::vector<Polynomial<number> >
297  generate_complete_basis (const unsigned int degree);
298 
299  private:
303  static std::vector<number> make_vector(unsigned int n,
304  const double coefficient);
305  };
306 
307 
325  class LagrangeEquidistant: public Polynomial<double>
326  {
327  public:
333  LagrangeEquidistant (const unsigned int n,
334  const unsigned int support_point);
335 
344  static
345  std::vector<Polynomial<double> >
346  generate_complete_basis (const unsigned int degree);
347 
348  private:
349 
354  static
355  void
356  compute_coefficients (const unsigned int n,
357  const unsigned int support_point,
358  std::vector<double> &a);
359  };
360 
361 
362 
369  std::vector<Polynomial<double> >
370  generate_complete_Lagrange_basis (const std::vector<Point<1> > &points);
371 
372 
373 
389  class Legendre : public Polynomial<double>
390  {
391  public:
395  Legendre (const unsigned int p);
396 
403  static
404  std::vector<Polynomial<double> >
405  generate_complete_basis (const unsigned int degree);
406  };
407 
429  class Lobatto : public Polynomial<double>
430  {
431  public:
436  Lobatto (const unsigned int p = 0);
437 
442  static std::vector<Polynomial<double> >
443  generate_complete_basis (const unsigned int p);
444 
445  private:
449  std::vector<double> compute_coefficients (const unsigned int p);
450  };
451 
452 
453 
493  class Hierarchical : public Polynomial<double>
494  {
495  public:
500  Hierarchical (const unsigned int p);
501 
512  static
513  std::vector<Polynomial<double> >
514  generate_complete_basis (const unsigned int degree);
515 
516  private:
520  static void compute_coefficients (const unsigned int p);
521 
526  static const std::vector<double> &
527  get_coefficients (const unsigned int p);
528 
537  static std::vector<std_cxx11::shared_ptr<const std::vector<double> > > recursive_coefficients;
538  };
539 
540 
571  class HermiteInterpolation : public Polynomial<double>
572  {
573  public:
578  HermiteInterpolation (const unsigned int p);
579 
585  static std::vector<Polynomial<double> >
586  generate_complete_basis (const unsigned int p);
587  };
588 }
589 
590 
593 /* -------------------------- inline functions --------------------- */
594 
595 namespace Polynomials
596 {
597  template <typename number>
598  inline
600  :
601  in_lagrange_product_form (false),
602  lagrange_weight (1.)
603  {}
604 
605 
606 
607  template <typename number>
608  inline
609  unsigned int
611  {
612  if (in_lagrange_product_form == true)
613  {
614  return lagrange_support_points.size();
615  }
616  else
617  {
618  Assert (coefficients.size()>0, ExcEmptyObject());
619  return coefficients.size() - 1;
620  }
621  }
622 
623 
624 
625  template <typename number>
626  inline
627  number
628  Polynomial<number>::value (const number x) const
629  {
630  if (in_lagrange_product_form == false)
631  {
632  Assert (coefficients.size() > 0, ExcEmptyObject());
633 
634  // Horner scheme
635  const unsigned int m=coefficients.size();
636  number value = coefficients.back();
637  for (int k=m-2; k>=0; --k)
638  value = value*x + coefficients[k];
639  return value;
640  }
641  else
642  {
643  // direct evaluation of Lagrange polynomial
644  const unsigned int m = lagrange_support_points.size();
645  number value = 1.;
646  for (unsigned int j=0; j<m; ++j)
647  value *= x-lagrange_support_points[j];
648  value *= lagrange_weight;
649  return value;
650  }
651  }
652 
653 
654 
655  template <typename number>
656  template <class Archive>
657  inline
658  void
659  Polynomial<number>::serialize (Archive &ar, const unsigned int)
660  {
661  // forward to serialization function in the base class.
662  ar &static_cast<Subscriptor &>(*this);
663  ar &coefficients;
664  ar &in_lagrange_product_form;
665  ar &lagrange_support_points;
666  ar &lagrange_weight;
667  }
668 
669 }
670 DEAL_II_NAMESPACE_CLOSE
671 
672 #endif
void serialize(Archive &ar, const unsigned int version)
Definition: polynomial.h:659
Polynomial< number > & operator*=(const double s)
Definition: polynomial.cc:337
void scale(const number factor)
Definition: polynomial.cc:300
void transform_into_standard_form()
Definition: polynomial.cc:247
bool operator==(const Polynomial< number > &p) const
Definition: polynomial.cc:479
LagrangeEquidistant(const unsigned int n, const unsigned int support_point)
Definition: polynomial.cc:722
Polynomial< number > derivative() const
Definition: polynomial.cc:593
void shift(const number2 offset)
Definition: polynomial.cc:574
static std::vector< Polynomial< double > > generate_complete_basis(const unsigned int p)
Definition: polynomial.cc:959
unsigned int degree() const
Definition: polynomial.h:610
static std::vector< Polynomial< double > > generate_complete_basis(const unsigned int degree)
Definition: polynomial.cc:1140
Legendre(const unsigned int p)
Definition: polynomial.cc:845
static std::vector< Polynomial< double > > generate_complete_basis(const unsigned int degree)
Definition: polynomial.cc:873
Polynomial< number > & operator-=(const Polynomial< number > &p)
Definition: polynomial.cc:443
std::vector< Polynomial< double > > generate_complete_Lagrange_basis(const std::vector< Point< 1 > > &points)
Definition: polynomial.cc:829
static void compute_coefficients(const unsigned int n, const unsigned int support_point, std::vector< double > &a)
Definition: polynomial.cc:747
static std::vector< Polynomial< double > > generate_complete_basis(const unsigned int degree)
Definition: polynomial.cc:806
Monomial(const unsigned int n, const double coefficient=1.)
Definition: polynomial.cc:682
number value(const number x) const
Definition: polynomial.h:628
Definition: point.h:89
void print(std::ostream &out) const
Definition: polynomial.cc:649
std::vector< double > compute_coefficients(const unsigned int p)
Definition: polynomial.cc:891
static void multiply(std::vector< number > &coefficients, const number factor)
Definition: polynomial.cc:325
#define Assert(cond, exc)
Definition: exceptions.h:313
Polynomial< number > & operator+=(const Polynomial< number > &p)
Definition: polynomial.cc:401
HermiteInterpolation(const unsigned int p)
Definition: polynomial.cc:1168
Lobatto(const unsigned int p=0)
Definition: polynomial.cc:887
static std::vector< Polynomial< number > > generate_complete_basis(const unsigned int degree)
Definition: polynomial.cc:691
static std::vector< number > make_vector(unsigned int n, const double coefficient)
Definition: polynomial.cc:671
Hierarchical(const unsigned int p)
Definition: polynomial.cc:981
std::vector< number > coefficients
Definition: polynomial.h:250
Polynomial< number > primitive() const
Definition: polynomial.cc:622
static void compute_coefficients(const unsigned int p)
Definition: polynomial.cc:989
static const std::vector< double > & get_coefficients(const unsigned int p)
Definition: polynomial.cc:1124
std::vector< number > lagrange_support_points
Definition: polynomial.h:262
static ::ExceptionBase & ExcEmptyObject()
static std::vector< std_cxx11::shared_ptr< const std::vector< double > > > recursive_coefficients
Definition: polynomial.h:537
static std::vector< Polynomial< double > > generate_complete_basis(const unsigned int p)
Definition: polynomial.cc:1221