NCBI C++ ToolKit
Macros | Functions | Variables
ncbi_math.c File Reference

Definitions for portable math library. More...

#include <algo/blast/core/ncbi_math.h>
#include "ncbi_erf.c"
+ Include dependency graph for ncbi_math.c:

Go to the source code of this file.

Go to the SVN repository for this file.

Macros

#define DBL_EPSILON   2.2204460492503131e-16
 size of the next series term that indicates convergence in the log and polygamma functions More...
 
#define F(x)   ((*f)((x), fargs))
 Make a parametrized function appear to have only one variable. More...
 
#define MAX_DIAGS   20
 Maximum number of diagonals in the Romberg array. More...
 
#define NCBI_Erf   BLAST_Erf
 
#define NCBI_ErfC   BLAST_ErfC
 
#define NCBI_INCLUDE_NCBI_ERF_C   1
 

Functions

double BLAST_Expm1 (double x)
 Exponentional with base e. More...
 
double BLAST_Log1p (double x)
 Natural logarithm with shifted input. More...
 
static double s_LogDerivative (Int4 order, double *u)
 evaluate a specified-order derivative of ln(f(x)) More...
 
static double s_GeneralLnGamma (double x, Int4 order)
 Compute a specified-order derivative of ln(gamma(x)) evaluated at some point x. More...
 
static double s_PolyGamma (double x, Int4 order)
 Compute, to 10-digit accuracy, a specified order derivative of ln(abs(gamma(x))). More...
 
static double s_LnGamma (double x)
 Compute ln(abs(gamma(x))) to 10-digit accuracy. More...
 
double BLAST_Factorial (Int4 n)
 Factorial function. More...
 
double BLAST_LnGammaInt (Int4 n)
 log(gamma(n)), integral n More...
 
double BLAST_RombergIntegrate (double(*f)(double, void *), void *fargs, double p, double q, double eps, Int4 epsit, Int4 itmin)
 Romberg numerical integrator. More...
 
Int4 BLAST_Gcd (Int4 a, Int4 b)
 Greatest common divisor. More...
 
Int4 BLAST_Gdb3 (Int4 *a, Int4 *b, Int4 *c)
 Divide 3 numbers by their greatest common divisor. More...
 
long BLAST_Nint (double x)
 Nearest integer. More...
 
double BLAST_Powi (double x, Int4 n)
 Integral power of x. More...
 
double BLAST_LnFactorial (double x)
 Logarithm of the factorial. More...
 

Variables

static double _default_gamma_coef []
 auxiliary values for computation of derivative of ln(gamma(x)) More...
 
static const double kPrecomputedFactorial []
 Tabulated values of the first few factorials. More...
 

Detailed Description

Definitions for portable math library.

Definition in file ncbi_math.c.

Macro Definition Documentation

◆ DBL_EPSILON

#define DBL_EPSILON   2.2204460492503131e-16

size of the next series term that indicates convergence in the log and polygamma functions

Definition at line 61 of file ncbi_math.c.

◆ F

#define F (   x)    ((*f)((x), fargs))

Make a parametrized function appear to have only one variable.

Definition at line 342 of file ncbi_math.c.

◆ MAX_DIAGS

#define MAX_DIAGS   20

Maximum number of diagonals in the Romberg array.

Definition at line 344 of file ncbi_math.c.

◆ NCBI_Erf

#define NCBI_Erf   BLAST_Erf

Definition at line 491 of file ncbi_math.c.

◆ NCBI_ErfC

#define NCBI_ErfC   BLAST_ErfC

Definition at line 492 of file ncbi_math.c.

◆ NCBI_INCLUDE_NCBI_ERF_C

#define NCBI_INCLUDE_NCBI_ERF_C   1

Definition at line 493 of file ncbi_math.c.

Function Documentation

◆ BLAST_Expm1()

double BLAST_Expm1 ( double  x)

Exponentional with base e.

Parameters
xinput operand
Returns
exp(x) - 1

Definition at line 33 of file ncbi_math.c.

References ABS.

Referenced by BLAST_KarlinEtoP(), BlastKarlinLHtoK(), s_BlastSumP(), and s_BlastSumPCalc().

◆ BLAST_Factorial()

double BLAST_Factorial ( Int4  n)

Factorial function.

Parameters
ninput operand
Returns
(double)(1 * 2 * 3 * ... * n)

