CMS 3D CMS Logo

Public Member Functions | Static Public Member Functions | Static Protected Member Functions | Protected Attributes | Static Protected Attributes

magfieldparam::poly2d_base Class Reference

#include <poly2d_base.h>

Inheritance diagram for magfieldparam::poly2d_base:
magfieldparam::rz_harm_poly

List of all members.

Public Member Functions

void Collect ()
void Compress ()
void DecPow (int nvar)
void Diff (int nvar)
double Eval ()
double GetVal (const double r, const double z)
double GetVal ()
void IncPow (int nvar)
void Int (int nvar)
bool IsOn ()
bool IsRZSet ()
 poly2d_base (const poly2d_base &S)
 poly2d_base ()
void Print (std::ostream &out=std::cout, const std::streamsize prec=5)
void Scale (const double C)
virtual ~poly2d_base ()

Static Public Member Functions

static unsigned Count ()
static int GetMaxPow ()
static void IncNPwr (const unsigned N)
static void PrintTab (std::ostream &out=std::cout, const std::streamsize prec=5)
static void SetPoint (const double r, const double z)

Static Protected Member Functions

static void AdjustTab ()
static void FillTable (const double r, const double z)
static void SetTabSize (const unsigned N)

Protected Attributes

std::vector< poly2d_termdata
unsigned max_pwr

Static Protected Attributes

static const double MIN_COEFF = DBL_EPSILON
static unsigned NPwr = 0
static unsigned NTab = 0
static std::set< poly2d_base * > poly2d_base_set
static double rval = 0.
static double ** rz_pow = 0
static bool rz_set = false
static double zval = 0.

Detailed Description

Definition at line 44 of file poly2d_base.h.


Constructor & Destructor Documentation

magfieldparam::poly2d_base::poly2d_base ( ) [inline]

Definition at line 79 of file poly2d_base.h.

References max_pwr, and poly2d_base_set.

                 {
      max_pwr = 0;
      poly2d_base_set.insert(this);
   }
magfieldparam::poly2d_base::poly2d_base ( const poly2d_base S) [inline]

Definition at line 83 of file poly2d_base.h.

References data, max_pwr, and poly2d_base_set.

                                     {
      data    = S.data;
      max_pwr = S.max_pwr;
      poly2d_base_set.insert(this);
   }
poly2d_base::~poly2d_base ( ) [virtual]

Definition at line 49 of file poly2d_base.cc.

References GetMaxPow(), max_pwr, NPwr, NTab, poly2d_base_set, rz_pow, rz_set, and zval.

{
   poly2d_base_set.erase(poly2d_base_set.find(this));
   if(poly2d_base_set.size()) { //some objects left
      if (max_pwr >= NPwr) NPwr = GetMaxPow();
   } else {
      if (rz_pow) {
         delete [] rz_pow[0]; //deleting the last instance -> memory cleanup
         delete [] rz_pow;
      }
      rz_pow = 0;
      rval = zval = 0.;
      NPwr = 0;
      NTab = 0;
      rz_set = false;
//      poly2d_base_set.resize(0);
   }
}

Member Function Documentation

void poly2d_base::AdjustTab ( ) [static, protected]

Definition at line 115 of file poly2d_base.cc.

References GetMaxPow(), NPwr, NTab, and SetTabSize().

{
   NPwr = GetMaxPow();
   if (NPwr >= NTab) SetTabSize(NPwr+1);
}
void poly2d_base::Collect ( )

Definition at line 166 of file poly2d_base.cc.

References funct::C, data, max_pwr, MIN_COEFF, and runTheMatrix::np.

Referenced by Compress(), magfieldparam::rz_harm_poly::LadderDwn(), and magfieldparam::rz_harm_poly::LadderUp().

