-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathcomplex.h
More file actions
53 lines (47 loc) · 1.17 KB
/
complex.h
File metadata and controls
53 lines (47 loc) · 1.17 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
#ifndef RCPP__complex_H
#define RCPP__complex_H
// Rcomplex support
inline Rcomplex operator*( const Rcomplex& lhs, const Rcomplex& rhs){
Rcomplex y ;
y.r = lhs.r * rhs.r - lhs.i * rhs.i ;
y.i = lhs.r * rhs.i + rhs.r * lhs.i ;
return y ;
}
inline Rcomplex operator+( const Rcomplex& lhs, const Rcomplex& rhs){
Rcomplex y ;
y.r = lhs.r + rhs.r ;
y.i = lhs.i + rhs.i ;
return y ;
}
inline Rcomplex operator-( const Rcomplex& lhs, const Rcomplex& rhs){
Rcomplex y ;
y.r = lhs.r - rhs.r ;
y.i = lhs.i - rhs.i ;
return y ;
}
inline Rcomplex operator/( const Rcomplex& a, const Rcomplex& b){
Rcomplex c ;
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);
c.r = (a.r*ratio + a.i) / den;
c.i = (a.i*ratio - a.r) / den;
}
else {
ratio = b.i / b.r ;
den = b.r * (1 + ratio*ratio);
c.r = (a.r + a.i*ratio) / den;
c.i = (a.i - a.r*ratio) / den;
}
return c ;
}
inline bool operator==( const Rcomplex& a, const Rcomplex& b){
return a.r == b.r && a.i == b.i ;
}
#endif