1 /* 2 * Mathlib : A C Library of Special Functions 3 * Copyright (C) 2000-2019 The R Core Team 4 * Copyright (C) 1998 Ross Ihaka 5 * 6 * This program is free software; you can redistribute it and/or modify 7 * it under the terms of the GNU General Public License as published by 8 * the Free Software Foundation; either version 2 of the License, or 9 * (at your option) any later version. 10 * 11 * This program is distributed in the hope that it will be useful, 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 * GNU General Public License for more details. 15 * 16 * You should have received a copy of the GNU General Public License 17 * along with this program; if not, a copy is available at 18 * https://www.R-project.org/Licenses/ 19 * 20 * DESCRIPTION 21 * 22 * The density of the lognormal distribution. 23 */ 24 25 #include "nmath.h" 26 #include "dpq.h" 27 dlnorm(double x,double meanlog,double sdlog,int give_log)28double dlnorm(double x, double meanlog, double sdlog, int give_log) 29 { 30 double y; 31 32 #ifdef IEEE_754 33 if (ISNAN(x) || ISNAN(meanlog) || ISNAN(sdlog)) 34 return x + meanlog + sdlog; 35 #endif 36 if(sdlog < 0) ML_WARN_return_NAN; 37 if(!R_FINITE(x) && log(x) == meanlog) return ML_NAN;/* log(x) - meanlog is NaN */ 38 if(sdlog == 0) 39 return (log(x) == meanlog) ? ML_POSINF : R_D__0; 40 if(x <= 0) return R_D__0; 41 42 y = (log(x) - meanlog) / sdlog; 43 return (give_log ? 44 -(M_LN_SQRT_2PI + 0.5 * y * y + log(x * sdlog)) : 45 M_1_SQRT_2PI * exp(-0.5 * y * y) / (x * sdlog)); 46 /* M_1_SQRT_2PI = 1 / sqrt(2 * pi) */ 47 } 48