From 1e6a9de279f4cac6208df484f9390e4f56b2b047 Mon Sep 17 00:00:00 2001 From: Hecker165 Date: Wed, 19 Mar 2025 02:24:46 +0530 Subject: [PATCH 1/8] feat: add support for accessor arrays and refactor stats/base/stdevyc --- .../@stdlib/stats/base/stdevyc/README.md | 41 +++---- .../stats/base/stdevyc/benchmark/benchmark.js | 20 +-- .../stdevyc/benchmark/benchmark.ndarray.js | 16 +-- .../@stdlib/stats/base/stdevyc/docs/repl.txt | 29 ++--- .../stats/base/stdevyc/docs/types/index.d.ts | 20 +-- .../stats/base/stdevyc/docs/types/test.ts | 3 + .../stats/base/stdevyc/examples/index.js | 14 +-- .../stats/base/stdevyc/lib/accessors.js | 93 ++++++++++++++ .../@stdlib/stats/base/stdevyc/lib/index.js | 13 +- .../@stdlib/stats/base/stdevyc/lib/ndarray.js | 11 +- .../stats/base/stdevyc/test/test.ndarray.js | 114 ++++++++++++++++-- 11 files changed, 276 insertions(+), 98 deletions(-) create mode 100644 lib/node_modules/@stdlib/stats/base/stdevyc/lib/accessors.js diff --git a/lib/node_modules/@stdlib/stats/base/stdevyc/README.md b/lib/node_modules/@stdlib/stats/base/stdevyc/README.md index 2ddfad8004fa..87c3b60152a9 100644 --- a/lib/node_modules/@stdlib/stats/base/stdevyc/README.md +++ b/lib/node_modules/@stdlib/stats/base/stdevyc/README.md @@ -98,7 +98,7 @@ The use of the term `n-1` is commonly referred to as Bessel's correction. Note, var stdevyc = require( '@stdlib/stats/base/stdevyc' ); ``` -#### stdevyc( N, correction, x, stride ) +#### stdevyc( N, correction, x, strideX ) Computes the [standard deviation][standard-deviation] of a strided array `x` using a one-pass algorithm proposed by Youngs and Cramer. @@ -114,17 +114,15 @@ The function has the following parameters: - **N**: number of indexed elements. - **correction**: degrees of freedom adjustment. Setting this parameter to a value other than `0` has the effect of adjusting the divisor during the calculation of the [standard deviation][standard-deviation] according to `N-c` where `c` corresponds to the provided degrees of freedom adjustment. When computing the [standard deviation][standard-deviation] of a population, setting this parameter to `0` is the standard choice (i.e., the provided array contains data constituting an entire population). When computing the corrected sample [standard deviation][standard-deviation], setting this parameter to `1` is the standard choice (i.e., the provided array contains data sampled from a larger population; this is commonly referred to as Bessel's correction). - **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 [standard deviation][standard-deviation] 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 [standard deviation][standard-deviation] 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 = stdevyc( N, 1, x, 2 ); +var v = stdevyc( 4, 1, x, 2 ); // returns 2.5 ``` @@ -134,18 +132,16 @@ 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 = stdevyc( N, 1, x1, 2 ); +var v = stdevyc( 4, 1, x1, 2 ); // returns 2.5 ``` -#### stdevyc.ndarray( N, correction, x, stride, offset ) +#### stdevyc.ndarray( N, correction, x, strideX , offsetX ) Computes the [standard deviation][standard-deviation] of a strided array using a one-pass algorithm proposed by Youngs and Cramer and alternative indexing semantics. @@ -158,17 +154,15 @@ var v = stdevyc.ndarray( x.length, 1, x, 1, 0 ); 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 [standard deviation][standard-deviation] 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 [standard deviation][standard-deviation] 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 = stdevyc.ndarray( N, 1, x, 2, 1 ); +var v = stdevyc.ndarray( 4, 1, x, 2, 1 ); // returns 2.5 ``` @@ -181,6 +175,7 @@ var v = stdevyc.ndarray( N, 1, 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]). - If `N - c` is less than or equal to `0` (where `c` corresponds to the provided degrees of freedom adjustment), both functions return `NaN`. - Depending on the environment, the typed versions ([`dstdevyc`][@stdlib/stats/base/dstdevyc], [`sstdevyc`][@stdlib/stats/base/sstdevyc], etc.) are likely to be significantly more performant. @@ -195,18 +190,12 @@ var v = stdevyc.ndarray( N, 1, 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 stdevyc = require( '@stdlib/stats/base/stdevyc' ); -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 = stdevyc( x.length, 1, x, 1 ); @@ -257,6 +246,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 + [@youngs:1971a]: https://doi.org/10.1080/00401706.1971.10488826 diff --git a/lib/node_modules/@stdlib/stats/base/stdevyc/benchmark/benchmark.js b/lib/node_modules/@stdlib/stats/base/stdevyc/benchmark/benchmark.js index c04b66da0f6a..3236b2c26802 100644 --- a/lib/node_modules/@stdlib/stats/base/stdevyc/benchmark/benchmark.js +++ b/lib/node_modules/@stdlib/stats/base/stdevyc/benchmark/benchmark.js @@ -21,12 +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 stdevyc = require( './../lib/stdevyc.js' ); - +var stdevyc = require( './../lib/main.js' ); + + + // VARIABLES // + + var options = { + 'dtype': 'generic' + }; // FUNCTIONS // @@ -38,13 +44,7 @@ var stdevyc = require( './../lib/stdevyc.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/stdevyc/benchmark/benchmark.ndarray.js b/lib/node_modules/@stdlib/stats/base/stdevyc/benchmark/benchmark.ndarray.js index ec00dbc56160..4795b794c4f6 100644 --- a/lib/node_modules/@stdlib/stats/base/stdevyc/benchmark/benchmark.ndarray.js +++ b/lib/node_modules/@stdlib/stats/base/stdevyc/benchmark/benchmark.ndarray.js @@ -21,12 +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 stdevyc = require( './../lib/ndarray.js' ); +// VARIABLES // + +var options = { + 'dtype': 'generic' +}; + // FUNCTIONS // @@ -38,13 +44,7 @@ var stdevyc = 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/stdevyc/docs/repl.txt b/lib/node_modules/@stdlib/stats/base/stdevyc/docs/repl.txt index df1c1fc4b8ed..8730fcb9237a 100644 --- a/lib/node_modules/@stdlib/stats/base/stdevyc/docs/repl.txt +++ b/lib/node_modules/@stdlib/stats/base/stdevyc/docs/repl.txt @@ -1,9 +1,9 @@ -{{alias}}( N, correction, x, stride ) +{{alias}}( N, correction, x, strideX ) Computes the standard deviation of a strided array using a one-pass algorithm proposed by Youngs and Cramer. - The `N` and `stride` parameters determine which elements in `x` are accessed + 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 @@ -31,8 +31,8 @@ x: Array|TypedArray Input array. - stride: integer - Index increment. + strideX: integer + Stride length. Returns ------- @@ -46,22 +46,18 @@ > {{alias}}( x.length, 1, x, 1 ) ~2.0817 - // 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, 1, x, stride ) + > {{alias}}( 3, 1, x, 2 ) ~2.0817 // 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, 1, x1, stride ) + > {{alias}}( 3, 1, x1, 2 ) ~2.0817 -{{alias}}.ndarray( N, correction, x, stride, offset ) +{{alias}}.ndarray( N, correction, x, strideX, offsetX ) Computes the standard deviation of a strided array using a one-pass algorithm proposed by Youngs and Cramer and alternative indexing semantics. @@ -89,10 +85,10 @@ x: Array|TypedArray Input array. - stride: integer - Index increment. + strideX: integer + Stride length. - offset: integer + offsetX: integer Starting index. Returns @@ -109,8 +105,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, 1, x, 2, 1 ) + > {{alias}}.ndarray( 3, 1, x, 2, 1 ) ~2.0817 See Also diff --git a/lib/node_modules/@stdlib/stats/base/stdevyc/docs/types/index.d.ts b/lib/node_modules/@stdlib/stats/base/stdevyc/docs/types/index.d.ts index 98e70a8ef7e7..f2edfa6320a7 100644 --- a/lib/node_modules/@stdlib/stats/base/stdevyc/docs/types/index.d.ts +++ b/lib/node_modules/@stdlib/stats/base/stdevyc/docs/types/index.d.ts @@ -20,8 +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 `stdevyc`. */ @@ -32,7 +36,7 @@ interface Routine { * @param N - number of indexed elements * @param correction - degrees of freedom adjustment * @param x - input array - * @param stride - stride length + * @param strideX - stride length * @returns standard deviation * * @example @@ -41,7 +45,7 @@ interface Routine { * var v = stdevyc( x.length, 1, x, 1 ); * // returns ~2.0817 */ - ( N: number, correction: number, x: NumericArray, stride: number ): number; + ( N: number, correction: number, x: InputArray, strideX: number ): number; /** * Computes the standard deviation of a strided array using a one-pass algorithm proposed by Youngs and Cramer and alternative indexing semantics. @@ -49,8 +53,8 @@ interface Routine { * @param N - number of indexed elements * @param correction - degrees of freedom adjustment * @param x - input array - * @param stride - stride length - * @param offset - starting index + * @param strideX - stride length + * @param offsetX - starting index * @returns standard deviation * * @example @@ -59,7 +63,7 @@ interface Routine { * var v = stdevyc.ndarray( x.length, 1, x, 1, 0 ); * // returns ~2.0817 */ - ndarray( N: number, correction: number, x: NumericArray, stride: number, offset: number ): number; + ndarray( N: number, correction: number, x: InputArray, strideX: number, offsetX: number ): number; } /** @@ -68,7 +72,7 @@ interface Routine { * @param N - number of indexed elements * @param correction - degrees of freedom adjustment * @param x - input array -* @param stride - stride length +* @param strideX - stride length * @returns standard deviation * * @example diff --git a/lib/node_modules/@stdlib/stats/base/stdevyc/docs/types/test.ts b/lib/node_modules/@stdlib/stats/base/stdevyc/docs/types/test.ts index 8e93e0a98b06..b1720123c60c 100644 --- a/lib/node_modules/@stdlib/stats/base/stdevyc/docs/types/test.ts +++ b/lib/node_modules/@stdlib/stats/base/stdevyc/docs/types/test.ts @@ -16,6 +16,7 @@ * limitations under the License. */ +import AccessorArray = require( '@stdlib/array/base/accessor' ); import stdevyc = require( './index' ); @@ -26,6 +27,7 @@ import stdevyc = require( './index' ); const x = new Float64Array( 10 ); stdevyc( x.length, 1, x, 1 ); // $ExpectType number + stdevyc( x.length, 1, new AccessorArray( x ), 1 ); // $ExpectType number } // The compiler throws an error if the function is provided a first argument which is not a number... @@ -101,6 +103,7 @@ import stdevyc = require( './index' ); const x = new Float64Array( 10 ); stdevyc.ndarray( x.length, 1, x, 1, 0 ); // $ExpectType number + stdevyc.ndarray( x.length, 1, 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/stdevyc/examples/index.js b/lib/node_modules/@stdlib/stats/base/stdevyc/examples/index.js index 62393d3f8151..b3c6ceb20938 100644 --- a/lib/node_modules/@stdlib/stats/base/stdevyc/examples/index.js +++ b/lib/node_modules/@stdlib/stats/base/stdevyc/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 stdevyc = 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 = stdevyc( x.length, 1, x, 1 ); diff --git a/lib/node_modules/@stdlib/stats/base/stdevyc/lib/accessors.js b/lib/node_modules/@stdlib/stats/base/stdevyc/lib/accessors.js new file mode 100644 index 000000000000..4eb886e8a6af --- /dev/null +++ b/lib/node_modules/@stdlib/stats/base/stdevyc/lib/accessors.js @@ -0,0 +1,93 @@ +/** +* @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 isnan = require( '@stdlib/math/base/assert/is-nan' ); +var sqrt = require( '@stdlib/math/base/special/sqrt' ); + +// MAIN // + +/** +* Computes the standard deviation of a strided array using Youngs and Cramer's method. +* +* @private +* @param {PositiveInteger} N - number of indexed elements +* @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} standard deviation +* +* @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 = stdevyc( 4, arraylike2object( x ), 2, 1 ); +* // returns +*/ +function stdevyc( N, x, strideX, offsetX ) { + var xbuf; + var get; + var mean; + var M2; + var delta; + var count; + var ix; + var v; + var i; + + // Cache reference to array data: + xbuf = x.data; + + // Cache a reference to the element accessor: + get = x.accessors[ 0 ]; + + if ( N < 2 ) { + return NaN; + } + + // Initialize variables: + mean = 0.0; + M2 = 0.0; + count = 0; + ix = offsetX; + + for ( i = 0; i < N; i++ ) { + v = get( xbuf, ix ); + if ( isnan( v ) ) { + return NaN; + } + count += 1; + delta = v - mean; + mean += delta / count; + M2 += delta * ( v - mean ); + ix += strideX; + } + + return sqrt( M2 / ( count - 1 ) ); +} + +// EXPORTS // + +module.exports = stdevyc; diff --git a/lib/node_modules/@stdlib/stats/base/stdevyc/lib/index.js b/lib/node_modules/@stdlib/stats/base/stdevyc/lib/index.js index 4466e430d335..08473507fe54 100644 --- a/lib/node_modules/@stdlib/stats/base/stdevyc/lib/index.js +++ b/lib/node_modules/@stdlib/stats/base/stdevyc/lib/index.js @@ -36,16 +36,21 @@ * var stdevyc = require( '@stdlib/stats/base/stdevyc' ); * * 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 = stdevyc.ndarray( N, 1, x, 2, 1 ); +* var v = stdevyc.ndarray( 4, 1, x, 2, 1 ); * // returns 2.5 */ // MODULES // -var main = 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 // diff --git a/lib/node_modules/@stdlib/stats/base/stdevyc/lib/ndarray.js b/lib/node_modules/@stdlib/stats/base/stdevyc/lib/ndarray.js index f69a3652a931..8eb97c015fe4 100644 --- a/lib/node_modules/@stdlib/stats/base/stdevyc/lib/ndarray.js +++ b/lib/node_modules/@stdlib/stats/base/stdevyc/lib/ndarray.js @@ -36,21 +36,20 @@ var sqrt = require( '@stdlib/math/base/special/sqrt' ); * @param {PositiveInteger} N - number of indexed elements * @param {number} correction - degrees of freedom adjustment * @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} standard deviation * * @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 = stdevyc( N, 1, x, 2, 1 ); +* var v = stdevyc( 4, 1, x, 2, 1 ); * // returns 2.5 */ -function stdevyc( N, correction, x, stride, offset ) { - return sqrt( varianceyc( N, correction, x, stride, offset ) ); +function stdevyc( N, correction, x, strideX, offsetX ) { + return sqrt( varianceyc( N, correction, x, strideX, offsetX ) ); } diff --git a/lib/node_modules/@stdlib/stats/base/stdevyc/test/test.ndarray.js b/lib/node_modules/@stdlib/stats/base/stdevyc/test/test.ndarray.js index 7d8a571e5c7f..3b4309277fdd 100644 --- a/lib/node_modules/@stdlib/stats/base/stdevyc/test/test.ndarray.js +++ b/lib/node_modules/@stdlib/stats/base/stdevyc/test/test.ndarray.js @@ -21,9 +21,9 @@ // MODULES // var tape = require( 'tape' ); -var floor = require( '@stdlib/math/base/special/floor' ); var sqrt = require( '@stdlib/math/base/special/sqrt' ); var isnan = require( '@stdlib/math/base/assert/is-nan' ); +var toAccessorArray = require( '@stdlib/array/base/to-accessor-array' ); var stdevyc = require( './../lib/ndarray.js' ); @@ -59,6 +59,30 @@ tape( 'the function calculates the population standard deviation of a strided ar t.end(); }); +tape( 'the function calculates the standard deviation 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 = stdevyc( x.length, 0, toAccessorArray( x ), 1, 0 ); + t.strictEqual( v, sqrt( 53.5 / x.length ), 'returns expected value' ); + + x = [ 1.0, -2.0, -4.0, 5.0, 0.0, 3.0 ]; + v = stdevyc( x.length, 1, toAccessorArray( x ), 1, 0 ); + t.strictEqual( v, sqrt( 53.5 / (x.length - 1) ), 'returns expected value' ); + + x = [ -4.0, -4.0 ]; + v = stdevyc( x.length, 0, toAccessorArray( x ), 1, 0 ); + t.strictEqual( v, 0.0, 'returns expected value' ); + + x = [ NaN, 4.0 ]; + v = stdevyc( x.length, 0, toAccessorArray( x ), 1, 0 ); + t.strictEqual( isnan( v ), true, 'returns expected value' ); + + t.end(); +}); + + tape( 'the function calculates the sample standard deviation of a strided array', function test( t ) { var x; var v; @@ -121,7 +145,6 @@ tape( 'if provided a `correction` parameter yielding `N-correction` less than or }); tape( 'the function supports a `stride` parameter', function test( t ) { - var N; var x; var v; @@ -136,15 +159,34 @@ tape( 'the function supports a `stride` parameter', function test( t ) { 2.0 ]; - N = floor( x.length / 2 ); - v = stdevyc( N, 1, x, 2, 0 ); + v = stdevyc( 4, 1, x, 2, 0 ); + + t.strictEqual( v, 2.5, 'returns expected value' ); + t.end(); +}); + +tape( 'the function supports a `stride` parameter', 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 = stdevyc( 4, 1, toAccessorArray( x ), 2, 0 ); t.strictEqual( v, 2.5, 'returns expected value' ); t.end(); }); tape( 'the function supports a negative `stride` parameter', function test( t ) { - var N; var x; var v; @@ -159,13 +201,34 @@ tape( 'the function supports a negative `stride` parameter', function test( t ) 2.0 ]; - N = floor( x.length / 2 ); - v = stdevyc( N, 1, x, -2, 6 ); + v = stdevyc( 4, 1, x, -2, 6 ); t.strictEqual( v, 2.5, 'returns expected value' ); t.end(); }); +tape( 'the function supports a negative `stride` parameter', 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 = stdevyc( 4, 1, toAccessorArray( x ), -2, 6 ); + + t.strictEqual( v, 2.5, 'returns expected value' ); + t.end(); +}); + + tape( 'if provided a `stride` parameter equal to `0`, the function returns `0`', function test( t ) { var x; var v; @@ -178,8 +241,19 @@ tape( 'if provided a `stride` parameter equal to `0`, the function returns `0`', t.end(); }); +tape( 'if provided a `stride` parameter equal to `0`, the function returns `0`', function test( t ) { + var x; + var v; + + x = [ 1.0, -2.0, -4.0, 5.0, 3.0 ]; + + v = stdevyc( x.length, 1, toAccessorArray( x ), 0, 0 ); + t.strictEqual( v, 0.0, 'returns expected value' ); + + t.end(); +}); + tape( 'the function supports an `offset` parameter', function test( t ) { - var N; var x; var v; @@ -193,10 +267,30 @@ tape( 'the function supports an `offset` parameter', function test( t ) { 3.0, 4.0 // 3 ]; - N = floor( x.length / 2 ); - v = stdevyc( N, 1, x, 2, 1 ); + v = stdevyc( 4, 1, x, 2, 1 ); t.strictEqual( v, 2.5, '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 = stdevyc( 4, 1, toAccessorArray( x ), 2, 1 ); + t.strictEqual( v, 2.5, 'returns expected value' ); + + t.end(); +}); \ No newline at end of file From 0d23ce3a0bdbb37532d0ba963e73fb98d60c8017 Mon Sep 17 00:00:00 2001 From: Hecker165 Date: Wed, 19 Mar 2025 02:41:07 +0530 Subject: [PATCH 2/8] fixed minor errors --- lib/node_modules/@stdlib/stats/base/stdevyc/README.md | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/lib/node_modules/@stdlib/stats/base/stdevyc/README.md b/lib/node_modules/@stdlib/stats/base/stdevyc/README.md index 87c3b60152a9..27851256a4c0 100644 --- a/lib/node_modules/@stdlib/stats/base/stdevyc/README.md +++ b/lib/node_modules/@stdlib/stats/base/stdevyc/README.md @@ -119,7 +119,6 @@ The function has the following parameters: The `N` and stride parameters determine which elements in the strided array are accessed at runtime. For example, to compute the [standard deviation][standard-deviation] of every other element in `x`, ```javascript - var x = [ 1.0, 2.0, 2.0, -7.0, -2.0, 3.0, 4.0, 2.0 ]; var v = stdevyc( 4, 1, x, 2 ); @@ -136,7 +135,6 @@ var Float64Array = require( '@stdlib/array/float64' ); 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 v = stdevyc( 4, 1, x1, 2 ); // returns 2.5 ``` @@ -159,7 +157,6 @@ The function has the following additional parameters: 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 [standard deviation][standard-deviation] for every other element in the strided array starting from the second element ```javascript - var x = [ 2.0, 1.0, 2.0, -2.0, -2.0, 2.0, 3.0, 4.0 ]; var v = stdevyc.ndarray( 4, 1, x, 2, 1 ); @@ -190,12 +187,12 @@ var v = stdevyc.ndarray( 4, 1, x, 2, 1 ); ```javascript -var discreteUniform = require( '@stdlib/random/array/discrete-uniform'); +var discreteUniform = require( '@stdlib/random/array/discrete-uniform' ); var stdevyc = require( '@stdlib/stats/base/stdevyc' ); var x = discreteUniform( 10, -50, 50, { - 'dtype': 'float64' - }); + 'dtype': 'float64' +}); console.log( x ); var v = stdevyc( x.length, 1, x, 1 ); From 3d83101339c29d07f434feed83d26150b4919631 Mon Sep 17 00:00:00 2001 From: Hecker165 Date: Wed, 19 Mar 2025 22:18:01 +0530 Subject: [PATCH 3/8] feat: add support for accessor arrays and refactor stats/base/stdevyc --- .../stats/base/stdevyc/docs/types/index.d.ts | 10 +++++----- .../@stdlib/stats/base/stdevyc/lib/accessors.js | 16 +++++++++------- 2 files changed, 14 insertions(+), 12 deletions(-) diff --git a/lib/node_modules/@stdlib/stats/base/stdevyc/docs/types/index.d.ts b/lib/node_modules/@stdlib/stats/base/stdevyc/docs/types/index.d.ts index f2edfa6320a7..f349fef42ec0 100644 --- a/lib/node_modules/@stdlib/stats/base/stdevyc/docs/types/index.d.ts +++ b/lib/node_modules/@stdlib/stats/base/stdevyc/docs/types/index.d.ts @@ -21,11 +21,11 @@ /// import { NumericArray, Collection, AccessorArrayLike } from '@stdlib/types/array'; - - /** - * Input array. - */ - type InputArray = NumericArray | Collection | AccessorArrayLike; + +/** +* Input array. +*/ +type InputArray = NumericArray | Collection | AccessorArrayLike; /** * Interface describing `stdevyc`. */ diff --git a/lib/node_modules/@stdlib/stats/base/stdevyc/lib/accessors.js b/lib/node_modules/@stdlib/stats/base/stdevyc/lib/accessors.js index 4eb886e8a6af..c6edf4fda272 100644 --- a/lib/node_modules/@stdlib/stats/base/stdevyc/lib/accessors.js +++ b/lib/node_modules/@stdlib/stats/base/stdevyc/lib/accessors.js @@ -23,6 +23,7 @@ var isnan = require( '@stdlib/math/base/assert/is-nan' ); var sqrt = require( '@stdlib/math/base/special/sqrt' ); + // MAIN // /** @@ -43,16 +44,16 @@ var sqrt = require( '@stdlib/math/base/special/sqrt' ); * * var x = toAccessorArray( [ 2.0, 1.0, 2.0, -2.0, -2.0, 2.0, 3.0, 4.0 ] ); * -* var v = stdevyc( 4, arraylike2object( x ), 2, 1 ); +* var v = stdevyc( 4, 1, arraylike2object( x ), 2, 1 ); * // returns */ -function stdevyc( N, x, strideX, offsetX ) { +function stdevyc( N, correction, x, strideX, offsetX ) { + var delta; + var count; var xbuf; - var get; var mean; + var get; var M2; - var delta; - var count; var ix; var v; var i; @@ -63,7 +64,7 @@ function stdevyc( N, x, strideX, offsetX ) { // Cache a reference to the element accessor: get = x.accessors[ 0 ]; - if ( N < 2 ) { + if ( N <= correction ) { // Prevent division by zero return NaN; } @@ -85,9 +86,10 @@ function stdevyc( N, x, strideX, offsetX ) { ix += strideX; } - return sqrt( M2 / ( count - 1 ) ); + return sqrt( M2 / ( count - correction ) ); } + // EXPORTS // module.exports = stdevyc; From 142c93f7730f968c934535c43404d647ac79c5fb Mon Sep 17 00:00:00 2001 From: Hecker165 Date: Thu, 20 Mar 2025 02:13:15 +0530 Subject: [PATCH 4/8] feat: add support for accessor arrays and refactor stats/base/stdevyc --- .../stats/base/stdevyc/lib/accessors.js | 67 ++++++++++--------- .../@stdlib/stats/base/stdevyc/lib/index.js | 3 +- .../@stdlib/stats/base/stdevyc/lib/main.js | 31 +++++++-- .../@stdlib/stats/base/stdevyc/lib/ndarray.js | 3 +- .../@stdlib/stats/base/stdevyc/lib/stdevyc.js | 26 +++++-- 5 files changed, 83 insertions(+), 47 deletions(-) diff --git a/lib/node_modules/@stdlib/stats/base/stdevyc/lib/accessors.js b/lib/node_modules/@stdlib/stats/base/stdevyc/lib/accessors.js index c6edf4fda272..bb69f278758a 100644 --- a/lib/node_modules/@stdlib/stats/base/stdevyc/lib/accessors.js +++ b/lib/node_modules/@stdlib/stats/base/stdevyc/lib/accessors.js @@ -31,6 +31,7 @@ var sqrt = require( '@stdlib/math/base/special/sqrt' ); * * @private * @param {PositiveInteger} N - number of indexed elements +* @param {NonNegativeInteger} correction - degrees of freedom adjustment (0 for population, 1 for sample) * @param {Object} x - input array object * @param {Collection} x.data - input array data * @param {Array} x.accessors - array element accessors @@ -48,45 +49,45 @@ var sqrt = require( '@stdlib/math/base/special/sqrt' ); * // returns */ function stdevyc( N, correction, x, strideX, offsetX ) { - var delta; - var count; - var xbuf; - var mean; - var get; - var M2; - var ix; - var v; - var i; + var delta; + var count; + var xbuf; + var mean; + var get; + var M2; + var ix; + var v; + var i; - // Cache reference to array data: - xbuf = x.data; + // Cache reference to array data: + xbuf = x.data; - // Cache a reference to the element accessor: - get = x.accessors[ 0 ]; + // Cache a reference to the element accessor: + get = x.accessors[ 0 ]; - if ( N <= correction ) { // Prevent division by zero - return NaN; - } + if ( N <= correction ) { // Prevent division by zero + return NaN; + } - // Initialize variables: - mean = 0.0; - M2 = 0.0; - count = 0; - ix = offsetX; + // Initialize variables: + mean = 0.0; + M2 = 0.0; + count = 0; + ix = offsetX; - for ( i = 0; i < N; i++ ) { - v = get( xbuf, ix ); - if ( isnan( v ) ) { - return NaN; - } - count += 1; - delta = v - mean; - mean += delta / count; - M2 += delta * ( v - mean ); - ix += strideX; - } + for ( i = 0; i < N; i++ ) { + v = get( xbuf, ix ); + if ( isnan( v ) ) { + return NaN; + } + count += 1; + delta = v - mean; + mean += delta / count; + M2 += delta * ( v - mean ); + ix += strideX; + } - return sqrt( M2 / ( count - correction ) ); + return sqrt( M2 / ( count - correction ) ); } diff --git a/lib/node_modules/@stdlib/stats/base/stdevyc/lib/index.js b/lib/node_modules/@stdlib/stats/base/stdevyc/lib/index.js index 08473507fe54..68637465a448 100644 --- a/lib/node_modules/@stdlib/stats/base/stdevyc/lib/index.js +++ b/lib/node_modules/@stdlib/stats/base/stdevyc/lib/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. @@ -32,7 +32,6 @@ * // returns ~2.0817 * * @example -* var floor = require( '@stdlib/math/base/special/floor' ); * var stdevyc = require( '@stdlib/stats/base/stdevyc' ); * * var x = [ 2.0, 1.0, 2.0, -2.0, -2.0, 2.0, 3.0, 4.0 ]; diff --git a/lib/node_modules/@stdlib/stats/base/stdevyc/lib/main.js b/lib/node_modules/@stdlib/stats/base/stdevyc/lib/main.js index 3df6b62d7501..08fb0eb64a8b 100644 --- a/lib/node_modules/@stdlib/stats/base/stdevyc/lib/main.js +++ b/lib/node_modules/@stdlib/stats/base/stdevyc/lib/main.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,14 +20,37 @@ // MODULES // -var setReadOnly = require( '@stdlib/utils/define-nonenumerable-read-only-property' ); -var stdevyc = require( './stdevyc.js' ); +var stride2offset = require( '@stdlib/strided/base/stride2offset' ); var ndarray = require( './ndarray.js' ); // MAIN // -setReadOnly( stdevyc, 'ndarray', ndarray ); +/** + * Computes the standard deviation of a strided array using a one-pass algorithm proposed by Youngs and Cramer. +* +* ## References +* +* - Youngs, Edward A., and Elliot M. Cramer. 1971. "Some Results Relevant to Choice of Sum and Sum-of-Product Algorithms." _Technometrics_ 13 (3): 657–65. doi:[10.1080/00401706.1971.10488826](https://doi.org/10.1080/00401706.1971.10488826). + + * @param {PositiveInteger} N - number of indexed elements + * @param {number} correction - degrees of freedom adjustment + * @param {NumericArray} x - input array + * @param {integer} strideX - stride length + * @returns {number} standard deviation + * + * @example + * var x = [ 1.0, -2.0, 2.0 ]; + * + * var v = stdevyc( x.length, 1, x, 1 ); + * // returns ~2.0817 + */ +function stdevyc( N, correction, x, strideX ) { + return ndarray( N, correction, x, strideX, stride2offset( N, strideX ) ); +} + +// EXPORTS // +module.exports = stdevyc; // EXPORTS // diff --git a/lib/node_modules/@stdlib/stats/base/stdevyc/lib/ndarray.js b/lib/node_modules/@stdlib/stats/base/stdevyc/lib/ndarray.js index 8eb97c015fe4..b29bddbc4f80 100644 --- a/lib/node_modules/@stdlib/stats/base/stdevyc/lib/ndarray.js +++ b/lib/node_modules/@stdlib/stats/base/stdevyc/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. @@ -53,6 +53,7 @@ function stdevyc( N, correction, x, strideX, offsetX ) { } + // EXPORTS // module.exports = stdevyc; diff --git a/lib/node_modules/@stdlib/stats/base/stdevyc/lib/stdevyc.js b/lib/node_modules/@stdlib/stats/base/stdevyc/lib/stdevyc.js index 3030bafe696a..3c04a9ce8b86 100644 --- a/lib/node_modules/@stdlib/stats/base/stdevyc/lib/stdevyc.js +++ b/lib/node_modules/@stdlib/stats/base/stdevyc/lib/stdevyc.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,9 +20,11 @@ // MODULES // -var varianceyc = require( '@stdlib/stats/base/varianceyc' ); +var isnan = require( '@stdlib/math/base/assert/is-nan' ); +var arraylike2object = require( '@stdlib/array/base/arraylike2object' ); +var accessors = require( './accessors.js' ); var sqrt = require( '@stdlib/math/base/special/sqrt' ); - +var varianceyc = require( '@stdlib/stats/base/varianceyc' ).ndarray; // MAIN // @@ -36,17 +38,27 @@ var sqrt = require( '@stdlib/math/base/special/sqrt' ); * @param {PositiveInteger} N - number of indexed elements * @param {number} correction - degrees of freedom adjustment * @param {NumericArray} x - input array -* @param {integer} stride - stride length +* @param {integer} strideX - stride length +* @param {NonNegativeInteger} offsetX - starting index * @returns {number} standard deviation * * @example * var x = [ 1.0, -2.0, 2.0 ]; * -* var v = stdevyc( x.length, 1, x, 1 ); +* var v = stdevyc( x.length, 1, x, 1, 0 ); * // returns ~2.0817 */ -function stdevyc( N, correction, x, stride ) { - return sqrt( varianceyc( N, correction, x, stride ) ); +function stdevyc( N, correction, x, strideX, offsetX ) { + var o; + + if ( N <= 0 ) { + return NaN; + } + o = arraylike2object( x ); + if ( o.accessorProtocol ) { + return accessors( N, correction, o, strideX, offsetX ); + } + return sqrt( varianceyc( N, correction, x, strideX, offsetX ) ); } From 41058a4c44f77cd07d52f159b11cedec0bb2616d Mon Sep 17 00:00:00 2001 From: Hecker165 Date: Thu, 20 Mar 2025 02:19:13 +0530 Subject: [PATCH 5/8] feat: add support for accessor arrays and refactor stats/base/stdevyc --- .../@stdlib/stats/base/stdevyc/lib/ndarray.js | 25 +++++++++++++------ 1 file changed, 17 insertions(+), 8 deletions(-) diff --git a/lib/node_modules/@stdlib/stats/base/stdevyc/lib/ndarray.js b/lib/node_modules/@stdlib/stats/base/stdevyc/lib/ndarray.js index b29bddbc4f80..43adc6521f53 100644 --- a/lib/node_modules/@stdlib/stats/base/stdevyc/lib/ndarray.js +++ b/lib/node_modules/@stdlib/stats/base/stdevyc/lib/ndarray.js @@ -20,8 +20,11 @@ // MODULES // -var varianceyc = require( '@stdlib/stats/base/varianceyc' ).ndarray; +var isnan = require( '@stdlib/math/base/assert/is-nan' ); +var arraylike2object = require( '@stdlib/array/base/arraylike2object' ); +var accessors = require( './accessors.js' ); var sqrt = require( '@stdlib/math/base/special/sqrt' ); +var varianceyc = require( '@stdlib/stats/base/varianceyc' ).ndarray; // MAIN // @@ -41,19 +44,25 @@ var sqrt = require( '@stdlib/math/base/special/sqrt' ); * @returns {number} standard deviation * * @example -* var floor = require( '@stdlib/math/base/special/floor' ); +* var x = [ 1.0, -2.0, 2.0 ]; * -* var x = [ 2.0, 1.0, 2.0, -2.0, -2.0, 2.0, 3.0, 4.0 ]; -* -* var v = stdevyc( 4, 1, x, 2, 1 ); -* // returns 2.5 +* var v = stdevyc( x.length, 1, x, 1, 0 ); +* // returns ~2.0817 */ function stdevyc( N, correction, x, strideX, offsetX ) { - return sqrt( varianceyc( N, correction, x, strideX, offsetX ) ); + var o; + + if ( N <= 0 ) { + return NaN; + } + o = arraylike2object( x ); + if ( o.accessorProtocol ) { + return accessors( N, correction, o, strideX, offsetX ); + } + return sqrt( varianceyc( N, correction, x, strideX, offsetX ) ); } - // EXPORTS // module.exports = stdevyc; From 0cac3f8c0d4c0ac1a120f99e583447d3a6b08215 Mon Sep 17 00:00:00 2001 From: Hecker165 Date: Thu, 20 Mar 2025 02:33:39 +0530 Subject: [PATCH 6/8] feat: add support for accessor arrays and refactor stats/base/stdevyc --- .../stats/base/stdevyc/benchmark/benchmark.js | 6 +- .../stdevyc/benchmark/benchmark.ndarray.js | 2 +- .../@stdlib/stats/base/stdevyc/lib/index.js | 6 +- .../@stdlib/stats/base/stdevyc/lib/main.js | 2 +- .../@stdlib/stats/base/stdevyc/lib/ndarray.js | 20 ++--- .../@stdlib/stats/base/stdevyc/lib/stdevyc.js | 20 ++--- .../stats/base/stdevyc/test/test.ndarray.js | 84 +++++++++++++------ 7 files changed, 88 insertions(+), 52 deletions(-) diff --git a/lib/node_modules/@stdlib/stats/base/stdevyc/benchmark/benchmark.js b/lib/node_modules/@stdlib/stats/base/stdevyc/benchmark/benchmark.js index 3236b2c26802..05004de1cc35 100644 --- a/lib/node_modules/@stdlib/stats/base/stdevyc/benchmark/benchmark.js +++ b/lib/node_modules/@stdlib/stats/base/stdevyc/benchmark/benchmark.js @@ -26,10 +26,10 @@ var isnan = require( '@stdlib/math/base/assert/is-nan' ); var pow = require( '@stdlib/math/base/special/pow' ); var pkg = require( './../package.json' ).name; var stdevyc = require( './../lib/main.js' ); - - + + // VARIABLES // - + var options = { 'dtype': 'generic' }; diff --git a/lib/node_modules/@stdlib/stats/base/stdevyc/benchmark/benchmark.ndarray.js b/lib/node_modules/@stdlib/stats/base/stdevyc/benchmark/benchmark.ndarray.js index 4795b794c4f6..05544364ee88 100644 --- a/lib/node_modules/@stdlib/stats/base/stdevyc/benchmark/benchmark.ndarray.js +++ b/lib/node_modules/@stdlib/stats/base/stdevyc/benchmark/benchmark.ndarray.js @@ -28,7 +28,7 @@ var pkg = require( './../package.json' ).name; var stdevyc = require( './../lib/ndarray.js' ); // VARIABLES // - + var options = { 'dtype': 'generic' }; diff --git a/lib/node_modules/@stdlib/stats/base/stdevyc/lib/index.js b/lib/node_modules/@stdlib/stats/base/stdevyc/lib/index.js index 68637465a448..1d655aef6c50 100644 --- a/lib/node_modules/@stdlib/stats/base/stdevyc/lib/index.js +++ b/lib/node_modules/@stdlib/stats/base/stdevyc/lib/index.js @@ -45,10 +45,10 @@ 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 // diff --git a/lib/node_modules/@stdlib/stats/base/stdevyc/lib/main.js b/lib/node_modules/@stdlib/stats/base/stdevyc/lib/main.js index 08fb0eb64a8b..ca28f6ba486e 100644 --- a/lib/node_modules/@stdlib/stats/base/stdevyc/lib/main.js +++ b/lib/node_modules/@stdlib/stats/base/stdevyc/lib/main.js @@ -46,7 +46,7 @@ var ndarray = require( './ndarray.js' ); * // returns ~2.0817 */ function stdevyc( N, correction, x, strideX ) { - return ndarray( N, correction, x, strideX, stride2offset( N, strideX ) ); + return ndarray( N, correction, x, strideX, stride2offset( N, strideX ) ); } // EXPORTS // diff --git a/lib/node_modules/@stdlib/stats/base/stdevyc/lib/ndarray.js b/lib/node_modules/@stdlib/stats/base/stdevyc/lib/ndarray.js index 43adc6521f53..a21a5f1e7310 100644 --- a/lib/node_modules/@stdlib/stats/base/stdevyc/lib/ndarray.js +++ b/lib/node_modules/@stdlib/stats/base/stdevyc/lib/ndarray.js @@ -50,16 +50,16 @@ var varianceyc = require( '@stdlib/stats/base/varianceyc' ).ndarray; * // returns ~2.0817 */ function stdevyc( N, correction, x, strideX, offsetX ) { - var o; - - if ( N <= 0 ) { - return NaN; - } - o = arraylike2object( x ); - if ( o.accessorProtocol ) { - return accessors( N, correction, o, strideX, offsetX ); - } - return sqrt( varianceyc( N, correction, x, strideX, offsetX ) ); + var o; + + if ( N <= 0 ) { + return NaN; + } + o = arraylike2object( x ); + if ( o.accessorProtocol ) { + return accessors( N, correction, o, strideX, offsetX ); + } + return sqrt( varianceyc( N, correction, x, strideX, offsetX ) ); } diff --git a/lib/node_modules/@stdlib/stats/base/stdevyc/lib/stdevyc.js b/lib/node_modules/@stdlib/stats/base/stdevyc/lib/stdevyc.js index 3c04a9ce8b86..aba96422fabd 100644 --- a/lib/node_modules/@stdlib/stats/base/stdevyc/lib/stdevyc.js +++ b/lib/node_modules/@stdlib/stats/base/stdevyc/lib/stdevyc.js @@ -49,16 +49,16 @@ var varianceyc = require( '@stdlib/stats/base/varianceyc' ).ndarray; * // returns ~2.0817 */ function stdevyc( N, correction, x, strideX, offsetX ) { - var o; - - if ( N <= 0 ) { - return NaN; - } - o = arraylike2object( x ); - if ( o.accessorProtocol ) { - return accessors( N, correction, o, strideX, offsetX ); - } - return sqrt( varianceyc( N, correction, x, strideX, offsetX ) ); + var o; + + if ( N <= 0 ) { + return NaN; + } + o = arraylike2object( x ); + if ( o.accessorProtocol ) { + return accessors( N, correction, o, strideX, offsetX ); + } + return sqrt( varianceyc( N, correction, x, strideX, offsetX ) ); } diff --git a/lib/node_modules/@stdlib/stats/base/stdevyc/test/test.ndarray.js b/lib/node_modules/@stdlib/stats/base/stdevyc/test/test.ndarray.js index 3b4309277fdd..db276805db6d 100644 --- a/lib/node_modules/@stdlib/stats/base/stdevyc/test/test.ndarray.js +++ b/lib/node_modules/@stdlib/stats/base/stdevyc/test/test.ndarray.js @@ -59,30 +59,25 @@ tape( 'the function calculates the population standard deviation of a strided ar t.end(); }); -tape( 'the function calculates the standard deviation 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 = stdevyc( x.length, 0, toAccessorArray( x ), 1, 0 ); - t.strictEqual( v, sqrt( 53.5 / x.length ), 'returns expected value' ); +tape( 'the function calculates the population standard deviation of a strided array (accessor)', function test( t ) { + var x; + var v; - x = [ 1.0, -2.0, -4.0, 5.0, 0.0, 3.0 ]; - v = stdevyc( x.length, 1, toAccessorArray( x ), 1, 0 ); - t.strictEqual( v, sqrt( 53.5 / (x.length - 1) ), 'returns expected value' ); + x = toAccessorArray( [ 1.0, -2.0, -4.0, 5.0, 0.0, 3.0 ] ); + v = stdevyc( x.length, 0, x, 1, 0 ); + t.strictEqual( v, sqrt( 53.5/x.length ), 'returns expected value' ); - x = [ -4.0, -4.0 ]; - v = stdevyc( x.length, 0, toAccessorArray( x ), 1, 0 ); - t.strictEqual( v, 0.0, 'returns expected value' ); + x = [ -4.0, -4.0 ]; + v = stdevyc( x.length, 0, x, 1, 0 ); + t.strictEqual( v, 0.0, 'returns expected value' ); - x = [ NaN, 4.0 ]; - v = stdevyc( x.length, 0, toAccessorArray( x ), 1, 0 ); - t.strictEqual( isnan( v ), true, 'returns expected value' ); + x = [ NaN, 4.0 ]; + v = stdevyc( x.length, 0, x, 1, 0 ); + t.strictEqual( isnan( v ), true, 'returns expected value' ); - t.end(); + t.end(); }); - tape( 'the function calculates the sample standard deviation of a strided array', function test( t ) { var x; var v; @@ -117,6 +112,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` (accessor)', function test( t ) { + var x; + var v; + + x = [ 1.0, -2.0, -4.0, 5.0, 3.0 ]; + + v = stdevyc( 0, 1, toAccessorArray( x ), 1, 0 ); + t.strictEqual( isnan( v ), true, 'returns expected value' ); + + v = stdevyc( -1, 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 a population standard deviation of `0`', function test( t ) { var x; var v; @@ -129,6 +139,18 @@ tape( 'if provided an `N` parameter equal to `1`, the function returns a populat t.end(); }); +tape( 'if provided an `N` parameter equal to `1`, the function returns a population standard deviation of `0` (accessor)', function test( t ) { + var x; + var v; + + x = [ 1.0, -2.0, -4.0, 5.0, 3.0 ]; + + v = stdevyc( 1, 0, toAccessorArray( x ), 1, 0 ); + t.strictEqual( v, 0.0, 'returns expected value' ); + + t.end(); +}); + tape( 'if provided a `correction` parameter yielding `N-correction` less than or equal to `0`, the function returns `NaN`', function test( t ) { var x; var v; @@ -144,6 +166,21 @@ tape( 'if provided a `correction` parameter yielding `N-correction` less than or t.end(); }); +tape( 'if provided a `correction` parameter yielding `N-correction` less than or equal to `0`, the function returns `NaN` (accessor)', function test( t ) { + var x; + var v; + + x = [ 1.0, -2.0, -4.0, 5.0, 3.0 ]; + + v = stdevyc( x.length, x.length, toAccessorArray( x ), 1, 0 ); + t.strictEqual( isnan( v ), true, 'returns expected value' ); + + v = stdevyc( x.length, x.length+1, toAccessorArray( x ), 1, 0 ); + t.strictEqual( isnan( v ), true, 'returns expected value' ); + + t.end(); +}); + tape( 'the function supports a `stride` parameter', function test( t ) { var x; var v; @@ -165,7 +202,7 @@ tape( 'the function supports a `stride` parameter', function test( t ) { t.end(); }); -tape( 'the function supports a `stride` parameter', function test( t ) { +tape( 'the function supports a `stride` parameter (accessor)', function test( t ) { var x; var v; @@ -207,7 +244,7 @@ tape( 'the function supports a negative `stride` parameter', function test( t ) t.end(); }); -tape( 'the function supports a negative `stride` parameter', function test( t ) { +tape( 'the function supports a negative `stride` parameter (accessor)', function test( t ) { var x; var v; @@ -228,7 +265,6 @@ tape( 'the function supports a negative `stride` parameter', function test( t ) t.end(); }); - tape( 'if provided a `stride` parameter equal to `0`, the function returns `0`', function test( t ) { var x; var v; @@ -241,7 +277,7 @@ tape( 'if provided a `stride` parameter equal to `0`, the function returns `0`', t.end(); }); -tape( 'if provided a `stride` parameter equal to `0`, the function returns `0`', function test( t ) { +tape( 'if provided a `stride` parameter equal to `0`, the function returns `0` (accessor)', function test( t ) { var x; var v; @@ -274,7 +310,7 @@ tape( 'the function supports an `offset` parameter', function test( t ) { t.end(); }); -tape( 'the function supports an `offset` parameter', function test( t ) { +tape( 'the function supports an `offset` parameter (accessor)', function test( t ) { var x; var v; @@ -293,4 +329,4 @@ tape( 'the function supports an `offset` parameter', function test( t ) { t.strictEqual( v, 2.5, 'returns expected value' ); t.end(); -}); \ No newline at end of file +}); From 17f50630e25a4caf3878ef9097aa14949787647e Mon Sep 17 00:00:00 2001 From: Hecker165 Date: Thu, 20 Mar 2025 03:03:52 +0530 Subject: [PATCH 7/8] feat: add support for accessor arrays and refactor stats/base/stdevyc --- .../@stdlib/stats/base/stdevyc/lib/stdevyc.js | 20 +++++++++---------- .../stats/base/stdevyc/test/test.stdevyc.js | 13 +++--------- 2 files changed, 13 insertions(+), 20 deletions(-) diff --git a/lib/node_modules/@stdlib/stats/base/stdevyc/lib/stdevyc.js b/lib/node_modules/@stdlib/stats/base/stdevyc/lib/stdevyc.js index aba96422fabd..50ff909fd37e 100644 --- a/lib/node_modules/@stdlib/stats/base/stdevyc/lib/stdevyc.js +++ b/lib/node_modules/@stdlib/stats/base/stdevyc/lib/stdevyc.js @@ -48,17 +48,17 @@ var varianceyc = require( '@stdlib/stats/base/varianceyc' ).ndarray; * var v = stdevyc( x.length, 1, x, 1, 0 ); * // returns ~2.0817 */ -function stdevyc( N, correction, x, strideX, offsetX ) { - var o; +function stdevyc( N, correction, x, strideX ) { + var o; - if ( N <= 0 ) { - return NaN; - } - o = arraylike2object( x ); - if ( o.accessorProtocol ) { - return accessors( N, correction, o, strideX, offsetX ); - } - return sqrt( varianceyc( N, correction, x, strideX, offsetX ) ); + if ( N <= 0 ) { + return NaN; + } + o = arraylike2object( x ); + if ( o.accessorProtocol ) { + return accessors( N, correction, o, strideX, 0 ); + } + return sqrt( varianceyc( N, correction, x, strideX ) ); } diff --git a/lib/node_modules/@stdlib/stats/base/stdevyc/test/test.stdevyc.js b/lib/node_modules/@stdlib/stats/base/stdevyc/test/test.stdevyc.js index 7eb5dc11d7b1..a6bf1ee7e913 100644 --- a/lib/node_modules/@stdlib/stats/base/stdevyc/test/test.stdevyc.js +++ b/lib/node_modules/@stdlib/stats/base/stdevyc/test/test.stdevyc.js @@ -21,7 +21,6 @@ // MODULES // var tape = require( 'tape' ); -var floor = require( '@stdlib/math/base/special/floor' ); var sqrt = require( '@stdlib/math/base/special/sqrt' ); var isnan = require( '@stdlib/math/base/assert/is-nan' ); var Float64Array = require( '@stdlib/array/float64' ); @@ -122,7 +121,6 @@ tape( 'if provided a `correction` parameter yielding `N-correction` less than or }); tape( 'the function supports a `stride` parameter', function test( t ) { - var N; var x; var v; @@ -137,15 +135,13 @@ tape( 'the function supports a `stride` parameter', function test( t ) { 2.0 ]; - N = floor( x.length / 2 ); - v = stdevyc( N, 1, x, 2 ); + v = stdevyc( 4, 1, x, 2 ); t.strictEqual( v, 2.5, 'returns expected value' ); t.end(); }); tape( 'the function supports a negative `stride` parameter', function test( t ) { - var N; var x; var v; @@ -160,8 +156,7 @@ tape( 'the function supports a negative `stride` parameter', function test( t ) 2.0 ]; - N = floor( x.length / 2 ); - v = stdevyc( N, 1, x, -2 ); + v = stdevyc( 4, 1, x, -2 ); t.strictEqual( v, 2.5, 'returns expected value' ); t.end(); @@ -182,7 +177,6 @@ tape( 'if provided a `stride` parameter equal to `0`, the function returns `0`', tape( 'the function supports view offsets', function test( t ) { var x0; var x1; - var N; var v; x0 = new Float64Array([ @@ -198,9 +192,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 = stdevyc( N, 1, x1, 2 ); + v = stdevyc( 4, 1, x1, 2 ); t.strictEqual( v, 2.5, 'returns expected value' ); t.end(); From 7e8de102abc2e120f3c50e587381ac7bb10d743a Mon Sep 17 00:00:00 2001 From: Hecker165 Date: Mon, 7 Apr 2025 02:23:45 +0530 Subject: [PATCH 8/8] feat: add support for accessor arrays and refactor stats/base/max-by --- .../@stdlib/stats/base/max-by/README.md | 24 +++++++-------- .../stats/base/max-by/benchmark/benchmark.js | 17 ++++++----- .../max-by/benchmark/benchmark.ndarray.js | 17 ++++++----- .../@stdlib/stats/base/max-by/docs/repl.txt | 27 ++++++++--------- .../stats/base/max-by/docs/types/index.d.ts | 15 ++++++---- .../stats/base/max-by/docs/types/test.ts | 3 ++ .../@stdlib/stats/base/max-by/lib/index.js | 13 ++++++-- .../@stdlib/stats/base/max-by/lib/main.js | 30 +++++++++++++++---- 8 files changed, 88 insertions(+), 58 deletions(-) diff --git a/lib/node_modules/@stdlib/stats/base/max-by/README.md b/lib/node_modules/@stdlib/stats/base/max-by/README.md index 96c7229df2f2..f02bfbf9a919 100644 --- a/lib/node_modules/@stdlib/stats/base/max-by/README.md +++ b/lib/node_modules/@stdlib/stats/base/max-by/README.md @@ -30,7 +30,7 @@ limitations under the License. var maxBy = require( '@stdlib/stats/base/max-by' ); ``` -#### maxBy( N, x, stride, clbk\[, thisArg] ) +#### maxBy( N, x, strideX, clbk\[, thisArg] ) Calculates the maximum value of strided array `x` via a callback function. @@ -49,7 +49,7 @@ The function has the following parameters: - **N**: number of indexed elements. - **x**: input [`Array`][mdn-array], [`typed array`][mdn-typed-array], or an array-like object (excluding strings and functions). -- **stride**: index increment. +- **strideX**: stride length. - **clbk**: callback function. - **thisArg**: execution context (_optional_). @@ -81,19 +81,16 @@ var cnt = context.count; // returns 8 ``` -The `N` and `stride` parameters determine which elements in `x` are accessed at runtime. For example, to access every other element +The `N` and stride parameters determine which elements in the strided array are accessed at runtime. For example, to access every other element ```javascript -var floor = require( '@stdlib/math/base/special/floor' ); - function accessor( v ) { return v * 2.0; } var x = [ -2.0, 1.0, 3.0, -5.0, 4.0, 0.0, -1.0, -3.0 ]; -var N = floor( x.length / 2 ); -var v = maxBy( N, x, 2, accessor ); +var v = maxBy( 4, x, 2, accessor ); // returns 8.0 ``` @@ -101,7 +98,6 @@ 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' ); function accessor( v ) { return v * 2.0; @@ -112,14 +108,13 @@ var x0 = new Float64Array( [ 1.0, -2.0, 3.0, -4.0, 5.0, -6.0 ] ); // Create an offset view... var x1 = new Float64Array( x0.buffer, x0.BYTES_PER_ELEMENT*1 ); // start at 2nd element -var N = floor( x0.length/2 ); // Access every other element... -var v = maxBy( N, x1, 2, accessor ); +var v = maxBy( 3, x1, 2, accessor ); // returns -4.0 ``` -#### maxBy.ndarray( N, x, stride, offset, clbk\[, thisArg] ) +#### maxBy.ndarray( N, x, strideX, offsetX, clbk\[, thisArg] ) Calculates the maximum value of strided array `x` via a callback function and using alternative indexing semantics. @@ -136,9 +131,9 @@ var v = maxBy.ndarray( x.length, x, 1, 0, accessor ); The function has the following additional parameters: -- **offset**: starting index. +- **offsetX**: starting index. -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 access only the last three elements of `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 access only the last three elements of the strided array ```javascript function accessor( v ) { @@ -160,6 +155,7 @@ var v = maxBy.ndarray( 3, x, 1, x.length-3, accessor ); ## 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]). - A provided callback function should return a numeric value. - If a provided callback function does not return any value (or equivalently, explicitly returns `undefined`), the value is **ignored**. - When possible, prefer using [`dmax`][@stdlib/stats/strided/dmax], [`smax`][@stdlib/stats/base/smax], and/or [`max`][@stdlib/stats/base/max], as, depending on the environment, these interfaces are likely to be significantly more performant. @@ -220,6 +216,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/dmax]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/strided/dmax diff --git a/lib/node_modules/@stdlib/stats/base/max-by/benchmark/benchmark.js b/lib/node_modules/@stdlib/stats/base/max-by/benchmark/benchmark.js index 1fa249e06191..1eeea2ef350f 100644 --- a/lib/node_modules/@stdlib/stats/base/max-by/benchmark/benchmark.js +++ b/lib/node_modules/@stdlib/stats/base/max-by/benchmark/benchmark.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 maxBy = require( './../lib/max_by.js' ); +// VARIABLES // + +var options = { + 'dtype': 'generic' +}; + + // FUNCTIONS // /** @@ -49,13 +56,7 @@ function accessor( value ) { * @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/max-by/benchmark/benchmark.ndarray.js b/lib/node_modules/@stdlib/stats/base/max-by/benchmark/benchmark.ndarray.js index 6d6c35b06fd6..876d2bf0f00b 100644 --- a/lib/node_modules/@stdlib/stats/base/max-by/benchmark/benchmark.ndarray.js +++ b/lib/node_modules/@stdlib/stats/base/max-by/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 maxBy = require( './../lib/ndarray.js' ); +// VARIABLES // + +var options = { + 'dtype': 'generic' +}; + + // FUNCTIONS // /** @@ -49,13 +56,7 @@ function accessor( value ) { * @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/max-by/docs/repl.txt b/lib/node_modules/@stdlib/stats/base/max-by/docs/repl.txt index 177b7b5f91ff..abc04b3cbf07 100644 --- a/lib/node_modules/@stdlib/stats/base/max-by/docs/repl.txt +++ b/lib/node_modules/@stdlib/stats/base/max-by/docs/repl.txt @@ -1,8 +1,8 @@ -{{alias}}( N, x, stride, clbk[, thisArg] ) +{{alias}}( N, x, strideX, clbk[, thisArg] ) Calculates the maximum value of a strided array via a callback function. - The `N` and `stride` parameters determine which elements in `x` are accessed + 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 typed @@ -31,8 +31,8 @@ Input array/collection. If provided an object, the object must be array- like (excluding strings and functions). - stride: integer - Index increment for `x`. + strideX: integer + Stride length for `x`. clbk: Function Callback function. @@ -53,20 +53,18 @@ > {{alias}}( x.length, x, 1, accessor ) 8.0 - // Using `N` and `stride` parameters: + // Using `N` and stride parameters: > x = [ -2.0, 1.0, 3.0, -5.0, 4.0, -1.0, -3.0 ]; - > var N = {{alias:@stdlib/math/base/special/floor}}( x.length / 2 ); - > {{alias}}( N, x, 2, accessor ) + > {{alias}}( 3, x, 2, accessor ) 8.0 // Using view offsets: > var x0 = new {{alias:@stdlib/array/float64}}( [ 1.0, -2.0, 3.0, -4.0, 5.0, -6.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 ); - > {{alias}}( N, x1, 2, accessor ) + > {{alias}}( 3, x1, 2, accessor ) -4.0 -{{alias}}.ndarray( N, x, stride, offset, clbk[, thisArg] ) +{{alias}}.ndarray( N, x, strideX, offsetX, clbk[, thisArg] ) Calculates the maximum value of a strided array via a callback function and using alternative indexing semantics. @@ -83,10 +81,10 @@ Input array/collection. If provided an object, the object must be array- like (excluding strings and functions). - stride: integer - Index increment for `x`. + strideX: integer + Stride length for `x`. - offset: integer + offsetX: integer Starting index of `x`. clbk: Function @@ -110,8 +108,7 @@ // Using an index offset: > x = [ 1.0, -2.0, 3.0, -4.0, 5.0, -6.0 ]; - > var N = {{alias:@stdlib/math/base/special/floor}}( x.length / 2 ); - > {{alias}}.ndarray( N, x, 2, 1, accessor ) + > {{alias}}.ndarray( 3, x, 2, 1, accessor ) -4.0 See Also diff --git a/lib/node_modules/@stdlib/stats/base/max-by/docs/types/index.d.ts b/lib/node_modules/@stdlib/stats/base/max-by/docs/types/index.d.ts index 71733418f849..8ca458f9a580 100644 --- a/lib/node_modules/@stdlib/stats/base/max-by/docs/types/index.d.ts +++ b/lib/node_modules/@stdlib/stats/base/max-by/docs/types/index.d.ts @@ -20,7 +20,12 @@ /// -import { Collection } from '@stdlib/types/array'; +import { NumericArray, Collection, AccessorArrayLike } from '@stdlib/types/array'; + + /** + * Input array. + */ + type InputArray = NumericArray | Collection | AccessorArrayLike; /** * Returns an accessed value. @@ -131,8 +136,8 @@ interface Routine { * * @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 * @param clbk - callback * @param thisArg - execution context * @returns maximum value @@ -147,7 +152,7 @@ interface Routine { * var v = maxBy.ndarray( x.length, x, 1, 0, accessor ); * // returns 8.0 */ - ndarray( N: number, x: Collection, stride: number, offset: number, clbk: Callback, thisArg?: ThisParameterType> ): number; + ndarray( N: number, x: Collection, strideX: number, offsetX: number, clbk: Callback, thisArg?: ThisParameterType> ): number; } /** @@ -166,7 +171,7 @@ interface Routine { * * @param N - number of indexed elements * @param x - input array -* @param stride - stride length +* @param strideX - stride length * @param clbk - callback * @param thisArg - execution context * @returns maximum value diff --git a/lib/node_modules/@stdlib/stats/base/max-by/docs/types/test.ts b/lib/node_modules/@stdlib/stats/base/max-by/docs/types/test.ts index d03925870c1c..ddc38778b978 100644 --- a/lib/node_modules/@stdlib/stats/base/max-by/docs/types/test.ts +++ b/lib/node_modules/@stdlib/stats/base/max-by/docs/types/test.ts @@ -16,6 +16,7 @@ * limitations under the License. */ +import AccessorArray = require( '@stdlib/array/base/accessor' ); import maxBy = require( './index' ); const accessor = (): number => { @@ -31,6 +32,7 @@ const accessor = (): number => { maxBy( x.length, x, 1, accessor ); // $ExpectType number maxBy( x.length, x, 1, accessor, {} ); // $ExpectType number + maxBy( x.length, new AccessorArray( x ), 1, accessor, {} ); // $ExpectType number } // The compiler throws an error if the function is provided a first argument which is not a number... @@ -103,6 +105,7 @@ const accessor = (): number => { maxBy.ndarray( x.length, x, 1, 0, accessor ); // $ExpectType number maxBy.ndarray( x.length, x, 1, 0, accessor, {} ); // $ExpectType number + maxBy.ndarray( x.length, new AccessorArray( x ), 1, 0, accessor, {} ); // $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/max-by/lib/index.js b/lib/node_modules/@stdlib/stats/base/max-by/lib/index.js index 29304e7b467b..db99ce9f4e46 100644 --- a/lib/node_modules/@stdlib/stats/base/max-by/lib/index.js +++ b/lib/node_modules/@stdlib/stats/base/max-by/lib/index.js @@ -50,11 +50,18 @@ // MODULES // -var maxBy = 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 = maxBy; +module.exports = main; -// exports: { "ndarray": "maxBy.ndarray" } +// exports: { "ndarray": "main.ndarray" } \ No newline at end of file diff --git a/lib/node_modules/@stdlib/stats/base/max-by/lib/main.js b/lib/node_modules/@stdlib/stats/base/max-by/lib/main.js index fa380c09009f..31c400d767b3 100644 --- a/lib/node_modules/@stdlib/stats/base/max-by/lib/main.js +++ b/lib/node_modules/@stdlib/stats/base/max-by/lib/main.js @@ -20,16 +20,34 @@ // MODULES // -var setReadOnly = require( '@stdlib/utils/define-nonenumerable-read-only-property' ); -var maxBy = require( './max_by.js' ); +var stride2offset = require( '@stdlib/strided/base/stride2offset' ); var ndarray = require( './ndarray.js' ); // MAIN // -setReadOnly( maxBy, 'ndarray', ndarray ); - - -// EXPORTS // +/** + * Calculates the maximum value of a strided array via a callback function. + * + * @param {PositiveInteger} N - number of indexed elements + * @param {NumericArray} x - input array + * @param {integer} strideX - stride length + * @param {Callback} clbk - callback function + * @param {*} [thisArg] - execution context + * @returns {number} maximum value + * + * @example + * var x = [ -2.0, 1.0, 3.0, -5.0, 4.0, 0.0, -1.0, -3.0 ]; + * + * function accessor( v ) { + * return v * 2.0; + * } + * + * var v = maxBy( x.length, x, 1, accessor ); + * // returns 8.0 + */ +function maxBy( N, x, strideX, clbk, thisArg ) { + return ndarray( N, x, strideX, stride2offset( N, strideX ), clbk, thisArg ); +} module.exports = maxBy;