[Rcpp-commits] r1923 - in pkg/Rcpp/inst: include/Rcpp/stats unitTests

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Thu Aug 5 21:22:34 CEST 2010


Author: edd
Date: 2010-08-05 21:22:34 +0200 (Thu, 05 Aug 2010)
New Revision: 1923

Added:
   pkg/Rcpp/inst/include/Rcpp/stats/t.h
Modified:
   pkg/Rcpp/inst/include/Rcpp/stats/stats.h
   pkg/Rcpp/inst/unitTests/runit.stats.R
Log:
added dt() with a unit test (note that is only dt without the ncp parameter)


Modified: pkg/Rcpp/inst/include/Rcpp/stats/stats.h
===================================================================
--- pkg/Rcpp/inst/include/Rcpp/stats/stats.h	2010-08-05 17:24:49 UTC (rev 1922)
+++ pkg/Rcpp/inst/include/Rcpp/stats/stats.h	2010-08-05 19:22:34 UTC (rev 1923)
@@ -25,5 +25,6 @@
 #include <Rcpp/stats/binom.h>
 #include <Rcpp/stats/pois.h>
 #include <Rcpp/stats/norm.h>
+#include <Rcpp/stats/t.h>
 
 #endif

Added: pkg/Rcpp/inst/include/Rcpp/stats/t.h
===================================================================
--- pkg/Rcpp/inst/include/Rcpp/stats/t.h	                        (rev 0)
+++ pkg/Rcpp/inst/include/Rcpp/stats/t.h	2010-08-05 19:22:34 UTC (rev 1923)
@@ -0,0 +1,62 @@
+// -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; tab-width: 4 -*-
+//
+// t.h: Rcpp R/C++ interface class library -- t distribution
+//
+// 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__stats__t_h
+#define Rcpp__stats__t_h
+
+namespace Rcpp {
+
+namespace stats {
+
+namespace impl {
+
+	template <bool NA, typename T>
+	class DT : public Rcpp::VectorBase< REALSXP, NA, DT<NA,T> >{
+	public:
+		typedef typename Rcpp::VectorBase<REALSXP,NA,T> VEC_TYPE;
+	
+		DT( const VEC_TYPE& vec_, double df_, bool log_ = false ) : 
+			vec(vec_), df(df_), log(log_) {}
+		
+		inline double operator[]( int i) const {
+			return ::dt( vec[i], df, log );
+		}
+		
+		inline int size() const { return vec.size(); }
+		
+	private:
+		const VEC_TYPE& vec;
+		double df;
+		int log;
+	
+	};
+	
+} // impl
+
+template <bool NA, typename T>
+inline impl::DT<NA,T> dt( const Rcpp::VectorBase<REALSXP,NA,T>& x, double df, bool log = false ) {
+	return impl::DT<NA,T>( x, df, log ); 
+}
+	
+}
+}
+
+#endif


Property changes on: pkg/Rcpp/inst/include/Rcpp/stats/t.h
___________________________________________________________________
Added: svn:eol-style
   + native

Modified: pkg/Rcpp/inst/unitTests/runit.stats.R
===================================================================
--- pkg/Rcpp/inst/unitTests/runit.stats.R	2010-08-05 17:24:49 UTC (rev 1922)
+++ pkg/Rcpp/inst/unitTests/runit.stats.R	2010-08-05 19:22:34 UTC (rev 1923)
@@ -51,15 +51,26 @@
 						_["true"]  = stats::dnorm( xx, 0.0, 1.0, true )
 						) ;
 				'
+			),
+			"runit_dt" = list(
+				signature( x = "numeric" ),
+				'
+					NumericVector xx(x) ;
+					return List::create(
+						_["false"] = stats::dt( xx, 5),
+						_["true"]  = stats::dt( xx, 5, true )
+						) ;
+				'
 			)
 
+
 		)
 
 		signatures <- lapply( f, "[[", 1L )
 		bodies <- lapply( f, "[[", 2L )
 		fx <- cxxfunction( signatures, bodies, plugin = "Rcpp")
 		getDynLib( fx ) # just forcing loading the dll now
-		assign( ".rcpp.stats", fx, globalenv() ) 
+		assign( ".rcpp.stats", fx, globalenv() )
 	}
 }
 
@@ -85,3 +96,11 @@
                 msg = "stats.dnorm" )
 }
 
+test.stats.dt <- function( ) {
+	fx <- .rcpp.stats$runit_dt
+    v <- seq(0.0, 1.0, by=0.1)
+    checkEquals(fx(v),
+                list( false = dt(v, 5), true = dt(v, 5, log=TRUE ) ), # NB: need log=TRUE here
+                msg = "stats.dt" )
+}
+



More information about the Rcpp-commits mailing list