File indexing completed on 2025-04-19 09:10:06
0001 #ifndef METOOLS_Explicit_C_Scalar_H
0002 #define METOOLS_Explicit_C_Scalar_H
0003
0004 #include "METOOLS/Explicit/C_Object.H"
0005 #include "ATOOLS/Math/MathTools.H"
0006 #include "ATOOLS/Org/STL_Tools.H"
0007
0008 #include <vector>
0009 #include <iostream>
0010
0011 namespace METOOLS {
0012
0013
0014 template <class Scalar>
0015 class CScalar: public CObject {
0016 public:
0017
0018 typedef std::complex<Scalar> SComplex;
0019
0020 private:
0021
0022 SComplex m_x;
0023
0024
0025
0026 static double s_accu;
0027
0028
0029
0030
0031
0032 static ATOOLS::AutoDelete_Vector<CScalar> s_objects;
0033
0034 template <class _Scalar> friend std::ostream &
0035 operator<<(std::ostream &,const CScalar<_Scalar> &);
0036
0037 public:
0038
0039
0040
0041
0042 static CScalar *New();
0043 static CScalar *New(const CScalar &s);
0044
0045
0046 CObject* Copy() const;
0047
0048
0049
0050
0051
0052 void Delete();
0053
0054
0055 bool IsZero() const;
0056
0057
0058
0059
0060
0061
0062
0063 inline CScalar(): m_x(Scalar(0.0))
0064 {
0065 m_h=m_s=0; m_c[0]=m_c[1]=0;
0066 }
0067
0068 inline CScalar(const CScalar &s): m_x(s.m_x)
0069 {
0070 m_h=s.m_h; m_s=s.m_s; m_c[0]=s.m_c[0]; m_c[1]=s.m_c[1];
0071 }
0072
0073
0074
0075
0076
0077
0078
0079 inline CScalar(const Scalar &x,
0080 const int cr=0,const int ca=0,
0081 const size_t &h=0,const size_t &s=0): m_x(x)
0082 {
0083 m_h=h; m_s=s; m_c[0]=cr; m_c[1]=ca;
0084 }
0085
0086
0087
0088
0089
0090
0091
0092 inline CScalar(const SComplex &x,
0093 const int cr=0,const int ca=0,
0094 const size_t &h=0,const size_t &s=0): m_x(x)
0095 {
0096 m_h=h; m_s=s; m_c[0]=cr; m_c[1]=ca;
0097 }
0098
0099
0100
0101
0102
0103
0104 inline CScalar(const CScalar &s,const SComplex &c): m_x(s.m_x*c)
0105 {
0106 m_h=s.m_h; m_s=s.m_s; m_c[0]=s.m_c[0]; m_c[1]=s.m_c[1];
0107 }
0108
0109
0110
0111
0112
0113
0114
0115
0116
0117 void Add(const CObject *c);
0118 void Divide(const double &d);
0119 void Multiply(const Complex &c);
0120 void Invert();
0121
0122
0123
0124
0125
0126 inline SComplex &operator[](const int i) { return m_x; }
0127 inline const SComplex &operator[](const int i) const { return m_x; }
0128
0129
0130
0131
0132
0133 inline CScalar operator+(const CScalar &s) const
0134 {
0135 return CScalar(m_x+s.m_x,m_c[0],m_c[1],m_h,m_s);
0136 }
0137 inline CScalar operator-(const CScalar &s) const
0138 {
0139 return CScalar(m_x-s.m_x,m_c[0],m_c[1],m_h,m_s);
0140 }
0141 inline CScalar operator-() const
0142 {
0143 return CScalar(-m_x,m_c[0],m_c[1],m_h,m_s);
0144 }
0145
0146 inline CScalar& operator+=(const CScalar &s)
0147 {
0148 m_x+=s.m_x;
0149 return *this;
0150 }
0151 inline CScalar& operator-=(const CScalar &s)
0152 {
0153 m_x-=s.m_x;
0154 return *this;
0155 }
0156 inline CScalar& operator*=(const SComplex &c)
0157 {
0158 m_x*=c;
0159 return *this;
0160 }
0161
0162
0163
0164 inline CScalar Conj() const
0165 {
0166 return CScalar(std::conj(m_x),m_c[0],m_c[1],m_h,m_s);
0167 }
0168
0169 inline SComplex Abs2() const
0170 {
0171 return m_x*m_x;
0172 }
0173
0174 inline SComplex Abs() const
0175 {
0176 return sqrt(Abs2());
0177 }
0178
0179
0180 inline bool Nan() const { return ATOOLS::IsNan<SComplex>(m_x); }
0181
0182
0183
0184
0185
0186
0187 static void ResetAccu();
0188 inline static void SetAccu(const Scalar &accu) { s_accu=accu; }
0189 inline static Scalar Accu() { return s_accu; }
0190
0191
0192 };
0193
0194 template <class Scalar> inline std::complex<Scalar>
0195 operator*(const Scalar &d,const CScalar<Scalar> &s)
0196 { return CScalar<Scalar>(s,d); }
0197 template <class Scalar> inline CScalar<Scalar>
0198 operator*(const CScalar<Scalar> &s,const Scalar &d)
0199 { return CScalar<Scalar>(s,d); }
0200 template <class Scalar> inline CScalar<Scalar>
0201 operator*(const std::complex<Scalar> &c,const CScalar<Scalar> &s)
0202 { return CScalar<Scalar>(s,c); }
0203 template <class Scalar> inline CScalar<Scalar>
0204 operator*(const CScalar<Scalar> &s,const std::complex<Scalar> &c)
0205 { return CScalar<Scalar>(s,c); }
0206 template <class Scalar> inline CScalar<Scalar>
0207 operator/(const CScalar<Scalar> &s,const std::complex<Scalar> &c)
0208 { return CScalar<Scalar>(s,1.0/c); }
0209
0210 template <class Scalar> inline std::complex<Scalar>
0211 operator*(const CScalar<Scalar> &s1,const CScalar<Scalar> &s2)
0212 { return s1[0]*s2[0]; }
0213
0214 template <class Scalar>
0215 std::ostream &operator<<(std::ostream &str,const CScalar<Scalar> &s);
0216
0217 }
0218
0219 #define DCScalar METOOLS::CScalar<double>
0220 #define QCScalar METOOLS::CScalar<long double>
0221
0222 #endif