CMS 3D CMS Logo

Fraction.h
Go to the documentation of this file.
1 #ifndef PhysicsTools_Utilities_Fraction_h
2 #define PhysicsTools_Utilities_Fraction_h
3 
6 #include <numeric>
7 
8 namespace funct {
9 
10  template <int n, int m>
11  struct FractionStruct {
12  static_assert(m != 0);
13  static const int numerator = n, denominator = m;
14  double operator()() const { return double(n) / double(m); }
15  operator double() const { return double(n) / double(m); }
16  double operator()(double) const { return double(n) / double(m); }
17  double operator()(double, double) const { return double(n) / double(m); }
18  };
19 
20  template <int n, int m, unsigned gcd = std::gcd(n, m), int num = n / gcd, int den = m / gcd>
23  };
24 
25  template <int n, int m, unsigned gcd, int num>
26  struct PositiveFraction<n, m, gcd, num, 1> {
28  };
29 
30  template <int n, int m, bool pn = (n >= 0), bool pm = (m >= 0)>
31  struct Fraction {
33  };
34 
35  template <int n, int m>
36  const typename Fraction<n, m>::type& fract() {
37  static typename Fraction<n, m>::type c;
38  return c;
39  }
40 
41  template <int n, int m>
42  struct Ratio<Numerical<n>, Numerical<m> > {
43  typedef typename Fraction<n, m>::type type;
44  inline static type combine(const Numerical<n>&, const Numerical<m>&) { return fract<n, m>(); }
45  };
46 
47  template <int n, int m>
48  struct Fraction<n, m, false, true> {
49  typedef typename Minus<typename PositiveFraction<-n, m>::type>::type type;
50  };
51 
52  template <int n, int m>
53  struct Fraction<n, m, true, false> {
54  typedef typename Minus<typename PositiveFraction<n, -m>::type>::type type;
55  };
56 
57  template <int n, int m>
58  struct Fraction<n, m, false, false> {
59  typedef typename Minus<typename PositiveFraction<-n, -m>::type>::type type;
60  };
61 
62  template <int a, int b, int c>
64  typedef typename Fraction<a * b, c>::type type;
65  inline static type combine(const Numerical<a>&, const FractionStruct<b, c>&) { return fract<a * b, c>(); }
66  };
67 
68  template <int a, int b, int c>
70  typedef typename Fraction<a * b, c>::type type;
71  inline static type combine(const FractionStruct<b, c>&, const Numerical<a>&) { return fract<a * b, c>(); }
72  };
73 
74  template <int a, int b, int c>
75  struct Ratio<Numerical<a>, FractionStruct<b, c> > {
76  typedef typename Fraction<a * c, b>::type type;
77  inline static type combine(const Numerical<a>&, const FractionStruct<b, c>&) { return fract<a * c, b>(); }
78  };
79 
80  template <int a, int b, int c>
81  struct Sum<Numerical<a>, FractionStruct<b, c> > {
83  inline static type combine(const Numerical<a>&, const FractionStruct<b, c>&) { return fract<a * c + b, b>(); }
84  };
85 
86  template <int a, int b, int c>
88  typedef typename Fraction<a * c - b, b>::type type;
89  inline static type combine(const Numerical<a>&, const FractionStruct<b, c>&) { return fract<a * c - b, b>(); }
90  };
91 
92  template <int a, int b, int c>
93  struct Sum<FractionStruct<b, c>, Numerical<a> > {
95  inline static type combine(const FractionStruct<b, c>&, const Numerical<a>&) { return fract<a * c + b, b>(); }
96  };
97 
98  template <int a, int b, int c>
99  struct Ratio<FractionStruct<b, c>, Numerical<a> > {
100  typedef typename Fraction<b, a * c>::type type;
101  inline static type combine(const FractionStruct<b, c>&, const Numerical<a>&) { return fract<b, a * c>(); }
102  };
103 
104  template <int a, int b, int c, int d>
107  inline static type combine(const FractionStruct<a, b>&, const FractionStruct<c, d>&) {
108  return fract<a * d + c * b, b * d>();
109  }
110  };
111 
112  template <int a, int b, int c, int d>
114  typedef typename Fraction<a * d - c * b, b * d>::type type;
115  inline static type combine(const FractionStruct<a, b>&, const FractionStruct<c, d>&) {
116  return fract<a * d - c * b, b * d>();
117  }
118  };
119 
120  template <int a, int b, int c, int d>
123  inline static type combine(const FractionStruct<a, b>&, const FractionStruct<c, d>&) {
124  return fract<a * c, b * d>();
125  }
126  };
127 
128  template <int a, int b, int c, int d>
131  inline static type combine(const FractionStruct<a, b>&, const FractionStruct<c, d>&) {
132  return fract<a * d, b * c>();
133  }
134  };
135 
136 } // namespace funct
137 
138 #endif
const Numerical< n > & num()
Definition: Numerical.h:18
static type combine(const Numerical< a > &, const FractionStruct< b, c > &)
Definition: Fraction.h:89
static type combine(const Numerical< a > &, const FractionStruct< b, c > &)
Definition: Fraction.h:83
Definition: Abs.h:5
static type combine(const FractionStruct< b, c > &, const Numerical< a > &)
Definition: Fraction.h:71
Minus< typename PositiveFraction<-n, m >::type >::type type
Definition: Fraction.h:49
FractionStruct< num, den > type
Definition: Fraction.h:22
static const int denominator
Definition: Fraction.h:13
static type combine(const Numerical< n > &, const Numerical< m > &)
Definition: Fraction.h:44
static type combine(const Numerical< a > &, const FractionStruct< b, c > &)
Definition: Fraction.h:77
static type combine(const Numerical< a > &, const FractionStruct< b, c > &)
Definition: Fraction.h:65
static type combine(const FractionStruct< a, b > &, const FractionStruct< c, d > &)
Definition: Fraction.h:115
Minus< typename PositiveFraction<-n, -m >::type >::type type
Definition: Fraction.h:59
PositiveFraction< n, m >::type type
Definition: Fraction.h:32
double operator()(double, double) const
Definition: Fraction.h:17
const Fraction< n, m >::type & fract()
Definition: Fraction.h:36
static const int numerator
Definition: Fraction.h:13
double operator()() const
Definition: Fraction.h:14
d
Definition: ztail.py:151
static type combine(const FractionStruct< a, b > &, const FractionStruct< c, d > &)
Definition: Fraction.h:123
static type combine(const FractionStruct< a, b > &, const FractionStruct< c, d > &)
Definition: Fraction.h:131
double b
Definition: hdecay.h:120
Minus< typename PositiveFraction< n, -m >::type >::type type
Definition: Fraction.h:54
static type combine(const FractionStruct< a, b > &, const FractionStruct< c, d > &)
Definition: Fraction.h:107
double a
Definition: hdecay.h:121
Definition: Sum.h:18
double operator()(double) const
Definition: Fraction.h:16
static type combine(const FractionStruct< b, c > &, const Numerical< a > &)
Definition: Fraction.h:95
static type combine(const FractionStruct< b, c > &, const Numerical< a > &)
Definition: Fraction.h:101