|
Rcpp Version 0.9.10
|
00001 // -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; tab-width: 8 -*- 00002 // 00003 // complex.cpp : Rcpp R/C++ interface class library -- complex binary operators 00004 // 00005 // Copyright (C) 2010 - 2011 Dirk Eddelbuettel and Romain Francois 00006 // 00007 // This file is part of Rcpp. 00008 // 00009 // Rcpp is free software: you can redistribute it and/or modify it 00010 // under the terms of the GNU General Public License as published by 00011 // the Free Software Foundation, either version 2 of the License, or 00012 // (at your option) any later version. 00013 // 00014 // Rcpp is distributed in the hope that it will be useful, but 00015 // WITHOUT ANY WARRANTY; without even the implied warranty of 00016 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00017 // GNU General Public License for more details. 00018 // 00019 // You should have received a copy of the GNU General Public License 00020 // along with Rcpp. If not, see <http://www.gnu.org/licenses/>. 00021 00022 #include <RcppCommon.h> 00023 00024 Rcomplex operator*( const Rcomplex& lhs, const Rcomplex& rhs){ 00025 Rcomplex y ; 00026 y.r = lhs.r * rhs.r - lhs.i * rhs.i ; 00027 y.i = lhs.r * rhs.i + rhs.r * lhs.i ; 00028 return y ; 00029 } 00030 Rcomplex operator+( const Rcomplex& lhs, const Rcomplex& rhs){ 00031 Rcomplex y ; 00032 y.r = lhs.r + rhs.r ; 00033 y.i = lhs.i + rhs.i ; 00034 return y ; 00035 } 00036 00037 Rcomplex operator-( const Rcomplex& lhs, const Rcomplex& rhs){ 00038 Rcomplex y ; 00039 y.r = lhs.r - rhs.r ; 00040 y.i = lhs.i - rhs.i ; 00041 return y ; 00042 } 00043 00044 Rcomplex operator/( const Rcomplex& a, const Rcomplex& b){ 00045 00046 Rcomplex c ; 00047 double ratio, den; 00048 double abr, abi; 00049 00050 if( (abr = b.r) < 0) 00051 abr = - abr; 00052 if( (abi = b.i) < 0) 00053 abi = - abi; 00054 if( abr <= abi ) { 00055 ratio = b.r / b.i ; 00056 den = b.i * (1 + ratio*ratio); 00057 c.r = (a.r*ratio + a.i) / den; 00058 c.i = (a.i*ratio - a.r) / den; 00059 } 00060 else { 00061 ratio = b.i / b.r ; 00062 den = b.r * (1 + ratio*ratio); 00063 c.r = (a.r + a.i*ratio) / den; 00064 c.i = (a.i - a.r*ratio) / den; 00065 } 00066 return c ; 00067 00068 } 00069