From 8143b618ed4700c89b1270828283e02320d235ab Mon Sep 17 00:00:00 2001 From: Muhammad Haris <101793258+headlessNode@users.noreply.github.com> Date: Thu, 7 Aug 2025 06:01:18 +0000 Subject: [PATCH] feat: add blas/ext/find-last-index --- .../blas/ext/find-last-index/README.md | 312 ++++++++ .../benchmark/benchmark.assign.js | 124 ++++ .../find-last-index/benchmark/benchmark.js | 116 +++ .../blas/ext/find-last-index/docs/repl.txt | 115 +++ .../ext/find-last-index/docs/types/index.d.ts | 276 +++++++ .../ext/find-last-index/docs/types/test.ts | 405 ++++++++++ .../ext/find-last-index/examples/index.js | 48 ++ .../blas/ext/find-last-index/lib/assign.js | 171 +++++ .../blas/ext/find-last-index/lib/base.js | 96 +++ .../blas/ext/find-last-index/lib/index.js | 74 ++ .../blas/ext/find-last-index/lib/main.js | 164 +++++ .../blas/ext/find-last-index/package.json | 62 ++ .../ext/find-last-index/test/test.assign.js | 694 ++++++++++++++++++ .../blas/ext/find-last-index/test/test.js | 39 + .../ext/find-last-index/test/test.main.js | 680 +++++++++++++++++ 15 files changed, 3376 insertions(+) create mode 100644 lib/node_modules/@stdlib/blas/ext/find-last-index/README.md create mode 100644 lib/node_modules/@stdlib/blas/ext/find-last-index/benchmark/benchmark.assign.js create mode 100644 lib/node_modules/@stdlib/blas/ext/find-last-index/benchmark/benchmark.js create mode 100644 lib/node_modules/@stdlib/blas/ext/find-last-index/docs/repl.txt create mode 100644 lib/node_modules/@stdlib/blas/ext/find-last-index/docs/types/index.d.ts create mode 100644 lib/node_modules/@stdlib/blas/ext/find-last-index/docs/types/test.ts create mode 100644 lib/node_modules/@stdlib/blas/ext/find-last-index/examples/index.js create mode 100644 lib/node_modules/@stdlib/blas/ext/find-last-index/lib/assign.js create mode 100644 lib/node_modules/@stdlib/blas/ext/find-last-index/lib/base.js create mode 100644 lib/node_modules/@stdlib/blas/ext/find-last-index/lib/index.js create mode 100644 lib/node_modules/@stdlib/blas/ext/find-last-index/lib/main.js create mode 100644 lib/node_modules/@stdlib/blas/ext/find-last-index/package.json create mode 100644 lib/node_modules/@stdlib/blas/ext/find-last-index/test/test.assign.js create mode 100644 lib/node_modules/@stdlib/blas/ext/find-last-index/test/test.js create mode 100644 lib/node_modules/@stdlib/blas/ext/find-last-index/test/test.main.js diff --git a/lib/node_modules/@stdlib/blas/ext/find-last-index/README.md b/lib/node_modules/@stdlib/blas/ext/find-last-index/README.md new file mode 100644 index 000000000000..0c5ef7acb456 --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/find-last-index/README.md @@ -0,0 +1,312 @@ + + +# findLastIndex + +> Return the index of the last element along an [ndarray][@stdlib/ndarray/ctor] dimension which passes a test implemented by a predicate function. + +
+ +## Usage + +```javascript +var findLastIndex = require( '@stdlib/blas/ext/find-last-index' ); +``` + +#### findLastIndex( x\[, options], clbk\[, thisArg] ) + +Returns the index of the last element along an [ndarray][@stdlib/ndarray/ctor] dimension which passes a test implemented by a predicate function. + +```javascript +var array = require( '@stdlib/ndarray/array' ); + +function isEven( v ) { + return v % 2.0 === 0.0; +} + +// Create an input ndarray: +var x = array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] ); +// returns + +// Perform operation: +var out = findLastIndex( x, isEven ); +// returns + +var idx = out.get(); +// returns 5 +``` + +The function has the following parameters: + +- **x**: input [ndarray][@stdlib/ndarray/ctor]. Must have at least one dimension. +- **options**: function options (_optional_). +- **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 array = require( '@stdlib/ndarray/array' ); + +function isEven( v ) { + this.count += 1; + return v % 2.0 === 0.0; +} + +var x = array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] ); + +var ctx = { + 'count': 0 +}; +var out = findLastIndex( x, isEven, ctx ); +// returns + +var idx = out.get(); +// returns 5 + +var count = ctx.count; +// returns 6 +``` + +The function accepts the following options: + +- **dtype**: output ndarray [data type][@stdlib/ndarray/dtypes]. Must be an integer index or generic [data type][@stdlib/ndarray/dtypes]. +- **dim**: dimension over which to perform operation. If provided a negative integer, the dimension along which to perform the operation is determined by counting backward from the last dimension (where `-1` refers to the last dimension). Default: `-1`. +- **keepdims**: boolean indicating whether the reduced dimensions should be included in the returned [ndarray][@stdlib/ndarray/ctor] as singleton dimensions. Default: `false`. + +If no element along an [ndarray][@stdlib/ndarray/ctor] passes a test implemented by the predicate function, the corresponding element in the returned [ndarray][@stdlib/ndarray/ctor] is `-1`. + +```javascript +var array = require( '@stdlib/ndarray/array' ); + +function isEven( v ) { + return v % 2.0 === 0.0; +} + +// Create an input ndarray: +var x = array( [ 1.0, 3.0, 5.0, 7.0 ] ); +// returns + +// Perform operation: +var out = findLastIndex( x, isEven ); +// returns + +var idx = out.get(); +// returns -1 +``` + +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 +var ndarray2array = require( '@stdlib/ndarray/to-array' ); +var array = require( '@stdlib/ndarray/array' ); + +function isEven( v ) { + return v % 2.0 === 0.0; +} + +var x = array( [ [ -1.0, 2.0 ], [ -3.0, 4.0 ] ] ); + +var opts = { + 'dim': 0 +}; + +var out = findLastIndex( x, opts, isEven ); +// returns + +var idx = ndarray2array( out ); +// returns [ -1, 1 ] +``` + +By default, the function excludes reduced dimensions from the output [ndarray][@stdlib/ndarray/ctor]. To include the reduced dimensions as singleton dimensions, set the `keepdims` option to `true`. + +```javascript +var array = require( '@stdlib/ndarray/array' ); +var ndarray2array = require( '@stdlib/ndarray/to-array' ); + +function isEven( v ) { + return v % 2.0 === 0.0; +} + +var x = array( [ [ -1.0, 2.0 ], [ -3.0, 4.0 ] ] ); + +var opts = { + 'dim': 0, + 'keepdims': true +}; + +var out = findLastIndex( x, opts, isEven ); +// returns + +var idx = ndarray2array( out ); +// returns [ [ -1, 1 ] ] +``` + +By default, the function returns an [ndarray][@stdlib/ndarray/ctor] having a [data type][@stdlib/ndarray/dtypes] determined by the function's output data type [policy][@stdlib/ndarray/output-dtype-policies]. To override the default behavior, set the `dtype` option. + +```javascript +var ndarray2array = require( '@stdlib/ndarray/to-array' ); +var dtype = require( '@stdlib/ndarray/dtype' ); +var array = require( '@stdlib/ndarray/array' ); + +function isEven( v ) { + return v % 2.0 === 0.0; +} + +var x = array( [ 1.0, 2.0, 3.0, 4.0 ] ); + +var opts = { + 'dtype': 'generic' +}; + +var idx = findLastIndex( x, opts, isEven ); +// returns + +var dt = dtype( idx ); +// returns 'generic' +``` + +#### findLastIndex.assign( x, out\[, options], clbk\[, thisArg] ) + +Returns the index of the last 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]. + +```javascript +var array = require( '@stdlib/ndarray/array' ); +var zeros = require( '@stdlib/ndarray/zeros' ); + +function isEven( v ) { + return v % 2.0 === 0.0; +} + +var x = array( [ 1.0, 2.0, 3.0, 4.0 ] ); +var y = zeros( [], { + 'dtype': 'int32' +}); + +var out = findLastIndex.assign( x, y, isEven ); +// returns + +var idx = out.get(); +// returns 1 + +var bool = ( out === y ); +// returns true +``` + +The method has the following parameters: + +- **x**: input [ndarray][@stdlib/ndarray/ctor]. Must have at least one dimension. +- **out**: output [ndarray][@stdlib/ndarray/ctor]. +- **options**: function options (_optional_). +- **clbk**: callback function. +- **thisArg**: callback execution context (_optional_). + +The method accepts the following options: + +- **dim**: dimension over which to perform operation. If provided a negative integer, the dimension along which to perform the operation is determined by counting backward from the last dimension (where `-1` refers to the last dimension). Default: `-1`. + +
+ + + +
+ +## Notes + +- A provided callback function should return a boolean. +- Setting the `keepdims` option to `true` can be useful when wanting to ensure that the output [ndarray][@stdlib/ndarray/ctor] is [broadcast-compatible][@stdlib/ndarray/base/broadcast-shapes] with ndarrays having the same shape as the input [ndarray][@stdlib/ndarray/ctor]. +- The output data type [policy][@stdlib/ndarray/output-dtype-policies] only applies to the main function and specifies that, by default, the function must return an [ndarray][@stdlib/ndarray/ctor] having an integer index or "generic" [data type][@stdlib/ndarray/dtypes]. For the `assign` method, the output [ndarray][@stdlib/ndarray/ctor] is allowed to have any supported output [data type][@stdlib/ndarray/dtypes]. + +
+ + + +
+ +## Examples + + + +```javascript +var discreteUniform = require( '@stdlib/random/array/discrete-uniform' ); +var ndarray2array = require( '@stdlib/ndarray/to-array' ); +var ndarray = require( '@stdlib/ndarray/ctor' ); +var findLastIndex = require( '@stdlib/blas/ext/find-last-index' ); + +// Define a callback function: +function isEven( v ) { + return v % 2.0 === 0.0; +} + +// Generate an array of random numbers: +var xbuf = discreteUniform( 10, 0, 20, { + 'dtype': 'generic' +}); + +// Wrap in an ndarray: +var x = new ndarray( 'generic', xbuf, [ 5, 2 ], [ 2, 1 ], 0, 'row-major' ); +console.log( ndarray2array( x ) ); + +var opts = { + 'dim': 0 +}; + +// Perform operation: +var idx = findLastIndex( x, opts, isEven ); + +// Print the results: +console.log( ndarray2array( idx ) ); +``` + +
+ + + + + + + + + + + + + + diff --git a/lib/node_modules/@stdlib/blas/ext/find-last-index/benchmark/benchmark.assign.js b/lib/node_modules/@stdlib/blas/ext/find-last-index/benchmark/benchmark.assign.js new file mode 100644 index 000000000000..baa11a25a030 --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/find-last-index/benchmark/benchmark.assign.js @@ -0,0 +1,124 @@ +/** +* @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 isnan = require( '@stdlib/math/base/assert/is-nan' ); +var pow = require( '@stdlib/math/base/special/pow' ); +var uniform = require( '@stdlib/random/array/uniform' ); +var zeros = require( '@stdlib/ndarray/zeros' ); +var ndarray = require( '@stdlib/ndarray/base/ctor' ); +var pkg = require( './../package.json' ).name; +var findLastIndex = require( './../lib' ); + + +// VARIABLES // + +var options = { + 'dtype': 'float64' +}; + + +// FUNCTIONS // + +/** +* Callback function. +* +* @private +* @param {number} v - input value +* @returns {boolean} result +*/ +function clbk( v ) { + return v < 0.0; +} + +/** +* Creates a benchmark function. +* +* @private +* @param {PositiveInteger} len - array length +* @returns {Function} benchmark function +*/ +function createBenchmark( len ) { + var out; + var x; + + x = uniform( len, 0.0, 100.0, options ); + x = new ndarray( options.dtype, x, [ len ], [ 1 ], 0, 'row-major' ); + + out = zeros( [], { + 'dtype': 'int32' + }); + + return benchmark; + + /** + * Benchmark function. + * + * @private + * @param {Benchmark} b - benchmark instance + */ + function benchmark( b ) { + var o; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + o = findLastIndex.assign( x, out, clbk ); + if ( typeof o !== 'object' ) { + b.fail( 'should return an ndarray' ); + } + } + b.toc(); + if ( isnan( o.get() ) ) { + 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+':assign:dtype='+options.dtype+',len='+len, f ); + } +} + +main(); diff --git a/lib/node_modules/@stdlib/blas/ext/find-last-index/benchmark/benchmark.js b/lib/node_modules/@stdlib/blas/ext/find-last-index/benchmark/benchmark.js new file mode 100644 index 000000000000..8e24fe67f117 --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/find-last-index/benchmark/benchmark.js @@ -0,0 +1,116 @@ +/** +* @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 isnan = require( '@stdlib/math/base/assert/is-nan' ); +var pow = require( '@stdlib/math/base/special/pow' ); +var uniform = require( '@stdlib/random/array/uniform' ); +var ndarray = require( '@stdlib/ndarray/base/ctor' ); +var pkg = require( './../package.json' ).name; +var findLastIndex = require( './../lib' ); + + +// VARIABLES // + +var options = { + 'dtype': 'float64' +}; + + +// FUNCTIONS // + +/** +* Callback function. +* +* @private +* @param {number} v - input value +* @returns {boolean} result +*/ +function clbk( v ) { + return v < 0.0; +} + +/** +* Creates a benchmark function. +* +* @private +* @param {PositiveInteger} len - array length +* @returns {Function} benchmark function +*/ +function createBenchmark( len ) { + var x = uniform( len, 0.0, 100.0, options ); + x = new ndarray( options.dtype, x, [ len ], [ 1 ], 0, 'row-major' ); + + return benchmark; + + /** + * Benchmark function. + * + * @private + * @param {Benchmark} b - benchmark instance + */ + function benchmark( b ) { + var o; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + o = findLastIndex( x, clbk ); + if ( typeof o !== 'object' ) { + b.fail( 'should return an ndarray' ); + } + } + b.toc(); + if ( isnan( o.get() ) ) { + 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+':dtype='+options.dtype+',len='+len, f ); + } +} + +main(); diff --git a/lib/node_modules/@stdlib/blas/ext/find-last-index/docs/repl.txt b/lib/node_modules/@stdlib/blas/ext/find-last-index/docs/repl.txt new file mode 100644 index 000000000000..3e2b64317cfd --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/find-last-index/docs/repl.txt @@ -0,0 +1,115 @@ + +{{alias}}( x[, options], clbk[, thisArg] ) + Returns the index of the last element along an ndarray dimension which + passes a test implemented by a predicate function. + + The callback function is provided the following arguments: + + - value: current array element. + - index: current array index. + - array: the input ndarray. + + The callback function should return a boolean. + + If no element along an ndarray passes a test implemented by the predicate + function, the corresponding element in the returned ndarray is `-1`. + + Parameters + ---------- + x: ndarray + Input array. Must at least have one dimension. + + options: Object (optional) + Function options. + + options.dtype: string (optional) + Output array data type. Must be an integer index or "generic" data type. + + options.dim: integer (optional) + Dimension over which to perform a reduction. If provided a negative + integer, the dimension along which to perform the operation is + determined by counting backward from the last dimension (where -1 refers + to the last dimension). Default: -1. + + options.keepdims: boolean (optional) + Boolean indicating whether the reduced dimensions should be included in + the returned ndarray as singleton dimensions. Default: false. + + clbk: Function + Callback function. + + thisArg: any (optional) + Callback execution context. + + Returns + ------- + out: ndarray + Output array. + + Examples + -------- + > var x = {{alias:@stdlib/ndarray/array}}( [ -1.0, 2.0, -3.0, -4.0 ] ); + > function clbk( v ) { return v % 2.0 === 0.0; }; + > var y = {{alias}}( x, clbk ); + > var v = y.get() + 3 + + +{{alias}}.assign( x, out[, options], clbk[, thisArg] ) + Returns the index of the last element along an ndarray dimension which + passes a test implemented by a predicate function and assigns results to a + provided output ndarray. + + The callback function is provided the following arguments: + + - value: current array element. + - index: current array index. + - array: the input ndarray. + + The callback function should return a boolean. + + If no element along an ndarray passes a test implemented by the predicate + function, the corresponding element in the returned ndarray is `-1`. + + Parameters + ---------- + x: ndarray + Input array. Must at least have one dimension. + + out: ndarray + Output array. + + options: Object (optional) + Function options. + + options.dim: integer (optional) + Dimension over which to perform a reduction. If provided a negative + integer, the dimension along which to perform the operation is + determined by counting backward from the last dimension (where -1 refers + to the last dimension). Default: -1. + + clbk: Function + Callback function. + + thisArg: any (optional) + Callback execution context. + + Returns + ------- + out: ndarray + Output array. + + Examples + -------- + > var x = {{alias:@stdlib/ndarray/array}}( [ -1.0, 2.0, -3.0, -4.0 ] ); + > var out = {{alias:@stdlib/ndarray/zeros}}( [] ); + > function clbk( v ) { return v % 2.0 === 0.0; }; + > var y = {{alias}}.assign( x, out, clbk ) + + > var bool = ( out === y ) + true + > var v = out.get() + 3 + + See Also + -------- diff --git a/lib/node_modules/@stdlib/blas/ext/find-last-index/docs/types/index.d.ts b/lib/node_modules/@stdlib/blas/ext/find-last-index/docs/types/index.d.ts new file mode 100644 index 000000000000..139845346aff --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/find-last-index/docs/types/index.d.ts @@ -0,0 +1,276 @@ +/* +* @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 { IntegerIndexAndGenericDataType as DataType, typedndarray } from '@stdlib/types/ndarray'; + +/** +* Input array. +*/ +type InputArray = typedndarray; + +/** +* Output array. +*/ +type OutputArray = typedndarray; + +/** +* Returns the result of callback function. +* +* @returns result +*/ +type NullaryCallback = ( this: ThisArg ) => boolean; + +/** +* Returns the result of callback function. +* +* @param value - current array element +* @returns result +*/ +type UnaryCallback = ( this: ThisArg, value: T ) => boolean; + +/** +* Returns the result of callback function. +* +* @param value - current array element +* @param index - current array element index +* @returns result +*/ +type BinaryCallback = ( this: ThisArg, value: T, index: number ) => boolean; + +/** +* Returns the result of callback function. +* +* @param value - current array element +* @param index - current array element index +* @param array - input ndarray +* @returns result +*/ +type TernaryCallback = ( this: ThisArg, value: T, index: number, array: U ) => boolean; + +/** +* 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 = NullaryCallback | UnaryCallback | BinaryCallback | TernaryCallback; + +/** +* Interface defining "base" options. +*/ +interface BaseOptions { + /** + * Dimension over which to perform operation. Default: `-1`. + * + * ## Notes + * + * - If provided a negative integer, the dimension along which to perform the operation is determined by counting backward from the last dimension (where `-1` refers to the last dimension). + */ + dim?: number; +} + +/** +* Interface defining options. +*/ +interface Options extends BaseOptions { + /** + * Output array data type. + */ + dtype?: DataType; + + /** + * Boolean indicating whether the reduced dimensions should be included in the returned array as singleton dimensions. Default: `false`. + */ + keepdims?: boolean; +} + +/** +* Interface describing `findLastIndex` +*/ +interface FindLastIndex { + /** + * Returns the index of the last element along an ndarray dimension which passes a test implemented by a predicate function. + * + * @param x - input ndarray + * @param clbk - callback function + * @param thisArg - callback 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( [ -1.0, 2.0, -3.0 ] ); + * + * var y = findLastIndex( x, clbk ); + * // returns + * + * var v = y.get(); + * // returns 1 + */ + = InputArray, ThisArg = unknown>( x: U, clbk: Callback, thisArg?: ThisParameterType> ): OutputArray; + + /** + * Returns the index of the last element along an ndarray dimension which passes a test implemented by a predicate function. + * + * @param x - input ndarray + * @param options - function options + * @param clbk - callback function + * @param thisArg - callback 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( [ -1.0, 2.0, -3.0 ] ); + * + * var y = findLastIndex( x, {}, clbk ); + * // returns + * + * var v = y.get(); + * // returns 1 + */ + = InputArray, ThisArg = unknown>( x: U, options: Options, clbk: Callback, thisArg?: ThisParameterType> ): OutputArray; + + /** + * Returns the index of the last element along an ndarray dimension which passes a test implemented by a predicate function and assigns results to a provided output ndarray. + * + * @param x - input ndarray + * @param out - output ndarray + * @param clbk - callback function + * @param thisArg - callback 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( [ -1.0, 2.0, -3.0 ] ); + * var y = zeros( [] ); + * + * var out = findLastIndex.assign( x, y, clbk ); + * // returns + * + * var v = out.get(); + * // returns 1 + * + * var bool = ( out === y ); + * // returns true + */ + assign = InputArray, V extends OutputArray = OutputArray, ThisArg = unknown>( x: U, out: V, clbk: Callback, thisArg?: ThisParameterType> ): V; + + /** + * Returns the index of the last element along an ndarray dimension which passes a test implemented by a predicate function and assigns results to a provided output ndarray. + * + * @param x - input ndarray + * @param out - output ndarray + * @param options - function options + * @param clbk - callback function + * @param thisArg - callback 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( [ -1.0, 2.0, -3.0 ] ); + * var y = zeros( [] ); + * + * var out = findLastIndex.assign( x, y, {}, clbk ); + * // returns + * + * var v = out.get(); + * // returns 1 + * + * var bool = ( out === y ); + * // returns true + */ + assign = InputArray, V extends OutputArray = OutputArray, ThisArg = unknown>( x: U, out: V, options: BaseOptions, clbk: Callback, thisArg?: ThisParameterType> ): V; +} + +/** +* Returns the index of the last element along an ndarray dimension which passes a test implemented by a predicate function and assigns results to a provided output ndarray. +* +* @param x - input ndarray +* @param options - function options +* @param clbk - callback function +* @param thisArg - callback execution context +* @returns output ndarray +* +* @example +* var array = require( '@stdlib/ndarray/array' ); +* +* function clbk( value ) { +* return value % 2.0 === 0.0; +* } +* +* var x = array( [ -1.0, 2.0, -3.0 ] ) +* +* var y = findLastIndex( x, clbk ); +* // returns +* +* var v = y.get(); +* // returns 1 +* +* @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( [ -1.0, 2.0, -3.0 ] ) +* var y = zeros( [] ); +* +* var out = findLastIndex.assign( x, y, clbk ); +* // returns +* +* var v = out.get(); +* // returns 1 +* +* var bool = ( out === y ); +* // returns true +*/ +declare const findLastIndex: FindLastIndex; + + +// EXPORTS // + +export = findLastIndex; diff --git a/lib/node_modules/@stdlib/blas/ext/find-last-index/docs/types/test.ts b/lib/node_modules/@stdlib/blas/ext/find-last-index/docs/types/test.ts new file mode 100644 index 000000000000..68128b161921 --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/find-last-index/docs/types/test.ts @@ -0,0 +1,405 @@ +/* +* @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 @typescript-eslint/no-unused-expressions, space-in-parens */ + +/// + +import zeros = require( '@stdlib/ndarray/zeros' ); +import findLastIndex = require( './index' ); + +/** +* Callback function. +* +* @param value - ndarray element +* @returns result +*/ +function clbk( value: any ): boolean { + return value % 2.0 === 0.0; +} + + +// TESTS // + +// The function returns an ndarray... +{ + const x = zeros( [ 2, 2 ], { + 'dtype': 'generic' + }); + + findLastIndex( x, clbk ); // $ExpectType OutputArray + findLastIndex( x, clbk, {} ); // $ExpectType OutputArray + + findLastIndex( x, {}, clbk ); // $ExpectType OutputArray + findLastIndex( x, {}, clbk, {} ); // $ExpectType OutputArray +} + +// The compiler throws an error if the function is provided a first argument which is not an ndarray... +{ + findLastIndex( '5', clbk ); // $ExpectError + findLastIndex( 5, clbk ); // $ExpectError + findLastIndex( true, clbk ); // $ExpectError + findLastIndex( false, clbk ); // $ExpectError + findLastIndex( null, clbk ); // $ExpectError + findLastIndex( void 0, clbk ); // $ExpectError + findLastIndex( {}, clbk ); // $ExpectError + findLastIndex( ( x: number ): number => x, clbk ); // $ExpectError + + findLastIndex( '5', clbk, {} ); // $ExpectError + findLastIndex( 5, clbk, {} ); // $ExpectError + findLastIndex( true, clbk, {} ); // $ExpectError + findLastIndex( false, clbk, {} ); // $ExpectError + findLastIndex( null, clbk, {} ); // $ExpectError + findLastIndex( void 0, clbk, {} ); // $ExpectError + findLastIndex( {}, clbk, {} ); // $ExpectError + findLastIndex( ( x: number ): number => x, clbk, {} ); // $ExpectError + + findLastIndex( '5', {}, clbk ); // $ExpectError + findLastIndex( 5, {}, clbk ); // $ExpectError + findLastIndex( true, {}, clbk ); // $ExpectError + findLastIndex( false, {}, clbk ); // $ExpectError + findLastIndex( null, {}, clbk ); // $ExpectError + findLastIndex( void 0, {}, clbk ); // $ExpectError + findLastIndex( {}, {}, clbk ); // $ExpectError + findLastIndex( ( x: number ): number => x, {}, clbk ); // $ExpectError + + findLastIndex( '5', {}, clbk, {} ); // $ExpectError + findLastIndex( 5, {}, clbk, {} ); // $ExpectError + findLastIndex( true, {}, clbk, {} ); // $ExpectError + findLastIndex( false, {}, clbk, {} ); // $ExpectError + findLastIndex( null, {}, clbk, {} ); // $ExpectError + findLastIndex( void 0, {}, clbk, {} ); // $ExpectError + findLastIndex( {}, {}, clbk, {} ); // $ExpectError + findLastIndex( ( x: number ): number => x, {}, clbk, {} ); // $ExpectError +} + +// The compiler throws an error if the function is provided an options argument which is not an object... +{ + const x = zeros( [ 2, 2 ], { + 'dtype': 'generic' + }); + + findLastIndex( x, '5', clbk ); // $ExpectError + findLastIndex( x, true, clbk ); // $ExpectError + findLastIndex( x, false, clbk ); // $ExpectError + findLastIndex( x, null, clbk ); // $ExpectError + findLastIndex( x, [], clbk ); // $ExpectError + + findLastIndex( x, '5', clbk, {} ); // $ExpectError + findLastIndex( x, true, clbk, {} ); // $ExpectError + findLastIndex( x, false, clbk, {} ); // $ExpectError + findLastIndex( x, null, clbk, {} ); // $ExpectError + findLastIndex( x, [], clbk, {} ); // $ExpectError +} + +// The compiler throws an error if the function is provided a callback argument which is not a function... +{ + const x = zeros( [ 2, 2 ], { + 'dtype': 'generic' + }); + + findLastIndex( x, '5' ); // $ExpectError + findLastIndex( x, true ); // $ExpectError + findLastIndex( x, false ); // $ExpectError + findLastIndex( x, null ); // $ExpectError + findLastIndex( x, [] ); // $ExpectError + + findLastIndex( x, '5', {} ); // $ExpectError + findLastIndex( x, true, {} ); // $ExpectError + findLastIndex( x, false, {} ); // $ExpectError + findLastIndex( x, null, {} ); // $ExpectError + findLastIndex( x, [], {} ); // $ExpectError + + findLastIndex( x, {}, '5', {} ); // $ExpectError + findLastIndex( x, {}, true, {} ); // $ExpectError + findLastIndex( x, {}, false, {} ); // $ExpectError + findLastIndex( x, {}, null, {} ); // $ExpectError + findLastIndex( x, {}, [], {} ); // $ExpectError +} + +// The compiler throws an error if the function is provided an invalid `dtype` option... +{ + const x = zeros( [ 2, 2 ], { + 'dtype': 'generic' + }); + + findLastIndex( x, { 'dtype': '5' }, clbk ); // $ExpectError + findLastIndex( x, { 'dtype': 5 }, clbk ); // $ExpectError + findLastIndex( x, { 'dtype': true }, clbk ); // $ExpectError + findLastIndex( x, { 'dtype': false }, clbk ); // $ExpectError + findLastIndex( x, { 'dtype': null }, clbk ); // $ExpectError + findLastIndex( x, { 'dtype': [] }, clbk ); // $ExpectError + findLastIndex( x, { 'dtype': {} }, clbk ); // $ExpectError + findLastIndex( x, { 'dtype': ( x: number ): number => x }, clbk ); // $ExpectError + + findLastIndex( x, { 'dtype': '5' }, clbk, {} ); // $ExpectError + findLastIndex( x, { 'dtype': 5 }, clbk, {} ); // $ExpectError + findLastIndex( x, { 'dtype': true }, clbk, {} ); // $ExpectError + findLastIndex( x, { 'dtype': false }, clbk, {} ); // $ExpectError + findLastIndex( x, { 'dtype': null }, clbk, {} ); // $ExpectError + findLastIndex( x, { 'dtype': [] }, clbk, {} ); // $ExpectError + findLastIndex( x, { 'dtype': {} }, clbk, {} ); // $ExpectError + findLastIndex( x, { 'dtype': ( x: number ): number => x }, clbk, {} ); // $ExpectError +} + +// The compiler throws an error if the function is provided an invalid `keepdims` option... +{ + const x = zeros( [ 2, 2 ], { + 'dtype': 'generic' + }); + + findLastIndex( x, { 'keepdims': '5' }, clbk ); // $ExpectError + findLastIndex( x, { 'keepdims': 5 }, clbk ); // $ExpectError + findLastIndex( x, { 'keepdims': null }, clbk ); // $ExpectError + findLastIndex( x, { 'keepdims': [] }, clbk ); // $ExpectError + findLastIndex( x, { 'keepdims': {} }, clbk ); // $ExpectError + findLastIndex( x, { 'keepdims': ( x: number ): number => x }, clbk ); // $ExpectError + + findLastIndex( x, { 'keepdims': '5' }, clbk, {} ); // $ExpectError + findLastIndex( x, { 'keepdims': 5 }, clbk, {} ); // $ExpectError + findLastIndex( x, { 'keepdims': null }, clbk, {} ); // $ExpectError + findLastIndex( x, { 'keepdims': [] }, clbk, {} ); // $ExpectError + findLastIndex( x, { 'keepdims': {} }, clbk, {} ); // $ExpectError + findLastIndex( x, { 'keepdims': ( x: number ): number => x }, clbk, {} ); // $ExpectError +} + +// The compiler throws an error if the function is provided an invalid `dim` option... +{ + const x = zeros( [ 2, 2 ], { + 'dtype': 'generic' + }); + + findLastIndex( x, { 'dim': '5' }, clbk ); // $ExpectError + findLastIndex( x, { 'dim': true }, clbk ); // $ExpectError + findLastIndex( x, { 'dim': false }, clbk ); // $ExpectError + findLastIndex( x, { 'dim': null }, clbk ); // $ExpectError + findLastIndex( x, { 'dim': {} }, clbk ); // $ExpectError + findLastIndex( x, { 'dim': ( x: number ): number => x }, clbk ); // $ExpectError + + findLastIndex( x, { 'dim': '5' }, clbk, {} ); // $ExpectError + findLastIndex( x, { 'dim': 5 }, clbk, {} ); // $ExpectError + findLastIndex( x, { 'dim': true }, clbk, {} ); // $ExpectError + findLastIndex( x, { 'dim': false }, clbk, {} ); // $ExpectError + findLastIndex( x, { 'dim': null }, clbk, {} ); // $ExpectError + findLastIndex( x, { 'dim': {} }, clbk, {} ); // $ExpectError + findLastIndex( x, { 'dim': ( x: number ): number => x }, clbk, {} ); // $ExpectError +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + const x = zeros( [ 2, 2 ], { + 'dtype': 'generic' + }); + + findLastIndex(); // $ExpectError + findLastIndex( x ); + findLastIndex( x, {}, clbk, {}, {} ); // $ExpectError +} + +// Attached to the function is an `assign` method which returns an ndarray... +{ + const x = zeros( [ 2, 2 ], { + 'dtype': 'generic' + }); + const y = zeros( [], { + 'dtype': 'int32' + }); + + findLastIndex.assign( x, y, clbk ); // $ExpectType int32ndarray + findLastIndex.assign( x, y, {}, clbk ); // $ExpectType int32ndarray + + findLastIndex.assign( x, y, clbk, {} ); // $ExpectType int32ndarray + findLastIndex.assign( x, y, {}, clbk, {} ); // $ExpectType int32ndarray +} + +// The compiler throws an error if the `assign` method is provided a first argument which is not an ndarray... +{ + const y = zeros( [], { + 'dtype': 'int32' + }); + + findLastIndex.assign( '5', y, clbk ); // $ExpectError + findLastIndex.assign( 5, y, clbk ); // $ExpectError + findLastIndex.assign( true, y, clbk ); // $ExpectError + findLastIndex.assign( false, y, clbk ); // $ExpectError + findLastIndex.assign( null, y, clbk ); // $ExpectError + findLastIndex.assign( void 0, y, clbk ); // $ExpectError + findLastIndex.assign( {}, y, clbk ); // $ExpectError + findLastIndex.assign( ( x: number ): number => x, y, clbk ); // $ExpectError + + findLastIndex.assign( '5', y, {}, clbk ); // $ExpectError + findLastIndex.assign( 5, y, {}, clbk ); // $ExpectError + findLastIndex.assign( true, y, {}, clbk ); // $ExpectError + findLastIndex.assign( false, y, {}, clbk ); // $ExpectError + findLastIndex.assign( null, y, {}, clbk ); // $ExpectError + findLastIndex.assign( void 0, y, {}, clbk ); // $ExpectError + findLastIndex.assign( {}, y, {}, clbk ); // $ExpectError + findLastIndex.assign( ( x: number ): number => x, y, {}, clbk ); // $ExpectError + + findLastIndex.assign( '5', y, clbk, {} ); // $ExpectError + findLastIndex.assign( 5, y, clbk, {} ); // $ExpectError + findLastIndex.assign( true, y, clbk, {} ); // $ExpectError + findLastIndex.assign( false, y, clbk, {} ); // $ExpectError + findLastIndex.assign( null, y, clbk, {} ); // $ExpectError + findLastIndex.assign( void 0, y, clbk, {} ); // $ExpectError + findLastIndex.assign( {}, y, clbk, {} ); // $ExpectError + findLastIndex.assign( ( x: number ): number => x, y, clbk, {} ); // $ExpectError + + findLastIndex.assign( '5', y, {}, clbk, {} ); // $ExpectError + findLastIndex.assign( 5, y, {}, clbk, {} ); // $ExpectError + findLastIndex.assign( true, y, {}, clbk, {} ); // $ExpectError + findLastIndex.assign( false, y, {}, clbk, {} ); // $ExpectError + findLastIndex.assign( null, y, {}, clbk, {} ); // $ExpectError + findLastIndex.assign( void 0, y, {}, clbk, {} ); // $ExpectError + findLastIndex.assign( {}, y, {}, clbk, {} ); // $ExpectError + findLastIndex.assign( ( x: number ): number => x, y, {}, clbk, {} ); // $ExpectError +} + +// The compiler throws an error if the `assign` method is provided a second argument which is not an ndarray... +{ + const x = zeros( [ 2, 2 ], { + 'dtype': 'generic' + }); + + findLastIndex.assign( x, '5', clbk ); // $ExpectError + findLastIndex.assign( x, 5, clbk ); // $ExpectError + findLastIndex.assign( x, true, clbk ); // $ExpectError + findLastIndex.assign( x, false, clbk ); // $ExpectError + findLastIndex.assign( x, null, clbk ); // $ExpectError + findLastIndex.assign( x, void 0, clbk ); // $ExpectError + findLastIndex.assign( x, ( x: number ): number => x, clbk ); // $ExpectError + + findLastIndex.assign( x, '5', {}, clbk ); // $ExpectError + findLastIndex.assign( x, 5, {}, clbk ); // $ExpectError + findLastIndex.assign( x, true, {}, clbk ); // $ExpectError + findLastIndex.assign( x, false, {}, clbk ); // $ExpectError + findLastIndex.assign( x, null, {}, clbk ); // $ExpectError + findLastIndex.assign( x, void 0, {}, clbk ); // $ExpectError + findLastIndex.assign( x, ( x: number ): number => x, {}, clbk ); // $ExpectError + + findLastIndex.assign( x, '5', clbk, {} ); // $ExpectError + findLastIndex.assign( x, 5, clbk, {} ); // $ExpectError + findLastIndex.assign( x, true, clbk, {} ); // $ExpectError + findLastIndex.assign( x, false, clbk, {} ); // $ExpectError + findLastIndex.assign( x, null, clbk, {} ); // $ExpectError + findLastIndex.assign( x, void 0, clbk, {} ); // $ExpectError + findLastIndex.assign( x, ( x: number ): number => x, clbk, {} ); // $ExpectError + + findLastIndex.assign( x, '5', {}, clbk, {} ); // $ExpectError + findLastIndex.assign( x, 5, {}, clbk, {} ); // $ExpectError + findLastIndex.assign( x, true, {}, clbk, {} ); // $ExpectError + findLastIndex.assign( x, false, {}, clbk, {} ); // $ExpectError + findLastIndex.assign( x, null, {}, clbk, {} ); // $ExpectError + findLastIndex.assign( x, void 0, {}, clbk, {} ); // $ExpectError + findLastIndex.assign( x, ( x: number ): number => x, {}, clbk, {} ); // $ExpectError +} + +// The compiler throws an error if the `assign` method is provided an options argument which is not an object... +{ + const x = zeros( [ 2, 2 ], { + 'dtype': 'generic' + }); + const y = zeros( [], { + 'dtype': 'int32' + }); + + findLastIndex.assign( x, y, '5', clbk ); // $ExpectError + findLastIndex.assign( x, y, true, clbk ); // $ExpectError + findLastIndex.assign( x, y, false, clbk ); // $ExpectError + findLastIndex.assign( x, y, null, clbk ); // $ExpectError + findLastIndex.assign( x, y, [], clbk ); // $ExpectError + + findLastIndex.assign( x, y, '5', clbk, {} ); // $ExpectError + findLastIndex.assign( x, y, true, clbk, {} ); // $ExpectError + findLastIndex.assign( x, y, false, clbk, {} ); // $ExpectError + findLastIndex.assign( x, y, null, clbk, {} ); // $ExpectError + findLastIndex.assign( x, y, [], clbk, {} ); // $ExpectError +} + +// The compiler throws an error if the `assign` method is provided a callback argument which is not a function... +{ + const x = zeros( [ 2, 2 ], { + 'dtype': 'generic' + }); + const y = zeros( [], { + 'dtype': 'int32' + }); + + findLastIndex.assign( x, y, '5' ); // $ExpectError + findLastIndex.assign( x, y, true ); // $ExpectError + findLastIndex.assign( x, y, false ); // $ExpectError + findLastIndex.assign( x, y, null ); // $ExpectError + findLastIndex.assign( x, y, [] ); // $ExpectError + + findLastIndex.assign( x, y, '5', {} ); // $ExpectError + findLastIndex.assign( x, y, true, {} ); // $ExpectError + findLastIndex.assign( x, y, false, {} ); // $ExpectError + findLastIndex.assign( x, y, null, {} ); // $ExpectError + findLastIndex.assign( x, y, [], {} ); // $ExpectError + + findLastIndex.assign( x, y, {}, '5' ); // $ExpectError + findLastIndex.assign( x, y, {}, true ); // $ExpectError + findLastIndex.assign( x, y, {}, false ); // $ExpectError + findLastIndex.assign( x, y, {}, null ); // $ExpectError + findLastIndex.assign( x, y, {}, [] ); // $ExpectError + + findLastIndex.assign( x, y, {}, '5', {} ); // $ExpectError + findLastIndex.assign( x, y, {}, true, {} ); // $ExpectError + findLastIndex.assign( x, y, {}, false, {} ); // $ExpectError + findLastIndex.assign( x, y, {}, null, {} ); // $ExpectError + findLastIndex.assign( x, y, {}, [], {} ); // $ExpectError +} + +// The compiler throws an error if the `assign` method is provided an invalid `dim` option... +{ + const x = zeros( [ 2, 2 ], { + 'dtype': 'generic' + }); + const y = zeros( [], { + 'dtype': 'int32' + }); + + findLastIndex.assign( x, y, { 'dim': '5' }, clbk ); // $ExpectError + findLastIndex.assign( x, y, { 'dim': true }, clbk ); // $ExpectError + findLastIndex.assign( x, y, { 'dim': false }, clbk ); // $ExpectError + findLastIndex.assign( x, y, { 'dim': null }, clbk ); // $ExpectError + findLastIndex.assign( x, y, { 'dim': {} }, clbk ); // $ExpectError + findLastIndex.assign( x, y, { 'dim': ( x: number ): number => x }, clbk ); // $ExpectError + + findLastIndex.assign( x, y, { 'dim': '5' }, clbk, {} ); // $ExpectError + findLastIndex.assign( x, y, { 'dim': true }, clbk, {} ); // $ExpectError + findLastIndex.assign( x, y, { 'dim': false }, clbk, {} ); // $ExpectError + findLastIndex.assign( x, y, { 'dim': null }, clbk, {} ); // $ExpectError + findLastIndex.assign( x, y, { 'dim': {} }, clbk, {} ); // $ExpectError + findLastIndex.assign( x, y, { 'dim': ( x: number ): number => x }, clbk, {} ); // $ExpectError +} + +// The compiler throws an error if the `assign` method is provided an unsupported number of arguments... +{ + const x = zeros( [ 2, 2 ], { + 'dtype': 'generic' + }); + const y = zeros( [], { + 'dtype': 'int32' + }); + + findLastIndex.assign(); // $ExpectError + findLastIndex.assign( x ); // $ExpectError + findLastIndex.assign( x, y ); // $ExpectError + findLastIndex.assign( x, y, {}, clbk, {}, {} ); // $ExpectError +} diff --git a/lib/node_modules/@stdlib/blas/ext/find-last-index/examples/index.js b/lib/node_modules/@stdlib/blas/ext/find-last-index/examples/index.js new file mode 100644 index 000000000000..4351f9dfd94a --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/find-last-index/examples/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'; + +var discreteUniform = require( '@stdlib/random/array/discrete-uniform' ); +var ndarray2array = require( '@stdlib/ndarray/to-array' ); +var ndarray = require( '@stdlib/ndarray/ctor' ); +var findLastIndex = require( './../lib' ); + +// Define a callback function: +function isEven( v ) { + return v % 2.0 === 0.0; +} + +// Generate an array of random numbers: +var xbuf = discreteUniform( 10, 0, 20, { + 'dtype': 'generic' +}); + +// Wrap in an ndarray: +var x = new ndarray( 'generic', xbuf, [ 5, 2 ], [ 2, 1 ], 0, 'row-major' ); +console.log( ndarray2array( x ) ); + +var opts = { + 'dim': 0 +}; + +// Perform operation: +var idx = findLastIndex( x, opts, isEven ); + +// Print the results: +console.log( ndarray2array( idx ) ); diff --git a/lib/node_modules/@stdlib/blas/ext/find-last-index/lib/assign.js b/lib/node_modules/@stdlib/blas/ext/find-last-index/lib/assign.js new file mode 100644 index 000000000000..0ff4ae9e9389 --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/find-last-index/lib/assign.js @@ -0,0 +1,171 @@ +/** +* @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 hasOwnProp = require( '@stdlib/assert/has-own-property' ); +var isFunction = require( '@stdlib/assert/is-function' ); +var isPlainObject = require( '@stdlib/assert/is-plain-object' ); +var isndarrayLike = require( '@stdlib/assert/is-ndarray-like' ); +var getShape = require( '@stdlib/ndarray/shape' ); +var format = require( '@stdlib/string/format' ); +var base = require( './base.js' ).assign; + + +// MAIN // + +/** +* Returns the index of the last 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} out - output ndarray +* @param {Options} [options] - function options +* @param {integer} [options.dim=-1] - dimension over which to perform operation +* @param {Function} clbk - callback function +* @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} third argument must be a function +* @throws {TypeError} options argument must be an object +* @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 +* @returns {ndarray} output ndarray +* +* @example +* var zeros = require( '@stdlib/ndarray/zeros' ); +* var ndarray2array = require( '@stdlib/ndarray/to-array' ); +* var ndarray = require( '@stdlib/ndarray/ctor' ); +* +* function isEven( v ) { +* return v % 2.0 === 0.0; +* } +* +* // Create data buffers: +* var xbuf = [ 1.0, 2.0, -3.0, 4.0, -5.0, 6.0 ]; +* +* // Define the shape of the input array: +* var shape = [ 2, 3 ]; +* +* // Define the array strides: +* var strides = [ 3, 1 ]; +* +* // Define the index offset: +* var offset = 0; +* +* // Create an input ndarray: +* var x = new ndarray( 'generic', xbuf, shape, strides, offset, 'row-major' ); +* +* // Create an output ndarray: +* var y = zeros( [ 2 ], { +* 'dtype': 'int32' +* }); +* +* // Perform operation: +* var out = assign( x, y, isEven ); +* // returns +* +* var bool = ( out === y ); +* // returns true +* +* var arr = ndarray2array( out ); +* // returns [ -1, 1 ] +*/ +function assign( x, out ) { + var hasOptions; + var options; + var nargs; + var opts; + var ctx; + var cb; + var sh; + + nargs = arguments.length; + if ( !isndarrayLike( x ) ) { + throw new TypeError( format( 'invalid argument. The first argument must be an ndarray. Value: `%s`.', x ) ); + } + if ( !isndarrayLike( out ) ) { + throw new TypeError( format( 'invalid argument. The second argument must be an ndarray. Value: `%s`.', out ) ); + } + if ( nargs < 3 ) { + throw new TypeError( format( 'invalid argument. Function must be provided a callback function. Value: `%s`.', arguments[ 2 ] ) ); + } + + // Initialize an options object: + opts = { + 'dims': [ -1 ] // default behavior is to perform a reduction over the last dimension + }; + + // Initialize a flag indicating whether an `options` argument was provided: + hasOptions = false; + + // Case: assign( x, out, clbk ) + if ( nargs === 3 ) { + cb = arguments[ 1 ]; + if ( !isFunction( cb ) ) { + throw new TypeError( format( 'invalid argument. Second argument must be a function. Value: `%s`.', cb ) ); + } + } + // Case: assign( x, out, options, clbk ) or Case: assign( x, out, clbk, thisArg ) + else if ( nargs < 5 ) { + options = arguments[ 2 ]; + cb = arguments[ 3 ]; + if ( isFunction( options ) ) { + cb = options; + ctx = cb; + } else { + if ( !isFunction( cb ) ) { + throw new TypeError( format( 'invalid argument. Second argument must be a function. Value: `%s`.', cb ) ); + } + hasOptions = true; + } + } + // Case: assign( x, out, options, clbk, thisArg ) + else { + options = arguments[ 2 ]; + hasOptions = true; + cb = arguments[ 3 ]; + if ( !isFunction( cb ) ) { + throw new TypeError( format( 'invalid argument. Second argument must be a function. Value: `%s`.', cb ) ); + } + ctx = arguments[ 4 ]; + } + if ( hasOptions ) { + if ( !isPlainObject( options ) ) { + throw new TypeError( format( 'invalid argument. Options argument must be an object. Value: `%s`.', options ) ); + } + // Resolve provided options... + if ( hasOwnProp( options, 'dim' ) ) { + opts.dims[ 0 ] = options.dim; + } + } + // Resolve the list of non-reduced dimensions: + sh = getShape( x ); + if ( sh.length < 1 ) { + throw new RangeError( 'invalid argument. First argument must have at least one dimension.' ); + } + return base( x, out, opts, cb, ctx ); +} + + +// EXPORTS // + +module.exports = assign; diff --git a/lib/node_modules/@stdlib/blas/ext/find-last-index/lib/base.js b/lib/node_modules/@stdlib/blas/ext/find-last-index/lib/base.js new file mode 100644 index 000000000000..399887f0eb0c --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/find-last-index/lib/base.js @@ -0,0 +1,96 @@ +/** +* @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 dtypes = require( '@stdlib/ndarray/dtypes' ); +var gfindLastIndex = require( '@stdlib/blas/ext/base/ndarray/gfind-last-index' ); +var factory = require( '@stdlib/ndarray/base/unary-reduce-strided1d-dispatch-by-factory' ); + + +// VARIABLES // + +var idtypes = dtypes( 'all' ); +var odtypes = dtypes( 'integer_index_and_generic' ); +var policies = { + 'output': 'integer_index_and_generic', + 'casting': 'none' +}; +var table = { + 'default': gfindLastIndex +}; + + +// MAIN // + +/** +* Returns the index of the last element along an ndarray dimension which passes a test implemented by a predicate function. +* +* @private +* @name findLastIndex +* @type {Function} +* @param {ndarrayLike} x - input ndarray +* @param {Options} [options] - function options +* @param {IntegerArray} [options.dims] - list of dimensions over which to perform operation +* @param {string} [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} 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 +* @throws {Error} must provide valid options +* @returns {ndarray} output ndarray +* +* @example +* var ndarray = require( '@stdlib/ndarray/ctor' ); +* +* function isEven( v ) { +* return v % 2.0 === 0.0; +* } +* +* // Create a data buffer: +* var xbuf = [ 1.0, 2.0, -3.0, 4.0, -5.0, 6.0 ]; +* +* // Define the shape of the input array: +* var sh = [ 6 ]; +* +* // Define the array strides: +* var sx = [ 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 = findLastIndex( x, isEven ); +* // returns +* +* var idx = out.get(); +* // returns 1 +*/ +var findLastIndex = factory( table, [ idtypes ], odtypes, policies ); + + +// EXPORTS // + +module.exports = findLastIndex; diff --git a/lib/node_modules/@stdlib/blas/ext/find-last-index/lib/index.js b/lib/node_modules/@stdlib/blas/ext/find-last-index/lib/index.js new file mode 100644 index 000000000000..ae725d001505 --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/find-last-index/lib/index.js @@ -0,0 +1,74 @@ +/** +* @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'; + +/** +* Return the index of the last element along an ndarray dimension which passes a test implemented by a predicate function. +* +* @module @stdlib/blas/ext/find-last-index +* +* @example +* var ndarray2array = require( '@stdlib/ndarray/to-array' ); +* var ndarray = require( '@stdlib/ndarray/ctor' ); +* var findLastIndex = require( '@stdlib/blas/ext/find-last-index' ); +* +* function isEven( v ) { +* return v % 2.0 === 0.0; +* } +* +* // Create a data buffer: +* var xbuf = [ 1.0, 2.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 = findLastIndex( x, isEven ); +* // returns +* +* var arr = ndarray2array( out ); +* // returns [ -1, 1 ] +*/ + +// MODULES // + +var setReadOnly = require( '@stdlib/utils/define-nonenumerable-read-only-property' ); +var main = require( './main.js' ); +var assign = require( './assign.js' ); + + +// MAIN // + +setReadOnly( main, 'assign', assign ); + + +// EXPORTS // + +module.exports = main; + +// exports: { "assign": "main.assign" } diff --git a/lib/node_modules/@stdlib/blas/ext/find-last-index/lib/main.js b/lib/node_modules/@stdlib/blas/ext/find-last-index/lib/main.js new file mode 100644 index 000000000000..c1d0768ec9d3 --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/find-last-index/lib/main.js @@ -0,0 +1,164 @@ +/** +* @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 hasOwnProp = require( '@stdlib/assert/has-own-property' ); +var isFunction = require( '@stdlib/assert/is-function' ); +var isPlainObject = require( '@stdlib/assert/is-plain-object' ); +var isndarrayLike = require( '@stdlib/assert/is-ndarray-like' ); +var getShape = require( '@stdlib/ndarray/shape' ); +var format = require( '@stdlib/string/format' ); +var base = require( './base.js' ); + + +// MAIN // + +/** +* Returns the index of the last element along an ndarray dimension which passes a test implemented by a predicate function. +* +* @param {ndarrayLike} x - input ndarray +* @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 +* @param {string} [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} options argument must be an object +* @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 +* @returns {ndarray} output ndarray +* +* @example +* var ndarray2array = require( '@stdlib/ndarray/to-array' ); +* var ndarray = require( '@stdlib/ndarray/ctor' ); +* +* function isEven( v ) { +* return v % 2.0 === 0.0; +* } +* +* // Create a data buffer: +* var xbuf = [ 1.0, 2.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 = findLastIndex( x, isEven ); +* // returns +* +* var arr = ndarray2array( out ); +* // returns [ -1, 1 ] +*/ +function findLastIndex( x ) { + var hasOptions; + var options; + var nargs; + var opts; + var ctx; + var cb; + var sh; + + nargs = arguments.length; + if ( !isndarrayLike( x ) ) { + throw new TypeError( format( 'invalid argument. First argument must be an ndarray. Value: `%s`.', x ) ); + } + if ( nargs < 2 ) { + throw new TypeError( format( 'invalid argument. Function must be provided a callback function. Value: `%s`.', arguments[ 1 ] ) ); + } + + // Initialize an options object: + opts = { + 'dims': [ -1 ], // default behavior is to perform a reduction over the last dimension + 'keepdims': false + }; + + // Initialize a flag indicating whether an `options` argument was provided: + hasOptions = false; + + // Case: findLastIndex( x, clbk ) + if ( nargs === 2 ) { + cb = arguments[ 1 ]; + if ( !isFunction( cb ) ) { + throw new TypeError( format( 'invalid argument. Second argument must be a function. Value: `%s`.', cb ) ); + } + } + // Case: findLastIndex( x, options, clbk ) or Case: findLastIndex( x, clbk, thisArg ) + else if ( nargs < 4 ) { + options = arguments[ 1 ]; + cb = arguments[ 2 ]; + if ( isFunction( options ) ) { + cb = options; + ctx = cb; + } else { + if ( !isFunction( cb ) ) { + throw new TypeError( format( 'invalid argument. Second argument must be a function. Value: `%s`.', cb ) ); + } + hasOptions = true; + } + } + // Case: findLastIndex( x, options, clbk, thisArg ) + else { + options = arguments[ 1 ]; + hasOptions = true; + cb = arguments[ 2 ]; + if ( !isFunction( cb ) ) { + throw new TypeError( format( 'invalid argument. Second argument must be a function. Value: `%s`.', cb ) ); + } + ctx = arguments[ 3 ]; + } + if ( hasOptions ) { + if ( !isPlainObject( options ) ) { + throw new TypeError( format( 'invalid argument. Options argument must be an object. Value: `%s`.', options ) ); + } + // Resolve provided options... + if ( hasOwnProp( options, 'dim' ) ) { + opts.dims[ 0 ] = options.dim; + } + if ( hasOwnProp( options, 'keepdims' ) ) { + opts.keepdims = options.keepdims; + } + if ( hasOwnProp( options, 'dtype' ) ) { + opts.dtype = options.dtype; + } + } + // Resolve the list of non-reduced dimensions: + sh = getShape( x ); + if ( sh.length < 1 ) { + throw new RangeError( 'invalid argument. First argument must have at least one dimension.' ); + } + return base( x, opts, cb, ctx ); +} + + +// EXPORTS // + +module.exports = findLastIndex; diff --git a/lib/node_modules/@stdlib/blas/ext/find-last-index/package.json b/lib/node_modules/@stdlib/blas/ext/find-last-index/package.json new file mode 100644 index 000000000000..014d763b900e --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/find-last-index/package.json @@ -0,0 +1,62 @@ +{ + "name": "@stdlib/blas/ext/find-last-index", + "version": "0.0.0", + "description": "Return the index of the last element along an ndarray dimension which passes a test implemented by a predicate 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", + "blas", + "find", + "index", + "callback", + "search", + "array", + "ndarray" + ], + "__stdlib__": {} +} diff --git a/lib/node_modules/@stdlib/blas/ext/find-last-index/test/test.assign.js b/lib/node_modules/@stdlib/blas/ext/find-last-index/test/test.assign.js new file mode 100644 index 000000000000..7fd38b84b737 --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/find-last-index/test/test.assign.js @@ -0,0 +1,694 @@ +/** +* @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 isndarrayLike = require( '@stdlib/assert/is-ndarray-like' ); +var isSameArray = require( '@stdlib/assert/is-same-array' ); +var ndarray = require( '@stdlib/ndarray/ctor' ); +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 getOrder = require( '@stdlib/ndarray/order' ); +var getData = require( '@stdlib/ndarray/data-buffer' ); +var findLastIndex = require( './../lib' ).assign; + + +// FUNCTIONS // + +/** +* Callback function. +* +* @private +* @param {number} v - input value +* @returns {boolean} result +*/ +function clbk( v ) { + return v % 2.0 === 0.0; +} + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof findLastIndex, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function throws an error if provided a first argument which is not an ndarray-like object', function test( t ) { + var values; + var i; + var y; + + y = zeros( [], { + 'dtype': 'generic' + }); + + 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() { + findLastIndex( value, y, clbk ); + }; + } +}); + +tape( 'the function throws an error if provided a first argument which is not an ndarray-like object (thisArg)', function test( t ) { + var values; + var i; + var y; + + y = zeros( [], { + 'dtype': 'generic' + }); + + 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() { + findLastIndex( value, y, clbk, {} ); + }; + } +}); + +tape( 'the function throws an error if provided a first argument which is not an ndarray-like object (options)', function test( t ) { + var values; + var i; + var y; + + y = zeros( [], { + 'dtype': 'generic' + }); + + 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() { + findLastIndex( value, y, {}, clbk ); + }; + } +}); + +tape( 'the function throws an error if provided a first argument which is not an ndarray-like object (options, thisArg)', function test( t ) { + var values; + var i; + var y; + + y = zeros( [], { + 'dtype': 'generic' + }); + + 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() { + findLastIndex( value, y, {}, clbk, {} ); + }; + } +}); + +tape( 'the function throws an error if provided a second argument which is not an ndarray-like object', function test( t ) { + var values; + var i; + var x; + + x = zeros( [], { + 'dtype': 'generic' + }); + + 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() { + findLastIndex( x, value, clbk ); + }; + } +}); + +tape( 'the function throws an error if provided a first argument which is not an ndarray-like object (thisArg)', function test( t ) { + var values; + var i; + var x; + + x = zeros( [], { + 'dtype': 'generic' + }); + + 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() { + findLastIndex( x, value, clbk, {} ); + }; + } +}); + +tape( 'the function throws an error if provided a first argument which is not an ndarray-like object (options)', function test( t ) { + var values; + var i; + var x; + + x = zeros( [], { + 'dtype': 'generic' + }); + + 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() { + findLastIndex( x, value, {}, clbk ); + }; + } +}); + +tape( 'the function throws an error if provided a first argument which is not an ndarray-like object (options, thisArg)', function test( t ) { + var values; + var i; + var x; + + x = zeros( [], { + 'dtype': 'generic' + }); + + 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() { + findLastIndex( x, value, {}, clbk, {} ); + }; + } +}); + +tape( 'the function throws an error if provided insufficient number of arguments', function test( t ) { + var x; + var y; + + x = zeros( [ 2, 2 ], { + 'dtype': 'generic' + }); + y = zeros( [], { + 'dtype': 'int32' + }); + + t.throws( badValue1, TypeError, 'throws an error when provided insufficient arguments' ); + t.throws( badValue2, TypeError, 'throws an error when provided insufficient arguments' ); + t.throws( badValue3, TypeError, 'throws an error when provided insufficient arguments' ); + t.end(); + + function badValue1() { + findLastIndex( x ); + } + + function badValue2() { + findLastIndex( x, y ); + } + + function badValue3() { + findLastIndex(); + } +}); + +tape( 'the function throws an error if provided an options argument which is not an object', function test( t ) { + var values; + var opts; + var x; + var y; + var i; + + opts = { + 'dtype': 'generic' + }; + + x = zeros( [ 2, 2 ], opts ); + y = zeros( [], opts ); + + 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() { + findLastIndex( x, y, value, clbk ); + }; + } +}); + +tape( 'the function throws an error if provided an options argument which is not an object (thisArg)', function test( t ) { + var values; + var opts; + var x; + var y; + var i; + + opts = { + 'dtype': 'generic' + }; + + x = zeros( [ 2, 2 ], opts ); + y = zeros( [], opts ); + + 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() { + findLastIndex( x, y, value, clbk, {} ); + }; + } +}); + +tape( 'the function throws an error if provided a `dim` option which is not an integer', function test( t ) { + var options; + var values; + var opts; + var x; + var y; + var i; + + opts = { + 'dtype': 'generic' + }; + + x = zeros( [ 2, 2 ], opts ); + y = zeros( [], opts ); + + values = [ + '5', + NaN, + true, + false, + null, + void 0, + [ 'a' ], + {}, + 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() { + options = { + 'dim': value + }; + findLastIndex( x, y, options, clbk ); + }; + } +}); + +tape( 'the function throws an error if provided a `dim` option which is not an integer (thisArg)', function test( t ) { + var options; + var values; + var opts; + var x; + var y; + var i; + + opts = { + 'dtype': 'generic' + }; + + x = zeros( [ 2, 2 ], opts ); + y = zeros( [], opts ); + + values = [ + '5', + NaN, + true, + false, + null, + void 0, + [ 'a' ], + {}, + 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() { + options = { + 'dim': value + }; + findLastIndex( x, y, options, clbk, {} ); + }; + } +}); + +tape( 'the function returns the first index of an element which passes a test (row-major)', function test( t ) { + var expected; + var actual; + var xbuf; + var x; + var y; + + xbuf = [ -1.0, 2.0, -3.0, 4.0 ]; + x = new ndarray( 'generic', xbuf, [ 2, 2 ], [ 2, 1 ], 0, 'row-major' ); + y = zeros( [ 2 ], { + 'dtype': 'generic', + 'order': 'row-major' + }); + + actual = findLastIndex( x, y, clbk ); + expected = [ 1, -1 ]; + + t.strictEqual( isndarrayLike( actual ), true, 'returns expected value' ); + t.strictEqual( getDType( actual ), 'generic', 'returns expected value' ); + t.deepEqual( getShape( actual ), [ 2 ], 'returns expected value' ); + t.strictEqual( getOrder( actual ), getOrder( x ), 'returns expected value' ); + t.strictEqual( isSameArray( getData( actual ), expected ), true, 'returns expected value' ); + t.strictEqual( ( y === actual ), true, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function returns the first index of an element which passes a test (column-major)', function test( t ) { + var expected; + var actual; + var xbuf; + var x; + var y; + + xbuf = [ -1.0, 2.0, -3.0, 4.0 ]; + x = new ndarray( 'generic', xbuf, [ 2, 2 ], [ 1, 2 ], 0, 'column-major' ); + y = zeros( [ 2 ], { + 'dtype': 'generic', + 'order': 'column-major' + }); + + actual = findLastIndex( x, y, clbk ); + expected = [ -1, 0 ]; + + t.strictEqual( isndarrayLike( actual ), true, 'returns expected value' ); + t.strictEqual( getDType( actual ), 'generic', 'returns expected value' ); + t.deepEqual( getShape( actual ), [ 2 ], 'returns expected value' ); + t.strictEqual( getOrder( actual ), getOrder( x ), '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 operation dimensions (row-major)', function test( t ) { + var expected; + var actual; + var xbuf; + var opts; + var x; + var y; + + xbuf = [ -1.0, 2.0, -3.0, 4.0 ]; + x = new ndarray( 'generic', xbuf, [ 2, 2 ], [ 2, 1 ], 0, 'row-major' ); + y = zeros( [ 2 ], { + 'dtype': 'generic', + 'order': 'row-major' + }); + opts = { + 'dim': 0 + }; + + actual = findLastIndex( x, y, opts, clbk ); + expected = [ -1, 0 ]; + + t.strictEqual( isndarrayLike( actual ), true, 'returns expected value' ); + t.strictEqual( getDType( actual ), 'generic', 'returns expected value' ); + t.deepEqual( getShape( actual ), [ 2 ], 'returns expected value' ); + t.strictEqual( getOrder( actual ), getOrder( x ), 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + t.strictEqual( ( y === actual ), true, 'returns expected value' ); + + y = zeros( [ 2 ], { + 'dtype': 'generic', + 'order': 'row-major' + }); + opts = { + 'dim': 1 + }; + + actual = findLastIndex( x, y, opts, clbk ); + expected = [ 1, -1 ]; + + t.strictEqual( isndarrayLike( actual ), true, 'returns expected value' ); + t.strictEqual( getDType( actual ), 'generic', 'returns expected value' ); + t.deepEqual( getShape( actual ), [ 2 ], 'returns expected value' ); + t.strictEqual( getOrder( actual ), getOrder( x ), '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 an operation dimension (column-major)', function test( t ) { + var expected; + var actual; + var xbuf; + var opts; + var x; + var y; + + xbuf = [ -1.0, 2.0, -3.0, 4.0 ]; + x = new ndarray( 'generic', xbuf, [ 2, 2 ], [ 1, 2 ], 0, 'column-major' ); + y = zeros( [ 2 ], { + 'dtype': 'generic', + 'order': 'column-major' + }); + opts = { + 'dim': 0 + }; + + actual = findLastIndex( x, y, opts, clbk ); + expected = [ 1, -1 ]; + + t.strictEqual( isndarrayLike( actual ), true, 'returns expected value' ); + t.strictEqual( getDType( actual ), 'generic', 'returns expected value' ); + t.deepEqual( getShape( actual ), [ 2 ], 'returns expected value' ); + t.strictEqual( getOrder( actual ), getOrder( x ), 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + t.strictEqual( ( y === actual ), true, 'returns expected value' ); + + xbuf = [ -1.0, 2.0, -3.0, 4.0 ]; + x = new ndarray( 'generic', xbuf, [ 2, 2 ], [ 1, 2 ], 0, 'column-major' ); + y = zeros( [ 2 ], { + 'dtype': 'generic', + 'order': 'column-major' + }); + opts = { + 'dims': 1 + }; + + actual = findLastIndex( x, y, opts, clbk ); + expected = [ -1, 0 ]; + + t.strictEqual( isndarrayLike( actual ), true, 'returns expected value' ); + t.strictEqual( getDType( actual ), 'generic', 'returns expected value' ); + t.deepEqual( getShape( actual ), [ 2 ], 'returns expected value' ); + t.strictEqual( getOrder( actual ), getOrder( x ), '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 actual; + var xbuf; + var ctx; + var x; + var y; + + xbuf = [ -1.0, 2.0, -3.0, 4.0 ]; + x = new ndarray( 'generic', xbuf, [ 2, 2 ], [ 2, 1 ], 0, 'row-major' ); + y = zeros( [ 2 ], { + 'dtype': 'generic', + 'order': 'row-major' + }); + ctx = { + 'count': 0 + }; + + actual = findLastIndex( x, y, clbk1, ctx ); + expected = [ 1, -1 ]; + + t.strictEqual( isndarrayLike( actual ), true, 'returns expected value' ); + t.strictEqual( getDType( actual ), 'generic', 'returns expected value' ); + t.deepEqual( getShape( actual ), [ 2 ], 'returns expected value' ); + t.strictEqual( getOrder( actual ), getOrder( x ), 'returns expected value' ); + t.strictEqual( isSameArray( getData( actual ), expected ), true, 'returns expected value' ); + t.strictEqual( ( y === actual ), true, 'returns expected value' ); + t.strictEqual( ctx.count, 1, 'returns expected value' ); + + t.end(); + + function clbk1( v ) { + this.count += 1; // eslint-disable-line no-invalid-this + return v % 2.0 === 0.0; + } +}); diff --git a/lib/node_modules/@stdlib/blas/ext/find-last-index/test/test.js b/lib/node_modules/@stdlib/blas/ext/find-last-index/test/test.js new file mode 100644 index 000000000000..429028745969 --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/find-last-index/test/test.js @@ -0,0 +1,39 @@ +/** +* @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 isMethod = require( '@stdlib/assert/is-method' ); +var findLastIndex = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof findLastIndex, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'attached to the main export is an `assign` method', function test( t ) { + t.strictEqual( isMethod( findLastIndex, 'assign' ), true, 'returns expected value' ); + t.end(); +}); diff --git a/lib/node_modules/@stdlib/blas/ext/find-last-index/test/test.main.js b/lib/node_modules/@stdlib/blas/ext/find-last-index/test/test.main.js new file mode 100644 index 000000000000..0424a0ba8d99 --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/find-last-index/test/test.main.js @@ -0,0 +1,680 @@ +/** +* @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 isndarrayLike = require( '@stdlib/assert/is-ndarray-like' ); +var ndarray = require( '@stdlib/ndarray/ctor' ); +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 getOrder = require( '@stdlib/ndarray/order' ); +var findLastIndex = require( './../lib' ); + + +// FUNCTIONS // + +/** +* Callback function. +* +* @private +* @param {number} v - input value +* @returns {boolean} result +*/ +function clbk( v ) { + return v % 2.0 === 0.0; +} + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof findLastIndex, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function throws an error if provided a first argument which is not an ndarray-like object', function test( t ) { + var values; + var i; + + values = [ + '5', + 5, + NaN, + true, + false, + null, + void 0, + [], + {}, + function noop() {} + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + findLastIndex( value, clbk ); + }; + } +}); + +tape( 'the function throws an error if provided a first argument which is not an ndarray-like object (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() { + findLastIndex( value, clbk, {} ); + }; + } +}); + +tape( 'the function throws an error if provided a first argument which is not an ndarray-like object (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() { + findLastIndex( value, {}, clbk ); + }; + } +}); + +tape( 'the function throws an error if provided a first argument which is not an ndarray-like object (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() { + findLastIndex( value, {}, clbk, {} ); + }; + } +}); + +tape( 'the function throws an error if provided a second argument which is not a function', function test( t ) { + var values; + var x; + var i; + + x = zeros( [ 2, 2 ], { + 'dtype': 'float64' + }); + 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() { + findLastIndex( x, value ); + }; + } +}); + +tape( 'the function throws an error if provided a second argument which is not a function (thisArg)', function test( t ) { + var values; + var x; + var i; + + x = zeros( [ 2, 2 ], { + 'dtype': 'float64' + }); + 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() { + findLastIndex( x, value, {} ); + }; + } +}); + +tape( 'the function throws an error if provided a second argument which is not a function (options)', function test( t ) { + var values; + var x; + var i; + + x = zeros( [ 2, 2 ], { + 'dtype': 'float64' + }); + 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() { + findLastIndex( x, {}, value ); + }; + } +}); + +tape( 'the function throws an error if provided a second argument which is not a function (options, thisArg)', function test( t ) { + var values; + var x; + var i; + + x = zeros( [ 2, 2 ], { + 'dtype': 'float64' + }); + 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() { + findLastIndex( x, {}, value, {} ); + }; + } +}); + +tape( 'the function throws an error if provided insufficient number of arguments', function test( t ) { + var x; + + x = zeros( [ 2, 2 ], { + 'dtype': 'generic' + }); + + t.throws( badValue1, TypeError, 'throws an error when provided insufficient arguments' ); + t.throws( badValue2, TypeError, 'throws an error when provided insufficient arguments' ); + t.end(); + + function badValue1() { + findLastIndex( x ); + } + + function badValue2() { + findLastIndex(); + } +}); + +tape( 'the function throws an error if provided an options argument which is not an object', 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() { + findLastIndex( x, value, clbk ); + }; + } +}); + +tape( 'the function throws an error if provided an options argument which is not an object (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() { + findLastIndex( x, value, clbk, {} ); + }; + } +}); + +tape( 'the function throws an error if provided a `dtype` option which is not a supported data type', function test( t ) { + var values; + var opts; + var x; + var i; + + x = zeros( [ 2, 2 ], { + 'dtype': 'generic' + }); + + values = [ + 'bool', + 'float64', + 'float32', + 'boop' + ]; + 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() { + opts = { + 'dtype': value + }; + findLastIndex( x, opts, clbk ); + }; + } +}); + +tape( 'the function throws an error if provided a `dtype` option which is not a supported data type (thisArg)', function test( t ) { + var values; + var opts; + var x; + var i; + + x = zeros( [ 2, 2 ], { + 'dtype': 'generic' + }); + + values = [ + 'bool', + 'float64', + 'float32', + 'boop' + ]; + 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() { + opts = { + 'dtype': value + }; + findLastIndex( x, opts, clbk, {} ); + }; + } +}); + +tape( 'the function throws an error if provided a `dim` option which is not an integer', function test( t ) { + var values; + var opts; + var x; + var i; + + x = zeros( [ 2, 2 ], { + 'dtype': 'generic' + }); + + values = [ + '5', + NaN, + true, + false, + null, + void 0, + [ 'a' ], + {}, + 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() { + opts = { + 'dim': value + }; + findLastIndex( x, opts, clbk ); + }; + } +}); + +tape( 'the function throws an error if provided a `dim` option which is not an integer (thisArg)', function test( t ) { + var values; + var opts; + var x; + var i; + + x = zeros( [ 2, 2 ], { + 'dtype': 'generic' + }); + + values = [ + '5', + NaN, + true, + false, + null, + void 0, + [ 'a' ], + {}, + 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() { + opts = { + 'dim': value + }; + findLastIndex( x, opts, clbk, {} ); + }; + } +}); + +tape( 'the function returns the first index of an element which passes a test (row-major)', function test( t ) { + var expected; + var actual; + var xbuf; + var x; + + xbuf = [ -1.0, 2.0, -3.0, 4.0 ]; + x = new ndarray( 'generic', xbuf, [ 2, 2 ], [ 2, 1 ], 0, 'row-major' ); + + actual = findLastIndex( x, clbk ); + expected = [ 1, -1 ]; + + t.strictEqual( isndarrayLike( actual ), true, 'returns expected value' ); + t.strictEqual( getDType( actual ), 'generic', 'returns expected value' ); + t.deepEqual( getShape( actual ), [ 2 ], 'returns expected value' ); + t.strictEqual( getOrder( actual ), getOrder( x ), 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function returns the first index of an element which passes a test (column-major)', function test( t ) { + var expected; + var actual; + var xbuf; + var x; + + xbuf = [ -1.0, 2.0, -3.0, 4.0 ]; + x = new ndarray( 'generic', xbuf, [ 2, 2 ], [ 1, 2 ], 0, 'column-major' ); + + actual = findLastIndex( x, clbk ); + expected = [ -1, 0 ]; + + t.strictEqual( isndarrayLike( actual ), true, 'returns expected value' ); + t.strictEqual( getDType( actual ), 'generic', 'returns expected value' ); + t.deepEqual( getShape( actual ), [ 2 ], 'returns expected value' ); + t.strictEqual( getOrder( actual ), getOrder( x ), 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports specifying the operation dimension (row-major)', function test( t ) { + var expected; + var actual; + var xbuf; + var opts; + var x; + + xbuf = [ -1.0, 2.0, -3.0, 4.0 ]; + x = new ndarray( 'generic', xbuf, [ 2, 2 ], [ 2, 1 ], 0, 'row-major' ); + opts = { + 'dim': 0 + }; + + actual = findLastIndex( x, opts, clbk ); + expected = [ -1, 0 ]; + + t.strictEqual( isndarrayLike( actual ), true, 'returns expected value' ); + t.strictEqual( getDType( actual ), 'generic', 'returns expected value' ); + t.deepEqual( getShape( actual ), [ 2 ], 'returns expected value' ); + t.strictEqual( getOrder( actual ), getOrder( x ), 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + xbuf = [ -1.0, 2.0, -3.0, 4.0 ]; + x = new ndarray( 'generic', xbuf, [ 2, 2 ], [ 2, 1 ], 0, 'row-major' ); + opts = { + 'dim': 1 + }; + + actual = findLastIndex( x, opts, clbk ); + expected = [ 1, -1 ]; + + t.strictEqual( isndarrayLike( actual ), true, 'returns expected value' ); + t.strictEqual( getDType( actual ), 'generic', 'returns expected value' ); + t.deepEqual( getShape( actual ), [ 2 ], 'returns expected value' ); + t.strictEqual( getOrder( actual ), getOrder( x ), 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports specifying the operation dimension (column-major)', function test( t ) { + var expected; + var actual; + var xbuf; + var opts; + var x; + + xbuf = [ -1.0, 2.0, -3.0, 4.0 ]; + x = new ndarray( 'generic', xbuf, [ 2, 2 ], [ 1, 2 ], 0, 'column-major' ); + opts = { + 'dim': 0 + }; + + actual = findLastIndex( x, opts, clbk ); + expected = [ 1, -1 ]; + + t.strictEqual( isndarrayLike( actual ), true, 'returns expected value' ); + t.strictEqual( getDType( actual ), 'generic', 'returns expected value' ); + t.deepEqual( getShape( actual ), [ 2 ], 'returns expected value' ); + t.strictEqual( getOrder( actual ), getOrder( x ), 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + xbuf = [ -1.0, 2.0, -3.0, 4.0 ]; + x = new ndarray( 'generic', xbuf, [ 2, 2 ], [ 1, 2 ], 0, 'column-major' ); + opts = { + 'dim': 1 + }; + + actual = findLastIndex( x, opts, clbk ); + expected = [ -1, 0 ]; + + t.strictEqual( isndarrayLike( actual ), true, 'returns expected value' ); + t.strictEqual( getDType( actual ), 'generic', 'returns expected value' ); + t.deepEqual( getShape( actual ), [ 2 ], 'returns expected value' ); + t.strictEqual( getOrder( actual ), getOrder( x ), 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports specifying the output array data type', function test( t ) { + var expected; + var actual; + var xbuf; + var opts; + var x; + + xbuf = [ -1.0, 2.0, -3.0, 4.0 ]; + x = new ndarray( 'generic', xbuf, [ 2, 2 ], [ 2, 1 ], 0, 'row-major' ); + opts = { + 'dtype': 'int32' + }; + + actual = findLastIndex( x, opts, clbk ); + expected = [ 1, -1 ]; + + t.strictEqual( isndarrayLike( actual ), true, 'returns expected value' ); + t.strictEqual( getDType( actual ), 'int32', 'returns expected value' ); + t.deepEqual( getShape( actual ), [ 2 ], 'returns expected value' ); + t.strictEqual( getOrder( actual ), getOrder( x ), '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 actual; + var xbuf; + var ctx; + var x; + + xbuf = [ -1.0, 2.0, -3.0, 4.0 ]; + x = new ndarray( 'generic', xbuf, [ 2, 2 ], [ 2, 1 ], 0, 'row-major' ); + ctx = { + 'count': 0 + }; + + actual = findLastIndex( x, clbk1, ctx ); + expected = [ 1, -1 ]; + + t.strictEqual( isndarrayLike( actual ), true, 'returns expected value' ); + t.strictEqual( getDType( actual ), 'generic', 'returns expected value' ); + t.deepEqual( getShape( actual ), [ 2 ], 'returns expected value' ); + t.strictEqual( getOrder( actual ), getOrder( x ), 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + t.strictEqual( ctx.count, 1, 'returns expected value' ); + + t.end(); + + function clbk1( v ) { + this.count += 1; // eslint-disable-line no-invalid-this + return v % 2.0 === 0.0; + } +});