-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathcomplex.h
More file actions
63 lines (53 loc) · 1.68 KB
/
Copy pathcomplex.h
File metadata and controls
63 lines (53 loc) · 1.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#ifndef RCPP__complex_H
#define RCPP__complex_H
inline Rcomplex& operator+=( Rcomplex& lhs, const Rcomplex& rhs ){
lhs.r += rhs.r ;
lhs.i += rhs.i ;
return lhs ;
}
inline Rcomplex operator*( const Rcomplex& lhs, const Rcomplex& rhs){
return Rcomplex { lhs.r * rhs.r - lhs.i * rhs.i, lhs.r * rhs.i + rhs.r * lhs.i };
}
inline Rcomplex operator+( const Rcomplex& lhs, const Rcomplex& rhs){
return Rcomplex { lhs.r + rhs.r, lhs.i + rhs.i } ;
}
inline Rcomplex operator-( const Rcomplex& lhs, const Rcomplex& rhs){
return Rcomplex { lhs.r - rhs.r, lhs.i - rhs.i } ;
}
inline Rcomplex operator-( const Rcomplex& lhs ){
return Rcomplex{ -lhs.r, -lhs.i } ;
}
inline bool operator!( const Rcomplex& lhs ){
return ( lhs.r != 0.0 || lhs.i != 0.0 ) ;
}
inline Rcomplex operator/( const Rcomplex& a, const Rcomplex& b){
double ratio, den;
double abr, abi;
if( (abr = b.r) < 0) abr = - abr;
if( (abi = b.i) < 0) abi = - abi;
if( abr <= abi ) {
ratio = b.r / b.i ;
den = b.i * (1 + ratio*ratio);
return Rcomplex{
(a.r*ratio + a.i) / den,
(a.i*ratio - a.r) / den
} ;
} else {
ratio = b.i / b.r ;
den = b.r * (1 + ratio*ratio);
return Rcomplex {
(a.r + a.i*ratio) / den,
(a.i - a.r*ratio) / den
} ;
}
}
inline Rcomplex operator/( Rcomplex a, double n){
return Rcomplex{ a.r / n, a.i / n } ;
}
inline bool operator==( const Rcomplex& a, const Rcomplex& b){
return a.r == b.r && a.i == b.i ;
}
inline std::ostream & operator<<(std::ostream &os, const Rcomplex& cplx ){
return os << cplx.r << "+" << cplx.i << "i" ;
}
#endif