Logo ROOT  
Reference Guide
No Matches
Go to the documentation of this file.
1// @(#)root/mathcore:$Id$
2// Author: Federico Carminati 22/04/2004
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 *************************************************************************/
12#ifndef ROOT_TComplex
13#define ROOT_TComplex
16// //
17// TComplex //
18// //
21#include "TMath.h"
23#include "Rtypes.h"
25#include <string>
26#include <type_traits>
29class TComplex {
32 Double_t fRe; // real part
33 Double_t fIm; // imaginary part
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() {}
41 // constants
42 static TComplex I() {return TComplex(0,1);}
43 static TComplex One() {return TComplex(1,0);}
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; }
55 // Simple operators complex - complex
57 {return TComplex(fRe*c.fRe-fIm*c.fIm,fRe*c.fIm+fIm*c.fRe);}
59 {return TComplex(fRe+c.fRe, fIm+c.fIm);}
61 {return TComplex(fRe*c.fRe+fIm*c.fIm,-fRe*c.fIm+fIm*c.fRe)/c.Rho2();}
63 {return TComplex(fRe-c.fRe, fIm-c.fIm);}
66 {return ((*this) = (*this) * c);}
68 {return ((*this) = (*this) + c);}
70 {return ((*this) = (*this) / c);}
72 {return ((*this) = (*this) - c);}
75 {return TComplex(-fRe,-fIm);}
77 {return *this;}
79 // Metafunction to figure out whether a type is arithmetic. This is used for
80 // the arithmetic operator implementation, so we can be sure that nothing
81 // unexpected will happen for non-arithmetic types that implement these
82 // operators with `double` themselves.
83 template<class T>
84 using enable_if_arithmetic = typename std::enable_if<std::is_arithmetic<T>::value, bool>::type;
86 // Simple operators complex - arithmetic
87 template <class T, enable_if_arithmetic<T> = true>
88 TComplex operator *(T c) const { return {fRe*c,fIm*c}; }
89 template <class T, enable_if_arithmetic<T> = true>
90 TComplex operator +(T c) const { return {fRe+c, fIm}; }
91 template <class T, enable_if_arithmetic<T> = true>
92 TComplex operator /(T c) const { return {fRe/c,fIm/c}; }
93 template <class T, enable_if_arithmetic<T> = true>
94 TComplex operator -(T c) const { return {fRe-c, fIm}; }
96 // Simple operators arithmetic - complex
97 template <class T, enable_if_arithmetic<T> = true>
98 friend TComplex operator *(T d, const TComplex & c) { return {d*c.fRe,d*c.fIm}; }
99 template <class T, enable_if_arithmetic<T> = true>
100 friend TComplex operator +(T d, const TComplex & c) { return {d+c.fRe, c.fIm}; }
101 template <class T, enable_if_arithmetic<T> = true>
102 friend TComplex operator /(T d, const TComplex & c) { return TComplex{d*c.fRe,-d*c.fIm} / c.Rho2(); }
103 template <class T, enable_if_arithmetic<T> = true>
104 friend TComplex operator -(T d, const TComplex & c) { return {d-c.fRe, -c.fIm}; }
106 // Convertors
107 operator Double_t () const {return fRe;}
108 operator Float_t () const {return static_cast<Float_t>(fRe);}
109 operator Int_t () const {return static_cast<Int_t>(fRe);}
111 // TMath:: extensions
112 static TComplex Sqrt(const TComplex &c)
113 {return TComplex(TMath::Sqrt(c.Rho()),0.5*c.Theta(),kTRUE);}
115 static TComplex Exp(const TComplex &c)
116 {return TComplex(TMath::Exp(c.fRe),c.fIm,kTRUE);}
117 static TComplex Log(const TComplex &c)
118 {return TComplex(0.5*TMath::Log(c.Rho2()),c.Theta());}
119 static TComplex Log2(const TComplex &c)
120 {return Log(c)/TMath::Log(2);}
121 static TComplex Log10(const TComplex &c)
122 {return Log(c)/TMath::Log(10);}
124 static TComplex Sin(const TComplex &c)
125 {return TComplex(TMath::Sin(c.fRe)*TMath::CosH(c.fIm),
126 TMath::Cos(c.fRe)*TMath::SinH(c.fIm));}
127 static TComplex Cos(const TComplex &c)
128 {return TComplex(TMath::Cos(c.fRe)*TMath::CosH(c.fIm),
129 -TMath::Sin(c.fRe)*TMath::SinH(c.fIm));}
130 static TComplex Tan(const TComplex &c)
131 {TComplex cc=Cos(c); return Sin(c)*Conjugate(cc)/cc.Rho2();}
133 static TComplex ASin(const TComplex &c)
134 {return -I()*Log(I()*c+TMath::Sign(1.,c.Im())*Sqrt(1.-c*c));}
135 static TComplex ACos(const TComplex &c)
136 {return -I()*Log(c+TMath::Sign(1.,c.Im())*Sqrt(c*c-1.));}
137 static TComplex ATan(const TComplex &c)
138 {return -0.5*I()*Log((1.+I()*c)/(1.-I()*c));}
140 static TComplex SinH(const TComplex &c)
141 {return TComplex(TMath::SinH(c.fRe)*TMath::Cos(c.fIm),
142 TMath::CosH(c.fRe)*TMath::Sin(c.fIm));}
143 static TComplex CosH(const TComplex &c)
144 {return TComplex(TMath::CosH(c.fRe)*TMath::Cos(c.fIm),
145 TMath::SinH(c.fRe)*TMath::Sin(c.fIm));}
146 static TComplex TanH(const TComplex &c)
147 {TComplex cc=CosH(c); return SinH(c)*Conjugate(cc)/cc.Rho2();}
149 static TComplex ASinH(const TComplex &c)
150 {return Log(c+TMath::Sign(1.,c.Im())*Sqrt(c*c+1.));}
151 static TComplex ACosH(const TComplex &c)
152 {return Log(c+TMath::Sign(1.,c.Im())*Sqrt(c*c-1.));}
153 static TComplex ATanH(const TComplex &c)
154 {return 0.5*Log((1.+c)/(1.-c));}
156 static Double_t Abs(const TComplex &c)
157 {return c.Rho();}
159 static TComplex Power(const TComplex& x, const TComplex& y)
160 {Double_t lrho=TMath::Log(x.Rho());
161 Double_t theta=x.Theta();
162 return TComplex(TMath::Exp(lrho*y.Re()-theta*y.Im()),
163 lrho*y.Im()+theta*y.Re(),kTRUE);}
165 {return TComplex(TMath::Power(x.Rho(),y),x.Theta()*y,kTRUE);}
168 Double_t theta=(x>0)?0:TMath::Pi();
169 return TComplex(TMath::Exp(lrho*y.Re()-theta*y.Im()),
170 lrho*y.Im()+theta*y.Re(),kTRUE);}
171 static TComplex Power(const TComplex& x, Int_t y)
172 {return TComplex(TMath::Power(x.Rho(),y),x.Theta()*y,kTRUE);}
174 static Int_t Finite(const TComplex& c)
175 {return TMath::Min(TMath::Finite(c.Re()),TMath::Finite(c.Im()));}
176 static Int_t IsNaN(const TComplex& c)
177 {return TMath::IsNaN(c.Re()) || TMath::IsNaN(c.Im());}
179 static TComplex Min(const TComplex &a, const TComplex &b)
180 {return a.Rho()<=b.Rho()?a:b;}
181 static TComplex Max(const TComplex &a, const TComplex &b)
182 {return a.Rho()>=b.Rho()?a:b;}
184 {return TComplex(1.,c.Theta(),kTRUE);}
186 {return TComplex(c.Re(),-c.Im());}
187 static TComplex Range(const TComplex &lb, const TComplex &ub, const TComplex &c)
188 {return Max(lb,Min(c,ub));}
190 // I/O
191 friend std::ostream& operator<<(std::ostream& out, const TComplex& c);
192 friend std::istream& operator>>(std::istream& in, TComplex& c);
194 ClassDef(TComplex,1) //Complex Class
197namespace cling {
198std::string printValue(TComplex *c);
#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
bool Bool_t
Definition RtypesCore.h:63
int Int_t
Definition RtypesCore.h:45
float Float_t
Definition RtypesCore.h:57
constexpr Bool_t kFALSE
Definition RtypesCore.h:101
double Double_t
Definition RtypesCore.h:59
constexpr Bool_t kTRUE
Definition RtypesCore.h:100
#define ClassDef(name, id)
Definition Rtypes.h:337
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t Int_t Int_t Window_t TString Int_t GCValues_t GetPrimarySelectionOwner GetDisplay GetScreen GetColormap GetNativeEvent const char const char dpyName wid window const char font_name cursor keysym reg const char only_if_exist regb h Point_t winding char text const char depth char const char Int_t count const char ColorStruct_t color const char Pixmap_t Pixmap_t PictureAttributes_t attr const char char ret_data h unsigned char height h Atom_t Int_t ULong_t ULong_t unsigned char prop_list Atom_t Atom_t Atom_t Time_t type
static TComplex Max(const TComplex &a, const TComplex &b)
Definition TComplex.h:181
Double_t fRe
Definition TComplex.h:32
Double_t Im() const
Definition TComplex.h:47
virtual ~TComplex()
Definition TComplex.h:39
Double_t fIm
Definition TComplex.h:33
static TComplex Power(const TComplex &x, Double_t y)
Definition TComplex.h:164
static TComplex ACos(const TComplex &c)
Definition TComplex.h:135
static TComplex Power(Double_t x, const TComplex &y)
Definition TComplex.h:166
static TComplex I()
Definition TComplex.h:42
friend std::istream & operator>>(std::istream &in, TComplex &c)
Definition TComplex.cxx:49
TComplex operator+()
Definition TComplex.h:76
friend TComplex operator*(T d, const TComplex &c)
Definition TComplex.h:98
Double_t Re() const
Definition TComplex.h:46
static TComplex ATan(const TComplex &c)
Definition TComplex.h:137
static TComplex Log2(const TComplex &c)
Definition TComplex.h:119
static TComplex Sqrt(const TComplex &c)
Definition TComplex.h:112
TComplex operator-()
Definition TComplex.h:74
TComplex operator/=(const TComplex &c)
Definition TComplex.h:69
TComplex operator*=(const TComplex &c)
Definition TComplex.h:65
static TComplex CosH(const TComplex &c)
Definition TComplex.h:143
static TComplex ACosH(const TComplex &c)
Definition TComplex.h:151
TComplex operator-=(const TComplex &c)
Definition TComplex.h:71
static TComplex Cos(const TComplex &c)
Definition TComplex.h:127
static TComplex Power(const TComplex &x, const TComplex &y)
Definition TComplex.h:159
static TComplex Log(const TComplex &c)
Definition TComplex.h:117
Double_t Rho2() const
Definition TComplex.h:49
friend std::ostream & operator<<(std::ostream &out, const TComplex &c)
Definition TComplex.cxx:41
static TComplex Sin(const TComplex &c)
Definition TComplex.h:124
static TComplex One()
Definition TComplex.h:43
static TComplex Conjugate(const TComplex &c)
Definition TComplex.h:185
Double_t Theta() const
Definition TComplex.h:50
TComplex operator+=(const TComplex &c)
Definition TComplex.h:67
static TComplex SinH(const TComplex &c)
Definition TComplex.h:140
static TComplex TanH(const TComplex &c)
Definition TComplex.h:146
static TComplex ASin(const TComplex &c)
Definition TComplex.h:133
static TComplex Log10(const TComplex &c)
Definition TComplex.h:121
friend TComplex operator/(T d, const TComplex &c)
Definition TComplex.h:102
static TComplex Range(const TComplex &lb, const TComplex &ub, const TComplex &c)
Definition TComplex.h:187
static Int_t Finite(const TComplex &c)
Definition TComplex.h:174
static TComplex Tan(const TComplex &c)
Definition TComplex.h:130
Double_t Rho() const
Definition TComplex.h:48
static TComplex Normalize(const TComplex &c)
Definition TComplex.h:183
static TComplex Min(const TComplex &a, const TComplex &b)
Definition TComplex.h:179
static TComplex Power(const TComplex &x, Int_t y)
Definition TComplex.h:171
static TComplex Exp(const TComplex &c)
Definition TComplex.h:115
static Int_t IsNaN(const TComplex &c)
Definition TComplex.h:176
static Double_t Abs(const TComplex &c)
Definition TComplex.h:156
TComplex operator()(Double_t x, Double_t y, Bool_t polar=kFALSE)
Definition TComplex.h:51
static TComplex ASinH(const TComplex &c)
Definition TComplex.h:149
Definition TComplex.h:37
typename std::enable_if< std::is_arithmetic< T >::value, bool >::type enable_if_arithmetic
Definition TComplex.h:84
static TComplex ATanH(const TComplex &c)
Definition TComplex.h:153
Double_t y[n]
Definition legend1.C:17
Double_t x[n]
Definition legend1.C:17
Double_t CosH(Double_t)
Returns the hyperbolic cosine of x.
Definition TMath.h:612
Bool_t IsNaN(Double_t x)
Definition TMath.h:892
Double_t Exp(Double_t x)
Returns the base-e exponential function of x, which is e raised to the power x.
Definition TMath.h:709
T1 Sign(T1 a, T2 b)
Returns a value with the magnitude of a and the sign of b.
Definition TMathBase.h:175
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 y, Double_t x)
Returns the principal value of the arc tangent of y/x, expressed in radians.
Definition TMath.h:646
Double_t Log(Double_t x)
Returns the natural logarithm of x.
Definition TMath.h:756
Double_t Sqrt(Double_t x)
Returns the square root of x.
Definition TMath.h:662
LongDouble_t Power(LongDouble_t x, LongDouble_t y)
Returns x raised to the power y.
Definition TMath.h:721
Short_t Min(Short_t a, Short_t b)
Returns the smallest of a and b.
Definition TMathBase.h:198
Double_t Cos(Double_t)
Returns the cosine of an angle of x radians.
Definition TMath.h:594
constexpr Double_t Pi()
Definition TMath.h:37
Double_t Sin(Double_t)
Returns the sine of an angle of x radians.
Definition TMath.h:588
Short_t Abs(Short_t d)
Returns the absolute value of parameter Short_t d.
Definition TMathBase.h:123
Double_t SinH(Double_t)
Returns the hyperbolic sine of `x.
Definition TMath.h:606