CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
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/math/common_factor.hpp>
7 #include <boost/static_assert.hpp>
8 
9 namespace funct {
10 
11  template<int n, int m>
12  struct FractionStruct {
13  BOOST_STATIC_ASSERT(m != 0);
14  static const int numerator = n, denominator = m;
15  double operator()() const { return double(n) / double (m); }
16  operator double() const { return double(n) / double (m); }
17  double operator()(double) const { return double(n) / double (m); }
18  double operator()(double, double) const { return double(n) / double (m); }
19  };
20 
22  int num = n / gcd, int den = m / gcd>
25  };
26 
27  template<int n, int m, unsigned gcd, int num>
28  struct PositiveFraction<n, m , gcd, num, 1> {
30  };
31 
32  template<int n, int m, bool pn = (n >= 0), bool pm = (m >= 0)>
33  struct Fraction {
35  };
36 
37  template<int n, int m>
38  const typename Fraction<n, m>::type& fract() {
39  static typename Fraction<n, m>::type c;
40  return c;
41  }
42 
43  template<int n, int m>
44  struct Ratio<Numerical<n>, Numerical<m> > {
45  typedef typename Fraction<n, m>::type type;
46  inline static type combine(const Numerical<n>&, const Numerical<m>&) {
47  return fract<n, m>();
48  }
49  };
50 
51  template<int n, int m>
52  struct Fraction<n, m, false, true> {
53  typedef typename Minus<typename PositiveFraction<-n, m>::type>::type type;
54  };
55 
56  template<int n, int m>
57  struct Fraction<n, m, true, false> {
58  typedef typename Minus<typename PositiveFraction<n, -m>::type>::type type;
59  };
60 
61  template<int n, int m>
62  struct Fraction<n, m, false, false> {
63  typedef typename Minus<typename PositiveFraction<-n, -m>::type>::type type;
64  };
65 
66  template<int a, int b, int c>
67  struct Product<Numerical<a>, FractionStruct<b, c> > {
68  typedef typename Fraction<a * b, c>::type type;
69  inline static type combine(const Numerical<a>&, const FractionStruct<b, c>&) {
70  return fract<a * b, c>();
71  }
72  };
73 
74  template<int a, int b, int c>
75  struct Product<FractionStruct<b, c>, Numerical<a> > {
76  typedef typename Fraction<a * b, c>::type type;
77  inline static type combine(const FractionStruct<b, c>&, const Numerical<a>&) {
78  return fract<a * b, c>();
79  }
80  };
81 
82  template<int a, int b, int c>
83  struct Ratio<Numerical<a>, FractionStruct<b, c> > {
84  typedef typename Fraction<a * c, b>::type type;
85  inline static type combine(const Numerical<a>&, const FractionStruct<b, c>&) {
86  return fract<a * c, b>();
87  }
88  };
89 
90  template<int a, int b, int c>
91  struct Sum<Numerical<a>, FractionStruct<b, c> > {
93  inline static type combine(const Numerical<a>&, const FractionStruct<b, c>&) {
94  return fract<a * c + b, b>();
95  }
96  };
97 
98  template<int a, int b, int c>
100  typedef typename Fraction<a * c - b, b>::type type;
101  inline static type combine(const Numerical<a>&, const FractionStruct<b, c>&) {
102  return fract<a * c - b, b>();
103  }
104  };
105 
106  template<int a, int b, int c>
107  struct Sum<FractionStruct<b, c>, Numerical<a> > {
109  inline static type combine(const FractionStruct<b, c>&, const Numerical<a>&) {
110  return fract<a * c + b, b>();
111  }
112  };
113 
114  template<int a, int b, int c>
115  struct Ratio<FractionStruct<b, c>, Numerical<a> > {
116  typedef typename Fraction<b, a * c>::type type;
117  inline static type combine(const FractionStruct<b, c>&, const Numerical<a>&) {
118  return fract<b, a * c>();
119  }
120  };
121 
122  template<int a, int b, int c, int d>
123  struct Sum<FractionStruct<a, b>, FractionStruct<c, d> > {
125  inline static type combine(const FractionStruct<a, b>&, const FractionStruct<c, d>&) {
126  return fract<a * d + c * b, b * d>();
127  }
128  };
129 
130  template<int a, int b, int c, int d>
132  typedef typename Fraction<a * d - c * b, b * d>::type type;
133  inline static type combine(const FractionStruct<a, b>&, const FractionStruct<c, d>&) {
134  return fract<a * d - c * b, b * d>();
135  }
136  };
137 
138  template<int a, int b, int c, int d>
141  inline static type combine(const FractionStruct<a, b>&, const FractionStruct<c, d>&) {
142  return fract<a * c, b * d>();
143  }
144  };
145 
146  template<int a, int b, int c, int d>
147  struct Ratio<FractionStruct<a, b>, FractionStruct<c, d> > {
149  inline static type combine(const FractionStruct<a, b>&, const FractionStruct<c, d>& ) {
150  return fract<a * d, b * c>();
151  }
152  };
153 
154 }
155 
156 #endif
const Numerical< n > & num()
Definition: Numerical.h:16
static type combine(const Numerical< a > &, const FractionStruct< b, c > &)
Definition: Fraction.h:101
static type combine(const Numerical< a > &, const FractionStruct< b, c > &)
Definition: Fraction.h:93
static type combine(const FractionStruct< b, c > &, const Numerical< a > &)
Definition: Fraction.h:77
Minus< typename PositiveFraction<-n, m >::type >::type type
Definition: Fraction.h:53
FractionStruct< num, den > type
Definition: Fraction.h:24
static const int denominator
Definition: Fraction.h:14
static type combine(const Numerical< n > &, const Numerical< m > &)
Definition: Fraction.h:46
static type combine(const Numerical< a > &, const FractionStruct< b, c > &)
Definition: Fraction.h:85
static type combine(const Numerical< a > &, const FractionStruct< b, c > &)
Definition: Fraction.h:69
static type combine(const FractionStruct< a, b > &, const FractionStruct< c, d > &)
Definition: Fraction.h:133
Minus< typename PositiveFraction<-n,-m >::type >::type type
Definition: Fraction.h:63
PositiveFraction< n, m >::type type
Definition: Fraction.h:34
const Fraction< n, m >::type & fract()
Definition: Fraction.h:38
static const int numerator
Definition: Fraction.h:14
static type combine(const FractionStruct< a, b > &, const FractionStruct< c, d > &)
Definition: Fraction.h:141
static type combine(const FractionStruct< a, b > &, const FractionStruct< c, d > &)
Definition: Fraction.h:149
double operator()(double, double) const
Definition: Fraction.h:18
double b
Definition: hdecay.h:120
Minus< typename PositiveFraction< n,-m >::type >::type type
Definition: Fraction.h:58
static type combine(const FractionStruct< a, b > &, const FractionStruct< c, d > &)
Definition: Fraction.h:125
double a
Definition: hdecay.h:121
Definition: Sum.h:27
double operator()() const
Definition: Fraction.h:15
static type combine(const FractionStruct< b, c > &, const Numerical< a > &)
Definition: Fraction.h:109
double operator()(double) const
Definition: Fraction.h:17
static type combine(const FractionStruct< b, c > &, const Numerical< a > &)
Definition: Fraction.h:117