spence.js (3100B)
1 /** 2 * @license Apache-2.0 3 * 4 * Copyright (c) 2018 The Stdlib Authors. 5 * 6 * Licensed under the Apache License, Version 2.0 (the "License"); 7 * you may not use this file except in compliance with the License. 8 * You may obtain a copy of the License at 9 * 10 * http://www.apache.org/licenses/LICENSE-2.0 11 * 12 * Unless required by applicable law or agreed to in writing, software 13 * distributed under the License is distributed on an "AS IS" BASIS, 14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 * See the License for the specific language governing permissions and 16 * limitations under the License. 17 * 18 * 19 * ## Notice 20 * 21 * 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. 22 * 23 * ```text 24 * Copyright 1985, 1987, 1989, 2000 by Stephen L. Moshier 25 * 26 * Some software in this archive may be from the book _Methods and Programs for Mathematical Functions_ (Prentice-Hall or Simon & Schuster International, 1989) or from the Cephes Mathematical Library, a commercial product. In either event, it is copyrighted by the author. What you see here may be used freely but it comes with no support or guarantee. 27 * 28 * Stephen L. Moshier 29 * moshier@na-net.ornl.gov 30 * ``` 31 */ 32 33 'use strict'; 34 35 // MODULES // 36 37 var isnan = require( './../../../../base/assert/is-nan' ); 38 var ln = require( './../../../../base/special/ln' ); 39 var PI_SQUARED = require( '@stdlib/constants/float64/pi-squared' ); 40 var polyvalA = require( './polyval_a.js' ); 41 var polyvalB = require( './polyval_b.js' ); 42 43 44 // MAIN // 45 46 /** 47 * Evaluates Spence’s function, which is also known as the dilogarithm. 48 * 49 * ## Method 50 * 51 * - A rational approximation gives the integral in the interval (0.5, 1.5). 52 * - Transformation formulas for \\( \tfrac{1}{x} \\) and \\( 1 - x \\) are employed outside the basic expansion range. 53 * 54 * ## Notes 55 * 56 * - Relative error: 57 * 58 * | arithmetic | domain | # trials | peak | rms | 59 * |:----------:|:-----------:|:--------:|:-------:|:-------:| 60 * | IEEE | 0,4 | 30000 | 3.9e-15 | 5.4e-16 | 61 * 62 * 63 * @param {NonNegativeNumber} x - input value 64 * @returns {number} function value 65 * 66 * @example 67 * var v = spence( 3.0 ); 68 * // returns ~-1.437 69 * 70 * @example 71 * var v = spence( 0.0 ); 72 * // returns ~1.645 73 * 74 * @example 75 * var v = spence( -9.0 ); 76 * // returns NaN 77 * 78 * @example 79 * var v = spence( NaN ); 80 * // returns NaN 81 */ 82 function spence( x ) { 83 var flg; 84 var w; 85 var y; 86 var z; 87 88 if ( isnan( x ) || x < 0.0 ) { 89 return NaN; 90 } 91 if ( x === 1.0 ) { 92 return 0.0; 93 } 94 if ( x === 0.0 ) { 95 return ( PI_SQUARED / 6.0 ); 96 } 97 flg = 0; 98 if ( x > 2.0 ) { 99 x = 1.0 / x; 100 flg |= 2; 101 } 102 if ( x > 1.5 ) { 103 w = (1.0 / x) - 1.0; 104 flg |= 2; 105 } 106 else if ( x < 0.5 ) { 107 w = -x; 108 flg |= 1; 109 } 110 else { 111 w = x - 1.0; 112 } 113 y = -w * polyvalA( w ) / polyvalB( w ); 114 if ( flg & 1 ) { 115 y = ( PI_SQUARED/6.0 ) - ( ln( x ) * ln( 1.0-x ) ) - y; 116 } 117 if ( flg & 2 ) { 118 z = ln( x ); 119 y = -( 0.5 * z * z ) - y; 120 } 121 return y; 122 } 123 124 125 // EXPORTS // 126 127 module.exports = spence;