Logo ROOT   6.13/01
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 
25 
26 class TComplex {
27 
28 protected:
29  Double_t fRe; // real part
30  Double_t fIm; // imaginary part
31 
32 public:
33  // ctors and dtors
34  TComplex(): fRe(0), fIm(0) {}
35  TComplex(Double_t re, Double_t im=0, Bool_t polar=kFALSE);
36  virtual ~TComplex() {}
37 
38  // constants
39  static TComplex I() {return TComplex(0,1);}
40  static TComplex One() {return TComplex(1,0);}
41 
42  // getters and setters
43  Double_t Re() const {return fRe;}
44  Double_t Im() const {return fIm;}
45  Double_t Rho() const {return TMath::Sqrt(fRe*fRe+fIm*fIm);}
46  Double_t Rho2() const {return fRe*fRe+fIm*fIm;}
47  Double_t Theta() const {return (fIm||fRe)?TMath::ATan2(fIm,fRe):0;}
48  TComplex operator()(Double_t x, Double_t y, Bool_t polar=kFALSE)
49  { if (polar) { fRe = x*TMath::Cos(y); fIm = x*TMath::Sin(y); }
50  else { fRe = x; fIm = y; } return *this; }
51 
52  // Simple operators complex - complex
53  TComplex operator *(const TComplex & c) const
54  {return TComplex(fRe*c.fRe-fIm*c.fIm,fRe*c.fIm+fIm*c.fRe);}
55  TComplex operator +(const TComplex & c) const
56  {return TComplex(fRe+c.fRe, fIm+c.fIm);}
57  TComplex operator /(const TComplex & c) const
58  {return TComplex(fRe*c.fRe+fIm*c.fIm,-fRe*c.fIm+fIm*c.fRe)/c.Rho2();}
59  TComplex operator -(const TComplex & c) const
60  {return TComplex(fRe-c.fRe, fIm-c.fIm);}
61 
63  {return ((*this) = (*this) * c);}
65  {return ((*this) = (*this) + c);}
67  {return ((*this) = (*this) / c);}
69  {return ((*this) = (*this) - c);}
70 
72  {return TComplex(-fRe,-fIm);}
74  {return *this;}
75 
76  // Simple operators complex - double
77  TComplex operator *(Double_t c) const
78  {return TComplex(fRe*c,fIm*c);}
79  TComplex operator +(Double_t c) const
80  {return TComplex(fRe+c, fIm);}
81  TComplex operator /(Double_t c) const
82  {return TComplex(fRe/c,fIm/c);}
83  TComplex operator -(Double_t c) const
84  {return TComplex(fRe-c, fIm);}
85 
86  // Simple operators double - complex
87  friend TComplex operator *(Double_t d, const TComplex & c)
88  {return TComplex(d*c.fRe,d*c.fIm);}
89  friend TComplex operator +(Double_t d, const TComplex & c)
90  {return TComplex(d+c.fRe, c.fIm);}
91  friend TComplex operator /(Double_t d, const TComplex & c)
92  {return TComplex(d*c.fRe,-d*c.fIm)/c.Rho2();}
93  friend TComplex operator -(Double_t d, const TComplex & c)
94  {return TComplex(d-c.fRe, -c.fIm);}
95 
96  // Convertors
97  operator Double_t () const {return fRe;}
98  operator Float_t () const {return static_cast<Float_t>(fRe);}
99  operator Int_t () const {return static_cast<Int_t>(fRe);}
100 
101  // TMath:: extensions
102  static TComplex Sqrt(const TComplex &c)
103  {return TComplex(TMath::Sqrt(c.Rho()),0.5*c.Theta(),kTRUE);}
104 
105  static TComplex Exp(const TComplex &c)
106  {return TComplex(TMath::Exp(c.fRe),c.fIm,kTRUE);}
107  static TComplex Log(const TComplex &c)
108  {return TComplex(0.5*TMath::Log(c.Rho2()),c.Theta());}
109  static TComplex Log2(const TComplex &c)
110  {return Log(c)/TMath::Log(2);}
111  static TComplex Log10(const TComplex &c)
112  {return Log(c)/TMath::Log(10);}
113 
114  static TComplex Sin(const TComplex &c)
115  {return TComplex(TMath::Sin(c.fRe)*TMath::CosH(c.fIm),
116  TMath::Cos(c.fRe)*TMath::SinH(c.fIm));}
117  static TComplex Cos(const TComplex &c)
118  {return TComplex(TMath::Cos(c.fRe)*TMath::CosH(c.fIm),
119  -TMath::Sin(c.fRe)*TMath::SinH(c.fIm));}
120  static TComplex Tan(const TComplex &c)
121  {TComplex cc=Cos(c); return Sin(c)*Conjugate(cc)/cc.Rho2();}
122 
123  static TComplex ASin(const TComplex &c)
124  {return -I()*Log(I()*c+TMath::Sign(1.,c.Im())*Sqrt(1.-c*c));}
125  static TComplex ACos(const TComplex &c)
126  {return -I()*Log(c+TMath::Sign(1.,c.Im())*Sqrt(c*c-1.));}
127  static TComplex ATan(const TComplex &c)
128  {return -0.5*I()*Log((1.+I()*c)/(1.-I()*c));}
129 
130  static TComplex SinH(const TComplex &c)
131  {return TComplex(TMath::SinH(c.fRe)*TMath::Cos(c.fIm),
132  TMath::CosH(c.fRe)*TMath::Sin(c.fIm));}
133  static TComplex CosH(const TComplex &c)
134  {return TComplex(TMath::CosH(c.fRe)*TMath::Cos(c.fIm),
135  TMath::SinH(c.fRe)*TMath::Sin(c.fIm));}
136  static TComplex TanH(const TComplex &c)
137  {TComplex cc=CosH(c); return SinH(c)*Conjugate(cc)/cc.Rho2();}
138 
139  static TComplex ASinH(const TComplex &c)
140  {return Log(c+TMath::Sign(1.,c.Im())*Sqrt(c*c+1.));}
141  static TComplex ACosH(const TComplex &c)
142  {return Log(c+TMath::Sign(1.,c.Im())*Sqrt(c*c-1.));}
143  static TComplex ATanH(const TComplex &c)
144  {return 0.5*Log((1.+c)/(1.-c));}
145 
146  static Double_t Abs(const TComplex &c)
147  {return c.Rho();}
148 
149  static TComplex Power(const TComplex& x, const TComplex& y)
150  {Double_t lrho=TMath::Log(x.Rho());
151  Double_t theta=x.Theta();
152  return TComplex(TMath::Exp(lrho*y.Re()-theta*y.Im()),
153  lrho*y.Im()+theta*y.Re(),kTRUE);}
154  static TComplex Power(const TComplex& x, Double_t y)
155  {return TComplex(TMath::Power(x.Rho(),y),x.Theta()*y,kTRUE);}
156  static TComplex Power(Double_t x, const TComplex& y)
157  {Double_t lrho=TMath::Log(TMath::Abs(x));
158  Double_t theta=(x>0)?0:TMath::Pi();
159  return TComplex(TMath::Exp(lrho*y.Re()-theta*y.Im()),
160  lrho*y.Im()+theta*y.Re(),kTRUE);}
161  static TComplex Power(const TComplex& x, Int_t y)
162  {return TComplex(TMath::Power(x.Rho(),y),x.Theta()*y,kTRUE);}
163 
164  static Int_t Finite(const TComplex& c)
165  {return TMath::Min(TMath::Finite(c.Re()),TMath::Finite(c.Im()));}
166  static Int_t IsNaN(const TComplex& c)
167  {return TMath::IsNaN(c.Re()) || TMath::IsNaN(c.Im());}
168 
169  static TComplex Min(const TComplex &a, const TComplex &b)
170  {return a.Rho()<=b.Rho()?a:b;}
171  static TComplex Max(const TComplex &a, const TComplex &b)
172  {return a.Rho()>=b.Rho()?a:b;}
173  static TComplex Normalize(const TComplex &c)
174  {return TComplex(1.,c.Theta(),kTRUE);}
175  static TComplex Conjugate(const TComplex &c)
176  {return TComplex(c.Re(),-c.Im());}
177  static TComplex Range(const TComplex &lb, const TComplex &ub, const TComplex &c)
178  {return Max(lb,Min(c,ub));}
179 
180  // I/O
181  friend std::ostream& operator<<(std::ostream& out, const TComplex& c);
182  friend std::istream& operator>>(std::istream& in, TComplex& c);
183 
184  ClassDef(TComplex,1) //Complex Class
185 };
186 
187 #endif
virtual ~TComplex()
Definition: TComplex.h:36
friend std::ostream & operator<<(std::ostream &out, const TComplex &c)
Definition: TComplex.cxx:41
TComplex operator/=(const TComplex &c)
Definition: TComplex.h:66
static TComplex Power(const TComplex &x, Double_t y)
Definition: TComplex.h:154
static TComplex I()
Definition: TComplex.h:39
TComplex()
Definition: TComplex.h:34
static TComplex ACosH(const TComplex &c)
Definition: TComplex.h:141
Double_t Log(Double_t x)
Definition: TMath.h:759
static TComplex ASinH(const TComplex &c)
Definition: TComplex.h:139
Double_t fIm
Definition: TComplex.h:30
static TComplex Power(Double_t x, const TComplex &y)
Definition: TComplex.h:156
static TComplex Normalize(const TComplex &c)
Definition: TComplex.h:173
static TComplex Exp(const TComplex &c)
Definition: TComplex.h:105
Double_t Re() const
Definition: TComplex.h:43
static TComplex One()
Definition: TComplex.h:40
TComplex operator-=(const TComplex &c)
Definition: TComplex.h:68
Double_t Rho2() const
Definition: TComplex.h:46
Bool_t IsNaN(Double_t x)
Definition: TMath.h:891
static TComplex Tan(const TComplex &c)
Definition: TComplex.h:120
you should not use this method at all Int_t y
Definition: TRolke.cxx:630
static TComplex Power(const TComplex &x, const TComplex &y)
Definition: TComplex.h:149
Double_t Theta() const
Definition: TComplex.h:47
LongDouble_t Power(LongDouble_t x, LongDouble_t y)
Definition: TMath.h:734
TComplex operator*=(const TComplex &c)
Definition: TComplex.h:62
int Sign(const T &x)
sign.
Definition: Functions.h:127
static TComplex CosH(const TComplex &c)
Definition: TComplex.h:133
static TComplex ACos(const TComplex &c)
Definition: TComplex.h:125
static TComplex Log2(const TComplex &c)
Definition: TComplex.h:109
static TComplex Range(const TComplex &lb, const TComplex &ub, const TComplex &c)
Definition: TComplex.h:177
static TComplex Cos(const TComplex &c)
Definition: TComplex.h:117
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:770
Double_t ATan2(Double_t, Double_t)
Definition: TMath.h:678
constexpr Double_t Pi()
Definition: TMath.h:38
static TComplex Power(const TComplex &x, Int_t y)
Definition: TComplex.h:161
static TComplex Conjugate(const TComplex &c)
Definition: TComplex.h:175
friend std::istream & operator>>(std::istream &in, TComplex &c)
Definition: TComplex.cxx:49
static TComplex TanH(const TComplex &c)
Definition: TComplex.h:136
TComplex operator*(const TComplex &c) const
Definition: TComplex.h:53
static TComplex Log(const TComplex &c)
Definition: TComplex.h:107
static Int_t Finite(const TComplex &c)
Definition: TComplex.h:164
static Double_t Abs(const TComplex &c)
Definition: TComplex.h:146
static TComplex ASin(const TComplex &c)
Definition: TComplex.h:123
* x
Deprecated and error prone model selection interface.
Definition: TRolke.cxx:630
TComplex operator/(const TComplex &c) const
Definition: TComplex.h:57
Double_t Cos(Double_t)
Definition: TMath.h:640
static TComplex ATanH(const TComplex &c)
Definition: TComplex.h:143
static TComplex ATan(const TComplex &c)
Definition: TComplex.h:127
static TComplex Max(const TComplex &a, const TComplex &b)
Definition: TComplex.h:171
Double_t Exp(Double_t x)
Definition: TMath.h:726
static TComplex Min(const TComplex &a, const TComplex &b)
Definition: TComplex.h:169
TComplex operator()(Double_t x, Double_t y, Bool_t polar=kFALSE)
Definition: TComplex.h:48
TComplex operator+()
Definition: TComplex.h:73
Double_t fRe
Definition: TComplex.h:29
Double_t Im() const
Definition: TComplex.h:44
static TComplex Log10(const TComplex &c)
Definition: TComplex.h:111
static Int_t IsNaN(const TComplex &c)
Definition: TComplex.h:166
static TComplex Sin(const TComplex &c)
Definition: TComplex.h:114
static TComplex Sqrt(const TComplex &c)
Definition: TComplex.h:102
Double_t Sin(Double_t)
Definition: TMath.h:636
you should not use this method at all Int_t Int_t Double_t Double_t Double_t Int_t Double_t Double_t Double_t Double_t b
Definition: TRolke.cxx:630
static TComplex SinH(const TComplex &c)
Definition: TComplex.h:130
Double_t SinH(Double_t)
Definition: TMath.h:648
Double_t Sqrt(Double_t x)
Definition: TMath.h:690
TComplex operator-()
Definition: TComplex.h:71
TComplex operator+=(const TComplex &c)
Definition: TComplex.h:64
Double_t CosH(Double_t)
Definition: TMath.h:652
Double_t Rho() const
Definition: TComplex.h:45