1 // -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; indent-tabs-mode: nil; -*-
2 //
3 // norm.h: Rcpp R/C++ interface class library -- normal distribution
4 //
5 // Copyright (C) 2010 - 2016  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__norm_h
23 #define Rcpp__stats__norm_h
24 
25 namespace Rcpp {
26 namespace stats {
27 
dnorm_1(double x,double mu,int give_log)28 inline double dnorm_1(double x, double mu /*, double sigma [=1.0]*/ , int give_log) {
29 #ifdef IEEE_754
30     if (ISNAN(x) || ISNAN(mu))
31         return x + mu + 1.0;
32 #endif
33     if (!R_FINITE(x) && mu == x) return ML_NAN; /* x-mu is NaN */
34     x = (x - mu);
35 
36     if (!R_FINITE(x)) return R_D__0;
37     return (give_log ?
38             -(M_LN_SQRT_2PI + 0.5 * x * x) :
39             M_1_SQRT_2PI * ::exp(-0.5 * x * x));
40     /* M_1_SQRT_2PI = 1 / sqrt(2 * pi) */
41 }
42 
dnorm_0(double x,int give_log)43 inline double dnorm_0(double x /*, double mu [=0.0], double sigma [=1.0]*/ ,
44                       int give_log) {
45 #ifdef IEEE_754
46     if (ISNAN(x))
47         return x + 1.0;
48 #endif
49     if (!R_FINITE(x)) return R_D__0;
50     return (give_log ?
51             -(M_LN_SQRT_2PI + 0.5 * x * x) :
52             M_1_SQRT_2PI * ::exp(-0.5 * x * x));
53     /* M_1_SQRT_2PI = 1 / sqrt(2 * pi) */
54 }
55 
pnorm_1(double x,double mu,int lower_tail,int log_p)56 inline double pnorm_1(double x, double mu /*, double sigma [=1.]*/ ,
57                       int lower_tail, int log_p) {
58     double p, cp;
59 
60     /* Note: The structure of these checks has been carefully thought through.
61      * For example, if x == mu and sigma == 0, we get the correct answer 1.
62      */
63 #ifdef IEEE_754
64     if (ISNAN(x) || ISNAN(mu))
65         return x + mu + 1.0;
66 #endif
67     if (!R_FINITE(x) && mu == x) return ML_NAN; /* x-mu is NaN */
68     p = (x - mu);
69     if (!R_FINITE(p))
70         return (x < mu) ? R_DT_0 : R_DT_1;
71     x = p;
72 
73     ::Rf_pnorm_both(x, &p, &cp, (lower_tail ? 0 : 1), log_p);
74 
75     return(lower_tail ? p : cp);
76 }
77 
pnorm_0(double x,int lower_tail,int log_p)78 inline double pnorm_0(double x /*, double mu [=0.] , double sigma [=1.]*/ , int lower_tail, int log_p) {
79     double p, cp;
80 
81     /* Note: The structure of these checks has been carefully thought through.
82      * For example, if x == mu and sigma == 0, we get the correct answer 1.
83      */
84 #ifdef IEEE_754
85     if (ISNAN(x))
86         return x + 1.0;
87 #endif
88     p = x;
89     if (!R_FINITE(p))
90         return (x < 0.0) ? R_DT_0 : R_DT_1;
91     x = p;
92 
93     ::Rf_pnorm_both(x, &p, &cp, (lower_tail ? 0 : 1), log_p);
94 
95     return(lower_tail ? p : cp);
96 }
97 
qnorm_1(double p,double mu,int lower_tail,int log_p)98 inline double qnorm_1(double p, double mu /*, double sigma [=1.] */,
99                       int lower_tail, int log_p){
100     return ::Rf_qnorm5(p, mu, 1.0, lower_tail, log_p);
101 }
102 
qnorm_0(double p,int lower_tail,int log_p)103 inline double qnorm_0(double p /*, double mu [=0.], double sigma [=1.] */,
104                       int lower_tail, int log_p){
105     return ::Rf_qnorm5(p, 0.0, 1.0, lower_tail, log_p);
106 }
107 
108 } // stats
109 } // Rcpp
110 
111 RCPP_DPQ_0(norm, Rcpp::stats::dnorm_0, Rcpp::stats::pnorm_0, Rcpp::stats::qnorm_0)
112 RCPP_DPQ_1(norm, Rcpp::stats::dnorm_1, Rcpp::stats::pnorm_1, Rcpp::stats::qnorm_1)
113 RCPP_DPQ_2(norm, ::Rf_dnorm4, ::Rf_pnorm5, ::Rf_qnorm5)
114 
115 #endif
116