|
Rcpp Version 0.9.10
|
00001 // -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; tab-width: 4 -*- 00002 // 00003 // rlnorm.h: Rcpp R/C++ interface class library -- 00004 // 00005 // Copyright (C) 2010 - 2011 Douglas Bates, Dirk Eddelbuettel and Romain Francois 00006 // 00007 // This file is part of Rcpp. 00008 // 00009 // Rcpp is free software: you can redistribute it and/or modify it 00010 // under the terms of the GNU General Public License as published by 00011 // the Free Software Foundation, either version 2 of the License, or 00012 // (at your option) any later version. 00013 // 00014 // Rcpp is distributed in the hope that it will be useful, but 00015 // WITHOUT ANY WARRANTY; without even the implied warranty of 00016 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00017 // GNU General Public License for more details. 00018 // 00019 // You should have received a copy of the GNU General Public License 00020 // along with Rcpp. If not, see <http://www.gnu.org/licenses/>. 00021 00022 #ifndef Rcpp__stats__random_rlnorm_h 00023 #define Rcpp__stats__random_rlnorm_h 00024 00025 namespace Rcpp { 00026 namespace stats { 00027 00028 class LNormGenerator : public Generator<false,double> { 00029 public: 00030 00031 LNormGenerator( double meanlog_ = 0.0 , double sdlog_ = 1.0 ) : 00032 meanlog(meanlog_), sdlog(sdlog_) {} 00033 00034 inline double operator()() const { 00035 return ::exp( meanlog + sdlog * ::norm_rand() ) ; 00036 } 00037 00038 private: 00039 double meanlog ; 00040 double sdlog ; 00041 } ; 00042 00043 class LNormGenerator_1 : public Generator<false,double> { 00044 public: 00045 00046 LNormGenerator_1( double meanlog_ = 0.0 ) : 00047 meanlog(meanlog_) {} 00048 00049 inline double operator()() const { 00050 return ::exp( meanlog + ::norm_rand() ) ; 00051 } 00052 00053 private: 00054 double meanlog ; 00055 } ; 00056 00057 class LNormGenerator_0 : public Generator<false,double> { 00058 public: 00059 00060 LNormGenerator_0( ) {} 00061 00062 inline double operator()() const { 00063 return ::exp(::norm_rand() ) ; 00064 } 00065 00066 } ; 00067 00068 } // stats 00069 00070 // Please make sure you to read Section 6.3 of "Writing R Extensions" 00071 // about the need to call GetRNGstate() and PutRNGstate() when using 00072 // the random number generators provided by R. 00073 inline NumericVector rlnorm( int n, double meanlog, double sdlog ){ 00074 if (ISNAN(meanlog) || !R_FINITE(sdlog) || sdlog < 0.){ 00075 // TODO: R also throws a warning in that case, should we ? 00076 return NumericVector( n, R_NaN ) ; 00077 } else if (sdlog == 0. || !R_FINITE(meanlog)){ 00078 return NumericVector( n, ::exp( meanlog ) ) ; 00079 } else { 00080 return NumericVector( n, stats::LNormGenerator( meanlog, sdlog ) ); 00081 } 00082 } 00083 00084 // Please make sure you to read Section 6.3 of "Writing R Extensions" 00085 // about the need to call GetRNGstate() and PutRNGstate() when using 00086 // the random number generators provided by R. 00087 inline NumericVector rlnorm( int n, double meanlog /*, double sdlog = 1.0 */){ 00088 if (ISNAN(meanlog) ){ 00089 // TODO: R also throws a warning in that case, should we ? 00090 return NumericVector( n, R_NaN ) ; 00091 } else if ( !R_FINITE(meanlog)){ 00092 return NumericVector( n, ::exp( meanlog ) ) ; 00093 } else { 00094 return NumericVector( n, stats::LNormGenerator_1( meanlog ) ); 00095 } 00096 } 00097 00098 // Please make sure you to read Section 6.3 of "Writing R Extensions" 00099 // about the need to call GetRNGstate() and PutRNGstate() when using 00100 // the random number generators provided by R. 00101 inline NumericVector rlnorm( int n /*, double meanlog [=0.], double sdlog = 1.0 */){ 00102 return NumericVector( n, stats::LNormGenerator_0( ) ); 00103 } 00104 00105 00106 } // Rcpp 00107 00108 #endif