[Rcpp-commits] r2048 - in pkg/Rcpp: . inst inst/include inst/include/Rcpp inst/unitTests src
noreply at r-forge.r-project.org
noreply at r-forge.r-project.org
Fri Aug 20 14:33:52 CEST 2010
Author: romain
Date: 2010-08-20 14:33:52 +0200 (Fri, 20 Aug 2010)
New Revision: 2048
Added:
pkg/Rcpp/inst/include/Rcpp/complex.h
pkg/Rcpp/src/complex.cpp
Modified:
pkg/Rcpp/TODO
pkg/Rcpp/inst/ChangeLog
pkg/Rcpp/inst/include/RcppCommon.h
pkg/Rcpp/inst/unitTests/runit.Vector.R
Log:
binary operators for complex
Modified: pkg/Rcpp/TODO
===================================================================
--- pkg/Rcpp/TODO 2010-08-19 09:12:48 UTC (rev 2047)
+++ pkg/Rcpp/TODO 2010-08-20 12:33:52 UTC (rev 2048)
@@ -49,8 +49,6 @@
Syntactic sugar
- o binary operators for Rcomplex. http://comments.gmane.org/gmane.comp.lang.r.rcpp/649
-
o recycling : binary operators and math functions of 2 or more arguments
need to recycle their arguments.
Modified: pkg/Rcpp/inst/ChangeLog
===================================================================
--- pkg/Rcpp/inst/ChangeLog 2010-08-19 09:12:48 UTC (rev 2047)
+++ pkg/Rcpp/inst/ChangeLog 2010-08-20 12:33:52 UTC (rev 2048)
@@ -1,3 +1,7 @@
+2010-08-20 Romain Francois <romain at r-enthusiasts.com>
+
+ * inst/include/Rcpp/complex.h : binary operators for Rcomplex : +,-,*,/
+
2010-08-15 Romain Francois <romain at r-enthusiasts.com>
* inst/include/Rcpp/sugar/SugarBlock.h: more control of the NA behavior
Added: pkg/Rcpp/inst/include/Rcpp/complex.h
===================================================================
--- pkg/Rcpp/inst/include/Rcpp/complex.h (rev 0)
+++ pkg/Rcpp/inst/include/Rcpp/complex.h 2010-08-20 12:33:52 UTC (rev 2048)
@@ -0,0 +1,31 @@
+// -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; tab-width: 4 -*-
+//
+// complex.h: Rcpp R/C++ interface class library -- binary operators for Rcomplex
+//
+// Copyright (C) 2010 Dirk Eddelbuettel and Romain Francois
+//
+// This file is part of Rcpp.
+//
+// Rcpp is free software: you can redistribute it and/or modify it
+// under the terms of the GNU General Public License as published by
+// the Free Software Foundation, either version 2 of the License, or
+// (at your option) any later version.
+//
+// Rcpp is distributed in the hope that it will be useful, but
+// WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with Rcpp. If not, see <http://www.gnu.org/licenses/>.
+
+#ifndef RCPP__complex_H
+#define RCPP__complex_H
+
+Rcomplex operator*( const Rcomplex& lhs, const Rcomplex& rhs) ;
+Rcomplex operator+( const Rcomplex& lhs, const Rcomplex& rhs) ;
+Rcomplex operator-( const Rcomplex& lhs, const Rcomplex& rhs) ;
+Rcomplex operator/( const Rcomplex& a, const Rcomplex& b) ;
+
+
+#endif
Modified: pkg/Rcpp/inst/include/RcppCommon.h
===================================================================
--- pkg/Rcpp/inst/include/RcppCommon.h 2010-08-19 09:12:48 UTC (rev 2047)
+++ pkg/Rcpp/inst/include/RcppCommon.h 2010-08-20 12:33:52 UTC (rev 2048)
@@ -113,6 +113,8 @@
#define R_2_12_0
#endif
+#include <Rcpp/complex.h>
+
// #ifdef BUILDING_DLL
// #define RcppExport extern "C" __declspec(dllexport)
// #else
Modified: pkg/Rcpp/inst/unitTests/runit.Vector.R
===================================================================
--- pkg/Rcpp/inst/unitTests/runit.Vector.R 2010-08-19 09:12:48 UTC (rev 2047)
+++ pkg/Rcpp/inst/unitTests/runit.Vector.R 2010-08-20 12:33:52 UTC (rev 2048)
@@ -560,6 +560,18 @@
) ;
return output ;
'
+ ), "complex_binary_sugar" = list(
+ signature( x = "complex", y = "complex" ),
+ '
+ ComplexVector xx(x), yy(y) ;
+ return List::create(
+ _["+"] = xx + yy,
+ _["-"] = xx - yy,
+ _["*"] = xx * yy,
+ _["/"] = xx / yy
+ ) ;
+
+ '
)
@@ -1165,4 +1177,19 @@
msg = "CharacterVector::create" )
}
+test.ComplexVector.binary.operators <- function(){
+ fun <- .rcpp.Vector$complex_binary_sugar
+ x <- (1+1i) * 1:10
+ y <- (2-3i) * 1:10
+
+ checkEquals(
+ fun(x, y),
+ list(
+ "+" = x + y,
+ "-" = x - y,
+ "*" = x * y,
+ "/" = x / y
+ ), msg = "complex binary operators" )
+}
+
Added: pkg/Rcpp/src/complex.cpp
===================================================================
--- pkg/Rcpp/src/complex.cpp (rev 0)
+++ pkg/Rcpp/src/complex.cpp 2010-08-20 12:33:52 UTC (rev 2048)
@@ -0,0 +1,69 @@
+// -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; tab-width: 8 -*-
+//
+// complex.cpp : Rcpp R/C++ interface class library -- complex binary operators
+//
+// Copyright (C) 2010 Dirk Eddelbuettel and Romain Francois
+//
+// This file is part of Rcpp.
+//
+// Rcpp is free software: you can redistribute it and/or modify it
+// under the terms of the GNU General Public License as published by
+// the Free Software Foundation, either version 2 of the License, or
+// (at your option) any later version.
+//
+// Rcpp is distributed in the hope that it will be useful, but
+// WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with Rcpp. If not, see <http://www.gnu.org/licenses/>.
+
+#include <RcppCommon.h>
+
+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 ;
+}
+Rcomplex operator+( const Rcomplex& lhs, const Rcomplex& rhs){
+ Rcomplex y ;
+ y.r = lhs.r + rhs.r ;
+ y.i = lhs.i + rhs.i ;
+ return y ;
+}
+
+Rcomplex operator-( const Rcomplex& lhs, const Rcomplex& rhs){
+ Rcomplex y ;
+ y.r = lhs.r - rhs.r ;
+ y.i = lhs.i - rhs.i ;
+ return y ;
+}
+
+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 ;
+
+}
+
More information about the Rcpp-commits
mailing list