ROOT   Reference Guide
Searching...
No Matches
TComplex.h
Go to the documentation of this file.
1// @(#)root/mathcore:$Id$
2// Author: Federico Carminati 22/04/2004
3
4/*************************************************************************
7 * *
8 * For the licensing terms see $ROOTSYS/LICENSE. * 9 * For the list of contributors see$ROOTSYS/README/CREDITS. *
10 *************************************************************************/
11
12#ifndef ROOT_TComplex
13#define ROOT_TComplex
14
15//////////////////////////////////////////////////////////////////////////
16// //
17// TComplex //
18// //
19//////////////////////////////////////////////////////////////////////////
20
21#include "TMath.h"
22
23#include "Rtypes.h"
24#include <string>
25
26
27class TComplex {
28
29protected:
30 Double_t fRe; // real part
31 Double_t fIm; // imaginary part
32
33public:
34 // ctors and dtors
35 TComplex(): fRe(0), fIm(0) {}
36 TComplex(Double_t re, Double_t im=0, Bool_t polar=kFALSE);
37 virtual ~TComplex() {}
38
39 // constants
40 static TComplex I() {return TComplex(0,1);}
41 static TComplex One() {return TComplex(1,0);}
42
43 // getters and setters
44 Double_t Re() const {return fRe;}
45 Double_t Im() const {return fIm;}
46 Double_t Rho() const {return TMath::Sqrt(fRe*fRe+fIm*fIm);}
47 Double_t Rho2() const {return fRe*fRe+fIm*fIm;}
48 Double_t Theta() const {return (fIm||fRe)?TMath::ATan2(fIm,fRe):0;}
50 { if (polar) { fRe = x*TMath::Cos(y); fIm = x*TMath::Sin(y); }
51 else { fRe = x; fIm = y; } return *this; }
52
53 // Simple operators complex - complex
55 {return TComplex(fRe*c.fRe-fIm*c.fIm,fRe*c.fIm+fIm*c.fRe);}
57 {return TComplex(fRe+c.fRe, fIm+c.fIm);}
59 {return TComplex(fRe*c.fRe+fIm*c.fIm,-fRe*c.fIm+fIm*c.fRe)/c.Rho2();}
61 {return TComplex(fRe-c.fRe, fIm-c.fIm);}
62
64 {return ((*this) = (*this) * c);}
66 {return ((*this) = (*this) + c);}
68 {return ((*this) = (*this) / c);}
70 {return ((*this) = (*this) - c);}
71
73 {return TComplex(-fRe,-fIm);}
75 {return *this;}
76
77 // Simple operators complex - double
79 {return TComplex(fRe*c,fIm*c);}
81 {return TComplex(fRe+c, fIm);}
83 {return TComplex(fRe/c,fIm/c);}
85 {return TComplex(fRe-c, fIm);}
86
87 // Simple operators double - complex
89 {return TComplex(d*c.fRe,d*c.fIm);}
91 {return TComplex(d+c.fRe, c.fIm);}
93 {return TComplex(d*c.fRe,-d*c.fIm)/c.Rho2();}
95 {return TComplex(d-c.fRe, -c.fIm);}
96
97 // Convertors
98 operator Double_t () const {return fRe;}
99 operator Float_t () const {return static_cast<Float_t>(fRe);}
100 operator Int_t () const {return static_cast<Int_t>(fRe);}
101
102 // TMath:: extensions
103 static TComplex Sqrt(const TComplex &c)
104 {return TComplex(TMath::Sqrt(c.Rho()),0.5*c.Theta(),kTRUE);}
105
106 static TComplex Exp(const TComplex &c)
107 {return TComplex(TMath::Exp(c.fRe),c.fIm,kTRUE);}
108 static TComplex Log(const TComplex &c)
109 {return TComplex(0.5*TMath::Log(c.Rho2()),c.Theta());}
110 static TComplex Log2(const TComplex &c)
111 {return Log(c)/TMath::Log(2);}
112 static TComplex Log10(const TComplex &c)
113 {return Log(c)/TMath::Log(10);}
114
115 static TComplex Sin(const TComplex &c)
116 {return TComplex(TMath::Sin(c.fRe)*TMath::CosH(c.fIm),
117 TMath::Cos(c.fRe)*TMath::SinH(c.fIm));}
118 static TComplex Cos(const TComplex &c)
119 {return TComplex(TMath::Cos(c.fRe)*TMath::CosH(c.fIm),
120 -TMath::Sin(c.fRe)*TMath::SinH(c.fIm));}
121 static TComplex Tan(const TComplex &c)
122 {TComplex cc=Cos(c); return Sin(c)*Conjugate(cc)/cc.Rho2();}
123
124 static TComplex ASin(const TComplex &c)
125 {return -I()*Log(I()*c+TMath::Sign(1.,c.Im())*Sqrt(1.-c*c));}
126 static TComplex ACos(const TComplex &c)
127 {return -I()*Log(c+TMath::Sign(1.,c.Im())*Sqrt(c*c-1.));}
128 static TComplex ATan(const TComplex &c)
129 {return -0.5*I()*Log((1.+I()*c)/(1.-I()*c));}
130
131 static TComplex SinH(const TComplex &c)
132 {return TComplex(TMath::SinH(c.fRe)*TMath::Cos(c.fIm),
133 TMath::CosH(c.fRe)*TMath::Sin(c.fIm));}
134 static TComplex CosH(const TComplex &c)
135 {return TComplex(TMath::CosH(c.fRe)*TMath::Cos(c.fIm),
136 TMath::SinH(c.fRe)*TMath::Sin(c.fIm));}
137 static TComplex TanH(const TComplex &c)
138 {TComplex cc=CosH(c); return SinH(c)*Conjugate(cc)/cc.Rho2();}
139
140 static TComplex ASinH(const TComplex &c)
141 {return Log(c+TMath::Sign(1.,c.Im())*Sqrt(c*c+1.));}
142 static TComplex ACosH(const TComplex &c)
143 {return Log(c+TMath::Sign(1.,c.Im())*Sqrt(c*c-1.));}
144 static TComplex ATanH(const TComplex &c)
145 {return 0.5*Log((1.+c)/(1.-c));}
146
147 static Double_t Abs(const TComplex &c)
148 {return c.Rho();}
149
150 static TComplex Power(const TComplex& x, const TComplex& y)
151 {Double_t lrho=TMath::Log(x.Rho());
152 Double_t theta=x.Theta();
153 return TComplex(TMath::Exp(lrho*y.Re()-theta*y.Im()),
154 lrho*y.Im()+theta*y.Re(),kTRUE);}
156 {return TComplex(TMath::Power(x.Rho(),y),x.Theta()*y,kTRUE);}
159 Double_t theta=(x>0)?0:TMath::Pi();
160 return TComplex(TMath::Exp(lrho*y.Re()-theta*y.Im()),
161 lrho*y.Im()+theta*y.Re(),kTRUE);}
162 static TComplex Power(const TComplex& x, Int_t y)
163 {return TComplex(TMath::Power(x.Rho(),y),x.Theta()*y,kTRUE);}
164
165 static Int_t Finite(const TComplex& c)
166 {return TMath::Min(TMath::Finite(c.Re()),TMath::Finite(c.Im()));}
167 static Int_t IsNaN(const TComplex& c)
168 {return TMath::IsNaN(c.Re()) || TMath::IsNaN(c.Im());}
169
170 static TComplex Min(const TComplex &a, const TComplex &b)
171 {return a.Rho()<=b.Rho()?a:b;}
172 static TComplex Max(const TComplex &a, const TComplex &b)
173 {return a.Rho()>=b.Rho()?a:b;}
175 {return TComplex(1.,c.Theta(),kTRUE);}
177 {return TComplex(c.Re(),-c.Im());}
178 static TComplex Range(const TComplex &lb, const TComplex &ub, const TComplex &c)
179 {return Max(lb,Min(c,ub));}
180
181 // I/O
182 friend std::ostream& operator<<(std::ostream& out, const TComplex& c);
183 friend std::istream& operator>>(std::istream& in, TComplex& c);
184
185 ClassDef(TComplex,1) //Complex Class
186};
187
188namespace cling {
189std::string printValue(TComplex *c);
190}
191
192#endif
#define d(i)
Definition RSha256.hxx:102
#define b(i)
Definition RSha256.hxx:100
#define c(i)
Definition RSha256.hxx:101
#define a(i)
Definition RSha256.hxx:99
int Int_t
Definition RtypesCore.h:45
const Bool_t kFALSE
Definition RtypesCore.h:92
bool Bool_t
Definition RtypesCore.h:63
double Double_t
Definition RtypesCore.h:59
float Float_t
Definition RtypesCore.h:57
const Bool_t kTRUE
Definition RtypesCore.h:91
#define ClassDef(name, id)
Definition Rtypes.h:325
static TComplex Max(const TComplex &a, const TComplex &b)
Definition TComplex.h:172
Double_t fRe
Definition TComplex.h:30
Double_t Im() const
Definition TComplex.h:45
friend TComplex operator/(Double_t d, const TComplex &c)
Definition TComplex.h:92
virtual ~TComplex()
Definition TComplex.h:37
Double_t fIm
Definition TComplex.h:31
static TComplex Power(const TComplex &x, Double_t y)
Definition TComplex.h:155
static TComplex ACos(const TComplex &c)
Definition TComplex.h:126
static TComplex Power(Double_t x, const TComplex &y)
Definition TComplex.h:157
friend TComplex operator*(Double_t d, const TComplex &c)
Definition TComplex.h:88
static TComplex I()
Definition TComplex.h:40
friend std::istream & operator>>(std::istream &in, TComplex &c)
Definition TComplex.cxx:49
TComplex operator+()
Definition TComplex.h:74
Double_t Re() const
Definition TComplex.h:44
static TComplex ATan(const TComplex &c)
Definition TComplex.h:128
static TComplex Log2(const TComplex &c)
Definition TComplex.h:110
static TComplex Sqrt(const TComplex &c)
Definition TComplex.h:103
TComplex operator-()
Definition TComplex.h:72
TComplex operator/=(const TComplex &c)
Definition TComplex.h:67
TComplex operator*=(const TComplex &c)
Definition TComplex.h:63
static TComplex CosH(const TComplex &c)
Definition TComplex.h:134
static TComplex ACosH(const TComplex &c)
Definition TComplex.h:142
TComplex operator-=(const TComplex &c)
Definition TComplex.h:69
static TComplex Cos(const TComplex &c)
Definition TComplex.h:118
static TComplex Power(const TComplex &x, const TComplex &y)
Definition TComplex.h:150
static TComplex Log(const TComplex &c)
Definition TComplex.h:108
Double_t Rho2() const
Definition TComplex.h:47
friend std::ostream & operator<<(std::ostream &out, const TComplex &c)
Definition TComplex.cxx:41
static TComplex Sin(const TComplex &c)
Definition TComplex.h:115
static TComplex One()
Definition TComplex.h:41
static TComplex Conjugate(const TComplex &c)
Definition TComplex.h:176
Double_t Theta() const
Definition TComplex.h:48
TComplex operator+=(const TComplex &c)
Definition TComplex.h:65
static TComplex SinH(const TComplex &c)
Definition TComplex.h:131
static TComplex TanH(const TComplex &c)
Definition TComplex.h:137
static TComplex ASin(const TComplex &c)
Definition TComplex.h:124
static TComplex Log10(const TComplex &c)
Definition TComplex.h:112
static TComplex Range(const TComplex &lb, const TComplex &ub, const TComplex &c)
Definition TComplex.h:178
static Int_t Finite(const TComplex &c)
Definition TComplex.h:165
static TComplex Tan(const TComplex &c)
Definition TComplex.h:121
Double_t Rho() const
Definition TComplex.h:46
static TComplex Normalize(const TComplex &c)
Definition TComplex.h:174
static TComplex Min(const TComplex &a, const TComplex &b)
Definition TComplex.h:170
static TComplex Power(const TComplex &x, Int_t y)
Definition TComplex.h:162
static TComplex Exp(const TComplex &c)
Definition TComplex.h:106
static Int_t IsNaN(const TComplex &c)
Definition TComplex.h:167
static Double_t Abs(const TComplex &c)
Definition TComplex.h:147
TComplex operator()(Double_t x, Double_t y, Bool_t polar=kFALSE)
Definition TComplex.h:49
static TComplex ASinH(const TComplex &c)
Definition TComplex.h:140
TComplex()
Definition TComplex.h:35
static TComplex ATanH(const TComplex &c)
Definition TComplex.h:144
Double_t y[n]
Definition legend1.C:17
Double_t x[n]
Definition legend1.C:17
Double_t CosH(Double_t)
Definition TMath.h:655
Bool_t IsNaN(Double_t x)
Definition TMath.h:892
Double_t Exp(Double_t x)
Definition TMath.h:727
T1 Sign(T1 a, T2 b)
Definition TMathBase.h:165
Int_t Finite(Double_t x)
Check if it is finite with a mask in order to be consistent in presence of fast math.
Definition TMath.h:771
Double_t ATan2(Double_t y, Double_t x)
Definition TMath.h:679
Double_t Log(Double_t x)
Definition TMath.h:760
Double_t Sqrt(Double_t x)
Definition TMath.h:691
LongDouble_t Power(LongDouble_t x, LongDouble_t y)
Definition TMath.h:735
Short_t Min(Short_t a, Short_t b)
Definition TMathBase.h:180
Double_t Cos(Double_t)
Definition TMath.h:643
constexpr Double_t Pi()
Definition TMath.h:37
Double_t Sin(Double_t)
Definition TMath.h:639
Short_t Abs(Short_t d)
Definition TMathBase.h:120
Double_t SinH(Double_t)
Definition TMath.h:651