[Rcpp-commits] r4327 - in pkg/Rcpp: . 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:25:27 CEST 2013


Author: romain
Date: 2013-05-31 11:25:26 +0200 (Fri, 31 May 2013)
New Revision: 4327

Added:
   pkg/Rcpp/inst/include/Rcpp/sugar/functions/is_nan.h
   pkg/Rcpp/inst/include/Rcpp/traits/is_nan.h
Modified:
   pkg/Rcpp/ChangeLog
   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:
is_nan

Modified: pkg/Rcpp/ChangeLog
===================================================================
--- pkg/Rcpp/ChangeLog	2013-05-31 09:04:22 UTC (rev 4326)
+++ pkg/Rcpp/ChangeLog	2013-05-31 09:25:26 UTC (rev 4327)
@@ -1,9 +1,11 @@
 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
+        * unitTests/runit.sugar.R : new test for is_infinite and is_nan
+        * unitTests/cpp/sugar.cpp : new test for is_infinite and is_nan
         * include/Rcpp/sugar/functions/is_infinite.h : new function is_infinite
         * include/Rcpp/traits/is_infinite.h : new function is_infinite
+        * include/Rcpp/sugar/functions/is_nan.h : new function is_nan
+        * include/Rcpp/traits/is_nan.h : new function is_nan
         
 2013-05-30 Romain Francois <romain at r-enthusiasts.com>
 

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

Added: pkg/Rcpp/inst/include/Rcpp/traits/is_nan.h
===================================================================
--- pkg/Rcpp/inst/include/Rcpp/traits/is_nan.h	                        (rev 0)
+++ pkg/Rcpp/inst/include/Rcpp/traits/is_nan.h	2013-05-31 09:25:26 UTC (rev 4327)
@@ -0,0 +1,47 @@
+// -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; tab-width: 8 -*-
+//
+// is_nan.h: Rcpp R/C++ interface class library -- is NaN
+//                                                                      
+// 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_nan_h
+#define Rcpp__traits_is_nan_h
+
+namespace Rcpp{
+namespace traits{
+	
+	// default for most types 
+	template <int RTYPE> 
+	bool is_nan( typename storage_type<RTYPE>::type){
+	    return false ;    
+	}
+	
+	template <> 
+	inline bool is_nan<REALSXP>( double x ){
+		return R_IsNaN(x) ;
+	}
+	
+	template <> 
+	inline bool is_nan<CPLXSXP>( Rcomplex x ){
+		return R_IsNaN(x.r) || R_IsNaN(x.i) ;
+	}
+	
+}
+}
+
+#endif

Modified: pkg/Rcpp/inst/include/Rcpp/traits/traits.h
===================================================================
--- pkg/Rcpp/inst/include/Rcpp/traits/traits.h	2013-05-31 09:04:22 UTC (rev 4326)
+++ pkg/Rcpp/inst/include/Rcpp/traits/traits.h	2013-05-31 09:25:26 UTC (rev 4327)
@@ -3,7 +3,7 @@
 //
 // traits.h: Rcpp R/C++ interface class library -- traits to help wrap
 //
-// Copyright (C) 2012 Dirk Eddelbuettel and Romain Francois
+// Copyright (C) 2012 - 2013 Dirk Eddelbuettel and Romain Francois
 //
 // This file is part of Rcpp.
 //
@@ -47,6 +47,7 @@
 #include <Rcpp/traits/is_na.h>
 #include <Rcpp/traits/is_finite.h>
 #include <Rcpp/traits/is_infinite.h>
+#include <Rcpp/traits/is_nan.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-31 09:04:22 UTC (rev 4326)
+++ pkg/Rcpp/inst/unitTests/cpp/sugar.cpp	2013-05-31 09:25:26 UTC (rev 4327)
@@ -190,6 +190,11 @@
 }
 
 // [[Rcpp::export]]
+LogicalVector runit_isnan( NumericVector xx){
+    return is_nan(xx) ;
+}
+
+// [[Rcpp::export]]
 LogicalVector runit_isna_isna( NumericVector xx ){
     return is_na( is_na( xx ) ) ;
 }

Modified: pkg/Rcpp/inst/unitTests/runit.sugar.R
===================================================================
--- pkg/Rcpp/inst/unitTests/runit.sugar.R	2013-05-31 09:04:22 UTC (rev 4326)
+++ pkg/Rcpp/inst/unitTests/runit.sugar.R	2013-05-31 09:25:26 UTC (rev 4327)
@@ -282,6 +282,15 @@
 	)
 }
 
+
+test.sugar.isnan <- function( ){
+	checkEquals( 
+	    runit_isnan( c(1, NA, Inf, -Inf, NaN) ) , 
+	    c(FALSE, FALSE, FALSE, FALSE, TRUE), 
+	    msg = "is_nan"
+	)
+}
+
 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