Definition at line 312 of file ncbi_math.c.

References DIM, kPrecomputedFactorial, n, and s_LnGamma().

Referenced by s_GeneralLnGamma(), and s_PolyGamma().

◆ BLAST_Gcd()

Int4 BLAST_Gcd ( Int4  a,
Int4  b 
)

Greatest common divisor.

Parameters
aFirst operand (any integer)
bSecond operand (any integer)
Returns
The largest integer that evenly divides a and b

Definition at line 405 of file ncbi_math.c.

References a, ABS, and b.

Referenced by BLAST_Gdb3(), Blast_KarlinLambdaNR(), BlastKarlinLHtoK(), and s_GetNuclValuesArray().

◆ BLAST_Gdb3()

Int4 BLAST_Gdb3 ( Int4 a,
Int4 b,
Int4 c 
)

Divide 3 numbers by their greatest common divisor.

Parameters
aFirst integer [in] [out]
bSecond integer [in] [out]
cThird integer [in] [out]
Returns
The greatest common divisor

Definition at line 422 of file ncbi_math.c.

References a, b, BLAST_Gcd(), and g().

Referenced by BLAST_AffineGreedyAlign(), and s_BlastGreedyAlignMemAlloc().

◆ BLAST_LnFactorial()

double BLAST_LnFactorial ( double  x)

Logarithm of the factorial.

Parameters
xinput operand
Returns
log(1 * 2 * 3 * ... * x)

Definition at line 473 of file ncbi_math.c.

References s_LnGamma().

Referenced by BLAST_LargeGapSumE(), BLAST_SmallGapSumE(), and BLAST_UnevenGapSumE().

◆ BLAST_LnGammaInt()

double BLAST_LnGammaInt ( Int4  n)

log(gamma(n)), integral n

Parameters
ninput operand
Returns
log(1 * 2 * 3 * ... (n-1))

Definition at line 323 of file ncbi_math.c.

References DIM, kPrecomputedFactorial, log, n, and s_LnGamma().

Referenced by s_BlastSumP(), and s_BlastSumPCalc().

◆ BLAST_Log1p()

double BLAST_Log1p ( double  x)

Natural logarithm with shifted input.

Parameters
xinput operand (x > -1)
Returns
log(x+1)

Definition at line 64 of file ncbi_math.c.

References ABS, DBL_EPSILON, i, and log.

Referenced by BLAST_KarlinPtoE().

◆ BLAST_Nint()

long BLAST_Nint ( double  x)

◆ BLAST_Powi()

double BLAST_Powi ( double  x,
Int4  n 
)

Integral power of x.

Parameters
xfloating-point base of the exponential
n(integer) exponent
Returns
x multiplied by itself n times

Definition at line 444 of file ncbi_math.c.

References n.

Referenced by BLAST_GapDecayDivisor(), BlastKarlinLtoH(), s_GeneralLnGamma(), and s_PolyGamma().

◆ BLAST_RombergIntegrate()

double BLAST_RombergIntegrate ( double(*)(double, void *)  f,
void *  fargs,
double  p,
double  q,
double  eps,
Int4  epsit,
Int4  itmin 
)

Romberg numerical integrator.

Parameters
fPointer to the function to integrate; the first argument is the variable to integrate over, the second is a pointer to a list of additional arguments that f may need
fargsPointer to an array of extra arguments or parameters needed to compute the function to be integrated. None of the items in this list may vary over the region of integration
pLeft-hand endpoint of the integration interval
qRight-hand endpoint of the integration interval (q is assumed > p)
epsThe relative error tolerance that indicates convergence
epsitThe number of consecutive diagonal entries in the Romberg array whose relative difference must be less than eps before convergence is assumed. This is presently limited to 1, 2, or 3
itminThe minimum number of diagnonal Romberg entries that will be computed
Returns
The computed integral of f() between p and q

Definition at line 346 of file ncbi_math.c.

References ABS, F, i, MAX, MAX_DIAGS, MIN, and n.

Referenced by s_BlastSumPCalc(), and s_InnerIntegralCback().

◆ s_GeneralLnGamma()

static double s_GeneralLnGamma ( double  x,
Int4  order 
)
static

Compute a specified-order derivative of ln(gamma(x)) evaluated at some point x.

