Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-08-06 09:38:20

0001 // -*- C++ -*-
0002 //
0003 // PhysicalQtyOps.h is a part of ThePEG - Toolkit for HEP Event Generation
0004 // Copyright (C) 2006-2019 David Grellscheid, Leif Lonnblad
0005 //
0006 // ThePEG is licenced under version 3 of the GPL, see COPYING for details.
0007 // Please respect the MCnet academic guidelines, see GUIDELINES for details.
0008 //
0009 #ifndef Physical_Qty_Ops_H
0010 #define Physical_Qty_Ops_H
0011 #include "PhysicalQty.h"
0012 #include <cmath>
0013 
0014 /** @file PhysicalQtyOps.h 
0015  * Overloads for mathematical operations on physical quantities.
0016  */
0017 
0018 namespace ThePEG {
0019 /// @name Overloads for mathematical operations on physical quantities.
0020 //@{
0021 // qty = qty * qty
0022 template<typename T, typename U>
0023 inline constexpr typename BinaryOpTraits<T,U>::MulT
0024 operator*(T q1, U q2) {
0025   typedef typename BinaryOpTraits<T,U>::MulT RetT;
0026   return RetT{RetT::baseunit(), q1.rawValue()*q2.rawValue()};
0027 }
0028 
0029 // qty = qty / qty
0030 template<typename T, typename U>
0031 inline constexpr typename BinaryOpTraits<T,U>::DivT
0032 operator/(T q1, U q2) {
0033   typedef typename BinaryOpTraits<T,U>::DivT RetT;
0034   return RetT{RetT::baseunit(), q1.rawValue()/q2.rawValue()};
0035 }
0036 
0037 // qty = qty + qty
0038 template<typename T, typename U>
0039 inline enable_if_same_qty<T,T,U>
0040 operator+(T q1, U q2) {
0041   q1 += q2;
0042   return q1;
0043 }
0044 
0045 // qty = qty - qty
0046 template<typename T, typename U>
0047 inline enable_if_same_qty<T,T,U>
0048 operator-(T q1, U q2) {
0049   q1 -= q2;
0050   return q1;
0051 }
0052 
0053 // qty == qty
0054 template<typename T, typename U>
0055 inline constexpr enable_if_same_qty<bool,T,U>
0056 operator==(T q1, U q2) {
0057   return q1.rawValue()==q2.rawValue();
0058 }
0059 
0060 // qty != qty
0061 template<typename T, typename U>
0062 inline constexpr enable_if_same_qty<bool,T,U>
0063 operator!=(T q1, U q2) {
0064   return q1.rawValue()!=q2.rawValue();
0065 }
0066 
0067 // qty < qty
0068 template<typename T, typename U>
0069 inline constexpr enable_if_same_qty<bool,T,U>
0070 operator<(T q1, U q2) {
0071   return q1.rawValue()<q2.rawValue();
0072 }
0073 
0074 // qty <= qty
0075 template<typename T, typename U>
0076 inline constexpr enable_if_same_qty<bool,T,U>
0077 operator<=(T q1, U q2) {
0078   return q1.rawValue()<=q2.rawValue();
0079 }
0080 
0081 // qty > qty
0082 template<typename T, typename U>
0083 inline constexpr enable_if_same_qty<bool,T,U>
0084 operator>(T q1, U q2) {
0085   return q1.rawValue()>q2.rawValue();
0086 }
0087 
0088 // qty >= qty
0089 template<typename T, typename U>
0090 inline constexpr enable_if_same_qty<bool,T,U>
0091 operator>=(T q1, U q2) {
0092   return q1.rawValue()>=q2.rawValue();
0093 }
0094 
0095 // comparisons with ZERO
0096 template<typename T>
0097 inline constexpr enable_if_same_qty<bool, T>
0098 operator==(T q1, ZeroUnit) {
0099   return q1.rawValue() == 0.0;
0100 }
0101 template<typename T>
0102 inline constexpr enable_if_same_qty<bool, T>
0103 operator!=(T q1, ZeroUnit) {
0104   return q1.rawValue() != 0.0;
0105 }
0106 template<typename T>
0107 inline constexpr enable_if_same_qty<bool, T>
0108 operator<(T q1, ZeroUnit) {
0109   return q1.rawValue() < 0.0;
0110 }
0111 template<typename T>
0112 inline constexpr enable_if_same_qty<bool, T>
0113 operator>(T q1, ZeroUnit) {
0114   return q1.rawValue() > 0.0;
0115 }
0116 template<typename T>
0117 inline constexpr enable_if_same_qty<bool, T>
0118 operator<=(T q1, ZeroUnit) {
0119   return q1.rawValue() <= 0.0;
0120 }
0121 template<typename T>
0122 inline constexpr enable_if_same_qty<bool, T>
0123 operator>=(T q1, ZeroUnit) {
0124   return q1.rawValue() >= 0.0;
0125 }
0126 
0127 // qty = qty * double
0128 template<typename T>
0129 inline constexpr enable_if_same_qty<T, T>
0130 operator*(T q,double x) {
0131   return T{q,x};
0132 }
0133 
0134 // qty = double * qty
0135 template<typename T>
0136 inline constexpr enable_if_same_qty<T, T>
0137 operator*(double x,T q) {
0138   return T{q,x};
0139 }
0140 
0141 // qty = qty / double
0142 template<typename T>
0143 inline constexpr enable_if_same_qty<T, T>
0144 operator/(T q,double x) {
0145   return T{q, 1./x};
0146 }
0147 
0148 // qty = double / qty
0149 template<typename T>
0150 inline constexpr enable_if_same_qty<typename T::Inverse, T>
0151 operator/(double x, T q) {
0152   typedef typename T::Inverse RetT;
0153   return RetT{RetT::baseunit(), x/q.rawValue()};
0154 }
0155 
0156 // qty = -qty
0157 template<typename T>
0158 inline constexpr enable_if_same_qty<T, T>
0159 operator-(T q) {
0160   typedef T RetT;
0161   return RetT{q, -1.0};
0162 }
0163 
0164 // qty = sqrt(qty) // std::sqrt is not constexpr
0165 template<typename T>
0166 inline enable_if_same_qty<typename T::Sqrt, T>
0167 sqrt(T q) {
0168   typedef typename T::Sqrt RetT;
0169   return RetT{RetT::baseunit(), std::sqrt(q.rawValue())};
0170 }
0171 
0172 // double = atan2(y,x)
0173 template<typename T, typename U>
0174 inline constexpr enable_if_same_qty<double,T,U>
0175 atan2(T y, U x) {
0176   return std::atan2(y.rawValue(), x.rawValue());
0177 }
0178 
0179 // qty = abs(qty)
0180 template<typename T>
0181 inline constexpr enable_if_same_qty<T, T>
0182 abs(T q) {
0183   return T{T::baseunit(), std::abs(q.rawValue())};
0184 }
0185 
0186 // qty = pow<P,R>(qty)
0187 template<long int Num, long int Den, typename T>
0188 inline constexpr enable_if_same_qty<typename T::template Power<Num,Den>, T>
0189 pow(T q) {
0190   typedef typename T::template Power<Num,Den> RetT;
0191   return RetT{RetT::baseunit(), std::pow(q.rawValue(),double(Num)/double(Den))};
0192 }
0193   
0194 // max for T,U types
0195 template<typename T, typename U>
0196 inline T max(const T & t, const U & u) {
0197   const T & utmp = u;
0198   return std::max(t, utmp);
0199 }
0200 
0201 // ZeroUnit in front should take U type
0202 template<typename U>
0203 inline U max(const ZeroUnit & t, const U & u) {
0204   const U & ttmp = t;
0205   return std::max(ttmp, u);
0206 }
0207 
0208 // min for T,U types
0209 template<typename T, typename U>
0210 inline T min(const T & t, const U & u) {
0211   const T & utmp = u;
0212   return std::min(t, utmp);
0213 }
0214 
0215 // ZeroUnit in front should take U type
0216 template<typename U>
0217 inline U min(const ZeroUnit & t, const U & u) {
0218   const U & ttmp = t;
0219   return std::min(ttmp, u);
0220 }
0221 
0222 
0223 //@}
0224 }
0225 
0226 #endif