{
   if (!(data.size())) return;

   unsigned j1, j2, rpow, zpow, noff = 0, jend = data.size();
   double C;
   std::vector<bool> mask(jend, false);
   max_pwr = 0;

   for (j1 = 0; j1 < jend; ++j1) {
      if (mask[j1]) continue;
      C = data[j1].coeff;
      rpow = data[j1].np[0];
      zpow = data[j1].np[1];
      for (j2 = j1+1; j2 < jend; ++j2) {
         if (mask[j2]) continue;
         if ((rpow == data[j2].np[0]) && (zpow == data[j2].np[1])) {
            C += data[j2].coeff;
            mask[j2] = true;
            ++noff;
         }
      }
      if (fabs(C) > MIN_COEFF) {
         data[j1].coeff = C;
         if ((rpow = rpow+zpow) > max_pwr) max_pwr = rpow;
      } else {
         mask[j1] = true;
         ++noff;
      }
   }
   std::vector<poly2d_term> newdata; newdata.reserve(jend - noff);
   for (j1 = 0; j1 < jend; ++j1) {
      if (!(mask[j1])) newdata.push_back(data[j1]);
   }
   data.swap(newdata);
}
void magfieldparam::poly2d_base::Compress ( ) [inline]

Definition at line 95 of file poly2d_base.h.

References Collect().

{ Collect();}
static unsigned magfieldparam::poly2d_base::Count ( ) [inline, static]

Reimplemented in magfieldparam::rz_harm_poly.

Definition at line 74 of file poly2d_base.h.

References poly2d_base_set.

Referenced by SetPoint().

{ return poly2d_base_set.size();}
void poly2d_base::DecPow ( int  nvar)

Definition at line 272 of file poly2d_base.cc.

References magfieldparam::poly2d_term::coeff, data, GetMaxPow(), max_pwr, magfieldparam::poly2d_term::np, and NPwr.

Referenced by magfieldparam::rz_harm_poly::GetDecPow().

{
//Divide the polynomial by variable# nvar. Remove terms with zero coefficients
//and also terms where the initial power of nvar is equal zero
//
   poly2d_term  v3;
   std::vector<poly2d_term> newdata;
   newdata.reserve(data.size());
   unsigned cur_pwr = 0, maxp = 0, oldp = max_pwr;
   for (unsigned it = 0; it < data.size(); ++it) {
      v3 = data[it];
      if ((v3.coeff != 0.) && (v3.np[nvar] > 0)) {
         --v3.np[nvar];
         newdata.push_back(v3);
         if ((cur_pwr = v3.np[0] + v3.np[1]) > maxp) maxp = cur_pwr;
      }
   }
   newdata.resize(newdata.size());
   max_pwr = maxp;
   data.swap(newdata);
   if (oldp >= NPwr) NPwr = GetMaxPow();
}
void poly2d_base::Diff ( int  nvar)

Definition at line 222 of file poly2d_base.cc.

References magfieldparam::poly2d_term::coeff, data, GetMaxPow(), max_pwr, magfieldparam::poly2d_term::np, and NPwr.

Referenced by magfieldparam::rz_harm_poly::GetDiff().

{
//differentiate the polynomial by variable nvar.
//
   poly2d_term  v3;
   std::vector<poly2d_term> newdata;
   newdata.reserve(data.size());
   unsigned cur_pwr = 0, maxp = 0, oldp = max_pwr;
   for (unsigned it = 0; it < data.size(); ++it) {
      v3 = data[it];
      v3.coeff *= v3.np[nvar];
      if (v3.coeff != 0.) {
         --v3.np[nvar];
         newdata.push_back(v3);
         if ((cur_pwr = v3.np[0] + v3.np[1]) > maxp) maxp = cur_pwr;
      }
   }
   newdata.resize(newdata.size());
   max_pwr = maxp;
   data.swap(newdata);
   if (oldp >= NPwr) NPwr = GetMaxPow();
}
double poly2d_base::Eval ( )

Definition at line 157 of file poly2d_base.cc.

References data, j, runTheMatrix::np, and rz_pow.

Referenced by magfieldparam::HarmBasis3DCyl::EvalBphi(), magfieldparam::HarmBasis3DCyl::EvalRZ(), and GetVal().

{
   double S = 0.;
   for (unsigned j = 0; j < data.size(); ++j)
      S += data[j].coeff*rz_pow[data[j].np[0]][data[j].np[1]];
   return S;
}
void poly2d_base::FillTable ( const double  r,
const double  z 
) [static, protected]

