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

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Wed Aug 11 21:08:38 CEST 2010


Author: romain
Date: 2010-08-11 21:08:37 +0200 (Wed, 11 Aug 2010)
New Revision: 1980

Added:
   pkg/Rcpp/inst/include/Rcpp/stats/random/rbinom.h
   pkg/Rcpp/inst/include/Rcpp/stats/random/rgamma.h
   pkg/Rcpp/inst/include/Rcpp/stats/random/rgeom.h
   pkg/Rcpp/inst/include/Rcpp/stats/random/rhyper.h
   pkg/Rcpp/inst/include/Rcpp/stats/random/rlnorm.h
   pkg/Rcpp/inst/include/Rcpp/stats/random/rlogis.h
   pkg/Rcpp/inst/include/Rcpp/stats/random/rnbinom.h
   pkg/Rcpp/inst/include/Rcpp/stats/random/rnbinom_mu.h
   pkg/Rcpp/inst/include/Rcpp/stats/random/rnchisq.h
   pkg/Rcpp/inst/include/Rcpp/stats/random/rpois.h
   pkg/Rcpp/inst/include/Rcpp/stats/random/rsignrank.h
   pkg/Rcpp/inst/include/Rcpp/stats/random/rt.h
   pkg/Rcpp/inst/include/Rcpp/stats/random/rweibull.h
   pkg/Rcpp/inst/include/Rcpp/stats/random/rwilcox.h
   pkg/Rcpp/inst/include/Rcpp/sugar/functions/SugarBlock.h
   pkg/Rcpp/inst/include/Rcpp/sugar/functions/gamma.h
Modified:
   pkg/Rcpp/TODO
   pkg/Rcpp/inst/ChangeLog
   pkg/Rcpp/inst/include/Rcpp.h
   pkg/Rcpp/inst/include/Rcpp/stats/norm.h
   pkg/Rcpp/inst/include/Rcpp/stats/random/random.h
   pkg/Rcpp/inst/include/Rcpp/stats/random/rbeta.h
   pkg/Rcpp/inst/include/Rcpp/stats/random/rcauchy.h
   pkg/Rcpp/inst/include/Rcpp/stats/random/rchisq.h
   pkg/Rcpp/inst/include/Rcpp/stats/random/rexp.h
   pkg/Rcpp/inst/include/Rcpp/stats/random/rf.h
   pkg/Rcpp/inst/include/Rcpp/stats/random/rnorm.h
   pkg/Rcpp/inst/include/Rcpp/stats/random/runif.h
   pkg/Rcpp/inst/include/Rcpp/stats/stats.h
   pkg/Rcpp/inst/include/Rcpp/sugar/functions/functions.h
   pkg/Rcpp/inst/include/Rcpp/vector/Vector.h
   pkg/Rcpp/inst/unitTests/runit.stats.R
   pkg/Rcpp/inst/unitTests/runit.sugar.R
Log:
long train ride

Modified: pkg/Rcpp/TODO
===================================================================
--- pkg/Rcpp/TODO	2010-08-10 20:58:56 UTC (rev 1979)
+++ pkg/Rcpp/TODO	2010-08-11 19:08:37 UTC (rev 1980)
@@ -77,3 +77,15 @@
     
     o	sum : deal with missing values
 
+    o	other random generators: 
+    
+    	rmultinom : tricky because it generates a matrix
+    	
+    	rnbeta : the R version is implemented in R (not in C), 
+    	should we still have it in Rcpp in compile code ?
+    	
+    	rnf : idem
+    	
+    	rnt : idem
+    	
+    	

