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

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Tue Aug 10 07:53:11 CEST 2010


Author: romain
Date: 2010-08-10 07:53:10 +0200 (Tue, 10 Aug 2010)
New Revision: 1958

Added:
   pkg/Rcpp/inst/include/Rcpp/stats/random/
   pkg/Rcpp/inst/include/Rcpp/stats/random/random.h
   pkg/Rcpp/inst/include/Rcpp/stats/random/rnorm.h
Modified:
   pkg/Rcpp/inst/ChangeLog
   pkg/Rcpp/inst/include/Rcpp/stats/stats.h
Log:
added rnorm(int,double,double)

Modified: pkg/Rcpp/inst/ChangeLog
===================================================================
--- pkg/Rcpp/inst/ChangeLog	2010-08-09 22:41:56 UTC (rev 1957)
+++ pkg/Rcpp/inst/ChangeLog	2010-08-10 05:53:10 UTC (rev 1958)
@@ -1,3 +1,7 @@
+2010-08-10  Romain Francois <romain at r-enthusiasts.com>
+
+	* inst/include/Rcpp/stats/random/rnorm.h: Added rnorm and rnorm_
+
 2010-08-09  Romain Francois <romain at r-enthusiasts.com>
 
 	* inst/include/Rcpp/stats/lnorm.h: Added d-p-q (lnorm|weibull|logis|f)

Added: pkg/Rcpp/inst/include/Rcpp/stats/random/random.h
===================================================================
--- pkg/Rcpp/inst/include/Rcpp/stats/random/random.h	                        (rev 0)
+++ pkg/Rcpp/inst/include/Rcpp/stats/random/random.h	2010-08-10 05:53:10 UTC (rev 1958)
@@ -0,0 +1,27 @@
+// -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; tab-width: 4 -*-
+//
+// random.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_random_h
+#define Rcpp__stats__random_random_h
+
+#include <Rcpp/stats/random/rnorm.h>
+
+#endif

Added: pkg/Rcpp/inst/include/Rcpp/stats/random/rnorm.h
===================================================================
--- pkg/Rcpp/inst/include/Rcpp/stats/random/rnorm.h	                        (rev 0)
+++ pkg/Rcpp/inst/include/Rcpp/stats/random/rnorm.h	2010-08-10 05:53:10 UTC (rev 1958)
@@ -0,0 +1,65 @@
+// -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; tab-width: 4 -*-
+//
+// rnorm.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 {
+
+template <bool seed>
+class NormGenerator : public Rcpp::Generator<seed,double> {
+public:
+	
+	NormGenerator( double mean = 0.0 , double sd = 1.0 ) : 
+		mean(mean), sd(sd) {}
+	
+	inline double operator()() const {
+		return mean + sd * ::norm_rand() ;
+	}
+	
+private:
+	double mean ;
+	double sd ;
+} ;
+
+template <bool seed>
+Rcpp::NumericVector rnorm__impl( int n, double mean, double sd ){
+	if (ISNAN(mean) || !R_FINITE(sd) || sd < 0.){
+		// TODO: R also throws a warning in that case, should we ?
+		return Rcpp::NumericVector( n, R_NaN ) ;
+	}  else if (sd == 0. || !R_FINITE(mean)){
+		return Rcpp::NumericVector( n, mean ) ;
+	} else {
+		return Rcpp::NumericVector( n, NormGenerator<seed>( mean, sd ) ); 
+	}
+}
+inline Rcpp::NumericVector rnorm( int n, double mean = 0.0, double sd = 1.0 ){
+	return rnorm__impl<true>( n, mean, sd );
+}
+inline Rcpp::NumericVector rnorm_( int n, double mean = 0.0, double sd = 1.0 ){
+	return rnorm__impl<false>( n, mean, sd );
+}
+
+}
+}
+
+#endif

Modified: pkg/Rcpp/inst/include/Rcpp/stats/stats.h
===================================================================
--- pkg/Rcpp/inst/include/Rcpp/stats/stats.h	2010-08-09 22:41:56 UTC (rev 1957)
+++ pkg/Rcpp/inst/include/Rcpp/stats/stats.h	2010-08-10 05:53:10 UTC (rev 1958)
@@ -36,4 +36,6 @@
 #include <Rcpp/stats/binom.h>
 #include <Rcpp/stats/pois.h>
 
+#include <Rcpp/stats/random/random.h>
+
 #endif



More information about the Rcpp-commits mailing list