LCOV - code coverage report
Current view: top level - ugbase/lib_disc/local_finite_element/common - polynomial1d.h (source / functions) Coverage Total Hit
Test: coverage.info Lines: 93.9 % 33 31
Test Date: 2025-09-21 23:31:46 Functions: 100.0 % 4 4

            Line data    Source code
       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              : 
      41              : /// \addtogroup lib_discretization
      42              : ///     @{
      43              : 
      44              : /** base class for one dimensional polynomials
      45              :  * This class is used to represent polynomials in one variable. For the
      46              :  * evaluation the horner scheme is used. Note that using this representation
      47              :  * the computation of higher order derivatives turns out easier than by
      48              :  * hard coded implementations.
      49              :  */
      50          299 : class Polynomial1D
      51              : {
      52              :         public:
      53              :         ///     Constructor producing zero polynomial of degree 'degree'
      54              :                 Polynomial1D(size_t degree = 0)
      55          210 :                         : m_vCoeff(degree+1, 0.0)
      56           10 :                 {}
      57              : 
      58              :         ///     Constructor passing coefficients for the polynomial
      59          112 :                 Polynomial1D(const std::vector<number>& a)
      60          112 :                         : m_vCoeff(a)
      61              :                 {
      62              :                 //      check that at least constant of polynomial set
      63          112 :                         if(m_vCoeff.empty())
      64            0 :                                 m_vCoeff.resize(1, 0.0);
      65          112 :                 };
      66              : 
      67              :         /**     returns the degree of the polynomial.
      68              :          * This function returns the degree of the polynomial, i.e. the
      69              :          * highest coefficient stored. Note that no checking is performed if
      70              :          * the leading coefficient is zero.
      71              :          */
      72           56 :                 size_t degree() const {return m_vCoeff.size() - 1;}
      73              : 
      74              :         ///     evaluate the value of the polynom at x
      75              :                 number value(const number x) const
      76              :                 {
      77              :                 //      get degree of polynomial (is >= 0 by construction)
      78       230202 :                         const size_t deg = m_vCoeff.size() - 1;
      79              : 
      80              :                 //      loop horner scheme
      81       230202 :                         number val = m_vCoeff[deg];
      82       476238 :                         for(size_t i = deg; i > 0; --i)
      83       366474 :                                 val = m_vCoeff[i-1] + val * x;
      84              : 
      85              :                 //      we're done
      86              :                         return val;
      87              :                 }
      88              : 
      89              :         ///     returns the derivative of this polynomial as a polynomial
      90           66 :                 Polynomial1D derivative() const
      91              :                 {
      92              :                 //      if only constant present, return empty Polynomial
      93           66 :                         if(degree() == 0)
      94              :                                 return Polynomial1D();
      95              : 
      96              :                 //      create empty polynomial of with correct size
      97              :                         Polynomial1D tmpPol(degree() - 1);
      98              : 
      99              :                 //      differentiate
     100          168 :                         for(size_t i = 0; i <= tmpPol.degree(); ++i)
     101          112 :                                 tmpPol.m_vCoeff[i] = (i+1) * m_vCoeff[i+1];
     102              : 
     103              :                 //      return derivative by copy
     104              :                         return tmpPol;
     105              :                 }
     106              : 
     107              :         ///     multiply by a polynomial
     108          112 :                 Polynomial1D& operator *=(const Polynomial1D& v)
     109              :                 {
     110              :                 //      new size of polynomial
     111          112 :                         size_t newDeg = degree() + v.degree();
     112              : 
     113              :                 //      create new coefficients
     114          112 :                         std::vector<number> vNewCoeff(newDeg+1, 0.0);
     115              : 
     116              :                 //      multiply
     117          299 :                         for(size_t i = 0; i <= degree(); ++i)
     118          561 :                                 for(size_t j = 0; j <= v.degree(); ++j)
     119          374 :                                         vNewCoeff[i+j] += m_vCoeff[i] * v.m_vCoeff[j];
     120              : 
     121              :                 //      Copy new coeffs
     122          112 :                         m_vCoeff = vNewCoeff;
     123              : 
     124              :                 //      we're done
     125          112 :                         return *this;
     126          112 :                 }
     127              : 
     128              :         ///     multiply by a scalar
     129              :                 Polynomial1D& operator *=(number scale)
     130              :                 {
     131              :                 //      multiply
     132          244 :                         for(size_t i = 0; i <= degree(); ++i)
     133          178 :                                 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           66 :                 void set_coefficients(const std::vector<number>& a)
     144              :                 {
     145              :                 //      assign coefficients
     146           66 :                         m_vCoeff = a;
     147              : 
     148              :                 //      check that at least constant of polynomial set
     149           66 :                         if(m_vCoeff.empty())
     150            0 :                                 m_vCoeff.resize(1, 0.0);
     151           66 :                 };
     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              : 
     170              : /// @}
     171              : } // end namespace ug
     172              : 
     173              : #endif /* __H__UG__LIB_DISC__LOCAL_SHAPE_FUNCTION_SET__COMMON__POLYNOMIAL1D__ */
        

Generated by: LCOV version 2.0-1