diff --git a/lib/node_modules/@stdlib/stats/base/maxsorted/README.md b/lib/node_modules/@stdlib/stats/base/maxsorted/README.md index dc558bdd6905..63a4fedd4da4 100644 --- a/lib/node_modules/@stdlib/stats/base/maxsorted/README.md +++ b/lib/node_modules/@stdlib/stats/base/maxsorted/README.md @@ -36,7 +36,7 @@ limitations under the License. var maxsorted = require( '@stdlib/stats/base/maxsorted' ); ``` -#### maxsorted( N, x, stride ) +#### maxsorted( N, x, strideX ) Computes the maximum value of a sorted strided array `x`. @@ -58,17 +58,14 @@ The function has the following parameters: - **N**: number of indexed elements. - **x**: sorted 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 maximum value 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 maximum value 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, 3.0, 3.0, 4.0, 2.0 ]; -var N = floor( x.length / 2 ); -var v = maxsorted( N, x, 2 ); +var v = maxsorted( 4, x, 2 ); // returns 4.0 ``` @@ -78,42 +75,35 @@ Note that indexing is relative to the first index. To introduce an offset, use [ ```javascript var Float64Array = require( '@stdlib/array/float64' ); -var floor = require( '@stdlib/math/base/special/floor' ); var x0 = new Float64Array( [ 2.0, 1.0, 2.0, 2.0, -2.0, 2.0, 3.0, 4.0 ] ); var x1 = new Float64Array( x0.buffer, x0.BYTES_PER_ELEMENT*1 ); // start at 2nd element -var N = floor( x0.length / 2 ); - -var v = maxsorted( N, x1, 2 ); +var v = maxsorted( 4, x1, 2 ); // returns 4.0 ``` -#### maxsorted.ndarray( N, x, stride, offset ) +#### maxsorted.ndarray( N, x, strideX, offsetX ) Computes the maximum value of a sorted strided array using alternative indexing semantics. ```javascript var x = [ 1.0, 2.0, 3.0 ]; -var N = x.length; -var v = maxsorted.ndarray( N, x, 1, 0 ); +var v = maxsorted.ndarray( x.length, x, 1, 0 ); // returns 3.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 maximum value 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 maximum value for every other value in `x` starting from the second value ```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 = maxsorted.ndarray( N, x, 2, 1 ); +var v = maxsorted.ndarray( 4, x, 2, 1 ); // returns 4.0 ``` @@ -127,6 +117,7 @@ var v = maxsorted.ndarray( N, x, 2, 1 ); - If `N <= 0`, both functions return `NaN`. - The input strided array must be sorted in either **strictly** ascending or descending order. +- 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]). @@ -139,16 +130,10 @@ var v = maxsorted.ndarray( N, x, 2, 1 ); ```javascript -var Float64Array = require( '@stdlib/array/float64' ); +var linspace = require( '@stdlib/array/base/linspace' ); var maxsorted = require( '@stdlib/stats/base/maxsorted' ); -var x; -var i; - -x = new Float64Array( 10 ); -for ( i = 0; i < x.length; i++ ) { - x[ i ] = i - 5.0; -} +var x = linspace( -50.0, 50.0, 10 ); console.log( x ); var v = maxsorted( x.length, x, 1 ); @@ -184,6 +169,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/dmaxsorted]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/strided/dmaxsorted diff --git a/lib/node_modules/@stdlib/stats/base/maxsorted/benchmark/benchmark.js b/lib/node_modules/@stdlib/stats/base/maxsorted/benchmark/benchmark.js index 438ad4798acf..7cf4f71ec966 100644 --- a/lib/node_modules/@stdlib/stats/base/maxsorted/benchmark/benchmark.js +++ b/lib/node_modules/@stdlib/stats/base/maxsorted/benchmark/benchmark.js @@ -22,6 +22,7 @@ var bench = require( '@stdlib/bench' ); var isnan = require( '@stdlib/math/base/assert/is-nan' ); +var linspace = require( '@stdlib/array/base/linspace' ); var pow = require( '@stdlib/math/base/special/pow' ); var pkg = require( './../package.json' ).name; var maxsorted = require( './../lib/maxsorted.js' ); @@ -37,13 +38,7 @@ var maxsorted = require( './../lib/maxsorted.js' ); * @returns {Function} benchmark function */ function createBenchmark( len ) { - var x; - var i; - - x = []; - for ( i = 0; i < len; i++ ) { - x.push( i ); - } + var x = linspace( 0.0, len, len ); return benchmark; function benchmark( b ) { diff --git a/lib/node_modules/@stdlib/stats/base/maxsorted/benchmark/benchmark.ndarray.js b/lib/node_modules/@stdlib/stats/base/maxsorted/benchmark/benchmark.ndarray.js index 24dd5bcfc454..45ade48501fb 100644 --- a/lib/node_modules/@stdlib/stats/base/maxsorted/benchmark/benchmark.ndarray.js +++ b/lib/node_modules/@stdlib/stats/base/maxsorted/benchmark/benchmark.ndarray.js @@ -21,8 +21,9 @@ // MODULES // var bench = require( '@stdlib/bench' ); -var isnan = require( '@stdlib/math/base/assert/is-nan' ); var pow = require( '@stdlib/math/base/special/pow' ); +var linspace = require( '@stdlib/array/base/linspace' ); +var isnan = require( '@stdlib/math/base/assert/is-nan' ); var pkg = require( './../package.json' ).name; var maxsorted = require( './../lib/ndarray.js' ); @@ -37,13 +38,7 @@ var maxsorted = require( './../lib/ndarray.js' ); * @returns {Function} benchmark function */ function createBenchmark( len ) { - var x; - var i; - - x = []; - for ( i = 0; i < len; i++ ) { - x.push( i ); - } + var x = linspace( 0.0, len, len ); return benchmark; function benchmark( b ) { diff --git a/lib/node_modules/@stdlib/stats/base/maxsorted/docs/repl.txt b/lib/node_modules/@stdlib/stats/base/maxsorted/docs/repl.txt index e849db00a187..e927eebd94a2 100644 --- a/lib/node_modules/@stdlib/stats/base/maxsorted/docs/repl.txt +++ b/lib/node_modules/@stdlib/stats/base/maxsorted/docs/repl.txt @@ -1,12 +1,12 @@ -{{alias}}( N, x, stride ) +{{alias}}( N, x, strideX ) Computes the maximum value of a sorted strided array. The input strided array must be sorted in either strictly ascending or descending order. - The `N` and `stride` parameters determine which elements in `x` are accessed - at runtime. + The `N` and stride parameters determine which elements in the strided array + are accessed at runtime. Indexing is relative to the first index. To introduce an offset, use a typed array view. @@ -21,8 +21,8 @@ x: Array|TypedArray Sorted input array. - stride: integer - Index increment. + strideX: integer + Stride length. Returns ------- @@ -36,22 +36,19 @@ > {{alias}}( x.length, x, 1 ) 3.0 - // 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, x, stride ) + > {{alias}}( 3, x, 2 ) 2.0 // Using view offsets: > var x0 = new {{alias:@stdlib/array/float64}}( [ 1.0, -2.0, 3.0, 2.0, 5.0, 3.0 ] ); > var x1 = new {{alias:@stdlib/array/float64}}( x0.buffer, x0.BYTES_PER_ELEMENT*1 ); - > N = {{alias:@stdlib/math/base/special/floor}}( x0.length / 2 ); - > stride = 2; - > {{alias}}( N, x1, stride ) + > {{alias}}( 3, x1, 2 ) 3.0 -{{alias}}.ndarray( N, x, stride, offset ) + +{{alias}}.ndarray( N, x, strideX, offsetX ) Computes the maximum value of a sorted strided array using alternative indexing semantics. @@ -67,10 +64,10 @@ x: Array|TypedArray Sorted input array. - stride: integer - Index increment. + strideX: integer + Stride length. - offset: integer + offsetX: integer Starting index. Returns @@ -87,8 +84,7 @@ // Using offset parameter: > var x = [ 1.0, -2.0, 3.0, 2.0, 5.0, 3.0 ]; - > var N = {{alias:@stdlib/math/base/special/floor}}( x.length / 2 ); - > {{alias}}.ndarray( N, x, 2, 1 ) + > {{alias}}.ndarray( 3, x, 2, 1 ) 3.0 See Also diff --git a/lib/node_modules/@stdlib/stats/base/maxsorted/docs/types/index.d.ts b/lib/node_modules/@stdlib/stats/base/maxsorted/docs/types/index.d.ts index c6f625ee2d3f..70dfefebe2ea 100644 --- a/lib/node_modules/@stdlib/stats/base/maxsorted/docs/types/index.d.ts +++ b/lib/node_modules/@stdlib/stats/base/maxsorted/docs/types/index.d.ts @@ -20,7 +20,12 @@ /// -import { NumericArray } from '@stdlib/types/array'; +import { NumericArray, Collection, AccessorArrayLike } from '@stdlib/types/array'; + +/** +* Input array. +*/ +type InputArray = NumericArray | Collection | AccessorArrayLike; /** * Interface describing `maxsorted`. @@ -31,7 +36,7 @@ interface Routine { * * @param N - number of indexed elements * @param x - sorted input array - * @param stride - stride length + * @param strideX - stride length * @returns maximum value * * @example @@ -40,15 +45,15 @@ interface Routine { * var v = maxsorted( x.length, x, 1 ); * // returns 3.0 */ - ( N: number, x: NumericArray, stride: number ): number; + ( N: number, x: InputArray, strideX: number ): number; /** * Computes the maximum value of a sorted strided array using alternative indexing semantics. * * @param N - number of indexed elements * @param x - sorted input array - * @param stride - stride length - * @param offset - starting index + * @param strideX - stride length + * @param offsetX - starting index * @returns maximum value * * @example @@ -57,7 +62,7 @@ interface Routine { * var v = maxsorted.ndarray( x.length, x, 1, 0 ); * // returns 3.0 */ - ndarray( N: number, x: NumericArray, stride: number, offset: number ): number; + ndarray( N: number, x: InputArray, strideX: number, offsetX: number ): number; } /** @@ -65,7 +70,7 @@ interface Routine { * * @param N - number of indexed elements * @param x - sorted input array -* @param stride - stride length +* @param strideX - stride length * @returns maximum value * * @example diff --git a/lib/node_modules/@stdlib/stats/base/maxsorted/docs/types/test.ts b/lib/node_modules/@stdlib/stats/base/maxsorted/docs/types/test.ts index b217080722fd..962f88763d38 100644 --- a/lib/node_modules/@stdlib/stats/base/maxsorted/docs/types/test.ts +++ b/lib/node_modules/@stdlib/stats/base/maxsorted/docs/types/test.ts @@ -16,6 +16,7 @@ * limitations under the License. */ +import AccessorArray = require( '@stdlib/array/base/accessor' ); import maxsorted = require( './index' ); @@ -26,6 +27,7 @@ import maxsorted = require( './index' ); const x = new Float64Array( 10 ); maxsorted( x.length, x, 1 ); // $ExpectType number + maxsorted( x.length, new AccessorArray( x ), 1 ); // $ExpectType number } // The compiler throws an error if the function is provided a first argument which is not a number... @@ -86,6 +88,7 @@ import maxsorted = require( './index' ); const x = new Float64Array( 10 ); maxsorted.ndarray( x.length, x, 1, 0 ); // $ExpectType number + maxsorted.ndarray( x.length, new AccessorArray( x ), 1, 0 ); // $ExpectType number } // The compiler throws an error if the `ndarray` method is provided a first argument which is not a number... diff --git a/lib/node_modules/@stdlib/stats/base/maxsorted/examples/index.js b/lib/node_modules/@stdlib/stats/base/maxsorted/examples/index.js index 979a90bd9e8e..95c41b16228c 100644 --- a/lib/node_modules/@stdlib/stats/base/maxsorted/examples/index.js +++ b/lib/node_modules/@stdlib/stats/base/maxsorted/examples/index.js @@ -18,16 +18,10 @@ 'use strict'; -var Float64Array = require( '@stdlib/array/float64' ); +var linspace = require( '@stdlib/array/base/linspace' ); var maxsorted = require( './../lib' ); -var x; -var i; - -x = new Float64Array( 10 ); -for ( i = 0; i < x.length; i++ ) { - x[ i ] = i - 5.0; -} +var x = linspace( -50.0, 50.0, 10 ); console.log( x ); var v = maxsorted( x.length, x, 1 ); diff --git a/lib/node_modules/@stdlib/stats/base/maxsorted/lib/accessors.js b/lib/node_modules/@stdlib/stats/base/maxsorted/lib/accessors.js new file mode 100644 index 000000000000..20c8ba1cc2d0 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/base/maxsorted/lib/accessors.js @@ -0,0 +1,88 @@ +/** +* @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 isPositiveZero = require( '@stdlib/math/base/assert/is-positive-zero' ); + + +// MAIN // + +/** +* Computes the maximum value of a sorted strided array. +* +* @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 for `x` +* @param {NonNegativeInteger} offsetX - starting index for `x` +* @returns {number} output maximum value +* +* @example +* var toAccessorArray = require( '@stdlib/array/base/to-accessor-array' ); +* var arraylike2object = require( '@stdlib/array/base/arraylike2object' ); +* +* var x = [ 2.0, 1.0, 2.0, -2.0, -2.0, 2.0, 3.0, 4.0 ]; +* +* var v = maxsorted( 4, arraylike2object( toAccessorArray( x ) ), 2, 1 ); +* // returns 4.0 +*/ +function maxsorted( N, x, strideX, offsetX ) { + var xbuf; + var xget; + var v1; + var v2; + + // Cache references to array data: + xbuf = x.data; + + // Cache references to element accessors: + xget = x.accessors[ 0 ]; + + if ( N <= 0 ) { + return NaN; + } + if ( N === 1 || strideX === 0 ) { + return xget( xbuf, 0 ); + } + v1 = xget( xbuf, offsetX ); + v2 = xget( xbuf, offsetX + ((N-1)*strideX) ); + if ( isnan( v1 ) || isnan( v2 ) ) { + return NaN; + } + if ( v1 === v2 ) { + if ( isPositiveZero( v1 ) || isPositiveZero( v2 ) ) { + return 0.0; + } + return v1; + } + if ( v1 > v2 ) { + return v1; + } + return v2; +} + + +// EXPORTS // + +module.exports = maxsorted; diff --git a/lib/node_modules/@stdlib/stats/base/maxsorted/lib/maxsorted.js b/lib/node_modules/@stdlib/stats/base/maxsorted/lib/maxsorted.js index f3fb20a39937..78ae62180344 100644 --- a/lib/node_modules/@stdlib/stats/base/maxsorted/lib/maxsorted.js +++ b/lib/node_modules/@stdlib/stats/base/maxsorted/lib/maxsorted.js @@ -20,8 +20,8 @@ // MODULES // -var isnan = require( '@stdlib/math/base/assert/is-nan' ); -var isPositiveZero = require( '@stdlib/math/base/assert/is-positive-zero' ); +var stride2offset = require( '@stdlib/strided/base/stride2offset' ); +var ndarray = require( './ndarray.js' ); // MAIN // @@ -31,7 +31,7 @@ var isPositiveZero = require( '@stdlib/math/base/assert/is-positive-zero' ); * * @param {PositiveInteger} N - number of indexed elements * @param {NumericArray} x - sorted input array -* @param {integer} stride - stride length +* @param {integer} strideX - stride length * @returns {number} maximum value * * @example @@ -41,36 +41,8 @@ var isPositiveZero = require( '@stdlib/math/base/assert/is-positive-zero' ); * var v = maxsorted( N, x, 1 ); * // returns 3.0 */ -function maxsorted( N, x, stride ) { - var v1; - var v2; - - if ( N <= 0 ) { - return NaN; - } - if ( N === 1 || stride === 0 ) { - return x[ 0 ]; - } - if ( stride < 0 ) { - v1 = x[ (1-N) * stride ]; - v2 = x[ 0 ]; - } else { - v1 = x[ 0 ]; - v2 = x[ (N-1) * stride ]; - } - if ( isnan( v1 ) || isnan( v2 ) ) { - return NaN; - } - if ( v1 === v2 ) { - if ( isPositiveZero( v1 ) || isPositiveZero( v2 ) ) { - return 0.0; - } - return v1; - } - if ( v1 > v2 ) { - return v1; - } - return v2; +function maxsorted( N, x, strideX ) { + return ndarray( N, x, strideX, stride2offset( N, strideX ) ); } diff --git a/lib/node_modules/@stdlib/stats/base/maxsorted/lib/ndarray.js b/lib/node_modules/@stdlib/stats/base/maxsorted/lib/ndarray.js index e234814aed3f..f513c29f2ab6 100644 --- a/lib/node_modules/@stdlib/stats/base/maxsorted/lib/ndarray.js +++ b/lib/node_modules/@stdlib/stats/base/maxsorted/lib/ndarray.js @@ -22,6 +22,8 @@ var isnan = require( '@stdlib/math/base/assert/is-nan' ); var isPositiveZero = require( '@stdlib/math/base/assert/is-positive-zero' ); +var arraylike2object = require( '@stdlib/array/base/arraylike2object' ); +var accessors = require( './accessors.js' ); // MAIN // @@ -31,31 +33,33 @@ var isPositiveZero = require( '@stdlib/math/base/assert/is-positive-zero' ); * * @param {PositiveInteger} N - number of indexed elements * @param {NumericArray} x - sorted input array -* @param {integer} stride - stride length -* @param {NonNegativeInteger} offset - starting index +* @param {integer} strideX - stride length +* @param {NonNegativeInteger} offsetX - starting index * @returns {number} maximum value * * @example -* var floor = require( '@stdlib/math/base/special/floor' ); -* * var x = [ 2.0, -3.0, 2.0, -2.0, -2.0, 2.0, 3.0, 4.0 ]; -* var N = floor( x.length / 2 ); * -* var v = maxsorted( N, x, 2, 1 ); +* var v = maxsorted( 4, x, 2, 1 ); * // returns 4.0 */ -function maxsorted( N, x, stride, offset ) { +function maxsorted( N, x, strideX, offsetX ) { var v1; var v2; + var o; if ( N <= 0 ) { return NaN; } - if ( N === 1 || stride === 0 ) { + o = arraylike2object( x ); + if ( o.accessorProtocol ) { + return accessors( N, o, strideX, offsetX ); + } + if ( N === 1 || strideX === 0 ) { return x[ 0 ]; } - v1 = x[ offset ]; - v2 = x[ offset + ((N-1)*stride) ]; + v1 = x[ offsetX ]; + v2 = x[ offsetX + ((N-1)*strideX) ]; if ( isnan( v1 ) || isnan( v2 ) ) { return NaN; } diff --git a/lib/node_modules/@stdlib/stats/base/maxsorted/test/test.maxsorted.js b/lib/node_modules/@stdlib/stats/base/maxsorted/test/test.maxsorted.js index 798e964757f3..62e0f9928a90 100644 --- a/lib/node_modules/@stdlib/stats/base/maxsorted/test/test.maxsorted.js +++ b/lib/node_modules/@stdlib/stats/base/maxsorted/test/test.maxsorted.js @@ -21,9 +21,9 @@ // MODULES // var tape = require( 'tape' ); -var floor = require( '@stdlib/math/base/special/floor' ); var isnan = require( '@stdlib/math/base/assert/is-nan' ); var isPositiveZero = require( '@stdlib/math/base/assert/is-positive-zero' ); +var toAccessorArray = require( '@stdlib/array/base/to-accessor-array' ); var Float64Array = require( '@stdlib/array/float64' ); var maxsorted = require( './../lib/maxsorted.js' ); @@ -88,6 +88,53 @@ tape( 'the function calculates the maximum value of a sorted strided array', fun t.end(); }); +tape( 'the function calculates the maximum value of a sorted strided array (accessor)', function test( t ) { + var x; + var v; + + x = [ -3.0, -2.0, -1.0, 1.0, 2.0, 3.0 ]; + v = maxsorted( x.length, toAccessorArray( x ), 1 ); + t.strictEqual( v, 3.0, 'returns expected value' ); + + x = [ 3.0, 2.0, 1.0, -1.0, -2.0, -3.0 ]; + v = maxsorted( x.length, toAccessorArray( x ), 1 ); + t.strictEqual( v, 3.0, 'returns expected value' ); + + x = [ -4.0, -5.0 ]; + v = maxsorted( x.length, toAccessorArray( x ), 1 ); + t.strictEqual( v, -4.0, 'returns expected value' ); + + x = [ -4.0, -4.0 ]; + v = maxsorted( x.length, toAccessorArray( x ), 1 ); + t.strictEqual( v, -4.0, 'returns expected value' ); + + x = [ -0.0, -0.0, 0.0 ]; + v = maxsorted( x.length, toAccessorArray( x ), 1 ); + t.strictEqual( isPositiveZero( v ), true, 'returns expected value' ); + + x = [ 0.0, -0.0, -0.0 ]; + v = maxsorted( x.length, toAccessorArray( x ), 1 ); + t.strictEqual( isPositiveZero( v ), true, 'returns expected value' ); + + x = [ NaN ]; + v = maxsorted( x.length, toAccessorArray( x ), 1 ); + t.strictEqual( isnan( v ), true, 'returns expected value' ); + + x = [ NaN, NaN ]; + v = maxsorted( x.length, toAccessorArray( x ), 1 ); + t.strictEqual( isnan( v ), true, 'returns expected value' ); + + x = [ 5.0, NaN ]; + v = maxsorted( x.length, toAccessorArray( x ), 1 ); + t.strictEqual( isnan( v ), true, 'returns expected value' ); + + x = [ NaN, 5.0 ]; + v = maxsorted( x.length, toAccessorArray( x ), 1 ); + t.strictEqual( isnan( v ), true, 'returns expected value' ); + + t.end(); +}); + tape( 'if provided an `N` parameter less than or equal to `0`, the function returns `NaN`', function test( t ) { var x; var v; @@ -116,7 +163,6 @@ tape( 'if provided an `N` parameter equal to `1`, the function returns the first }); tape( 'the function supports a `stride` parameter', function test( t ) { - var N; var x; var v; @@ -131,15 +177,34 @@ tape( 'the function supports a `stride` parameter', function test( t ) { 2.0 ]; - N = floor( x.length / 2 ); - v = maxsorted( N, x, 2 ); + v = maxsorted( 4, x, 2 ); + + t.strictEqual( v, 4.0, 'returns expected value' ); + t.end(); +}); + +tape( 'the function supports a `stride` parameter (accessor)', 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 = maxsorted( 4, toAccessorArray( x ), 2 ); t.strictEqual( v, 4.0, 'returns expected value' ); t.end(); }); tape( 'the function supports a negative `stride` parameter', function test( t ) { - var N; var x; var v; @@ -154,8 +219,28 @@ tape( 'the function supports a negative `stride` parameter', function test( t ) 2.0 ]; - N = floor( x.length / 2 ); - v = maxsorted( N, x, -2 ); + v = maxsorted( 4, x, -2 ); + + t.strictEqual( v, 4.0, 'returns expected value' ); + t.end(); +}); + +tape( 'the function supports a negative `stride` parameter (accessor)', function test( t ) { + var x; + var v; + + x = [ + 1.0, // 3 + 2.0, + 2.0, // 2 + -7.0, + 3.0, // 1 + 3.0, + 4.0, // 0 + 2.0 + ]; + + v = maxsorted( 4, toAccessorArray( x ), -2 ); t.strictEqual( v, 4.0, 'returns expected value' ); t.end(); @@ -173,10 +258,21 @@ tape( 'if provided a `stride` parameter equal to `0`, the function returns the f t.end(); }); +tape( 'if provided a `stride` parameter equal to `0`, the function returns the first element (accessor)', function test( t ) { + var x; + var v; + + x = [ 1.0, -2.0, -4.0, -5.0, -6.0 ]; + + v = maxsorted( x.length, toAccessorArray( x ), 0 ); + t.strictEqual( v, 1.0, 'returns expected value' ); + + t.end(); +}); + tape( 'the function supports view offsets', function test( t ) { var x0; var x1; - var N; var v; x0 = new Float64Array([ @@ -192,9 +288,33 @@ tape( 'the function supports view offsets', function test( t ) { ]); x1 = new Float64Array( x0.buffer, x0.BYTES_PER_ELEMENT*1 ); // start at 2nd element - N = floor(x1.length / 2); - v = maxsorted( N, x1, 2 ); + v = maxsorted( 4, x1, 2 ); + t.strictEqual( v, 4.0, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports view offsets (accessor)', function test( t ) { + var x0; + var x1; + var v; + + x0 = new Float64Array([ + 2.0, + 1.0, // 0 + 2.0, + 2.0, // 1 + -2.0, + 3.0, // 2 + 3.0, + 4.0, // 3 + 6.0 + ]); + + x1 = new Float64Array( x0.buffer, x0.BYTES_PER_ELEMENT*1 ); // start at 2nd element + + v = maxsorted( 4, toAccessorArray( x1 ), 2 ); t.strictEqual( v, 4.0, 'returns expected value' ); t.end(); diff --git a/lib/node_modules/@stdlib/stats/base/maxsorted/test/test.ndarray.js b/lib/node_modules/@stdlib/stats/base/maxsorted/test/test.ndarray.js index 512895960eeb..e168ed61fb37 100644 --- a/lib/node_modules/@stdlib/stats/base/maxsorted/test/test.ndarray.js +++ b/lib/node_modules/@stdlib/stats/base/maxsorted/test/test.ndarray.js @@ -21,8 +21,8 @@ // MODULES // var tape = require( 'tape' ); -var floor = require( '@stdlib/math/base/special/floor' ); var isnan = require( '@stdlib/math/base/assert/is-nan' ); +var toAccessorArray = require( '@stdlib/array/base/to-accessor-array' ); var isPositiveZero = require( '@stdlib/math/base/assert/is-positive-zero' ); var maxsorted = require( './../lib/ndarray.js' ); @@ -87,6 +87,53 @@ tape( 'the function calculates the maximum value of a sorted strided array', fun t.end(); }); +tape( 'the function calculates the maximum value of a sorted strided array (accessor)', function test( t ) { + var x; + var v; + + x = [ -3.0, -2.0, -1.0, 1.0, 2.0, 3.0 ]; + v = maxsorted( x.length, toAccessorArray( x ), 1, 0 ); + t.strictEqual( v, 3.0, 'returns expected value' ); + + x = [ 3.0, 2.0, 1, 0.0, -1.0, -2.0, -3.0 ]; + v = maxsorted( x.length, toAccessorArray( x ), 1, 0 ); + t.strictEqual( v, 3.0, 'returns expected value' ); + + x = [ -4.0, -5.0 ]; + v = maxsorted( x.length, toAccessorArray( x ), 1, 0 ); + t.strictEqual( v, -4.0, 'returns expected value' ); + + x = [ -4.0, -4.0 ]; + v = maxsorted( x.length, toAccessorArray( x ), 1, 0 ); + t.strictEqual( v, -4.0, 'returns expected value' ); + + x = [ -0.0, -0.0, 0.0 ]; + v = maxsorted( x.length, toAccessorArray( x ), 1, 0 ); + t.strictEqual( isPositiveZero( v ), true, 'returns expected value' ); + + x = [ 0.0, -0.0, -0.0 ]; + v = maxsorted( x.length, toAccessorArray( x ), 1, 0 ); + t.strictEqual( isPositiveZero( v ), true, 'returns expected value' ); + + x = [ NaN ]; + v = maxsorted( x.length, toAccessorArray( x ), 1, 0 ); + t.strictEqual( isnan( v ), true, 'returns expected value' ); + + x = [ NaN, NaN ]; + v = maxsorted( x.length, toAccessorArray( x ), 1, 0 ); + t.strictEqual( isnan( v ), true, 'returns expected value' ); + + x = [ 5.0, NaN ]; + v = maxsorted( x.length, toAccessorArray( x ), 1, 0 ); + t.strictEqual( isnan( v ), true, 'returns expected value' ); + + x = [ NaN, 5.0 ]; + v = maxsorted( x.length, toAccessorArray( x ), 1, 0 ); + t.strictEqual( isnan( v ), true, 'returns expected value' ); + + t.end(); +}); + tape( 'if provided an `N` parameter less than or equal to `0`, the function returns `NaN`', function test( t ) { var x; var v; @@ -115,7 +162,6 @@ tape( 'if provided an `N` parameter equal to `1`, the function returns the first }); tape( 'the function supports a `stride` parameter', function test( t ) { - var N; var x; var v; @@ -130,15 +176,34 @@ tape( 'the function supports a `stride` parameter', function test( t ) { 2.0 ]; - N = floor( x.length / 2 ); - v = maxsorted( N, x, 2, 0 ); + v = maxsorted( 4, x, 2, 0 ); + + t.strictEqual( v, 4.0, 'returns expected value' ); + t.end(); +}); + +tape( 'the function supports a `stride` parameter (accessor)', 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 = maxsorted( 4, toAccessorArray( x ), 2, 0 ); t.strictEqual( v, 4.0, 'returns expected value' ); t.end(); }); tape( 'the function supports a negative `stride` parameter', function test( t ) { - var N; var x; var v; @@ -153,8 +218,28 @@ tape( 'the function supports a negative `stride` parameter', function test( t ) 2.0 ]; - N = floor( x.length / 2 ); - v = maxsorted( N, x, -2, 6 ); + v = maxsorted( 4, x, -2, 6 ); + + t.strictEqual( v, 4.0, 'returns expected value' ); + t.end(); +}); + +tape( 'the function supports a negative `stride` parameter (accessor)', function test( t ) { + var x; + var v; + + x = [ + 1.0, // 3 + 2.0, + 2.0, // 2 + -7.0, + 3.0, // 1 + 3.0, + 4.0, // 0 + 2.0 + ]; + + v = maxsorted( 4, toAccessorArray( x ), -2, 6 ); t.strictEqual( v, 4.0, 'returns expected value' ); t.end(); @@ -172,8 +257,19 @@ tape( 'if provided a `stride` parameter equal to `0`, the function returns the f t.end(); }); +tape( 'if provided a `stride` parameter equal to `0`, the function returns the first indexed element (accessor)', function test( t ) { + var x; + var v; + + x = [ 1.0, -2.0, -4.0, -5.0, -6.0 ]; + + v = maxsorted( x.length, toAccessorArray( x ), 0, 0 ); + t.strictEqual( v, 1.0, 'returns expected value' ); + + t.end(); +}); + tape( 'the function supports an `offset` parameter', function test( t ) { - var N; var x; var v; @@ -187,9 +283,29 @@ tape( 'the function supports an `offset` parameter', function test( t ) { 3.0, 4.0 // 3 ]; - N = floor( x.length / 2 ); - v = maxsorted( N, x, 2, 1 ); + v = maxsorted( 4, x, 2, 1 ); + t.strictEqual( v, 4.0, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports an `offset` parameter (accessor)', 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 = maxsorted( 4, toAccessorArray( x ), 2, 1 ); t.strictEqual( v, 4.0, 'returns expected value' ); t.end();