[Rcpp-commits] r4326 - in pkg/Rcpp: . inst inst/include/Rcpp/sugar/functions inst/include/Rcpp/traits inst/unitTests inst/unitTests/cpp

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Fri May 31 11:04:22 CEST 2013


Author: romain
Date: 2013-05-31 11:04:22 +0200 (Fri, 31 May 2013)
New Revision: 4326

Added:
   pkg/Rcpp/inst/include/Rcpp/sugar/functions/is_infinite.h
   pkg/Rcpp/inst/include/Rcpp/traits/is_infinite.h
Modified:
   pkg/Rcpp/ChangeLog
   pkg/Rcpp/inst/NEWS.Rd
   pkg/Rcpp/inst/include/Rcpp/sugar/functions/functions.h
   pkg/Rcpp/inst/include/Rcpp/traits/traits.h
   pkg/Rcpp/inst/unitTests/cpp/sugar.cpp
   pkg/Rcpp/inst/unitTests/runit.sugar.R
Log:
added is_infinite

Modified: pkg/Rcpp/ChangeLog
===================================================================
--- pkg/Rcpp/ChangeLog	2013-05-30 16:01:04 UTC (rev 4325)
+++ pkg/Rcpp/ChangeLog	2013-05-31 09:04:22 UTC (rev 4326)
@@ -1,3 +1,10 @@
+2013-05-31 Romain Francois <romain at r-enthusiasts.com>
+
+        * unitTests/runit.sugar.R : new test for is_infinite
+        * unitTests/cpp/sugar.cpp : new test for is_infinite
+        * include/Rcpp/sugar/functions/is_infinite.h : new function is_infinite
+        * include/Rcpp/traits/is_infinite.h : new function is_infinite
+        
 2013-05-30 Romain Francois <romain at r-enthusiasts.com>
 
         * include/Rcpp/sugar/functions/all.h : bug fixed for the NA=false case

Modified: pkg/Rcpp/inst/NEWS.Rd
===================================================================
--- pkg/Rcpp/inst/NEWS.Rd	2013-05-30 16:01:04 UTC (rev 4325)
+++ pkg/Rcpp/inst/NEWS.Rd	2013-05-31 09:04:22 UTC (rev 4326)
@@ -29,13 +29,13 @@
     \itemize{
       \item New function \code{na_omit} based on the StackOverflow thread
       http://stackoverflow.com/questions/15953768/templated-rcpp-function-to-erase-na-values
-      \item New function \code{is_finite} that reproduces the behavior of
-      R's \code{is.finite} function
+      \item New function \code{is_finite} and \code{is_infinite} that reproduces 
+      the behavior of R's \code{is.finite} and \code{is.infinite} functions
     }
     \item Changes in Rcpp build tools:
     \itemize{
       \item Fix (from Martyn Plummer) for solaris in handling SingleLogicalResult.     
-      \item The \code{src/Makevars} file can now optionally ovveride the
+      \item The \code{src/Makevars} file can now optionally override the
       path for \code{/usr/bin/install_name_tool} which is used on OS X.
     }
   }

Modified: pkg/Rcpp/inst/include/Rcpp/sugar/functions/functions.h
===================================================================
--- pkg/Rcpp/inst/include/Rcpp/sugar/functions/functions.h	2013-05-30 16:01:04 UTC (rev 4325)
+++ pkg/Rcpp/inst/include/Rcpp/sugar/functions/functions.h	2013-05-31 09:04:22 UTC (rev 4326)
@@ -35,6 +35,7 @@
 #include <Rcpp/sugar/functions/all.h>
 #include <Rcpp/sugar/functions/is_na.h>
 #include <Rcpp/sugar/functions/is_finite.h>
+#include <Rcpp/sugar/functions/is_infinite.h>
 #include <Rcpp/sugar/functions/na_omit.h>
 #include <Rcpp/sugar/functions/seq_along.h>
 #include <Rcpp/sugar/functions/sapply.h>

Added: pkg/Rcpp/inst/include/Rcpp/sugar/functions/is_infinite.h
===================================================================
--- pkg/Rcpp/inst/include/Rcpp/sugar/functions/is_infinite.h	                        (rev 0)
+++ pkg/Rcpp/inst/include/Rcpp/sugar/functions/is_infinite.h	2013-05-31 09:04:22 UTC (rev 4326)
@@ -0,0 +1,55 @@
+// -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; tab-width: 8 -*-
+//
+// is_infinite.h: Rcpp R/C++ interface class library -- is_infinite
+//
+// Copyright (C) 2013 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__is_infinite_h
+#define Rcpp__sugar__is_infinite_h
+
+namespace Rcpp{
+namespace sugar{
+
+template <int RTYPE, bool NA, typename VEC_TYPE>
+class IsInfinite : public ::Rcpp::VectorBase< LGLSXP, false, IsInfinite<RTYPE,NA,VEC_TYPE> > {
+public:
+	
+	IsInfinite( const VEC_TYPE& obj_) : obj(obj_){}
+	
+	inline int operator[]( int i ) const {
+		return ::Rcpp::traits::is_infinite<RTYPE>( obj[i] ) ;
+	}
+	
+	inline int size() const { return obj.size() ; }
+	         
+private:
+	const VEC_TYPE& obj ;
+	
+} ;
+
+	
+} // sugar
+
+template <int RTYPE, bool NA, typename T>
+inline sugar::IsInfinite<RTYPE,NA,T> is_infinite( const Rcpp::VectorBase<RTYPE,NA,T>& t){
+	return sugar::IsInfinite<RTYPE,NA,T>( t.get_ref() ) ;
+}
+
+} // Rcpp
+#endif
+

