diff --git a/lib/node_modules/@stdlib/stats/base/ndarray/max-by/README.md b/lib/node_modules/@stdlib/stats/base/ndarray/max-by/README.md new file mode 100644 index 000000000000..ee0589662e13 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/base/ndarray/max-by/README.md @@ -0,0 +1,152 @@ + + +# maxBy + +> Compute the maximum value of a one-dimensional ndarray via a callback function. + +
+ +
+ + + +
+ +## Usage + +```javascript +var maxBy = require( '@stdlib/stats/base/ndarray/max-by' ); +``` + +#### maxBy( arrays, clbk\[, thisArg ] ) + +Computes the maximum value of a one-dimensional ndarray via a callback function. + +```javascript +var ndarray = require( '@stdlib/ndarray/base/ctor' ); + +function clbk( value ) { + return value * 2.0; +} + +var xbuf = [ 1.0, 3.0, 4.0, 2.0 ]; +var x = new ndarray( 'generic', xbuf, [ 4 ], [ 1 ], 0, 'row-major' ); + +var v = maxBy( [ x ], clbk ); +// returns 8.0 +``` + +The function has the following parameters: + +- **arrays**: array-like object containing a one-dimensional input ndarray. +- **clbk**: callback function. +- **thisArg**: callback execution context (_optional_). + +The invoked callback is provided three arguments: + +- **value**: current array element. +- **idx**: current array element index. +- **array**: input ndarray. + +To set the callback execution context, provide a `thisArg`. + +```javascript +var ndarray = require( '@stdlib/ndarray/base/ctor' ); + +function clbk( value ) { + this.count += 1; + return value * 2.0; +} + +var xbuf = [ 1.0, 3.0, 4.0, 2.0 ]; +var x = new ndarray( 'generic', xbuf, [ 4 ], [ 1 ], 0, 'row-major' ); +var ctx = { + 'count': 0 +}; + +var v = maxBy( [ x ], clbk, ctx ); +// returns 8.0 + +var count = ctx.count; +// returns 4 +``` + +
+ + + +
+ +## Notes + +- If provided an empty one-dimensional ndarray, the function returns `NaN`. +- 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**. + +
+ + + +
+ +## Examples + + + +```javascript +var discreteUniform = require( '@stdlib/random/array/discrete-uniform' ); +var ndarray = require( '@stdlib/ndarray/base/ctor' ); +var ndarray2array = require( '@stdlib/ndarray/to-array' ); +var maxBy = require( '@stdlib/stats/base/ndarray/max-by' ); + +function clbk( value ) { + return value * 2.0; +} + +var xbuf = discreteUniform( 10, -50, 50, { + 'dtype': 'generic' +}); +var x = new ndarray( 'generic', xbuf, [ xbuf.length ], [ 1 ], 0, 'row-major' ); +console.log( ndarray2array( x ) ); + +var v = maxBy( [ x ], clbk ); +console.log( v ); +``` + +
+ + + + + + + + + + + + + + diff --git a/lib/node_modules/@stdlib/stats/base/ndarray/max-by/benchmark/benchmark.js b/lib/node_modules/@stdlib/stats/base/ndarray/max-by/benchmark/benchmark.js new file mode 100644 index 000000000000..af52900c32b7 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/base/ndarray/max-by/benchmark/benchmark.js @@ -0,0 +1,113 @@ +/** +* @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 maxBy = require( './../lib' ); + + +// VARIABLES // + +var options = { + 'dtype': 'generic' +}; + + +// FUNCTIONS // + +/** +* Callback function. +* +* @private +* @param {number} value - array element +* @returns {number} callback result +*/ +function clbk( value ) { + return value * 2.0; +} + +/** +* 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 ); + 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 = maxBy( [ x ], clbk ); + 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/max-by/docs/repl.txt b/lib/node_modules/@stdlib/stats/base/ndarray/max-by/docs/repl.txt new file mode 100644 index 000000000000..534b9cedfb98 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/base/ndarray/max-by/docs/repl.txt @@ -0,0 +1,50 @@ + +{{alias}}( arrays, clbk[, thisArg] ) + Computes the maximum value of a one-dimensional ndarray via a callback + function. + + If provided an empty ndarray, the function returns `NaN`. + + The callback function is provided three arguments: + + - value: current array element. + - index: current array index. + - array: the input ndarray. + + The callback function should return a numeric value. + + If the callback function does not return any value (or equivalently, + explicitly returns `undefined`), the value is ignored. + + Parameters + ---------- + arrays: ArrayLikeObject + Array-like object containing a one-dimensional input ndarray. + + clbk: Function + Callback function. + + thisArg: any (optional) + Callback execution context. + + Returns + ------- + out: number + Maximum value. + + Examples + -------- + > var xbuf = [ 1.0, -2.0, 2.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 ); + > function f ( v ) { return v * 2.0; }; + > {{alias}}( [ x ], f ) + 4.0 + + See Also + -------- + diff --git a/lib/node_modules/@stdlib/stats/base/ndarray/max-by/docs/types/index.d.ts b/lib/node_modules/@stdlib/stats/base/ndarray/max-by/docs/types/index.d.ts new file mode 100644 index 000000000000..613d0b16e55e --- /dev/null +++ b/lib/node_modules/@stdlib/stats/base/ndarray/max-by/docs/types/index.d.ts @@ -0,0 +1,95 @@ +/* +* @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 { typedndarray } from '@stdlib/types/ndarray'; + +/** +* Returns the result of callback function. +* +* @returns result +*/ +type Nullary = ( this: ThisArg ) => number | void; + +/** +* Returns the result of callback function. +* +* @param value - current array element +* @returns result +*/ +type Unary = ( this: ThisArg, value: T ) => number | void; + +/** +* Returns the result of callback function. +* +* @param value - current array element +* @param index - current array element index +* @returns result +*/ +type Binary = ( this: ThisArg, value: T, index: number ) => number | void; + +/** +* Returns the result of callback function. +* +* @param value - current array element +* @param index - current array element index +* @param array - input ndarray +* @returns result +*/ +type Ternary = ( this: ThisArg, value: T, index: number, array: U ) => number | void; + +/** +* Returns the result of callback function. +* +* @param value - current array element +* @param index - current array element index +* @param array - input ndarray +* @returns result +*/ +type Callback = Nullary | Unary | Binary | Ternary; + +/** +* Computes the maximum value of a one-dimensional ndarray via a callback function. +* +* @param arrays - array-like object containing an input ndarray +* @param clbk - callback function +* @param thisArg - callback execution context +* @returns maximum value +* +* @example +* var ndarray = require( '@stdlib/ndarray/base/ctor' ); +* +* function clbk( value ) { +* return value * 2.0; +* } +* +* var xbuf = [ 1.0, 3.0, 4.0, 2.0 ]; +* var x = new ndarray( 'generic', xbuf, [ 4 ], [ 1 ], 0, 'row-major' ); +* +* var v = maxBy( [ x ], clbk ); +* // returns 8.0 +*/ +declare function maxBy = typedndarray, ThisArg = unknown>( arrays: [ U ], clbk: Callback, thisArg?: ThisParameterType> ): number; + + +// EXPORTS // + +export = maxBy; diff --git a/lib/node_modules/@stdlib/stats/base/ndarray/max-by/docs/types/test.ts b/lib/node_modules/@stdlib/stats/base/ndarray/max-by/docs/types/test.ts new file mode 100644 index 000000000000..2a3c7e2e51d7 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/base/ndarray/max-by/docs/types/test.ts @@ -0,0 +1,103 @@ +/* +* @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 maxBy = require( './index' ); + +/** +* Callback function. +* +* @param value - ndarray element +* @returns result +*/ +function clbk( value: any ): number { + return value * 2.0; +} + + +// TESTS // + +// The function returns a number... +{ + const x = zeros( [ 10 ], { + 'dtype': 'generic' + }); + + maxBy( [ x ], clbk ); // $ExpectType number + maxBy( [ x ], clbk, {} ); // $ExpectType number +} + +// The compiler throws an error if the function is provided a first argument which is not an array of ndarrays... +{ + maxBy( '10', clbk ); // $ExpectError + maxBy( 10, clbk ); // $ExpectError + maxBy( true, clbk ); // $ExpectError + maxBy( false, clbk ); // $ExpectError + maxBy( null, clbk ); // $ExpectError + maxBy( undefined, clbk ); // $ExpectError + maxBy( [], clbk ); // $ExpectError + maxBy( {}, clbk ); // $ExpectError + maxBy( ( x: number ): number => x, clbk ); // $ExpectError + + maxBy( '10', clbk, {} ); // $ExpectError + maxBy( 10, clbk, {} ); // $ExpectError + maxBy( true, clbk, {} ); // $ExpectError + maxBy( false, clbk, {} ); // $ExpectError + maxBy( null, clbk, {} ); // $ExpectError + maxBy( undefined, clbk, {} ); // $ExpectError + maxBy( [], clbk, {} ); // $ExpectError + maxBy( {}, clbk, {} ); // $ExpectError + maxBy( ( x: number ): number => x, clbk, {} ); // $ExpectError +} + +// The compiler throws an error if the function is provided a second argument which is not a callback function... +{ + const x = zeros( [ 10 ], { + 'dtype': 'generic' + }); + + maxBy( [ x ], '10' ); // $ExpectError + maxBy( [ x ], 10 ); // $ExpectError + maxBy( [ x ], true ); // $ExpectError + maxBy( [ x ], false ); // $ExpectError + maxBy( [ x ], null ); // $ExpectError + maxBy( [ x ], undefined ); // $ExpectError + maxBy( [ x ], [] ); // $ExpectError + maxBy( [ x ], {} ); // $ExpectError + + maxBy( [ x ], '10', {} ); // $ExpectError + maxBy( [ x ], 10, {} ); // $ExpectError + maxBy( [ x ], true, {} ); // $ExpectError + maxBy( [ x ], false, {} ); // $ExpectError + maxBy( [ x ], null, {} ); // $ExpectError + maxBy( [ x ], undefined, {} ); // $ExpectError + maxBy( [ x ], [], {} ); // $ExpectError + maxBy( [ x ], {}, {} ); // $ExpectError +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + const x = zeros( [ 10 ], { + 'dtype': 'generic' + }); + + maxBy(); // $ExpectError + maxBy( [ x ], clbk, {}, {} ); // $ExpectError +} diff --git a/lib/node_modules/@stdlib/stats/base/ndarray/max-by/examples/index.js b/lib/node_modules/@stdlib/stats/base/ndarray/max-by/examples/index.js new file mode 100644 index 000000000000..6db4e61246f3 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/base/ndarray/max-by/examples/index.js @@ -0,0 +1,37 @@ +/** +* @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 discreteUniform = require( '@stdlib/random/array/discrete-uniform' ); +var ndarray = require( '@stdlib/ndarray/base/ctor' ); +var ndarray2array = require( '@stdlib/ndarray/to-array' ); +var maxBy = require( './../lib' ); + +function clbk( value ) { + return value * 2.0; +} + +var xbuf = discreteUniform( 10, -50, 50, { + 'dtype': 'generic' +}); +var x = new ndarray( 'generic', xbuf, [ xbuf.length ], [ 1 ], 0, 'row-major' ); +console.log( ndarray2array( x ) ); + +var v = maxBy( [ x ], clbk ); +console.log( v ); diff --git a/lib/node_modules/@stdlib/stats/base/ndarray/max-by/lib/index.js b/lib/node_modules/@stdlib/stats/base/ndarray/max-by/lib/index.js new file mode 100644 index 000000000000..a139ac04ab90 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/base/ndarray/max-by/lib/index.js @@ -0,0 +1,48 @@ +/** +* @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 maximum value of a one-dimensional ndarray via a callback function. +* +* @module @stdlib/stats/base/ndarray/max-by +* +* @example +* var ndarray = require( '@stdlib/ndarray/base/ctor' ); +* var max = require( '@stdlib/stats/base/ndarray/max-by' ); +* +* function clbk( value ) { +* return value * 2.0; +* } +* +* var xbuf = [ 1.0, 3.0, 4.0, 2.0 ]; +* var x = new ndarray( 'generic', xbuf, [ 4 ], [ 1 ], 0, 'row-major' ); +* +* var v = maxBy( [ x ], clbk ); +* // returns 8.0 +*/ + +// MODULES // + +var main = require( './main.js' ); + + +// EXPORTS // + +module.exports = main; diff --git a/lib/node_modules/@stdlib/stats/base/ndarray/max-by/lib/main.js b/lib/node_modules/@stdlib/stats/base/ndarray/max-by/lib/main.js new file mode 100644 index 000000000000..821aa7dce48d --- /dev/null +++ b/lib/node_modules/@stdlib/stats/base/ndarray/max-by/lib/main.js @@ -0,0 +1,75 @@ +/** +* @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/base/max-by' ).ndarray; + + +// MAIN // + +/** +* Computes the maximum value of a one-dimensional ndarray via a callback function. +* +* @param {ArrayLikeObject} arrays - array-like object containing an input ndarray +* @param {Function} clbk - callback function +* @param {*} [thisArg] - callback execution context +* @returns {number} maximum value +* +* @example +* var ndarray = require( '@stdlib/ndarray/base/ctor' ); +* +* function clbk( value ) { +* return value * 2.0; +* } +* +* var xbuf = [ 1.0, 3.0, 4.0, 2.0 ]; +* var x = new ndarray( 'generic', xbuf, [ 4 ], [ 1 ], 0, 'row-major' ); +* +* var v = maxBy( [ x ], clbk ); +* // returns 8.0 +*/ +function maxBy( arrays, clbk, thisArg ) { + var x = arrays[ 0 ]; + return strided( numelDimension( x, 0 ), getData( x ), getStride( x, 0 ), getOffset( x ), wrapper, null ); // eslint-disable-line max-len + + /** + * Invokes a provided callback. + * + * @private + * @param {*} value - current array element + * @param {NonNegativeInteger} aidx - current array element index + * @param {NonNegativeInteger} sidx - current strided array element index + * @param {Collection} arr - input array + * @returns {*} result + */ + function wrapper( value, aidx ) { + return clbk.call( thisArg, value, aidx, x ); + } +} + + +// EXPORTS // + +module.exports = maxBy; diff --git a/lib/node_modules/@stdlib/stats/base/ndarray/max-by/package.json b/lib/node_modules/@stdlib/stats/base/ndarray/max-by/package.json new file mode 100644 index 000000000000..ebdaad041362 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/base/ndarray/max-by/package.json @@ -0,0 +1,67 @@ +{ + "name": "@stdlib/stats/base/ndarray/max-by", + "version": "0.0.0", + "description": "Compute the maximum value of a one-dimensional ndarray via a callback function.", + "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", + "maximum", + "max", + "range", + "extremes", + "domain", + "extent", + "ndarray" + ], + "__stdlib__": {} +} diff --git a/lib/node_modules/@stdlib/stats/base/ndarray/max-by/test/test.js b/lib/node_modules/@stdlib/stats/base/ndarray/max-by/test/test.js new file mode 100644 index 000000000000..11ffdd4f86bb --- /dev/null +++ b/lib/node_modules/@stdlib/stats/base/ndarray/max-by/test/test.js @@ -0,0 +1,249 @@ +/** +* @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 isPositiveZero = require( '@stdlib/math/base/assert/is-positive-zero' ); +var ndarray = require( '@stdlib/ndarray/base/ctor' ); +var maxBy = 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 maxBy, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function has an arity of 3', function test( t ) { + t.strictEqual( maxBy.length, 3, 'has expected arity' ); + t.end(); +}); + +tape( 'the function calculates the maximum value of a one-dimensional ndarray via a callback function', function test( t ) { + var x; + var v; + + x = [ 1.0, -2.0, -4.0, 5.0, 0.0, 3.0 ]; + v = maxBy( [ vector( x, 6, 1, 0 ) ], clbk ); + t.strictEqual( v, 10.0, 'returns expected value' ); + + x = [ -4.0, -5.0 ]; + v = maxBy( [ vector( x, 2, 1, 0 ) ], clbk ); + t.strictEqual( v, -8.0, 'returns expected value' ); + + x = [ -0.0, 0.0, -0.0 ]; + v = maxBy( [ vector( x, 3, 1, 0 ) ], clbk ); + t.strictEqual( isPositiveZero( v ), true, 'returns expected value' ); + + x = [ NaN ]; + v = maxBy( [ vector( x, 1, 1, 0 ) ], clbk ); + t.strictEqual( isnan( v ), true, 'returns expected value' ); + + x = [ NaN, NaN ]; + v = maxBy( [ vector( x, 2, 1, 0 ) ], clbk ); + t.strictEqual( isnan( v ), true, 'returns expected value' ); + + t.end(); + + function clbk( v ) { + return v * 2.0; + } +}); + +tape( 'if provided an empty vector, the function returns `NaN`', function test( t ) { + var x; + var v; + + x = []; + + v = maxBy( [ vector( x, 0, 1, 0 ) ], clbk ); + t.strictEqual( isnan( v ), true, 'returns expected value' ); + + t.end(); + + function clbk( v ) { + return v * 2.0; + } +}); + +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, + -2.0, // 2 + 3.0, + 4.0, // 3 + 2.0 + ]; + + v = maxBy( [ vector( x, 4, 2, 0 ) ], clbk ); + + t.strictEqual( v, 8.0, 'returns expected value' ); + t.end(); + + function clbk( v ) { + return v * 2.0; + } +}); + +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 = maxBy( [ vector( x, 4, -2, 6 ) ], clbk ); + + t.strictEqual( v, 8.0, 'returns expected value' ); + t.end(); + + function clbk( v ) { + return v * 2.0; + } +}); + +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, + 2.0, // 2 + 3.0, + 4.0 // 3 + ]; + + v = maxBy( [ vector( x, 4, 2, 1 ) ], clbk ); + t.strictEqual( v, 8.0, 'returns expected value' ); + + t.end(); + + function clbk( v ) { + return v * 2.0; + } +}); + +tape( 'the function supports providing an execution context', function test( t ) { + var expected; + var indices; + var values; + var arrays; + var ctx; + var arr; + 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 + ]; + ctx = { + 'count': 0 + }; + + indices = []; + values = []; + arrays = []; + + arr = vector( x, 4, 2, 0 ); + v = maxBy( [ arr ], clbk, ctx ); + + t.strictEqual( v, 8.0, 'returns expected value' ); + t.strictEqual( ctx.count, 4, 'returns expected value' ); + + expected = [ + 1.0, + 2.0, + -2.0, + 4.0 + ]; + t.deepEqual( values, expected, 'returns expected value' ); + + expected = [ + 0, + 1, + 2, + 3 + ]; + t.deepEqual( indices, expected, 'returns expected value' ); + + expected = [ + arr, + arr, + arr, + arr + ]; + t.deepEqual( arrays, expected, 'returns expected value' ); + + t.end(); + + function clbk( v, idx, arr ) { + this.count += 1; // eslint-disable-line no-invalid-this + values.push( v ); + indices.push( idx ); + arrays.push( arr ); + return v * 2.0; + } +});