[Rcpp-commits] r1995 - in pkg/Rcpp/inst: . include/Rcpp/sugar/functions unitTests

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Fri Aug 13 09:06:23 CEST 2010


Author: romain
Date: 2010-08-13 09:06:22 +0200 (Fri, 13 Aug 2010)
New Revision: 1995

Added:
   pkg/Rcpp/inst/include/Rcpp/sugar/functions/complex.h
Removed:
   pkg/Rcpp/inst/include/Rcpp/sugar/functions/Conj.h
   pkg/Rcpp/inst/include/Rcpp/sugar/functions/Im.h
   pkg/Rcpp/inst/include/Rcpp/sugar/functions/Mod.h
   pkg/Rcpp/inst/include/Rcpp/sugar/functions/Re.h
Modified:
   pkg/Rcpp/inst/ChangeLog
   pkg/Rcpp/inst/include/Rcpp/sugar/functions/functions.h
   pkg/Rcpp/inst/unitTests/runit.sugar.R
Log:
rework complex functions (Re, Im, Mod, Conj)

Modified: pkg/Rcpp/inst/ChangeLog
===================================================================
--- pkg/Rcpp/inst/ChangeLog	2010-08-12 15:42:52 UTC (rev 1994)
+++ pkg/Rcpp/inst/ChangeLog	2010-08-13 07:06:22 UTC (rev 1995)
@@ -1,3 +1,11 @@
+2010-08-13  Romain Francois <romain at r-enthusiasts.com>
+
+	* inst/include/Rcpp/sugar/complex.h: simplify complex sugar functions
+	Re, Im, Conj, Mod
+	
+	* inst/unitTests/runit.sugar.R: added regression test for complex functions
+	which did not handle NA properly before
+
 2010-08-12  Romain Francois <romain at r-enthusiasts.com>
 
 	* inst/include/Rcpp/sugar/math.h: replace previous versions of floor,

Deleted: pkg/Rcpp/inst/include/Rcpp/sugar/functions/Conj.h
===================================================================
--- pkg/Rcpp/inst/include/Rcpp/sugar/functions/Conj.h	2010-08-12 15:42:52 UTC (rev 1994)
+++ pkg/Rcpp/inst/include/Rcpp/sugar/functions/Conj.h	2010-08-13 07:06:22 UTC (rev 1995)
@@ -1,55 +0,0 @@
-// -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; tab-width: 8 -*-
-//
-// Conj.h: Rcpp R/C++ interface class library -- Conj
-//
-// 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__sugar__Conj_h
-#define Rcpp__sugar__Conj_h
-
-namespace Rcpp{
-namespace sugar{
-
-template <bool NA, typename T>
-class Conj : public Rcpp::VectorBase< CPLXSXP,NA, Conj<NA,T> > {
-public:
-	typedef typename Rcpp::VectorBase<CPLXSXP,NA,T> VEC_TYPE ;
-	
-	Conj( const VEC_TYPE& object_ ) : object(object_){}
-	
-	inline Rcomplex operator[]( int i ) const {
-		Rcomplex x = object[i] ;
-		x.i = - x.i ;
-		return x ;
-	}
-	inline int size() const { return object.size() ; }
-	         
-private:
-	const VEC_TYPE& object ;
-} ;
-	
-} // sugar
-
-template <bool NA, typename T>
-inline sugar::Conj<NA,T> Conj( const VectorBase<CPLXSXP,NA,T>& t){
-	return sugar::Conj<NA,T>( t ) ;
-}
-
-} // Rcpp
-#endif
-

Deleted: pkg/Rcpp/inst/include/Rcpp/sugar/functions/Im.h
===================================================================
--- pkg/Rcpp/inst/include/Rcpp/sugar/functions/Im.h	2010-08-12 15:42:52 UTC (rev 1994)
+++ pkg/Rcpp/inst/include/Rcpp/sugar/functions/Im.h	2010-08-13 07:06:22 UTC (rev 1995)
@@ -1,54 +0,0 @@
-// -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; tab-width: 8 -*-
-//
-// Im.h: Rcpp R/C++ interface class library -- Im
-//
-// Copyright (C) 2010 Dirk Eddelbuettel and Romain Francois
-//
-// This file is part of Rcpp.
-//
-// Rcpp is fIme softwaIm: you can Imdistribute it and/or modify it
-// under the terms of the GNU General Public License as published by
-// the FIme SoftwaIm 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 moIm details.
-//
-// You should have Imceived a copy of the GNU General Public License
-// along with Rcpp.  If not, see <http://www.gnu.org/licenses/>.
-
-#ifndef Rcpp__sugar__Im_h
-#define Rcpp__sugar__Im_h
-
-namespace Rcpp{
-namespace sugar{
-
-template <bool NA, typename T>
-class Im : public Rcpp::VectorBase< REALSXP,NA, Im<NA,T> > {
-public:
-	typedef typename Rcpp::VectorBase<CPLXSXP,NA,T> VEC_TYPE ;
-	
-	Im( const VEC_TYPE& object_ ) : object(object_){}
-	
-	inline double operator[]( int i ) const {
-		Rcomplex x = object[i] ;
-		return x.i ;
-	}
-	inline int size() const { return object.size() ; }
-	         
-private:
-	const VEC_TYPE& object ;
-} ;
-	
-} // sugar
-
-template <bool NA, typename T>
-inline sugar::Im<NA,T> Im( const VectorBase<CPLXSXP,NA,T>& t){
-	return sugar::Im<NA,T>( t ) ;
-}
-
-} // Rcpp
-#endif
-