Definition at line 89 of file poly2d_base.cc.

References NPwr, and rz_pow.

Referenced by SetPoint().

{
   if (!rz_pow) return;
   unsigned jr, jz;
   for (jz = 1; jz <= NPwr; ++jz) rz_pow[0][jz] = z*rz_pow[0][jz-1];
   for (jr = 1; jr <= NPwr; ++jr) {
      for (jz = 0; jz <= (NPwr - jr); ++jz) {
         rz_pow[jr][jz] = r*rz_pow[jr-1][jz];
      }
   }
}
int poly2d_base::GetMaxPow ( ) [static]

Definition at line 102 of file poly2d_base.cc.

References poly2d_base_set.

Referenced by AdjustTab(), DecPow(), Diff(), IncPow(), Int(), and ~poly2d_base().

{
   int curp, maxp = 0;
   std::set<poly2d_base*>::iterator it;

   for (it = poly2d_base_set.begin(); it != poly2d_base_set.end(); ++it) {
      curp = (*it)->max_pwr;
      if (curp > maxp) maxp = curp;
   }
   return maxp;
}
double magfieldparam::poly2d_base::GetVal ( ) [inline]

Definition at line 106 of file poly2d_base.h.

References Eval(), and rz_set.

{if (rz_set) return Eval(); else return 0.;}
double magfieldparam::poly2d_base::GetVal ( const double  r,
const double  z 
) [inline]

Definition at line 107 of file poly2d_base.h.

References Eval(), and SetPoint().

{ SetPoint(r,z); return Eval();}
static void magfieldparam::poly2d_base::IncNPwr ( const unsigned  N) [inline, static]

Definition at line 72 of file poly2d_base.h.

References MultiGaussianStateTransform::N, and NPwr.

Referenced by magfieldparam::HarmBasis3DCyl::HarmBasis3DCyl().

{if (N > NPwr) NPwr = N;}
void poly2d_base::IncPow ( int  nvar)

Definition at line 260 of file poly2d_base.cc.

References data, GetMaxPow(), max_pwr, and NPwr.

Referenced by magfieldparam::rz_harm_poly::GetIncPow().

{
//Multiply the polynomial by variable# nvar
//
   for (unsigned it = 0; it < data.size(); ++it) {
      ++data[it].np[nvar];
   }
   ++max_pwr;
   if (max_pwr > NPwr) NPwr = GetMaxPow();
}
void poly2d_base::Int ( int  nvar)

Definition at line 246 of file poly2d_base.cc.

References data, GetMaxPow(), max_pwr, and NPwr.

Referenced by magfieldparam::rz_harm_poly::GetInt().

{
//Integrate the polynomial by variable# nvar. Doesn't remove terms
//with zero coefficients; if you suspect they can appear, use Compress()
//after the integration.
//
   for (unsigned it = 0; it < data.size(); ++it) {
      data[it].coeff /= ++data[it].np[nvar];
   }
   ++max_pwr;
   if (max_pwr > NPwr) NPwr = GetMaxPow();
}
bool magfieldparam::poly2d_base::IsOn ( ) [inline]

Definition at line 91 of file poly2d_base.h.

References data.

{ return bool(data.size());}
bool magfieldparam::poly2d_base::IsRZSet ( ) [inline]

Definition at line 92 of file poly2d_base.h.

References rz_set.

{ return rz_set;}
void poly2d_base::Print ( std::ostream &  out = std::cout,
const std::streamsize  prec = 5 
)

Reimplemented in magfieldparam::rz_harm_poly.

Definition at line 204 of file poly2d_base.cc.

References data, and max_pwr.

{
   if (!data.size()) {
      out << "\"poly2d_base\" object contains no terms." << std::endl;
      return;
   }
   out << data.size() << " terms; max. degree = " << max_pwr << ":" << std::endl;
   std::streamsize old_prec = out.precision();
   out.precision(prec);
   data[0].Print(out);
   for (unsigned it = 1; it < data.size(); ++it) {
         data[it].Print(out, false);
   }
   out << std::endl;
   out.precision(old_prec);
}
void poly2d_base::PrintTab ( std::ostream &  out = std::cout,
const std::streamsize  prec = 5 
) [static]

