numer-utils: Numerical Utilities - Functions, Constants

numer-utilsR Documentation

Numerical Utilities - Functions, Constants

Description

The DPQ package provides some numeric constants used in some of its distribution computations.

all_mpfr() and any_mpfr() return TRUE iff all (or ‘any’, respectively) of their arguments inherit from class "mpfr" (from package Rmpfr).

logr(x,a) computes log(x / (x + a)) in a numerically stable way.

modf(x) splits each x into integer part (as trunc(x)) and fractional (remainder) part in (-1, 1) and corresponds to the R version of the C99 (and POSIX) standard C (and C++) mathlib functions of the same name.

Usage

## Numeric Constants : % mostly in   ../R/beta-fns.R
M_LN2        # = log(2)  = 0.693....
M_SQRT2      # = sqrt(2) = 1.4142...
M_cutoff     # := If |x| > |k| * M_cutoff, then  log[ exp(-x) * k^x ]  =~=  -x
             #  = 3196577161300663808 ~= 3.2e+18
M_minExp     # = log(2) * .Machine$double.min.exp # ~= -708.396..
G_half       # = sqrt(pi) = Gamma( 1/2 )

## Functions :
all_mpfr(...)
any_mpfr(...)
logr(x, a)    # == log(x / (x + a)) -- but numerically smart; x >= 0, a > -x
modf(x)
okLongDouble(lambda = 999, verbose = 0L, tol = 1e-15)

Arguments

...

numeric or "mpfr" numeric vectors.

x, a

number-like, not negative, now may be vectors of length(.) > 1.

lambda

a number, typically in the order of 500–10'000.

verbose

a non-negative integer, if not zero, okLongDouble() prints the intermediate long double computations' results.

tol

numerical tolerance used to determine the accuracy required for near equality in okLongDouble().

Details

all_mpfr(),
all_mpfr() :

test if all or any of their arguments or of class "mpfr" (from package Rmpfr). The arguments are evaluated only until the result is determined, see the example.

logr()

computes \log( x / (x+a) ) in a numerically stable way.

Value

The numeric constant in the first case; a numeric (or "mpfr") vector of appropriate size in the 2nd case.

okLongDouble() returns a logical, TRUE iff the long double arithmetic with expl() and logl() seems to work accurately and consistently for exp(-lambda) and log(lambda).

Author(s)

Martin Maechler

See Also

.Machine

Examples

(Ms <- ls("package:DPQ", pattern = "^M"))
lapply(Ms, function(nm) { cat(nm,": "); print(get(nm)) }) -> .tmp

logr(1:3, a=1e-10)

okLongDouble(verbose=TRUE) # verbose: show (C-level) computations
## typically TRUE, but not e.g. in a valgrinded R-devel of Oct.2019
## Here is typically the "boundary":

rr <- try(uniroot(function(x) okLongDouble(x) - 1/2,
              c(11350, 11400), tol=1e-7, extendInt = "yes"))
str(rr, digits=9) ## seems somewhat platform dependent: now see
## $ root      : num 11376.563
## $ estim.prec: num 9.313e-08
## $ iter      : int 29

set.seed(2021); x <- runif(100, -7,7)
mx <- modf(x)
with(mx, head( cbind(x, i=mx$i, fr=mx$fr) )) # showing the first cases
with(mx, stopifnot(   x == fr + i,
                      i == trunc(x),
               sign(fr) == sign(x)))

DPQ documentation built on Dec. 5, 2023, 3:05 a.m.