From 1911bb1e7e3c987bed4ef8379b3da763e733e8e0 Mon Sep 17 00:00:00 2001 From: headlessNode Date: Sat, 22 Aug 2026 16:50:47 +0500 Subject: [PATCH 1/4] feat: add fromIndex support to blas/ext/find-index --- .../@stdlib/blas/ext/find-index/README.md | 24 +- .../@stdlib/blas/ext/find-index/docs/repl.txt | 32 +- .../blas/ext/find-index/docs/types/index.d.ts | 128 +++++++ .../blas/ext/find-index/docs/types/test.ts | 64 +++- .../@stdlib/blas/ext/find-index/lib/assign.js | 158 +++++++- .../@stdlib/blas/ext/find-index/lib/base.js | 15 +- .../@stdlib/blas/ext/find-index/lib/index.js | 27 ++ .../@stdlib/blas/ext/find-index/lib/main.js | 115 +++++- .../blas/ext/find-index/test/test.assign.js | 283 ++++++++++++++- .../blas/ext/find-index/test/test.main.js | 337 ++++++++++++++++++ 10 files changed, 1147 insertions(+), 36 deletions(-) diff --git a/lib/node_modules/@stdlib/blas/ext/find-index/README.md b/lib/node_modules/@stdlib/blas/ext/find-index/README.md index 26ee1f7e1943..2dc6cd25255c 100644 --- a/lib/node_modules/@stdlib/blas/ext/find-index/README.md +++ b/lib/node_modules/@stdlib/blas/ext/find-index/README.md @@ -30,7 +30,7 @@ limitations under the License. var findIndex = require( '@stdlib/blas/ext/find-index' ); ``` -#### findIndex( x\[, options], clbk\[, thisArg] ) +#### findIndex( x\[, fromIndex]\[, options], clbk\[, thisArg] ) Returns the index of the first element along an [ndarray][@stdlib/ndarray/ctor] dimension which passes a test implemented by a predicate function. @@ -53,6 +53,7 @@ var out = findIndex( x, isEven ); The function has the following parameters: - **x**: input [ndarray][@stdlib/ndarray/ctor]. Must have at least one dimension. +- **fromIndex**: index from which to begin searching (_optional_). May be either a scalar value or an [ndarray][@stdlib/ndarray/ctor] having an integer index or "generic" [data type][@stdlib/ndarray/dtypes]. If provided an [ndarray][@stdlib/ndarray/ctor], the value must have a shape which is [broadcast-compatible][@stdlib/ndarray/base/broadcast-shapes] with the non-reduced dimensions of the input [ndarray][@stdlib/ndarray/ctor]. For example, given the input shape `[2, 3, 4]` and `options.dim=0`, a provided [ndarray][@stdlib/ndarray/ctor] must have a shape which is [broadcast-compatible][@stdlib/ndarray/base/broadcast-shapes] with the shape `[3, 4]`. If provided a negative integer, the index at which to begin searching along a dimension is determined by counting backward from the last element (where `-1` refers to the last element). Default: `0`. - **options**: function options (_optional_). - **clbk**: callback function. - **thisArg**: callback execution context (_optional_). @@ -111,6 +112,24 @@ var out = findIndex( x, isEven ); // returns [ -1 ] ``` +By default, the function begins searching from the first element along the reduction dimension. To begin searching from a different index, provide a `fromIndex` argument. + +```javascript +var array = require( '@stdlib/ndarray/array' ); + +function isEven( v ) { + return v % 2.0 === 0.0; +} + +// Create an input ndarray: +var x = array( [ 2.0, 1.0, 3.0, 4.0, 5.0, 6.0 ] ); +// returns + +// Perform operation: +var out = findIndex( x, 2, isEven ); +// returns [ 3 ] +``` + By default, the function performs the operation over elements in the last dimension. To perform the operation over a different dimension, provide a `dim` option. ```javascript @@ -173,7 +192,7 @@ var dt = dtype( idx ); // returns 'generic' ``` -#### findIndex.assign( x, out\[, options], clbk\[, thisArg] ) +#### findIndex.assign( x\[, fromIndex], out\[, options], clbk\[, thisArg] ) Returns the index of the first element along an [ndarray][@stdlib/ndarray/ctor] dimension which passes a test implemented by a predicate function and assigns results to a provided output [ndarray][@stdlib/ndarray/ctor]. @@ -200,6 +219,7 @@ var bool = ( out === y ); The method has the following parameters: - **x**: input [ndarray][@stdlib/ndarray/ctor]. Must have at least one dimension. +- **fromIndex**: index from which to begin searching (_optional_). May be either a scalar value or an [ndarray][@stdlib/ndarray/ctor] having an integer index or "generic" [data type][@stdlib/ndarray/dtypes]. If provided an [ndarray][@stdlib/ndarray/ctor], the value must have a shape which is [broadcast-compatible][@stdlib/ndarray/base/broadcast-shapes] with the non-reduced dimensions of the input [ndarray][@stdlib/ndarray/ctor]. For example, given the input shape `[2, 3, 4]` and `options.dim=0`, a provided [ndarray][@stdlib/ndarray/ctor] must have a shape which is [broadcast-compatible][@stdlib/ndarray/base/broadcast-shapes] with the shape `[3, 4]`. If provided a negative integer, the index at which to begin searching along a dimension is determined by counting backward from the last element (where `-1` refers to the last element). Default: `0`. - **out**: output [ndarray][@stdlib/ndarray/ctor]. - **options**: function options (_optional_). - **clbk**: callback function. diff --git a/lib/node_modules/@stdlib/blas/ext/find-index/docs/repl.txt b/lib/node_modules/@stdlib/blas/ext/find-index/docs/repl.txt index 56c183c4dd5a..c2f271daaf47 100644 --- a/lib/node_modules/@stdlib/blas/ext/find-index/docs/repl.txt +++ b/lib/node_modules/@stdlib/blas/ext/find-index/docs/repl.txt @@ -1,5 +1,5 @@ -{{alias}}( x[, options], clbk[, thisArg] ) +{{alias}}( x[, fromIndex][, options], clbk[, thisArg] ) Returns the index of the first element along an ndarray dimension which passes a test implemented by a predicate function. @@ -18,7 +18,18 @@ Parameters ---------- x: ndarray - Input array. Must at least have one dimension. + Input array. Must have at least one dimension. + + fromIndex: ndarray|integer (optional) + Index from which to begin searching. May be either a scalar value or an + ndarray having an integer or "generic" data type. If provided an ndarray + the value must have a shape which is broadcast compatible with the non- + reduced dimensions of the input ndarray. For example, given the input + shape `[2, 3, 4]` and `options.dim=0`, a provided ndarray must have a + shape which is broadcast-compatible with the shape `[3, 4]`. If provided + a negative integer, the index at which to begin searching along a + dimension is determined by counting backward from the last element + (where -1 refers to the last element). Default: 0. options: Object (optional) Function options. @@ -55,7 +66,7 @@ [ 1 ] -{{alias}}.assign( x, out[, options], clbk[, thisArg] ) +{{alias}}.assign( x[, fromIndex], out[, options], clbk[, thisArg] ) Returns the index of the first element along an ndarray dimension which passes a test implemented by a predicate function and assigns results to a provided output ndarray. @@ -75,7 +86,18 @@ Parameters ---------- x: ndarray - Input array. Must at least have one dimension. + Input array. Must have at least one dimension. + + fromIndex: ndarray|integer (optional) + Index from which to begin searching. May be either a scalar value or an + ndarray having an integer or "generic" data type. If provided an ndarray + the value must have a shape which is broadcast compatible with the non- + reduced dimensions of the input ndarray. For example, given the input + shape `[2, 3, 4]` and `options.dim=0`, a provided ndarray must have a + shape which is broadcast-compatible with the shape `[3, 4]`. If provided + a negative integer, the index at which to begin searching along a + dimension is determined by counting backward from the last element + (where -1 refers to the last element). Default: 0. out: ndarray Output array. @@ -103,7 +125,7 @@ Examples -------- > var x = {{alias:@stdlib/ndarray/array}}( [ -1.0, 2.0, -3.0, -4.0 ] ); - > var out = {{alias:@stdlib/ndarray/zeros}}( [] ); + > var out = {{alias:@stdlib/ndarray/zeros}}( [], { 'dtype': 'int32' } ); > function clbk( v ) { return v % 2.0 === 0.0; }; > var y = {{alias}}.assign( x, out, clbk ) [ 1 ] diff --git a/lib/node_modules/@stdlib/blas/ext/find-index/docs/types/index.d.ts b/lib/node_modules/@stdlib/blas/ext/find-index/docs/types/index.d.ts index 193aa35cad2a..91cca9da6a92 100644 --- a/lib/node_modules/@stdlib/blas/ext/find-index/docs/types/index.d.ts +++ b/lib/node_modules/@stdlib/blas/ext/find-index/docs/types/index.d.ts @@ -27,6 +27,11 @@ import { IntegerIndexAndGenericDataType as DataType, typedndarray } from '@stdli */ type InputArray = typedndarray; +/** +* From index. +*/ +type FromIndex = typedndarray | number; + /** * Output array. */ @@ -135,6 +140,33 @@ interface FindIndex { */ = InputArray, ThisArg = unknown>( x: U, clbk: Predicate, thisArg?: ThisParameterType> ): OutputArray; + /** + * Returns the index of the first element along an ndarray dimension which passes a test implemented by a predicate function. + * + * ## Notes + * + * - If no element along an ndarray dimension passes a test implemented by the predicate function, the corresponding element in the returned ndarray is `-1`. + * + * @param x - input ndarray + * @param fromIndex - index from which to begin searching + * @param clbk - predicate function + * @param thisArg - predicate function execution context + * @returns output ndarray + * + * @example + * var array = require( '@stdlib/ndarray/array' ); + * + * function clbk( value ) { + * return value % 2.0 === 0.0; + * } + * + * var x = array( [ 2.0, 1.0, -3.0, 4.0 ] ); + * + * var y = findIndex( x, 2, clbk ); + * // returns [ 3 ] + */ + = InputArray, ThisArg = unknown>( x: U, fromIndex: FromIndex, clbk: Predicate, thisArg?: ThisParameterType> ): OutputArray; + /** * Returns the index of the first element along an ndarray dimension which passes a test implemented by a predicate function. * @@ -162,6 +194,34 @@ interface FindIndex { */ = InputArray, ThisArg = unknown>( x: U, options: Options, clbk: Predicate, thisArg?: ThisParameterType> ): OutputArray; + /** + * Returns the index of the first element along an ndarray dimension which passes a test implemented by a predicate function. + * + * ## Notes + * + * - If no element along an ndarray dimension passes a test implemented by the predicate function, the corresponding element in the returned ndarray is `-1`. + * + * @param x - input ndarray + * @param fromIndex - index from which to begin searching + * @param options - function options + * @param clbk - predicate function + * @param thisArg - predicate function execution context + * @returns output ndarray + * + * @example + * var array = require( '@stdlib/ndarray/array' ); + * + * function clbk( value ) { + * return value % 2.0 === 0.0; + * } + * + * var x = array( [ 2.0, 1.0, -3.0, 4.0 ] ); + * + * var y = findIndex( x, 2, {}, clbk ); + * // returns [ 3 ] + */ + = InputArray, ThisArg = unknown>( x: U, fromIndex: FromIndex, options: Options, clbk: Predicate, thisArg?: ThisParameterType> ): OutputArray; + /** * Returns the index of the first element along an ndarray dimension which passes a test implemented by a predicate function and assigns results to a provided output ndarray. * @@ -194,6 +254,39 @@ interface FindIndex { */ assign = InputArray, V extends OutputArray = OutputArray, ThisArg = unknown>( x: U, out: V, clbk: Predicate, thisArg?: ThisParameterType> ): V; + /** + * Returns the index of the first element along an ndarray dimension which passes a test implemented by a predicate function and assigns results to a provided output ndarray. + * + * ## Notes + * + * - If no element along an ndarray dimension passes a test implemented by the predicate function, the corresponding element in the returned ndarray is `-1`. + * + * @param x - input ndarray + * @param fromIndex - index from which to begin searching + * @param out - output ndarray + * @param clbk - predicate function + * @param thisArg - predicate function execution context + * @returns output ndarray + * + * @example + * var array = require( '@stdlib/ndarray/array' ); + * var zeros = require( '@stdlib/ndarray/zeros' ); + * + * function clbk( value ) { + * return value % 2.0 === 0.0; + * } + * + * var x = array( [ 2.0, 1.0, -3.0, 4.0 ] ); + * var y = zeros( [] ); + * + * var out = findIndex.assign( x, 2, y, clbk ); + * // returns [ 3 ] + * + * var bool = ( out === y ); + * // returns true + */ + assign = InputArray, V extends OutputArray = OutputArray, ThisArg = unknown>( x: U, fromIndex: FromIndex, out: V, clbk: Predicate, thisArg?: ThisParameterType> ): V; + /** * Returns the index of the first element along an ndarray dimension which passes a test implemented by a predicate function and assigns results to a provided output ndarray. * @@ -226,6 +319,40 @@ interface FindIndex { * // returns true */ assign = InputArray, V extends OutputArray = OutputArray, ThisArg = unknown>( x: U, out: V, options: BaseOptions, clbk: Predicate, thisArg?: ThisParameterType> ): V; + + /** + * Returns the index of the first element along an ndarray dimension which passes a test implemented by a predicate function and assigns results to a provided output ndarray. + * + * ## Notes + * + * - If no element along an ndarray dimension passes a test implemented by the predicate function, the corresponding element in the returned ndarray is `-1`. + * + * @param x - input ndarray + * @param fromIndex - index from which to begin searching + * @param out - output ndarray + * @param options - function options + * @param clbk - predicate function + * @param thisArg - predicate function execution context + * @returns output ndarray + * + * @example + * var array = require( '@stdlib/ndarray/array' ); + * var zeros = require( '@stdlib/ndarray/zeros' ); + * + * function clbk( value ) { + * return value % 2.0 === 0.0; + * } + * + * var x = array( [ 2.0, 1.0, -3.0, 4.0 ] ); + * var y = zeros( [] ); + * + * var out = findIndex.assign( x, 2, y, {}, clbk ); + * // returns [ 3 ] + * + * var bool = ( out === y ); + * // returns true + */ + assign = InputArray, V extends OutputArray = OutputArray, ThisArg = unknown>( x: U, fromIndex: FromIndex, out: V, options: BaseOptions, clbk: Predicate, thisArg?: ThisParameterType> ): V; } /** @@ -236,6 +363,7 @@ interface FindIndex { * - If no element along an ndarray dimension passes a test implemented by the predicate function, the corresponding element in the returned ndarray is `-1`. * * @param x - input ndarray +* @param fromIndex - index from which to begin searching * @param options - function options * @param clbk - predicate function * @param thisArg - predicate function execution context diff --git a/lib/node_modules/@stdlib/blas/ext/find-index/docs/types/test.ts b/lib/node_modules/@stdlib/blas/ext/find-index/docs/types/test.ts index b96581e205e6..2ffb6004ed7c 100644 --- a/lib/node_modules/@stdlib/blas/ext/find-index/docs/types/test.ts +++ b/lib/node_modules/@stdlib/blas/ext/find-index/docs/types/test.ts @@ -45,8 +45,14 @@ function clbk( value: any ): boolean { findIndex( x, clbk ); // $ExpectType OutputArray findIndex( x, clbk, {} ); // $ExpectType OutputArray + findIndex( x, 1, clbk ); // $ExpectType OutputArray + findIndex( x, 1, clbk, {} ); // $ExpectType OutputArray + findIndex( x, {}, clbk ); // $ExpectType OutputArray findIndex( x, {}, clbk, {} ); // $ExpectType OutputArray + + findIndex( x, 1, {}, clbk ); // $ExpectType OutputArray + findIndex( x, 1, {}, clbk, {} ); // $ExpectType OutputArray } // The compiler throws an error if the function is provided a first argument which is not an ndarray... @@ -86,6 +92,55 @@ function clbk( value: any ): boolean { findIndex( void 0, {}, clbk, {} ); // $ExpectError findIndex( {}, {}, clbk, {} ); // $ExpectError findIndex( ( x: number ): number => x, {}, clbk, {} ); // $ExpectError + + findIndex( '5', 1, clbk ); // $ExpectError + findIndex( 5, 1, clbk ); // $ExpectError + findIndex( true, 1, clbk ); // $ExpectError + findIndex( false, 1, clbk ); // $ExpectError + findIndex( null, 1, clbk ); // $ExpectError + findIndex( void 0, 1, clbk ); // $ExpectError + findIndex( {}, 1, clbk ); // $ExpectError + findIndex( ( x: number ): number => x, 1, clbk ); // $ExpectError + + findIndex( '5', 1, {}, clbk ); // $ExpectError + findIndex( 5, 1, {}, clbk ); // $ExpectError + findIndex( true, 1, {}, clbk ); // $ExpectError + findIndex( false, 1, {}, clbk ); // $ExpectError + findIndex( null, 1, {}, clbk ); // $ExpectError + findIndex( void 0, 1, {}, clbk ); // $ExpectError + findIndex( {}, 1, {}, clbk ); // $ExpectError + findIndex( ( x: number ): number => x, 1, {}, clbk ); // $ExpectError +} + +// The compiler throws an error if the function is provided a from index argument which is not an ndarray or an integer value... +{ + const x = zeros( [ 2, 2 ], { + 'dtype': 'generic' + }); + + findIndex( x, '5', clbk ); // $ExpectError + findIndex( x, true, clbk ); // $ExpectError + findIndex( x, false, clbk ); // $ExpectError + findIndex( x, null, clbk ); // $ExpectError + findIndex( x, [], clbk ); // $ExpectError + + findIndex( x, '5', clbk, {} ); // $ExpectError + findIndex( x, true, clbk, {} ); // $ExpectError + findIndex( x, false, clbk, {} ); // $ExpectError + findIndex( x, null, clbk, {} ); // $ExpectError + findIndex( x, [], clbk, {} ); // $ExpectError + + findIndex( x, '5', {}, clbk ); // $ExpectError + findIndex( x, true, {}, clbk ); // $ExpectError + findIndex( x, false, {}, clbk ); // $ExpectError + findIndex( x, null, {}, clbk ); // $ExpectError + findIndex( x, [], {}, clbk ); // $ExpectError + + findIndex( x, '5', {}, clbk, {} ); // $ExpectError + findIndex( x, true, {}, clbk, {} ); // $ExpectError + findIndex( x, false, {}, clbk, {} ); // $ExpectError + findIndex( x, null, {}, clbk, {} ); // $ExpectError + findIndex( x, [], {}, clbk, {} ); // $ExpectError } // The compiler throws an error if the function is provided an options argument which is not an object... @@ -207,7 +262,7 @@ function clbk( value: any ): boolean { findIndex(); // $ExpectError findIndex( x ); - findIndex( x, {}, clbk, {}, {} ); // $ExpectError + findIndex( x, 1, {}, clbk, {}, {} ); // $ExpectError } // Attached to the function is an `assign` method which returns an ndarray... @@ -224,6 +279,11 @@ function clbk( value: any ): boolean { findIndex.assign( x, y, clbk, {} ); // $ExpectType int32ndarray findIndex.assign( x, y, {}, clbk, {} ); // $ExpectType int32ndarray + + findIndex.assign( x, 1, y, clbk ); // $ExpectType int32ndarray + findIndex.assign( x, 1, y, clbk, {} ); // $ExpectType int32ndarray + findIndex.assign( x, 1, y, {}, clbk ); // $ExpectType int32ndarray + findIndex.assign( x, 1, y, {}, clbk, {} ); // $ExpectType int32ndarray } // The compiler throws an error if the `assign` method is provided a first argument which is not an ndarray... @@ -400,5 +460,5 @@ function clbk( value: any ): boolean { findIndex.assign(); // $ExpectError findIndex.assign( x ); // $ExpectError findIndex.assign( x, y ); // $ExpectError - findIndex.assign( x, y, {}, clbk, {}, {} ); // $ExpectError + findIndex.assign( x, 1, y, {}, clbk, {}, {} ); // $ExpectError } diff --git a/lib/node_modules/@stdlib/blas/ext/find-index/lib/assign.js b/lib/node_modules/@stdlib/blas/ext/find-index/lib/assign.js index ebe52c62c871..5b92bbd30fa2 100644 --- a/lib/node_modules/@stdlib/blas/ext/find-index/lib/assign.js +++ b/lib/node_modules/@stdlib/blas/ext/find-index/lib/assign.js @@ -23,18 +23,31 @@ var hasOwnProp = require( '@stdlib/assert/has-own-property' ); var isFunction = require( '@stdlib/assert/is-function' ); var isPlainObject = require( '@stdlib/assert/is-plain-object' ); +var isInteger = require( '@stdlib/assert/is-integer' ).isPrimitive; var isndarrayLike = require( '@stdlib/assert/is-ndarray-like' ); +var broadcastScalar = require( '@stdlib/ndarray/base/broadcast-scalar' ); +var maybeBroadcastArray = require( '@stdlib/ndarray/base/maybe-broadcast-array' ); +var nonCoreShape = require( '@stdlib/ndarray/base/complement-shape' ); +var getShape = require( '@stdlib/ndarray/shape' ); +var getOrder = require( '@stdlib/ndarray/order' ); var ndims = require( '@stdlib/ndarray/ndims' ); var format = require( '@stdlib/string/format' ); +var defaults = require( '@stdlib/ndarray/defaults' ); var base = require( './base.js' ).assign; +// VARIABLES // + +var DEFAULT_DTYPE = defaults.get( 'dtypes.integer_index' ); + + // MAIN // /** * Returns the index of the first element along an ndarray dimension which passes a test implemented by a predicate function and assigns the results to a provided output ndarray. * * @param {ndarrayLike} x - input ndarray +* @param {(ndarrayLike|integer)} [fromIndex=0] - index from which to begin searching * @param {ndarrayLike} out - output ndarray * @param {Options} [options] - function options * @param {integer} [options.dim=-1] - dimension over which to perform operation @@ -42,7 +55,8 @@ var base = require( './base.js' ).assign; * @param {*} [thisArg] - callback execution context * @throws {TypeError} function must be provided at least three arguments * @throws {TypeError} first argument must be an ndarray-like object -* @throws {TypeError} second argument must be an ndarray-like object +* @throws {TypeError} `fromIndex` argument must be either an ndarray-like object or an integer +* @throws {TypeError} output argument must be an ndarray-like object * @throws {TypeError} callback argument must be a function * @throws {TypeError} options argument must be an object * @throws {RangeError} dimension index must not exceed input ndarray bounds @@ -85,62 +99,162 @@ var base = require( './base.js' ).assign; * var bool = ( out === y ); * // returns true */ -function assign( x, out ) { +function assign( x, fromIndex, out ) { var hasOptions; var options; var nargs; var opts; + var fidx; + var iflg; + var ord; var ctx; + var sh; var cb; + var o; nargs = arguments.length; if ( !isndarrayLike( x ) ) { throw new TypeError( format( 'invalid argument. First argument must be an ndarray. Value: `%s`.', x ) ); } - if ( !isndarrayLike( out ) ) { - throw new TypeError( format( 'invalid argument. Second argument must be an ndarray. Value: `%s`.', out ) ); - } + // Resolve input ndarray meta data: + ord = getOrder( x ); + // Initialize an options object: opts = { 'dims': [ -1 ] // default behavior is to perform a reduction over the last dimension }; + // Initialize the `fromIndex` to the first element along a dimension: + fidx = 0; + + // Initialize a flag indicating whether the `fromIndex` argument is a scalar: + iflg = true; + // Initialize a flag indicating whether an `options` argument was provided: hasOptions = false; // Case: assign( x, out, clbk ) if ( nargs <= 3 ) { - cb = arguments[ 2 ]; + o = fromIndex; + if ( !isndarrayLike( o ) ) { + throw new TypeError( format( 'invalid argument. Second argument must be an ndarray. Value: `%s`.', o ) ); + } + cb = out; if ( !isFunction( cb ) ) { throw new TypeError( format( 'invalid argument. Third argument must be a function. Value: `%s`.', cb ) ); } } - // Case: assign( x, out, ???, ??? ) + // Case: assign( x, ???, ???, ??? ) else if ( nargs === 4 ) { // Case: assign( x, out, clbk, thisArg ) - if ( isFunction( arguments[ 2 ] ) ) { - cb = arguments[ 2 ]; + if ( isndarrayLike( fromIndex ) && isFunction( out ) ) { + o = fromIndex; + cb = out; ctx = arguments[ 3 ]; } // Case: assign( x, out, options, clbk ) + else if ( isndarrayLike( fromIndex ) && isPlainObject( out ) ) { + o = fromIndex; + options = out; + hasOptions = true; + cb = arguments[ 3 ]; + if ( !isFunction( cb ) ) { + throw new TypeError( format( 'invalid argument. Fourth argument must be a function. Value: `%s`.', cb ) ); + } + } + // Case: assign( x, from_index, out, clbk ) else { - options = arguments[ 2 ]; + if ( isInteger( fromIndex ) ) { + fidx = fromIndex; + } else if ( isndarrayLike( fromIndex ) ) { + fidx = fromIndex; + iflg = false; + } else { + throw new TypeError( format( 'invalid argument. Second argument must be either an ndarray or an integer. Value: `%s`.', fromIndex ) ); + } + o = out; + if ( !isndarrayLike( o ) ) { + throw new TypeError( format( 'invalid argument. Third argument must be an ndarray. Value: `%s`.', o ) ); + } cb = arguments[ 3 ]; if ( !isFunction( cb ) ) { throw new TypeError( format( 'invalid argument. Fourth argument must be a function. Value: `%s`.', cb ) ); } + } + } + // Case: assign( x, ???, ???, ???, ??? ) + else if ( nargs === 5 ) { + // Case: assign( x, out, options, clbk, thisArg ) + if ( isndarrayLike( fromIndex ) && isPlainObject( out ) ) { + o = fromIndex; + options = out; hasOptions = true; + cb = arguments[ 3 ]; + if ( !isFunction( cb ) ) { + throw new TypeError( format( 'invalid argument. Fourth argument must be a function. Value: `%s`.', cb ) ); + } + ctx = arguments[ 4 ]; + } + // Case: assign( x, from_index, out, clbk, thisArg ) + else if ( isFunction( arguments[ 3 ] ) ) { + if ( isInteger( fromIndex ) ) { + fidx = fromIndex; + } else if ( isndarrayLike( fromIndex ) ) { + fidx = fromIndex; + iflg = false; + } else { + throw new TypeError( format( 'invalid argument. Second argument must be either an ndarray or an integer. Value: `%s`.', fromIndex ) ); + } + o = out; + if ( !isndarrayLike( o ) ) { + throw new TypeError( format( 'invalid argument. Third argument must be an ndarray. Value: `%s`.', o ) ); + } + cb = arguments[ 3 ]; + ctx = arguments[ 4 ]; + } + // Case: assign( x, from_index, out, options, clbk ) + else { + if ( isInteger( fromIndex ) ) { + fidx = fromIndex; + } else if ( isndarrayLike( fromIndex ) ) { + fidx = fromIndex; + iflg = false; + } else { + throw new TypeError( format( 'invalid argument. Second argument must be either an ndarray or an integer. Value: `%s`.', fromIndex ) ); + } + o = out; + if ( !isndarrayLike( o ) ) { + throw new TypeError( format( 'invalid argument. Third argument must be an ndarray. Value: `%s`.', o ) ); + } + options = arguments[ 3 ]; + hasOptions = true; + cb = arguments[ 4 ]; + if ( !isFunction( cb ) ) { + throw new TypeError( format( 'invalid argument. Fifth argument must be a function. Value: `%s`.', cb ) ); + } } } - // Case: assign( x, out, options, clbk, thisArg ) + // Case: assign( x, from_index, out, options, clbk, thisArg ) else { - options = arguments[ 2 ]; - cb = arguments[ 3 ]; - ctx = arguments[ 4 ]; - if ( !isFunction( cb ) ) { - throw new TypeError( format( 'invalid argument. Fourth argument must be a function. Value: `%s`.', cb ) ); + if ( isInteger( fromIndex ) ) { + fidx = fromIndex; + } else if ( isndarrayLike( fromIndex ) ) { + fidx = fromIndex; + iflg = false; + } else { + throw new TypeError( format( 'invalid argument. Second argument must be either an ndarray or an integer. Value: `%s`.', fromIndex ) ); + } + o = out; + if ( !isndarrayLike( o ) ) { + throw new TypeError( format( 'invalid argument. Third argument must be an ndarray. Value: `%s`.', o ) ); } + options = arguments[ 3 ]; hasOptions = true; + cb = arguments[ 4 ]; + if ( !isFunction( cb ) ) { + throw new TypeError( format( 'invalid argument. Fifth argument must be a function. Value: `%s`.', cb ) ); + } + ctx = arguments[ 5 ]; } if ( hasOptions ) { if ( !isPlainObject( options ) ) { @@ -154,7 +268,17 @@ function assign( x, out ) { if ( ndims( x ) < 1 ) { throw new RangeError( 'invalid argument. First argument must have at least one dimension.' ); } - return base( x, out, opts, cb, ctx ); + // Resolve the list of non-reduced dimensions: + sh = getShape( x ); + sh = nonCoreShape( sh, opts.dims ); + + // Broadcast the `fromIndex` to match the shape of the non-reduced dimensions... + if ( iflg ) { + fidx = broadcastScalar( fidx, DEFAULT_DTYPE, sh, ord ); + } else { + fidx = maybeBroadcastArray( fidx, sh ); + } + return base( x, fidx, o, opts, cb, ctx ); } diff --git a/lib/node_modules/@stdlib/blas/ext/find-index/lib/base.js b/lib/node_modules/@stdlib/blas/ext/find-index/lib/base.js index 7be8c1bf06a8..e416cdb9ca92 100644 --- a/lib/node_modules/@stdlib/blas/ext/find-index/lib/base.js +++ b/lib/node_modules/@stdlib/blas/ext/find-index/lib/base.js @@ -27,7 +27,8 @@ var factory = require( '@stdlib/ndarray/base/unary-reduce-strided1d-dispatch-by- // VARIABLES // -var idtypes = dtypes( 'all' ); +var idtypes0 = dtypes( 'all' ); // input ndarray +var idtypes1 = dtypes( 'integer_index_and_generic' ); // from index ndarray var odtypes = dtypes( 'integer_index_and_generic' ); var policies = { 'output': 'integer_index_and_generic', @@ -47,12 +48,14 @@ var table = { * @name findIndex * @type {Function} * @param {ndarrayLike} x - input ndarray +* @param {ndarrayLike} fromIndex - indices from which to begin searching * @param {Options} [options] - function options * @param {IntegerArray} [options.dims] - list of dimensions over which to perform operation * @param {*} [options.dtype] - output ndarray data type * @param {Function} clbk - callback function * @param {*} [thisArg] - callback execution context * @throws {TypeError} first argument must be an ndarray-like object +* @throws {TypeError} second argument must be an ndarray-like object * @throws {TypeError} options argument must be an object * @throws {RangeError} dimension indices must not exceed input ndarray bounds * @throws {RangeError} number of dimension indices must not exceed the number of input ndarray dimensions @@ -60,6 +63,7 @@ var table = { * @returns {ndarray} output ndarray * * @example +* var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' ); * var ndarray = require( '@stdlib/ndarray/ctor' ); * * function isEven( v ) { @@ -81,11 +85,16 @@ var table = { * // Create an input ndarray: * var x = new ndarray( 'generic', xbuf, sh, sx, ox, 'row-major' ); * +* // Create a from index ndarray: +* var fromIndex = scalar2ndarray( 0, { +* 'dtype': 'int32' +* }); +* * // Perform operation: -* var out = findIndex( x, isEven ); +* var out = findIndex( x, fromIndex, isEven ); * // returns [ 1 ] */ -var findIndex = factory( table, [ idtypes ], odtypes, policies ); +var findIndex = factory( table, [ idtypes0, idtypes1 ], odtypes, policies ); // EXPORTS // diff --git a/lib/node_modules/@stdlib/blas/ext/find-index/lib/index.js b/lib/node_modules/@stdlib/blas/ext/find-index/lib/index.js index acc53b0175af..94e200558272 100644 --- a/lib/node_modules/@stdlib/blas/ext/find-index/lib/index.js +++ b/lib/node_modules/@stdlib/blas/ext/find-index/lib/index.js @@ -49,6 +49,33 @@ * // Perform operation: * var out = findIndex( x, isEven ); * // returns [ 1, 0 ] +* +* @example +* var ndarray = require( '@stdlib/ndarray/ctor' ); +* var findIndex = require( '@stdlib/blas/ext/find-index' ); +* +* function isEven( v ) { +* return v % 2.0 === 0.0; +* } +* +* // Create a data buffer: +* var xbuf = [ 2.0, 1.0, -3.0, 4.0, -5.0, 6.0 ]; +* +* // Define the shape of the input array: +* var sh = [ 2, 3 ]; +* +* // Define the array strides: +* var sx = [ 3, 1 ]; +* +* // Define the index offset: +* var ox = 0; +* +* // Create an input ndarray: +* var x = new ndarray( 'generic', xbuf, sh, sx, ox, 'row-major' ); +* +* // Perform operation: +* var out = findIndex( x, 1, isEven ); +* // returns [ -1, 2 ] */ // MODULES // diff --git a/lib/node_modules/@stdlib/blas/ext/find-index/lib/main.js b/lib/node_modules/@stdlib/blas/ext/find-index/lib/main.js index fc6005e60031..bd19f67585b3 100644 --- a/lib/node_modules/@stdlib/blas/ext/find-index/lib/main.js +++ b/lib/node_modules/@stdlib/blas/ext/find-index/lib/main.js @@ -23,18 +23,31 @@ var hasOwnProp = require( '@stdlib/assert/has-own-property' ); var isFunction = require( '@stdlib/assert/is-function' ); var isPlainObject = require( '@stdlib/assert/is-plain-object' ); +var isInteger = require( '@stdlib/assert/is-integer' ).isPrimitive; var isndarrayLike = require( '@stdlib/assert/is-ndarray-like' ); +var broadcastScalar = require( '@stdlib/ndarray/base/broadcast-scalar' ); +var maybeBroadcastArray = require( '@stdlib/ndarray/base/maybe-broadcast-array' ); +var nonCoreShape = require( '@stdlib/ndarray/base/complement-shape' ); +var getShape = require( '@stdlib/ndarray/shape' ); +var getOrder = require( '@stdlib/ndarray/order' ); var ndims = require( '@stdlib/ndarray/ndims' ); var format = require( '@stdlib/string/format' ); +var defaults = require( '@stdlib/ndarray/defaults' ); var base = require( './base.js' ); +// VARIABLES // + +var DEFAULT_DTYPE = defaults.get( 'dtypes.integer_index' ); + + // MAIN // /** * Returns the index of the first element along an ndarray dimension which passes a test implemented by a predicate function. * * @param {ndarrayLike} x - input ndarray +* @param {(ndarrayLike|integer)} [fromIndex=0] - index from which to begin searching * @param {Options} [options] - function options * @param {integer} [options.dim=-1] - dimension over which to perform operation * @param {boolean} [options.keepdims=false] - boolean indicating whether the reduced dimensions should be included in the returned ndarray as singleton dimensions @@ -44,6 +57,7 @@ var base = require( './base.js' ); * @throws {TypeError} first argument must be an ndarray-like object * @throws {TypeError} callback argument must be a function * @throws {TypeError} options argument must be an object +* @throws {TypeError} `fromIndex` argument must be either an ndarray-like object or an integer * @throws {RangeError} dimension index must not exceed input ndarray bounds * @throws {RangeError} first argument must have at least one dimension * @throws {Error} must provide valid options @@ -80,19 +94,32 @@ function findIndex( x ) { var options; var nargs; var opts; + var fidx; + var iflg; + var ord; var ctx; + var sh; var cb; nargs = arguments.length; if ( !isndarrayLike( x ) ) { throw new TypeError( format( 'invalid argument. First argument must be an ndarray. Value: `%s`.', x ) ); } + // Resolve input ndarray meta data: + ord = getOrder( x ); + // Initialize an options object: opts = { 'dims': [ -1 ], // default behavior is to perform a reduction over the last dimension 'keepdims': false }; + // Initialize the `fromIndex` to the first element along a dimension: + fidx = 0; + + // Initialize a flag indicating whether the `fromIndex` argument is a scalar: + iflg = true; + // Initialize a flag indicating whether an `options` argument was provided: hasOptions = false; @@ -110,6 +137,23 @@ function findIndex( x ) { cb = arguments[ 1 ]; ctx = arguments[ 2 ]; } + // Case: findIndex( x, from_index_scalar, clbk ) + else if ( isInteger( arguments[ 1 ] ) ) { + fidx = arguments[ 1 ]; + cb = arguments[ 2 ]; + if ( !isFunction( cb ) ) { + throw new TypeError( format( 'invalid argument. Third argument must be a function. Value: `%s`.', cb ) ); + } + } + // Case: findIndex( x, from_index_ndarray, clbk ) + else if ( isndarrayLike( arguments[ 1 ] ) ) { + fidx = arguments[ 1 ]; + iflg = false; + cb = arguments[ 2 ]; + if ( !isFunction( cb ) ) { + throw new TypeError( format( 'invalid argument. Third argument must be a function. Value: `%s`.', cb ) ); + } + } // Case: findIndex( x, options, clbk ) else { options = arguments[ 1 ]; @@ -120,13 +164,62 @@ function findIndex( x ) { hasOptions = true; } } - // Case: findIndex( x, options, clbk, thisArg ) + // Case: findIndex( x, ???, ???, ??? ) + else if ( nargs === 4 ) { + // Case: findIndex( x, from_index, clbk, thisArg ) or findIndex( x, options, clbk, thisArg ) + if ( isFunction( arguments[ 2 ] ) ) { + cb = arguments[ 2 ]; + ctx = arguments[ 3 ]; + if ( isInteger( arguments[ 1 ] ) ) { + fidx = arguments[ 1 ]; + } else if ( isndarrayLike( arguments[ 1 ] ) ) { + fidx = arguments[ 1 ]; + iflg = false; + } else { + options = arguments[ 1 ]; + hasOptions = true; + } + } + // Case: findIndex( x, from_index, options, clbk ) + else if ( isFunction( arguments[ 3 ] ) ) { + cb = arguments[ 3 ]; + if ( isInteger( arguments[ 1 ] ) ) { + fidx = arguments[ 1 ]; + } else if ( isndarrayLike( arguments[ 1 ] ) ) { + fidx = arguments[ 1 ]; + iflg = false; + } else { + throw new TypeError( format( 'invalid argument. Second argument must be either an ndarray or an integer. Value: `%s`.', arguments[ 1 ] ) ); + } + options = arguments[ 2 ]; + hasOptions = true; + } + // Case: findIndex( x, options, clbk, thisArg ) + else { + options = arguments[ 1 ]; + cb = arguments[ 2 ]; + if ( !isFunction( cb ) ) { + throw new TypeError( format( 'invalid argument. Third argument must be a function. Value: `%s`.', cb ) ); + } + ctx = arguments[ 3 ]; + hasOptions = true; + } + } + // Case: findIndex( x, from_index, options, clbk, thisArg ) else { - options = arguments[ 1 ]; - cb = arguments[ 2 ]; - ctx = arguments[ 3 ]; + if ( isInteger( arguments[ 1 ] ) ) { + fidx = arguments[ 1 ]; + } else if ( isndarrayLike( arguments[ 1 ] ) ) { + fidx = arguments[ 1 ]; + iflg = false; + } else { + throw new TypeError( format( 'invalid argument. Second argument must be either an ndarray or an integer. Value: `%s`.', arguments[ 1 ] ) ); + } + options = arguments[ 2 ]; + cb = arguments[ 3 ]; + ctx = arguments[ 4 ]; if ( !isFunction( cb ) ) { - throw new TypeError( format( 'invalid argument. Third argument must be a function. Value: `%s`.', cb ) ); + throw new TypeError( format( 'invalid argument. Fourth argument must be a function. Value: `%s`.', cb ) ); } hasOptions = true; } @@ -148,7 +241,17 @@ function findIndex( x ) { if ( ndims( x ) < 1 ) { throw new RangeError( 'invalid argument. First argument must have at least one dimension.' ); } - return base( x, opts, cb, ctx ); + // Resolve the list of non-reduced dimensions: + sh = getShape( x ); + sh = nonCoreShape( sh, opts.dims ); + + // Broadcast the `fromIndex` to match the shape of the non-reduced dimensions... + if ( iflg ) { + fidx = broadcastScalar( fidx, DEFAULT_DTYPE, sh, ord ); + } else { + fidx = maybeBroadcastArray( fidx, sh ); + } + return base( x, fidx, opts, cb, ctx ); } diff --git a/lib/node_modules/@stdlib/blas/ext/find-index/test/test.assign.js b/lib/node_modules/@stdlib/blas/ext/find-index/test/test.assign.js index 4e7a99ac5e19..ddb813105b87 100644 --- a/lib/node_modules/@stdlib/blas/ext/find-index/test/test.assign.js +++ b/lib/node_modules/@stdlib/blas/ext/find-index/test/test.assign.js @@ -27,6 +27,7 @@ var zeros = require( '@stdlib/ndarray/zeros' ); var ndarray2array = require( '@stdlib/ndarray/to-array' ); var getDType = require( '@stdlib/ndarray/dtype' ); var getShape = require( '@stdlib/ndarray/shape' ); +var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' ); var getOrder = require( '@stdlib/ndarray/order' ); var findIndex = require( './../lib' ).assign; @@ -555,6 +556,74 @@ tape( 'the function throws an error if provided an options argument which is not } }); +tape( 'the function throws an error if provided a `fromIndex` argument which is not an ndarray-like object or an integer', function test( t ) { + var values; + var x; + var y; + var i; + + x = zeros( [ 2, 2 ], { + 'dtype': 'generic' + }); + y = zeros( [ 2 ], { + 'dtype': 'generic' + }); + values = [ + '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() { + findIndex( x, value, y, clbk ); + }; + } +}); + +tape( 'the function throws an error if provided a `fromIndex` argument which is not an ndarray-like object or an integer (options)', function test( t ) { + var values; + var x; + var y; + var i; + + x = zeros( [ 2, 2 ], { + 'dtype': 'generic' + }); + y = zeros( [ 2 ], { + 'dtype': 'generic' + }); + values = [ + '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() { + findIndex( x, value, y, {}, clbk ); + }; + } +}); + tape( 'the function throws an error if provided insufficient number of arguments', function test( t ) { var x; var y; @@ -804,7 +873,7 @@ tape( 'the function supports specifying an operation dimension (column-major)', 'order': 'column-major' }); opts = { - 'dims': 1 + 'dim': 1 }; actual = findIndex( x, y, opts, clbk ); @@ -820,6 +889,218 @@ tape( 'the function supports specifying an operation dimension (column-major)', t.end(); }); +tape( 'the function supports specifying a starting search index (row-major)', function test( t ) { + var expected; + var actual; + var xbuf; + var x; + var y; + + xbuf = [ 1.0, 2.0, 4.0, -3.0, 6.0, -5.0 ]; + x = new ndarray( 'generic', xbuf, [ 2, 3 ], [ 3, 1 ], 0, 'row-major' ); + y = zeros( [ 2 ], { + 'dtype': 'generic', + 'order': 'row-major' + }); + + actual = findIndex( x, 1, y, clbk ); + expected = [ 1, 1 ]; + + t.strictEqual( isndarrayLike( actual ), true, 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + t.strictEqual( ( y === actual ), true, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports specifying a starting search index (column-major)', function test( t ) { + var expected; + var actual; + var xbuf; + var x; + var y; + + xbuf = [ 1.0, -3.0, 2.0, 6.0, 4.0, -5.0 ]; + x = new ndarray( 'generic', xbuf, [ 2, 3 ], [ 1, 2 ], 0, 'column-major' ); + y = zeros( [ 2 ], { + 'dtype': 'generic', + 'order': 'column-major' + }); + + actual = findIndex( x, 1, y, clbk ); + expected = [ 1, 1 ]; + + t.strictEqual( isndarrayLike( actual ), true, 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + t.strictEqual( ( y === actual ), true, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports specifying a starting search index (scalar, options)', function test( t ) { + var expected; + var actual; + var xbuf; + var x; + var y; + + xbuf = [ 1.0, 2.0, 4.0, -3.0, 6.0, -5.0 ]; + x = new ndarray( 'generic', xbuf, [ 2, 3 ], [ 3, 1 ], 0, 'row-major' ); + y = zeros( [ 2 ], { + 'dtype': 'generic', + 'order': 'row-major' + }); + + actual = findIndex( x, 1, y, {}, clbk ); + expected = [ 1, 1 ]; + + t.strictEqual( isndarrayLike( actual ), true, 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + t.strictEqual( ( y === actual ), true, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports specifying a starting search index (0d ndarray)', function test( t ) { + var expected; + var fromIdx; + var actual; + var xbuf; + var x; + var y; + + xbuf = [ 1.0, 2.0, 4.0, -3.0, 6.0, -5.0 ]; + x = new ndarray( 'generic', xbuf, [ 2, 3 ], [ 3, 1 ], 0, 'row-major' ); + y = zeros( [ 2 ], { + 'dtype': 'generic', + 'order': 'row-major' + }); + + fromIdx = scalar2ndarray( 1, { + 'dtype': 'generic' + }); + actual = findIndex( x, fromIdx, y, clbk ); + expected = [ 1, 1 ]; + + t.strictEqual( isndarrayLike( actual ), true, 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + t.strictEqual( ( y === actual ), true, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports specifying a starting search index (0d ndarray, options)', function test( t ) { + var expected; + var fromIdx; + var actual; + var xbuf; + var x; + var y; + + xbuf = [ 1.0, 2.0, 4.0, -3.0, 6.0, -5.0 ]; + x = new ndarray( 'generic', xbuf, [ 2, 3 ], [ 3, 1 ], 0, 'row-major' ); + y = zeros( [ 2 ], { + 'dtype': 'generic', + 'order': 'row-major' + }); + + fromIdx = scalar2ndarray( 1, { + 'dtype': 'int32' + }); + actual = findIndex( x, fromIdx, y, {}, clbk ); + expected = [ 1, 1 ]; + + t.strictEqual( isndarrayLike( actual ), true, 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + t.strictEqual( ( y === actual ), true, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports specifying a starting search index (scalar, broadcasted)', function test( t ) { + var expected; + var actual; + var xbuf; + var x; + var y; + + xbuf = [ 1.0, 2.0, 4.0, -3.0, 6.0, -5.0 ]; + x = new ndarray( 'generic', xbuf, [ 3, 2 ], [ 2, 1 ], 0, 'row-major' ); + y = zeros( [ 2 ], { + 'dtype': 'generic', + 'order': 'row-major' + }); + + actual = findIndex( x, 1, y, { + 'dim': 0 + }, clbk ); + expected = [ 1, -1 ]; + + t.strictEqual( isndarrayLike( actual ), true, 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + t.strictEqual( ( y === actual ), true, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports specifying a starting search index (0d ndarray, broadcasted)', function test( t ) { + var expected; + var fromIdx; + var actual; + var xbuf; + var x; + var y; + + xbuf = [ 1.0, 2.0, 4.0, -3.0, 6.0, -5.0 ]; + x = new ndarray( 'generic', xbuf, [ 3, 2 ], [ 2, 1 ], 0, 'row-major' ); + y = zeros( [ 2 ], { + 'dtype': 'generic', + 'order': 'row-major' + }); + + fromIdx = scalar2ndarray( 1, { + 'dtype': 'int32' + }); + actual = findIndex( x, fromIdx, y, { + 'dim': 0 + }, clbk ); + expected = [ 1, -1 ]; + + t.strictEqual( isndarrayLike( actual ), true, 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + t.strictEqual( ( y === actual ), true, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports specifying a starting search index (1d ndarray)', function test( t ) { + var expected; + var fromIdx; + var actual; + var xbuf; + var x; + var y; + + xbuf = [ 1.0, 2.0, 4.0, -3.0, 6.0, -5.0 ]; + x = new ndarray( 'generic', xbuf, [ 3, 2 ], [ 2, 1 ], 0, 'row-major' ); + y = zeros( [ 2 ], { + 'dtype': 'generic', + 'order': 'row-major' + }); + + fromIdx = new ndarray( 'generic', [ 1, 0 ], [ 2 ], [ 1 ], 0, 'row-major' ); + actual = findIndex( x, fromIdx, y, { + 'dim': 0 + }, clbk ); + expected = [ 1, 0 ]; + + t.strictEqual( isndarrayLike( actual ), true, 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + t.strictEqual( ( y === actual ), true, 'returns expected value' ); + + t.end(); +}); + tape( 'the function supports providing an execution context', function test( t ) { var expected; var indices; diff --git a/lib/node_modules/@stdlib/blas/ext/find-index/test/test.main.js b/lib/node_modules/@stdlib/blas/ext/find-index/test/test.main.js index a33304ce8b55..41b86f115528 100644 --- a/lib/node_modules/@stdlib/blas/ext/find-index/test/test.main.js +++ b/lib/node_modules/@stdlib/blas/ext/find-index/test/test.main.js @@ -27,6 +27,7 @@ var zeros = require( '@stdlib/ndarray/zeros' ); var ndarray2array = require( '@stdlib/ndarray/to-array' ); var getDType = require( '@stdlib/ndarray/dtype' ); var getShape = require( '@stdlib/ndarray/shape' ); +var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' ); var getOrder = require( '@stdlib/ndarray/order' ); var findIndex = require( './../lib' ); @@ -165,6 +166,118 @@ tape( 'the function throws an error if provided a first argument which is not an } }); +tape( 'the function throws an error if provided a first argument which is not an ndarray-like object (fromIndex)', 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() { + findIndex( value, 0, clbk ); + }; + } +}); + +tape( 'the function throws an error if provided a first argument which is not an ndarray-like object (fromIndex, thisArg)', 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() { + findIndex( value, 0, clbk, {} ); + }; + } +}); + +tape( 'the function throws an error if provided a first argument which is not an ndarray-like object (fromIndex, options)', 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() { + findIndex( value, 0, {}, clbk ); + }; + } +}); + +tape( 'the function throws an error if provided a first argument which is not an ndarray-like object (fromIndex, options, thisArg)', 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() { + findIndex( value, 0, {}, clbk, {} ); + }; + } +}); + tape( 'the function throws an error if provided a zero-dimensional input ndarray-like object', function test( t ) { var values; var i; @@ -360,6 +473,66 @@ tape( 'the function throws an error if provided an options argument which is not } }); +tape( 'the function throws an error if provided a `fromIndex` argument which is not an ndarray-like object or an integer', function test( t ) { + var values; + var x; + var i; + + x = zeros( [ 2, 2 ], { + 'dtype': 'generic' + }); + values = [ + '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() { + findIndex( x, value, {}, clbk ); + }; + } +}); + +tape( 'the function throws an error if provided a `fromIndex` argument which is not an ndarray-like object or an integer (thisArg)', function test( t ) { + var values; + var x; + var i; + + x = zeros( [ 2, 2 ], { + 'dtype': 'generic' + }); + values = [ + '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() { + findIndex( x, value, {}, clbk, {} ); + }; + } +}); + tape( 'the function throws an error if provided insufficient number of arguments', function test( t ) { var x; @@ -686,6 +859,170 @@ tape( 'the function supports specifying the output array data type', function te t.end(); }); +tape( 'the function supports specifying a starting search index (row-major)', function test( t ) { + var expected; + var actual; + var xbuf; + var x; + + xbuf = [ 1.0, 2.0, 4.0, -3.0, 6.0, -5.0 ]; + x = new ndarray( 'generic', xbuf, [ 2, 3 ], [ 3, 1 ], 0, 'row-major' ); + + actual = findIndex( x, 1, clbk ); + expected = [ 1, 1 ]; + + t.strictEqual( isndarrayLike( actual ), true, 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports specifying a starting search index (column-major)', function test( t ) { + var expected; + var actual; + var xbuf; + var x; + + xbuf = [ 1.0, -3.0, 2.0, 6.0, 4.0, -5.0 ]; + x = new ndarray( 'generic', xbuf, [ 2, 3 ], [ 1, 2 ], 0, 'column-major' ); + + actual = findIndex( x, 1, clbk ); + expected = [ 1, 1 ]; + + t.strictEqual( isndarrayLike( actual ), true, 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports specifying a starting search index (scalar, options)', function test( t ) { + var expected; + var actual; + var xbuf; + var x; + + xbuf = [ 1.0, 2.0, 4.0, -3.0, 6.0, -5.0 ]; + x = new ndarray( 'generic', xbuf, [ 2, 3 ], [ 3, 1 ], 0, 'row-major' ); + + actual = findIndex( x, 1, {}, clbk ); + expected = [ 1, 1 ]; + + t.strictEqual( isndarrayLike( actual ), true, 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports specifying a starting search index (0d ndarray)', function test( t ) { + var expected; + var fromIdx; + var actual; + var xbuf; + var x; + + xbuf = [ 1.0, 2.0, 4.0, -3.0, 6.0, -5.0 ]; + x = new ndarray( 'generic', xbuf, [ 2, 3 ], [ 3, 1 ], 0, 'row-major' ); + + fromIdx = scalar2ndarray( 1, { + 'dtype': 'generic' + }); + actual = findIndex( x, fromIdx, clbk ); + expected = [ 1, 1 ]; + + t.strictEqual( isndarrayLike( actual ), true, 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports specifying a starting search index (0d ndarray, options)', function test( t ) { + var expected; + var fromIdx; + var actual; + var xbuf; + var x; + + xbuf = [ 1.0, 2.0, 4.0, -3.0, 6.0, -5.0 ]; + x = new ndarray( 'generic', xbuf, [ 2, 3 ], [ 3, 1 ], 0, 'row-major' ); + + fromIdx = scalar2ndarray( 1, { + 'dtype': 'int32' + }); + actual = findIndex( x, fromIdx, {}, clbk ); + expected = [ 1, 1 ]; + + t.strictEqual( isndarrayLike( actual ), true, 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports specifying a starting search index (scalar, broadcasted)', function test( t ) { + var expected; + var actual; + var xbuf; + var x; + + xbuf = [ 1.0, 2.0, 4.0, -3.0, 6.0, -5.0 ]; + x = new ndarray( 'generic', xbuf, [ 3, 2 ], [ 2, 1 ], 0, 'row-major' ); + + actual = findIndex( x, 1, { + 'dim': 0 + }, clbk ); + expected = [ 1, -1 ]; + + t.strictEqual( isndarrayLike( actual ), true, 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports specifying a starting search index (0d ndarray, broadcasted)', function test( t ) { + var expected; + var fromIdx; + var actual; + var xbuf; + var x; + + xbuf = [ 1.0, 2.0, 4.0, -3.0, 6.0, -5.0 ]; + x = new ndarray( 'generic', xbuf, [ 3, 2 ], [ 2, 1 ], 0, 'row-major' ); + + fromIdx = scalar2ndarray( 1, { + 'dtype': 'int32' + }); + actual = findIndex( x, fromIdx, { + 'dim': 0 + }, clbk ); + expected = [ 1, -1 ]; + + t.strictEqual( isndarrayLike( actual ), true, 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports specifying a starting search index (1d ndarray)', function test( t ) { + var expected; + var fromIdx; + var actual; + var xbuf; + var x; + + xbuf = [ 1.0, 2.0, 4.0, -3.0, 6.0, -5.0 ]; + x = new ndarray( 'generic', xbuf, [ 3, 2 ], [ 2, 1 ], 0, 'row-major' ); + + fromIdx = new ndarray( 'generic', [ 1, 0 ], [ 2 ], [ 1 ], 0, 'row-major' ); + actual = findIndex( x, fromIdx, { + 'dim': 0 + }, clbk ); + expected = [ 1, 0 ]; + + t.strictEqual( isndarrayLike( actual ), true, 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + t.end(); +}); + tape( 'the function supports providing an execution context', function test( t ) { var expected; var indices; From aaf0e3799f0558cdd6b8b84f3e221a9c1a112103 Mon Sep 17 00:00:00 2001 From: headlessNode Date: Sat, 22 Aug 2026 17:00:49 +0500 Subject: [PATCH 2/4] fix: lint errs --- lib/node_modules/@stdlib/blas/ext/find-index/docs/types/test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/node_modules/@stdlib/blas/ext/find-index/docs/types/test.ts b/lib/node_modules/@stdlib/blas/ext/find-index/docs/types/test.ts index 2ffb6004ed7c..a81ed5449d51 100644 --- a/lib/node_modules/@stdlib/blas/ext/find-index/docs/types/test.ts +++ b/lib/node_modules/@stdlib/blas/ext/find-index/docs/types/test.ts @@ -16,7 +16,7 @@ * limitations under the License. */ -/* eslint-disable space-in-parens */ +/* eslint-disable @typescript-eslint/no-unused-expressions, space-in-parens */ /// From 98021578f5cb80764913e547208ade1a27467277 Mon Sep 17 00:00:00 2001 From: headlessNode Date: Sat, 22 Aug 2026 17:05:48 +0500 Subject: [PATCH 3/4] fix: lint errs --- .../@stdlib/blas/ext/find-index/docs/types/test.ts | 6 ------ 1 file changed, 6 deletions(-) diff --git a/lib/node_modules/@stdlib/blas/ext/find-index/docs/types/test.ts b/lib/node_modules/@stdlib/blas/ext/find-index/docs/types/test.ts index a81ed5449d51..b0d18c254432 100644 --- a/lib/node_modules/@stdlib/blas/ext/find-index/docs/types/test.ts +++ b/lib/node_modules/@stdlib/blas/ext/find-index/docs/types/test.ts @@ -200,7 +200,6 @@ function clbk( value: any ): boolean { findIndex( x, { 'dtype': null }, clbk ); // $ExpectError findIndex( x, { 'dtype': [] }, clbk ); // $ExpectError findIndex( x, { 'dtype': {} }, clbk ); // $ExpectError - findIndex( x, { 'dtype': ( x: number ): number => x }, clbk ); // $ExpectError findIndex( x, { 'dtype': '5' }, clbk, {} ); // $ExpectError findIndex( x, { 'dtype': 5 }, clbk, {} ); // $ExpectError @@ -209,7 +208,6 @@ function clbk( value: any ): boolean { findIndex( x, { 'dtype': null }, clbk, {} ); // $ExpectError findIndex( x, { 'dtype': [] }, clbk, {} ); // $ExpectError findIndex( x, { 'dtype': {} }, clbk, {} ); // $ExpectError - findIndex( x, { 'dtype': ( x: number ): number => x }, clbk, {} ); // $ExpectError } // The compiler throws an error if the function is provided an invalid `keepdims` option... @@ -223,14 +221,12 @@ function clbk( value: any ): boolean { findIndex( x, { 'keepdims': null }, clbk ); // $ExpectError findIndex( x, { 'keepdims': [] }, clbk ); // $ExpectError findIndex( x, { 'keepdims': {} }, clbk ); // $ExpectError - findIndex( x, { 'keepdims': ( x: number ): number => x }, clbk ); // $ExpectError findIndex( x, { 'keepdims': '5' }, clbk, {} ); // $ExpectError findIndex( x, { 'keepdims': 5 }, clbk, {} ); // $ExpectError findIndex( x, { 'keepdims': null }, clbk, {} ); // $ExpectError findIndex( x, { 'keepdims': [] }, clbk, {} ); // $ExpectError findIndex( x, { 'keepdims': {} }, clbk, {} ); // $ExpectError - findIndex( x, { 'keepdims': ( x: number ): number => x }, clbk, {} ); // $ExpectError } // The compiler throws an error if the function is provided an invalid `dim` option... @@ -244,14 +240,12 @@ function clbk( value: any ): boolean { findIndex( x, { 'dim': false }, clbk ); // $ExpectError findIndex( x, { 'dim': null }, clbk ); // $ExpectError findIndex( x, { 'dim': {} }, clbk ); // $ExpectError - findIndex( x, { 'dim': ( x: number ): number => x }, clbk ); // $ExpectError findIndex( x, { 'dim': '5' }, clbk, {} ); // $ExpectError findIndex( x, { 'dim': true }, clbk, {} ); // $ExpectError findIndex( x, { 'dim': false }, clbk, {} ); // $ExpectError findIndex( x, { 'dim': null }, clbk, {} ); // $ExpectError findIndex( x, { 'dim': {} }, clbk, {} ); // $ExpectError - findIndex( x, { 'dim': ( x: number ): number => x }, clbk, {} ); // $ExpectError } // The compiler throws an error if the function is provided an unsupported number of arguments... From b821ec76e4485841697cd27619ad0e1a2d216f9e Mon Sep 17 00:00:00 2001 From: headlessNode Date: Sat, 22 Aug 2026 17:11:52 +0500 Subject: [PATCH 4/4] refactor: add missing tests in test.ts --- .../@stdlib/blas/ext/find-index/docs/types/test.ts | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/lib/node_modules/@stdlib/blas/ext/find-index/docs/types/test.ts b/lib/node_modules/@stdlib/blas/ext/find-index/docs/types/test.ts index b0d18c254432..a81ed5449d51 100644 --- a/lib/node_modules/@stdlib/blas/ext/find-index/docs/types/test.ts +++ b/lib/node_modules/@stdlib/blas/ext/find-index/docs/types/test.ts @@ -200,6 +200,7 @@ function clbk( value: any ): boolean { findIndex( x, { 'dtype': null }, clbk ); // $ExpectError findIndex( x, { 'dtype': [] }, clbk ); // $ExpectError findIndex( x, { 'dtype': {} }, clbk ); // $ExpectError + findIndex( x, { 'dtype': ( x: number ): number => x }, clbk ); // $ExpectError findIndex( x, { 'dtype': '5' }, clbk, {} ); // $ExpectError findIndex( x, { 'dtype': 5 }, clbk, {} ); // $ExpectError @@ -208,6 +209,7 @@ function clbk( value: any ): boolean { findIndex( x, { 'dtype': null }, clbk, {} ); // $ExpectError findIndex( x, { 'dtype': [] }, clbk, {} ); // $ExpectError findIndex( x, { 'dtype': {} }, clbk, {} ); // $ExpectError + findIndex( x, { 'dtype': ( x: number ): number => x }, clbk, {} ); // $ExpectError } // The compiler throws an error if the function is provided an invalid `keepdims` option... @@ -221,12 +223,14 @@ function clbk( value: any ): boolean { findIndex( x, { 'keepdims': null }, clbk ); // $ExpectError findIndex( x, { 'keepdims': [] }, clbk ); // $ExpectError findIndex( x, { 'keepdims': {} }, clbk ); // $ExpectError + findIndex( x, { 'keepdims': ( x: number ): number => x }, clbk ); // $ExpectError findIndex( x, { 'keepdims': '5' }, clbk, {} ); // $ExpectError findIndex( x, { 'keepdims': 5 }, clbk, {} ); // $ExpectError findIndex( x, { 'keepdims': null }, clbk, {} ); // $ExpectError findIndex( x, { 'keepdims': [] }, clbk, {} ); // $ExpectError findIndex( x, { 'keepdims': {} }, clbk, {} ); // $ExpectError + findIndex( x, { 'keepdims': ( x: number ): number => x }, clbk, {} ); // $ExpectError } // The compiler throws an error if the function is provided an invalid `dim` option... @@ -240,12 +244,14 @@ function clbk( value: any ): boolean { findIndex( x, { 'dim': false }, clbk ); // $ExpectError findIndex( x, { 'dim': null }, clbk ); // $ExpectError findIndex( x, { 'dim': {} }, clbk ); // $ExpectError + findIndex( x, { 'dim': ( x: number ): number => x }, clbk ); // $ExpectError findIndex( x, { 'dim': '5' }, clbk, {} ); // $ExpectError findIndex( x, { 'dim': true }, clbk, {} ); // $ExpectError findIndex( x, { 'dim': false }, clbk, {} ); // $ExpectError findIndex( x, { 'dim': null }, clbk, {} ); // $ExpectError findIndex( x, { 'dim': {} }, clbk, {} ); // $ExpectError + findIndex( x, { 'dim': ( x: number ): number => x }, clbk, {} ); // $ExpectError } // The compiler throws an error if the function is provided an unsupported number of arguments...