CMS 3D CMS Logo

/afs/cern.ch/work/a/aaltunda/public/www/CMSSW_5_3_14/src/PhysicsTools/Utilities/interface/Polynomial.h

Go to the documentation of this file.
00001 #ifndef PhysicsTools_Utilities_Polynomial_h
00002 #define PhysicsTools_Utilities_Polynomial_h
00003 #include "PhysicsTools/Utilities/interface/Parameter.h"
00004 #include "boost/shared_ptr.hpp"
00005 
00006 namespace funct {
00007   template<unsigned int n>
00008   class Polynomial { 
00009   public:
00010     Polynomial(const double * c);
00011     Polynomial(const boost::shared_ptr<double> * c);
00012     Polynomial(const Parameter * p);
00013     double operator()(double x) const;
00014   private:
00015     boost::shared_ptr<double> c0_;
00016     Polynomial<n-1> poly_;
00017   };
00018   
00019   template<unsigned int n>
00020   Polynomial<n>::Polynomial(const boost::shared_ptr<double> * c) : 
00021     c0_(c), poly_(c + 1) {
00022   }
00023   template<unsigned int n>
00024   Polynomial<n>::Polynomial(const Parameter * c) : 
00025     c0_(c->ptr()), poly_(c + 1) {
00026   }
00027   
00028   
00029   template<unsigned int n>
00030   Polynomial<n>::Polynomial(const double * c) : 
00031     c0_(new double(*c)), poly_(c + 1) {
00032   }
00033 
00034   template<unsigned int n>
00035   double Polynomial<n>::operator()(double x) const {
00036     return *c0_ + x*poly_(x);
00037   }
00038   
00039   template<>
00040   class Polynomial<0> {
00041   public:
00042     Polynomial(const boost::shared_ptr<double> * c) : 
00043       c0_(*c) {
00044     }
00045     Polynomial(const Parameter * c) : 
00046       c0_(c->ptr()) {
00047     }
00048     Polynomial(const double * c) : 
00049       c0_(new double(*c)) {
00050     }
00051     Polynomial(boost::shared_ptr<double> c0) : 
00052       c0_(c0) {
00053     }
00054     Polynomial(const Parameter & c0) : 
00055       c0_(c0.ptr()) {
00056     }
00057     Polynomial(double c0) : 
00058       c0_(new double(c0)) {
00059     }
00060     double operator()(double x) const {
00061       return *c0_;
00062     }
00063     double operator()() const {
00064       return *c0_;
00065     }
00066   private:
00067     boost::shared_ptr<double> c0_;
00068   };
00069   
00070   template<>
00071   class Polynomial<1> { 
00072   public:
00073     Polynomial(const boost::shared_ptr<double> * c) : 
00074       c0_(*c), poly_(c + 1) {
00075     }
00076     Polynomial(const Parameter * c) : 
00077       c0_(c->ptr()), poly_(c + 1) {
00078     }
00079     Polynomial(const double * c) : 
00080       c0_(new double(*c)), poly_(c + 1) {
00081     }
00082     Polynomial(boost::shared_ptr<double> c0, boost::shared_ptr<double> c1) : 
00083       c0_(c0), poly_(c1) {
00084     }
00085     Polynomial(const Parameter& c0, const Parameter& c1) : 
00086       c0_(c0.ptr()), poly_(c1.ptr()) {
00087     }
00088     Polynomial(double c0, double c1) : 
00089       c0_(new double(c0)), poly_(c1) {
00090     }
00091     double operator()(double x) const {
00092       return *c0_ + x*poly_(x);
00093     }
00094   private:
00095     boost::shared_ptr<double> c0_;
00096     Polynomial<0> poly_;
00097   };
00098   
00099   template<>
00100   class Polynomial<2> { 
00101   public:
00102     Polynomial(const boost::shared_ptr<double> * c) : 
00103       c0_(*c), poly_(c + 1) {
00104     }
00105     Polynomial(const Parameter * c) :
00106       c0_(c->ptr()), poly_(c + 1) {
00107     }
00108     Polynomial(const double * c) : 
00109       c0_(new double(*c)), poly_(c + 1) {
00110     }
00111     Polynomial(boost::shared_ptr<double> c0, 
00112                boost::shared_ptr<double> c1, 
00113                boost::shared_ptr<double> c2) : c0_(c0), poly_(c1, c2) {
00114     }
00115     Polynomial(const Parameter &c0, 
00116                const Parameter &c1, 
00117                const Parameter &c2) : c0_(c0.ptr()), poly_(c1, c2) {
00118     }
00119     Polynomial(double c0, double c1, double c2) : 
00120       c0_(new double(c0) ), poly_(c1, c2) {
00121     }
00122     double operator()(double x) const {
00123       return *c0_ + x*poly_(x);
00124     }
00125   private:
00126     boost::shared_ptr<double> c0_;
00127     Polynomial<1> poly_;
00128   };
00129 }
00130 
00131 #endif