Logo ROOT  
Reference Guide
TComplex.h
Go to the documentation of this file.
1 // @(#)root/mathcore:$Id$
2 // Author: Federico Carminati 22/04/2004
3 
4 /*************************************************************************
5  * Copyright (C) 1995-2004, Rene Brun and Fons Rademakers. *
6  * All rights reserved. *
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 
27 class TComplex {
28 
29 protected:
30  Double_t fRe; // real part
31  Double_t fIm; // imaginary part
32 
33 public:
34  // ctors and dtors
35  TComplex(): fRe(0), fIm(0) {}
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
54  TComplex operator *(const TComplex & c) const
55  {return TComplex(fRe*c.fRe-fIm*c.fIm,fRe*c.fIm+fIm*c.fRe);}
56  TComplex operator +(const TComplex & c) const
57  {return TComplex(fRe+c.fRe, fIm+c.fIm);}
58  TComplex operator /(const TComplex & c) const
59  {return TComplex(fRe*c.fRe+fIm*c.fIm,-fRe*c.fIm+fIm*c.fRe)/c.Rho2();}
60  TComplex operator -(const TComplex & c) const
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);}
92  friend TComplex operator /(Double_t d, const TComplex & c)
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);}
155  static TComplex Power(const TComplex& x, Double_t y)
156  {return TComplex(TMath::Power(x.Rho(),y),x.Theta()*y,kTRUE);}
157  static TComplex Power(Double_t x, const TComplex& y)
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;}
174  static TComplex Normalize(const TComplex &c)
175  {return TComplex(1.,c.Theta(),kTRUE);}
176  static TComplex Conjugate(const TComplex &c)
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 
188 namespace cling {
189 std::string printValue(TComplex *c);
190 }
191 
192 #endif
c
#define c(i)
Definition: RSha256.hxx:119
TComplex::Cos
static TComplex Cos(const TComplex &c)
Definition: TComplex.h:124
TComplex::operator*=
TComplex operator*=(const TComplex &c)
Definition: TComplex.h:69
TComplex::Re
Double_t Re() const
Definition: TComplex.h:50
TComplex::Finite
static Int_t Finite(const TComplex &c)
Definition: TComplex.h:171
kTRUE
const Bool_t kTRUE
Definition: RtypesCore.h:91
TComplex::Rho
Double_t Rho() const
Definition: TComplex.h:52
TMath::ATan2
Double_t ATan2(Double_t y, Double_t x)
Definition: TMath.h:668
TComplex
Definition: TComplex.h:27
TComplex::Theta
Double_t Theta() const
Definition: TComplex.h:54
TMath::Cos
Double_t Cos(Double_t)
Definition: TMath.h:630
TComplex::SinH
static TComplex SinH(const TComplex &c)
Definition: TComplex.h:137
TComplex::Conjugate
static TComplex Conjugate(const TComplex &c)
Definition: TComplex.h:182
TComplex::Max
static TComplex Max(const TComplex &a, const TComplex &b)
Definition: TComplex.h:178
TComplex::operator+
TComplex operator+()
Definition: TComplex.h:80
TMath::Log
Double_t Log(Double_t x)
Definition: TMath.h:749
TMath::Sqrt
Double_t Sqrt(Double_t x)
Definition: TMath.h:680
TComplex::operator()
TComplex operator()(Double_t x, Double_t y, Bool_t polar=kFALSE)
Definition: TComplex.h:55
TComplex::ASinH
static TComplex ASinH(const TComplex &c)
Definition: TComplex.h:146
Float_t
float Float_t
Definition: RtypesCore.h:57
TMath::Exp
Double_t Exp(Double_t x)
Definition: TMath.h:716
Int_t
int Int_t
Definition: RtypesCore.h:45
TComplex::Sqrt
static TComplex Sqrt(const TComplex &c)
Definition: TComplex.h:109
TComplex::One
static TComplex One()
Definition: TComplex.h:47
TComplex::Exp
static TComplex Exp(const TComplex &c)
Definition: TComplex.h:112
x
Double_t x[n]
Definition: legend1.C:17
TMath::Abs
Short_t Abs(Short_t d)
Definition: TMathBase.h:120
TComplex::Tan
static TComplex Tan(const TComplex &c)
Definition: TComplex.h:127
TComplex::operator*
TComplex operator*(const TComplex &c) const
Definition: TComplex.h:60
TComplex::Abs
static Double_t Abs(const TComplex &c)
Definition: TComplex.h:153
TComplex::operator+=
TComplex operator+=(const TComplex &c)
Definition: TComplex.h:71
Bool_t
bool Bool_t
Definition: RtypesCore.h:63
b
#define b(i)
Definition: RSha256.hxx:118
TComplex::CosH
static TComplex CosH(const TComplex &c)
Definition: TComplex.h:140
TComplex::operator<<
friend std::ostream & operator<<(std::ostream &out, const TComplex &c)
Definition: TComplex.cxx:41
TComplex::Normalize
static TComplex Normalize(const TComplex &c)
Definition: TComplex.h:180
TComplex::operator>>
friend std::istream & operator>>(std::istream &in, TComplex &c)
Definition: TComplex.cxx:49
bool
TComplex::Sin
static TComplex Sin(const TComplex &c)
Definition: TComplex.h:121
TComplex::fRe
Double_t fRe
Definition: TComplex.h:36
TComplex::ATan
static TComplex ATan(const TComplex &c)
Definition: TComplex.h:134
TComplex::Range
static TComplex Range(const TComplex &lb, const TComplex &ub, const TComplex &c)
Definition: TComplex.h:184
TComplex::Min
static TComplex Min(const TComplex &a, const TComplex &b)
Definition: TComplex.h:176
TComplex::operator-
TComplex operator-()
Definition: TComplex.h:78
TMath::Pi
constexpr Double_t Pi()
Definition: TMath.h:43
TComplex::operator/=
TComplex operator/=(const TComplex &c)
Definition: TComplex.h:73
TComplex::ACosH
static TComplex ACosH(const TComplex &c)
Definition: TComplex.h:148
TComplex::TanH
static TComplex TanH(const TComplex &c)
Definition: TComplex.h:143
TMath::IsNaN
Bool_t IsNaN(Double_t x)
Definition: TMath.h:881
TComplex::Power
static TComplex Power(const TComplex &x, const TComplex &y)
Definition: TComplex.h:156
TComplex::Log10
static TComplex Log10(const TComplex &c)
Definition: TComplex.h:118
a
auto * a
Definition: textangle.C:12
kFALSE
const Bool_t kFALSE
Definition: RtypesCore.h:92
TMath::Sign
T1 Sign(T1 a, T2 b)
Definition: TMathBase.h:165
TMath::Power
LongDouble_t Power(LongDouble_t x, LongDouble_t y)
Definition: TMath.h:724
TComplex::TComplex
TComplex()
Definition: TComplex.h:41
TComplex::Log
static TComplex Log(const TComplex &c)
Definition: TComplex.h:114
TComplex::IsNaN
static Int_t IsNaN(const TComplex &c)
Definition: TComplex.h:173
TMath::Sin
Double_t Sin(Double_t)
Definition: TMath.h:626
y
Double_t y[n]
Definition: legend1.C:17
TMath::SinH
Double_t SinH(Double_t)
Definition: TMath.h:638
TMath::Min
Short_t Min(Short_t a, Short_t b)
Definition: TMathBase.h:180
TComplex::fIm
Double_t fIm
Definition: TComplex.h:37
TComplex::Rho2
Double_t Rho2() const
Definition: TComplex.h:53
TMath::CosH
Double_t CosH(Double_t)
Definition: TMath.h:642
TComplex::operator/
TComplex operator/(const TComplex &c) const
Definition: TComplex.h:64
Double_t
double Double_t
Definition: RtypesCore.h:59
TComplex::ACos
static TComplex ACos(const TComplex &c)
Definition: TComplex.h:132
ClassDef
#define ClassDef(name, id)
Definition: Rtypes.h:325
TComplex::I
static TComplex I()
Definition: TComplex.h:46
TComplex::~TComplex
virtual ~TComplex()
Definition: TComplex.h:43
d
#define d(i)
Definition: RSha256.hxx:120
TComplex::ASin
static TComplex ASin(const TComplex &c)
Definition: TComplex.h:130
TComplex::ATanH
static TComplex ATanH(const TComplex &c)
Definition: TComplex.h:150
TComplex::Im
Double_t Im() const
Definition: TComplex.h:51
TComplex::operator-=
TComplex operator-=(const TComplex &c)
Definition: TComplex.h:75
Rtypes.h
TMath::Finite
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:760
TComplex::Log2
static TComplex Log2(const TComplex &c)
Definition: TComplex.h:116
TMath.h
int