diff --git a/lib/node_modules/@stdlib/stats/base/meanwd/README.md b/lib/node_modules/@stdlib/stats/base/meanwd/README.md index 72c0886ce2cc..0ccfbf960a80 100644 --- a/lib/node_modules/@stdlib/stats/base/meanwd/README.md +++ b/lib/node_modules/@stdlib/stats/base/meanwd/README.md @@ -51,7 +51,7 @@ The [arithmetic mean][arithmetic-mean] is defined as var meanwd = require( '@stdlib/stats/base/meanwd' ); ``` -#### meanwd( N, x, stride ) +#### meanwd( N, x, strideX ) Computes the [arithmetic mean][arithmetic-mean] of a strided array `x` using Welford's algorithm. @@ -67,17 +67,14 @@ The function has the following parameters: - **N**: number of indexed elements. - **x**: input [`Array`][mdn-array] or [`typed array`][mdn-typed-array]. -- **stride**: index increment for `x`. +- **strideX**: stride length for `x`. -The `N` and `stride` parameters determine which elements in `x` are accessed at runtime. For example, to compute the [arithmetic mean][arithmetic-mean] of every other element in `x`, +The `N` and stride parameters determine which elements in the strided array are accessed at runtime. For example, to compute the [arithmetic mean][arithmetic-mean] of every other element in `x`, ```javascript -var floor = require( '@stdlib/math/base/special/floor' ); - var x = [ 1.0, 2.0, 2.0, -7.0, -2.0, 3.0, 4.0, 2.0 ]; -var N = floor( x.length / 2 ); -var v = meanwd( N, x, 2 ); +var v = meanwd( 4, x, 2 ); // returns 1.25 ``` @@ -87,42 +84,37 @@ Note that indexing is relative to the first index. To introduce an offset, use [ ```javascript var Float64Array = require( '@stdlib/array/float64' ); -var floor = require( '@stdlib/math/base/special/floor' ); var x0 = new Float64Array( [ 2.0, 1.0, 2.0, -2.0, -2.0, 2.0, 3.0, 4.0 ] ); var x1 = new Float64Array( x0.buffer, x0.BYTES_PER_ELEMENT*1 ); // start at 2nd element -var N = floor( x0.length / 2 ); - -var v = meanwd( N, x1, 2 ); +var v = meanwd( 4, x1, 2 ); // returns 1.25 ``` -#### meanwd.ndarray( N, x, stride, offset ) +#### meanwd.ndarray( N, x, strideX, offsetX ) Computes the [arithmetic mean][arithmetic-mean] of a strided array using Welford's algorithm and alternative indexing semantics. ```javascript var x = [ 1.0, -2.0, 2.0 ]; -var N = x.length; -var v = meanwd.ndarray( N, x, 1, 0 ); +var v = meanwd.ndarray( 3, x, 1, 0 ); // returns ~0.33333 ``` The function has the following additional parameters: -- **offset**: starting index for `x`. +- **offsetX**: starting index for `x`. -While [`typed array`][mdn-typed-array] views mandate a view offset based on the underlying `buffer`, the `offset` parameter supports indexing semantics based on a starting index. For example, to calculate the [arithmetic mean][arithmetic-mean] for every other value in `x` starting from the second value +While [`typed array`][mdn-typed-array] views mandate a view offset based on the underlying buffer, the offset parameter supports indexing semantics based on a starting index. For example, to calculate the [arithmetic mean][arithmetic-mean] for every other element in the strided array starting from the second element ```javascript var floor = require( '@stdlib/math/base/special/floor' ); var x = [ 2.0, 1.0, 2.0, -2.0, -2.0, 2.0, 3.0, 4.0 ]; -var N = floor( x.length / 2 ); -var v = meanwd.ndarray( N, x, 2, 1 ); +var v = meanwd.ndarray( 4, x, 2, 1 ); // returns 1.25 ``` @@ -135,6 +127,7 @@ var v = meanwd.ndarray( N, x, 2, 1 ); ## Notes - If `N <= 0`, both functions return `NaN`. +- Both functions support array-like objects having getter and setter accessors for array element access (e.g., [`@stdlib/array/base/accessor`][@stdlib/array/base/accessor]). - Depending on the environment, the typed versions ([`dmeanwd`][@stdlib/stats/strided/dmeanwd], [`smeanwd`][@stdlib/stats/base/smeanwd], etc.) are likely to be significantly more performant. @@ -148,18 +141,12 @@ var v = meanwd.ndarray( N, x, 2, 1 ); ```javascript -var randu = require( '@stdlib/random/base/randu' ); -var round = require( '@stdlib/math/base/special/round' ); -var Float64Array = require( '@stdlib/array/float64' ); +var discreteUniform = require( '@stdlib/random/array/discrete-uniform' ); var meanwd = require( '@stdlib/stats/base/meanwd' ); -var x; -var i; - -x = new Float64Array( 10 ); -for ( i = 0; i < x.length; i++ ) { - x[ i ] = round( (randu()*100.0) - 50.0 ); -} +var x = discreteUniform( 10, -50, 50, { + 'dtype': 'float64' +}); console.log( x ); var v = meanwd( x.length, x, 1 ); @@ -210,6 +197,8 @@ console.log( v ); [mdn-typed-array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray +[@stdlib/array/base/accessor]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/array/base/accessor + [@welford:1962a]: https://doi.org/10.1080/00401706.1962.10490022 [@vanreeken:1968a]: https://doi.org/10.1145/362929.362961 diff --git a/lib/node_modules/@stdlib/stats/base/meanwd/benchmark/benchmark.js b/lib/node_modules/@stdlib/stats/base/meanwd/benchmark/benchmark.js index 0450abd2d004..e2978eab49c6 100644 --- a/lib/node_modules/@stdlib/stats/base/meanwd/benchmark/benchmark.js +++ b/lib/node_modules/@stdlib/stats/base/meanwd/benchmark/benchmark.js @@ -21,11 +21,18 @@ // MODULES // var bench = require( '@stdlib/bench' ); -var randu = require( '@stdlib/random/base/randu' ); +var uniform = require( '@stdlib/random/array/uniform' ); var isnan = require( '@stdlib/math/base/assert/is-nan' ); var pow = require( '@stdlib/math/base/special/pow' ); var pkg = require( './../package.json' ).name; -var meanwd = require( './../lib/meanwd.js' ); +var meanwd = require( './../lib/main.js' ); + + +// VARIABLES // + +var options = { + 'dtype': 'generic' +}; // FUNCTIONS // @@ -38,13 +45,7 @@ var meanwd = require( './../lib/meanwd.js' ); * @returns {Function} benchmark function */ function createBenchmark( len ) { - var x; - var i; - - x = []; - for ( i = 0; i < len; i++ ) { - x.push( ( randu()*20.0 ) - 10.0 ); - } + var x = uniform( len, -10, 10, options ); return benchmark; function benchmark( b ) { diff --git a/lib/node_modules/@stdlib/stats/base/meanwd/benchmark/benchmark.ndarray.js b/lib/node_modules/@stdlib/stats/base/meanwd/benchmark/benchmark.ndarray.js index 42c6d052b3f0..d58f5510e4d1 100644 --- a/lib/node_modules/@stdlib/stats/base/meanwd/benchmark/benchmark.ndarray.js +++ b/lib/node_modules/@stdlib/stats/base/meanwd/benchmark/benchmark.ndarray.js @@ -21,13 +21,20 @@ // MODULES // var bench = require( '@stdlib/bench' ); -var randu = require( '@stdlib/random/base/randu' ); +var uniform = require( '@stdlib/random/array/uniform' ); var isnan = require( '@stdlib/math/base/assert/is-nan' ); var pow = require( '@stdlib/math/base/special/pow' ); var pkg = require( './../package.json' ).name; var meanwd = require( './../lib/ndarray.js' ); +// VARIABLES // + +var options = { + 'dtype': 'generic' +}; + + // FUNCTIONS // /** @@ -38,13 +45,7 @@ var meanwd = require( './../lib/ndarray.js' ); * @returns {Function} benchmark function */ function createBenchmark( len ) { - var x; - var i; - - x = []; - for ( i = 0; i < len; i++ ) { - x.push( ( randu()*20.0 ) - 10.0 ); - } + var x = uniform( len, -10, 10, options ); return benchmark; function benchmark( b ) { diff --git a/lib/node_modules/@stdlib/stats/base/meanwd/docs/repl.txt b/lib/node_modules/@stdlib/stats/base/meanwd/docs/repl.txt index 3225621eb205..cd5777aa97ae 100644 --- a/lib/node_modules/@stdlib/stats/base/meanwd/docs/repl.txt +++ b/lib/node_modules/@stdlib/stats/base/meanwd/docs/repl.txt @@ -1,9 +1,9 @@ -{{alias}}( N, x, stride ) +{{alias}}( N, x, strideX ) Computes the arithmetic mean of a strided array using Welford's algorithm. - The `N` and `stride` parameters determine which elements in `x` are accessed - at runtime. + The `N` and stride parameters determine which elements in the strided array + are accessed at runtime. Indexing is relative to the first index. To introduce an offset, use a typed array view. @@ -18,8 +18,8 @@ x: Array|TypedArray Input array. - stride: integer - Index increment. + strideX: integer + Stride length. Returns ------- @@ -33,22 +33,19 @@ > {{alias}}( x.length, x, 1 ) ~0.3333 - // Using `N` and `stride` parameters: + // Using `N` and stride parameters: > x = [ -2.0, 1.0, 1.0, -5.0, 2.0, -1.0 ]; - > var N = {{alias:@stdlib/math/base/special/floor}}( x.length / 2 ); - > var stride = 2; - > {{alias}}( N, x, stride ) + > {{alias}}( 3, x, 2 ) ~0.3333 // Using view offsets: > var x0 = new {{alias:@stdlib/array/float64}}( [ 1.0, -2.0, 3.0, 2.0, 5.0, -1.0 ] ); > var x1 = new {{alias:@stdlib/array/float64}}( x0.buffer, x0.BYTES_PER_ELEMENT*1 ); - > N = {{alias:@stdlib/math/base/special/floor}}( x0.length / 2 ); - > stride = 2; - > {{alias}}( N, x1, stride ) + > {{alias}}( 3, x1, 2 ) ~-0.3333 -{{alias}}.ndarray( N, x, stride, offset ) + +{{alias}}.ndarray( N, x, strideX, offsetX ) Computes the arithmetic mean of a strided array using Welford's algorithm and alternative indexing semantics. @@ -64,10 +61,10 @@ x: Array|TypedArray Input array. - stride: integer - Index increment. + strideX: integer + Stride length. - offset: integer + offsetX: integer Starting index. Returns @@ -84,8 +81,7 @@ // Using offset parameter: > var x = [ 1.0, -2.0, 3.0, 2.0, 5.0, -1.0 ]; - > var N = {{alias:@stdlib/math/base/special/floor}}( x.length / 2 ); - > {{alias}}.ndarray( N, x, 2, 1 ) + > {{alias}}.ndarray( 3, x, 2, 1 ) ~-0.3333 See Also diff --git a/lib/node_modules/@stdlib/stats/base/meanwd/docs/types/index.d.ts b/lib/node_modules/@stdlib/stats/base/meanwd/docs/types/index.d.ts index 5c27895109b0..13a9a0defafc 100644 --- a/lib/node_modules/@stdlib/stats/base/meanwd/docs/types/index.d.ts +++ b/lib/node_modules/@stdlib/stats/base/meanwd/docs/types/index.d.ts @@ -20,7 +20,12 @@ /// -import { NumericArray } from '@stdlib/types/array'; +import { NumericArray, Collection, AccessorArrayLike } from '@stdlib/types/array'; + +/** +* Input array. +*/ +type InputArray = NumericArray | Collection | AccessorArrayLike; /** * Interface describing `meanwd`. @@ -31,7 +36,7 @@ interface Routine { * * @param N - number of indexed elements * @param x - input array - * @param stride - stride length + * @param strideX - stride length * @returns arithmetic mean * * @example @@ -40,7 +45,7 @@ interface Routine { * var v = meanwd( x.length, x, 1 ); * // returns ~0.3333 */ - ( N: number, x: NumericArray, stride: number ): number; + ( N: number, x: InputArray, strideX: number ): number; /** * Computes the arithmetic mean of a strided array using Welford's algorithm and alternative indexing semantics. @@ -57,7 +62,7 @@ interface Routine { * var v = meanwd.ndarray( x.length, x, 1, 0 ); * // returns ~0.3333 */ - ndarray( N: number, x: NumericArray, stride: number, offset: number ): number; + ndarray( N: number, x: InputArray, strideX: number, offset: number ): number; } /** @@ -65,7 +70,7 @@ interface Routine { * * @param N - number of indexed elements * @param x - input array -* @param stride - stride length +* @param strideX - stride length * @returns arithmetic mean * * @example diff --git a/lib/node_modules/@stdlib/stats/base/meanwd/docs/types/test.ts b/lib/node_modules/@stdlib/stats/base/meanwd/docs/types/test.ts index b3882c0a7f65..924924034dc1 100644 --- a/lib/node_modules/@stdlib/stats/base/meanwd/docs/types/test.ts +++ b/lib/node_modules/@stdlib/stats/base/meanwd/docs/types/test.ts @@ -16,6 +16,7 @@ * limitations under the License. */ +import AccessorArray = require( '@stdlib/array/base/accessor' ); import meanwd = require( './index' ); @@ -26,6 +27,7 @@ import meanwd = require( './index' ); const x = new Float64Array( 10 ); meanwd( x.length, x, 1 ); // $ExpectType number + meanwd( x.length, new AccessorArray( x ), 1 ); // $ExpectType number } // The compiler throws an error if the function is provided a first argument which is not a number... @@ -85,6 +87,7 @@ import meanwd = require( './index' ); const x = new Float64Array( 10 ); meanwd.ndarray( x.length, x, 1, 0 ); // $ExpectType number + meanwd.ndarray( x.length, new AccessorArray( x ), 1, 0 ); // $ExpectType number } // The compiler throws an error if the `ndarray` method is provided a first argument which is not a number... diff --git a/lib/node_modules/@stdlib/stats/base/meanwd/examples/index.js b/lib/node_modules/@stdlib/stats/base/meanwd/examples/index.js index fbe6d212d43e..0cc260539459 100644 --- a/lib/node_modules/@stdlib/stats/base/meanwd/examples/index.js +++ b/lib/node_modules/@stdlib/stats/base/meanwd/examples/index.js @@ -18,18 +18,12 @@ 'use strict'; -var randu = require( '@stdlib/random/base/randu' ); -var round = require( '@stdlib/math/base/special/round' ); -var Float64Array = require( '@stdlib/array/float64' ); +var discreteUniform = require( '@stdlib/random/array/discrete-uniform' ); var meanwd = require( './../lib' ); -var x; -var i; - -x = new Float64Array( 10 ); -for ( i = 0; i < x.length; i++ ) { - x[ i ] = round( (randu()*100.0) - 50.0 ); -} +var x = discreteUniform( 10, -50, 50, { + 'dtype': 'float64' +}); console.log( x ); var v = meanwd( x.length, x, 1 ); diff --git a/lib/node_modules/@stdlib/stats/base/meanwd/lib/meanwd.js b/lib/node_modules/@stdlib/stats/base/meanwd/lib/accessors.js similarity index 68% rename from lib/node_modules/@stdlib/stats/base/meanwd/lib/meanwd.js rename to lib/node_modules/@stdlib/stats/base/meanwd/lib/accessors.js index 66e22daeb604..125b762bcce1 100644 --- a/lib/node_modules/@stdlib/stats/base/meanwd/lib/meanwd.js +++ b/lib/node_modules/@stdlib/stats/base/meanwd/lib/accessors.js @@ -1,7 +1,7 @@ /** * @license Apache-2.0 * -* Copyright (c) 2020 The Stdlib Authors. +* Copyright (c) 2025 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. @@ -41,41 +41,47 @@ * - 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). * - 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). * +* @private * @param {PositiveInteger} N - number of indexed elements -* @param {NumericArray} x - input array -* @param {integer} stride - stride length +* @param {Object} x - input array object +* @param {Collection} x.data - input array data +* @param {Array} x.accessors - array element accessors +* @param {integer} strideX - stride length +* @param {NonNegativeInteger} offsetX - starting index * @returns {number} arithmetic mean * * @example -* var x = [ 1.0, -2.0, 2.0 ]; -* var N = x.length; +* var toAccessorArray = require( '@stdlib/array/base/to-accessor-array' ); +* var arraylike2object = require( '@stdlib/array/base/arraylike2object' ); * -* var v = meanwd( N, x, 1 ); +* var x = toAccessorArray( [ 1.0, -2.0, 2.0 ] ); +* var v = meanwd( 3, arraylike2object( x ), 1, 0 ); * // returns ~0.3333 */ -function meanwd( N, x, stride ) { +function meanwd( N, x, strideX, offsetX ) { + var xbuf; + var get; var mu; var ix; var n; var i; - if ( N <= 0 ) { - return NaN; - } - if ( N === 1 || stride === 0 ) { - return x[ 0 ]; - } - if ( stride < 0 ) { - ix = (1-N) * stride; - } else { - ix = 0; + // Cache reference to array data: + xbuf = x.data; + + // Cache a reference to the element accessor: + get = x.accessors[ 0 ]; + + if ( N === 1 || strideX === 0 ) { + return get( xbuf, offsetX ); } + ix = offsetX; mu = 0.0; n = 0; for ( i = 0; i < N; i++ ) { n += 1; - mu += ( x[ix]-mu ) / n; - ix += stride; + mu += ( get( xbuf, ix )-mu ) / n; + ix += strideX; } return mu; } diff --git a/lib/node_modules/@stdlib/stats/base/meanwd/lib/index.js b/lib/node_modules/@stdlib/stats/base/meanwd/lib/index.js index f585b16c783f..4e9aa63d45e9 100644 --- a/lib/node_modules/@stdlib/stats/base/meanwd/lib/index.js +++ b/lib/node_modules/@stdlib/stats/base/meanwd/lib/index.js @@ -27,29 +27,33 @@ * var meanwd = require( '@stdlib/stats/base/meanwd' ); * * var x = [ 1.0, -2.0, 2.0 ]; -* var N = x.length; * -* var v = meanwd( N, x, 1 ); +* var v = meanwd( x.length, x, 1 ); * // returns ~0.3333 * * @example -* var floor = require( '@stdlib/math/base/special/floor' ); * var meanwd = require( '@stdlib/stats/base/meanwd' ); * * var x = [ 2.0, 1.0, 2.0, -2.0, -2.0, 2.0, 3.0, 4.0 ]; -* var N = floor( x.length / 2 ); * -* var v = meanwd.ndarray( N, x, 2, 1 ); +* var v = meanwd.ndarray( 4, x, 2, 1 ); * // returns 1.25 */ // MODULES // -var meanwd = require( './main.js' ); +var setReadOnly = require( '@stdlib/utils/define-nonenumerable-read-only-property' ); +var main = require( './main.js' ); +var ndarray = require( './ndarray.js' ); + + +// MAIN // + +setReadOnly( main, 'ndarray', ndarray ); // EXPORTS // -module.exports = meanwd; +module.exports = main; -// exports: { "ndarray": "meanwd.ndarray" } +// exports: { "ndarray": "main.ndarray" } diff --git a/lib/node_modules/@stdlib/stats/base/meanwd/lib/main.js b/lib/node_modules/@stdlib/stats/base/meanwd/lib/main.js index 08d52a5ad79c..f41000f0b562 100644 --- a/lib/node_modules/@stdlib/stats/base/meanwd/lib/main.js +++ b/lib/node_modules/@stdlib/stats/base/meanwd/lib/main.js @@ -20,14 +20,30 @@ // MODULES // -var setReadOnly = require( '@stdlib/utils/define-nonenumerable-read-only-property' ); -var meanwd = require( './meanwd.js' ); +var stride2offset = require( '@stdlib/strided/base/stride2offset' ); var ndarray = require( './ndarray.js' ); // MAIN // -setReadOnly( meanwd, 'ndarray', ndarray ); +/** +* Computes the minimum value of a strided array. +* +* @param {PositiveInteger} N - number of indexed elements +* @param {NumericArray} x - input array +* @param {integer} strideX - stride length +* @returns {number} minimum value +* +* @example +* var x = [ 1.0, -2.0, 2.0 ]; +* var N = x.length; +* +* var v = meanwd( N, x, 1 ); +* // returns ~0.3333 +*/ +function meanwd( N, x, strideX ) { + return ndarray( N, x, strideX, stride2offset( N, strideX ) ); +} // EXPORTS // diff --git a/lib/node_modules/@stdlib/stats/base/meanwd/lib/ndarray.js b/lib/node_modules/@stdlib/stats/base/meanwd/lib/ndarray.js index 9f45c2d4d45c..da7ba9a7cfe4 100644 --- a/lib/node_modules/@stdlib/stats/base/meanwd/lib/ndarray.js +++ b/lib/node_modules/@stdlib/stats/base/meanwd/lib/ndarray.js @@ -18,6 +18,12 @@ 'use strict'; +// MODULES // + +var arraylike2object = require( '@stdlib/array/base/arraylike2object' ); +var accessors = require( './accessors.js' ); + + // MAIN // /** @@ -43,38 +49,40 @@ * * @param {PositiveInteger} N - number of indexed elements * @param {NumericArray} x - input array -* @param {integer} stride - stride length -* @param {NonNegativeInteger} offset - starting index +* @param {integer} strideX - stride length +* @param {NonNegativeInteger} offsetX - starting index * @returns {number} arithmetic mean * * @example -* var floor = require( '@stdlib/math/base/special/floor' ); -* * var x = [ 2.0, 1.0, 2.0, -2.0, -2.0, 2.0, 3.0, 4.0 ]; -* var N = floor( x.length / 2 ); * -* var v = meanwd( N, x, 2, 1 ); +* var v = meanwd( 4, x, 2, 1 ); * // returns 1.25 */ -function meanwd( N, x, stride, offset ) { +function meanwd( N, x, strideX, offsetX ) { var mu; var ix; + var o; var n; var i; if ( N <= 0 ) { return NaN; } - if ( N === 1 || stride === 0 ) { - return x[ offset ]; + o = arraylike2object( x ); + if ( o.accessorProtocol ) { + return accessors( N, o, strideX, offsetX ); + } + if ( N === 1 || strideX === 0 ) { + return x[ offsetX ]; } - ix = offset; + ix = offsetX; mu = 0.0; n = 0; for ( i = 0; i < N; i++ ) { n += 1; mu += ( x[ix]-mu ) / n; - ix += stride; + ix += strideX; } return mu; } diff --git a/lib/node_modules/@stdlib/stats/base/meanwd/test/test.meanwd.js b/lib/node_modules/@stdlib/stats/base/meanwd/test/test.main.js similarity index 68% rename from lib/node_modules/@stdlib/stats/base/meanwd/test/test.meanwd.js rename to lib/node_modules/@stdlib/stats/base/meanwd/test/test.main.js index 96d59c453dce..ef75a8d340d6 100644 --- a/lib/node_modules/@stdlib/stats/base/meanwd/test/test.meanwd.js +++ b/lib/node_modules/@stdlib/stats/base/meanwd/test/test.main.js @@ -21,10 +21,10 @@ // MODULES // var tape = require( 'tape' ); -var floor = require( '@stdlib/math/base/special/floor' ); var isnan = require( '@stdlib/math/base/assert/is-nan' ); +var toAccessorArray = require( '@stdlib/array/base/to-accessor-array' ); var Float64Array = require( '@stdlib/array/float64' ); -var meanwd = require( './../lib/meanwd.js' ); +var meanwd = require( './../lib/main.js' ); // TESTS // @@ -57,6 +57,23 @@ tape( 'the function calculates the arithmetic mean of a strided array', function t.end(); }); +tape( 'the function calculates the arithmetic mean of a strided array (accessors)', function test( t ) { + var x; + var v; + + x = [ 1.0, -2.0, -4.0, 5.0, 0.0, 3.0 ]; + + v = meanwd( x.length, toAccessorArray( x ), 1 ); + t.strictEqual( v, 0.5, 'returns expected value' ); + + x = [ -4.0 ]; + + v = meanwd( x.length, toAccessorArray( x ), 1 ); + t.strictEqual( v, -4.0, 'returns expected value' ); + + t.end(); +}); + tape( 'if provided an `N` parameter less than or equal to `0`, the function returns `NaN`', function test( t ) { var x; var v; @@ -85,7 +102,6 @@ tape( 'if provided an `N` parameter equal to `1`, the function returns the first }); tape( 'the function supports a `stride` parameter', function test( t ) { - var N; var x; var v; @@ -100,15 +116,34 @@ tape( 'the function supports a `stride` parameter', function test( t ) { 2.0 ]; - N = floor( x.length / 2 ); - v = meanwd( N, x, 2 ); + v = meanwd( 4, x, 2 ); + + t.strictEqual( v, 1.25, 'returns expected value' ); + t.end(); +}); + +tape( 'the function supports a `stride` parameter (accessors)', function test( t ) { + var x; + var v; + + x = [ + 1.0, // 0 + 2.0, + 2.0, // 1 + -7.0, + -2.0, // 2 + 3.0, + 4.0, // 3 + 2.0 + ]; + + v = meanwd( 4, toAccessorArray( x ), 2 ); t.strictEqual( v, 1.25, 'returns expected value' ); t.end(); }); tape( 'the function supports a negative `stride` parameter', function test( t ) { - var N; var x; var v; @@ -123,8 +158,28 @@ tape( 'the function supports a negative `stride` parameter', function test( t ) 2.0 ]; - N = floor( x.length / 2 ); - v = meanwd( N, x, -2 ); + v = meanwd( 4, x, -2 ); + + t.strictEqual( v, 1.25, 'returns expected value' ); + t.end(); +}); + +tape( 'the function supports a negative `stride` parameter (accessors)', function test( t ) { + var x; + var v; + + x = [ + 1.0, // 3 + 2.0, + 2.0, // 2 + -7.0, + -2.0, // 1 + 3.0, + 4.0, // 0 + 2.0 + ]; + + v = meanwd( 4, toAccessorArray( x ), -2 ); t.strictEqual( v, 1.25, 'returns expected value' ); t.end(); @@ -142,10 +197,21 @@ tape( 'if provided a `stride` parameter equal to `0`, the function returns the f t.end(); }); +tape( 'if provided a `stride` parameter equal to `0`, the function returns the first element (accessors)', function test( t ) { + var x; + var v; + + x = [ 1.0, -2.0, -4.0, 5.0, 3.0 ]; + + v = meanwd( x.length, toAccessorArray( x ), 0 ); + t.strictEqual( v, 1.0, 'returns expected value' ); + + t.end(); +}); + tape( 'the function supports view offsets', function test( t ) { var x0; var x1; - var N; var v; x0 = new Float64Array([ @@ -161,9 +227,8 @@ tape( 'the function supports view offsets', function test( t ) { ]); x1 = new Float64Array( x0.buffer, x0.BYTES_PER_ELEMENT*1 ); // start at 2nd element - N = floor(x1.length / 2); - v = meanwd( N, x1, 2 ); + v = meanwd( 4, x1, 2 ); t.strictEqual( v, 1.25, 'returns expected value' ); t.end(); diff --git a/lib/node_modules/@stdlib/stats/base/meanwd/test/test.ndarray.js b/lib/node_modules/@stdlib/stats/base/meanwd/test/test.ndarray.js index 4fa763ee2544..bc985e2dab6d 100644 --- a/lib/node_modules/@stdlib/stats/base/meanwd/test/test.ndarray.js +++ b/lib/node_modules/@stdlib/stats/base/meanwd/test/test.ndarray.js @@ -21,8 +21,8 @@ // MODULES // var tape = require( 'tape' ); -var floor = require( '@stdlib/math/base/special/floor' ); var isnan = require( '@stdlib/math/base/assert/is-nan' ); +var toAccessorArray = require( '@stdlib/array/base/to-accessor-array' ); var meanwd = require( './../lib/ndarray.js' ); @@ -56,6 +56,23 @@ tape( 'the function calculates the arithmetic mean of a strided array', function t.end(); }); +tape( 'the function calculates the arithmetic mean of a strided array (accessors)', function test( t ) { + var x; + var v; + + x = [ 1.0, -2.0, -4.0, 5.0, 0.0, 3.0 ]; + + v = meanwd( x.length, toAccessorArray( x ), 1, 0 ); + t.strictEqual( v, 0.5, 'returns expected value' ); + + x = [ -4.0 ]; + + v = meanwd( x.length, toAccessorArray( x ), 1, 0 ); + t.strictEqual( v, -4.0, 'returns expected value' ); + + t.end(); +}); + tape( 'if provided an `N` parameter less than or equal to `0`, the function returns `NaN`', function test( t ) { var x; var v; @@ -84,7 +101,6 @@ tape( 'if provided an `N` parameter equal to `1`, the function returns the first }); tape( 'the function supports a `stride` parameter', function test( t ) { - var N; var x; var v; @@ -99,15 +115,34 @@ tape( 'the function supports a `stride` parameter', function test( t ) { 2.0 ]; - N = floor( x.length / 2 ); - v = meanwd( N, x, 2, 0 ); + v = meanwd( 4, x, 2, 0 ); + + t.strictEqual( v, 1.25, 'returns expected value' ); + t.end(); +}); + +tape( 'the function supports a `stride` parameter (accessors)', function test( t ) { + var x; + var v; + + x = [ + 1.0, // 0 + 2.0, + 2.0, // 1 + -7.0, + -2.0, // 2 + 3.0, + 4.0, // 3 + 2.0 + ]; + + v = meanwd( 4, x, 2, 0 ); t.strictEqual( v, 1.25, 'returns expected value' ); t.end(); }); tape( 'the function supports a negative `stride` parameter', function test( t ) { - var N; var x; var v; @@ -122,8 +157,28 @@ tape( 'the function supports a negative `stride` parameter', function test( t ) 2.0 ]; - N = floor( x.length / 2 ); - v = meanwd( N, x, -2, 6 ); + v = meanwd( 4, x, -2, 6 ); + + t.strictEqual( v, 1.25, 'returns expected value' ); + t.end(); +}); + +tape( 'the function supports a negative `stride` parameter (accessors)', function test( t ) { + var x; + var v; + + x = [ + 1.0, // 3 + 2.0, + 2.0, // 2 + -7.0, + -2.0, // 1 + 3.0, + 4.0, // 0 + 2.0 + ]; + + v = meanwd( 4, toAccessorArray( x ), -2, 6 ); t.strictEqual( v, 1.25, 'returns expected value' ); t.end(); @@ -141,8 +196,40 @@ tape( 'if provided a `stride` parameter equal to `0`, the function returns the f t.end(); }); +tape( 'if provided a `stride` parameter equal to `0`, the function returns the first indexed element (accessors)', function test( t ) { + var x; + var v; + + x = [ 1.0, -2.0, -4.0, 5.0, 3.0 ]; + + v = meanwd( x.length, toAccessorArray( x ), 0, 0 ); + t.strictEqual( v, 1.0, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports an `offset` parameter', function test( t ) { + var x; + var v; + + x = [ + 2.0, + 1.0, // 0 + 2.0, + -2.0, // 1 + -2.0, + 2.0, // 2 + 3.0, + 4.0 // 3 + ]; + + v = meanwd( 4, x, 2, 1 ); + t.strictEqual( v, 1.25, 'returns expected value' ); + + t.end(); +}); + tape( 'the function supports an `offset` parameter', function test( t ) { - var N; var x; var v; @@ -156,9 +243,8 @@ tape( 'the function supports an `offset` parameter', function test( t ) { 3.0, 4.0 // 3 ]; - N = floor( x.length / 2 ); - v = meanwd( N, x, 2, 1 ); + v = meanwd( 4, toAccessorArray( x ), 2, 1 ); t.strictEqual( v, 1.25, 'returns expected value' ); t.end();