[Rcpp-commits] r1981 - in pkg/Rcpp: . inst inst/include/Rcpp inst/include/Rcpp/stats/random inst/include/Rcpp/sugar/functions

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Wed Aug 11 23:47:08 CEST 2010


Author: romain
Date: 2010-08-11 23:47:07 +0200 (Wed, 11 Aug 2010)
New Revision: 1981

Added:
   pkg/Rcpp/inst/include/Rcpp/sugar/functions/math.h
Modified:
   pkg/Rcpp/TODO
   pkg/Rcpp/inst/ChangeLog
   pkg/Rcpp/inst/include/Rcpp/Generator.h
   pkg/Rcpp/inst/include/Rcpp/stats/random/rcauchy.h
   pkg/Rcpp/inst/include/Rcpp/stats/random/rlogis.h
   pkg/Rcpp/inst/include/Rcpp/stats/random/rt.h
   pkg/Rcpp/inst/include/Rcpp/stats/random/rweibull.h
   pkg/Rcpp/inst/include/Rcpp/sugar/functions/Mod.h
   pkg/Rcpp/inst/include/Rcpp/sugar/functions/SugarBlock.h
   pkg/Rcpp/inst/include/Rcpp/sugar/functions/functions.h
Log:
some more (auto generated by the preprocessor) math functions : sqrt, log, log10, cos, ...

Modified: pkg/Rcpp/TODO
===================================================================
--- pkg/Rcpp/TODO	2010-08-11 19:08:37 UTC (rev 1980)
+++ pkg/Rcpp/TODO	2010-08-11 21:47:07 UTC (rev 1981)
@@ -48,7 +48,7 @@
 
     o	Vector * Matrix, Matrix * Matrix
 
-    o   duplicated, unique, count, sum, sqrt, log, log10, ln
+    o   duplicated, unique, count, sum
     
     o   operator% 
     
@@ -67,7 +67,8 @@
     
     o	other statistical distribution functions : 
     	
-    	multinom : this makes a matrix, not a vector
+    	multinom : this only has dmultinom which is handled in R, so maybe we can
+    	skip it
     	
     	signrank : has the weird call to .C( "signrank_free" ), need to understand that
     	

Modified: pkg/Rcpp/inst/ChangeLog
===================================================================
--- pkg/Rcpp/inst/ChangeLog	2010-08-11 19:08:37 UTC (rev 1980)
+++ pkg/Rcpp/inst/ChangeLog	2010-08-11 21:47:07 UTC (rev 1981)
@@ -43,6 +43,10 @@
 		pentagamma, expm1, log1p, factorial, lfactorial
 		
 		(2 parameter) : choose, lchoose, beta, lbeta, psigamma
+		
+	* inst/include/Rcpp/sugar/math.h : using the SUGAR_MATH_1 macro to generate
+	sugar functions from function pointers : acos, acosh, atan, cos, cosh, 
+	log, log10, sqrt, sin, sinh, tan, tanh
 	
 2010-08-10  Douglas Bates  <bates at stat.wisc.edu>
 

Modified: pkg/Rcpp/inst/include/Rcpp/Generator.h
===================================================================
--- pkg/Rcpp/inst/include/Rcpp/Generator.h	2010-08-11 19:08:37 UTC (rev 1980)
+++ pkg/Rcpp/inst/include/Rcpp/Generator.h	2010-08-11 21:47:07 UTC (rev 1981)
@@ -40,6 +40,13 @@
 	~Generator(){ }	
 } ;
 
+class RNGScope{
+public:
+	RNGScope(){ GetRNGstate(); }
+	~RNGScope(){ PutRNGstate(); }	
+} ;
+
+
 }
 
 #endif

