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
funct::Minus
Definition: Minus.h:18
funct::PositiveFraction< n, m, gcd, num, 1 >::type
Numerical< num > type
Definition: Fraction.h:27
funct::Sum< FractionStruct< b, c >, Numerical< a > >::combine
static type combine(const FractionStruct< b, c > &, const Numerical< a > &)
Definition: Fraction.h:95
funct::false
false
Definition: Factorize.h:29
dqmiodumpmetadata.n
n
Definition: dqmiodumpmetadata.py:28
funct::Fraction< n, m, true, false >::type
Minus< typename PositiveFraction< n, -m >::type >::type type
Definition: Fraction.h:54
funct::PositiveFraction::type
FractionStruct< num, den > type
Definition: Fraction.h:22
funct::Ratio< FractionStruct< a, b >, FractionStruct< c, d > >::type
Fraction< a *d, b *c >::type type
Definition: Fraction.h:130
funct::Sum< Numerical< a >, FractionStruct< b, c > >::combine
static type combine(const Numerical< a > &, const FractionStruct< b, c > &)
Definition: Fraction.h:83
funct::FractionStruct::operator()
double operator()() const
Definition: Fraction.h:14
funct::Ratio< FractionStruct< b, c >, Numerical< a > >::type
Fraction< b, a *c >::type type
Definition: Fraction.h:100
funct::Ratio< FractionStruct< a, b >, FractionStruct< c, d > >::combine
static type combine(const FractionStruct< a, b > &, const FractionStruct< c, d > &)
Definition: Fraction.h:131
funct::Difference< FractionStruct< a, b >, FractionStruct< c, d > >::combine
static type combine(const FractionStruct< a, b > &, const FractionStruct< c, d > &)
Definition: Fraction.h:115
funct::Ratio
Definition: Ratio.h:17
funct::Sum< FractionStruct< a, b >, FractionStruct< c, d > >::type
Fraction< a *d+c *b, b *d >::type type
Definition: Fraction.h:106
funct::FractionStruct::numerator
static const int numerator
Definition: Fraction.h:13
funct::Ratio< Numerical< a >, FractionStruct< b, c > >::combine
static type combine(const Numerical< a > &, const FractionStruct< b, c > &)
Definition: Fraction.h:77
funct::Ratio< FractionStruct< b, c >, Numerical< a > >::combine
static type combine(const FractionStruct< b, c > &, const Numerical< a > &)
Definition: Fraction.h:101
funct::Ratio< Numerical< n >, Numerical< m > >::type
Fraction< n, m >::type type
Definition: Fraction.h:43
funct::Product< Numerical< a >, FractionStruct< b, c > >::combine
static type combine(const Numerical< a > &, const FractionStruct< b, c > &)
Definition: Fraction.h:65
Numerical.h
funct::num
const Numerical< n > & num()
Definition: Numerical.h:18
funct::FractionStruct::operator()
double operator()(double) const
Definition: Fraction.h:16
funct::m
m
Definition: Factorize.h:45
funct::FractionStruct::denominator
static const int denominator
Definition: Fraction.h:13
b
double b
Definition: hdecay.h:118
funct::FractionStruct
Definition: Fraction.h:11
funct::true
true
Definition: Factorize.h:173
funct::Sum< FractionStruct< b, c >, Numerical< a > >::type
Fraction< a *c+b, b >::type type
Definition: Fraction.h:94
funct::Fraction
Definition: Fraction.h:31
a
double a
Definition: hdecay.h:119
funct::Product< FractionStruct< b, c >, Numerical< a > >::type
Fraction< a *b, c >::type type
Definition: Fraction.h:70
funct::MinusStruct
Definition: Minus.h:7
funct::Product< FractionStruct< a, b >, FractionStruct< c, d > >::combine
static type combine(const FractionStruct< a, b > &, const FractionStruct< c, d > &)
Definition: Fraction.h:123
funct::Difference
Definition: Difference.h:9
funct::fract
const Fraction< n, m >::type & fract()
Definition: Fraction.h:36
funct::Product< FractionStruct< a, b >, FractionStruct< c, d > >::type
Fraction< a *c, b *d >::type type
Definition: Fraction.h:122
funct::Fraction::type
PositiveFraction< n, m >::type type
Definition: Fraction.h:32
Operations.h
funct::PositiveFraction
Definition: Fraction.h:21
funct::Difference< FractionStruct< a, b >, FractionStruct< c, d > >::type
Fraction< a *d - c *b, b *d >::type type
Definition: Fraction.h:114
funct::Product
Definition: Product.h:17
funct::Ratio< Numerical< n >, Numerical< m > >::combine
static type combine(const Numerical< n > &, const Numerical< m > &)
Definition: Fraction.h:44
funct::Fraction< n, m, false, true >::type
Minus< typename PositiveFraction<-n, m >::type >::type type
Definition: Fraction.h:49
funct::Sum< Numerical< a >, FractionStruct< b, c > >::type
Fraction< a *c+b, b >::type type
Definition: Fraction.h:82
funct::Sum
Definition: Sum.h:18
funct::Sum< FractionStruct< a, b >, FractionStruct< c, d > >::combine
static type combine(const FractionStruct< a, b > &, const FractionStruct< c, d > &)
Definition: Fraction.h:107
funct::Product< FractionStruct< b, c >, Numerical< a > >::combine
static type combine(const FractionStruct< b, c > &, const Numerical< a > &)
Definition: Fraction.h:71
funct::Numerical
Definition: Numerical.h:7
ztail.d
d
Definition: ztail.py:151
funct::Product< Numerical< a >, FractionStruct< b, c > >::type
Fraction< a *b, c >::type type
Definition: Fraction.h:64
funct::Difference< Numerical< a >, FractionStruct< b, c > >::type
Fraction< a *c - b, b >::type type
Definition: Fraction.h:88
c
auto & c
Definition: CAHitNtupletGeneratorKernelsImpl.h:46
funct::FractionStruct::operator()
double operator()(double, double) const
Definition: Fraction.h:17
funct::Difference< Numerical< a >, FractionStruct< b, c > >::combine
static type combine(const Numerical< a > &, const FractionStruct< b, c > &)
Definition: Fraction.h:89
funct
Definition: Abs.h:5
funct::Ratio< Numerical< a >, FractionStruct< b, c > >::type
Fraction< a *c, b >::type type
Definition: Fraction.h:76
funct::Fraction< n, m, false, false >::type
Minus< typename PositiveFraction<-n, -m >::type >::type type
Definition: Fraction.h:59