Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 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 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1610x 1610x 1610x 1610x 1610x 1610x 1610x 1610x 1610x 1610x 1610x 1610x 1610x 1610x 1610x 1610x 1x 1x 1609x 1609x 1610x 504x 504x 504x 504x 504x 504x 504x 100x 100x 504x 101x 504x 303x 159x 159x 303x 303x 404x 404x 1610x 1105x 1105x 1509x 1509x 1610x 6x 6x 6x 18x 18x 6x 6x 4x 4x 6x 6x 1503x 1503x 1503x 1503x 1610x 7633x 7633x 7633x 1503x 1503x 1610x 1498x 1498x 1610x 5x 5x 1503x 1610x 400x 400x 1503x 1610x 1x 1x 1x 1x 1x | /**
* @license Apache-2.0
*
* Copyright (c) 2026 The Stdlib Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*
* ## Notice
*
* The original C code, long comment, copyright, license, and constants are from [Cephes]{@link http://www.netlib.org/cephes}. The implementation follows the original, but has been modified for JavaScript.
*
* ```text
* Cephes Math Library Release 2.2: June, 1992
* Copyright 1984, 1987, 1992 by Stephen L. Moshier
* Direct inquiries to 30 Frost Street, Cambridge, MA 02140
* ```
*/
'use strict';
// MODULES //
var f32 = require( '@stdlib/number/float64/base/to-float32' );
var floorf = require( '@stdlib/math/base/special/floorf' );
var lnf = require( '@stdlib/math/base/special/lnf' );
var tanf = require( '@stdlib/math/base/special/tanf' );
var FLOAT32_PI = require( '@stdlib/constants/float32/pi' );
var EULERGAMMA = require( '@stdlib/constants/float32/eulergamma' );
var isnanf = require( '@stdlib/math/base/assert/is-nanf' );
var polyvalP = require( './polyval_p.js' );
// VARIABLES //
// Threshold above which the asymptotic expansion's correction term is below float32 epsilon:
var ASYMPTOTIC_THRESHOLD = 1.0e8;
// Minimum value of x for which we use the asymptotic expansion directly:
var MIN_SAFE_ASYMPTOTIC = 10.0;
// MAIN //
/**
* Evaluates the digamma function for a single-precision floating-point number.
*
* ## Method
*
* 1. For \\\\(x \u003c= 0\\\\), we use the reflection formula
*
* ```tex
* \\psi(1-x) = \\psi(x) + \\frac{\\pi}{\\tan(\\pi \\cdot x)}
* ```
*
* to make \\\\(x\\\\) positive.
*
* 2. For positive integer \\\\(x \u003c= 10\\\\), we use the recurrence
*
* ```tex
* \\psi(n) = -\\gamma + \\sum_{k=1}^{n-1} \\frac{1}{k}
* ```
*
* where \\\\(\\gamma\\\\) is the Euler-Mascheroni constant.
*
* 3. For general \\\\(x\\\\), we apply the recurrence relation
*
* ```tex
* \\psi(x+1) = \\psi(x) + \\frac{1}{x}
* ```
*
* until \\\\(x \u003e= 10\\\\), then use the asymptotic expansion
*
* ```tex
* \\psi(x) = \\ln(x) - \\frac{1}{2x} - \\sum_{k=1}^{\\infty} \\frac{B_{2k}}{2k x^{2k}}
* ```
*
* where \\\\(B_{2k}\\\\) are Bernoulli numbers.
*
* @param {number} x - input value
* @returns {number} function value
*
* @example
* var v = digammaf( 1.0 );
* // returns ~-0.577
*
* @example
* var v = digammaf( 2.0 );
* // returns ~0.423
*
* @example
* var v = digammaf( 3.0 );
* // returns ~0.923
*
* @example
* var v = digammaf( -2.5 );
* // returns ~1.103
*
* @example
* var v = digammaf( 0.0 );
* // returns NaN
*
* @example
* var v = digammaf( NaN );
* // returns NaN
*/
function digammaf( x ) {
var negative;
var tmp;
var rem;
var xx;
var s;
var w;
var y;
var z;
var i;
var n;
xx = f32( x );
negative = false;
if ( isnanf( xx ) ) {
return xx;
}
// Handle negative arguments using reflection formula...
if ( xx <= 0.0 ) {
negative = true;
// Argument reduction for tan:
rem = f32( xx - floorf( xx ) );
// Check for singularity (negative integer or zero)...
if ( rem === 0.0 ) {
return NaN;
}
if ( rem === 0.5 ) {
tmp = 0.0;
} else {
if ( rem > 0.5 ) {
rem = f32( xx - f32( floorf( xx ) + 1.0 ) );
}
tmp = f32( FLOAT32_PI / tanf( f32( FLOAT32_PI * rem ) ) );
}
// Reflect:
xx = f32( 1.0 - xx );
} else {
tmp = 0.0;
}
// Use direct formula for small positive integers...
if ( xx <= MIN_SAFE_ASYMPTOTIC && xx === floorf( xx ) ) {
y = 0.0;
n = xx;
for ( i = 1; i < n; i++ ) {
y = f32( y + f32( 1.0 / i ) );
}
y = f32( y - EULERGAMMA );
if ( negative ) {
y = f32( y - tmp );
}
return y;
}
// Use recurrence to make x large enough for asymptotic expansion...
s = xx;
w = 0.0;
while ( s < MIN_SAFE_ASYMPTOTIC ) {
w = f32( w + f32( 1.0 / s ) );
s = f32( s + 1.0 );
}
// Asymptotic expansion...
if ( s < ASYMPTOTIC_THRESHOLD ) {
z = f32( 1.0 / f32( s * s ) );
y = f32( z * polyvalP( z ) );
} else {
y = 0.0;
}
y = f32( lnf( s ) - f32( 0.5 / s ) - y - w );
if ( negative ) {
y = f32( y - tmp );
}
return y;
}
// EXPORTS //
module.exports = digammaf;
|