CMS 3D CMS Logo

/data/doxygen/doxygen-1.7.3/gen/CMSSW_4_2_8/src/MagneticField/ParametrizedEngine/src/poly2d_base.h

Go to the documentation of this file.
00001 #ifndef poly2d_base_h
00002 #define poly2d_base_h
00003 
00004 #include <iostream>
00005 #include <fstream>
00006 #include <iomanip>
00007 #include <vector>
00008 #include <set>
00009 #include <cstring>
00010 
00011 #include <cmath>
00012 #include <float.h> //in order to use DBL_EPSILON (1+DBL_EPSILON > 1)
00013 
00015 //                                                                             //
00016 //  The "poly2d_term" represent a term of a polynomial of 2 variables.         //
00017 //                                                                             //
00019 
00020 //_______________________________________________________________________________
00021 namespace magfieldparam {
00022 
00023 struct poly2d_term {
00024    double   coeff;  //Coefficient of the term
00025    unsigned np[2];  //Powers of variables
00026 
00027    poly2d_term() {memset(this, 0, sizeof(*this));}
00028    poly2d_term(double C, unsigned nr, unsigned nz)
00029    {
00030       coeff = C; np[0] = nr; np[1] = nz;
00031    }
00032    void Print(std::ostream &out = std::cout, bool first_term = true);
00033 };
00034 
00036 //                                                                             //
00037 //  Base class that represent a polynomial of 2 variables. It isn't supposed   //
00038 //  to be used directly and provides no way of setting coefficients directly.  //
00039 //  Such methods must be defined in derived classes.                           //
00040 //                                                                             //
00042 
00043 //_______________________________________________________________________________
00044 class poly2d_base {   // a general polynomial of 2 variables
00045 
00046 protected:
00047    //Group of static members for the class memory management
00048    //----------------------------------------------------------------------------
00049    static double     rval;   //last r-value used in calculation
00050    static double     zval;   //last z-value used in calculation
00051 
00052    static double   **rz_pow; //table with calculated r^n*z^m values
00053    static unsigned   NTab;   //rz_pow table size
00054    static unsigned   NPwr;   //max power in use by CLASS
00055    static bool       rz_set;
00056 
00057    static const double MIN_COEFF; //Threshold for assigning a coeff. to 0
00058 
00059    static std::set<poly2d_base*> poly2d_base_set;  //Set of all poly2d_base objects
00060 //   static std::set<poly2d_base*, less<poly2d_base*> > poly2d_base_set;  //Set of all poly2d_base objects
00061 
00062    static void SetTabSize(const unsigned N); //Set rz-table size
00063    static void FillTable (const double r, const double z);
00064 
00065    static void AdjustTab();
00066    //----------------------------------------------------------------------------
00067 
00068    std::vector<poly2d_term> data; //polynomial terms
00069    unsigned max_pwr;         //max power in use by INSTANCE
00070    
00071 public:
00072    static void     IncNPwr(const unsigned N) {if (N > NPwr) NPwr = N;}
00073    static int      GetMaxPow();
00074    static unsigned Count() { return poly2d_base_set.size();}
00075    static void     PrintTab(std::ostream &out = std::cout, const std::streamsize prec = 5);
00076 
00077    static void SetPoint(const double r, const double z);
00078 
00079    poly2d_base() {
00080       max_pwr = 0;
00081       poly2d_base_set.insert(this);
00082    }
00083    poly2d_base(const poly2d_base &S) {
00084       data    = S.data;
00085       max_pwr = S.max_pwr;
00086       poly2d_base_set.insert(this);
00087    }
00088 
00089    virtual ~poly2d_base();
00090 
00091    bool IsOn()    { return bool(data.size());}
00092    bool IsRZSet() { return rz_set;}
00093 
00094    void Collect(); //Collect terms and remove zero terms
00095    void Compress() { Collect();}
00096    
00097    void Diff  (int nvar); //differentiate the polynomial by variable# nvar
00098    void Int   (int nvar); //Integrate the polynomial by variable# nvar
00099    void IncPow(int nvar); //Multiply the polynomial by variable# nvar
00100    void DecPow(int nvar); //Divide the polynomial by variable# nvar
00101 
00102    void Scale(const double C);
00103 //   poly2d_base& operator*=(const double C) { Scale(C); return *this;}
00104 
00105    double Eval(); //Evaluation with no check that rz_pow table exist
00106    double GetVal() {if (rz_set) return Eval(); else return 0.;}
00107    double GetVal(const double r, const double z) { SetPoint(r,z); return Eval();}
00108    
00109    void Print(std::ostream &out = std::cout, const std::streamsize prec = 5);
00110 
00111 }; //Class poly2d_base
00112 }
00113 
00114 #endif