forked from cms-sw/cmssw
/
SimplifyRatio.h
341 lines (282 loc) · 11.7 KB
/
SimplifyRatio.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
#ifndef PhysicsTools_Utilities_SimplifyRatio_h
#define PhysicsTools_Utilities_SimplifyRatio_h
#include "PhysicsTools/Utilities/interface/Ratio.h"
#include "PhysicsTools/Utilities/interface/Product.h"
#include "PhysicsTools/Utilities/interface/Power.h"
#include "PhysicsTools/Utilities/interface/Minus.h"
#include "PhysicsTools/Utilities/interface/Fraction.h"
#include "PhysicsTools/Utilities/interface/DecomposePower.h"
#include "PhysicsTools/Utilities/interface/ParametricTrait.h"
#include "PhysicsTools/Utilities/interface/Simplify_begin.h"
#include <boost/mpl/if.hpp>
namespace funct {
// 0 / a = 0
RATIO_RULE(TYPT1, NUM(0), A, NUM(0) , num<0>());
// a / 1 = a
RATIO_RULE(TYPT1, A, NUM(1), A, _1);
// ( a * b )/ 1 = a * b
RATIO_RULE(TYPT2, PROD_S(A, B), NUM(1), PROD(A, B), _1);
// a / ( -n ) = - ( a / n )
template <int n, typename A, bool positive = (n>= 0)>
struct SimplifyNegativeRatio {
typedef RATIO_S(A, NUM(n)) type;
COMBINE(A, NUM(n), type(_1, _2));
};
TEMPL(N1T1)
struct SimplifyNegativeRatio<n, A, false> {
typedef MINUS(RATIO(A, NUM(-n))) type;
COMBINE(A, NUM(n), - (_1 / num<-n>()));
};
TEMPL(N1T1) struct Ratio<A, NUM(n)> :
public SimplifyNegativeRatio<n, A> { };
// ( -a ) / b = - ( a / b )
RATIO_RULE(TYPT2, MINUS_S(A), B, MINUS(RATIO(A, B)), -(_1._ / _2));
// ( -a ) / n = - ( a / n )
RATIO_RULE(TYPN1T1, MINUS_S(A), NUM(n), MINUS(RATIO(A, NUM(n))), -(_1._ / _2));
//TEMPL( N1T2 struct Ratio<PROD_S( A, B ), NUM( n )> :
// public SimplifyNegativeRatio<n, PROD_S( A, B )> { };
// n / ( m * a ) = (n/m) * a
/* WRONG!!
RATIO_RULE(TYPN2T1, NUM(n), PROD_S(NUM(m), A), \
PROD(FRACT(n, m), A), (fract<n, m>() * _2._2));
*/
// ( a / b ) / c = a / ( b * c )
RATIO_RULE(TYPT3, RATIO_S(A, B), C, \
RATIO(A, PROD(B, C)), _1._1 / (_1._2 * _2));
// ( a / b ) / n = a / ( n * b )
RATIO_RULE(TYPN1T2, RATIO_S(A, B), NUM(n), \
RATIO(A, PROD(NUM(n), B)), _1._1 / (_2 * _1._2));
// ( a / b ) / ( c * d ) = a / ( b * c * d )
RATIO_RULE(TYPT4, RATIO_S(A, B), PROD_S(C, D), \
RATIO(A, PROD(PROD(B, C), D)), _1._1 / (_1._2 * _2));
// ( a * b ) / ( c / d ) = ( a * b * d ) / c
RATIO_RULE(TYPT4, PROD_S(A, B), RATIO_S(C, D), \
RATIO(PROD(PROD(A, B), D), C), (_1 * _2._2) / _2._1);
// ( n * a ) / ( m * b ) = ( n/m ) ( a / b )
RATIO_RULE(TYPN2T2, PROD_S(NUM(n), A), PROD_S(NUM(m), B), \
PROD_S(FRACT(n, m), RATIO(A, B)), \
(PROD_S(FRACT(n, m), RATIO(A, B))((fract<n, m>()), (_1._2 / _2._2))));
// a / ( b / c ) = a * c / b
RATIO_RULE(TYPT3, A, RATIO_S(B, C), \
RATIO(PROD(A, C), B), (_1 * _2._2) / _2._1);
// ( a + b ) / ( c / d ) = ( a + b ) * d / c
RATIO_RULE(TYPT4, SUM_S(A, B), RATIO_S(C, D), \
RATIO(PROD(SUM(A, B), D), C), (_1 * _2._2) / _2._1);
// ( a / b ) / ( c / d )= a * d / ( b * c )
RATIO_RULE(TYPT4, RATIO_S(A, B), RATIO_S(C, D), \
RATIO(PROD(A, D), PROD(B, C)), \
(_1._1 * _2._2) / (_1._2 * _2._1));
// ( a + b ) / ( b + a ) = 1
template<TYPT2,
bool parametric = (Parametric<A>::value == 1) ||
(Parametric<B>::value == 1)>
struct SimplifyRatioSum {
typedef RATIO_S(SUM(A, B), SUM(B, A)) type;
COMBINE(SUM(A, B), SUM(B, A), type(_1, _2));
};
TEMPL(T2) struct SimplifyRatioSum<A, B, false> {
typedef NUM(1) type;
COMBINE(SUM(A, B), SUM(B, A), num<1>());
};
TEMPL(T2) struct Ratio<SUM_S(A, B), SUM_S(B, A)> :
public SimplifyRatioSum<A, B> { };
// a^b / a^c => a^( b - c)
template<TYPT3, bool parametric = (Parametric<A>::value == 1)>
struct SimplifyPowerRatio {
typedef POWER(A, B) arg1;
typedef POWER(A, C) arg2;
typedef RATIO_S(arg1, arg2) type;
COMBINE(arg1, arg2, type(_1, _2));
};
TEMPL(T3)
struct SimplifyPowerRatio<A, B, C, false> {
typedef POWER(A, B) arg1;
typedef POWER(A, C) arg2;
typedef POWER(A, DIFF(B, C)) type;
inline static type combine(const arg1& _1, const arg2& _2) {
return pow(DecomposePower<A, B>::getBase(_1),
(DecomposePower<A, B>::getExp(_1) -
DecomposePower<A, C>::getExp(_2))); }
};
TEMPL(T3) struct Ratio<POWER_S(A, B), POWER_S(A, C)> :
public SimplifyPowerRatio<A, B, C> { };
TEMPL(T2) struct Ratio<POWER_S(A, B), POWER_S(A, B)> :
public SimplifyPowerRatio<A, B, B> { };
TEMPL(T2) struct Ratio<A, POWER_S(A, B)> :
public SimplifyPowerRatio<A, NUM(1), B> { };
TEMPL(N1T1) struct Ratio<A, POWER_S(A, NUM(n))> :
public SimplifyPowerRatio<A, NUM(1), NUM(n)> { };
TEMPL(T2) struct Ratio<POWER_S(A, B), A> :
public SimplifyPowerRatio<A, B, NUM(1)>{ };
TEMPL(N1T1) struct Ratio<POWER_S(A, NUM(n)), A> :
public SimplifyPowerRatio<A, NUM(n), NUM(1)> { };
TEMPL(T1) struct Ratio<A, A> :
public SimplifyPowerRatio<A, NUM(1), NUM(1)> { };
TEMPL(T2) struct Ratio<PROD_S(A, B), PROD_S(A, B)> :
public SimplifyPowerRatio<PROD_S(A, B), NUM(1), NUM(1)> { };
TEMPL(N1T1) struct Ratio<PROD_S(NUM(n), A), PROD_S(NUM(n), A)> :
public SimplifyPowerRatio<PROD_S(NUM(n), A), NUM(1), NUM(1)> { };
RATIO_RULE(TYPN1, NUM(n), NUM(n), NUM(1), num<1>());
// simplify ( f * g ) / h
// try ( f / h ) * g and ( g / h ) * f, otherwise leave ( f * g ) / h
template <typename Prod, bool simplify = Prod::value> struct AuxProductRatio {
typedef PROD(typename Prod::AB, typename Prod::C) type;
inline static type combine(const typename Prod::A& a,
const typename Prod::B& b,
const typename Prod::C& c) { return (a / b) * c; }
};
template<typename Prod> struct AuxProductRatio<Prod, false> {
typedef RATIO_S(typename Prod::AB, typename Prod::C) type;
inline static type combine(const typename Prod::A& a,
const typename Prod::B& b,
const typename Prod::C& c) { return type(a * b, c); }
};
template<typename F, typename G, typename H>
struct RatioP1 {
struct prod0 {
typedef F A; typedef G B; typedef H C;
typedef PROD_S(A, B) AB;
inline static const A& a(const F& f, const G& g, const H& h) { return f; }
inline static const B& b(const F& f, const G& g, const H& h) { return g; }
inline static const C& c(const F& f, const G& g, const H& h) { return h; }
enum { value = false };
};
struct prod1 {
typedef F A; typedef H B; typedef G C;
typedef RATIO_S(A, B) base;
typedef RATIO(A, B) AB;
inline static const A& a(const F& f, const G& g, const H& h) { return f; }
inline static const B& b(const F& f, const G& g, const H& h) { return h; }
inline static const C& c(const F& f, const G& g, const H& h) { return g; }
enum { value = not ::boost::is_same<AB, base>::value };
};
struct prod2 {
typedef G A; typedef H B; typedef F C;
typedef RATIO_S(A, B) base;
typedef RATIO(A, B) AB;
inline static const A& a(const F& f, const G& g, const H& h) { return g; }
inline static const B& b(const F& f, const G& g, const H& h) { return h; }
inline static const C& c(const F& f, const G& g, const H& h) { return f; }
enum { value = not ::boost::is_same<AB, base>::value };
};
typedef typename
::boost::mpl::if_<prod1,
prod1,
typename ::boost::mpl::if_<prod2,
prod2,
prod0
>::type
>::type prod;
typedef typename AuxProductRatio<prod>::type type;
inline static type combine(const PROD_S(F, G)& fg, const H& h) {
const F& f = fg._1;
const G& g = fg._2;
const typename prod::A & a = prod::a(f, g, h);
const typename prod::B & b = prod::b(f, g, h);
const typename prod::C & c = prod::c(f, g, h);
return AuxProductRatio<prod>::combine(a, b, c);
}
};
// simplify c / ( a * b )
// try ( c / a ) / b and ( c / b ) / a, otherwise leave c / ( a * b )
template <typename Prod, bool simplify = Prod::value>
struct AuxProductRatio2 {
typedef RATIO(typename Prod::AB, typename Prod::C) type;
inline static type combine(const typename Prod::A& a,
const typename Prod::B& b,
const typename Prod::C& c) { return (b / a) / c; }
};
template<typename Prod>
struct AuxProductRatio2<Prod, false> {
typedef RATIO_S(typename Prod::C, typename Prod::AB) type;
inline static type combine(const typename Prod::A& a,
const typename Prod::B& b,
const typename Prod::C& c) { return type(c, a * b); }
};
template<typename F, typename G, typename H>
struct RatioP2 {
struct prod0 {
typedef F A; typedef G B; typedef H C;
typedef PROD_S(A, B) AB;
inline static const A& a(const F& f, const G& g, const H& h) { return f; }
inline static const B& b(const F& f, const G& g, const H& h) { return g; }
inline static const C& c(const F& f, const G& g, const H& h) { return h; }
enum { value = false };
};
struct prod1 {
typedef F A; typedef H B; typedef G C;
typedef RATIO_S(B, A) base;
typedef RATIO(B, A) AB;
inline static const A& a(const F& f, const G& g, const H& h) { return f; }
inline static const B& b(const F& f, const G& g, const H& h) { return h; }
inline static const C& c(const F& f, const G& g, const H& h) { return g; }
enum { value = not ::boost::is_same<AB, base>::value };
};
struct prod2 {
typedef G A; typedef H B; typedef F C;
typedef RATIO_S(B, A) base;
typedef RATIO(B, A) AB;
inline static const A& a(const F& f, const G& g, const H& h) { return g; }
inline static const B& b(const F& f, const G& g, const H& h) { return h; }
inline static const C& c(const F& f, const G& g, const H& h) { return f; }
enum { value = not ::boost::is_same<AB, base>::value };
};
typedef typename
::boost::mpl::if_<prod1,
prod1,
typename ::boost::mpl::if_<prod2,
prod2,
prod0
>::type
>::type prod;
typedef typename AuxProductRatio2<prod>::type type;
inline static type combine(const H& h, const PROD_S(F, G)& fg) {
const F& f = fg._1;
const G& g = fg._2;
const typename prod::A & a = prod::a(f, g, h);
const typename prod::B & b = prod::b(f, g, h);
const typename prod::C & c = prod::c(f, g, h);
return AuxProductRatio2<prod>::combine(a, b, c);
}
};
TEMPL(T3) struct Ratio<PROD_S(A, B), C> :
public RatioP1<A, B, C> { };
TEMPL(N1T2) struct Ratio<PROD_S(A, B), NUM(n)> :
public RatioP1<A, B, NUM(n)> { };
TEMPL(T3) struct Ratio<C, PROD_S(A, B)> :
public RatioP2<A, B, C> { };
TEMPL(T4) struct Ratio<PROD_S(C, D), PROD_S(A, B)> :
public RatioP2<A, B, PROD_S(C, D)> { };
// simplify ( a + b ) / c trying to simplify ( a / c ) and ( b / c )
template <TYPT3, bool simplify = false> struct AuxSumRatio {
typedef RATIO_S(SUM_S(A, B), C) type;
COMBINE(SUM_S(A, B), C, type(_1, _2));
};
TEMPL(T3) struct AuxSumRatio<A, B, C, true> {
typedef SUM(RATIO(A, C), RATIO(B, C)) type;
COMBINE(SUM_S(A, B), C, (_1._1 / _2) + (_1._2 / _2));
};
TEMPL(T3) struct RatioSimpl {
struct ratio1 {
typedef RATIO_S(A, C) base;
typedef RATIO(A, C) type;
enum { value = not ::boost::is_same<type, base>::value };
};
struct ratio2 {
typedef RATIO_S(B, C) base;
typedef RATIO(B, C) type;
enum { value = not ::boost::is_same<type, base>::value };
};
typedef AuxSumRatio<A, B, C, ratio1::value or ratio2::value> aux;
typedef typename aux::type type;
COMBINE(SUM_S(A, B), C, aux::combine(_1, _2));
};
TEMPL(T3) struct Ratio<SUM_S(A, B), C> :
public RatioSimpl<A, B, C> { };
TEMPL(T4) struct Ratio<SUM_S(A, B), PROD_S(C, D)> :
public RatioSimpl<A, B, PROD_S(C, D)> { };
TEMPL(N1T2) struct Ratio<SUM_S(A, B), NUM(n)> :
public RatioSimpl<A, B, NUM(n)> { };
}
#include "PhysicsTools/Utilities/interface/Simplify_end.h"
#endif