Modified: pkg/Rcpp/inst/include/Rcpp/stats/random/rcauchy.h
===================================================================
--- pkg/Rcpp/inst/include/Rcpp/stats/random/rcauchy.h	2010-08-11 19:08:37 UTC (rev 1980)
+++ pkg/Rcpp/inst/include/Rcpp/stats/random/rcauchy.h	2010-08-11 21:47:07 UTC (rev 1981)
@@ -32,7 +32,7 @@
 		location(location_) , scale(scale_) {}
 	
 	inline double operator()() const {
-		return location + scale * tan(M_PI * unif_rand()) ;
+		return location + scale * ::tan(M_PI * unif_rand()) ;
 	}
 	
 private:

Modified: pkg/Rcpp/inst/include/Rcpp/stats/random/rlogis.h
===================================================================
--- pkg/Rcpp/inst/include/Rcpp/stats/random/rlogis.h	2010-08-11 19:08:37 UTC (rev 1980)
+++ pkg/Rcpp/inst/include/Rcpp/stats/random/rlogis.h	2010-08-11 21:47:07 UTC (rev 1981)
@@ -33,7 +33,7 @@
 	
 	inline double operator()() const {
 		double u = unif_rand() ;
-		return location + scale * log(u / (1. - u));
+		return location + scale * ::log(u / (1. - u));
 	}
 	
 private:

Modified: pkg/Rcpp/inst/include/Rcpp/stats/random/rt.h
===================================================================
--- pkg/Rcpp/inst/include/Rcpp/stats/random/rt.h	2010-08-11 19:08:37 UTC (rev 1980)
+++ pkg/Rcpp/inst/include/Rcpp/stats/random/rt.h	2010-08-11 21:47:07 UTC (rev 1981)
@@ -38,7 +38,7 @@
 		// return num / sqrt(rchisq(df) / df);
 		// replaced by the followoing line to skip the test in 
 		// rchisq because we already know
-		return num / sqrt( ::Rf_rgamma(df_2, 2.0) / df);
+		return num / ::sqrt( ::Rf_rgamma(df_2, 2.0) / df);
 	}
 	
 private:

Modified: pkg/Rcpp/inst/include/Rcpp/stats/random/rweibull.h
===================================================================
--- pkg/Rcpp/inst/include/Rcpp/stats/random/rweibull.h	2010-08-11 19:08:37 UTC (rev 1980)
+++ pkg/Rcpp/inst/include/Rcpp/stats/random/rweibull.h	2010-08-11 21:47:07 UTC (rev 1981)
@@ -32,7 +32,7 @@
 		shape_inv( 1/shape_), scale(scale_) {}
 	
 	inline double operator()() const {
-		return scale * ::R_pow(-log(unif_rand()), shape_inv );
+		return scale * ::R_pow(-::log(unif_rand()), shape_inv );
 	}
 	
 private:

Modified: pkg/Rcpp/inst/include/Rcpp/sugar/functions/Mod.h
===================================================================
--- pkg/Rcpp/inst/include/Rcpp/sugar/functions/Mod.h	2010-08-11 19:08:37 UTC (rev 1980)
+++ pkg/Rcpp/inst/include/Rcpp/sugar/functions/Mod.h	2010-08-11 21:47:07 UTC (rev 1981)
@@ -34,7 +34,7 @@
 	
 	inline double operator[]( int i ) const {
 		Rcomplex x = object[i] ;
-		return sqrt( x.i * x.i + x.r * x.r ); 
+		return ::sqrt( x.i * x.i + x.r * x.r ); 
 	}
 	inline int size() const { return object.size() ; }
 	         

Modified: pkg/Rcpp/inst/include/Rcpp/sugar/functions/SugarBlock.h
===================================================================
--- pkg/Rcpp/inst/include/Rcpp/sugar/functions/SugarBlock.h	2010-08-11 19:08:37 UTC (rev 1980)
+++ pkg/Rcpp/inst/include/Rcpp/sugar/functions/SugarBlock.h	2010-08-11 21:47:07 UTC (rev 1981)
@@ -41,6 +41,23 @@
 	const T1& vec ;
 };
 
