From c98c697b0ec426b10e171a0a8fce18091fd27c47 Mon Sep 17 00:00:00 2001 From: orthodox-64 Date: Tue, 10 Feb 2026 12:16:11 +0530 Subject: [PATCH] feat: add stats/array/midrange --- .../@stdlib/stats/array/midrange/README.md | 112 +++++++++++ .../array/midrange/benchmark/benchmark.js | 103 ++++++++++ .../stats/array/midrange/docs/repl.txt | 25 +++ .../array/midrange/docs/types/index.d.ts | 47 +++++ .../stats/array/midrange/docs/types/test.ts | 51 +++++ .../stats/array/midrange/examples/index.js | 30 +++ .../@stdlib/stats/array/midrange/lib/index.js | 42 ++++ .../@stdlib/stats/array/midrange/lib/main.js | 69 +++++++ .../@stdlib/stats/array/midrange/package.json | 70 +++++++ .../@stdlib/stats/array/midrange/test/test.js | 187 ++++++++++++++++++ 10 files changed, 736 insertions(+) create mode 100644 lib/node_modules/@stdlib/stats/array/midrange/README.md create mode 100644 lib/node_modules/@stdlib/stats/array/midrange/benchmark/benchmark.js create mode 100644 lib/node_modules/@stdlib/stats/array/midrange/docs/repl.txt create mode 100644 lib/node_modules/@stdlib/stats/array/midrange/docs/types/index.d.ts create mode 100644 lib/node_modules/@stdlib/stats/array/midrange/docs/types/test.ts create mode 100644 lib/node_modules/@stdlib/stats/array/midrange/examples/index.js create mode 100644 lib/node_modules/@stdlib/stats/array/midrange/lib/index.js create mode 100644 lib/node_modules/@stdlib/stats/array/midrange/lib/main.js create mode 100644 lib/node_modules/@stdlib/stats/array/midrange/package.json create mode 100644 lib/node_modules/@stdlib/stats/array/midrange/test/test.js diff --git a/lib/node_modules/@stdlib/stats/array/midrange/README.md b/lib/node_modules/@stdlib/stats/array/midrange/README.md new file mode 100644 index 000000000000..b77e6bdeef82 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/array/midrange/README.md @@ -0,0 +1,112 @@ + + +# midrange + +> Calculate the [midrange][midrange] of an array. + +
+ +The [**midrange**][midrange] is defined as the arithmetic mean of the maximum and minimum values in a data set. The measure is the midpoint of the range and a measure of central tendency. + +
+ + + +
+ +## Usage + +```javascript +var midrange = require( '@stdlib/stats/array/midrange' ); +``` + +#### midrange( x ) + +Computes the [midrange][midrange] of an array. + +```javascript +var x = [ 1.0, -2.0, 2.0 ]; + +var v = midrange( x ); +// returns 0.0 +``` + +The function has the following parameters: + +- **x**: input array. + +
+ + + +
+ +## Notes + +- If provided an empty array, the function returns `NaN`. +- The function supports array-like objects having getter and setter accessors for array element access (e.g., [`@stdlib/array/base/accessor`][@stdlib/array/base/accessor]). + +
+ + + +
+ +## Examples + + + +```javascript +var discreteUniform = require( '@stdlib/random/array/discrete-uniform' ); +var midrange = require( '@stdlib/stats/array/midrange' ); + +var x = discreteUniform( 10, -50, 50, { + 'dtype': 'float64' +}); +console.log( x ); + +var v = midrange( x ); +console.log( v ); +``` + +
+ + + + + + + + + + + + + + diff --git a/lib/node_modules/@stdlib/stats/array/midrange/benchmark/benchmark.js b/lib/node_modules/@stdlib/stats/array/midrange/benchmark/benchmark.js new file mode 100644 index 000000000000..fe230531f72f --- /dev/null +++ b/lib/node_modules/@stdlib/stats/array/midrange/benchmark/benchmark.js @@ -0,0 +1,103 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2026 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 bench = require( '@stdlib/bench' ); +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 format = require( '@stdlib/string/format' ); +var pkg = require( './../package.json' ).name; +var midrange = require( './../lib' ); + + +// VARIABLES // + +var options = { + 'dtype': 'generic' +}; + + +// FUNCTIONS // + +/** +* Creates a benchmark function. +* +* @private +* @param {PositiveInteger} len - array length +* @returns {Function} benchmark function +*/ +function createBenchmark( len ) { + var x = uniform( len, -10, 10, options ); + return benchmark; + + /** + * Benchmark function. + * + * @private + * @param {Benchmark} b - benchmark instance + */ + function benchmark( b ) { + var v; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + v = midrange( x ); + if ( isnan( v ) ) { + b.fail( 'should not return NaN' ); + } + } + b.toc(); + if ( isnan( v ) ) { + b.fail( 'should not return NaN' ); + } + b.pass( 'benchmark finished' ); + b.end(); + } +} + + +// MAIN // + +/** +* Main execution sequence. +* +* @private +*/ +function main() { + var len; + var min; + var max; + var f; + var i; + + min = 1; // 10^min + max = 6; // 10^max + + for ( i = min; i <= max; i++ ) { + len = pow( 10, i ); + f = createBenchmark( len ); + bench( format( '%s:len=%d', pkg, len ), f ); + } +} + +main(); diff --git a/lib/node_modules/@stdlib/stats/array/midrange/docs/repl.txt b/lib/node_modules/@stdlib/stats/array/midrange/docs/repl.txt new file mode 100644 index 000000000000..3697286798bb --- /dev/null +++ b/lib/node_modules/@stdlib/stats/array/midrange/docs/repl.txt @@ -0,0 +1,25 @@ + +{{alias}}( x ) + Computes the midrange of an array. + + If provided an empty array, the function returns `NaN`. + + Parameters + ---------- + x: Array|TypedArray + Input array. + + Returns + ------- + out: number + Midrange. + + Examples + -------- + > var x = [ 1.0, -2.0, 2.0 ]; + > {{alias}}( x ) + 0.0 + + See Also + -------- + diff --git a/lib/node_modules/@stdlib/stats/array/midrange/docs/types/index.d.ts b/lib/node_modules/@stdlib/stats/array/midrange/docs/types/index.d.ts new file mode 100644 index 000000000000..aafe5e6b0479 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/array/midrange/docs/types/index.d.ts @@ -0,0 +1,47 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2026 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. +*/ + +// TypeScript Version: 4.1 + +/// + +import { NumericArray, Collection, AccessorArrayLike } from '@stdlib/types/array'; + +/** +* Input array. +*/ +type InputArray = NumericArray | Collection | AccessorArrayLike; + +/** +* Computes the midrange of an array. +* +* @param x - input array +* @returns midrange +* +* @example +* var x = [ 1.0, -2.0, 2.0 ]; +* +* var v = midrange( x ); +* // returns 0.0 +*/ +declare function midrange( x: InputArray ): number; + + +// EXPORTS // + +export = midrange; diff --git a/lib/node_modules/@stdlib/stats/array/midrange/docs/types/test.ts b/lib/node_modules/@stdlib/stats/array/midrange/docs/types/test.ts new file mode 100644 index 000000000000..ffe951d39232 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/array/midrange/docs/types/test.ts @@ -0,0 +1,51 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2026 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. +*/ + +import AccessorArray = require( '@stdlib/array/base/accessor' ); +import midrange = require( './index' ); + + +// TESTS // + +// The function returns a number... +{ + const x = new Float64Array( 10 ); + + midrange( x ); // $ExpectType number + midrange( new AccessorArray( x ) ); // $ExpectType number +} + +// The compiler throws an error if the function is provided a first argument which is not a numeric array... +{ + midrange( 10 ); // $ExpectError + midrange( '10' ); // $ExpectError + midrange( true ); // $ExpectError + midrange( false ); // $ExpectError + midrange( null ); // $ExpectError + midrange( undefined ); // $ExpectError + midrange( {} ); // $ExpectError + midrange( ( x: number ): number => x ); // $ExpectError +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + const x = new Float64Array( 10 ); + + midrange(); // $ExpectError + midrange( x, {} ); // $ExpectError +} diff --git a/lib/node_modules/@stdlib/stats/array/midrange/examples/index.js b/lib/node_modules/@stdlib/stats/array/midrange/examples/index.js new file mode 100644 index 000000000000..d2fab56209c0 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/array/midrange/examples/index.js @@ -0,0 +1,30 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2026 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'; + +var discreteUniform = require( '@stdlib/random/array/discrete-uniform' ); +var midrange = require( './../lib' ); + +var x = discreteUniform( 10, -50, 50, { + 'dtype': 'float64' +}); +console.log( x ); + +var v = midrange( x ); +console.log( v ); diff --git a/lib/node_modules/@stdlib/stats/array/midrange/lib/index.js b/lib/node_modules/@stdlib/stats/array/midrange/lib/index.js new file mode 100644 index 000000000000..e724fde21470 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/array/midrange/lib/index.js @@ -0,0 +1,42 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2026 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'; + +/** +* Compute the midrange of an array. +* +* @module @stdlib/stats/array/midrange +* +* @example +* var midrange = require( '@stdlib/stats/array/midrange' ); +* +* var x = [ 1.0, -2.0, 2.0 ]; +* +* var v = midrange( x ); +* // returns 0.0 +*/ + +// MODULES // + +var main = require( './main.js' ); + + +// EXPORTS // + +module.exports = main; diff --git a/lib/node_modules/@stdlib/stats/array/midrange/lib/main.js b/lib/node_modules/@stdlib/stats/array/midrange/lib/main.js new file mode 100644 index 000000000000..2f61020becd6 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/array/midrange/lib/main.js @@ -0,0 +1,69 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2026 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 isCollection = require( '@stdlib/assert/is-collection' ); +var dtypes = require( '@stdlib/array/dtypes' ); +var dtype = require( '@stdlib/array/dtype' ); +var contains = require( '@stdlib/array/base/assert/contains' ); +var join = require( '@stdlib/array/base/join' ); +var strided = require( '@stdlib/stats/strided/midrange' ).ndarray; +var format = require( '@stdlib/string/format' ); + + +// VARIABLES // + +var IDTYPES = dtypes( 'real_and_generic' ); +var GENERIC_DTYPE = 'generic'; + + +// MAIN // + +/** +* Computes the midrange of an array. +* +* @param {NumericArray} x - input array +* @throws {TypeError} first argument must be an array-like object +* @throws {TypeError} first argument must have a supported data type +* @returns {number} midrange +* +* @example +* var x = [ 1.0, -2.0, 2.0 ]; +* +* var v = midrange( x ); +* // returns 0.0 +*/ +function midrange( x ) { + var dt; + if ( !isCollection( x ) ) { + throw new TypeError( format( 'invalid argument. First argument must be an array-like object. Value: `%s`.', x ) ); + } + dt = dtype( x ) || GENERIC_DTYPE; + if ( !contains( IDTYPES, dt ) ) { + throw new TypeError( format( 'invalid argument. First argument must have one of the following data types: "%s". Data type: `%s`.', join( IDTYPES, '", "' ), dt ) ); + } + return strided( x.length, x, 1, 0 ); +} + + +// EXPORTS // + +module.exports = midrange; diff --git a/lib/node_modules/@stdlib/stats/array/midrange/package.json b/lib/node_modules/@stdlib/stats/array/midrange/package.json new file mode 100644 index 000000000000..9665792eb649 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/array/midrange/package.json @@ -0,0 +1,70 @@ +{ + "name": "@stdlib/stats/array/midrange", + "version": "0.0.0", + "description": "Calculate the midrange of an array.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "main": "./lib", + "directories": { + "benchmark": "./benchmark", + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdmath", + "statistics", + "stats", + "mathematics", + "math", + "minimum", + "min", + "maximum", + "max", + "range", + "midrange", + "extremes", + "domain", + "extent", + "array" + ], + "__stdlib__": {} +} diff --git a/lib/node_modules/@stdlib/stats/array/midrange/test/test.js b/lib/node_modules/@stdlib/stats/array/midrange/test/test.js new file mode 100644 index 000000000000..adc6c8936192 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/array/midrange/test/test.js @@ -0,0 +1,187 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2026 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 tape = require( 'tape' ); +var isnan = require( '@stdlib/math/base/assert/is-nan' ); +var toAccessorArray = require( '@stdlib/array/base/to-accessor-array' ); +var isPositiveZero = require( '@stdlib/math/base/assert/is-positive-zero' ); +var BooleanArray = require( '@stdlib/array/bool' ); +var Complex128Array = require( '@stdlib/array/complex128' ); +var midrange = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof midrange, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function has an arity of 1', function test( t ) { + t.strictEqual( midrange.length, 1, 'returns expected value' ); + t.end(); +}); + +tape( 'the function throws an error if provided a first argument which is not an array-like object', function test( t ) { + var values; + var i; + + values = [ + '5', + 5, + NaN, + true, + false, + null, + void 0, + {}, + function noop() {} + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + midrange( value ); + }; + } +}); + +tape( 'the function throws an error if provided a first argument which has an unsupported data type', function test( t ) { + var values; + var i; + + values = [ + new BooleanArray( 4 ), + new Complex128Array( 4 ) + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + midrange( value ); + }; + } +}); + +tape( 'the function calculates the midrange of an array', function test( t ) { + var x; + var v; + + x = [ 1.0, -2.0, -4.0, 5.0, 0.0, 3.0 ]; + v = midrange( x ); + t.strictEqual( v, 0.5, 'returns expected value' ); + + x = [ -4.0, -5.0 ]; + v = midrange( x ); + t.strictEqual( v, -4.5, 'returns expected value' ); + + x = [ -0.0, 0.0, -0.0 ]; + v = midrange( x ); + t.strictEqual( isPositiveZero( v ), true, 'returns expected value' ); + + x = [ NaN ]; + v = midrange( x ); + t.strictEqual( isnan( v ), true, 'returns expected value' ); + + x = [ NaN, NaN ]; + v = midrange( x ); + t.strictEqual( isnan( v ), true, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function calculates the midrange of an array (accessors)', function test( t ) { + var x; + var v; + + x = [ 1.0, -2.0, -4.0, 5.0, 0.0, 3.0 ]; + v = midrange( toAccessorArray( x ) ); + t.strictEqual( v, 0.5, 'returns expected value' ); + + x = [ -4.0, -5.0 ]; + v = midrange( toAccessorArray( x ) ); + t.strictEqual( v, -4.5, 'returns expected value' ); + + x = [ -0.0, 0.0, -0.0 ]; + v = midrange( toAccessorArray( x ) ); + t.strictEqual( isPositiveZero( v ), true, 'returns expected value' ); + + x = [ NaN ]; + v = midrange( toAccessorArray( x ) ); + t.strictEqual( isnan( v ), true, 'returns expected value' ); + + x = [ NaN, NaN ]; + v = midrange( toAccessorArray( x ) ); + t.strictEqual( isnan( v ), true, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function calculates the midrange of an array (array-like object)', function test( t ) { + var x; + var v; + + x = { + 'length': 6, + '0': 1.0, + '1': -2.0, + '2': -4.0, + '3': 5.0, + '4': 0.0, + '5': 3.0 + }; + v = midrange( x ); + t.strictEqual( v, 0.5, 'returns expected value' ); + + t.end(); +}); + +tape( 'if provided an empty array, the function returns `NaN`', function test( t ) { + var v = midrange( [] ); + t.strictEqual( isnan( v ), true, 'returns expected value' ); + t.end(); +}); + +tape( 'if provided an empty array, the function returns `NaN` (accessors)', function test( t ) { + var v = midrange( toAccessorArray( [] ) ); + t.strictEqual( isnan( v ), true, 'returns expected value' ); + t.end(); +}); + +tape( 'if provided an array containing a single element, the function returns the element value', function test( t ) { + var v = midrange( [ 1.0 ] ); + t.strictEqual( v, 1.0, 'returns expected value' ); + t.end(); +}); + +tape( 'if provided an array containing a single element, the function returns the element value (accessors)', function test( t ) { + var v = midrange( toAccessorArray( [ 1.0 ] ) ); + t.strictEqual( v, 1.0, 'returns expected value' ); + t.end(); +});