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

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Mon Jun 28 16:37:20 CEST 2010


Author: romain
Date: 2010-06-28 16:37:19 +0200 (Mon, 28 Jun 2010)
New Revision: 1759

Added:
   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/DESCRIPTION
   pkg/Rcpp/TODO
   pkg/Rcpp/inst/ChangeLog
   pkg/Rcpp/inst/include/Rcpp/sugar/functions/functions.h
   pkg/Rcpp/inst/unitTests/runit.sugar.R
Log:
sugar functions for complex vectors : Re, Im, Mod, Conj

Modified: pkg/Rcpp/DESCRIPTION
===================================================================
--- pkg/Rcpp/DESCRIPTION	2010-06-28 08:00:06 UTC (rev 1758)
+++ pkg/Rcpp/DESCRIPTION	2010-06-28 14:37:19 UTC (rev 1759)
@@ -1,6 +1,6 @@
 Package: Rcpp
 Title: Rcpp R/C++ interface package
-Version: 0.8.3
+Version: 0.8.3.1
 Date: $Date$
 Author: Dirk Eddelbuettel and Romain Francois, with contributions 
  by Simon Urbanek, David Reiss and Douglas Bates; based on code written during 

Modified: pkg/Rcpp/TODO
===================================================================
--- pkg/Rcpp/TODO	2010-06-28 08:00:06 UTC (rev 1758)
+++ pkg/Rcpp/TODO	2010-06-28 14:37:19 UTC (rev 1759)
@@ -50,9 +50,7 @@
     o	operator% 
     
     o	operator/ needs to handle the case of division by 0
-    
-    o   for complex vectors: Re, Im, Mod, Arg, Conj
-    
+
     o   min, max with specialization of the binary operators, so that we can do 
         things like this lazily: 
         

Modified: pkg/Rcpp/inst/ChangeLog
===================================================================
--- pkg/Rcpp/inst/ChangeLog	2010-06-28 08:00:06 UTC (rev 1758)
+++ pkg/Rcpp/inst/ChangeLog	2010-06-28 14:37:19 UTC (rev 1759)
@@ -1,3 +1,12 @@
+
+2010-06-28  Romain Francois <romain at r-enthusiasts.com>
+
+	* inst/include/Rcpp/sugar/Im.h:
+	* inst/include/Rcpp/sugar/Re.h:
+	* inst/include/Rcpp/sugar/Conj.h:
+	* inst/include/Rcpp/sugar/Mod.h: added sugar functions for
+	complex vectors: Re, Im, Conj, Mod
+
 2010-06-27  Dirk Eddelbuettel  <edd at debian.org>
 
 	* DESCRIPTION: Release 0.8.3

Added: pkg/Rcpp/inst/include/Rcpp/sugar/functions/Conj.h
===================================================================
--- pkg/Rcpp/inst/include/Rcpp/sugar/functions/Conj.h	                        (rev 0)
+++ pkg/Rcpp/inst/include/Rcpp/sugar/functions/Conj.h	2010-06-28 14:37:19 UTC (rev 1759)
@@ -0,0 +1,55 @@
+// -*- 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
+

Added: pkg/Rcpp/inst/include/Rcpp/sugar/functions/Im.h
===================================================================
--- pkg/Rcpp/inst/include/Rcpp/sugar/functions/Im.h	                        (rev 0)
+++ pkg/Rcpp/inst/include/Rcpp/sugar/functions/Im.h	2010-06-28 14:37:19 UTC (rev 1759)
@@ -0,0 +1,54 @@
+// -*- 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
+

Added: pkg/Rcpp/inst/include/Rcpp/sugar/functions/Mod.h
===================================================================
--- pkg/Rcpp/inst/include/Rcpp/sugar/functions/Mod.h	                        (rev 0)
+++ pkg/Rcpp/inst/include/Rcpp/sugar/functions/Mod.h	2010-06-28 14:37:19 UTC (rev 1759)
@@ -0,0 +1,54 @@
+// -*- 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
+

Added: pkg/Rcpp/inst/include/Rcpp/sugar/functions/Re.h
===================================================================
--- pkg/Rcpp/inst/include/Rcpp/sugar/functions/Re.h	                        (rev 0)
+++ pkg/Rcpp/inst/include/Rcpp/sugar/functions/Re.h	2010-06-28 14:37:19 UTC (rev 1759)
@@ -0,0 +1,54 @@
+// -*- 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
+

Modified: pkg/Rcpp/inst/include/Rcpp/sugar/functions/functions.h
===================================================================
--- pkg/Rcpp/inst/include/Rcpp/sugar/functions/functions.h	2010-06-28 08:00:06 UTC (rev 1758)
+++ pkg/Rcpp/inst/include/Rcpp/sugar/functions/functions.h	2010-06-28 14:37:19 UTC (rev 1759)
@@ -39,4 +39,10 @@
 #include <Rcpp/sugar/functions/ceil.h>
 #include <Rcpp/sugar/functions/pow.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-06-28 08:00:06 UTC (rev 1758)
+++ pkg/Rcpp/inst/unitTests/runit.sugar.R	2010-06-28 14:37:19 UTC (rev 1759)
@@ -463,6 +463,18 @@
 					e["foo"] = xx < yy  ;
 					return R_NilValue ;
 				'
+			), 
+			"runit_complex" = list( 
+				signature(x = "complex" ), 
+				'
+					ComplexVector cx( x );
+					return List::create( 
+						_["Re"] = Re( cx ), 
+						_["Im"] = Im( cx ), 
+						_["Conj"] = Conj( cx ), 
+						_["Mod"] = Mod( cx )
+						) ;
+				'
 			)
 			
 		)
@@ -896,6 +908,17 @@
 	e <- new.env() 
 	fx( 1:10, 2:11, e )
 	checkEquals( e[["foo"]], rep(TRUE, 10 ) )
-	
 }
 
+
+test.sugar.complex <- function( ){
+	x <- rnorm(10) + 1i*rnorm(10)
+	fx <- .rcpp.sugar$runit_complex
+	checkEquals( fx(x), list( 
+		Re = Re( x ), 
+		Im = Im( x ), 
+		Conj = Conj(x), 
+		Mod = Mod(x)
+		)
+	)
+}



More information about the Rcpp-commits mailing list