Deleted: pkg/Rcpp/inst/include/Rcpp/sugar/functions/Mod.h
===================================================================
--- pkg/Rcpp/inst/include/Rcpp/sugar/functions/Mod.h	2010-08-12 15:42:52 UTC (rev 1994)
+++ pkg/Rcpp/inst/include/Rcpp/sugar/functions/Mod.h	2010-08-13 07:06:22 UTC (rev 1995)
@@ -1,54 +0,0 @@
-// -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; tab-width: 8 -*-
-//
-// Mod.h: Rcpp R/C++ interface class library -- Mod
-//
-// 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__sugar__Mod_h
-#define Rcpp__sugar__Mod_h
-
-namespace Rcpp{
-namespace sugar{
-
-template <bool NA, typename T>
-class Mod : public Rcpp::VectorBase< REALSXP,NA, Mod<NA,T> > {
-public:
-	typedef typename Rcpp::VectorBase<CPLXSXP,NA,T> VEC_TYPE ;
-	
-	Mod( const VEC_TYPE& object_ ) : object(object_){}
-	
-	inline double operator[]( int i ) const {
-		Rcomplex x = object[i] ;
-		return ::sqrt( x.i * x.i + x.r * x.r ); 
-	}
-	inline int size() const { return object.size() ; }
-	         
-private:
-	const VEC_TYPE& object ;
-} ;
-	
-} // sugar
-
-template <bool NA, typename T>
-inline sugar::Mod<NA,T> Mod( const VectorBase<CPLXSXP,NA,T>& t){
-	return sugar::Mod<NA,T>( t ) ;
-}
-
-} // Rcpp
-#endif
-

Deleted: pkg/Rcpp/inst/include/Rcpp/sugar/functions/Re.h
===================================================================
--- pkg/Rcpp/inst/include/Rcpp/sugar/functions/Re.h	2010-08-12 15:42:52 UTC (rev 1994)
+++ pkg/Rcpp/inst/include/Rcpp/sugar/functions/Re.h	2010-08-13 07:06:22 UTC (rev 1995)
@@ -1,54 +0,0 @@
-// -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; tab-width: 8 -*-
-//
-// Re.h: Rcpp R/C++ interface class library -- re
-//
-// 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__sugar__Re_h
-#define Rcpp__sugar__Re_h
-
-namespace Rcpp{
-namespace sugar{
-
-template <bool NA, typename T>
-class Re : public Rcpp::VectorBase< REALSXP,NA, Re<NA,T> > {
-public:
-	typedef typename Rcpp::VectorBase<CPLXSXP,NA,T> VEC_TYPE ;
-	
-	Re( const VEC_TYPE& object_ ) : object(object_){}
-	
-	inline double operator[]( int i ) const {
-		Rcomplex x = object[i] ;
-		return x.r ;
-	}
-	inline int size() const { return object.size() ; }
-	         
-private:
-	const VEC_TYPE& object ;
-} ;
-	
-} // sugar
-
-template <bool NA, typename T>
-inline sugar::Re<NA,T> Re( const VectorBase<CPLXSXP,NA,T>& t){
-	return sugar::Re<NA,T>( t ) ;
-}
-
-} // Rcpp
-#endif
-

