ROOT  6.06/09
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 #ifndef ROOT_Riosfwd
22 #include "Riosfwd.h"
23 #endif
24 #ifndef ROOT_TMath
25 #include "TMath.h"
26 #endif
27 
28 
29 class TComplex {
30 
31 protected:
32  Double_t fRe; // real part
33  Double_t fIm; // imaginary part
34 
35 public:
36  // ctors and dtors
37  TComplex(): fRe(0), fIm(0) {}
38  TComplex(Double_t re, Double_t im=0, Bool_t polar=kFALSE);
39  virtual ~TComplex() {}
40 
41  // constants
42  static TComplex I() {return TComplex(0,1);}
43  static TComplex One() {return TComplex(1,0);}
44 
45  // getters and setters
46  Double_t Re() const {return fRe;}
47  Double_t Im() const {return fIm;}
48  Double_t Rho() const {return TMath::Sqrt(fRe*fRe+fIm*fIm);}
49  Double_t Rho2() const {return fRe*fRe+fIm*fIm;}
50  Double_t Theta() const {return (fIm||fRe)?TMath::ATan2(fIm,fRe):0;}
52  { if (polar) { fRe = x*TMath::Cos(y); fIm = x*TMath::Sin(y); }
53  else { fRe = x; fIm = y; } return *this; }
54 
55  // Simple operators complex - complex
56  TComplex operator *(const TComplex & c) const
57  {return TComplex(fRe*c.fRe-fIm*c.fIm,fRe*c.fIm+fIm*c.fRe);}
58  TComplex operator +(const TComplex & c) const
59  {return TComplex(fRe+c.fRe, fIm+c.fIm);}
60  TComplex operator /(const TComplex & c) const
61  {return TComplex(fRe*c.fRe+fIm*c.fIm,-fRe*c.fIm+fIm*c.fRe)/c.Rho2();}
62  TComplex operator -(const TComplex & c) const
63  {return TComplex(fRe-c.fRe, fIm-c.fIm);}
64 
66  {return ((*this) = (*this) * c);}
68  {return ((*this) = (*this) + c);}
70  {return ((*this) = (*this) / c);}
72  {return ((*this) = (*this) - c);}
73 
75  {return TComplex(-fRe,-fIm);}
77  {return *this;}
78 
79  // Simple operators complex - double
81  {return TComplex(fRe*c,fIm*c);}
83  {return TComplex(fRe+c, fIm);}
85  {return TComplex(fRe/c,fIm/c);}
87  {return TComplex(fRe-c, fIm);}
88 
89  // Simple operators double - complex
90  friend TComplex operator *(Double_t d, const TComplex & c)
91  {return TComplex(d*c.fRe,d*c.fIm);}
92  friend TComplex operator +(Double_t d, const TComplex & c)
93  {return TComplex(d+c.fRe, c.fIm);}
94  friend TComplex operator /(Double_t d, const TComplex & c)
95  {return TComplex(d*c.fRe,-d*c.fIm)/c.Rho2();}
96  friend TComplex operator -(Double_t d, const TComplex & c)
97  {return TComplex(d-c.fRe, -c.fIm);}
98 
99  // Convertors
100  operator Double_t () const {return fRe;}
101  operator Float_t () const {return static_cast<Float_t>(fRe);}
102  operator Int_t () const {return static_cast<Int_t>(fRe);}
103 
104  // TMath:: extensions
105  static TComplex Sqrt(const TComplex &c)
106  {return TComplex(TMath::Sqrt(c.Rho()),0.5*c.Theta(),kTRUE);}
107 
108  static TComplex Exp(const TComplex &c)
109  {return TComplex(TMath::Exp(c.fRe),c.fIm,kTRUE);}
110  static TComplex Log(const TComplex &c)
111  {return TComplex(0.5*TMath::Log(c.Rho2()),c.Theta());}
112  static TComplex Log2(const TComplex &c)
113  {return Log(c)/TMath::Log(2);}
114  static TComplex Log10(const TComplex &c)
115  {return Log(c)/TMath::Log(10);}
116 
117  static TComplex Sin(const TComplex &c)
118  {return TComplex(TMath::Sin(c.fRe)*TMath::CosH(c.fIm),
119  TMath::Cos(c.fRe)*TMath::SinH(c.fIm));}
120  static TComplex Cos(const TComplex &c)
121  {return TComplex(TMath::Cos(c.fRe)*TMath::CosH(c.fIm),
122  -TMath::Sin(c.fRe)*TMath::SinH(c.fIm));}
123  static TComplex Tan(const TComplex &c)
124  {TComplex cc=Cos(c); return Sin(c)*Conjugate(cc)/cc.Rho2();}
125 
126  static TComplex ASin(const TComplex &c)
127  {return -I()*Log(I()*c+TMath::Sign(1.,c.Im())*Sqrt(1.-c*c));}
128  static TComplex ACos(const TComplex &c)
129  {return -I()*Log(c+TMath::Sign(1.,c.Im())*Sqrt(c*c-1.));}
130  static TComplex ATan(const TComplex &c)
131  {return -0.5*I()*Log((1.+I()*c)/(1.-I()*c));}
132 
133  static TComplex SinH(const TComplex &c)
134  {return TComplex(TMath::SinH(c.fRe)*TMath::Cos(c.fIm),
135  TMath::CosH(c.fRe)*TMath::Sin(c.fIm));}
136  static TComplex CosH(const TComplex &c)
137  {return TComplex(TMath::CosH(c.fRe)*TMath::Cos(c.fIm),
138  TMath::SinH(c.fRe)*TMath::Sin(c.fIm));}
139  static TComplex TanH(const TComplex &c)
140  {TComplex cc=CosH(c); return SinH(c)*Conjugate(cc)/cc.Rho2();}
141 
142  static TComplex ASinH(const TComplex &c)
143  {return Log(c+TMath::Sign(1.,c.Im())*Sqrt(c*c+1.));}
144  static TComplex ACosH(const TComplex &c)
145  {return Log(c+TMath::Sign(1.,c.Im())*Sqrt(c*c-1.));}
146  static TComplex ATanH(const TComplex &c)
147  {return 0.5*Log((1.+c)/(1.-c));}
148 
149  static Double_t Abs(const TComplex &c)
150  {return c.Rho();}
151 
152  static TComplex Power(const TComplex& x, const TComplex& y)
153  {Double_t lrho=TMath::Log(x.Rho());
154  Double_t theta=x.Theta();
155  return TComplex(TMath::Exp(lrho*y.Re()-theta*y.Im()),
156  lrho*y.Im()+theta*y.Re(),kTRUE);}
157  static TComplex Power(const TComplex& x, Double_t y)
158  {return TComplex(TMath::Power(x.Rho(),y),x.Theta()*y,kTRUE);}
159  static TComplex Power(Double_t x, const TComplex& y)
160  {Double_t lrho=TMath::Log(TMath::Abs(x));
161  Double_t theta=(x>0)?0:TMath::Pi();
162  return TComplex(TMath::Exp(lrho*y.Re()-theta*y.Im()),
163  lrho*y.Im()+theta*y.Re(),kTRUE);}
164  static TComplex Power(const TComplex& x, Int_t y)
165  {return TComplex(TMath::Power(x.Rho(),y),x.Theta()*y,kTRUE);}
166 
167  static Int_t Finite(const TComplex& c)
168  {return TMath::Min(TMath::Finite(c.Re()),TMath::Finite(c.Im()));}
169  static Int_t IsNaN(const TComplex& c)
170  {return TMath::Max(TMath::IsNaN(c.Re()),TMath::IsNaN(c.Im()));}
171 
172  static TComplex Min(const TComplex &a, const TComplex &b)
173  {return a.Rho()<=b.Rho()?a:b;}
174  static TComplex Max(const TComplex &a, const TComplex &b)
175  {return a.Rho()>=b.Rho()?a:b;}
176  static TComplex Normalize(const TComplex &c)
177  {return TComplex(1.,c.Theta(),kTRUE);}
178  static TComplex Conjugate(const TComplex &c)
179  {return TComplex(c.Re(),-c.Im());}
180  static TComplex Range(const TComplex &lb, const TComplex &ub, const TComplex &c)
181  {return Max(lb,Min(c,ub));}
182 
183  // I/O
184  friend std::ostream& operator<<(std::ostream& out, const TComplex& c);
185  friend std::istream& operator>>(std::istream& in, TComplex& c);
186 
187  ClassDef(TComplex,1) //Complex Class
188 };
189 
190 #endif
virtual ~TComplex()
Definition: TComplex.h:39
friend std::ostream & operator<<(std::ostream &out, const TComplex &c)
Definition: TComplex.cxx:41
TComplex operator*(const TComplex &c) const
Definition: TComplex.h:56
TComplex operator/=(const TComplex &c)
Definition: TComplex.h:69
static TComplex Power(const TComplex &x, Double_t y)
Definition: TComplex.h:157
static TComplex I()
Definition: TComplex.h:42
TComplex()
Definition: TComplex.h:37
static TComplex ACosH(const TComplex &c)
Definition: TComplex.h:144
T1 Sign(T1 a, T2 b)
Definition: TMathBase.h:155
Double_t Log(Double_t x)
Definition: TMath.h:526
Double_t Rho2() const
Definition: TComplex.h:49
float Float_t
Definition: RtypesCore.h:53
static TComplex ASinH(const TComplex &c)
Definition: TComplex.h:142
Double_t fIm
Definition: TComplex.h:33
static TComplex Power(Double_t x, const TComplex &y)
Definition: TComplex.h:159
static TComplex Normalize(const TComplex &c)
Definition: TComplex.h:176
static TComplex Exp(const TComplex &c)
Definition: TComplex.h:108
Double_t Re() const
Definition: TComplex.h:46
static TComplex One()
Definition: TComplex.h:43
Short_t Min(Short_t a, Short_t b)
Definition: TMathBase.h:170
TComplex operator-=(const TComplex &c)
Definition: TComplex.h:71
int Int_t
Definition: RtypesCore.h:41
bool Bool_t
Definition: RtypesCore.h:59
TArc * a
Definition: textangle.C:12
const Bool_t kFALSE
Definition: Rtypes.h:92
static TComplex Tan(const TComplex &c)
Definition: TComplex.h:123
static TComplex Power(const TComplex &x, const TComplex &y)
Definition: TComplex.h:152
Short_t Abs(Short_t d)
Definition: TMathBase.h:110
LongDouble_t Power(LongDouble_t x, LongDouble_t y)
Definition: TMath.h:501
TComplex operator*=(const TComplex &c)
Definition: TComplex.h:65
static TComplex CosH(const TComplex &c)
Definition: TComplex.h:136
Double_t x[n]
Definition: legend1.C:17
static TComplex ACos(const TComplex &c)
Definition: TComplex.h:128
#define ClassDef(name, id)
Definition: Rtypes.h:254
static TComplex Log2(const TComplex &c)
Definition: TComplex.h:112
static TComplex Range(const TComplex &lb, const TComplex &ub, const TComplex &c)
Definition: TComplex.h:180
static TComplex Cos(const TComplex &c)
Definition: TComplex.h:120
Int_t Finite(Double_t x)
Definition: TMath.h:532
Double_t ATan2(Double_t, Double_t)
Definition: TMath.h:454
char * out
Definition: TBase64.cxx:29
static TComplex Power(const TComplex &x, Int_t y)
Definition: TComplex.h:164
Double_t Rho() const
Definition: TComplex.h:48
static TComplex Conjugate(const TComplex &c)
Definition: TComplex.h:178
friend std::istream & operator>>(std::istream &in, TComplex &c)
Definition: TComplex.cxx:49
static TComplex TanH(const TComplex &c)
Definition: TComplex.h:139
static TComplex Log(const TComplex &c)
Definition: TComplex.h:110
static Int_t Finite(const TComplex &c)
Definition: TComplex.h:167
static Double_t Abs(const TComplex &c)
Definition: TComplex.h:149
static TComplex ASin(const TComplex &c)
Definition: TComplex.h:126
Double_t Cos(Double_t)
Definition: TMath.h:424
static TComplex ATanH(const TComplex &c)
Definition: TComplex.h:146
Double_t Pi()
Definition: TMath.h:44
static TComplex ATan(const TComplex &c)
Definition: TComplex.h:130
static TComplex Max(const TComplex &a, const TComplex &b)
Definition: TComplex.h:174
Double_t Exp(Double_t x)
Definition: TMath.h:495
static TComplex Min(const TComplex &a, const TComplex &b)
Definition: TComplex.h:172
double Double_t
Definition: RtypesCore.h:55
Double_t Im() const
Definition: TComplex.h:47
Double_t y[n]
Definition: legend1.C:17
TComplex operator()(Double_t x, Double_t y, Bool_t polar=kFALSE)
Definition: TComplex.h:51
TComplex operator+()
Definition: TComplex.h:76
Double_t fRe
Definition: TComplex.h:32
static TComplex Log10(const TComplex &c)
Definition: TComplex.h:114
static Int_t IsNaN(const TComplex &c)
Definition: TComplex.h:169
static TComplex Sin(const TComplex &c)
Definition: TComplex.h:117
static TComplex Sqrt(const TComplex &c)
Definition: TComplex.h:105
Short_t Max(Short_t a, Short_t b)
Definition: TMathBase.h:202
Double_t Theta() const
Definition: TComplex.h:50
Int_t IsNaN(Double_t x)
Definition: TMath.h:617
Double_t Sin(Double_t)
Definition: TMath.h:421
TComplex operator/(const TComplex &c) const
Definition: TComplex.h:60
static TComplex SinH(const TComplex &c)
Definition: TComplex.h:133
Double_t SinH(Double_t)
Definition: TMath.h:430
Double_t Sqrt(Double_t x)
Definition: TMath.h:464
TComplex operator-()
Definition: TComplex.h:74
TComplex operator+=(const TComplex &c)
Definition: TComplex.h:67
const Bool_t kTRUE
Definition: Rtypes.h:91
Double_t CosH(Double_t)
Definition: TMath.h:433