ug4
polynomial1d.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2010-2015: G-CSC, Goethe University Frankfurt
3  * Author: Andreas Vogel
4  *
5  * This file is part of UG4.
6  *
7  * UG4 is free software: you can redistribute it and/or modify it under the
8  * terms of the GNU Lesser General Public License version 3 (as published by the
9  * Free Software Foundation) with the following additional attribution
10  * requirements (according to LGPL/GPL v3 §7):
11  *
12  * (1) The following notice must be displayed in the Appropriate Legal Notices
13  * of covered and combined works: "Based on UG4 (www.ug4.org/license)".
14  *
15  * (2) The following notice must be displayed at a prominent place in the
16  * terminal output of covered works: "Based on UG4 (www.ug4.org/license)".
17  *
18  * (3) The following bibliography is recommended for citation and must be
19  * preserved in all covered files:
20  * "Reiter, S., Vogel, A., Heppner, I., Rupp, M., and Wittum, G. A massively
21  * parallel geometric multigrid solver on hierarchically distributed grids.
22  * Computing and visualization in science 16, 4 (2013), 151-164"
23  * "Vogel, A., Reiter, S., Rupp, M., Nägel, A., and Wittum, G. UG4 -- a novel
24  * flexible software system for simulating pde based models on high performance
25  * computers. Computing and visualization in science 16, 4 (2013), 165-179"
26  *
27  * This program is distributed in the hope that it will be useful,
28  * but WITHOUT ANY WARRANTY; without even the implied warranty of
29  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
30  * GNU Lesser General Public License for more details.
31  */
32 
33 #ifndef __H__UG__LIB_DISC__LOCAL_SHAPE_FUNCTION_SET__COMMON__POLYNOMIAL1D__
34 #define __H__UG__LIB_DISC__LOCAL_SHAPE_FUNCTION_SET__COMMON__POLYNOMIAL1D__
35 
36 #include "common/math/ugmath.h"
37 #include <vector>
38 
39 namespace ug{
40 
43 
51 {
52  public:
54  Polynomial1D(size_t degree = 0)
55  : m_vCoeff(degree+1, 0.0)
56  {}
57 
59  Polynomial1D(const std::vector<number>& a)
60  : m_vCoeff(a)
61  {
62  // check that at least constant of polynomial set
63  if(m_vCoeff.empty())
64  m_vCoeff.resize(1, 0.0);
65  };
66 
72  size_t degree() const {return m_vCoeff.size() - 1;}
73 
75  number value(const number x) const
76  {
77  // get degree of polynomial (is >= 0 by construction)
78  const size_t deg = m_vCoeff.size() - 1;
79 
80  // loop horner scheme
81  number val = m_vCoeff[deg];
82  for(size_t i = deg; i > 0; --i)
83  val = m_vCoeff[i-1] + val * x;
84 
85  // we're done
86  return val;
87  }
88 
91  {
92  // if only constant present, return empty Polynomial
93  if(degree() == 0)
94  return Polynomial1D();
95 
96  // create empty polynomial of with correct size
97  Polynomial1D tmpPol(degree() - 1);
98 
99  // differentiate
100  for(size_t i = 0; i <= tmpPol.degree(); ++i)
101  tmpPol.m_vCoeff[i] = (i+1) * m_vCoeff[i+1];
102 
103  // return derivative by copy
104  return tmpPol;
105  }
106 
109  {
110  // new size of polynomial
111  size_t newDeg = degree() + v.degree();
112 
113  // create new coefficients
114  std::vector<number> vNewCoeff(newDeg+1, 0.0);
115 
116  // multiply
117  for(size_t i = 0; i <= degree(); ++i)
118  for(size_t j = 0; j <= v.degree(); ++j)
119  vNewCoeff[i+j] += m_vCoeff[i] * v.m_vCoeff[j];
120 
121  // Copy new coeffs
122  m_vCoeff = vNewCoeff;
123 
124  // we're done
125  return *this;
126  }
127 
130  {
131  // multiply
132  for(size_t i = 0; i <= degree(); ++i)
133  m_vCoeff[i] *= scale;
134 
135  // we're done
136  return *this;
137  }
138 
139  // output
140  friend std::ostream& operator<< (std::ostream& outStream, Polynomial1D& v);
141 
142  protected:
143  void set_coefficients(const std::vector<number>& a)
144  {
145  // assign coefficients
146  m_vCoeff = a;
147 
148  // check that at least constant of polynomial set
149  if(m_vCoeff.empty())
150  m_vCoeff.resize(1, 0.0);
151  };
152 
153  private:
154  // vector holding the coefficients of the polynom
155  // An empty vector is the Polynomial p = 0;
156  // else we have p(x) = sum_i m_vCoeff[i] *x^i
157  std::vector<number> m_vCoeff;
158 };
159 
160 inline std::ostream& operator<< (std::ostream& outStream, Polynomial1D& v)
161 {
162  for(size_t i = 0; i <= v.degree(); ++i)
163  {
164  outStream << v.m_vCoeff[i] << " *x^" << i;
165  if(i != v.degree()) outStream << " + ";
166  }
167  return outStream;
168 }
169 
171 } // end namespace ug
172 
173 #endif /* __H__UG__LIB_DISC__LOCAL_SHAPE_FUNCTION_SET__COMMON__POLYNOMIAL1D__ */
Definition: polynomial1d.h:51
size_t degree() const
Definition: polynomial1d.h:72
Polynomial1D & operator*=(const Polynomial1D &v)
multiply by a polynomial
Definition: polynomial1d.h:108
Polynomial1D(size_t degree=0)
Constructor producing zero polynomial of degree 'degree'.
Definition: polynomial1d.h:54
friend std::ostream & operator<<(std::ostream &outStream, Polynomial1D &v)
Definition: polynomial1d.h:160
std::vector< number > m_vCoeff
Definition: polynomial1d.h:151
Polynomial1D derivative() const
returns the derivative of this polynomial as a polynomial
Definition: polynomial1d.h:90
Polynomial1D(const std::vector< number > &a)
Constructor passing coefficients for the polynomial.
Definition: polynomial1d.h:59
void set_coefficients(const std::vector< number > &a)
Definition: polynomial1d.h:143
number value(const number x) const
evaluate the value of the polynom at x
Definition: polynomial1d.h:75
std::ostream & operator<<(std::ostream &outStream, const ug::MathMatrix< 2, 2 > &m)
Definition: math_matrix.cpp:38
double number
Definition: types.h:124
the ug namespace