diff --git a/lib/node_modules/@stdlib/stats/base/meanors/README.md b/lib/node_modules/@stdlib/stats/base/meanors/README.md index 43651202ea2c..0fdc33d08ffb 100644 --- a/lib/node_modules/@stdlib/stats/base/meanors/README.md +++ b/lib/node_modules/@stdlib/stats/base/meanors/README.md @@ -51,15 +51,14 @@ The [arithmetic mean][arithmetic-mean] is defined as var meanors = require( '@stdlib/stats/base/meanors' ); ``` -#### meanors( N, x, stride ) +#### meanors( N, x, strideX ) -Computes the [arithmetic mean][arithmetic-mean] of a strided array `x` using ordinary recursive summation. +Computes the [arithmetic mean][arithmetic-mean] of a strided array using ordinary recursive summation. ```javascript var x = [ 1.0, -2.0, 2.0 ]; -var N = x.length; -var v = meanors( N, x, 1 ); +var v = meanors( x.length, x, 1 ); // returns ~0.3333 ``` @@ -67,17 +66,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 = meanors( N, x, 2 ); +var v = meanors( 4, x, 2 ); // returns 1.25 ``` @@ -87,42 +83,35 @@ 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 = meanors( N, x1, 2 ); +var v = meanors( 4, x1, 2 ); // returns 1.25 ``` -#### meanors.ndarray( N, x, stride, offset ) +#### meanors.ndarray( N, x, strideX, offsetX ) Computes the [arithmetic mean][arithmetic-mean] of a strided array using ordinary recursive summation and alternative indexing semantics. ```javascript var x = [ 1.0, -2.0, 2.0 ]; -var N = x.length; -var v = meanors.ndarray( N, x, 1, 0 ); +var v = meanors.ndarray( x.length, 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 `x` 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 = meanors.ndarray( N, x, 2, 1 ); +var v = meanors.ndarray( 4, x, 2, 1 ); // returns 1.25 ``` @@ -136,6 +125,7 @@ var v = meanors.ndarray( N, x, 2, 1 ); - If `N <= 0`, both functions return `NaN`. - Ordinary recursive summation (i.e., a "simple" sum) is performant, but can incur significant numerical error. If performance is paramount and error tolerated, using ordinary recursive summation to compute an arithmetic mean is acceptable; in all other cases, exercise due caution. +- 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 ([`dmeanors`][@stdlib/stats/strided/dmeanors], [`smeanors`][@stdlib/stats/base/smeanors], etc.) are likely to be significantly more performant. @@ -149,18 +139,12 @@ var v = meanors.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 meanors = require( '@stdlib/stats/base/meanors' ); -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 = meanors( x.length, x, 1 ); @@ -204,6 +188,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 + [@stdlib/stats/strided/dmeanors]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/strided/dmeanors diff --git a/lib/node_modules/@stdlib/stats/base/meanors/benchmark/benchmark.js b/lib/node_modules/@stdlib/stats/base/meanors/benchmark/benchmark.js index 786039489d6a..1a86329d32c9 100644 --- a/lib/node_modules/@stdlib/stats/base/meanors/benchmark/benchmark.js +++ b/lib/node_modules/@stdlib/stats/base/meanors/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 meanors = require( './../lib/meanors.js' ); +var meanors = require( './../lib/main.js' ); + + +// VARIABLES // + +var options = { + 'dtype': 'generic' +}; // FUNCTIONS // @@ -38,13 +45,7 @@ var meanors = require( './../lib/meanors.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/meanors/benchmark/benchmark.ndarray.js b/lib/node_modules/@stdlib/stats/base/meanors/benchmark/benchmark.ndarray.js index dc4523cd5eda..228271e6ed20 100644 --- a/lib/node_modules/@stdlib/stats/base/meanors/benchmark/benchmark.ndarray.js +++ b/lib/node_modules/@stdlib/stats/base/meanors/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 meanors = require( './../lib/ndarray.js' ); +// VARIABLES // + +var options = { + 'dtype': 'generic' +}; + + // FUNCTIONS // /** @@ -38,13 +45,7 @@ var meanors = 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/meanors/docs/repl.txt b/lib/node_modules/@stdlib/stats/base/meanors/docs/repl.txt index 1027d7775651..bc1488356863 100644 --- a/lib/node_modules/@stdlib/stats/base/meanors/docs/repl.txt +++ b/lib/node_modules/@stdlib/stats/base/meanors/docs/repl.txt @@ -1,10 +1,10 @@ -{{alias}}( N, x, stride ) +{{alias}}( N, x, strideX ) Computes the arithmetic mean of a strided array using ordinary recursive summation. - 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. @@ -19,8 +19,8 @@ x: Array|TypedArray Input array. - stride: integer - Index increment. + strideX: integer + Stride length. Returns ------- @@ -36,20 +36,17 @@ // 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 ordinary recursive summation and alternative indexing semantics. @@ -65,10 +62,10 @@ x: Array|TypedArray Input array. - stride: integer - Index increment. + strideX: integer + Stride length. - offset: integer + offsetX: integer Starting index. Returns @@ -86,7 +83,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/meanors/docs/types/index.d.ts b/lib/node_modules/@stdlib/stats/base/meanors/docs/types/index.d.ts index 2a9435fe895b..eb7ee69aeaca 100644 --- a/lib/node_modules/@stdlib/stats/base/meanors/docs/types/index.d.ts +++ b/lib/node_modules/@stdlib/stats/base/meanors/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 `meanors`. @@ -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,15 +45,15 @@ interface Routine { * var v = meanors( 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 ordinary recursive summation and alternative indexing semantics. * * @param N - number of indexed elements * @param x - input array - * @param stride - stride length - * @param offset - starting index + * @param strideX - stride length + * @param offsetX - starting index * @returns arithmetic mean * * @example @@ -57,7 +62,7 @@ interface Routine { * var v = meanors.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, offsetX: 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/meanors/docs/types/test.ts b/lib/node_modules/@stdlib/stats/base/meanors/docs/types/test.ts index 6a1c6df4ad12..17cf2bdda002 100644 --- a/lib/node_modules/@stdlib/stats/base/meanors/docs/types/test.ts +++ b/lib/node_modules/@stdlib/stats/base/meanors/docs/types/test.ts @@ -16,6 +16,7 @@ * limitations under the License. */ +import AccessorArray = require( '@stdlib/array/base/accessor' ); import meanors = require( './index' ); @@ -26,6 +27,7 @@ import meanors = require( './index' ); const x = new Float64Array( 10 ); meanors( x.length, x, 1 ); // $ExpectType number + meanors( 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 meanors = require( './index' ); const x = new Float64Array( 10 ); meanors.ndarray( x.length, x, 1, 0 ); // $ExpectType number + meanors.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/meanors/examples/index.js b/lib/node_modules/@stdlib/stats/base/meanors/examples/index.js index 5d0dca7b792c..317d437739cb 100644 --- a/lib/node_modules/@stdlib/stats/base/meanors/examples/index.js +++ b/lib/node_modules/@stdlib/stats/base/meanors/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 meanors = 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 = meanors( x.length, x, 1 ); diff --git a/lib/node_modules/@stdlib/stats/base/meanors/lib/index.js b/lib/node_modules/@stdlib/stats/base/meanors/lib/index.js index e5d6ba05786d..104b6ccffae3 100644 --- a/lib/node_modules/@stdlib/stats/base/meanors/lib/index.js +++ b/lib/node_modules/@stdlib/stats/base/meanors/lib/index.js @@ -27,19 +27,16 @@ * var meanors = require( '@stdlib/stats/base/meanors' ); * * var x = [ 1.0, -2.0, 2.0 ]; -* var N = x.length; * -* var v = meanors( N, x, 1 ); +* var v = meanors( x.length, x, 1 ); * // returns ~0.3333 * * @example -* var floor = require( '@stdlib/math/base/special/floor' ); * var meanors = require( '@stdlib/stats/base/meanors' ); * * 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 = meanors.ndarray( N, x, 2, 1 ); +* var v = meanors.ndarray( 4, x, 2, 1 ); * // returns 1.25 */ diff --git a/lib/node_modules/@stdlib/stats/base/meanors/lib/meanors.js b/lib/node_modules/@stdlib/stats/base/meanors/lib/meanors.js index a90aa15b2844..33c9c9c7b20b 100644 --- a/lib/node_modules/@stdlib/stats/base/meanors/lib/meanors.js +++ b/lib/node_modules/@stdlib/stats/base/meanors/lib/meanors.js @@ -20,7 +20,8 @@ // MODULES // -var gsumors = require( '@stdlib/blas/ext/base/gsumors' ); +var stride2offset = require( '@stdlib/strided/base/stride2offset' ); +var ndarray = require( './ndarray.js' ); // MAIN // @@ -30,24 +31,17 @@ var gsumors = require( '@stdlib/blas/ext/base/gsumors' ); * * @param {PositiveInteger} N - number of indexed elements * @param {NumericArray} x - input array -* @param {integer} stride - stride length +* @param {integer} strideX - stride length * @returns {number} arithmetic mean * * @example * var x = [ 1.0, -2.0, 2.0 ]; -* var N = x.length; * -* var v = meanors( N, x, 1 ); +* var v = meanors( x.length, x, 1 ); * // returns ~0.3333 */ -function meanors( N, x, stride ) { - if ( N <= 0 ) { - return NaN; - } - if ( N === 1 || stride === 0 ) { - return x[ 0 ]; - } - return gsumors( N, x, stride ) / N; +function meanors( N, x, strideX ) { + return ndarray( N, x, strideX, stride2offset( N, strideX ) ); } diff --git a/lib/node_modules/@stdlib/stats/base/meanors/lib/ndarray.js b/lib/node_modules/@stdlib/stats/base/meanors/lib/ndarray.js index e84c5247dc6e..bcae044a0580 100644 --- a/lib/node_modules/@stdlib/stats/base/meanors/lib/ndarray.js +++ b/lib/node_modules/@stdlib/stats/base/meanors/lib/ndarray.js @@ -30,27 +30,21 @@ var gsumors = require( '@stdlib/blas/ext/base/gsumors' ).ndarray; * * @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 = meanors( N, x, 2, 1 ); +* var v = meanors( 4, x, 2, 1 ); * // returns 1.25 */ -function meanors( N, x, stride, offset ) { +function meanors( N, x, strideX, offsetX ) { if ( N <= 0 ) { return NaN; } - if ( N === 1 || stride === 0 ) { - return x[ offset ]; - } - return gsumors( N, x, stride, offset ) / N; + return gsumors( N, x, strideX, offsetX ) / N; } diff --git a/lib/node_modules/@stdlib/stats/base/meanors/test/test.meanors.js b/lib/node_modules/@stdlib/stats/base/meanors/test/test.meanors.js index 9a3159a1650e..9c91bd97cc0b 100644 --- a/lib/node_modules/@stdlib/stats/base/meanors/test/test.meanors.js +++ b/lib/node_modules/@stdlib/stats/base/meanors/test/test.meanors.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 Float64Array = require( '@stdlib/array/float64' ); -var meanors = require( './../lib/meanors.js' ); +var toAccessorArray = require( '@stdlib/array/base/to-accessor-array' ); +var meanors = 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 = meanors( x.length, toAccessorArray( x ), 1 ); + t.strictEqual( v, 0.5, 'returns expected value' ); + + x = [ -4.0 ]; + + v = meanors( 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; @@ -72,6 +89,21 @@ tape( 'if provided an `N` parameter less than or equal to `0`, the function retu t.end(); }); +tape( 'if provided an `N` parameter less than or equal to `0`, the function returns `NaN` (accessors)', function test( t ) { + var x; + var v; + + x = [ 1.0, -2.0, -4.0, 5.0, 3.0 ]; + + v = meanors( 0, toAccessorArray( x ), 1 ); + t.strictEqual( isnan( v ), true, 'returns expected value' ); + + v = meanors( -1, toAccessorArray( x ), 1 ); + t.strictEqual( isnan( v ), true, 'returns expected value' ); + + t.end(); +}); + tape( 'if provided an `N` parameter equal to `1`, the function returns the first element', function test( t ) { var x; var v; @@ -84,8 +116,19 @@ tape( 'if provided an `N` parameter equal to `1`, the function returns the first t.end(); }); +tape( 'if provided an `N` parameter equal to `1`, 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 = meanors( 1, toAccessorArray( x ), 1 ); + t.strictEqual( v, 1.0, 'returns expected value' ); + + t.end(); +}); + tape( 'the function supports a `stride` parameter', function test( t ) { - var N; var x; var v; @@ -100,15 +143,34 @@ tape( 'the function supports a `stride` parameter', function test( t ) { 2.0 ]; - N = floor( x.length / 2 ); - v = meanors( N, x, 2 ); + v = meanors( 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 = meanors( 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 +185,28 @@ tape( 'the function supports a negative `stride` parameter', function test( t ) 2.0 ]; - N = floor( x.length / 2 ); - v = meanors( N, x, -2 ); + v = meanors( 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 = meanors( 4, toAccessorArray( x ), -2 ); t.strictEqual( v, 1.25, 'returns expected value' ); t.end(); @@ -142,10 +224,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 = meanors( 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 +254,33 @@ 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 = meanors( N, x1, 2 ); + v = meanors( 4, x1, 2 ); + t.strictEqual( v, 1.25, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports view offsets (accessors)', function test( t ) { + var x0; + var x1; + var v; + + x0 = new Float64Array([ + 2.0, + 1.0, // 0 + 2.0, + -2.0, // 1 + -2.0, + 2.0, // 2 + 3.0, + 4.0, // 3 + 6.0 + ]); + + x1 = new Float64Array( x0.buffer, x0.BYTES_PER_ELEMENT*1 ); // start at 2nd element + + v = meanors( 4, toAccessorArray( x1 ), 2 ); t.strictEqual( v, 1.25, 'returns expected value' ); t.end(); diff --git a/lib/node_modules/@stdlib/stats/base/meanors/test/test.ndarray.js b/lib/node_modules/@stdlib/stats/base/meanors/test/test.ndarray.js index 27eeea78a654..073d2c68ed53 100644 --- a/lib/node_modules/@stdlib/stats/base/meanors/test/test.ndarray.js +++ b/lib/node_modules/@stdlib/stats/base/meanors/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 meanors = 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 = meanors( x.length, toAccessorArray( x ), 1, 0 ); + t.strictEqual( v, 0.5, 'returns expected value' ); + + x = [ -4.0 ]; + + v = meanors( 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; @@ -71,6 +88,21 @@ tape( 'if provided an `N` parameter less than or equal to `0`, the function retu t.end(); }); +tape( 'if provided an `N` parameter less than or equal to `0`, the function returns `NaN` (accessors)', function test( t ) { + var x; + var v; + + x = [ 1.0, -2.0, -4.0, 5.0, 3.0 ]; + + v = meanors( 0, toAccessorArray( x ), 1, 0 ); + t.strictEqual( isnan( v ), true, 'returns expected value' ); + + v = meanors( -1, toAccessorArray( x ), 1, 0 ); + t.strictEqual( isnan( v ), true, 'returns expected value' ); + + t.end(); +}); + tape( 'if provided an `N` parameter equal to `1`, the function returns the first indexed element', function test( t ) { var x; var v; @@ -83,8 +115,19 @@ tape( 'if provided an `N` parameter equal to `1`, the function returns the first t.end(); }); +tape( 'if provided an `N` parameter equal to `1`, 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 = meanors( 1, toAccessorArray( x ), 1, 0 ); + t.strictEqual( v, 1.0, 'returns expected value' ); + + t.end(); +}); + tape( 'the function supports a `stride` parameter', function test( t ) { - var N; var x; var v; @@ -99,15 +142,34 @@ tape( 'the function supports a `stride` parameter', function test( t ) { 2.0 ]; - N = floor( x.length / 2 ); - v = meanors( N, x, 2, 0 ); + v = meanors( 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 = meanors( 4, toAccessorArray( 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 +184,28 @@ tape( 'the function supports a negative `stride` parameter', function test( t ) 2.0 ]; - N = floor( x.length / 2 ); - v = meanors( N, x, -2, 6 ); + v = meanors( 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 = meanors( 4, toAccessorArray( x ), -2, 6 ); t.strictEqual( v, 1.25, 'returns expected value' ); t.end(); @@ -141,8 +223,19 @@ 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 = meanors( 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 N; var x; var v; @@ -156,9 +249,29 @@ tape( 'the function supports an `offset` parameter', function test( t ) { 3.0, 4.0 // 3 ]; - N = floor( x.length / 2 ); - v = meanors( N, x, 2, 1 ); + v = meanors( 4, x, 2, 1 ); + t.strictEqual( v, 1.25, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports an `offset` parameter (accessors)', 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 = meanors( 4, toAccessorArray( x ), 2, 1 ); t.strictEqual( v, 1.25, 'returns expected value' ); t.end();