time-to-botec

Benchmark sampling in different programming languages
Log | Files | Refs | README

polynomial_series.js (2421B)


      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_60_0/boost/math/special_functions/zeta.hpp}. The implementation follows the original, but has been modified for JavaScript.
     22 *
     23 * ```text
     24 * (C) Copyright John Maddock 2006.
     25 *
     26 * Use, modification and distribution are subject to the
     27 * Boost Software License, Version 1.0. (See accompanying file
     28 * LICENSE or copy at http://www.boost.org/LICENSE_1_0.txt)
     29 * ```
     30 */
     31 
     32 'use strict';
     33 
     34 // MODULES //
     35 
     36 var pow = require( './../../../../base/special/pow' );
     37 var powm1 = require( './../../../../base/special/powm1' );
     38 
     39 
     40 // VARIABLES //
     41 
     42 // -ln(eps)/2 => -ln(2.220446049250313e-16)/2 = 18.021826694558577
     43 var N = 18|0; // asm type annotation
     44 
     45 // 2**N
     46 var TWO_N = 262144|0; // asm type annotation
     47 var NEG_TWO_N = -TWO_N;
     48 
     49 
     50 // MAIN //
     51 
     52 /**
     53 * Evaluates the Riemann zeta function using a polynomial series.
     54 *
     55 * ## References
     56 *
     57 * -   P. Borwein. "An Efficient Algorithm for the Riemann Zeta Function". Canadian Mathematical Society, Conference Proceedings. See algorithm [3][p155].
     58 *
     59 * [p155]: http://www.cecm.sfu.ca/personal/pborwein/PAPERS/P155.pdf
     60 *
     61 * @private
     62 * @param {number} s - input value
     63 * @returns {number} function value
     64 *
     65 * @example
     66 * var v = series( 3.0 );
     67 * // returns ~1.202
     68 */
     69 function series( s ) {
     70 	var sign;
     71 	var term;
     72 	var sum;
     73 	var tmp;
     74 	var N2;
     75 	var i;
     76 
     77 	sum = 0.0;
     78 	sign = 1;
     79 	for ( i = 0; i < N; i++ ) {
     80 		sum += sign * NEG_TWO_N / pow(i+1, s);
     81 		sign *= -1; // flip the sign
     82 	}
     83 	tmp = 1.0;
     84 	term = 1.0;
     85 	N2 = 2 * N;
     86 	for ( i = N; i <= N2-1; i++ ) {
     87 		sum += sign * (tmp - TWO_N) / pow(i+1, s);
     88 		sign *= -1; // flip the sign
     89 		term *= N2 - i;
     90 		term /= i - N + 1.0;
     91 		tmp += term;
     92 	}
     93 	return sum / (TWO_N * powm1(2.0, 1.0-s));
     94 }
     95 
     96 
     97 // EXPORTS //
     98 
     99 module.exports = series;