CMS 3D CMS Logo

SimplifySum.h
Go to the documentation of this file.
1 #ifndef PhysicsTools_Utilities_SimplifySum_h
2 #define PhysicsTools_Utilities_SimplifySum_h
3 
10 #include <type_traits>
11 
13 
14 namespace funct {
15 
16  // A + ( B + C ) => ( A + B ) + C
17  SUM_RULE(TYPT3, A, SUM_S(B, C), SUM(SUM(A, B), C), (_1 + _2._1) + _2._2);
18 
19  // ( A + B ) + ( C + D ) => ( ( A + B ) + C ) + D
20  SUM_RULE(TYPT4, SUM_S(A, B), SUM_S(C, D), SUM(SUM(SUM(A, B), C), D), (_1 + _2._1) + _2._2);
21 
22  // n + A = A + n
23  SUM_RULE(TYPN1T1, NUM(n), A, SUM(A, NUM(n)), _2 + _1);
24 
25  // n + ( A + B )= ( A + B ) + n
26  SUM_RULE(TYPN1T2, NUM(n), SUM_S(A, B), SUM(SUM_S(A, B), NUM(n)), _2 + _1);
27 
28  // A + 0 = A
29  SUM_RULE(TYPT1, A, NUM(0), A, _1);
30 
31  // 0 + 0 = 0
32  SUM_RULE(TYP0, NUM(0), NUM(0), NUM(0), num<0>());
33 
34  // ( A * B ) + 0 = ( A * B )
35  SUM_RULE(TYPT2, PROD_S(A, B), NUM(0), PROD_S(A, B), _1);
36 
37  // 0 + ( A * B ) = ( A * B )
38  SUM_RULE(TYPT2, NUM(0), PROD_S(A, B), PROD_S(A, B), _2);
39 
40  // 0 - ( A * B ) = - ( A * B )
42 
43  // ( A + B ) + 0 = ( A + B )
44  SUM_RULE(TYPT2, SUM_S(A, B), NUM(0), SUM_S(A, B), _1);
45 
46  // 0 + ( A + B ) = ( A + B )
47  SUM_RULE(TYPT2, NUM(0), SUM_S(A, B), SUM_S(A, B), _2);
48 
49  // A - ( -B ) = A + B
50  DIFF_RULE(TYPT2, A, MINUS_S(B), SUM(A, B), _1 + _2._);
51 
52  // n * A + m * A => ( n + m ) * A
55  typedef PROD(NUM(n), A) arg1;
56  typedef PROD(NUM(m), A) arg2;
57  typedef SUM_S(arg1, arg2) type;
58  COMBINE(arg1, arg2, type(_1, _2));
59  };
60 
61  TEMPL(N2T1)
63  typedef PROD(NUM(n + m), A) type;
64  typedef DecomposeProduct<PROD(NUM(n), A), A> Dec;
65  COMBINE(PROD(NUM(n), A), PROD(NUM(m), A), num<n + m>() * Dec::get(_1));
66  };
67 
68  TEMPL(T1)
69  struct ParametricSimplifiedSum<1, 1, A, true> {
71  COMBINE(A, A, type(_1, _2));
72  };
73 
74  TEMPL(T1)
75  struct ParametricSimplifiedSum<1, 1, A, false> {
76  typedef PROD(NUM(2), A) type;
77  COMBINE(A, A, num<2>() * _1);
78  };
79 
80  TEMPL(N2T1)
81  struct Sum<PROD_S(NUM(n), A), PROD_S(NUM(m), A)> : public ParametricSimplifiedSum<n, m, A> {};
82 
83  TEMPL(N1T1)
84  struct Sum<A, PROD_S(NUM(n), A)> : public ParametricSimplifiedSum<1, n, A> {};
85 
86  TEMPL(N1T1)
87  struct Sum<PROD_S(NUM(n), A), A> : public ParametricSimplifiedSum<n, 1, A> {};
88 
89  TEMPL(T1)
90  struct Sum<A, A> : public ParametricSimplifiedSum<1, 1, A> {};
91 
92  TEMPL(T1)
93  struct Sum<MINUS_S(A), MINUS_S(A)> : public ParametricSimplifiedSum<1, 1, MINUS_S(A)> {};
94 
95  TEMPL(T2)
96  struct Sum<MINUS_S(PROD_S(A, B)), MINUS_S(PROD_S(A, B))>
97  : public ParametricSimplifiedSum<1, 1, MINUS_S(PROD_S(A, B))> {};
98 
99  TEMPL(N1)
100  struct Sum<NUM(n), NUM(n)> : public ParametricSimplifiedSum<1, 1, NUM(n)> {};
101 
102  TEMPL(T2)
103  struct Sum<PROD_S(A, B), PROD_S(A, B)> : public ParametricSimplifiedSum<1, 1, PROD_S(A, B)> {};
104 
105  TEMPL(N1T1)
106  struct Sum<PROD_S(NUM(n), A), PROD_S(NUM(n), A)> : public ParametricSimplifiedSum<1, 1, PROD_S(NUM(n), A)> {};
107 
108  // simplify f + g + h regardless of the order
109  template <typename Prod, bool simplify = Prod::value>
110  struct AuxSum {
111  typedef SUM(typename Prod::AB, typename Prod::C) type;
112  COMBINE(typename Prod::AB, typename Prod::C, _1 + _2);
113  };
114 
115  template <typename Prod>
116  struct AuxSum<Prod, false> {
117  typedef SUM_S(typename Prod::AB, typename Prod::C) type;
118  COMBINE(typename Prod::AB, typename Prod::C, type(_1, _2));
119  };
120 
121  template <typename F, typename G, typename H>
122  struct SimplSumOrd {
123  struct prod0 {
124  typedef F A;
125  typedef G B;
126  typedef H C;
127  typedef SUM_S(A, B) AB;
128  inline static const A& a(const F& f, const G& g, const H& h) { return f; }
129  inline static const B& b(const F& f, const G& g, const H& h) { return g; }
130  inline static const C& c(const F& f, const G& g, const H& h) { return h; }
131  enum { value = false };
132  };
133  struct prod1 {
134  typedef F A;
135  typedef H B;
136  typedef G C;
137  typedef SUM_S(A, B) base;
138  typedef SUM(A, B) AB;
139  inline static const A& a(const F& f, const G& g, const H& h) { return f; }
140  inline static const B& b(const F& f, const G& g, const H& h) { return h; }
141  inline static const C& c(const F& f, const G& g, const H& h) { return g; }
143  };
144  struct prod2 {
145  typedef G A;
146  typedef H B;
147  typedef F C;
148  typedef SUM_S(A, B) base;
149  typedef SUM(A, B) AB;
150  inline static const A& a(const F& f, const G& g, const H& h) { return g; }
151  inline static const B& b(const F& f, const G& g, const H& h) { return h; }
152  inline static const C& c(const F& f, const G& g, const H& h) { return f; }
154  };
155 
156  typedef
159  typedef typename AuxSum<prod>::type type;
160  inline static type combine(const SUM_S(F, G) & fg, const H& h) {
161  const F& f = fg._1;
162  const G& g = fg._2;
163  const typename prod::A& a = prod::a(f, g, h);
164  const typename prod::B& b = prod::b(f, g, h);
165  const typename prod::C& c = prod::c(f, g, h);
166  return AuxSum<prod>::combine(a + b, c);
167  }
168  };
169 
170  TEMPL(T3)
171  struct Sum<SUM_S(A, B), C> : public SimplSumOrd<A, B, C> {};
172 
173  TEMPL(T4)
174  struct Sum<SUM_S(A, B), PROD_S(C, D)> : public SimplSumOrd<A, B, PROD_S(C, D)> {};
175 
176 } // namespace funct
177 
179 
180 #endif
funct::AuxSum::combine
static type combine(const typename Prod::AB &_1, const typename Prod::C &_2)
Definition: SimplifySum.h:112
funct::DecomposeProduct
Definition: DecomposeProduct.h:8
SUM
#define SUM(A, B)
Definition: Simplify_begin.h:52
funct::SimplSumOrd::type
AuxSum< prod >::type type
Definition: SimplifySum.h:159
Product.h
TYPN1T1
#define TYPN1T1
Definition: Simplify_begin.h:16
TYP0
#define TYP0
Definition: Simplify_begin.h:4
funct::false
false
Definition: Factorize.h:29
dqmiodumpmetadata.n
n
Definition: dqmiodumpmetadata.py:28
funct::SimplSumOrd::prod2::c
static const C & c(const F &f, const G &g, const H &h)
Definition: SimplifySum.h:152
funct::SimplSumOrd::prod0::B
G B
Definition: SimplifySum.h:125
funct::AuxSum::type
Sum< typename Prod::AB, typename Prod::C >::type type
Definition: SimplifySum.h:111
funct::PROD_S
PROD_S(B, C)>
Definition: Factorize.h:114
f
double f[11][100]
Definition: MuScleFitUtils.cc:78
funct::SimplSumOrd::prod1::A
F A
Definition: SimplifySum.h:134
funct::B
TEMPL(T2) struct Divides B
Definition: Factorize.h:24
funct::AuxSum< Prod, false >::type
SumStruct< typename Prod::AB, typename Prod::C > type
Definition: SimplifySum.h:117
TYPT4
#define TYPT4
Definition: Simplify_begin.h:9
funct::SimplSumOrd::prod2::base
SumStruct< A, B > base
Definition: SimplifySum.h:148
funct::SimplSumOrd::prod2::AB
Sum< A, B >::type AB
Definition: SimplifySum.h:149
funct::SimplSumOrd::prod2::A
G A
Definition: SimplifySum.h:145
funct::ProductStruct
Definition: Product.h:6
funct::C
C
Definition: Factorize.h:133
funct::SimplSumOrd::prod1::b
static const B & b(const F &f, const G &g, const H &h)
Definition: SimplifySum.h:140
funct::SimplSumOrd::prod2::C
F C
Definition: SimplifySum.h:147
h
The Signals That Services Can Subscribe To This is based on ActivityRegistry h
Helper function to determine trigger accepts.
Definition: Activities.doc:4
funct::SimplSumOrd::prod2::B
H B
Definition: SimplifySum.h:146
TYPT1
#define TYPT1
Definition: Simplify_begin.h:6
funct::ParametricSimplifiedSum::type
SumStruct< arg1, arg2 > type
Definition: SimplifySum.h:57
funct::SimplSumOrd::prod0
Definition: SimplifySum.h:123
F
static uInt32 F(BLOWFISH_CTX *ctx, uInt32 x)
Definition: blowfish.cc:163
funct::SimplSumOrd::prod0::c
static const C & c(const F &f, const G &g, const H &h)
Definition: SimplifySum.h:130
TYPT2
#define TYPT2
Definition: Simplify_begin.h:7
Difference.h
funct::SimplSumOrd::prod1::B
H B
Definition: SimplifySum.h:135
Numerical.h
funct::ParametricSimplifiedSum< 1, 1, A, false >::type
Product< Numerical< 2 >, A >::type type
Definition: SimplifySum.h:76
funct::SimplSumOrd::prod1::base
SumStruct< A, B > base
Definition: SimplifySum.h:137
funct::SimplSumOrd
Definition: SimplifySum.h:122
funct::AuxSum
Definition: SimplifySum.h:110
funct::SimplSumOrd::prod1::AB
Sum< A, B >::type AB
Definition: SimplifySum.h:138
funct::m
m
Definition: Factorize.h:45
SUM_S
#define SUM_S(A, B)
Definition: Simplify_begin.h:37
h
b
double b
Definition: hdecay.h:118
funct::SimplSumOrd::prod0::C
H C
Definition: SimplifySum.h:126
funct::SimplSumOrd::prod1::a
static const A & a(const F &f, const G &g, const H &h)
Definition: SimplifySum.h:139
Sum.h
funct::true
true
Definition: Factorize.h:173
funct::ParametricSimplifiedSum< n, m, A, false >::Dec
DecomposeProduct< typename Product< Numerical< n >, A >::type, A > Dec
Definition: SimplifySum.h:64
ParametricTrait.h
funct::SimplSumOrd::combine
static type combine(const SumStruct< F, G > &fg, const H &h)
Definition: SimplifySum.h:160
a
double a
Definition: hdecay.h:119
funct::SimplSumOrd::prod0::b
static const B & b(const F &f, const G &g, const H &h)
Definition: SimplifySum.h:129
gainCalibHelper::gainCalibPI::type
type
Definition: SiPixelGainCalibHelper.h:40
funct::MINUS_S
typedef MINUS_S(A) arg
A
DecomposeProduct.h
funct::SimplSumOrd::prod0::a
static const A & a(const F &f, const G &g, const H &h)
Definition: SimplifySum.h:128
funct::type
arg type
Definition: Factorize.h:32
value
Definition: value.py:1
funct::TEMPL
TEMPL(T1) struct Divides0
Definition: Factorize.h:15
funct::ParametricSimplifiedSum::arg2
Product< Numerical< m >, A >::type arg2
Definition: SimplifySum.h:56
funct::SimplSumOrd::prod1::c
static const C & c(const F &f, const G &g, const H &h)
Definition: SimplifySum.h:141
get
#define get
funct::ParametricSimplifiedSum< 1, 1, A, true >::type
SumStruct< A, A > type
Definition: SimplifySum.h:70
funct::SimplSumOrd::prod0::AB
SumStruct< A, B > AB
Definition: SimplifySum.h:127
TYPN1T2
#define TYPN1T2
Definition: Simplify_begin.h:17
Simplify_end.h
SUM_RULE
#define SUM_RULE(TMPL, T1, T2, RES, COMB)
Definition: Simplify_begin.h:81
funct::SimplSumOrd::prod2
Definition: SimplifySum.h:144
TtFullHadDaughter::B
static const std::string B
Definition: TtFullHadronicEvent.h:9
MaterialEffects_cfi.A
A
Definition: MaterialEffects_cfi.py:11
funct::SimplSumOrd::prod0::A
F A
Definition: SimplifySum.h:124
funct::ParametricSimplifiedSum::arg1
Product< Numerical< n >, A >::type arg1
Definition: SimplifySum.h:55
gen::C
C
Definition: PomwigHadronizer.cc:78
relativeConstraints.value
value
Definition: relativeConstraints.py:53
funct::NUM
PROD_S(A, B)> NUM(n))
Definition: Factorize.h:87
COMBINE
#define COMBINE(A, B, RES)
Definition: Simplify_begin.h:70
TYPT3
#define TYPT3
Definition: Simplify_begin.h:8
data-class-funcs.H
H
Definition: data-class-funcs.py:33
funct::SumStruct
Definition: Sum.h:7
funct::Sum
Definition: Sum.h:18
Simplify_begin.h
DIFF_RULE
#define DIFF_RULE(TMPL, T1, T2, RES, COMB)
Definition: Simplify_begin.h:88
funct::SimplSumOrd::prod2::a
static const A & a(const F &f, const G &g, const H &h)
Definition: SimplifySum.h:150
c
auto & c
Definition: CAHitNtupletGeneratorKernelsImpl.h:56
funct::ParametricSimplifiedSum< n, m, A, false >::type
Product< Numerical< n+m >, A >::type type
Definition: SimplifySum.h:63
funct::PROD
typedef PROD(F, SUM(RATIO(A, F), RATIO(B, F))) type
cmssw_cycle_finder.G
G
Definition: cmssw_cycle_finder.py:154
funct::SimplSumOrd::prod2::b
static const B & b(const F &f, const G &g, const H &h)
Definition: SimplifySum.h:151
funct::SimplSumOrd::prod1::C
G C
Definition: SimplifySum.h:136
funct
Definition: Abs.h:5
funct::SimplSumOrd::prod1
Definition: SimplifySum.h:133
funct::ParametricSimplifiedSum
Definition: SimplifySum.h:54
funct::SimplSumOrd::prod
std::conditional< prod1::value, prod1, typename std::conditional< prod2::value, prod2, prod0 >::type >::type prod
Definition: SimplifySum.h:158
g
The Signals That Services Can Subscribe To This is based on ActivityRegistry and is current per Services can connect to the signals distributed by the ActivityRegistry in order to monitor the activity of the application Each possible callback has some defined which we here list in angle e g
Definition: Activities.doc:4