Added: pkg/Rcpp/inst/include/Rcpp/traits/is_infinite.h
===================================================================
--- pkg/Rcpp/inst/include/Rcpp/traits/is_infinite.h	                        (rev 0)
+++ pkg/Rcpp/inst/include/Rcpp/traits/is_infinite.h	2013-05-31 09:04:22 UTC (rev 4326)
@@ -0,0 +1,47 @@
+// -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; tab-width: 8 -*-
+//
+// is_infinite.h: Rcpp R/C++ interface class library -- is infinite
+//                                                                      
+// Copyright (C) 2013 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__traits_is_infinite_h
+#define Rcpp__traits_is_infinite_h
+
+namespace Rcpp{
+namespace traits{
+	
+    // default for all but REALSXP and CPLXSXP
+	template <int RTYPE> 
+	bool is_infinite( typename storage_type<RTYPE>::type){
+	    return false ;    
+	}
+	
+	template <> 
+	inline bool is_infinite<REALSXP>( double x ){
+		return !( ISNAN(x) || R_FINITE(x) ) ;
+	}
+	
+	template <> 
+	inline bool is_infinite<CPLXSXP>( Rcomplex x ){
+		return is_infinite<REALSXP>(x.r) || is_infinite<REALSXP>(x.i) ;
+	}
+	
+}
+}
+
+#endif

Modified: pkg/Rcpp/inst/include/Rcpp/traits/traits.h
===================================================================
--- pkg/Rcpp/inst/include/Rcpp/traits/traits.h	2013-05-30 16:01:04 UTC (rev 4325)
+++ pkg/Rcpp/inst/include/Rcpp/traits/traits.h	2013-05-31 09:04:22 UTC (rev 4326)
@@ -46,6 +46,7 @@
 #include <Rcpp/traits/module_wrap_traits.h>
 #include <Rcpp/traits/is_na.h>
 #include <Rcpp/traits/is_finite.h>
+#include <Rcpp/traits/is_infinite.h>
 #include <Rcpp/traits/if_.h>
 #include <Rcpp/traits/get_na.h>
 #include <Rcpp/traits/is_trivial.h>

Modified: pkg/Rcpp/inst/unitTests/cpp/sugar.cpp
===================================================================
--- pkg/Rcpp/inst/unitTests/cpp/sugar.cpp	2013-05-30 16:01:04 UTC (rev 4325)
+++ pkg/Rcpp/inst/unitTests/cpp/sugar.cpp	2013-05-31 09:04:22 UTC (rev 4326)
@@ -185,8 +185,13 @@
 }
 
 // [[Rcpp::export]]
+LogicalVector runit_isinfinite( NumericVector xx){
+    return is_infinite(xx) ;
+}
+
+// [[Rcpp::export]]
 LogicalVector runit_isna_isna( NumericVector xx ){
-    return wrap( is_na( is_na( xx ) ) ) ;
+    return is_na( is_na( xx ) ) ;
 }
 
 // [[Rcpp::export]]

Modified: pkg/Rcpp/inst/unitTests/runit.sugar.R
===================================================================
--- pkg/Rcpp/inst/unitTests/runit.sugar.R	2013-05-30 16:01:04 UTC (rev 4325)
+++ pkg/Rcpp/inst/unitTests/runit.sugar.R	2013-05-31 09:04:22 UTC (rev 4326)
@@ -269,10 +269,19 @@
 test.sugar.isfinite <- function( ){
 	checkEquals( 
 	    runit_isfinite( c(1, NA, Inf, -Inf, NaN) ) , 
-	    c(TRUE, FALSE, FALSE, FALSE, FALSE)
+	    c(TRUE, FALSE, FALSE, FALSE, FALSE), 
+	    msg = "is_finite"
 	)
 }
 
+test.sugar.isinfinite <- function( ){
+	checkEquals( 
+	    runit_isinfinite( c(1, NA, Inf, -Inf, NaN) ) , 
+	    c(FALSE, FALSE, TRUE, TRUE, FALSE), 
+	    msg = "is_infinite"
+	)
+}
+
 test.sugar.isna.isna <- function( ){
 	fx <- runit_isna_isna
 	checkEquals( fx( c(1:5,NA,7:10) ) , rep(FALSE,10) )



More information about the Rcpp-commits mailing list