Rcpp Version 1.0.9
complex.h
Go to the documentation of this file.
1 // -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; tab-width: 4 -*-
2 //
3 // complex.h: Rcpp R/C++ interface class library -- binary operators for Rcomplex
4 //
5 // Copyright (C) 2010 - 2015 Dirk Eddelbuettel and Romain Francois
6 //
7 // This file is part of Rcpp.
8 //
9 // Rcpp is free software: you can redistribute it and/or modify it
10 // under the terms of the GNU General Public License as published by
11 // the Free Software Foundation, either version 2 of the License, or
12 // (at your option) any later version.
13 //
14 // Rcpp is distributed in the hope that it will be useful, but
15 // WITHOUT ANY WARRANTY; without even the implied warranty of
16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 // GNU General Public License for more details.
18 //
19 // You should have received a copy of the GNU General Public License
20 // along with Rcpp. If not, see <http://www.gnu.org/licenses/>.
21 
22 #ifndef RCPP__complex_H
23 #define RCPP__complex_H
24 
25 inline Rcomplex operator*( const Rcomplex& lhs, const Rcomplex& rhs) {
26  Rcomplex y ;
27  y.r = lhs.r * rhs.r - lhs.i * rhs.i ;
28  y.i = lhs.r * rhs.i + rhs.r * lhs.i ;
29  return y ;
30 }
31 
32 inline Rcomplex operator+( const Rcomplex& lhs, const Rcomplex& rhs) {
33  Rcomplex y ;
34  y.r = lhs.r + rhs.r ;
35  y.i = lhs.i + rhs.i ;
36  return y ;
37 }
38 
39 inline Rcomplex operator-( const Rcomplex& lhs, const Rcomplex& rhs) {
40  Rcomplex y ;
41  y.r = lhs.r - rhs.r ;
42  y.i = lhs.i - rhs.i ;
43  return y ;
44 }
45 
46 inline Rcomplex operator/( const Rcomplex& a, const Rcomplex& b) {
47  Rcomplex c ;
48  double ratio, den;
49  double abr, abi;
50 
51  if( (abr = b.r) < 0) abr = - abr;
52  if( (abi = b.i) < 0) abi = - abi;
53  if( abr <= abi ) {
54  ratio = b.r / b.i ;
55  den = b.i * (1 + ratio*ratio);
56  c.r = (a.r*ratio + a.i) / den;
57  c.i = (a.i*ratio - a.r) / den;
58  } else {
59  ratio = b.i / b.r ;
60  den = b.r * (1 + ratio*ratio);
61  c.r = (a.r + a.i*ratio) / den;
62  c.i = (a.i - a.r*ratio) / den;
63  }
64  return c ;
65 
66 }
67 
68 inline bool operator==( const Rcomplex& a, const Rcomplex& b) {
69  return a.r == b.r && a.i == b.i ;
70 }
71 
72 // to prevent a redefinition error in dplyr (<= 0.4.3) which has the _same_
73 // definition of operator<<() for Rcomplex
74 #define dplyr_tools_complex_H
75 
76 inline std::ostream & operator<<(std::ostream &os, const Rcomplex& cplx) {
77  return os << cplx.r << "+" << cplx.i << "i" ;
78 }
79 
80 
81 #endif
Rcomplex operator-(const Rcomplex &lhs, const Rcomplex &rhs)
Definition: complex.h:39
Rcomplex operator+(const Rcomplex &lhs, const Rcomplex &rhs)
Definition: complex.h:32
std::ostream & operator<<(std::ostream &os, const Rcomplex &cplx)
Definition: complex.h:76
Rcomplex operator*(const Rcomplex &lhs, const Rcomplex &rhs)
Definition: complex.h:25
Rcomplex operator/(const Rcomplex &a, const Rcomplex &b)
Definition: complex.h:46
bool operator==(const Rcomplex &a, const Rcomplex &b)
Definition: complex.h:68