Parameters
xValue at which derivative will be evaluated
orderOrder of derivative (0...POLYGAMMA_ORDER_MAX)
Returns
'order'-th derivative of ln(gamma(x)) at specified x. Accuracy is to 10 digits for x >= 1

Definition at line 162 of file ncbi_math.c.

References _default_gamma_coef, BLAST_Factorial(), BLAST_Powi(), DIM, i, log, NCBIMATH_LN2, NCBIMATH_LNPI, POLYGAMMA_ORDER_MAX, s_LogDerivative(), tmp, and rapidjson::value.

Referenced by s_PolyGamma().

◆ s_LnGamma()

static double s_LnGamma ( double  x)
static

Compute ln(abs(gamma(x))) to 10-digit accuracy.

Parameters
xPoint to evaluate ln(abs(gamma(x)))
Returns
The function value

Definition at line 292 of file ncbi_math.c.

References s_PolyGamma().

Referenced by BLAST_Factorial(), BLAST_LnFactorial(), and BLAST_LnGammaInt().

◆ s_LogDerivative()

static double s_LogDerivative ( Int4  order,
double *  u 
)
static

evaluate a specified-order derivative of ln(f(x))

Parameters
orderThe order of derivative to evaluate (0...LOGDERIV_ORDER_MAX). Derivative order 0 just computes ln(f(x))
uA list of numerical values of f(x) and its derivatives, all at the same point x, to be used within the computations
Returns
'order'-th derivative of ln(f(x)) or HUGE_VAL if order is out of range or u[0] is zero

Definition at line 95 of file ncbi_math.c.

References i, log, LOGDERIV_ORDER_MAX, tmp, and rapidjson::value.

Referenced by s_GeneralLnGamma(), and s_PolyGamma().

◆ s_PolyGamma()

static double s_PolyGamma ( double  x,
Int4  order 
)
static

Compute, to 10-digit accuracy, a specified order derivative of ln(abs(gamma(x))).

Parameters
xvalue at which derivative will be evaluated
orderOrder of derivative (0...POLYGAMMA_ORDER_MAX) order = 0, 1, 2, corresponds to ln(gamma), digamma, trigamma, etc. Note that the value here is one less than that suggested by the "di" and "tri" prefixes of digamma, trigamma, etc. In other words, it is truly the order of the derivative.
Returns
Computed derivative value, or HUGE_VAL if order is out of range

Definition at line 236 of file ncbi_math.c.

References ABS, BLAST_Factorial(), BLAST_Powi(), DBL_EPSILON, log, NCBIMATH_LNPI, NCBIMATH_PI, POLYGAMMA_ORDER_MAX, s_GeneralLnGamma(), s_LogDerivative(), tmp, and rapidjson::value.

Referenced by s_LnGamma().

Variable Documentation

◆ _default_gamma_coef

double _default_gamma_coef[]
static
Initial value:
= {
4.694580336184385e+04,
-1.560605207784446e+05,
2.065049568014106e+05,
-1.388934775095388e+05,
5.031796415085709e+04,
-9.601592329182778e+03,
8.785855930895250e+02,
-3.155153906098611e+01,
2.908143421162229e-01,
-2.319827630494973e-04,
1.251639670050933e-10
}

auxiliary values for computation of derivative of ln(gamma(x))

Definition at line 140 of file ncbi_math.c.

Referenced by s_GeneralLnGamma().

◆ kPrecomputedFactorial

const double kPrecomputedFactorial[]
static
Initial value:
= {
1., 1., 2., 6., 24., 120., 720., 5040., 40320., 362880., 3628800.,
39916800., 479001600., 6227020800., 87178291200., 1307674368000.,
20922789888000., 355687428096000., 6402373705728000.,
121645100408832000., 2432902008176640000., 51090942171709440000.,
1124000727777607680000., 25852016738884976640000.,
620448401733239439360000., 15511210043330985984000000.,
403291461126605635584000000., 10888869450418352160768000000.,
304888344611713860501504000000., 8841761993739701954543616000000.,
265252859812191058636308480000000., 8222838654177922817725562880000000.,
263130836933693530167218012160000000.,
8683317618811886495518194401280000000.,
295232799039604140847618609643520000000.
}

Tabulated values of the first few factorials.

Definition at line 297 of file ncbi_math.c.

Referenced by BLAST_Factorial(), and BLAST_LnGammaInt().

Modified on Mon Jul 22 05:08:14 2024 by modify_doxy.py rev. 669887