[Rcpp-commits] r4324 - 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
Thu May 30 16:13:18 CEST 2013


Author: romain
Date: 2013-05-30 16:13:18 +0200 (Thu, 30 May 2013)
New Revision: 4324

Added:
   pkg/Rcpp/inst/include/Rcpp/sugar/functions/is_finite.h
   pkg/Rcpp/inst/include/Rcpp/traits/is_finite.h
Modified:
   pkg/Rcpp/ChangeLog
   pkg/Rcpp/inst/NEWS.Rd
   pkg/Rcpp/inst/include/Rcpp/sugar/functions/all.h
   pkg/Rcpp/inst/include/Rcpp/sugar/functions/functions.h
   pkg/Rcpp/inst/include/Rcpp/sugar/functions/is_na.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_finite

Modified: pkg/Rcpp/ChangeLog
===================================================================
--- pkg/Rcpp/ChangeLog	2013-05-26 19:52:22 UTC (rev 4323)
+++ pkg/Rcpp/ChangeLog	2013-05-30 14:13:18 UTC (rev 4324)
@@ -1,3 +1,11 @@
+2013-05-30 Romain Francois <romain at r-enthusiasts.com>
+
+        * include/Rcpp/sugar/functions/all.h : bug fixed for the NA=false case
+        * include/Rcpp/sugar/functions/is_na.h : header
+        * include/Rcpp/sugar/functions/is_finite.h : added sugar is_finite function
+        * include/Rcpp/traits/is_finite.h : added is_finite trait
+        * unitTests/runit.sugar.R : added test for is_finite
+        
 2013-05-26  Dirk Eddelbuettel  <edd at debian.org>
 
 	* src/api.cpp: Minor tweak for internal formatting

