Rcpp Version 0.9.10
norm.h
Go to the documentation of this file.
00001 // -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; tab-width: 4 -*-
00002 //
00003 // norm.h: Rcpp R/C++ interface class library -- normal distribution
00004 //
00005 // Copyright (C) 2010 - 2011 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__norm_h
00023 #define Rcpp__stats__norm_h
00024 
00025 namespace Rcpp {
00026 namespace stats {
00027 
00028 inline double dnorm_1(double x, double mu /*, double sigma [=1.0]*/ , int give_log) {
00029 #ifdef IEEE_754
00030     if (ISNAN(x) || ISNAN(mu) )
00031         return x + mu + 1.0 ;
00032 #endif
00033     if(!R_FINITE(x) && mu == x) return ML_NAN;/* x-mu is NaN */
00034     x = (x - mu) ;
00035 
00036     if(!R_FINITE(x)) return R_D__0;
00037     return (give_log ?
00038             -(M_LN_SQRT_2PI  +  0.5 * x * x ) :
00039             M_1_SQRT_2PI * ::exp(-0.5 * x * x) );
00040     /* M_1_SQRT_2PI = 1 / sqrt(2 * pi) */
00041 }       
00042 
00043 inline double dnorm_0(double x /*, double mu [=0.0], double sigma [=1.0]*/ , int give_log) {
00044 #ifdef IEEE_754
00045     if (ISNAN(x) )
00046         return x + 1.0 ;
00047 #endif
00048         if(!R_FINITE(x)) return R_D__0;
00049     return (give_log ?
00050             -(M_LN_SQRT_2PI  +  0.5 * x * x ) :
00051             M_1_SQRT_2PI * ::exp(-0.5 * x * x) );
00052     /* M_1_SQRT_2PI = 1 / sqrt(2 * pi) */
00053 }       
00054 
00055 inline double pnorm_1(double x, double mu /*, double sigma [=1.]*/ , int lower_tail, int log_p){
00056     double p, cp;
00057 
00058     /* Note: The structure of these checks has been carefully thought through.
00059      * For example, if x == mu and sigma == 0, we get the correct answer 1.
00060      */
00061 #ifdef IEEE_754
00062     if(ISNAN(x) || ISNAN(mu) )
00063         return x + mu + 1.0 ;
00064 #endif
00065     if(!R_FINITE(x) && mu == x) return ML_NAN;/* x-mu is NaN */
00066     p = (x - mu) ;
00067     if(!R_FINITE(p))
00068         return (x < mu) ? R_DT_0 : R_DT_1;
00069     x = p;
00070 
00071     ::Rf_pnorm_both(x, &p, &cp, (lower_tail ? 0 : 1), log_p);
00072 
00073     return(lower_tail ? p : cp);
00074 }
00075 
00076 inline double pnorm_0(double x /*, double mu [=0.] , double sigma [=1.]*/ , int lower_tail, int log_p) {
00077     double p, cp;
00078 
00079     /* Note: The structure of these checks has been carefully thought through.
00080      * For example, if x == mu and sigma == 0, we get the correct answer 1.
00081      */
00082 #ifdef IEEE_754
00083     if(ISNAN(x)  )
00084         return x + 1.0 ;
00085 #endif
00086     p = x ;
00087     if(!R_FINITE(p))
00088         return (x < 0.0 ) ? R_DT_0 : R_DT_1;
00089     x = p;
00090 
00091     ::Rf_pnorm_both(x, &p, &cp, (lower_tail ? 0 : 1), log_p);
00092 
00093     return(lower_tail ? p : cp);
00094 }
00095 
00096 inline double qnorm_1(double p, double mu /*, double sigma [=1.] */, int lower_tail, int log_p){
00097         return ::Rf_qnorm5(p, mu, 1.0, lower_tail, log_p ) ;
00098 }
00099 inline double qnorm_0(double p /*, double mu [=0.], double sigma [=1.] */, int lower_tail, int log_p){
00100         return ::Rf_qnorm5(p, 0.0, 1.0, lower_tail, log_p ) ;
00101 }
00102 
00103 } // stats
00104 } // Rcpp
00105         
00106 RCPP_DPQ_0(norm, Rcpp::stats::dnorm_0, Rcpp::stats::pnorm_0, Rcpp::stats::qnorm_0 )
00107 RCPP_DPQ_1(norm, Rcpp::stats::dnorm_1, Rcpp::stats::pnorm_1, Rcpp::stats::qnorm_1 )
00108 RCPP_DPQ_2(norm, ::Rf_dnorm4, ::Rf_pnorm5, ::Rf_qnorm5 )
00109 
00110 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerator Friends Defines