Added: pkg/Rcpp/inst/include/Rcpp/sugar/functions/complex.h
===================================================================
--- pkg/Rcpp/inst/include/Rcpp/sugar/functions/complex.h	                        (rev 0)
+++ pkg/Rcpp/inst/include/Rcpp/sugar/functions/complex.h	2010-08-13 07:06:22 UTC (rev 1995)
@@ -0,0 +1,87 @@
+// -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; tab-width: 8 -*-
+//
+// Mod.h: Rcpp R/C++ interface class library -- Mod
+//
+// 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__sugar__Mod_h
+#define Rcpp__sugar__Mod_h
+
+namespace Rcpp{
+namespace sugar{
+
+	
+template <bool NA, typename OUT, typename T, typename FunPtr>
+class SugarComplex : public Rcpp::VectorBase< 
+	Rcpp::traits::r_sexptype_traits<OUT>::rtype , 
+	NA, 
+	SugarComplex<NA,OUT,T,FunPtr>
+	> {
+public:
+	
+	typedef Rcpp::VectorBase<CPLXSXP,NA,T> VEC_TYPE ;
+	
+	SugarComplex( FunPtr ptr_, const VEC_TYPE & vec_) : ptr(ptr_), vec(vec_){}
+	
+	inline OUT operator[]( int i) const { 
+		Rcomplex x = vec[i] ;
+		if( Rcpp::traits::is_na<CPLXSXP>( x ) ) 
+			return Rcpp::traits::get_na< Rcpp::traits::r_sexptype_traits<OUT>::rtype >() ;
+		return ptr( x ); 
+	}
+	inline int size() const { return vec.size() ; }
+	
+private:
+	FunPtr ptr ;
+	const VEC_TYPE& vec ;
+};
+} // sugar
+
+namespace internal{
+inline double complex__Re( Rcomplex x){ return x.r ; }
+	inline double complex__Im( Rcomplex x){ return x.i ; }
+	inline double complex__Mod( Rcomplex x){ return ::sqrt( x.i * x.i + x.r * x.r) ; }
+	inline Rcomplex complex__Conj( Rcomplex x){
+		Rcomplex y ;
+		y.r = x.r; 
+		y.i = -x.i ;
+		return y ;
+	}
+} // internal
+
+#define RCPP_SUGAR_COMPLEX(__NAME__,__OUT__)                                \
+	template <bool NA, typename T>                                          \
+	inline sugar::SugarComplex<NA,__OUT__,T, __OUT__ (*)(Rcomplex) >        \
+	__NAME__(                                                               \
+		const VectorBase<CPLXSXP,NA,T>& t                                   \
+		){                                                                  \
+		return sugar::SugarComplex<NA,__OUT__,T, __OUT__ (*)(Rcomplex) >(    \
+			internal::complex__##__NAME__, t                                \
+		) ;                                                                 \
+	}
+
+RCPP_SUGAR_COMPLEX( Re, double )
+RCPP_SUGAR_COMPLEX( Im, double )
+RCPP_SUGAR_COMPLEX( Mod, double )
+RCPP_SUGAR_COMPLEX( Conj, Rcomplex )
+
+#undef RCPP_SUGAR_COMPLEX	 
+	
+} // Rcpp
+#endif
+

Modified: pkg/Rcpp/inst/include/Rcpp/sugar/functions/functions.h
===================================================================
--- pkg/Rcpp/inst/include/Rcpp/sugar/functions/functions.h	2010-08-12 15:42:52 UTC (rev 1994)
+++ pkg/Rcpp/inst/include/Rcpp/sugar/functions/functions.h	2010-08-13 07:06:22 UTC (rev 1995)
@@ -29,6 +29,8 @@
 #include <Rcpp/sugar/functions/SugarMath.h>
 #include <Rcpp/sugar/functions/math.h>
 
+#include <Rcpp/sugar/functions/complex.h>
+
 #include <Rcpp/sugar/functions/any.h>
 #include <Rcpp/sugar/functions/all.h>
 #include <Rcpp/sugar/functions/is_na.h>
@@ -50,9 +52,5 @@
 
 #include <Rcpp/sugar/functions/sum.h>
 
-#include <Rcpp/sugar/functions/Re.h>
-#include <Rcpp/sugar/functions/Im.h>
-#include <Rcpp/sugar/functions/Conj.h>
-#include <Rcpp/sugar/functions/Mod.h>
 
 #endif

Modified: pkg/Rcpp/inst/unitTests/runit.sugar.R
===================================================================
--- pkg/Rcpp/inst/unitTests/runit.sugar.R	2010-08-12 15:42:52 UTC (rev 1994)
+++ pkg/Rcpp/inst/unitTests/runit.sugar.R	2010-08-13 07:06:22 UTC (rev 1995)
@@ -1065,7 +1065,7 @@
 
 
 test.sugar.complex <- function( ){
-	x <- rnorm(10) + 1i*rnorm(10)
+	x <- c( rnorm(10), NA ) + 1i*c( rnorm(10), NA )
 	fx <- .rcpp.sugar$runit_complex
 	checkEquals( fx(x), list( 
 		Re = Re( x ), 



More information about the Rcpp-commits mailing list