+template <bool NA, typename OUT, typename U1, typename T1>
+class SugarMath_1 : public Rcpp::VectorBase< Rcpp::traits::r_sexptype_traits<OUT>::rtype , NA, SugarMath_1<NA,OUT,U1,T1> > {
+public:
+	typedef OUT (*FunPtr)(U1) ;
+	SugarMath_1( FunPtr ptr_, const T1 & vec_) : ptr(ptr_), vec(vec_){}
+	
+	inline OUT operator[]( int i) const { 
+		double x = vec[i] ;
+		if( ISNAN(x) ) return x;
+		return ptr( x ) ;
+	}
+	inline int size() const { return vec.size() ; }
+	
+private:
+	FunPtr ptr ;
+	const T1& vec ;
+};
 
 template <bool NA, typename OUT, typename U1, typename T1, typename U2, typename T2>
 class SugarBlock_2 : public Rcpp::VectorBase< Rcpp::traits::r_sexptype_traits<OUT>::rtype , NA, SugarBlock_2<NA,OUT,U1,T1,U2,T2> > {
@@ -116,7 +133,20 @@
 	}                                                               \
 	}
 
+#define SUGAR_MATH_1(__NAME__,__SYMBOL__) \
+	namespace Rcpp{                                                 \
+	template <bool NA, typename T>                                  \
+	inline sugar::SugarMath_1<NA,double,double,T>                  \
+	__NAME__(                                                       \
+		const VectorBase<REALSXP,NA,T>& t                           \
+	){                                                              \
+		return sugar::SugarMath_1<NA,double,double,T>(             \
+			__SYMBOL__ , t                                          \
+		) ;                                                         \
+	}                                                               \
+	}
 
+
 #define SUGAR_BLOCK_2(__NAME__,__SYMBOL__)                                          \
 	namespace Rcpp{                                                                  \
 	template <bool LHS_NA, typename LHS_T, bool RHS_NA, typename RHS_T >             \

Modified: pkg/Rcpp/inst/include/Rcpp/sugar/functions/functions.h
===================================================================
--- pkg/Rcpp/inst/include/Rcpp/sugar/functions/functions.h	2010-08-11 19:08:37 UTC (rev 1980)
+++ pkg/Rcpp/inst/include/Rcpp/sugar/functions/functions.h	2010-08-11 21:47:07 UTC (rev 1981)
@@ -27,6 +27,7 @@
 
 #include <Rcpp/sugar/functions/SugarBlock.h>
 #include <Rcpp/sugar/functions/gamma.h>
+#include <Rcpp/sugar/functions/math.h>
 
 #include <Rcpp/sugar/functions/any.h>
 #include <Rcpp/sugar/functions/all.h>

Added: pkg/Rcpp/inst/include/Rcpp/sugar/functions/math.h
===================================================================
--- pkg/Rcpp/inst/include/Rcpp/sugar/functions/math.h	                        (rev 0)
+++ pkg/Rcpp/inst/include/Rcpp/sugar/functions/math.h	2010-08-11 21:47:07 UTC (rev 1981)
@@ -0,0 +1,38 @@
+// -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; tab-width: 8 -*-
+//
+// SugarBlock.h: Rcpp R/C++ interface class library -- sugar functions
+//
+// 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_MATH_H
+#define RCPP_SUGAR_MATH_H
+
+SUGAR_MATH_1(acos,::acos)
+SUGAR_MATH_1(asin,::asin)
+SUGAR_MATH_1(atan,::atan)
+SUGAR_MATH_1(cos,::cos)
+SUGAR_MATH_1(cosh,::cosh)
+SUGAR_MATH_1(log,::log)
+SUGAR_MATH_1(log10,::log10)
+SUGAR_MATH_1(sqrt,::sqrt)
+SUGAR_MATH_1(sin,::sin)
+SUGAR_MATH_1(sinh,::sinh)
+SUGAR_MATH_1(tan,::tan)
+SUGAR_MATH_1(tanh,::tanh)
+
+#endif



More information about the Rcpp-commits mailing list