Rcpp Version 1.0.14
Loading...
Searching...
No Matches
rnchisq.h
Go to the documentation of this file.
1// -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; indent-tabs-mode: nil; -*-
2//
3// rnchisq.h: Rcpp R/C++ interface class library --
4//
5// Copyright (C) 2010 - 2016 Douglas Bates, Dirk Eddelbuettel and Romain Francois
6//
7// This file is part of Rcpp.
8//
9// Rcpp is free software: you can redistribute it and/or modify it
10// under the terms of the GNU General Public License as published by
11// the Free Software Foundation, either version 2 of the License, or
12// (at your option) any later version.
13//
14// Rcpp is distributed in the hope that it will be useful, but
15// WITHOUT ANY WARRANTY; without even the implied warranty of
16// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17// GNU General Public License for more details.
18//
19// You should have received a copy of the GNU General Public License
20// along with Rcpp. If not, see <http://www.gnu.org/licenses/>.
21
22#ifndef Rcpp__stats__random_rnchisq_h
23#define Rcpp__stats__random_rnchisq_h
24
25namespace Rcpp {
26namespace stats {
27
28class NChisqGenerator : public ::Rcpp::Generator<double> {
29public:
30
31 NChisqGenerator( double df_, double lambda_ ) :
32 df(df_), df_2(df_ / 2.0), lambda_2(lambda_ / 2.0 ) {}
33
34 inline double operator()() const {
35 double r = ::Rf_rpois( lambda_2 ) ;
36 // if( r > 0.0 ) r = Rf_rchisq( 2. * r ) ;
37 // replace by so that we can skip the tests in rchisq
38 // because there is no point in doing them as we know the
39 // outcome for sure
40 if( r > 0.0 ) r = ::Rf_rgamma( r, 2. ) ;
41 if (df > 0.) r += ::Rf_rgamma( df_2, 2.);
42 return r;
43 }
44
45private:
46 double df ;
47 double df_2 ;
48 double lambda_2 ;
49};
50
51} // stats
52} // Rcpp
53
54#endif
double operator()() const
Definition rnchisq.h:34
NChisqGenerator(double df_, double lambda_)
Definition rnchisq.h:31
Rcpp API.
Definition algo.h:28
T as(SEXP x)
Definition as.h:151