/usr/include/d/std/mathspecial.d is in libphobos2-ldc-dev 1:0.17.1-1ubuntu1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 | // Written in the D programming language.
/**
* Mathematical Special Functions
*
* The technical term 'Special Functions' includes several families of
* transcendental functions, which have important applications in particular
* branches of mathematics and physics.
*
* The gamma and related functions, and the error function are crucial for
* mathematical statistics.
* The Bessel and related functions arise in problems involving wave propagation
* (especially in optics).
* Other major categories of special functions include the elliptic integrals
* (related to the arc length of an ellipse), and the hypergeometric functions.
*
* Status:
* Many more functions will be added to this module.
* The naming convention for the distribution functions (gammaIncomplete, etc)
* is not yet finalized and will probably change.
*
* Macros:
* WIKI = Phobos/StdMathSpecial
*
* TABLE_SV = <table border=1 cellpadding=4 cellspacing=0>
* <caption>Special Values</caption>
* $0</table>
* SVH = $(TR $(TH $1) $(TH $2))
* SV = $(TR $(TD $1) $(TD $2))
*
* NAN = $(RED NAN)
* SUP = <span style="vertical-align:super;font-size:smaller">$0</span>
* GAMMA = Γ
* THETA = θ
* INTEGRAL = ∫
* INTEGRATE = $(BIG ∫<sub>$(SMALL $1)</sub><sup>$2</sup>)
* POWER = $1<sup>$2</sup>
* SUB = $1<sub>$2</sub>
* BIGSUM = $(BIG Σ <sup>$2</sup><sub>$(SMALL $1)</sub>)
* CHOOSE = $(BIG () <sup>$(SMALL $1)</sup><sub>$(SMALL $2)</sub> $(BIG ))
* PLUSMN = ±
* INFIN = ∞
* PLUSMNINF = ±∞
* PI = π
* LT = <
* GT = >
* SQRT = √
* HALF = ½
*
*
* Copyright: Based on the CEPHES math library, which is
* Copyright (C) 1994 Stephen L. Moshier (moshier@world.std.com).
* License: $(WEB www.boost.org/LICENSE_1_0.txt, Boost License 1.0).
* Authors: Stephen L. Moshier (original C code). Conversion to D by Don Clugston
* Source: $(PHOBOSSRC std/_mathspecial.d)
*/
module std.mathspecial;
public import std.math;
private import std.internal.math.gammafunction;
private import std.internal.math.errorfunction;
/* ***********************************************
* GAMMA AND RELATED FUNCTIONS *
* ***********************************************/
pure:
nothrow:
@safe:
@nogc:
/** The Gamma function, $(GAMMA)(x)
*
* $(GAMMA)(x) is a generalisation of the factorial function
* to real and complex numbers.
* Like x!, $(GAMMA)(x+1) = x * $(GAMMA)(x).
*
* Mathematically, if z.re > 0 then
* $(GAMMA)(z) = $(INTEGRATE 0, $(INFIN)) $(POWER t, z-1)$(POWER e, -t) dt
*
* $(TABLE_SV
* $(SVH x, $(GAMMA)(x) )
* $(SV $(NAN), $(NAN) )
* $(SV $(PLUSMN)0.0, $(PLUSMNINF))
* $(SV integer > 0, (x-1)! )
* $(SV integer < 0, $(NAN) )
* $(SV +$(INFIN), +$(INFIN) )
* $(SV -$(INFIN), $(NAN) )
* )
*/
real gamma(real x)
{
return std.internal.math.gammafunction.gamma(x);
}
/** Natural logarithm of the gamma function, $(GAMMA)(x)
*
* Returns the base e (2.718...) logarithm of the absolute
* value of the gamma function of the argument.
*
* For reals, logGamma is equivalent to log(fabs(gamma(x))).
*
* $(TABLE_SV
* $(SVH x, logGamma(x) )
* $(SV $(NAN), $(NAN) )
* $(SV integer <= 0, +$(INFIN) )
* $(SV $(PLUSMNINF), +$(INFIN) )
* )
*/
real logGamma(real x)
{
return std.internal.math.gammafunction.logGamma(x);
}
/** The sign of $(GAMMA)(x).
*
* Returns -1 if $(GAMMA)(x) < 0, +1 if $(GAMMA)(x) > 0,
* $(NAN) if sign is indeterminate.
*
* Note that this function can be used in conjunction with logGamma(x) to
* evaluate gamma for very large values of x.
*/
real sgnGamma(real x)
{
/* Author: Don Clugston. */
if (isNaN(x)) return x;
if (x > 0) return 1.0;
if (x < -1/real.epsilon)
{
// Large negatives lose all precision
return real.nan;
}
long n = rndtol(x);
if (x == n) {
return x == 0 ? copysign(1, x) : real.nan;
}
return n & 1 ? 1.0 : -1.0;
}
unittest {
assert(sgnGamma(5.0) == 1.0);
assert(isNaN(sgnGamma(-3.0)));
assert(sgnGamma(-0.1) == -1.0);
assert(sgnGamma(-55.1) == 1.0);
assert(isNaN(sgnGamma(-real.infinity)));
assert(isIdentical(sgnGamma(NaN(0xABC)), NaN(0xABC)));
}
/** Beta function
*
* The beta function is defined as
*
* beta(x, y) = ($(GAMMA)(x) * $(GAMMA)(y)) / $(GAMMA)(x + y)
*/
real beta(real x, real y)
{
if ((x+y)> MAXGAMMA) {
return exp(logGamma(x) + logGamma(y) - logGamma(x+y));
} else return gamma(x) * gamma(y) / gamma(x+y);
}
unittest {
assert(isIdentical(beta(NaN(0xABC), 4), NaN(0xABC)));
assert(isIdentical(beta(2, NaN(0xABC)), NaN(0xABC)));
}
/** Digamma function
*
* The digamma function is the logarithmic derivative of the gamma function.
*
* digamma(x) = d/dx logGamma(x)
*
* See_Also: $(LREF logmdigamma), $(LREF logmdigammaInverse).
*/
real digamma(real x)
{
return std.internal.math.gammafunction.digamma(x);
}
/** Log Minus Digamma function
*
* logmdigamma(x) = log(x) - digamma(x)
*
* See_Also: $(LREF digamma), $(LREF logmdigammaInverse).
*/
real logmdigamma(real x)
{
return std.internal.math.gammafunction.logmdigamma(x);
}
/** Inverse of the Log Minus Digamma function
*
* Given y, the function finds x such log(x) - digamma(x) = y.
*
* See_Also: $(LREF logmdigamma), $(LREF digamma).
*/
real logmdigammaInverse(real x)
{
return std.internal.math.gammafunction.logmdigammaInverse(x);
}
/** Incomplete beta integral
*
* Returns incomplete beta integral of the arguments, evaluated
* from zero to x. The regularized incomplete beta function is defined as
*
* betaIncomplete(a, b, x) = $(GAMMA)(a + b) / ( $(GAMMA)(a) $(GAMMA)(b) ) *
* $(INTEGRATE 0, x) $(POWER t, a-1)$(POWER (1-t), b-1) dt
*
* and is the same as the the cumulative distribution function.
*
* The domain of definition is 0 <= x <= 1. In this
* implementation a and b are restricted to positive values.
* The integral from x to 1 may be obtained by the symmetry
* relation
*
* betaIncompleteCompl(a, b, x ) = betaIncomplete( b, a, 1-x )
*
* The integral is evaluated by a continued fraction expansion
* or, when b * x is small, by a power series.
*/
real betaIncomplete(real a, real b, real x )
{
return std.internal.math.gammafunction.betaIncomplete(a, b, x);
}
/** Inverse of incomplete beta integral
*
* Given y, the function finds x such that
*
* betaIncomplete(a, b, x) == y
*
* Newton iterations or interval halving is used.
*/
real betaIncompleteInverse(real a, real b, real y )
{
return std.internal.math.gammafunction.betaIncompleteInv(a, b, y);
}
/** Incomplete gamma integral and its complement
*
* These functions are defined by
*
* gammaIncomplete = ( $(INTEGRATE 0, x) $(POWER e, -t) $(POWER t, a-1) dt )/ $(GAMMA)(a)
*
* gammaIncompleteCompl(a,x) = 1 - gammaIncomplete(a,x)
* = ($(INTEGRATE x, $(INFIN)) $(POWER e, -t) $(POWER t, a-1) dt )/ $(GAMMA)(a)
*
* In this implementation both arguments must be positive.
* The integral is evaluated by either a power series or
* continued fraction expansion, depending on the relative
* values of a and x.
*/
real gammaIncomplete(real a, real x )
in {
assert(x >= 0);
assert(a > 0);
}
body {
return std.internal.math.gammafunction.gammaIncomplete(a, x);
}
/** ditto */
real gammaIncompleteCompl(real a, real x )
in {
assert(x >= 0);
assert(a > 0);
}
body {
return std.internal.math.gammafunction.gammaIncompleteCompl(a, x);
}
/** Inverse of complemented incomplete gamma integral
*
* Given a and p, the function finds x such that
*
* gammaIncompleteCompl( a, x ) = p.
*/
real gammaIncompleteComplInverse(real a, real p)
in {
assert(p >= 0 && p <= 1);
assert(a > 0);
}
body {
return std.internal.math.gammafunction.gammaIncompleteComplInv(a, p);
}
/* ***********************************************
* ERROR FUNCTIONS & NORMAL DISTRIBUTION *
* ***********************************************/
/** Error function
*
* The integral is
*
* erf(x) = 2/ $(SQRT)($(PI))
* $(INTEGRATE 0, x) exp( - $(POWER t, 2)) dt
*
* The magnitude of x is limited to about 106.56 for IEEE 80-bit
* arithmetic; 1 or -1 is returned outside this range.
*/
real erf(real x)
{
return std.internal.math.errorfunction.erf(x);
}
/** Complementary error function
*
* erfc(x) = 1 - erf(x)
* = 2/ $(SQRT)($(PI))
* $(INTEGRATE x, $(INFIN)) exp( - $(POWER t, 2)) dt
*
* This function has high relative accuracy for
* values of x far from zero. (For values near zero, use erf(x)).
*/
real erfc(real x)
{
return std.internal.math.errorfunction.erfc(x);
}
/** Normal distribution function.
*
* The normal (or Gaussian, or bell-shaped) distribution is
* defined as:
*
* normalDist(x) = 1/$(SQRT)(2$(PI)) $(INTEGRATE -$(INFIN), x) exp( - $(POWER t, 2)/2) dt
* = 0.5 + 0.5 * erf(x/sqrt(2))
* = 0.5 * erfc(- x/sqrt(2))
*
* To maintain accuracy at values of x near 1.0, use
* normalDistribution(x) = 1.0 - normalDistribution(-x).
*
* References:
* $(LINK http://www.netlib.org/cephes/ldoubdoc.html),
* G. Marsaglia, "Evaluating the Normal Distribution",
* Journal of Statistical Software <b>11</b>, (July 2004).
*/
real normalDistribution(real x)
{
return std.internal.math.errorfunction.normalDistributionImpl(x);
}
/** Inverse of Normal distribution function
*
* Returns the argument, x, for which the area under the
* Normal probability density function (integrated from
* minus infinity to x) is equal to p.
*/
real normalDistributionInverse(real p)
in {
assert(p>=0.0L && p<=1.0L, "Domain error");
}
body
{
return std.internal.math.errorfunction.normalDistributionInvImpl(p);
}
|