From 8ec678440d9a8f480292d1b8fe6164bf1aeb3191 Mon Sep 17 00:00:00 2001 From: Sachin Pangal Date: Fri, 7 Nov 2025 20:25:09 +0530 Subject: [PATCH 1/4] feat: add stats/base/ndarray/mediansorted --- .../stats/base/ndarray/mediansorted/README.md | 116 ++++++++++++ .../mediansorted/benchmark/benchmark.js | 105 +++++++++++ .../base/ndarray/mediansorted/docs/repl.txt | 31 ++++ .../mediansorted/docs/types/index.d.ts | 45 +++++ .../ndarray/mediansorted/docs/types/test.ts | 57 ++++++ .../ndarray/mediansorted/examples/index.js | 42 +++++ .../base/ndarray/mediansorted/lib/index.js | 44 +++++ .../base/ndarray/mediansorted/lib/main.js | 55 ++++++ .../base/ndarray/mediansorted/package.json | 67 +++++++ .../base/ndarray/mediansorted/test/test.js | 167 ++++++++++++++++++ 10 files changed, 729 insertions(+) create mode 100644 lib/node_modules/@stdlib/stats/base/ndarray/mediansorted/README.md create mode 100644 lib/node_modules/@stdlib/stats/base/ndarray/mediansorted/benchmark/benchmark.js create mode 100644 lib/node_modules/@stdlib/stats/base/ndarray/mediansorted/docs/repl.txt create mode 100644 lib/node_modules/@stdlib/stats/base/ndarray/mediansorted/docs/types/index.d.ts create mode 100644 lib/node_modules/@stdlib/stats/base/ndarray/mediansorted/docs/types/test.ts create mode 100644 lib/node_modules/@stdlib/stats/base/ndarray/mediansorted/examples/index.js create mode 100644 lib/node_modules/@stdlib/stats/base/ndarray/mediansorted/lib/index.js create mode 100644 lib/node_modules/@stdlib/stats/base/ndarray/mediansorted/lib/main.js create mode 100644 lib/node_modules/@stdlib/stats/base/ndarray/mediansorted/package.json create mode 100644 lib/node_modules/@stdlib/stats/base/ndarray/mediansorted/test/test.js diff --git a/lib/node_modules/@stdlib/stats/base/ndarray/mediansorted/README.md b/lib/node_modules/@stdlib/stats/base/ndarray/mediansorted/README.md new file mode 100644 index 000000000000..4c030905fdde --- /dev/null +++ b/lib/node_modules/@stdlib/stats/base/ndarray/mediansorted/README.md @@ -0,0 +1,116 @@ + + +# mediansorted + +> Compute the median value of a sorted one-dimensional ndarray. + +
+ +
+ + + +
+ +## Usage + +```javascript +var mediansorted = require( '@stdlib/stats/base/ndarray/mediansorted' ); +``` + +#### mediansorted( arrays ) + +Computes the median value of a sorted one-dimensional ndarray. + +```javascript +var ndarray = require( '@stdlib/ndarray/base/ctor' ); + +var xbuf = [ 1.0, 2.0, 3.0 ]; +var x = new ndarray( 'generic', xbuf, [ 3 ], [ 1 ], 0, 'row-major' ); + +var v = mediansorted( [ x ] ); +// returns 2.0 +``` + +The function has the following parameters: + +- **arrays**: array-like object containing a sorted one-dimensional input ndarray. + +
+ + + +
+ +## Notes + +- If provided an empty ndarray, the function returns `NaN`. + +
+ + + +
+ +## Examples + + + +```javascript +var discreteUniform = require( '@stdlib/random/array/discrete-uniform' ); +var sort = require( '@stdlib/array/base/sort2hp' ); +var ndarray = require( '@stdlib/ndarray/base/ctor' ); +var ndarray2array = require( '@stdlib/ndarray/to-array' ); +var mediansorted = require( '@stdlib/stats/base/ndarray/mediansorted' ); + +var xbuf = discreteUniform( 10, -50, 50, { + 'dtype': 'generic' +}); + +// Sort the array: +sort( xbuf, xbuf.length ); + +var x = new ndarray( 'generic', xbuf, [ xbuf.length ], [ 1 ], 0, 'row-major' ); +console.log( ndarray2array( x ) ); + +var v = mediansorted( [ x ] ); +console.log( v ); +``` + +
+ + + + + + + + + + + + + + diff --git a/lib/node_modules/@stdlib/stats/base/ndarray/mediansorted/benchmark/benchmark.js b/lib/node_modules/@stdlib/stats/base/ndarray/mediansorted/benchmark/benchmark.js new file mode 100644 index 000000000000..a3b576df9b1a --- /dev/null +++ b/lib/node_modules/@stdlib/stats/base/ndarray/mediansorted/benchmark/benchmark.js @@ -0,0 +1,105 @@ +/** +* @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 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 ndarray = require( '@stdlib/ndarray/base/ctor' ); +var pkg = require( './../package.json' ).name; +var mediansorted = 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 xbuf; + var x; + + xbuf = uniform( len, -10.0, 10.0, options ); + xbuf.sort( function compare( a, b ) { + return a - b; + }); + x = new ndarray( options.dtype, xbuf, [ len ], [ 1 ], 0, 'row-major' ); + + return benchmark; + + function benchmark( b ) { + var v; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + v = mediansorted( [ 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( pkg+':len='+len, f ); + } +} + +main(); diff --git a/lib/node_modules/@stdlib/stats/base/ndarray/mediansorted/docs/repl.txt b/lib/node_modules/@stdlib/stats/base/ndarray/mediansorted/docs/repl.txt new file mode 100644 index 000000000000..84b90d53d94d --- /dev/null +++ b/lib/node_modules/@stdlib/stats/base/ndarray/mediansorted/docs/repl.txt @@ -0,0 +1,31 @@ + +{{alias}}( arrays ) + Computes the median value of a sorted one-dimensional ndarray. + + If provided an empty ndarray, the function returns `NaN`. + + Parameters + ---------- + arrays: ArrayLikeObject + Array-like object containing a sorted one-dimensional input ndarray. + + Returns + ------- + out: number + Median value. + + Examples + -------- + > var xbuf = [ 1.0, 2.0, 3.0 ]; + > var dt = 'generic'; + > var sh = [ xbuf.length ]; + > var sx = [ 1 ]; + > var ox = 0; + > var ord = 'row-major'; + > var x = new {{alias:@stdlib/ndarray/ctor}}( dt, xbuf, sh, sx, ox, ord ); + > {{alias}}( [ x ] ) + 2.0 + + See Also + -------- + diff --git a/lib/node_modules/@stdlib/stats/base/ndarray/mediansorted/docs/types/index.d.ts b/lib/node_modules/@stdlib/stats/base/ndarray/mediansorted/docs/types/index.d.ts new file mode 100644 index 000000000000..a53472bf7a3a --- /dev/null +++ b/lib/node_modules/@stdlib/stats/base/ndarray/mediansorted/docs/types/index.d.ts @@ -0,0 +1,45 @@ +/* +* @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. +*/ + +// TypeScript Version: 4.1 + +/// + +import { ndarray } from '@stdlib/types/ndarray'; + +/** +* Computes the median value of a sorted one-dimensional ndarray. +* +* @param arrays - array-like object containing a sorted input ndarray +* @returns median value +* +* @example +* var ndarray = require( '@stdlib/ndarray/base/ctor' ); +* +* var xbuf = [ 1.0, 2.0, 3.0 ]; +* var x = new ndarray( 'generic', xbuf, [ 3 ], [ 1 ], 0, 'row-major' ); +* +* var v = mediansorted( [ x ] ); +* // returns 2.0 +*/ +declare function mediansorted( arrays: [ T ] ): number; + + +// EXPORTS // + +export = mediansorted; diff --git a/lib/node_modules/@stdlib/stats/base/ndarray/mediansorted/docs/types/test.ts b/lib/node_modules/@stdlib/stats/base/ndarray/mediansorted/docs/types/test.ts new file mode 100644 index 000000000000..6da978af700f --- /dev/null +++ b/lib/node_modules/@stdlib/stats/base/ndarray/mediansorted/docs/types/test.ts @@ -0,0 +1,57 @@ +/* +* @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. +*/ + +/* eslint-disable space-in-parens */ + +import zeros = require( '@stdlib/ndarray/zeros' ); +import mediansorted = require( './index' ); + + +// TESTS // + +// The function returns a number... +{ + const x = zeros( [ 10 ], { + 'dtype': 'generic' + }); + + mediansorted( [ x ] ); // $ExpectType number +} + +// The compiler throws an error if the function is provided a first argument which is not an array of ndarrays... +{ + mediansorted( '10' ); // $ExpectError + mediansorted( 10 ); // $ExpectError + mediansorted( true ); // $ExpectError + mediansorted( false ); // $ExpectError + mediansorted( null ); // $ExpectError + mediansorted( undefined ); // $ExpectError + mediansorted( [] ); // $ExpectError + mediansorted( {} ); // $ExpectError + mediansorted( ( x: number ): number => x ); // $ExpectError +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + const x = zeros( [ 10 ], { + 'dtype': 'generic' + }); + + mediansorted(); // $ExpectError + mediansorted( [ x ], {} ); // $ExpectError +} diff --git a/lib/node_modules/@stdlib/stats/base/ndarray/mediansorted/examples/index.js b/lib/node_modules/@stdlib/stats/base/ndarray/mediansorted/examples/index.js new file mode 100644 index 000000000000..33c52a5bdde3 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/base/ndarray/mediansorted/examples/index.js @@ -0,0 +1,42 @@ +/** +* @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'; + +var uniform = require( '@stdlib/random/array/uniform' ); +var ndarray = require( '@stdlib/ndarray/base/ctor' ); +var mediansorted = require( './../lib' ); + +// Create a sorted array: +var xbuf = uniform( 10, -10.0, 10.0, { + 'dtype': 'generic' +}); + +// Sort the array: +xbuf.sort( function compare( a, b ) { + return a - b; +}); + +// Create an ndarray: +var x = new ndarray( 'generic', xbuf, [ 10 ], [ 1 ], 0, 'row-major' ); + +// Compute the median: +var v = mediansorted( [ x ] ); + +console.log( 'x: %s', x.data ); +console.log( 'median: %d', v ); diff --git a/lib/node_modules/@stdlib/stats/base/ndarray/mediansorted/lib/index.js b/lib/node_modules/@stdlib/stats/base/ndarray/mediansorted/lib/index.js new file mode 100644 index 000000000000..49d80b4237e8 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/base/ndarray/mediansorted/lib/index.js @@ -0,0 +1,44 @@ +/** +* @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'; + +/** +* Compute the median value of a sorted one-dimensional ndarray. +* +* @module @stdlib/stats/base/ndarray/mediansorted +* +* @example +* var ndarray = require( '@stdlib/ndarray/base/ctor' ); +* var mediansorted = require( '@stdlib/stats/base/ndarray/mediansorted' ); +* +* var xbuf = [ 1.0, 2.0, 3.0 ]; +* var x = new ndarray( 'generic', xbuf, [ 3 ], [ 1 ], 0, 'row-major' ); +* +* var v = mediansorted( [ x ] ); +* // returns 2.0 +*/ + +// MODULES // + +var main = require( './main.js' ); + + +// EXPORTS // + +module.exports = main; diff --git a/lib/node_modules/@stdlib/stats/base/ndarray/mediansorted/lib/main.js b/lib/node_modules/@stdlib/stats/base/ndarray/mediansorted/lib/main.js new file mode 100644 index 000000000000..bb295a2f0031 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/base/ndarray/mediansorted/lib/main.js @@ -0,0 +1,55 @@ +/** +* @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 numelDimension = require( '@stdlib/ndarray/base/numel-dimension' ); +var getStride = require( '@stdlib/ndarray/base/stride' ); +var getOffset = require( '@stdlib/ndarray/base/offset' ); +var getData = require( '@stdlib/ndarray/base/data-buffer' ); +var strided = require( '@stdlib/stats/strided/mediansorted' ).ndarray; + + +// MAIN // + +/** +* Computes the median value of a sorted one-dimensional ndarray. +* +* @param {ArrayLikeObject} arrays - array-like object containing a sorted input ndarray +* @returns {number} median value +* +* @example +* var ndarray = require( '@stdlib/ndarray/base/ctor' ); +* +* var xbuf = [ 1.0, 2.0, 3.0 ]; +* var x = new ndarray( 'generic', xbuf, [ 3 ], [ 1 ], 0, 'row-major' ); +* +* var v = mediansorted( [ x ] ); +* // returns 2.0 +*/ +function mediansorted( arrays ) { + var x = arrays[ 0 ]; + return strided( numelDimension( x, 0 ), getData( x ), getStride( x, 0 ), getOffset( x ) ); // eslint-disable-line max-len +} + + +// EXPORTS // + +module.exports = mediansorted; diff --git a/lib/node_modules/@stdlib/stats/base/ndarray/mediansorted/package.json b/lib/node_modules/@stdlib/stats/base/ndarray/mediansorted/package.json new file mode 100644 index 000000000000..99b773d7b135 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/base/ndarray/mediansorted/package.json @@ -0,0 +1,67 @@ +{ + "name": "@stdlib/stats/base/ndarray/mediansorted", + "version": "0.0.0", + "description": "Compute the median value of a sorted one-dimensional ndarray.", + "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", + "median", + "mid", + "middle", + "center", + "central tendency", + "sorted", + "ndarray" + ], + "__stdlib__": {} +} diff --git a/lib/node_modules/@stdlib/stats/base/ndarray/mediansorted/test/test.js b/lib/node_modules/@stdlib/stats/base/ndarray/mediansorted/test/test.js new file mode 100644 index 000000000000..7499afa0bb2a --- /dev/null +++ b/lib/node_modules/@stdlib/stats/base/ndarray/mediansorted/test/test.js @@ -0,0 +1,167 @@ +/** +* @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 tape = require( 'tape' ); +var isnan = require( '@stdlib/math/base/assert/is-nan' ); +var ndarray = require( '@stdlib/ndarray/base/ctor' ); +var mediansorted = require( './../lib' ); + + +// FUNCTIONS // + +/** +* Returns a one-dimensional ndarray. +* +* @private +* @param {Collection} buffer - underlying data buffer +* @param {NonNegativeInteger} length - number of indexed elements +* @param {integer} stride - stride length +* @param {NonNegativeInteger} offset - index offset +* @returns {ndarray} one-dimensional ndarray +*/ +function vector( buffer, length, stride, offset ) { + return new ndarray( 'generic', buffer, [ length ], [ stride ], offset, 'row-major' ); +} + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof mediansorted, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function has an arity of 1', function test( t ) { + t.strictEqual( mediansorted.length, 1, 'has expected arity' ); + t.end(); +}); + +tape( 'the function calculates the median value of a sorted one-dimensional ndarray', function test( t ) { + var x; + var v; + + x = [ 1.0, 2.0, 3.0 ]; + v = mediansorted( [ vector( x, 3, 1, 0 ) ] ); + t.strictEqual( v, 2.0, 'returns expected value' ); + + x = [ 3.0, 2.0, 1.0 ]; + v = mediansorted( [ vector( x, 3, 1, 0 ) ] ); + t.strictEqual( v, 2.0, 'returns expected value' ); + + x = [ 1.0, 2.0, 3.0, 4.0 ]; + v = mediansorted( [ vector( x, 4, 1, 0 ) ] ); + t.strictEqual( v, 2.5, 'returns expected value' ); + + x = [ 4.0, 3.0, 2.0, 1.0 ]; + v = mediansorted( [ vector( x, 4, 1, 0 ) ] ); + t.strictEqual( v, 2.5, 'returns expected value' ); + + t.end(); +}); + +tape( 'if provided an empty vector, the function returns `NaN`', function test( t ) { + var x; + var v; + + x = []; + + v = mediansorted( [ vector( x, 0, 1, 0 ) ] ); + t.strictEqual( isnan( v ), true, 'returns expected value' ); + + t.end(); +}); + +tape( 'if provided a vector containing a single element, the function returns that element', function test( t ) { + var x; + var v; + + x = [ 1.0 ]; + + v = mediansorted( [ vector( x, 1, 1, 0 ) ] ); + t.strictEqual( v, 1.0, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports one-dimensional ndarrays having non-unit strides', function test( t ) { + var x; + var v; + + x = [ + 1.0, // 0 + 2.0, + 2.0, // 1 + -7.0, + 3.0, // 2 + 3.0, + 4.0, // 3 + 2.0 + ]; + + v = mediansorted( [ vector( x, 4, 2, 0 ) ] ); + + t.strictEqual( v, 2.5, 'returns expected value' ); + t.end(); +}); + +tape( 'the function supports one-dimensional ndarrays having negative strides', 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 = mediansorted( [ vector( x, 4, -2, 6 ) ] ); + + t.strictEqual( v, 2.0, 'returns expected value' ); + t.end(); +}); + +tape( 'the function supports one-dimensional ndarrays having non-zero offsets', function test( t ) { + var x; + var v; + + x = [ + 2.0, + 1.0, // 0 + 2.0, + 2.0, // 1 + 2.0, + 3.0, // 2 + 3.0, + 4.0 // 3 + ]; + + v = mediansorted( [ vector( x, 4, 2, 1 ) ] ); + t.strictEqual( v, 2.5, 'returns expected value' ); + + t.end(); +}); From dd41c5c319af89709de43afec675e4df447908cf Mon Sep 17 00:00:00 2001 From: Sachin Pangal Date: Fri, 7 Nov 2025 20:38:46 +0530 Subject: [PATCH 2/4] Fix: update README, benchmark, and example files for mediansorted --- .../@stdlib/stats/base/ndarray/mediansorted/README.md | 3 +-- .../stats/base/ndarray/mediansorted/benchmark/benchmark.js | 4 +--- .../stats/base/ndarray/mediansorted/examples/index.js | 6 ++---- 3 files changed, 4 insertions(+), 9 deletions(-) diff --git a/lib/node_modules/@stdlib/stats/base/ndarray/mediansorted/README.md b/lib/node_modules/@stdlib/stats/base/ndarray/mediansorted/README.md index 4c030905fdde..d34132808b76 100644 --- a/lib/node_modules/@stdlib/stats/base/ndarray/mediansorted/README.md +++ b/lib/node_modules/@stdlib/stats/base/ndarray/mediansorted/README.md @@ -76,7 +76,6 @@ The function has the following parameters: ```javascript var discreteUniform = require( '@stdlib/random/array/discrete-uniform' ); -var sort = require( '@stdlib/array/base/sort2hp' ); var ndarray = require( '@stdlib/ndarray/base/ctor' ); var ndarray2array = require( '@stdlib/ndarray/to-array' ); var mediansorted = require( '@stdlib/stats/base/ndarray/mediansorted' ); @@ -86,7 +85,7 @@ var xbuf = discreteUniform( 10, -50, 50, { }); // Sort the array: -sort( xbuf, xbuf.length ); +xbuf.sort( ( a, b ) => a - b ); var x = new ndarray( 'generic', xbuf, [ xbuf.length ], [ 1 ], 0, 'row-major' ); console.log( ndarray2array( x ) ); diff --git a/lib/node_modules/@stdlib/stats/base/ndarray/mediansorted/benchmark/benchmark.js b/lib/node_modules/@stdlib/stats/base/ndarray/mediansorted/benchmark/benchmark.js index a3b576df9b1a..4cacf7938da0 100644 --- a/lib/node_modules/@stdlib/stats/base/ndarray/mediansorted/benchmark/benchmark.js +++ b/lib/node_modules/@stdlib/stats/base/ndarray/mediansorted/benchmark/benchmark.js @@ -50,9 +50,7 @@ function createBenchmark( len ) { var x; xbuf = uniform( len, -10.0, 10.0, options ); - xbuf.sort( function compare( a, b ) { - return a - b; - }); + xbuf.sort( ( a, b ) => a - b ); x = new ndarray( options.dtype, xbuf, [ len ], [ 1 ], 0, 'row-major' ); return benchmark; diff --git a/lib/node_modules/@stdlib/stats/base/ndarray/mediansorted/examples/index.js b/lib/node_modules/@stdlib/stats/base/ndarray/mediansorted/examples/index.js index 33c52a5bdde3..6dd18d5fcc09 100644 --- a/lib/node_modules/@stdlib/stats/base/ndarray/mediansorted/examples/index.js +++ b/lib/node_modules/@stdlib/stats/base/ndarray/mediansorted/examples/index.js @@ -27,10 +27,8 @@ var xbuf = uniform( 10, -10.0, 10.0, { 'dtype': 'generic' }); -// Sort the array: -xbuf.sort( function compare( a, b ) { - return a - b; -}); +// Sort the array (using arrow function for ESLint compliance): +xbuf.sort( ( a, b ) => a - b ); // Create an ndarray: var x = new ndarray( 'generic', xbuf, [ 10 ], [ 1 ], 0, 'row-major' ); From d822ad0cb518c853a6a030576445f5767e24ed96 Mon Sep 17 00:00:00 2001 From: Sachin Pangal Date: Fri, 7 Nov 2025 20:44:36 +0530 Subject: [PATCH 3/4] Update: revise README, benchmark, and examples for mediansorted --- .../stats/base/ndarray/mediansorted/README.md | 7 ++++++- .../mediansorted/benchmark/benchmark.js | 18 +++++++++++++++--- .../ndarray/mediansorted/examples/index.js | 8 +------- 3 files changed, 22 insertions(+), 11 deletions(-) diff --git a/lib/node_modules/@stdlib/stats/base/ndarray/mediansorted/README.md b/lib/node_modules/@stdlib/stats/base/ndarray/mediansorted/README.md index d34132808b76..7435ba318d77 100644 --- a/lib/node_modules/@stdlib/stats/base/ndarray/mediansorted/README.md +++ b/lib/node_modules/@stdlib/stats/base/ndarray/mediansorted/README.md @@ -80,12 +80,17 @@ var ndarray = require( '@stdlib/ndarray/base/ctor' ); var ndarray2array = require( '@stdlib/ndarray/to-array' ); var mediansorted = require( '@stdlib/stats/base/ndarray/mediansorted' ); +// Define a comparison function for sorting in ascending order: +function ascending( a, b ) { + return a - b; +} + var xbuf = discreteUniform( 10, -50, 50, { 'dtype': 'generic' }); // Sort the array: -xbuf.sort( ( a, b ) => a - b ); +xbuf.sort( ascending ); var x = new ndarray( 'generic', xbuf, [ xbuf.length ], [ 1 ], 0, 'row-major' ); console.log( ndarray2array( x ) ); diff --git a/lib/node_modules/@stdlib/stats/base/ndarray/mediansorted/benchmark/benchmark.js b/lib/node_modules/@stdlib/stats/base/ndarray/mediansorted/benchmark/benchmark.js index 4cacf7938da0..fe15480da6f8 100644 --- a/lib/node_modules/@stdlib/stats/base/ndarray/mediansorted/benchmark/benchmark.js +++ b/lib/node_modules/@stdlib/stats/base/ndarray/mediansorted/benchmark/benchmark.js @@ -21,7 +21,7 @@ // MODULES // var bench = require( '@stdlib/bench' ); -var uniform = require( '@stdlib/random/array/uniform' ); +var discreteUniform = require( '@stdlib/random/array/discrete-uniform' ); var isnan = require( '@stdlib/math/base/assert/is-nan' ); var pow = require( '@stdlib/math/base/special/pow' ); var ndarray = require( '@stdlib/ndarray/base/ctor' ); @@ -38,6 +38,18 @@ var options = { // FUNCTIONS // +/** +* Comparison function for sorting in ascending order. +* +* @private +* @param {number} a - first value +* @param {number} b - second value +* @returns {number} difference +*/ +function ascending( a, b ) { + return a - b; +} + /** * Creates a benchmark function. * @@ -49,8 +61,8 @@ function createBenchmark( len ) { var xbuf; var x; - xbuf = uniform( len, -10.0, 10.0, options ); - xbuf.sort( ( a, b ) => a - b ); + xbuf = discreteUniform( len, -100, 100, options ); + xbuf.sort( ascending ); x = new ndarray( options.dtype, xbuf, [ len ], [ 1 ], 0, 'row-major' ); return benchmark; diff --git a/lib/node_modules/@stdlib/stats/base/ndarray/mediansorted/examples/index.js b/lib/node_modules/@stdlib/stats/base/ndarray/mediansorted/examples/index.js index 6dd18d5fcc09..f38641964f29 100644 --- a/lib/node_modules/@stdlib/stats/base/ndarray/mediansorted/examples/index.js +++ b/lib/node_modules/@stdlib/stats/base/ndarray/mediansorted/examples/index.js @@ -18,17 +18,11 @@ 'use strict'; -var uniform = require( '@stdlib/random/array/uniform' ); var ndarray = require( '@stdlib/ndarray/base/ctor' ); var mediansorted = require( './../lib' ); // Create a sorted array: -var xbuf = uniform( 10, -10.0, 10.0, { - 'dtype': 'generic' -}); - -// Sort the array (using arrow function for ESLint compliance): -xbuf.sort( ( a, b ) => a - b ); +var xbuf = [ -3.0, -1.0, 0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0 ]; // Create an ndarray: var x = new ndarray( 'generic', xbuf, [ 10 ], [ 1 ], 0, 'row-major' ); From 988f7858e3799cfbc22561dc9892f7784db02fa2 Mon Sep 17 00:00:00 2001 From: Sachin Pangal Date: Fri, 7 Nov 2025 23:38:29 +0530 Subject: [PATCH 4/4] updated test cases --- .../stats/base/ndarray/mediansorted/README.md | 17 ++---- .../mediansorted/benchmark/benchmark.js | 17 +----- .../ndarray/mediansorted/examples/index.js | 15 +++-- .../base/ndarray/mediansorted/test/test.js | 56 ++++++++++++++----- 4 files changed, 55 insertions(+), 50 deletions(-) diff --git a/lib/node_modules/@stdlib/stats/base/ndarray/mediansorted/README.md b/lib/node_modules/@stdlib/stats/base/ndarray/mediansorted/README.md index 7435ba318d77..41fad23d163b 100644 --- a/lib/node_modules/@stdlib/stats/base/ndarray/mediansorted/README.md +++ b/lib/node_modules/@stdlib/stats/base/ndarray/mediansorted/README.md @@ -72,25 +72,16 @@ The function has the following parameters: ## Examples - + ```javascript -var discreteUniform = require( '@stdlib/random/array/discrete-uniform' ); +var linspace = require( '@stdlib/array/linspace' ); var ndarray = require( '@stdlib/ndarray/base/ctor' ); var ndarray2array = require( '@stdlib/ndarray/to-array' ); var mediansorted = require( '@stdlib/stats/base/ndarray/mediansorted' ); -// Define a comparison function for sorting in ascending order: -function ascending( a, b ) { - return a - b; -} - -var xbuf = discreteUniform( 10, -50, 50, { - 'dtype': 'generic' -}); - -// Sort the array: -xbuf.sort( ascending ); +// Create a linearly spaced sorted array: +var xbuf = linspace( 0.0, 10.0, 11 ); var x = new ndarray( 'generic', xbuf, [ xbuf.length ], [ 1 ], 0, 'row-major' ); console.log( ndarray2array( x ) ); diff --git a/lib/node_modules/@stdlib/stats/base/ndarray/mediansorted/benchmark/benchmark.js b/lib/node_modules/@stdlib/stats/base/ndarray/mediansorted/benchmark/benchmark.js index fe15480da6f8..4235afe6c3f7 100644 --- a/lib/node_modules/@stdlib/stats/base/ndarray/mediansorted/benchmark/benchmark.js +++ b/lib/node_modules/@stdlib/stats/base/ndarray/mediansorted/benchmark/benchmark.js @@ -21,7 +21,7 @@ // MODULES // var bench = require( '@stdlib/bench' ); -var discreteUniform = require( '@stdlib/random/array/discrete-uniform' ); +var linspace = require( '@stdlib/array/linspace' ); var isnan = require( '@stdlib/math/base/assert/is-nan' ); var pow = require( '@stdlib/math/base/special/pow' ); var ndarray = require( '@stdlib/ndarray/base/ctor' ); @@ -38,18 +38,6 @@ var options = { // FUNCTIONS // -/** -* Comparison function for sorting in ascending order. -* -* @private -* @param {number} a - first value -* @param {number} b - second value -* @returns {number} difference -*/ -function ascending( a, b ) { - return a - b; -} - /** * Creates a benchmark function. * @@ -61,8 +49,7 @@ function createBenchmark( len ) { var xbuf; var x; - xbuf = discreteUniform( len, -100, 100, options ); - xbuf.sort( ascending ); + xbuf = linspace( 0.0, 100.0, len ); x = new ndarray( options.dtype, xbuf, [ len ], [ 1 ], 0, 'row-major' ); return benchmark; diff --git a/lib/node_modules/@stdlib/stats/base/ndarray/mediansorted/examples/index.js b/lib/node_modules/@stdlib/stats/base/ndarray/mediansorted/examples/index.js index f38641964f29..68b4a38a7f2c 100644 --- a/lib/node_modules/@stdlib/stats/base/ndarray/mediansorted/examples/index.js +++ b/lib/node_modules/@stdlib/stats/base/ndarray/mediansorted/examples/index.js @@ -18,17 +18,16 @@ 'use strict'; +var linspace = require( '@stdlib/array/linspace' ); var ndarray = require( '@stdlib/ndarray/base/ctor' ); +var ndarray2array = require( '@stdlib/ndarray/to-array' ); var mediansorted = require( './../lib' ); -// Create a sorted array: -var xbuf = [ -3.0, -1.0, 0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0 ]; +// Create a linearly spaced sorted array: +var xbuf = linspace( 0.0, 10.0, 11 ); -// Create an ndarray: -var x = new ndarray( 'generic', xbuf, [ 10 ], [ 1 ], 0, 'row-major' ); +var x = new ndarray( 'generic', xbuf, [ xbuf.length ], [ 1 ], 0, 'row-major' ); +console.log( ndarray2array( x ) ); -// Compute the median: var v = mediansorted( [ x ] ); - -console.log( 'x: %s', x.data ); -console.log( 'median: %d', v ); +console.log( v ); diff --git a/lib/node_modules/@stdlib/stats/base/ndarray/mediansorted/test/test.js b/lib/node_modules/@stdlib/stats/base/ndarray/mediansorted/test/test.js index 7499afa0bb2a..a19ea9479dc0 100644 --- a/lib/node_modules/@stdlib/stats/base/ndarray/mediansorted/test/test.js +++ b/lib/node_modules/@stdlib/stats/base/ndarray/mediansorted/test/test.js @@ -60,21 +60,37 @@ tape( 'the function calculates the median value of a sorted one-dimensional ndar var x; var v; - x = [ 1.0, 2.0, 3.0 ]; - v = mediansorted( [ vector( x, 3, 1, 0 ) ] ); - t.strictEqual( v, 2.0, 'returns expected value' ); + x = [ -3.0, -2.0, -1.0, 1.0, 2.0, 3.0 ]; + v = mediansorted( [ vector( x, 6, 1, 0 ) ] ); + t.strictEqual( v, 0.0, 'returns expected value' ); - x = [ 3.0, 2.0, 1.0 ]; - v = mediansorted( [ vector( x, 3, 1, 0 ) ] ); - t.strictEqual( v, 2.0, 'returns expected value' ); + x = [ 3.0, 2.0, 1.0, 0.0, -1.0, -2.0, -3.0 ]; + v = mediansorted( [ vector( x, 7, 1, 0 ) ] ); + t.strictEqual( v, 0.0, 'returns expected value' ); - x = [ 1.0, 2.0, 3.0, 4.0 ]; - v = mediansorted( [ vector( x, 4, 1, 0 ) ] ); - t.strictEqual( v, 2.5, 'returns expected value' ); + x = [ -4.0, -5.0 ]; + v = mediansorted( [ vector( x, 2, 1, 0 ) ] ); + t.strictEqual( v, -4.5, 'returns expected value' ); - x = [ 4.0, 3.0, 2.0, 1.0 ]; - v = mediansorted( [ vector( x, 4, 1, 0 ) ] ); - t.strictEqual( v, 2.5, 'returns expected value' ); + x = [ -4.0, -4.0 ]; + v = mediansorted( [ vector( x, 2, 1, 0 ) ] ); + t.strictEqual( v, -4.0, 'returns expected value' ); + + x = [ NaN ]; + v = mediansorted( [ vector( x, 1, 1, 0 ) ] ); + t.strictEqual( isnan( v ), true, 'returns expected value' ); + + x = [ NaN, NaN ]; + v = mediansorted( [ vector( x, 2, 1, 0 ) ] ); + t.strictEqual( isnan( v ), true, 'returns expected value' ); + + x = [ 5.0, NaN ]; + v = mediansorted( [ vector( x, 2, 1, 0 ) ] ); + t.strictEqual( isnan( v ), true, 'returns expected value' ); + + x = [ NaN, 5.0 ]; + v = mediansorted( [ vector( x, 2, 1, 0 ) ] ); + t.strictEqual( isnan( v ), true, 'returns expected value' ); t.end(); }); @@ -133,7 +149,7 @@ tape( 'the function supports one-dimensional ndarrays having negative strides', 2.0, 2.0, // 2 -7.0, - 2.0, // 1 + 3.0, // 1 3.0, 4.0, // 0 2.0 @@ -141,7 +157,19 @@ tape( 'the function supports one-dimensional ndarrays having negative strides', v = mediansorted( [ vector( x, 4, -2, 6 ) ] ); - t.strictEqual( v, 2.0, 'returns expected value' ); + t.strictEqual( v, 2.5, 'returns expected value' ); + t.end(); +}); + +tape( 'if provided a stride equal to `0`, the function returns the first indexed element', function test( t ) { + var x; + var v; + + x = [ 1.0, -2.0, -4.0, -5.0, -6.0 ]; + + v = mediansorted( [ vector( x, 5, 0, 0 ) ] ); + t.strictEqual( v, 1.0, 'returns expected value' ); + t.end(); });