time-to-botec

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

ndarray.js (2713B)


      1 /**
      2 * @license Apache-2.0
      3 *
      4 * Copyright (c) 2020 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 'use strict';
     20 
     21 // MODULES //
     22 
     23 var float64ToFloat32 = require( '@stdlib/number/float64/base/to-float32' );
     24 
     25 
     26 // MAIN //
     27 
     28 /**
     29 * Computes the arithmetic mean of a single-precision floating-point strided array using Welford's algorithm.
     30 *
     31 * ## Method
     32 *
     33 * -   This implementation uses Welford's algorithm for efficient computation, which can be derived as follows
     34 *
     35 *     ```tex
     36 *     \begin{align*}
     37 *     \mu_n &= \frac{1}{n} \sum_{i=0}^{n-1} x_i \\
     38 *           &= \frac{1}{n} \biggl(x_{n-1} + \sum_{i=0}^{n-2} x_i \biggr) \\
     39 *           &= \frac{1}{n} (x_{n-1} + (n-1)\mu_{n-1}) \\
     40 *           &= \mu_{n-1} + \frac{1}{n} (x_{n-1} - \mu_{n-1})
     41 *     \end{align*}
     42 *     ```
     43 *
     44 * ## References
     45 *
     46 * -   Welford, B. P. 1962. "Note on a Method for Calculating Corrected Sums of Squares and Products." _Technometrics_ 4 (3). Taylor & Francis: 419–20. doi:[10.1080/00401706.1962.10490022](https://doi.org/10.1080/00401706.1962.10490022).
     47 * -   van Reeken, A. J. 1968. "Letters to the Editor: Dealing with Neely's Algorithms." _Communications of the ACM_ 11 (3): 149–50. doi:[10.1145/362929.362961](https://doi.org/10.1145/362929.362961).
     48 *
     49 * @param {PositiveInteger} N - number of indexed elements
     50 * @param {Float32Array} x - input array
     51 * @param {integer} stride - stride length
     52 * @param {NonNegativeInteger} offset - starting index
     53 * @returns {number} arithmetic mean
     54 *
     55 * @example
     56 * var Float32Array = require( '@stdlib/array/float32' );
     57 * var floor = require( '@stdlib/math/base/special/floor' );
     58 *
     59 * var x = new Float32Array( [ 2.0, 1.0, 2.0, -2.0, -2.0, 2.0, 3.0, 4.0 ] );
     60 * var N = floor( x.length / 2 );
     61 *
     62 * var v = smeanwd( N, x, 2, 1 );
     63 * // returns 1.25
     64 */
     65 function smeanwd( N, x, stride, offset ) {
     66 	var mu;
     67 	var ix;
     68 	var n;
     69 	var i;
     70 
     71 	if ( N <= 0 ) {
     72 		return NaN;
     73 	}
     74 	if ( N === 1 || stride === 0 ) {
     75 		return x[ offset ];
     76 	}
     77 	ix = offset;
     78 	mu = 0.0;
     79 	n = 0;
     80 	for ( i = 0; i < N; i++ ) {
     81 		n += 1;
     82 		mu = float64ToFloat32( mu + float64ToFloat32( float64ToFloat32( x[ix]-mu ) / n ) ); // eslint-disable-line max-len
     83 		ix += stride;
     84 	}
     85 	return mu;
     86 }
     87 
     88 
     89 // EXPORTS //
     90 
     91 module.exports = smeanwd;