Modified: pkg/Rcpp/inst/NEWS.Rd
===================================================================
--- pkg/Rcpp/inst/NEWS.Rd	2013-05-26 19:52:22 UTC (rev 4323)
+++ pkg/Rcpp/inst/NEWS.Rd	2013-05-30 14:13:18 UTC (rev 4324)
@@ -29,6 +29,8 @@
     \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 Changes in Rcpp build tools:
     \itemize{

Modified: pkg/Rcpp/inst/include/Rcpp/sugar/functions/all.h
===================================================================
--- pkg/Rcpp/inst/include/Rcpp/sugar/functions/all.h	2013-05-26 19:52:22 UTC (rev 4323)
+++ pkg/Rcpp/inst/include/Rcpp/sugar/functions/all.h	2013-05-30 14:13:18 UTC (rev 4324)
@@ -2,7 +2,7 @@
 //
 // all.h: Rcpp R/C++ interface class library -- all
 //
-// Copyright (C) 2010 - 2011 Dirk Eddelbuettel and Romain Francois
+// Copyright (C) 2010 - 2013 Dirk Eddelbuettel and Romain Francois
 //
 // This file is part of Rcpp.
 //
@@ -67,7 +67,7 @@
 		int n = object.size() ;
 		PARENT::set_true() ;
 		for( int i=0 ; i<n ; i++){
-			if( object[i] == TRUE ) {
+			if( object[i] == FALSE ) {
 				PARENT::set_false() ; 
 				return ;
 			}

Modified: pkg/Rcpp/inst/include/Rcpp/sugar/functions/functions.h
===================================================================
--- pkg/Rcpp/inst/include/Rcpp/sugar/functions/functions.h	2013-05-26 19:52:22 UTC (rev 4323)
+++ pkg/Rcpp/inst/include/Rcpp/sugar/functions/functions.h	2013-05-30 14:13:18 UTC (rev 4324)
@@ -34,6 +34,7 @@
 #include <Rcpp/sugar/functions/any.h>
 #include <Rcpp/sugar/functions/all.h>
 #include <Rcpp/sugar/functions/is_na.h>
+#include <Rcpp/sugar/functions/is_finite.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_finite.h
===================================================================
--- pkg/Rcpp/inst/include/Rcpp/sugar/functions/is_finite.h	                        (rev 0)
+++ pkg/Rcpp/inst/include/Rcpp/sugar/functions/is_finite.h	2013-05-30 14:13:18 UTC (rev 4324)
@@ -0,0 +1,55 @@
+// -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; tab-width: 8 -*-
+//
+// is_finite.h: Rcpp R/C++ interface class library -- is_finite
+//
+// 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_finite_h
+#define Rcpp__sugar__is_finite_h
+
+namespace Rcpp{
+namespace sugar{
+
+template <int RTYPE, bool NA, typename VEC_TYPE>
+class IsFinite : public ::Rcpp::VectorBase< LGLSXP, false, IsFinite<RTYPE,NA,VEC_TYPE> > {
+public:
+	
+	IsFinite( const VEC_TYPE& obj_) : obj(obj_){}
+	
+	inline int operator[]( int i ) const {
+		return ::Rcpp::traits::is_finite<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::IsFinite<RTYPE,NA,T> is_finite( const Rcpp::VectorBase<RTYPE,NA,T>& t){
+	return sugar::IsFinite<RTYPE,NA,T>( t.get_ref() ) ;
+}
+
+} // Rcpp
+#endif
+

Modified: pkg/Rcpp/inst/include/Rcpp/sugar/functions/is_na.h
===================================================================
--- pkg/Rcpp/inst/include/Rcpp/sugar/functions/is_na.h	2013-05-26 19:52:22 UTC (rev 4323)
+++ pkg/Rcpp/inst/include/Rcpp/sugar/functions/is_na.h	2013-05-30 14:13:18 UTC (rev 4324)
@@ -1,8 +1,8 @@
 // -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; tab-width: 8 -*-
 //
-// any.h: Rcpp R/C++ interface class library -- any
+// is_na.h: Rcpp R/C++ interface class library -- is_na
 //
-// Copyright (C) 2010 - 2011 Dirk Eddelbuettel and Romain Francois
+// Copyright (C) 2010 - 2013 Dirk Eddelbuettel and Romain Francois
 //
 // This file is part of Rcpp.
 //

Added: pkg/Rcpp/inst/include/Rcpp/traits/is_finite.h
===================================================================
--- pkg/Rcpp/inst/include/Rcpp/traits/is_finite.h	                        (rev 0)
+++ pkg/Rcpp/inst/include/Rcpp/traits/is_finite.h	2013-05-30 14:13:18 UTC (rev 4324)
@@ -0,0 +1,56 @@
+// -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; tab-width: 8 -*-
+//
+// is_finite.h: Rcpp R/C++ interface class library -- is finite
+//                                                                      
+// 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_finite_h
+#define Rcpp__traits_is_finite_h
+
+namespace Rcpp{
+namespace traits{
+	
+	// default for complex, 
+	template <int RTYPE> 
+	bool is_finite( typename storage_type<RTYPE>::type) ;
+	
+	template <> 
+	inline bool is_finite<INTSXP>( int x ){
+		return x != NA_INTEGER ;
+	}
+	
+	template <> 
+	inline bool is_finite<REALSXP>( double x ){
+		return R_finite(x) ;
+	}
+	
+	template <> 
+	inline bool is_finite<CPLXSXP>( Rcomplex x ){
+		return !( !R_finite(x.r) || !R_finite(x.i) );
+	}
+	
+	template <>
+	inline bool is_finite<STRSXP>( SEXP x ){ return x != NA_STRING ; }
+	
+	template <>
+	inline bool is_finite<LGLSXP>( int x ){ return x != NA_LOGICAL ; }
+	
+}
+}
+
+#endif

Modified: pkg/Rcpp/inst/include/Rcpp/traits/traits.h
===================================================================
--- pkg/Rcpp/inst/include/Rcpp/traits/traits.h	2013-05-26 19:52:22 UTC (rev 4323)
+++ pkg/Rcpp/inst/include/Rcpp/traits/traits.h	2013-05-30 14:13:18 UTC (rev 4324)
@@ -45,6 +45,7 @@
 #include <Rcpp/traits/wrap_type_traits.h>
 #include <Rcpp/traits/module_wrap_traits.h>
 #include <Rcpp/traits/is_na.h>
+#include <Rcpp/traits/is_finite.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-26 19:52:22 UTC (rev 4323)
+++ pkg/Rcpp/inst/unitTests/cpp/sugar.cpp	2013-05-30 14:13:18 UTC (rev 4324)
@@ -180,6 +180,11 @@
 }
 
 // [[Rcpp::export]]
+LogicalVector runit_isfinite( NumericVector xx){
+    return is_finite(xx) ;
+}
+
+// [[Rcpp::export]]
 LogicalVector runit_isna_isna( NumericVector xx ){
     return wrap( is_na( is_na( xx ) ) ) ;
 }

Modified: pkg/Rcpp/inst/unitTests/runit.sugar.R
===================================================================
--- pkg/Rcpp/inst/unitTests/runit.sugar.R	2013-05-26 19:52:22 UTC (rev 4323)
+++ pkg/Rcpp/inst/unitTests/runit.sugar.R	2013-05-30 14:13:18 UTC (rev 4324)
@@ -1,7 +1,7 @@
 #!/usr/bin/r -t
 #                     -*- mode: R; ess-indent-level: 4; indent-tabs-mode: nil; -*-
 #
-# Copyright (C) 2010 - 2012  Dirk Eddelbuettel and Romain Francois
+# Copyright (C) 2010 - 2013  Dirk Eddelbuettel and Romain Francois
 #
 # This file is part of Rcpp.
 #
@@ -266,6 +266,13 @@
 	checkEquals( fx( 1:10) , rep(FALSE,10) )
 }
 
+test.sugar.isfinite <- function( ){
+	checkEquals( 
+	    runit_isfinite( c(1, NA, Inf, -Inf, NaN) ) , 
+	    c(TRUE, FALSE, FALSE, FALSE, FALSE)
+	)
+}
+
 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