npstat is hosted by Hepforge, IPPP Durham
NPStat  5.10.0
AbsDistributionTransform1D.hh
Go to the documentation of this file.
1 #ifndef NPSTAT_ABSDISTRIBUTIONTRANSFORM1D_HH_
2 #define NPSTAT_ABSDISTRIBUTIONTRANSFORM1D_HH_
3 
4 /*!
5 // \file AbsDistributionTransform1D.hh
6 //
7 // \brief Interface definition for 1-d coordinate transformations used
8 // to build statistical distributions
9 //
10 // Author: I. Volobouev
11 //
12 // April 2015
13 */
14 
15 #include <cassert>
16 #include <typeinfo>
17 #include <stdexcept>
18 #include <utility>
19 
20 #include "geners/ClassId.hh"
22 
23 namespace npstat {
24  /**
25  // It will be assumed that transforms implementing this interface
26  // are monotonous
27  */
29  {
30  public:
31  inline explicit AbsDistributionTransform1D(const unsigned nParams)
32  : npara_(nParams) {}
33 
34  inline virtual ~AbsDistributionTransform1D() {}
35 
36  /** "Virtual copy constructor" */
37  virtual AbsDistributionTransform1D* clone() const = 0;
38 
39  inline unsigned nParameters() const {return npara_;}
40 
41  inline void setParameter(const unsigned which, const double value)
42  {
43  if (which >= npara_) throw std::invalid_argument(
44  "In npstat::AbsDistributionTransform1D::setParameter: "
45  "parameter number out of range");
46  this->setParameterChecked(which, value);
47  }
48 
49  inline void setAllParameters(const double* p, const unsigned len)
50  {
51  if (len != npara_) throw std::invalid_argument(
52  "In npstat::AbsDistributionTransform1D::setAllParameters: "
53  "wrong number of parameters");
54  if (len)
55  assert(p);
56  this->setAllParametersChecked(p);
57  }
58 
59  inline double getParameter(const unsigned which) const
60  {
61  if (which >= npara_) throw std::invalid_argument(
62  "In npstat::AbsDistributionTransform1D::getParameter: "
63  "parameter number out of range");
64  return this->getParameterChecked(which);
65  }
66 
67  /** In this method, dydx is allowed to be a null pointer */
68  virtual double transformForward(double x, double* dydx) const = 0;
69 
70  virtual double transformBack(double y) const = 0;
71 
72  /** Is y increasing or decreasing as a function of x? */
73  virtual bool isIncreasing() const = 0;
74 
75  /**
76  // Derived classes should not implement "operator==", implement
77  // "isEqual" instead
78  */
79  inline bool operator==(const AbsDistributionTransform1D& r) const
80  {
81  return (typeid(*this) == typeid(r)) &&
82  npara_ == r.npara_ && this->isEqual(r);
83  }
84 
85  /** Logical negation of operator== */
86  inline bool operator!=(const AbsDistributionTransform1D& r) const
87  {return !(*this == r);}
88 
89  //@{
90  /** Prototype needed for I/O */
91  virtual gs::ClassId classId() const = 0;
92  virtual bool write(std::ostream&) const {return false;}
93  //@}
94 
95  static inline const char* classname()
96  {return "npstat::AbsDistributionTransform1D";}
97  static inline unsigned version() {return 1;}
98  static AbsDistributionTransform1D* read(
99  const gs::ClassId& id, std::istream& is);
100 
101  protected:
102  /** Comparison for equality. To be implemented by derived classes. */
103  virtual bool isEqual(const AbsDistributionTransform1D&) const = 0;
104 
105  private:
106  virtual void setParameterChecked(unsigned which, double value) = 0;
107  virtual void setAllParametersChecked(const double* p) = 0;
108  virtual double getParameterChecked(unsigned which) const = 0;
109 
110  unsigned npara_;
111 
112 #ifdef SWIG
113  public:
114  inline double transformForward2(const double x) const
115  {
116  double dydx;
117  return this->transformForward(x, &dydx);
118  }
119 
120  inline std::pair<double,double> transformWithDerivative(const double x) const
121  {
122  double dydx;
123  const double y = this->transformForward(x, &dydx);
124  return std::pair<double,double>(y, dydx);
125  }
126 #endif // SWIG
127  };
128 
129  /** A functor for the forward transform */
130  class ForwardDistroTransform1DFunctor : public Functor1<double, double>
131  {
132  public:
134  : transform_(t) {}
135 
136  inline virtual ~ForwardDistroTransform1DFunctor() {}
137 
138  inline virtual double operator()(const double& x) const
139  {return transform_.transformForward(x, 0);}
140 
141  private:
142  const AbsDistributionTransform1D& transform_;
143  };
144 
145  /** A functor for the inverse transform */
146  class BackDistroTransform1DFunctor : public Functor1<double, double>
147  {
148  public:
150  : transform_(t) {}
151 
152  inline virtual ~BackDistroTransform1DFunctor() {}
153 
154  inline virtual double operator()(const double& x) const
155  {return transform_.transformBack(x);}
156 
157  private:
158  const AbsDistributionTransform1D& transform_;
159  };
160 
161  /** A functor acting on the transformed coordinate */
162  class TransformedFunctor1 : public Functor1<double, double>
163  {
164  public:
167  const bool isForward)
168  : transform_(t), fcn_(fcn), isForward_(isForward) {}
169 
170  inline virtual ~TransformedFunctor1() {}
171 
172  inline virtual double operator()(const double& x) const
173  {
174  double y;
175  if (isForward_)
176  y = transform_.transformForward(x, 0);
177  else
178  y = transform_.transformBack(x);
179  return fcn_(y);
180  }
181 
182  private:
183  const AbsDistributionTransform1D& transform_;
184  const Functor1<double, double>& fcn_;
185  bool isForward_;
186  };
187 }
188 
189 #endif // NPSTAT_ABSDISTRIBUTIONTRANSFORM1D_HH_
Interface definitions and concrete simple functors for a variety of functor-based calculations.
Definition: AbsDistributionTransform1D.hh:29
virtual bool isIncreasing() const =0
virtual double transformForward(double x, double *dydx) const =0
bool operator==(const AbsDistributionTransform1D &r) const
Definition: AbsDistributionTransform1D.hh:79
virtual gs::ClassId classId() const =0
virtual AbsDistributionTransform1D * clone() const =0
bool operator!=(const AbsDistributionTransform1D &r) const
Definition: AbsDistributionTransform1D.hh:86
virtual bool isEqual(const AbsDistributionTransform1D &) const =0
Definition: AbsDistributionTransform1D.hh:147
Definition: AbsDistributionTransform1D.hh:131
Definition: AbsDistributionTransform1D.hh:163
Definition: AbsArrayProjector.hh:14
Definition: SimpleFunctors.hh:58