diff --git a/lib/node_modules/@stdlib/stats/base/meanpn/README.md b/lib/node_modules/@stdlib/stats/base/meanpn/README.md index 2d6091bd86d7..2577ed074b2b 100644 --- a/lib/node_modules/@stdlib/stats/base/meanpn/README.md +++ b/lib/node_modules/@stdlib/stats/base/meanpn/README.md @@ -2,7 +2,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. @@ -51,15 +51,14 @@ The [arithmetic mean][arithmetic-mean] is defined as var meanpn = require( '@stdlib/stats/base/meanpn' ); ``` -#### meanpn( N, x, stride ) +#### meanpn( N, x, strideX ) -Computes the [arithmetic mean][arithmetic-mean] of a strided array `x` using a two-pass error correction algorithm. +Computes the [arithmetic mean][arithmetic-mean] of a strided array using a two-pass error correction algorithm. ```javascript var x = [ 1.0, -2.0, 2.0 ]; -var N = x.length; -var v = meanpn( N, x, 1 ); +var v = meanpn( x.length, x, 1 ); // returns ~0.3333 ``` @@ -67,17 +66,13 @@ 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 the input array ```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 = meanpn( N, x, 2 ); +var v = meanpn( 4, x, 2 ); // returns 1.25 ``` @@ -87,42 +82,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 = meanpn( N, x1, 2 ); +var v = meanpn( 4, x1, 2 ); // returns 1.25 ``` -#### meanpn.ndarray( N, x, stride, offset ) +#### meanpn.ndarray( N, x, strideX, offsetX ) Computes the [arithmetic mean][arithmetic-mean] of a strided array using a two-pass error correction algorithm and alternative indexing semantics. ```javascript var x = [ 1.0, -2.0, 2.0 ]; -var N = x.length; -var v = meanpn.ndarray( N, x, 1, 0 ); +var v = meanpn.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 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 = meanpn.ndarray( N, x, 2, 1 ); +var v = meanpn.ndarray( 4, x, 2, 1 ); // returns 1.25 ``` @@ -135,6 +123,7 @@ var v = meanpn.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 ([`dmeanpn`][@stdlib/stats/base/dmeanpn], [`smeanpn`][@stdlib/stats/base/smeanpn], etc.) are likely to be significantly more performant. @@ -148,18 +137,12 @@ var v = meanpn.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 meanpn = require( '@stdlib/stats/base/meanpn' ); -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 = meanpn( x.length, x, 1 ); @@ -210,6 +193,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 + [@neely:1966a]: https://doi.org/10.1145/365719.365958 [@schubert:2018a]: https://doi.org/10.1145/3221269.3223036 diff --git a/lib/node_modules/@stdlib/stats/base/meanpn/benchmark/benchmark.js b/lib/node_modules/@stdlib/stats/base/meanpn/benchmark/benchmark.js index 93e5e5da8440..b50cb1942a3f 100644 --- a/lib/node_modules/@stdlib/stats/base/meanpn/benchmark/benchmark.js +++ b/lib/node_modules/@stdlib/stats/base/meanpn/benchmark/benchmark.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. @@ -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 meanpn = require( './../lib/meanpn.js' ); +var meanpn = require( './../lib/main.js' ); + + +// VARIABLES // + +var options = { + 'dtype': 'generic' +}; // FUNCTIONS // @@ -38,13 +45,7 @@ var meanpn = require( './../lib/meanpn.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/meanpn/docs/repl.txt b/lib/node_modules/@stdlib/stats/base/meanpn/docs/repl.txt index 2ab9f8a3e820..690600a8e3ab 100644 --- a/lib/node_modules/@stdlib/stats/base/meanpn/docs/repl.txt +++ b/lib/node_modules/@stdlib/stats/base/meanpn/docs/repl.txt @@ -1,10 +1,10 @@ -{{alias}}( N, x, stride ) +{{alias}}( N, x, strideX ) Computes the arithmetic mean of a strided array using a two-pass error correction algorithm. - The `N` and `stride` parameters determine which elements in `x` are accessed - at runtime. + The `N` and stride parameters determine which elements 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,25 +36,22 @@ // 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 a two-pass error correction algorithm and alternative indexing semantics. While typed array views mandate a view offset based on the underlying - buffer, the `offset` parameter supports indexing semantics based on a + buffer, the offset parameter supports indexing semantics based on a starting index. Parameters @@ -65,10 +62,10 @@ x: Array|TypedArray Input array. - stride: integer - Index increment. + strideX: integer + Stride length. - offset: integer + offsetX: integer Starting index. Returns @@ -85,8 +82,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/meanpn/docs/types/index.d.ts b/lib/node_modules/@stdlib/stats/base/meanpn/docs/types/index.d.ts index 55b85f2ea987..54bd111f5a96 100644 --- a/lib/node_modules/@stdlib/stats/base/meanpn/docs/types/index.d.ts +++ b/lib/node_modules/@stdlib/stats/base/meanpn/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 `meanpn`. @@ -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 = meanpn( x.length, x, 1 ); * // returns ~0.3333 */ - ( N: number, x: NumericArray, stride: number ): number; + ( N: number, x: InputArray, stride: number ): number; /** * Computes the arithmetic mean of a strided array using a two-pass error correction algorithm 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 = meanpn.ndarray( x.length, x, 1, 0 ); * // returns ~0.3333 */ - ndarray( N: number, x: NumericArray, stride: number, offset: number ): number; + ndarray( N: number, x: InputArray, stride: 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/meanpn/docs/types/test.ts b/lib/node_modules/@stdlib/stats/base/meanpn/docs/types/test.ts index 907803b47531..bdf06bf75e17 100644 --- a/lib/node_modules/@stdlib/stats/base/meanpn/docs/types/test.ts +++ b/lib/node_modules/@stdlib/stats/base/meanpn/docs/types/test.ts @@ -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. @@ -16,6 +16,7 @@ * limitations under the License. */ +import AccessorArray = require( '@stdlib/array/base/accessor' ); import meanpn = require( './index' ); @@ -26,6 +27,7 @@ import meanpn = require( './index' ); const x = new Float64Array( 10 ); meanpn( x.length, x, 1 ); // $ExpectType number + meanpn( 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 meanpn = require( './index' ); const x = new Float64Array( 10 ); meanpn.ndarray( x.length, x, 1, 0 ); // $ExpectType number + meanpn.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/meanpn/examples/index.js b/lib/node_modules/@stdlib/stats/base/meanpn/examples/index.js index 6ad01ae95c0f..30c19ffe85ae 100644 --- a/lib/node_modules/@stdlib/stats/base/meanpn/examples/index.js +++ b/lib/node_modules/@stdlib/stats/base/meanpn/examples/index.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. @@ -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 meanpn = 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 = meanpn( x.length, x, 1 ); diff --git a/lib/node_modules/@stdlib/stats/base/meanpn/lib/accessors.js b/lib/node_modules/@stdlib/stats/base/meanpn/lib/accessors.js new file mode 100644 index 000000000000..e28d24b5f8d6 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/base/meanpn/lib/accessors.js @@ -0,0 +1,72 @@ +/** +* @license Apache-2.0 +* +* 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. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var gsumpw = require( '@stdlib/blas/ext/base/gsumpw' ).ndarray; +var gapxsumpw = require( '@stdlib/blas/ext/base/gapxsumpw' ).ndarray; + + +// MAIN // + +/** +* Computes the arithmetic mean of a strided array using a two-pass error correction algorithm. +* +* @private +* @param {PositiveInteger} N - number of indexed elements +* @param {Object} x - input array object +* @param {integer} strideX - stride length +* @param {NonNegativeInteger} offsetX - starting index +* @returns {number} arithmetic mean +* +* @example +* var toAccessorArray = require( '@stdlib/array/base/to-accessor-array' ); +* var arraylike2object = require( '@stdlib/array/base/arraylike2object' ); +* +* var x = toAccessorArray( [ 2.0, 1.0, 2.0, -2.0, -2.0, 2.0, 3.0, 4.0 ] ); +* +* var v = meanpn( 4, arraylike2object( x ), 2, 1 ); +* // returns 1.25 +*/ +function meanpn( N, x, strideX, offsetX ) { + var xbuf; + var get; + var mu; + var c; + + xbuf = x.data; + get = x.accessors[ 0 ]; + + if ( N === 1 || strideX === 0 ) { + return get( xbuf, offsetX ); + } + // Compute an estimate for the meanpn: + mu = gsumpw( N, xbuf, strideX, offsetX ) / N; + + // Compute an error term: + c = gapxsumpw( N, -mu, xbuf, strideX, offsetX ) / N; + + return mu + c; +} + + +// EXPORTS // + +module.exports = meanpn; diff --git a/lib/node_modules/@stdlib/stats/base/meanpn/lib/index.js b/lib/node_modules/@stdlib/stats/base/meanpn/lib/index.js index e290ed148ba1..257321818928 100644 --- a/lib/node_modules/@stdlib/stats/base/meanpn/lib/index.js +++ b/lib/node_modules/@stdlib/stats/base/meanpn/lib/index.js @@ -27,19 +27,16 @@ * var meanpn = require( '@stdlib/stats/base/meanpn' ); * * var x = [ 1.0, -2.0, 2.0 ]; -* var N = x.length; * -* var v = meanpn( N, x, 1 ); +* var v = meanpn( 3, x, 1 ); * // returns ~0.3333 * * @example -* var floor = require( '@stdlib/math/base/special/floor' ); * var meanpn = require( '@stdlib/stats/base/meanpn' ); * * 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 = meanpn.ndarray( N, x, 2, 1 ); +* var v = meanpn.ndarray( 4, x, 2, 1 ); * // returns 1.25 */ diff --git a/lib/node_modules/@stdlib/stats/base/meanpn/lib/meanpn.js b/lib/node_modules/@stdlib/stats/base/meanpn/lib/meanpn.js index 2c006a1539c1..8cc79011eb88 100644 --- a/lib/node_modules/@stdlib/stats/base/meanpn/lib/meanpn.js +++ b/lib/node_modules/@stdlib/stats/base/meanpn/lib/meanpn.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. @@ -20,8 +20,8 @@ // MODULES // -var gsumpw = require( '@stdlib/blas/ext/base/gsumpw' ); -var gapxsumpw = require( '@stdlib/blas/ext/base/gapxsumpw' ); +var stride2offset = require( '@stdlib/strided/base/stride2offset' ); +var ndarray = require( './ndarray.js' ); // MAIN // @@ -40,33 +40,17 @@ var gapxsumpw = require( '@stdlib/blas/ext/base/gapxsumpw' ); * * @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 = meanpn( N, x, 1 ); +* var v = meanpn( 3, x, 1 ); * // returns ~0.3333 */ -function meanpn( N, x, stride ) { - var mu; - var c; - - if ( N <= 0 ) { - return NaN; - } - if ( N === 1 || stride === 0 ) { - return x[ 0 ]; - } - // Compute an estimate for the meanpn: - mu = gsumpw( N, x, stride ) / N; - - // Compute an error term: - c = gapxsumpw( N, -mu, x, stride ) / N; - - return mu + c; +function meanpn( N, x, strideX ) { + return ndarray( N, x, strideX, stride2offset( N, strideX ) ); } diff --git a/lib/node_modules/@stdlib/stats/base/meanpn/lib/ndarray.js b/lib/node_modules/@stdlib/stats/base/meanpn/lib/ndarray.js index 0707013def95..160bad81fad9 100644 --- a/lib/node_modules/@stdlib/stats/base/meanpn/lib/ndarray.js +++ b/lib/node_modules/@stdlib/stats/base/meanpn/lib/ndarray.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. @@ -22,6 +22,8 @@ var gsumpw = require( '@stdlib/blas/ext/base/gsumpw' ).ndarray; var gapxsumpw = require( '@stdlib/blas/ext/base/gapxsumpw' ).ndarray; +var arraylike2object = require( '@stdlib/array/base/arraylike2object' ); +var accessors = require( './accessors.js' ); // MAIN // @@ -40,34 +42,37 @@ var gapxsumpw = require( '@stdlib/blas/ext/base/gapxsumpw' ).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 = meanpn( N, x, 2, 1 ); +* var v = meanpn( 4, x, 2, 1 ); * // returns 1.25 */ -function meanpn( N, x, stride, offset ) { +function meanpn( N, x, strideX, offsetX ) { var mu; var c; + var o; 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 ]; } // Compute an estimate for the meanpn: - mu = gsumpw( N, x, stride, offset ) / N; + mu = gsumpw( N, x, strideX, offsetX ) / N; // Compute an error term: - c = gapxsumpw( N, -mu, x, stride, offset ) / N; + c = gapxsumpw( N, -mu, x, strideX, offsetX ) / N; return mu + c; } diff --git a/lib/node_modules/@stdlib/stats/base/meanpn/test/test.meanpn.js b/lib/node_modules/@stdlib/stats/base/meanpn/test/test.meanpn.js index 0cf9cda9c019..06a49d1faadc 100644 --- a/lib/node_modules/@stdlib/stats/base/meanpn/test/test.meanpn.js +++ b/lib/node_modules/@stdlib/stats/base/meanpn/test/test.meanpn.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. @@ -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 meanpn = require( './../lib/meanpn.js' ); +var toAccessorArray = require( '@stdlib/array/base/to-accessor-array' ); +var meanpn = require( './../lib/main.js' ); // TESTS // @@ -40,6 +40,23 @@ tape( 'the function has an arity of 3', function test( t ) { 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 = meanpn( x.length, toAccessorArray( x ), 1 ); + t.strictEqual( v, 0.5, 'returns expected value' ); + + x = [ -4.0 ]; + + v = meanpn( x.length, toAccessorArray( x ), 1 ); + t.strictEqual( v, -4.0, 'returns expected value' ); + + t.end(); +}); + tape( 'the function calculates the arithmetic mean of a strided array', 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 = meanpn( N, x, 2 ); + v = meanpn( 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 = meanpn( 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 = meanpn( N, x, -2 ); + v = meanpn( 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 = meanpn( 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 = meanpn( 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 = meanpn( N, x1, 2 ); + v = meanpn( 4, x1, 2 ); t.strictEqual( v, 1.25, 'returns expected value' ); t.end(); diff --git a/lib/node_modules/@stdlib/stats/base/meanpn/test/test.ndarray.js b/lib/node_modules/@stdlib/stats/base/meanpn/test/test.ndarray.js index 2e07125465ca..162808617dd7 100644 --- a/lib/node_modules/@stdlib/stats/base/meanpn/test/test.ndarray.js +++ b/lib/node_modules/@stdlib/stats/base/meanpn/test/test.ndarray.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. @@ -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 meanpn = 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 = meanpn( x.length, toAccessorArray( x ), 1, 0 ); + t.strictEqual( v, 0.5, 'returns expected value' ); + + x = [ -4.0 ]; + + v = meanpn( 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 = meanpn( N, x, 2, 0 ); + v = meanpn( 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 = meanpn( 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 +157,28 @@ tape( 'the function supports a negative `stride` parameter', function test( t ) 2.0 ]; - N = floor( x.length / 2 ); - v = meanpn( N, x, -2, 6 ); + v = meanpn( 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 = meanpn( 4, toAccessorArray( x ), -2, 6 ); t.strictEqual( v, 1.25, 'returns expected value' ); t.end(); @@ -141,8 +196,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 = meanpn( 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 +222,29 @@ tape( 'the function supports an `offset` parameter', function test( t ) { 3.0, 4.0 // 3 ]; - N = floor( x.length / 2 ); - v = meanpn( N, x, 2, 1 ); + v = meanpn( 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 = meanpn( 4, toAccessorArray( x ), 2, 1 ); t.strictEqual( v, 1.25, 'returns expected value' ); t.end();