[Rcpp-commits] r1964 - in pkg/Rcpp/inst: . include/Rcpp/stats/random

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Tue Aug 10 09:00:36 CEST 2010


Author: romain
Date: 2010-08-10 09:00:35 +0200 (Tue, 10 Aug 2010)
New Revision: 1964

Added:
   pkg/Rcpp/inst/include/Rcpp/stats/random/rchisq.h
   pkg/Rcpp/inst/include/Rcpp/stats/random/rexp.h
Modified:
   pkg/Rcpp/inst/ChangeLog
   pkg/Rcpp/inst/include/Rcpp/stats/random/random.h
Log:
rchisq and rexp

Modified: pkg/Rcpp/inst/ChangeLog
===================================================================
--- pkg/Rcpp/inst/ChangeLog	2010-08-10 06:49:05 UTC (rev 1963)
+++ pkg/Rcpp/inst/ChangeLog	2010-08-10 07:00:35 UTC (rev 1964)
@@ -8,6 +8,10 @@
 
 	* inst/include/Rcpp/stats/random/rcauchy.h: Added rcauchy and rcauchy_
 
+	* inst/include/Rcpp/stats/random/rchisq.h: Added rchisq and rchisq_
+
+	* inst/include/Rcpp/stats/random/rchisq.h: Added rexp and rexp_
+
 	* inst/include/Rcpp/stats/stats.h : fixed name clash reported on Rcpp-devel
 	http://permalink.gmane.org/gmane.comp.lang.r.rcpp/610
 

Modified: pkg/Rcpp/inst/include/Rcpp/stats/random/random.h
===================================================================
--- pkg/Rcpp/inst/include/Rcpp/stats/random/random.h	2010-08-10 06:49:05 UTC (rev 1963)
+++ pkg/Rcpp/inst/include/Rcpp/stats/random/random.h	2010-08-10 07:00:35 UTC (rev 1964)
@@ -26,5 +26,7 @@
 #include <Rcpp/stats/random/rnorm.h>
 #include <Rcpp/stats/random/rcauchy.h>
 #include <Rcpp/stats/random/runif.h>
+#include <Rcpp/stats/random/rchisq.h>
+#include <Rcpp/stats/random/rexp.h>
 
 #endif

Added: pkg/Rcpp/inst/include/Rcpp/stats/random/rchisq.h
===================================================================
--- pkg/Rcpp/inst/include/Rcpp/stats/random/rchisq.h	                        (rev 0)
+++ pkg/Rcpp/inst/include/Rcpp/stats/random/rchisq.h	2010-08-10 07:00:35 UTC (rev 1964)
@@ -0,0 +1,57 @@
+// -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; tab-width: 4 -*-
+//
+// rchisq.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_rchisq_h
+#define Rcpp__stats__random_rchisq_h
+
+namespace Rcpp {
+namespace stats {
+
+template <bool seed>
+class ChisqGenerator : public Rcpp::Generator<seed,double> {
+public:
+	
+	ChisqGenerator( double df_ ) : df_2(df_ / 2.0) {}
+	
+	inline double operator()() const {
+		return ::rgamma( df_2, 2.0 ) ; 
+	}
+	
+private:
+	double df_2 ;
+} ;
+
+template <bool seed>
+Rcpp::NumericVector rchisq__impl( int n, double df ){
+	if (!R_FINITE(df) || df < 0.0) return Rcpp::NumericVector(n, R_NaN) ;
+	return Rcpp::NumericVector( n, ChisqGenerator<seed>( df ) ) ;
+}
+inline Rcpp::NumericVector rchisq( int n, double df ){
+	return rchisq__impl<true>( n, df );
+}
+inline Rcpp::NumericVector rchisq_( int n, double df ){
+	return rchisq__impl<false>( n, df );
+}
+
+}
+}
+
+#endif

Added: pkg/Rcpp/inst/include/Rcpp/stats/random/rexp.h
===================================================================
--- pkg/Rcpp/inst/include/Rcpp/stats/random/rexp.h	                        (rev 0)
+++ pkg/Rcpp/inst/include/Rcpp/stats/random/rexp.h	2010-08-10 07:00:35 UTC (rev 1964)
@@ -0,0 +1,61 @@
+// -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; tab-width: 4 -*-
+//
+// rexp.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_rexp_h
+#define Rcpp__stats__random_rexp_h
+
+namespace Rcpp {
+namespace stats {
+
+template <bool seed>
+class ExpGenerator : public Rcpp::Generator<seed,double> {
+public:
+	
+	ExpGenerator( double scale_ ) : scale(scale_) {}
+	
+	inline double operator()() const {
+		return scale * exp_rand() ;
+	}
+	
+private:
+	double rate ;
+} ;
+
+template <bool seed>
+Rcpp::NumericVector rexp__impl( int n, double scale ){
+	if (!R_FINITE(scale) || scale <= 0.0) {
+		if(scale == 0.) return Rcpp::NumericVector( n, 0.0 ) ;
+		/* else */
+		return Rcpp::NumericVector( n, R_NaN ) ;
+    }
+    return Rcpp::NumericVector( n, ExpGenerator<seed>( scale ) ) ;
+}
+inline Rcpp::NumericVector rexp( int n, double rate ){
+	return rexp__impl<true>( n, 1/rate );
+}
+inline Rcpp::NumericVector rexp_( int n, double rate ){
+	return rexp__impl<false>( n, 1/rate );
+}
+
+}
+}
+
+#endif



More information about the Rcpp-commits mailing list