npstat is hosted by Hepforge, IPPP Durham
NPStat  5.10.0
LinearMapper1d.hh
Go to the documentation of this file.
1 #ifndef NPSTAT_LINEARMAPPER1D_HH_
2 #define NPSTAT_LINEARMAPPER1D_HH_
3 
4 /*!
5 // \file LinearMapper1d.hh
6 //
7 // \brief Linear transformation functor
8 //
9 // Author: I. Volobouev
10 //
11 // October 2009
12 */
13 
14 #include <stdexcept>
15 
16 namespace npstat {
17  /** Functor which performs linear mapping in 1-d */
18  template<class Numeric>
20  {
21  public:
22  /** Default constructor builds an identity transformation */
23  inline LinearMapper1dTmpl() : a_(1.0), b_(0.0) {}
24 
25  /**
26  // Transform definition from two points. The point at x0
27  // is mapped into y0, the point at x1 is mapped into y1.
28  // The linear transformation is thus fully defined.
29  */
30  inline LinearMapper1dTmpl(const Numeric x0, const Numeric y0,
31  const Numeric x1, const Numeric y1)
32  {
33  const Numeric dx = x1 - x0;
34  if (!dx) throw std::invalid_argument(
35  "In npstat::LinearMapper1dTmpl constructor: "
36  "invalid arguments (x0 == x1)");
37  a_ = (y1 - y0)/dx;
38  b_ = ((y0 + y1) - a_*(x0 + x1))/2.0;
39  }
40 
41  /** Explicitly provide the transform coefficients as in y = ca*x + cb */
42  inline LinearMapper1dTmpl(const Numeric ca, const Numeric cb)
43  : a_(ca), b_(cb) {}
44 
45  /** Perform the transformation */
46  inline Numeric operator()(const Numeric& x) const {return a_*x + b_;}
47 
48  /** Get the linear coefficient of the transform */
49  inline Numeric a() const {return a_;}
50 
51  /** Get the transform constant */
52  inline Numeric b() const {return b_;}
53 
54  /** Create the inverse transform */
55  inline LinearMapper1dTmpl inverse() const
56  {
57  if (!a_) throw std::invalid_argument(
58  "In npstat::LinearMapper1dTmpl::inverse: "
59  "mapping is not invertible");
60  return LinearMapper1dTmpl(1.0/a_, -b_/a_);
61  }
62 
63  /** Sequence of two transforms: the one on the right is applied first */
65  {
66  return LinearMapper1dTmpl(a_*r.a_, a_*r.b_ + b_);
67  }
68 
69  private:
70  Numeric a_;
71  Numeric b_;
72  };
73 
74  /** Typedefs for typical template parameters */
78 }
79 
80 #endif // NPSTAT_LINEARMAPPER1D_HH_
Definition: LinearMapper1d.hh:20
LinearMapper1dTmpl()
Definition: LinearMapper1d.hh:23
Numeric a() const
Definition: LinearMapper1d.hh:49
LinearMapper1dTmpl operator*(const LinearMapper1dTmpl &r) const
Definition: LinearMapper1d.hh:64
Numeric b() const
Definition: LinearMapper1d.hh:52
LinearMapper1dTmpl(const Numeric x0, const Numeric y0, const Numeric x1, const Numeric y1)
Definition: LinearMapper1d.hh:30
Numeric operator()(const Numeric &x) const
Definition: LinearMapper1d.hh:46
LinearMapper1dTmpl inverse() const
Definition: LinearMapper1d.hh:55
LinearMapper1dTmpl(const Numeric ca, const Numeric cb)
Definition: LinearMapper1d.hh:42
Definition: AbsArrayProjector.hh:14
LinearMapper1dTmpl< double > LinearMapper1d
Definition: LinearMapper1d.hh:75