Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:24:19

0001 #ifndef PhysicsTools_Utilities_Polynomial_h
0002 #define PhysicsTools_Utilities_Polynomial_h
0003 #include "PhysicsTools/Utilities/interface/Parameter.h"
0004 
0005 namespace funct {
0006   template <unsigned int n>
0007   class Polynomial {
0008   public:
0009     Polynomial(const double *c);
0010     Polynomial(const std::shared_ptr<double> *c);
0011     Polynomial(const Parameter *p);
0012     double operator()(double x) const;
0013 
0014   private:
0015     std::shared_ptr<double> c0_;
0016     Polynomial<n - 1> poly_;
0017   };
0018 
0019   template <unsigned int n>
0020   Polynomial<n>::Polynomial(const std::shared_ptr<double> *c) : c0_(*c), poly_(c + 1) {}
0021   template <unsigned int n>
0022   Polynomial<n>::Polynomial(const Parameter *c) : c0_(c->ptr()), poly_(c + 1) {}
0023 
0024   template <unsigned int n>
0025   Polynomial<n>::Polynomial(const double *c) : c0_(new double(*c)), poly_(c + 1) {}
0026 
0027   template <unsigned int n>
0028   double Polynomial<n>::operator()(double x) const {
0029     return *c0_ + x * poly_(x);
0030   }
0031 
0032   template <>
0033   class Polynomial<0> {
0034   public:
0035     Polynomial(const std::shared_ptr<double> *c) : c0_(*c) {}
0036     Polynomial(const Parameter *c) : c0_(c->ptr()) {}
0037     Polynomial(const double *c) : c0_(new double(*c)) {}
0038     Polynomial(std::shared_ptr<double> c0) : c0_(c0) {}
0039     Polynomial(const Parameter &c0) : c0_(c0.ptr()) {}
0040     Polynomial(double c0) : c0_(new double(c0)) {}
0041     double operator()(double x) const { return *c0_; }
0042     double operator()() const { return *c0_; }
0043 
0044   private:
0045     std::shared_ptr<double> c0_;
0046   };
0047 
0048   template <>
0049   class Polynomial<1> {
0050   public:
0051     Polynomial(const std::shared_ptr<double> *c) : c0_(*c), poly_(c + 1) {}
0052     Polynomial(const Parameter *c) : c0_(c->ptr()), poly_(c + 1) {}
0053     Polynomial(const double *c) : c0_(new double(*c)), poly_(c + 1) {}
0054     Polynomial(std::shared_ptr<double> c0, std::shared_ptr<double> c1) : c0_(c0), poly_(c1) {}
0055     Polynomial(const Parameter &c0, const Parameter &c1) : c0_(c0.ptr()), poly_(c1.ptr()) {}
0056     Polynomial(double c0, double c1) : c0_(new double(c0)), poly_(c1) {}
0057     double operator()(double x) const { return *c0_ + x * poly_(x); }
0058 
0059   private:
0060     std::shared_ptr<double> c0_;
0061     Polynomial<0> poly_;
0062   };
0063 
0064   template <>
0065   class Polynomial<2> {
0066   public:
0067     Polynomial(const std::shared_ptr<double> *c) : c0_(*c), poly_(c + 1) {}
0068     Polynomial(const Parameter *c) : c0_(c->ptr()), poly_(c + 1) {}
0069     Polynomial(const double *c) : c0_(new double(*c)), poly_(c + 1) {}
0070     Polynomial(std::shared_ptr<double> c0, std::shared_ptr<double> c1, std::shared_ptr<double> c2)
0071         : c0_(c0), poly_(c1, c2) {}
0072     Polynomial(const Parameter &c0, const Parameter &c1, const Parameter &c2) : c0_(c0.ptr()), poly_(c1, c2) {}
0073     Polynomial(double c0, double c1, double c2) : c0_(new double(c0)), poly_(c1, c2) {}
0074     double operator()(double x) const { return *c0_ + x * poly_(x); }
0075 
0076   private:
0077     std::shared_ptr<double> c0_;
0078     Polynomial<1> poly_;
0079   };
0080 }  // namespace funct
0081 
0082 #endif