Definition at line 122 of file poly2d_base.cc.

References NPwr, NTab, and rz_pow.

{
   out << "poly2d_base table size NTab = " << NTab
       << "\tmax. power NPwr = " << NPwr << std::endl;
   if (rz_pow) {
      if (NPwr < NTab) {
         std::streamsize old_prec = out.precision(), wdt = prec+7; 
         out.precision(prec);
         out << "Table content:" << std::endl;
         unsigned jr, jz;
         for (jr = 0; jr <= NPwr; ++jr) {
            for (jz = 0; jz <= (NPwr-jr); ++jz) {
               out << std::setw(wdt) << std::left << rz_pow[jr][jz];
            }
            out << "|" << std::endl;
         }
         out.precision(old_prec);
      } else {
         out << "\tTable size is not adjusted." << std::endl;
      }
   } else {
      out << "\tTable is not allocated." << std::endl;
   }
}
void poly2d_base::Scale ( const double  C)

Definition at line 296 of file poly2d_base.cc.

References funct::C, and data.

Referenced by magfieldparam::rz_harm_poly::LadderDwn(), and magfieldparam::rz_harm_poly::LadderUp().

{
//Multiply the polynomial by a constant.
//
   if (C != 0.) {
      for (unsigned it = 0; it < data.size(); ++it) {
         data[it].coeff *= C;
      }
   } else data.resize(0);
}
void poly2d_base::SetPoint ( const double  r,
const double  z 
) [static]

Definition at line 148 of file poly2d_base.cc.

References Count(), FillTable(), NPwr, NTab, rz_set, SetTabSize(), and zval.

Referenced by GetVal().

{
   if (!Count()) return;
   if (NPwr >= NTab) { SetTabSize(NPwr+1); FillTable(r, z);}
   else if ((r != rval) || (z != zval)) FillTable(r, z);
   rz_set = true;
}
void poly2d_base::SetTabSize ( const unsigned  N) [static, protected]

Definition at line 69 of file poly2d_base.cc.

References MultiGaussianStateTransform::N, NTab, rz_pow, and zval.

Referenced by AdjustTab(), and SetPoint().

{
   if (N <= NTab) return;
   if (rz_pow) {
      delete [] rz_pow[0];
      delete [] rz_pow;
   }
   rz_pow    = new double* [N];
   unsigned jr, dN = N*(N+1)/2;
   rz_pow[0] = new double  [dN];
   memset(rz_pow[0], 0, dN*sizeof(double));
   rz_pow[0][0] = 1.;
   for (jr = 1, dN = N; jr < N; ++jr, --dN) {
      rz_pow[jr] = rz_pow[jr-1] + dN;
   }
   rval = zval = 0.;
   NTab = N;
}

Member Data Documentation

const double poly2d_base::MIN_COEFF = DBL_EPSILON [static, protected]

Definition at line 57 of file poly2d_base.h.

Referenced by Collect().

unsigned poly2d_base::NPwr = 0 [static, protected]
unsigned poly2d_base::NTab = 0 [static, protected]

Definition at line 53 of file poly2d_base.h.

Referenced by AdjustTab(), PrintTab(), SetPoint(), SetTabSize(), and ~poly2d_base().

std::set< poly2d_base * > poly2d_base::poly2d_base_set [static, protected]
double poly2d_base::rval = 0. [static, protected]

Definition at line 49 of file poly2d_base.h.

double ** poly2d_base::rz_pow = 0 [static, protected]

Definition at line 52 of file poly2d_base.h.

Referenced by Eval(), FillTable(), PrintTab(), SetTabSize(), and ~poly2d_base().

bool poly2d_base::rz_set = false [static, protected]
double poly2d_base::zval = 0. [static, protected]

Definition at line 50 of file poly2d_base.h.

Referenced by SetPoint(), SetTabSize(), and ~poly2d_base().