polycotpi.js (8228B)
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 and copyright notice are from the [Boost library]{@link http://www.boost.org/doc/libs/1_65_0/boost/math/special_functions/detail/polygamma.hpp}. The implementation follows the original but has been modified for JavaScript. 22 * 23 * ```text 24 * (C) Copyright Nikhar Agrawal 2013. 25 * (C) Copyright Christopher Kormanyos 2013. 26 * (C) Copyright John Maddock 2014. 27 * (C) Copyright Paul Bristow 2013. 28 * 29 * Use, modification and distribution are subject to the 30 * Boost Software License, Version 1.0. (See accompanying file 31 * LICENSE or copy at http://www.boost.org/LICENSE_1_0.txt) 32 * ``` 33 */ 34 35 'use strict'; 36 37 // MODULES // 38 39 var logger = require( 'debug' ); 40 var evalpoly = require( './../../../../base/tools/evalpoly' ); 41 var gammaln = require( './../../../../base/special/gammaln' ); 42 var signum = require( './../../../../base/special/signum' ); 43 var cospi = require( './../../../../base/special/cospi' ); 44 var sinpi = require( './../../../../base/special/sinpi' ); 45 var abs = require( './../../../../base/special/abs' ); 46 var exp = require( './../../../../base/special/exp' ); 47 var pow = require( './../../../../base/special/pow' ); 48 var ln = require( './../../../../base/special/ln' ); 49 var MAX_LN = require( '@stdlib/constants/float64/max-ln' ); 50 var PINF = require( '@stdlib/constants/float64/pinf' ); 51 var NINF = require( '@stdlib/constants/float64/ninf' ); 52 var LN_PI = require( '@stdlib/constants/float64/ln-pi' ); 53 var PI = require( '@stdlib/constants/float64/pi' ); 54 var polyval3 = require( './polyval_p3.js' ); 55 var polyval4 = require( './polyval_p4.js' ); 56 var polyval5 = require( './polyval_p5.js' ); 57 var polyval6 = require( './polyval_p6.js' ); 58 var polyval7 = require( './polyval_p7.js' ); 59 var polyval8 = require( './polyval_p8.js' ); 60 var polyval9 = require( './polyval_p9.js' ); 61 var polyval10 = require( './polyval_p10.js' ); 62 var polyval11 = require( './polyval_p11.js' ); 63 var polyval12 = require( './polyval_p12.js' ); 64 65 66 // VARIABLES // 67 68 var debug = logger( 'polygamma' ); 69 var MAX_SERIES_ITERATIONS = 1000000; 70 71 // π raised to powers two to twelve (obtained from Wolfram Alpha): 72 var PI2 = 9.869604401089358; 73 var PI3 = 31.00627668029982; 74 var PI4 = 97.40909103400244; 75 var PI5 = 306.01968478528147; 76 var PI6 = 961.3891935753045; 77 var PI7 = 3020.2932277767923; 78 var PI8 = 9488.531016070574; 79 var PI9 = 29809.09933344621; 80 var PI10 = 93648.04747608303; 81 var PI11 = 294204.0179738906; 82 var PI12 = 924269.1815233742; 83 84 // Derivative memoization table: 85 var table = [ 86 [ -1.0 ] 87 ]; 88 89 90 // FUNCTIONS // 91 92 /** 93 * Returns an array of zeros of the specified length. 94 * 95 * @private 96 * @param {NonNegativeInteger} len - array length 97 * @returns {Array} array of zeros 98 */ 99 function zeros( len ) { 100 var out; 101 var i; 102 103 out = []; 104 for ( i = 0; i < len; i++ ) { 105 out.push( 0.0 ); 106 } 107 return out; 108 } 109 110 /** 111 * Updates the derivatives table. 112 * 113 * @private 114 * @param {PositiveInteger} n - derivative 115 */ 116 function calculateDerivatives( n ) { 117 var noffset; // offset for next row 118 var offset; // 1 if the first cos power is 0; otherwise 0 119 var ncols; // how many entries there are in the current row 120 var mcols; // how many entries there will be in the next row 121 var mo; // largest order of the polynomial of cos terms 122 var so; // order of the sin term 123 var co; // order of the cosine term in entry "j" 124 var i; 125 var j; 126 var k; 127 128 for ( i = table.length-1; i < n-1; i++ ) { 129 offset = ( i&1 )|0; 130 so = ( i+2 )|0; 131 mo = ( so-1 )|0; 132 ncols = ( (mo-offset)/2 )|0; 133 noffset = ( offset ) ? 0 : 1; 134 mcols = ( (mo+1-noffset)/2 )|0; 135 table.push( zeros( mcols+1 ) ); 136 for ( j = 0; j <= ncols; j++ ) { 137 co = ( (2*j)+offset )|0; 138 k = ( (co+1)/2 )|0; 139 table[ i+1 ][ k ] += ((co-so)*table[i][j]) / (so-1); 140 if ( co ) { 141 k = ( (co-1)/2 )|0; 142 table[ i+1 ][ k ] += (-co*table[i][j]) / (so-1); 143 } 144 } 145 } 146 } 147 148 149 // MAIN // 150 151 /** 152 * Returns n'th derivative of \\(\operatorname{cot|(\pi x)\\) at \\(x\\). 153 * 154 * ## Notes 155 * 156 * - The derivatives are simply tabulated for up to \\(n = 9\\), beyond that it is possible to calculate coefficients as follows. The general form of each derivative is: 157 * 158 * ```tex 159 * \pi^n * \sum_{k=0}^n C[k,n] \cdot \cos^k(\pi \cdot x) \cdot \operatorname{csc}^{(n+1)}(\pi \cdot x) 160 * ``` 161 * 162 * with constant \\( C\[0,1\] = -1 \\) and all other \\( C\[k,n\] = 0 \)). Then for each \\( k < n+1 \\): 163 * 164 * ```tex 165 * \begin{align*} 166 * C[k-1, n+1] &-= k * C[k, n]; \\ 167 * C[k+1, n+1] &+= (k-n-1) * C[k, n]; 168 * \end{align*} 169 * ``` 170 * 171 * - Note that there are many different ways of representing this derivative thanks to the many trigonometric identities available. In particular, the sum of powers of cosines could be replaced by a sum of cosine multiple angles, and, indeed, if you plug the derivative into Mathematica, this is the form it will give. The two forms are related via the Chebeshev polynomials of the first kind and \\( T_n(\cos(x)) = \cos(n x) \\). The polynomial form has the great advantage that all the cosine terms are zero at half integer arguments - right where this function has it's minimum - thus avoiding cancellation error in this region. 172 * 173 * - And finally, since every other term in the polynomials is zero, we can save space by only storing the non-zero terms. This greatly increases complexity when subscripting the tables in the calculation, but halves the storage space (and complexity for that matter). 174 * 175 * 176 * @private 177 * @param {PositiveInteger} n - derivative to evaluate 178 * @param {number} x - input 179 * @param {number} xc - one minus `x` 180 * @returns {number} n'th derivative 181 */ 182 function polycotpi( n, x, xc ) { 183 var powTerms; 184 var idx; 185 var out; 186 var sum; 187 var c; 188 var s; 189 190 s = ( abs( x ) < abs( xc ) ) ? sinpi( x ) : sinpi( xc ); 191 c = cospi( x ); 192 switch ( n ) { // eslint-disable-line default-case 193 case 1: 194 return -PI / ( s * s ); 195 case 2: 196 return 2.0 * PI2 * c / pow( s, 3.0 ); 197 case 3: 198 return PI3 * polyval3( c*c ) / pow( s, 4.0 ); 199 case 4: 200 return PI4 * c * polyval4( c*c ) / pow( s, 5.0 ); 201 case 5: 202 return PI5 * polyval5( c*c ) / pow( s, 6.0 ); 203 case 6: 204 return PI6 * c * polyval6( c*c ) / pow( s, 7.0 ); 205 case 7: 206 return PI7 * polyval7( c*c ) / pow( s, 8.0 ); 207 case 8: 208 return PI8 * c * polyval8( c*c ) / pow( s, 9.0 ); 209 case 9: 210 return PI9 * polyval9( c*c ) / pow( s, 10.0 ); 211 case 10: 212 return PI10 * c * polyval10( c*c ) / pow( s, 11.0 ); 213 case 11: 214 return PI11 * polyval11( c*c ) / pow( s, 12.0 ); 215 case 12: 216 return PI12 * c * polyval12( c*c ) / pow( s, 13.0 ); 217 } 218 // We'll have to compute the coefficients up to `n`, complexity is O(n^2) which we don't worry about as the values are computed once and then cached. However, if the final evaluation would have too many terms just bail out right away: 219 if ( n/2 > MAX_SERIES_ITERATIONS ) { 220 debug( 'The value of `n` is so large that we\'re unable to compute the result in reasonable time.' ); 221 return NaN; 222 } 223 idx = n - 1; 224 if ( idx >= table.length ) { 225 // Lazily calculate derivatives: 226 calculateDerivatives( n ); 227 } 228 sum = evalpoly( table[ idx ], c*c ); 229 if ( idx & 1 ) { 230 sum *= c; // First coefficient is order 1, and really an odd polynomial. 231 } 232 if ( sum === 0.0 ) { 233 return sum; 234 } 235 // The remaining terms are computed using logs since the powers and factorials get real large real quick: 236 powTerms = n * LN_PI; 237 if ( s === 0.0 ) { 238 return ( sum >= 0.0 ) ? PINF : NINF; 239 } 240 powTerms -= ln( abs( s ) ) * ( n+1 ); 241 powTerms += gammaln( n ) + ln( abs(sum) ); 242 243 if ( powTerms > MAX_LN ) { 244 return ( sum >= 0.0 ) ? PINF : NINF; 245 } 246 out = exp( powTerms ) * signum( sum ); 247 if ( s < 0.0 && ( (n+1)&1 ) ) { 248 out *= -1; 249 } 250 return out; 251 } 252 253 254 // EXPORTS // 255 256 module.exports = polycotpi;