File indexing completed on 2024-04-06 12:24:18
0001 #ifndef PhysicsTools_Utilities_interface_FunctionsIO_h
0002 #define PhysicsTools_Utilities_interface_FunctionsIO_h
0003 #include "PhysicsTools/Utilities/interface/Variables.h"
0004 #include "PhysicsTools/Utilities/interface/Numerical.h"
0005 #include "PhysicsTools/Utilities/interface/Fraction.h"
0006 #include "PhysicsTools/Utilities/interface/Functions.h"
0007 #include "PhysicsTools/Utilities/interface/Operations.h"
0008 #include <ostream>
0009
0010 namespace funct {
0011
0012 template <int n>
0013 std::ostream& operator<<(std::ostream& cout, const Numerical<n>&) {
0014 return cout << n;
0015 }
0016
0017 template <int n, int m>
0018 std::ostream& operator<<(std::ostream& cout, const funct::FractionStruct<n, m>&) {
0019 return cout << n << "/" << m;
0020 }
0021
0022 template <int n, int m>
0023 std::ostream& operator<<(std::ostream& cout, const funct::MinusStruct<funct::FractionStruct<n, m> >&) {
0024 return cout << "-" << n << "/" << m;
0025 }
0026
0027 #define PRINT_FUNCTION(FUN, NAME) \
0028 template <typename T> \
0029 std::ostream& operator<<(std::ostream& cout, const funct::FUN<T>& f) { \
0030 return cout << NAME << "(" << f._ << ")"; \
0031 } \
0032 \
0033 struct __useless_ignoreme
0034
0035 PRINT_FUNCTION(SqrtStruct, "sqrt");
0036 PRINT_FUNCTION(ExpStruct, "exp");
0037 PRINT_FUNCTION(LogStruct, "log");
0038 PRINT_FUNCTION(SinStruct, "sin");
0039 PRINT_FUNCTION(CosStruct, "cos");
0040 PRINT_FUNCTION(TanStruct, "tan");
0041 PRINT_FUNCTION(SgnStruct, "sgn");
0042 PRINT_FUNCTION(AbsStruct, "abs");
0043
0044 #undef PRINT_FUNCTION
0045
0046 #define PRINT_BINARY_OPERATOR(TMPL, OP) \
0047 template <typename A, typename B> \
0048 std::ostream& operator<<(std::ostream& cout, const funct::TMPL<A, B>& f) { \
0049 return cout << f._1 << OP << f._2; \
0050 } \
0051 \
0052 struct __useless_ignoreme
0053
0054 #define PRINT_UNARY_OPERATOR(TMPL, OP) \
0055 template <typename A> \
0056 std::ostream& operator<<(std::ostream& cout, const funct::TMPL<A>& f) { \
0057 return cout << OP << f._; \
0058 } \
0059 \
0060 struct __useless_ignoreme
0061
0062 PRINT_BINARY_OPERATOR(SumStruct, " + ");
0063 PRINT_BINARY_OPERATOR(ProductStruct, " ");
0064 PRINT_BINARY_OPERATOR(RatioStruct, "/");
0065 PRINT_BINARY_OPERATOR(PowerStruct, "^");
0066 PRINT_UNARY_OPERATOR(MinusStruct, "-");
0067
0068 #undef PRINT_BINARY_OPERATOR
0069 #undef PRINT_UNARY_OPERATOR
0070
0071 template <typename A, typename B>
0072 std::ostream& operator<<(std::ostream& cout, const funct::SumStruct<A, funct::MinusStruct<B> >& f) {
0073 return cout << f._1 << " - " << f._2._;
0074 }
0075
0076 template <typename A, typename B>
0077 std::ostream& operator<<(std::ostream& cout,
0078 const funct::SumStruct<funct::MinusStruct<A>, funct::MinusStruct<B> >& f) {
0079 return cout << "- " << f._1._ << " - " << f._2._;
0080 }
0081
0082 template <typename A, typename B>
0083 std::ostream& operator<<(std::ostream& cout, const funct::SumStruct<funct::MinusStruct<A>, B>& f) {
0084 return cout << "- " << f._1._ << " + " << f._2;
0085 }
0086
0087 template <typename A, int n>
0088 std::ostream& operator<<(std::ostream& cout, const funct::SumStruct<A, funct::Numerical<n> >& f) {
0089 return cout << f._1 << (n >= 0 ? " + " : " - ") << ::abs(n);
0090 }
0091
0092 template <typename A, int n>
0093 std::ostream& operator<<(std::ostream& cout, const funct::SumStruct<funct::MinusStruct<A>, funct::Numerical<n> >& f) {
0094 return cout << "- " << f._1._ << (n >= 0 ? " + " : " - ") << ::abs(n);
0095 }
0096
0097 #define PARENTHESES(TMPL1, TMPL2, OP) \
0098 template <typename A, typename B, typename C> \
0099 std::ostream& operator<<(std::ostream& cout, const funct::TMPL1<funct::TMPL2<A, B>, C>& f) { \
0100 return cout << "( " << f._1 << " )" << OP << f._2; \
0101 } \
0102 \
0103 template <typename A, typename B, typename C> \
0104 std::ostream& operator<<(std::ostream& cout, const funct::TMPL1<C, funct::TMPL2<A, B> >& f) { \
0105 return cout << f._1 << OP << "( " << f._2 << " )"; \
0106 } \
0107 \
0108 template <typename A, typename B, typename C, typename D> \
0109 std::ostream& operator<<(std::ostream& cout, const funct::TMPL1<funct::TMPL2<A, B>, funct::TMPL2<C, D> >& f) { \
0110 return cout << "( " << f._1 << " )" << OP << "( " << f._2 << " )"; \
0111 } \
0112 \
0113 struct __useless_ignoreme
0114
0115 #define PARENTHESES_FRACT(TMPL, OP) \
0116 template <int n, int m, typename A> \
0117 std::ostream& operator<<(std::ostream& cout, const funct::TMPL<funct::FractionStruct<n, m>, A>& f) { \
0118 return cout << "( " << f._1 << " )" << OP << f._2; \
0119 } \
0120 \
0121 template <int n, int m, typename A> \
0122 std::ostream& operator<<(std::ostream& cout, const funct::TMPL<A, funct::FractionStruct<n, m> >& f) { \
0123 return cout << f._1 << OP << "( " << f._2 << " )"; \
0124 } \
0125 \
0126 template <int n, int m, int k, int l> \
0127 std::ostream& operator<<(std::ostream& cout, \
0128 const funct::TMPL<funct::FractionStruct<n, m>, funct::FractionStruct<k, l> >& f) { \
0129 return cout << "( " << f._1 << " )" << OP << "( " << f._2 << " )"; \
0130 } \
0131 \
0132 template <int n, int m, typename A> \
0133 std::ostream& operator<<(std::ostream& cout, \
0134 const funct::TMPL<funct::MinusStruct<funct::FractionStruct<n, m> >, A>& f) { \
0135 return cout << "( " << f._1 << " )" << OP << f._2; \
0136 } \
0137 \
0138 template <int n, int m, typename A> \
0139 std::ostream& operator<<(std::ostream& cout, \
0140 const funct::TMPL<A, funct::MinusStruct<funct::FractionStruct<n, m> > >& f) { \
0141 return cout << f._1 << OP << "( " << f._2 << " )"; \
0142 } \
0143 struct __useless_ignoreme
0144
0145 #define PARENTHESES_1(TMPL1, TMPL2, OP) \
0146 template <typename A, typename B> \
0147 std::ostream& operator<<(std::ostream& cout, const funct::TMPL1<funct::TMPL2<A, B> >& f) { \
0148 return cout << OP << "( " << f._ << " )"; \
0149 } \
0150 struct __useless_ignoreme
0151
0152 PARENTHESES(ProductStruct, SumStruct, " ");
0153 PARENTHESES(ProductStruct, RatioStruct, " ");
0154 PARENTHESES(RatioStruct, SumStruct, "/");
0155 PARENTHESES(RatioStruct, ProductStruct, "/");
0156 PARENTHESES(RatioStruct, RatioStruct, "/");
0157
0158 PARENTHESES(PowerStruct, SumStruct, "^");
0159 PARENTHESES(PowerStruct, ProductStruct, "^");
0160 PARENTHESES(PowerStruct, RatioStruct, "^");
0161
0162
0163 PARENTHESES_FRACT(RatioStruct, "/");
0164 PARENTHESES_FRACT(PowerStruct, "^");
0165
0166 PARENTHESES_1(MinusStruct, SumStruct, "-");
0167
0168
0169 #undef PARENTHESES
0170 #undef PARENTHESES_FRACT
0171 #undef PARENTHESES_1
0172
0173 }
0174
0175 #endif