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 <boost/integer/common_factor.hpp>
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 
21  int num = n / gcd, int den = m / gcd>
24  };
25 
26  template<int n, int m, unsigned gcd, int num>
27  struct PositiveFraction<n, m , gcd, num, 1> {
29  };
30 
31  template<int n, int m, bool pn = (n >= 0), bool pm = (m >= 0)>
32  struct Fraction {
34  };
35 
36  template<int n, int m>
37  const typename Fraction<n, m>::type& fract() {
38  static typename Fraction<n, m>::type c;
39  return c;
40  }
41 
42  template<int n, int m>
43  struct Ratio<Numerical<n>, Numerical<m> > {
44  typedef typename Fraction<n, m>::type type;
45  inline static type combine(const Numerical<n>&, const Numerical<m>&) {
46  return fract<n, m>();
47  }
48  };
49 
50  template<int n, int m>
51  struct Fraction<n, m, false, true> {
52  typedef typename Minus<typename PositiveFraction<-n, m>::type>::type type;
53  };
54 
55  template<int n, int m>
56  struct Fraction<n, m, true, false> {
57  typedef typename Minus<typename PositiveFraction<n, -m>::type>::type type;
58  };
59 
60  template<int n, int m>
61  struct Fraction<n, m, false, false> {
62  typedef typename Minus<typename PositiveFraction<-n, -m>::type>::type type;
63  };
64 
65  template<int a, int b, int c>
66  struct Product<Numerical<a>, FractionStruct<b, c> > {
67  typedef typename Fraction<a * b, c>::type type;
68  inline static type combine(const Numerical<a>&, const FractionStruct<b, c>&) {
69  return fract<a * b, c>();
70  }
71  };
72 
73  template<int a, int b, int c>
74  struct Product<FractionStruct<b, c>, Numerical<a> > {
75  typedef typename Fraction<a * b, c>::type type;
76  inline static type combine(const FractionStruct<b, c>&, const Numerical<a>&) {
77  return fract<a * b, c>();
78  }
79  };
80 
81  template<int a, int b, int c>
82  struct Ratio<Numerical<a>, FractionStruct<b, c> > {
83  typedef typename Fraction<a * c, b>::type type;
84  inline static type combine(const Numerical<a>&, const FractionStruct<b, c>&) {
85  return fract<a * c, b>();
86  }
87  };
88 
89  template<int a, int b, int c>
90  struct Sum<Numerical<a>, FractionStruct<b, c> > {
92  inline static type combine(const Numerical<a>&, const FractionStruct<b, c>&) {
93  return fract<a * c + b, b>();
94  }
95  };
96 
97  template<int a, int b, int c>
99  typedef typename Fraction<a * c - b, b>::type type;
100  inline static type combine(const Numerical<a>&, const FractionStruct<b, c>&) {
101  return fract<a * c - b, b>();
102  }
103  };
104 
105  template<int a, int b, int c>
106  struct Sum<FractionStruct<b, c>, Numerical<a> > {
108  inline static type combine(const FractionStruct<b, c>&, const Numerical<a>&) {
109  return fract<a * c + b, b>();
110  }
111  };
112 
113  template<int a, int b, int c>
114  struct Ratio<FractionStruct<b, c>, Numerical<a> > {
115  typedef typename Fraction<b, a * c>::type type;
116  inline static type combine(const FractionStruct<b, c>&, const Numerical<a>&) {
117  return fract<b, a * c>();
118  }
119  };
120 
121  template<int a, int b, int c, int d>
122  struct Sum<FractionStruct<a, b>, FractionStruct<c, d> > {
124  inline static type combine(const FractionStruct<a, b>&, const FractionStruct<c, d>&) {
125  return fract<a * d + c * b, b * d>();
126  }
127  };
128 
129  template<int a, int b, int c, int d>
131  typedef typename Fraction<a * d - c * b, b * d>::type type;
132  inline static type combine(const FractionStruct<a, b>&, const FractionStruct<c, d>&) {
133  return fract<a * d - c * b, b * d>();
134  }
135  };
136 
137  template<int a, int b, int c, int d>
138  struct Product<FractionStruct<a, b>, FractionStruct<c, d> > {
140  inline static type combine(const FractionStruct<a, b>&, const FractionStruct<c, d>&) {
141  return fract<a * c, b * d>();
142  }
143  };
144 
145  template<int a, int b, int c, int d>
146  struct Ratio<FractionStruct<a, b>, FractionStruct<c, d> > {
148  inline static type combine(const FractionStruct<a, b>&, const FractionStruct<c, d>& ) {
149  return fract<a * d, b * c>();
150  }
151  };
152 
153 }
154 
155 #endif
const Numerical< n > & num()
Definition: Numerical.h:16
static type combine(const Numerical< a > &, const FractionStruct< b, c > &)
Definition: Fraction.h:100
static type combine(const Numerical< a > &, const FractionStruct< b, c > &)
Definition: Fraction.h:92
Definition: Abs.h:5
static type combine(const FractionStruct< b, c > &, const Numerical< a > &)
Definition: Fraction.h:76
Minus< typename PositiveFraction<-n, m >::type >::type type
Definition: Fraction.h:52
FractionStruct< num, den > type
Definition: Fraction.h:23
static const int denominator
Definition: Fraction.h:13
static type combine(const Numerical< n > &, const Numerical< m > &)
Definition: Fraction.h:45
static type combine(const Numerical< a > &, const FractionStruct< b, c > &)
Definition: Fraction.h:84
static type combine(const Numerical< a > &, const FractionStruct< b, c > &)
Definition: Fraction.h:68
static type combine(const FractionStruct< a, b > &, const FractionStruct< c, d > &)
Definition: Fraction.h:132
Minus< typename PositiveFraction<-n,-m >::type >::type type
Definition: Fraction.h:62
PositiveFraction< n, m >::type type
Definition: Fraction.h:33
const Fraction< n, m >::type & fract()
Definition: Fraction.h:37
static const int numerator
Definition: Fraction.h:13
static type combine(const FractionStruct< a, b > &, const FractionStruct< c, d > &)
Definition: Fraction.h:140
static type combine(const FractionStruct< a, b > &, const FractionStruct< c, d > &)
Definition: Fraction.h:148
double operator()(double, double) const
Definition: Fraction.h:17
double b
Definition: hdecay.h:120
Minus< typename PositiveFraction< n,-m >::type >::type type
Definition: Fraction.h:57
static type combine(const FractionStruct< a, b > &, const FractionStruct< c, d > &)
Definition: Fraction.h:124
double a
Definition: hdecay.h:121
Definition: Sum.h:26
double operator()() const
Definition: Fraction.h:14
static type combine(const FractionStruct< b, c > &, const Numerical< a > &)
Definition: Fraction.h:108
double operator()(double) const
Definition: Fraction.h:16
static type combine(const FractionStruct< b, c > &, const Numerical< a > &)
Definition: Fraction.h:116