Modified: pkg/Rcpp/inst/ChangeLog
===================================================================
--- pkg/Rcpp/inst/ChangeLog	2010-08-10 20:58:56 UTC (rev 1979)
+++ pkg/Rcpp/inst/ChangeLog	2010-08-11 19:08:37 UTC (rev 1980)
@@ -1,3 +1,49 @@
+2010-08-11  Romain Francois <romain at r-enthusiasts.com>
+
+	* inst/include/Rcpp/vector/vector.h: Vector gains constructor 
+	taking size and function pointers, acting as generators : 
+	
+		Vector( const int& siz, stored_type (*gen)(void), bool seed = true )
+		
+		template <typename U1>
+    	Vector( const int& siz, stored_type (*gen)(U1), const U1& u1, bool seed = true)
+    	
+    	template <typename U1, typename U2>
+    	Vector( const int& siz, stored_type (*gen)(U1,U2), const U1& u1, const U2& u2, bool seed = true){
+    	
+    	so that for example : 
+    	
+    	NumericVector( 10, norm_rand ) generates a N(0,1) vector of size 10
+    
+    * inst/include/Rcpp/stats/random/rlnorm.h: Added rlnorm and rlnorm_
+    * inst/include/Rcpp/stats/random/rgamma.h: Added rgamma and rgamma_
+    * inst/include/Rcpp/stats/random/rt.h: Added rt and rt_
+    * inst/include/Rcpp/stats/random/rbinom.h: Added rbinom and rbinom_
+    * inst/include/Rcpp/stats/random/rnchisq.h: Added rnchisq and rnchisq_
+    * inst/include/Rcpp/stats/random/rgeom.h: Added rgeom and rgeom_
+    * inst/include/Rcpp/stats/random/rhyper.h: Added rhyper and rhyper_
+    * inst/include/Rcpp/stats/random/rnbinom_mu.h: Added rnbinom_mu and rnbinom_mu_
+    * inst/include/Rcpp/stats/random/rnbinom.h: Added rnbinom and rnbinom_
+    * inst/include/Rcpp/stats/random/rpois.h: Added rpois and rpois_
+    * inst/include/Rcpp/stats/random/rweibull.h: Added rweibull and rweibull_
+    * inst/include/Rcpp/stats/random/rlogis.h: Added rlogis and rlogis_
+    * inst/include/Rcpp/stats/random/rwilcox.h: Added rwilcox and rwilcox_
+    * inst/include/Rcpp/stats/random/rsignrank.h: Added rsignrank and rsignrank_
+
+	* inst/include/Rcpp/stats/random/rnorm.h: some optimization
+    
+	* inst/include/Rcpp/stats/random/rf.h: calculate n2/n1 just once
+    
+	* inst/include/Rcpp/sugar/SugarBlock.h : templates and macros to facilitate
+	generation of sugar functions that take one or two double parameters
+	
+	* inst/include/Rcpp/sugar/gamma.h: using SugarBlock to generate new 
+	sugar functions: 
+		(1 parameter) : gamma, lgamma, digamma, trigamma, tetragamma, 
+		pentagamma, expm1, log1p, factorial, lfactorial
+		
+		(2 parameter) : choose, lchoose, beta, lbeta, psigamma
+	
 2010-08-10  Douglas Bates  <bates at stat.wisc.edu>
 
 	* include/Rcpp/stats/nbinom[_mu].h: Replace conflicting data member name

Modified: pkg/Rcpp/inst/include/Rcpp/stats/norm.h
===================================================================
--- pkg/Rcpp/inst/include/Rcpp/stats/norm.h	2010-08-10 20:58:56 UTC (rev 1979)
+++ pkg/Rcpp/inst/include/Rcpp/stats/norm.h	2010-08-11 19:08:37 UTC (rev 1980)
@@ -37,7 +37,7 @@
 			vec(vec_), mu(mu_), sigma(sigma_), log(log_) {}
 		
 		inline double operator[]( int i) const {
-			return ::dnorm( vec[i], mu, sigma, log );
+			return ::dnorm4( vec[i], mu, sigma, log );
 		}
 		
 		inline int size() const { return vec.size(); }
@@ -59,7 +59,7 @@
 			vec(vec_), mu(mu_), sigma(sigma_), lower(lower_tail), log(log_) {}
 		
 		inline double operator[]( int i) const {
-			return ::pnorm( vec[i], mu, sigma, lower, log );
+			return ::pnorm5( vec[i], mu, sigma, lower, log );
 		}
 		
 		inline int size() const { return vec.size(); }
@@ -81,7 +81,7 @@
 			vec(vec_), mu(mu_), sigma(sigma_), lower(lower_tail), log(log_) {}
 		
 		inline double operator[]( int i) const {
-			return ::qnorm( vec[i], mu, sigma, lower, log );
+			return ::qnorm5( vec[i], mu, sigma, lower, log );
 		}
 		
 		inline int size() const { return vec.size(); }

Modified: pkg/Rcpp/inst/include/Rcpp/stats/random/random.h
===================================================================
--- pkg/Rcpp/inst/include/Rcpp/stats/random/random.h	2010-08-10 20:58:56 UTC (rev 1979)
+++ pkg/Rcpp/inst/include/Rcpp/stats/random/random.h	2010-08-11 19:08:37 UTC (rev 1980)
@@ -22,12 +22,25 @@
 #ifndef Rcpp__stats__random_random_h
 #define Rcpp__stats__random_random_h
 
-#include <Rcpp/stats/random/rbeta.h>
 #include <Rcpp/stats/random/rnorm.h>
-#include <Rcpp/stats/random/rcauchy.h>
 #include <Rcpp/stats/random/runif.h>
+#include <Rcpp/stats/random/rgamma.h>
+#include <Rcpp/stats/random/rbeta.h>
+#include <Rcpp/stats/random/rlnorm.h>
 #include <Rcpp/stats/random/rchisq.h>
+#include <Rcpp/stats/random/rnchisq.h>
+#include <Rcpp/stats/random/rf.h>
+#include <Rcpp/stats/random/rt.h>
+#include <Rcpp/stats/random/rbinom.h>
+#include <Rcpp/stats/random/rcauchy.h>
 #include <Rcpp/stats/random/rexp.h>
-#include <Rcpp/stats/random/rf.h>
+#include <Rcpp/stats/random/rgeom.h>
+#include <Rcpp/stats/random/rnbinom.h>
+#include <Rcpp/stats/random/rnbinom_mu.h>
+#include <Rcpp/stats/random/rpois.h>
+#include <Rcpp/stats/random/rweibull.h>
+#include <Rcpp/stats/random/rlogis.h>
+#include <Rcpp/stats/random/rwilcox.h>
+#include <Rcpp/stats/random/rsignrank.h>
 
 #endif

Modified: pkg/Rcpp/inst/include/Rcpp/stats/random/rbeta.h
===================================================================
--- pkg/Rcpp/inst/include/Rcpp/stats/random/rbeta.h	2010-08-10 20:58:56 UTC (rev 1979)
+++ pkg/Rcpp/inst/include/Rcpp/stats/random/rbeta.h	2010-08-11 19:08:37 UTC (rev 1980)
@@ -23,31 +23,10 @@
 #define Rcpp__stats__random_rbeta_h
 
 namespace Rcpp {
-namespace stats {
 
-template <bool seed>
-class BetaGenerator : public ::Rcpp::Generator<seed,double> {
-public:
-	BetaGenerator( double a_, double b_ ) : a(a_), b(b_) {}
-	inline double operator()() const { 
-		return ::rbeta( a, b );
-	}
-private:
-	double a, b ;
-} ;
-
-} // stats
-
-template <bool seed>
-NumericVector rbeta__impl( int n, double a, double b ){
-	return NumericVector( n, stats::BetaGenerator<seed>( a, b ) ) ;
-}
 inline NumericVector rbeta( int n, double a, double b ){
-	return rbeta__impl<true>( n, a, b );
+	return NumericVector( n, ::Rf_rbeta, a, b ) ;
 }
-inline NumericVector rbeta_( int n, double a, double b ){
-	return rbeta__impl<false>( n, a, b );
-}
 
 } // Rcpp
 

Added: pkg/Rcpp/inst/include/Rcpp/stats/random/rbinom.h
===================================================================
--- pkg/Rcpp/inst/include/Rcpp/stats/random/rbinom.h	                        (rev 0)
+++ pkg/Rcpp/inst/include/Rcpp/stats/random/rbinom.h	2010-08-11 19:08:37 UTC (rev 1980)
@@ -0,0 +1,33 @@
+// -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; tab-width: 4 -*-
+//
+// rbinom.h: Rcpp R/C++ interface class library -- 
+//
+// Copyright (C) 2010 Douglas Bates, 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__stats__random_rbinom_h
+#define Rcpp__stats__random_rbinom_h
+
+namespace Rcpp {
+
+inline NumericVector rbinom( int n, double nin, double pp ){
+	return NumericVector( n, ::Rf_rbinom, nin, pp) ;
+}
+
+} // Rcpp
+
+#endif

Modified: pkg/Rcpp/inst/include/Rcpp/stats/random/rcauchy.h
===================================================================
--- pkg/Rcpp/inst/include/Rcpp/stats/random/rcauchy.h	2010-08-10 20:58:56 UTC (rev 1979)
+++ pkg/Rcpp/inst/include/Rcpp/stats/random/rcauchy.h	2010-08-11 19:08:37 UTC (rev 1980)
@@ -25,8 +25,7 @@
 namespace Rcpp {
 namespace stats {
 
-template <bool seed>
-class CauchyGenerator : public ::Rcpp::Generator<seed,double> {
+class CauchyGenerator : public ::Rcpp::Generator<false,double> {
 public:
 	
 	CauchyGenerator( double location_, double scale_) : 
@@ -41,22 +40,16 @@
 } ;
 } // stats
 
-template <bool seed>
-NumericVector rcauchy__impl( int n, double location, double scale ){
+// perhaps this should go to a cpp file
+inline NumericVector rcauchy( int n, double location, double scale ){
 	if (ISNAN(location) || !R_FINITE(scale) || scale < 0)
 		return NumericVector( n, R_NaN ) ;
 	
     if (scale == 0. || !R_FINITE(location))
     	return NumericVector( n, location ) ;
     
-	return NumericVector( n, stats::CauchyGenerator<seed>( location, scale ) ) ;
+	return NumericVector( n, stats::CauchyGenerator( location, scale ) ) ;
 }
-inline NumericVector rcauchy( int n, double location, double scale ){
-	return rcauchy__impl<true>( n, location, scale );
-}
-inline NumericVector rcauchy_( int n, double location, double scale ){
-	return rcauchy__impl<false>( n, location, scale );
-}
 
 } // Rcpp
 

Modified: pkg/Rcpp/inst/include/Rcpp/stats/random/rchisq.h
===================================================================
--- pkg/Rcpp/inst/include/Rcpp/stats/random/rchisq.h	2010-08-10 20:58:56 UTC (rev 1979)
+++ pkg/Rcpp/inst/include/Rcpp/stats/random/rchisq.h	2010-08-11 19:08:37 UTC (rev 1980)
@@ -25,8 +25,7 @@
 namespace Rcpp {
 namespace stats {
 
-template <bool seed>
-class ChisqGenerator : public ::Rcpp::Generator<seed,double> {
+class ChisqGenerator : public ::Rcpp::Generator<false,double> {
 public:
 	
 	ChisqGenerator( double df_ ) : df_2(df_ / 2.0) {}
@@ -40,17 +39,10 @@
 } ;
 } // stats
 
-template <bool seed>
-NumericVector rchisq__impl( int n, double df ){
+inline NumericVector rchisq( int n, double df ){
 	if (!R_FINITE(df) || df < 0.0) return NumericVector(n, R_NaN) ;
-	return NumericVector( n, stats::ChisqGenerator<seed>( df ) ) ;
+	return NumericVector( n, stats::ChisqGenerator( df ) ) ;
 }
-inline NumericVector rchisq( int n, double df ){
-	return rchisq__impl<true>( n, df );
-}
-inline NumericVector rchisq_( int n, double df ){
-	return rchisq__impl<false>( n, df );
-}
 
 } // Rcpp
 

Modified: pkg/Rcpp/inst/include/Rcpp/stats/random/rexp.h
===================================================================
--- pkg/Rcpp/inst/include/Rcpp/stats/random/rexp.h	2010-08-10 20:58:56 UTC (rev 1979)
+++ pkg/Rcpp/inst/include/Rcpp/stats/random/rexp.h	2010-08-11 19:08:37 UTC (rev 1980)
@@ -25,8 +25,7 @@
 namespace Rcpp {
 namespace stats {
 
-template <bool seed>
-class ExpGenerator : public ::Rcpp::Generator<seed,double> {
+class ExpGenerator : public ::Rcpp::Generator<false,double> {
 public:
 	
 	ExpGenerator( double scale_ ) : scale(scale_) {}
@@ -41,21 +40,17 @@
 
 } // stats
 
-template <bool seed>
-NumericVector rexp__impl( int n, double scale ){
+inline NumericVector rexp( int n, double rate ){
+	double scale = 1.0 / rate ;
 	if (!R_FINITE(scale) || scale <= 0.0) {
 		if(scale == 0.) return NumericVector( n, 0.0 ) ;
 		/* else */
 		return NumericVector( n, R_NaN ) ;
     }
-    return NumericVector( n, stats::ExpGenerator<seed>( scale ) ) ;
+    if( scale == 1.0 )
+    	return NumericVector( n, ::exp_rand ) ;
+    return NumericVector( n, stats::ExpGenerator( scale ) ) ;
 }
-inline NumericVector rexp( int n, double rate ){
-	return rexp__impl<true>( n, 1/rate );
-}
-inline NumericVector rexp_( int n, double rate ){
-	return rexp__impl<false>( n, 1/rate );
-}
 
 }
 

Modified: pkg/Rcpp/inst/include/Rcpp/stats/random/rf.h
===================================================================
--- pkg/Rcpp/inst/include/Rcpp/stats/random/rf.h	2010-08-10 20:58:56 UTC (rev 1979)
+++ pkg/Rcpp/inst/include/Rcpp/stats/random/rf.h	2010-08-11 19:08:37 UTC (rev 1980)
@@ -25,27 +25,25 @@
 namespace Rcpp {
 namespace stats {
 
-template <bool seed>
-class FGenerator_Finite_Finite : public ::Rcpp::Generator<seed,double> {
+
+class FGenerator_Finite_Finite : public ::Rcpp::Generator<false,double> {
 public:
 	
 	FGenerator_Finite_Finite( double n1_, double n2_ ) : 
-		n1(n1_), n2(n2_), n1__2(n1_ / 2.0 ), n2__2(n2_ / 2.0 ) {}
+		n1(n1_), n2(n2_), n1__2(n1_ / 2.0 ), n2__2(n2_ / 2.0 ), ratio(n2_/n1_) {}
 	
 	inline double operator()() const {
 		// here we know that both n1 and n2 are finite
 		// return ( ::rchisq( n1 ) / n1 ) / ( ::rchisq( n2 ) / n2 );
-		return 
-			( ::rgamma( n1__2, 2.0 ) / n1 ) / 
-			( ::rgamma( n2__2, 2.0 ) / n2 ) ;
+		return ratio * ::rgamma( n1__2, 2.0 ) / ::rgamma( n2__2, 2.0 ) ;
 	}
 	
 private:
-	double n1, n2, n1__2, n2__2 ;
+	double n1, n2, n1__2, n2__2, ratio ;
 } ;
 
-template <bool seed>
-class FGenerator_NotFinite_Finite : public ::Rcpp::Generator<seed,double> {
+
+class FGenerator_NotFinite_Finite : public ::Rcpp::Generator<false,double> {
 public:
 	
 	FGenerator_NotFinite_Finite( double n2_ ) : n2( n2_), n2__2(n2_ / 2.0 ) {}
@@ -59,8 +57,8 @@
 	double n2, n2__2 ;
 } ;
 
-template <bool seed>
-class FGenerator_Finite_NotFinite : public ::Rcpp::Generator<seed,double> {
+
+class FGenerator_Finite_NotFinite : public ::Rcpp::Generator<false,double> {
 public:
 	
 	FGenerator_Finite_NotFinite( double n1_ ) : n1(n1_), n1__2(n1_ / 2.0 ) {}
@@ -76,26 +74,19 @@
 
 } // stats 
 
-template <bool seed>
-NumericVector rf__impl( int n, double n1, double n2 ){
+inline NumericVector rf( int n, double n1, double n2 ){
 	if (ISNAN(n1) || ISNAN(n2) || n1 <= 0. || n2 <= 0.)
 		return NumericVector( n, R_NaN ) ;
 	if( R_FINITE( n1 ) && R_FINITE( n2 ) ){
-		return NumericVector( n, stats::FGenerator_Finite_Finite<seed>( n1, n2 ) ) ;
+		return NumericVector( n, stats::FGenerator_Finite_Finite( n1, n2 ) ) ;
 	} else if( ! R_FINITE( n1 ) && ! R_FINITE( n2 ) ){
 		return NumericVector( n, 1.0 ) ;
 	} else if( ! R_FINITE( n1 ) ) {
-		return NumericVector( n, stats::FGenerator_NotFinite_Finite<seed>( n2 ) ) ;
+		return NumericVector( n, stats::FGenerator_NotFinite_Finite( n2 ) ) ;
 	} else {
-		return NumericVector( n, stats::FGenerator_Finite_NotFinite<seed>( n1 ) ) ;	
+		return NumericVector( n, stats::FGenerator_Finite_NotFinite( n1 ) ) ;	
 	}
 }
-inline NumericVector rf( int n, double n1, double n2 ){
-	return rf__impl<true>( n, n1, n2 );
-}
-inline NumericVector rf_( int n, double n1, double n2 ){
-	return rf__impl<false>( n, n1, n2 );
-}
 
 } // Rcpp
 

Added: pkg/Rcpp/inst/include/Rcpp/stats/random/rgamma.h
===================================================================
--- pkg/Rcpp/inst/include/Rcpp/stats/random/rgamma.h	                        (rev 0)
+++ pkg/Rcpp/inst/include/Rcpp/stats/random/rgamma.h	2010-08-11 19:08:37 UTC (rev 1980)
@@ -0,0 +1,38 @@
+// -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; tab-width: 4 -*-
+//
+// rgammah: Rcpp R/C++ interface class library -- 
+//
+// Copyright (C) 2010 Douglas Bates, 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__stats__random_rgamma_h
+#define Rcpp__stats__random_rgamma_h
+
+namespace Rcpp {
+
+inline NumericVector rgamma( int n, double a, double scale ){
+	if (!R_FINITE(a) || !R_FINITE(scale) || a < 0.0 || scale <= 0.0) {
+		if(scale == 0.) return NumericVector( n, 0.) ;
+		return NumericVector( n, R_NaN ) ;
+    }
+    if( a == 0. ) return NumericVector(n, 0. ) ;
+	return NumericVector( n, ::Rf_rgamma, a, scale ) ;
+}
+
+} // Rcpp
+
+#endif

Added: pkg/Rcpp/inst/include/Rcpp/stats/random/rgeom.h
===================================================================
--- pkg/Rcpp/inst/include/Rcpp/stats/random/rgeom.h	                        (rev 0)
+++ pkg/Rcpp/inst/include/Rcpp/stats/random/rgeom.h	2010-08-11 19:08:37 UTC (rev 1980)
@@ -0,0 +1,50 @@
+// -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; tab-width: 4 -*-
+//
+// rgeom.h: Rcpp R/C++ interface class library -- 
+//
+// Copyright (C) 2010 Douglas Bates, 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__stats__random_rgeom_h
+#define Rcpp__stats__random_rgeom_h
+
+namespace Rcpp {
+namespace stats {
+
+class GeomGenerator : public ::Rcpp::Generator<false,double> {
+public:
+	
+	GeomGenerator( double p ) : lambda( (1-p)/p  ) {}
+	
+	inline double operator()() const {
+		return ::Rf_rpois(exp_rand() * lambda); 
+	}
+	
+private:
+	double lambda ;
+} ;
+} // stats
+
+inline NumericVector rgeom( int n, double p ){
+	if (!R_FINITE(p) || p <= 0 || p > 1) 
+		return NumericVector( n, R_NaN );
+	return NumericVector( n, stats::GeomGenerator( p ) ) ;
+}
+
+} // Rcpp
+
+#endif

Added: pkg/Rcpp/inst/include/Rcpp/stats/random/rhyper.h
===================================================================
--- pkg/Rcpp/inst/include/Rcpp/stats/random/rhyper.h	                        (rev 0)
+++ pkg/Rcpp/inst/include/Rcpp/stats/random/rhyper.h	2010-08-11 19:08:37 UTC (rev 1980)
@@ -0,0 +1,33 @@
+// -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; tab-width: 4 -*-
+//
+// rhyper.h: Rcpp R/C++ interface class library -- 
+//
+// Copyright (C) 2010 Douglas Bates, 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__stats__random_rhyper_h
+#define Rcpp__stats__random_rhyper_h
+
+namespace Rcpp {
+
+inline NumericVector rhyper( int n, double nn1, double nn2, double kk ){
+	return NumericVector( n, ::Rf_rhyper, a, b ) ;
+}
+
+} // Rcpp
+
+#endif

Added: pkg/Rcpp/inst/include/Rcpp/stats/random/rlnorm.h
===================================================================
--- pkg/Rcpp/inst/include/Rcpp/stats/random/rlnorm.h	                        (rev 0)
+++ pkg/Rcpp/inst/include/Rcpp/stats/random/rlnorm.h	2010-08-11 19:08:37 UTC (rev 1980)
@@ -0,0 +1,57 @@
+// -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; tab-width: 4 -*-
+//
+// rlnorm.h: Rcpp R/C++ interface class library -- 
+//
+// Copyright (C) 2010 Douglas Bates, 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__stats__random_norm_h
+#define Rcpp__stats__random_norm_h
+
+namespace Rcpp {
+namespace stats {
+
+class LNormGenerator : public Generator<false,double> {
+public:
+	
+	LNormGenerator( double meanlog_ = 0.0 , double sdlog_ = 1.0 ) : 
+		meanlog(meanlog_), sdlog(sdlog_) {}
+	
+	inline double operator()() const {
+		return exp( meanlog + sdlog * ::norm_rand() ) ;
+	}
+	
+private:
+	double meanlog ;
+	double sdlog ;
+} ;
+} // stats
+
+inline NumericVector rlnorm( int n, double meanlog = 0.0, double sdlog = 1.0 ){
+	if (ISNAN(meanlog) || !R_FINITE(sdlog) || sdlog < 0.){
+		// TODO: R also throws a warning in that case, should we ?
+		return NumericVector( n, R_NaN ) ;
+	}  else if (sdlog == 0. || !R_FINITE(meanlog)){
+		return NumericVector( n, exp( mean ) ) ;
+	} else {
+		return NumericVector( n, stats::LNormGenerator( meanlog, sdlog ) ); 
+	}
+}
+
+} // Rcpp
+
+#endif

Added: pkg/Rcpp/inst/include/Rcpp/stats/random/rlogis.h
===================================================================
--- pkg/Rcpp/inst/include/Rcpp/stats/random/rlogis.h	                        (rev 0)
+++ pkg/Rcpp/inst/include/Rcpp/stats/random/rlogis.h	2010-08-11 19:08:37 UTC (rev 1980)
@@ -0,0 +1,57 @@
+// -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; tab-width: 4 -*-
+//
+// rlogis.h: Rcpp R/C++ interface class library -- 
+//
+// Copyright (C) 2010 Douglas Bates, 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__stats__random_rlogis_h
+#define Rcpp__stats__random_rlogis_h
+
+namespace Rcpp {
+namespace stats {
+
+class LogisGenerator : public ::Rcpp::Generator<false,double> {
+public:
+	
+	LogisGenerator( double location_, double scale_ ) : 
+		location(location_), scale(scale_) {}
+	
+	inline double operator()() const {
+		double u = unif_rand() ;
+		return location + scale * log(u / (1. - u));
+	}
+	
+private:
+	double location ;
+	double scale ;
+} ;
+} // stats
+
+inline NumericVector rlogis( int n, double location, double scale ){
+	if (ISNAN(location) || !R_FINITE(scale))
+	return NumericVector( n, R_NaN ) ;
+
+    if (scale == 0. || !R_FINITE(location))
+	return NumericVector( n, location );
+    
+	return NumericVector( n, stats::LogisGenerator( location, scale ) ) ;
+}
+
+} // Rcpp
+
+#endif

Added: pkg/Rcpp/inst/include/Rcpp/stats/random/rnbinom.h
===================================================================
--- pkg/Rcpp/inst/include/Rcpp/stats/random/rnbinom.h	                        (rev 0)
+++ pkg/Rcpp/inst/include/Rcpp/stats/random/rnbinom.h	2010-08-11 19:08:37 UTC (rev 1980)
@@ -0,0 +1,54 @@
+// -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; tab-width: 4 -*-
+//
+// rnbinom.h: Rcpp R/C++ interface class library -- 
+//
+// Copyright (C) 2010 Douglas Bates, 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__stats__random_rnbinom_h
+#define Rcpp__stats__random_rnbinom_h
+
+namespace Rcpp {
+namespace stats {
+
+class NBinomGenerator : public ::Rcpp::Generator<false,double> {
+public:
+	
+	NBinomGenerator( double siz_, double prob_ ) : 
+		siz(siz_), lambda( (1-prob_)/prob_ ) {}
+	
+	inline double operator()() const {
+		return ::Rf_rpois( ::Rf_rgamma( siz, lambda ) ) ; 
+	}
+	
+private:
+	double siz ;
+	double lambda ;
+} ;
+} // stats
+
+inline NumericVector rnbinom( int n, double siz, double prob ){
+	if(!R_FINITE(siz) || !R_FINITE(prob) || siz <= 0 || prob <= 0 || prob > 1)
+		/* prob = 1 is ok, PR#1218 */
+		return NumericVector( n, R_NaN ) ;
+    
+	return NumericVector( n, stats::NBinomGenerator( siz, prob ) ) ;
+}
+
+} // Rcpp
+
+#endif

Added: pkg/Rcpp/inst/include/Rcpp/stats/random/rnbinom_mu.h
===================================================================
--- pkg/Rcpp/inst/include/Rcpp/stats/random/rnbinom_mu.h	                        (rev 0)
+++ pkg/Rcpp/inst/include/Rcpp/stats/random/rnbinom_mu.h	2010-08-11 19:08:37 UTC (rev 1980)
@@ -0,0 +1,53 @@
+// -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; tab-width: 4 -*-
+//
+// rnbinom_mu.h: Rcpp R/C++ interface class library -- 
+//
+// Copyright (C) 2010 Douglas Bates, 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__stats__random_rnbinom_mu_h
+#define Rcpp__stats__random_rnbinom_mu_h
+
+namespace Rcpp {
+namespace stats {
+
+class NBinomGenerator_Mu : public ::Rcpp::Generator<false,double> {
+public:
+	
+	NBinomGenerator_Mu( double siz_, double mu_ ) : 
+		siz(siz_), lambda( mu_ / siz_ ) {}
+	
+	inline double operator()() const {
+		return ::Rf_rpois( ::Rf_rgamma( siz, lambda ) ) ; 
+	}
+	
+private:
+	double siz ;
+	double lambda ;
+} ;
+} // stats
+
+inline NumericVector rnbinom_mu( int n, double siz, double mu ){
+	if(!R_FINITE(siz) || !R_FINITE(mu) || siz <= 0 || mu < 0)
+		return NumericVector( n, R_NaN ) ;
+    
+	return NumericVector( n, stats::NBinomGenerator_Mu( siz, mu ) ) ;
+}
+
+} // Rcpp
+
+#endif

Added: pkg/Rcpp/inst/include/Rcpp/stats/random/rnchisq.h
===================================================================
--- pkg/Rcpp/inst/include/Rcpp/stats/random/rnchisq.h	                        (rev 0)
+++ pkg/Rcpp/inst/include/Rcpp/stats/random/rnchisq.h	2010-08-11 19:08:37 UTC (rev 1980)
@@ -0,0 +1,64 @@
+// -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; tab-width: 4 -*-
+//
+// rnchisq.h: Rcpp R/C++ interface class library -- 
+//
+// Copyright (C) 2010 Douglas Bates, 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__stats__random_rnchisq_h
+#define Rcpp__stats__random_rnchisq_h
+
+namespace Rcpp {
+namespace stats {
+
+class NChisqGenerator : public ::Rcpp::Generator<false,double> {
+public:
+	
+	NChisqGenerator( double df_, double lambda_ ) : 
+		df(df_), df_2(df_ / 2.0), lambda_2(lambda_ / 2.0 ) {}
+	
+	inline double operator()() const {
+		double r = ::Rf_rpois( lambda_2 ) ;
+		// if( r > 0.0 ) r = Rf_rchisq( 2. * r ) ;
+		// replace by so that we can skip the tests in rchisq
+		// because there is no point in doing them as we know the 
+		// outcome for sure
+		if( r > 0.0 ) r = ::Rf_rgamma( r, 2. ) ;
+		if (df > 0.) r += ::Rf_rgamma( df_2, 2.);
+		return r;
+    }
+	
+private:
+	double df ;
+	double df_2 ;
+	double lambda_2 ;
+} ;
+} // stats
+
+inline NumericVector rnchisq( int n, double df, double lambda = 1.0 ){
+	if (!R_FINITE(df) || !R_FINITE(lambda) || df < 0. || lambda < 0.) 
+		return NumericVector(n, R_NaN) ;
+	if( lambda == 0.0 ){
+		// using the central generator, see rchisq.h
+		return NumericVector( n, stats::ChisqGenerator( df ) ) ;
+	}
+	return NumericVector( n, stats::NChisqGenerator( df, lambda ) ) ;
+}
+
+} // Rcpp
+
+#endif

Modified: pkg/Rcpp/inst/include/Rcpp/stats/random/rnorm.h
===================================================================
--- pkg/Rcpp/inst/include/Rcpp/stats/random/rnorm.h	2010-08-10 20:58:56 UTC (rev 1979)
+++ pkg/Rcpp/inst/include/Rcpp/stats/random/rnorm.h	2010-08-11 19:08:37 UTC (rev 1980)
@@ -25,8 +25,8 @@
 namespace Rcpp {
 namespace stats {
 
-template <bool seed>
-class NormGenerator : public Generator<seed,double> {
+
+class NormGenerator : public Generator<false,double> {
 public:
 	
 	NormGenerator( double mean_ = 0.0 , double sd_ = 1.0 ) : 
@@ -40,25 +40,59 @@
 	double mean ;
 	double sd ;
 } ;
+
+
+class NormGenerator__sd1 : public Generator<false,double> {
+public:
+	
+	NormGenerator__sd1( double mean_ = 0.0 ) : mean(mean_) {}
+	
+	inline double operator()() const {
+		return mean + ::norm_rand() ;
+	}
+	
+private:
+	double mean ;
+} ;
+
+
+class NormGenerator__mean0 : public Generator<false,double> {
+public:
+	
+	NormGenerator__mean0( double sd_ = 1.0 ) : sd(sd_) {}
+	
+	inline double operator()() const {
+		return sd * ::norm_rand() ;
+	}
+	
+private:
+	double sd ;
+} ;
+
+
 } // stats
 
-template <bool seed>
-NumericVector rnorm__impl( int n, double mean, double sd ){
+inline NumericVector rnorm( int n, double mean = 0.0, double sd = 1.0 ){
 	if (ISNAN(mean) || !R_FINITE(sd) || sd < 0.){
 		// TODO: R also throws a warning in that case, should we ?
 		return NumericVector( n, R_NaN ) ;
 	}  else if (sd == 0. || !R_FINITE(mean)){
 		return NumericVector( n, mean ) ;
 	} else {
-		return NumericVector( n, stats::NormGenerator<seed>( mean, sd ) ); 
+		bool sd1 = sd == 1.0 ; 
+		bool mean0 = mean == 0.0 ;
+		if( sd1 && mean0 ){
+			return NumericVector( n, norm_rand ) ;
+		} else if( sd1 ){
+			return NumericVector( n, stats::NormGenerator__sd1( mean ) );
+		} else if( mean0 ){
+			return NumericVector( n, stats::NormGenerator__mean0( sd ) );
+		} else {
+			// general case
+			return NumericVector( n, stats::NormGenerator( mean, sd ) );
+		}
 	}
 }
-inline NumericVector rnorm( int n, double mean = 0.0, double sd = 1.0 ){
-	return rnorm__impl<true>( n, mean, sd );
-}
-inline NumericVector rnorm_( int n, double mean = 0.0, double sd = 1.0 ){
-	return rnorm__impl<false>( n, mean, sd );
-}
 
 } // Rcpp
 

Added: pkg/Rcpp/inst/include/Rcpp/stats/random/rpois.h
===================================================================
--- pkg/Rcpp/inst/include/Rcpp/stats/random/rpois.h	                        (rev 0)
+++ pkg/Rcpp/inst/include/Rcpp/stats/random/rpois.h	2010-08-11 19:08:37 UTC (rev 1980)
@@ -0,0 +1,33 @@
+// -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; tab-width: 4 -*-
+//
+// rpois.h: Rcpp R/C++ interface class library -- 
+//
+// Copyright (C) 2010 Douglas Bates, 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__stats__random_rpois_h
+#define Rcpp__stats__random_rpois_h
+
+namespace Rcpp {
+
+inline NumericVector rpois( int n, double mu ){
[TRUNCATED]

To get the complete diff run:
    svnlook diff /svnroot/rcpp -r 1980


More information about the Rcpp-commits mailing list