From 2b8e0bdfb05e701ccaa82f5721cc8865d7846e87 Mon Sep 17 00:00:00 2001 From: headlessNode Date: Wed, 3 Dec 2025 01:29:34 +0500 Subject: [PATCH] feat: add ndarray/find-last --- .../@stdlib/ndarray/find-last/README.md | 335 +++++++ .../find-last/benchmark/benchmark.1d.js | 152 ++++ .../find-last/benchmark/benchmark.2d.js | 159 ++++ .../@stdlib/ndarray/find-last/docs/repl.txt | 106 +++ .../ndarray/find-last/docs/types/index.d.ts | 350 ++++++++ .../ndarray/find-last/docs/types/test.ts | 402 +++++++++ .../ndarray/find-last/examples/index.js | 32 + .../@stdlib/ndarray/find-last/lib/assign.js | 142 +++ .../ndarray/find-last/lib/callback_wrapper.js | 51 ++ .../@stdlib/ndarray/find-last/lib/index.js | 57 ++ .../@stdlib/ndarray/find-last/lib/main.js | 131 +++ .../@stdlib/ndarray/find-last/package.json | 65 ++ .../ndarray/find-last/test/test.assign.js | 825 ++++++++++++++++++ .../@stdlib/ndarray/find-last/test/test.js | 39 + .../ndarray/find-last/test/test.main.js | 667 ++++++++++++++ 15 files changed, 3513 insertions(+) create mode 100644 lib/node_modules/@stdlib/ndarray/find-last/README.md create mode 100644 lib/node_modules/@stdlib/ndarray/find-last/benchmark/benchmark.1d.js create mode 100644 lib/node_modules/@stdlib/ndarray/find-last/benchmark/benchmark.2d.js create mode 100644 lib/node_modules/@stdlib/ndarray/find-last/docs/repl.txt create mode 100644 lib/node_modules/@stdlib/ndarray/find-last/docs/types/index.d.ts create mode 100644 lib/node_modules/@stdlib/ndarray/find-last/docs/types/test.ts create mode 100644 lib/node_modules/@stdlib/ndarray/find-last/examples/index.js create mode 100644 lib/node_modules/@stdlib/ndarray/find-last/lib/assign.js create mode 100644 lib/node_modules/@stdlib/ndarray/find-last/lib/callback_wrapper.js create mode 100644 lib/node_modules/@stdlib/ndarray/find-last/lib/index.js create mode 100644 lib/node_modules/@stdlib/ndarray/find-last/lib/main.js create mode 100644 lib/node_modules/@stdlib/ndarray/find-last/package.json create mode 100644 lib/node_modules/@stdlib/ndarray/find-last/test/test.assign.js create mode 100644 lib/node_modules/@stdlib/ndarray/find-last/test/test.js create mode 100644 lib/node_modules/@stdlib/ndarray/find-last/test/test.main.js diff --git a/lib/node_modules/@stdlib/ndarray/find-last/README.md b/lib/node_modules/@stdlib/ndarray/find-last/README.md new file mode 100644 index 000000000000..c2920af23f29 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/find-last/README.md @@ -0,0 +1,335 @@ + + +# findLast + +> Return a new [ndarray][@stdlib/ndarray/ctor] containing the first elements which pass a test implemented by a predicate function along one or more [ndarray][@stdlib/ndarray/ctor] dimensions. + +
+ +
+ + + +
+ +## Usage + +```javascript +var findLast = require( '@stdlib/ndarray/find-last' ); +``` + +#### findLast( x\[, options], predicate\[, thisArg] ) + +Returns a new [ndarray][@stdlib/ndarray/ctor] containing the last elements which pass a test implemented by a predicate function along one or more [ndarray][@stdlib/ndarray/ctor] dimensions. + + + +```javascript +var array = require( '@stdlib/ndarray/array' ); + +function isEven( value ) { + return value % 2.0 === 0.0; +} + +// Create an input ndarray: +var x = array( [ [ [ 1.0, 2.0 ], [ 3.0, 4.0 ] ], [ [ 5.0, 6.0 ], [ 7.0, 8.0 ] ] ] ); +// returns + +// Perform reduction: +var out = findLast( x, isEven ); +// returns + +var v = out.get(); +// returns 8.0 +``` + +The function accepts the following arguments: + +- **x**: input [ndarray][@stdlib/ndarray/ctor]. +- **options**: function options _(optional)_. +- **predicate**: predicate function. +- **thisArg**: predicate function execution context _(optional)_. + +The function accepts the following options: + +- **dims**: list of dimensions over which to perform a reduction. +- **keepdims**: boolean indicating whether the reduced dimensions should be included in the returned [ndarray][@stdlib/ndarray/ctor] as singleton dimensions. Default: `false`. +- **sentinelValue**: value to return when no element passes the test. May be either a scalar value or a zero-dimensional [ndarray][@stdlib/ndarray/ctor]. + +By default, the function performs reduction over all all elements in a provided [ndarray][@stdlib/ndarray/ctor]. To reduce specific dimensions, set the `dims` option. + +```javascript +var array = require( '@stdlib/ndarray/array' ); +var ndarray2array = require( '@stdlib/ndarray/to-array' ); + +function isEven( value ) { + return value % 2.0 === 0.0; +} + +// Create an input ndarray: +var x = array( [ [ [ 1.0, 2.0 ], [ 3.0, 4.0 ] ], [ [ 5.0, 6.0 ], [ 7.0, 8.0 ] ] ] ); +// returns + +var opts = { + 'dims': [ 0 ] +}; + +// Perform reduction: +var out = findLast( x, opts, isEven ); +// returns + +var v = ndarray2array( out ); +// returns [ [ 8.0, NaN ], [ 6.0, NaN ] ] +``` + +By default, the function returns an [ndarray][@stdlib/ndarray/ctor] having a shape matching only the non-reduced dimensions of the input [ndarray][@stdlib/ndarray/ctor] (i.e., the reduced dimensions are dropped). To include the reduced dimensions as singleton dimensions in the output [ndarray][@stdlib/ndarray/ctor], set the `keepdims` option to `true`. + +```javascript +var array = require( '@stdlib/ndarray/array' ); +var ndarray2array = require( '@stdlib/ndarray/to-array' ); + +function isEven( value ) { + return value % 2.0 === 0.0; +} + +// Create an input ndarray: +var x = array( [ [ [ 1.0, 2.0 ], [ 3.0, 4.0 ] ], [ [ 5.0, 6.0 ], [ 7.0, 8.0 ] ] ] ); +// returns + +var opts = { + 'dims': [ 0 ], + 'keepdims': true +}; + +// Perform reduction: +var out = findLast( x, opts, isEven ); +// returns + +var v = ndarray2array( out ); +// returns [ [ [ 8.0, NaN ], [ 6.0, NaN ] ] ] +``` + +To specify a custom sentinel value to return when no element passes the test, set the `sentinelValue` option. + +```javascript +var array = require( '@stdlib/ndarray/array' ); +var ndarray2array = require( '@stdlib/ndarray/to-array' ); + +function isEven( value ) { + return value % 2.0 === 0.0; +} + +// Create an input ndarray: +var x = array( [ [ [ 1.0, 3.0 ], [ 5.0, 7.0 ] ], [ [ 9.0, 11.0 ], [ 13.0, 15.0 ] ] ] ); +// returns + +var opts = { + 'sentinelValue': -999 +}; + +// Perform reduction: +var out = findLast( x, opts, isEven ); +// returns + +var v = out.get(); +// returns -999 +``` + +To set the `predicate` function execution context, provide a `thisArg`. + + + +```javascript +var array = require( '@stdlib/ndarray/array' ); +var ndarray2array = require( '@stdlib/ndarray/to-array' ); + +function isEven( value ) { + this.count += 1; + return value % 2.0 === 0.0; +} + +// Create an input ndarray: +var x = array( [ [ [ 1.0, 2.0 ], [ 3.0, 4.0 ] ], [ [ 5.0, 6.0 ], [ 7.0, 8.0 ] ] ] ); +// returns + +var ctx = { + 'count': 0 +}; + +// Perform reduction: +var out = findLast( x, isEven, ctx ); +// returns + +var v = out.get(); +// returns 8.0 + +var count = ctx.count; +// returns 1 +``` + +#### findLast.assign( x, out\[, options], predicate\[, thisArg] ) + +Finds the last elements which pass a test implemented by a predicate function along one or more [ndarray][@stdlib/ndarray/ctor] dimensions and assigns results to a provided output [ndarray][@stdlib/ndarray/ctor]. + +```javascript +var array = require( '@stdlib/ndarray/array' ); +var empty = require( '@stdlib/ndarray/empty' ); + +function isEven( value ) { + return value % 2.0 === 0.0; +} + +// Create an input ndarray: +var x = array( [ [ [ 1.0, 2.0 ], [ 3.0, 4.0 ] ], [ [ 5.0, 6.0 ], [ 7.0, 8.0 ] ] ] ); +// returns + +// Create an output ndarray: +var y = empty( [], { + 'dtype': x.dtype +}); + +// Perform reduction: +var out = findLast.assign( x, y, isEven ); +// returns + +var bool = ( out === y ); +// returns true + +var v = y.get(); +// returns 8.0 +``` + +The function accepts the following arguments: + +- **x**: input [ndarray][@stdlib/ndarray/ctor]. +- **out**: output [ndarray][@stdlib/ndarray/ctor]. +- **options**: function options _(optional)_. +- **predicate**: predicate function. +- **thisArg**: predicate function execution context _(optional)_. + +The function accepts the following options: + +- **dims**: list of dimensions over which to perform a reduction. +- **sentinelValue**: value to return when no element passes the test. May be either a scalar value or a zero-dimensional [ndarray][@stdlib/ndarray/ctor]. + +```javascript +var array = require( '@stdlib/ndarray/array' ); +var empty = require( '@stdlib/ndarray/empty' ); +var ndarray2array = require( '@stdlib/ndarray/to-array' ); + +function isEven( value ) { + return value % 2.0 === 0.0; +} + +// Create an input ndarray: +var x = array( [ [ [ 1.0, 2.0 ], [ 3.0, 4.0 ] ], [ [ 5.0, 6.0 ], [ 7.0, 8.0 ] ] ] ); +// returns + +// Create an output ndarray: +var y = empty( [ 2, 2 ], { + 'dtype': x.dtype +}); + +var opts = { + 'dims': [ 0 ] +}; + +// Perform reduction: +var out = findLast.assign( x, y, opts, isEven ); + +var bool = ( out === y ); +// returns true + +var v = ndarray2array( y ); +// returns [ [ 8.0, NaN ], [ 6.0, NaN ] ] +``` + +
+ + + +
+ +## Notes + +- By default, when no `sentinelValue` is provided, the function returns a default sentinel value based on the input [ndarray][@stdlib/ndarray/ctor] [data-type][@stdlib/ndarray/dtypes]: + + - real-valued floating-point data types: `NaN`. + - complex-valued floating-point data types: `NaN + NaNj`. + - integer data types: maximum value. + - boolean data types: `false`. + +- The `predicate` function is provided the following arguments: + + - **value**: current array element. + - **indices**: current array element indices. + - **arr**: the input [ndarray][@stdlib/ndarray/ctor]. + +
+ + + +
+ +## Examples + + + +```javascript +var uniform = require( '@stdlib/random/uniform' ); +var isPositive = require( '@stdlib/assert/is-positive-number' ).isPrimitive; +var ndarray2array = require( '@stdlib/ndarray/to-array' ); +var findLast = require( '@stdlib/ndarray/find-last' ); + +var x = uniform( [ 2, 4, 5 ], -10.0, 10.0, { + 'dtype': 'float64' +}); +console.log( ndarray2array( x ) ); + +var y = findLast( x, isPositive ); +console.log( y.get() ); +``` + +
+ + + + + + + + + + + + diff --git a/lib/node_modules/@stdlib/ndarray/find-last/benchmark/benchmark.1d.js b/lib/node_modules/@stdlib/ndarray/find-last/benchmark/benchmark.1d.js new file mode 100644 index 000000000000..f79f189098ad --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/find-last/benchmark/benchmark.1d.js @@ -0,0 +1,152 @@ +/** +* @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 isndarrayLike = require( '@stdlib/assert/is-ndarray-like' ); +var pow = require( '@stdlib/math/base/special/pow' ); +var discreteUniform = require( '@stdlib/random/array/discrete-uniform' ); +var shape2strides = require( '@stdlib/ndarray/base/shape2strides' ); +var ndarray = require( '@stdlib/ndarray/ctor' ); +var pkg = require( './../package.json' ).name; +var findLast = require( './../lib' ); + + +// VARIABLES // + +var types = [ 'float64' ]; +var orders = [ 'row-major', 'column-major' ]; + + +// FUNCTIONS // + +/** +* Callback function. +* +* @param {*} v - value +* @returns {boolean} result +*/ +function clbk( v ) { + return v < 0.0; +} + +/** +* Creates a benchmark function. +* +* @private +* @param {PositiveInteger} len - ndarray length +* @param {NonNegativeIntegerArray} shape - ndarray shape +* @param {string} xtype - ndarray data type +* @param {string} order - memory layout +* @param {NonNegativeIntegerArray} dims - list of dimensions to reduce +* @returns {Function} benchmark function +*/ +function createBenchmark( len, shape, xtype, order, dims ) { + var x; + + x = discreteUniform( len, 1, 100, { + 'dtype': xtype + }); + x = new ndarray( xtype, x, shape, shape2strides( shape, order ), 0, order ); + + return benchmark; + + /** + * Benchmark function. + * + * @private + * @param {Benchmark} b - benchmark instance + */ + function benchmark( b ) { + var opts; + var out; + var i; + + opts = { + 'dims': dims + }; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + out = findLast( x, opts, clbk ); + if ( typeof out !== 'object' ) { + b.fail( 'should return an ndarray' ); + } + } + b.toc(); + if ( !isndarrayLike( out ) ) { + b.fail( 'should return an ndarray' ); + } + b.pass( 'benchmark finished' ); + b.end(); + } +} + + +// MAIN // + +/** +* Main execution sequence. +* +* @private +*/ +function main() { + var dims; + var len; + var min; + var max; + var ord; + var sh; + var t1; + var f; + var i; + var j; + var k; + var n; + var d; + + min = 1; // 10^min + max = 6; // 10^max + + d = [ + [ 0 ], + [] + ]; + + for ( n = 0; n < d.length; n++ ) { + dims = d[ n ]; + for ( k = 0; k < orders.length; k++ ) { + ord = orders[ k ]; + for ( j = 0; j < types.length; j++ ) { + t1 = types[ j ]; + for ( i = min; i <= max; i++ ) { + len = pow( 10, i ); + + sh = [ len ]; + f = createBenchmark( len, sh, t1, ord, dims ); + bench( pkg+':ndims='+sh.length+',len='+len+',shape=['+sh.join(',')+'],xorder='+ord+',xtype='+t1+',dims=['+dims.join(',')+']', f ); + } + } + } + } +} + +main(); diff --git a/lib/node_modules/@stdlib/ndarray/find-last/benchmark/benchmark.2d.js b/lib/node_modules/@stdlib/ndarray/find-last/benchmark/benchmark.2d.js new file mode 100644 index 000000000000..3509fcb5453a --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/find-last/benchmark/benchmark.2d.js @@ -0,0 +1,159 @@ +/** +* @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 isndarrayLike = require( '@stdlib/assert/is-ndarray-like' ); +var pow = require( '@stdlib/math/base/special/pow' ); +var sqrt = require( '@stdlib/math/base/special/sqrt' ); +var floor = require( '@stdlib/math/base/special/floor' ); +var discreteUniform = require( '@stdlib/random/array/discrete-uniform' ); +var shape2strides = require( '@stdlib/ndarray/base/shape2strides' ); +var ndarray = require( '@stdlib/ndarray/ctor' ); +var pkg = require( './../package.json' ).name; +var findLast = require( './../lib' ); + + +// VARIABLES // + +var types = [ 'float64' ]; +var orders = [ 'row-major', 'column-major' ]; + + +// FUNCTIONS // + +/** +* Callback function. +* +* @param {*} v - value +* @returns {boolean} result +*/ +function clbk( v ) { + return v < 0.0; +} + +/** +* Creates a benchmark function. +* +* @private +* @param {PositiveInteger} len - ndarray length +* @param {NonNegativeIntegerArray} shape - ndarray shape +* @param {string} xtype - ndarray data type +* @param {string} order - memory layout +* @param {NonNegativeIntegerArray} dims - list of dimensions to reduce +* @returns {Function} benchmark function +*/ +function createBenchmark( len, shape, xtype, order, dims ) { + var x; + + x = discreteUniform( len, 1, 100, { + 'dtype': xtype + }); + x = new ndarray( xtype, x, shape, shape2strides( shape, order ), 0, order ); + + return benchmark; + + /** + * Benchmark function. + * + * @private + * @param {Benchmark} b - benchmark instance + */ + function benchmark( b ) { + var opts; + var out; + var i; + + opts = { + 'dims': dims + }; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + out = findLast( x, opts, clbk ); + if ( typeof out !== 'object' ) { + b.fail( 'should return an ndarray' ); + } + } + b.toc(); + if ( !isndarrayLike( out ) ) { + b.fail( 'should return an ndarray' ); + } + b.pass( 'benchmark finished' ); + b.end(); + } +} + + +// MAIN // + +/** +* Main execution sequence. +* +* @private +*/ +function main() { + var dims; + var len; + var min; + var max; + var ord; + var sh; + var t1; + var f; + var i; + var j; + var k; + var n; + var d; + + min = 1; // 10^min + max = 6; // 10^max + + d = [ + [ 0 ], + [ 1 ], + [ 0, 1 ], + [] + ]; + + for ( n = 0; n < d.length; n++ ) { + dims = d[ n ]; + for ( k = 0; k < orders.length; k++ ) { + ord = orders[ k ]; + for ( j = 0; j < types.length; j++ ) { + t1 = types[ j ]; + for ( i = min; i <= max; i++ ) { + len = pow( 10, i ); + + sh = [ floor( sqrt( len ) ), floor( sqrt( len ) ) ]; + if ( sh[ 0 ]*sh[ 1 ] !== len ) { + continue; + } + f = createBenchmark( len, sh, t1, ord, dims ); + bench( pkg+':ndims='+sh.length+',len='+len+',shape=['+sh.join(',')+'],xorder='+ord+',xtype='+t1+',dims=['+dims.join(',')+']', f ); + } + } + } + } +} + +main(); diff --git a/lib/node_modules/@stdlib/ndarray/find-last/docs/repl.txt b/lib/node_modules/@stdlib/ndarray/find-last/docs/repl.txt new file mode 100644 index 000000000000..126be0ebddb5 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/find-last/docs/repl.txt @@ -0,0 +1,106 @@ + +{{alias}}( x[, options], predicate[, thisArg] ) + Return a new ndarray containing the last elements which pass a test + implemented by a predicate function along one or more ndarray dimensions. + + Parameters + ---------- + x: ndarray + Input ndarray. + + options: Object (optional) + Function options. + + options.dims: Array (optional) + List of dimensions over which to perform a reduction. If not provided, + the function performs a reduction over all elements in a provided input + ndarray. + + options.keepdims: boolean (optional) + Boolean indicating whether the reduced dimensions should be included in + the returned ndarray as singleton dimensions. Default: false. + + options.sentinelValue: any|ndarray (optional) + Value to return when no element passes the test. May be either a scalar + value or a zero-dimensional ndarray. + + predicate: Function + Predicate function. + + thisArg: Any (optional) + Predicate execution context. + + Returns + ------- + out: ndarray + Output ndarray. When performing a reduction over all elements, the + function returns a zero-dimensional ndarray containing the result. + + Examples + -------- + > function f ( v ) { return v > 1.0; }; + > var x = {{alias:@stdlib/ndarray/array}}( [[[1,2],[3,4]],[[5,6],[7,8]]] ); + > var y = {{alias}}( x, f ) + + > y.get() + 8 + > y = {{alias}}( x, { 'keepdims': true }, f ) + + > {{alias:@stdlib/ndarray/to-array}}( y ) + [ [ [ 8 ] ] ] + > y.get( 0, 0, 0 ) + 8 + + +{{alias}}.assign( x, out[, options], predicate[, thisArg] ) + Finds the last elements which pass a test implemented by a predicate + function along one or more ndarray dimensions and assign results to a + provided output ndarray. + + Parameters + ---------- + x: ndarray + Input ndarray. + + out: ndarray + Output ndarray. The output shape must match the shape of the non-reduced + dimensions of the input ndarray. + + options: Object (optional) + Function options. + + options.dims: Array (optional) + List of dimensions over which to perform a reduction. If not provided, + the function performs a reduction over all elements in a provided input + ndarray. + + options.sentinelValue: any|ndarray (optional) + Value to return when no element passes the test. May be either a scalar + value or a zero-dimensional ndarray. + + predicate: Function + Predicate function. + + thisArg: Any (optional) + Predicate execution context. + + Returns + ------- + out: ndarray + Output ndarray. + + Examples + -------- + > function f ( v ) { return v > 1.0; }; + > var x = {{alias:@stdlib/ndarray/array}}( [[[1,2],[3,4]],[[5,6],[7,8]]] ); + > var y = {{alias:@stdlib/ndarray/from-scalar}}( 0 ); + > var out = {{alias}}.assign( x, y, f ) + + > var bool = ( out === y ) + true + > y.get() + 8 + + See Also + -------- + diff --git a/lib/node_modules/@stdlib/ndarray/find-last/docs/types/index.d.ts b/lib/node_modules/@stdlib/ndarray/find-last/docs/types/index.d.ts new file mode 100644 index 000000000000..3b6aa6e7f4d7 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/find-last/docs/types/index.d.ts @@ -0,0 +1,350 @@ +/* +* @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 { ArrayLike } from '@stdlib/types/array'; +import { ndarray, typedndarray } from '@stdlib/types/ndarray'; + +/** +* Returns a boolean indicating whether an element passes a test. +* +* @returns boolean indicating whether an ndarray element passes a test +*/ +type Nullary = ( this: ThisArg ) => boolean; + +/** +* Returns a boolean indicating whether an element passes a test. +* +* @param value - current array element +* @returns boolean indicating whether an ndarray element passes a test +*/ +type Unary = ( this: ThisArg, value: T ) => boolean; + +/** +* Returns a boolean indicating whether an element passes a test. +* +* @param value - current array element +* @param indices - current array element indices +* @returns boolean indicating whether an ndarray element passes a test +*/ +type Binary = ( this: ThisArg, value: T, indices: Array ) => boolean; + +/** +* Returns a boolean indicating whether an element passes a test. +* +* @param value - current array element +* @param indices - current array element indices +* @param arr - input array +* @returns boolean indicating whether an ndarray element passes a test +*/ +type Ternary = ( this: ThisArg, value: T, indices: Array, arr: U ) => boolean; + +/** +* Returns a boolean indicating whether an element passes a test. +* +* @param value - current array element +* @param indices - current array element indices +* @param arr - input array +* @returns boolean indicating whether an ndarray element passes a test +*/ +type Predicate = Nullary | Unary | Binary | Ternary; + +/** +* Base options. +*/ +interface BaseOptions { + /** + * List of dimensions over which to perform the reduction. + */ + dims?: ArrayLike; +} + +/** +* Options. +*/ +interface Options extends BaseOptions { + /** + * Boolean indicating whether the reduced dimensions should be included in the returned ndarray as singleton dimensions. Default: `false`. + */ + keepdims?: boolean; + + /** + * Sentinel value. + */ + sentinelValue?: T | typedndarray; +} + +/** +* Interface describing `findLast`. +*/ +interface FindLast { + /** + * Returns a new ndarray containing the last elements which pass a test implemented by a predicate function along one or more ndarray dimensions. + * + * @param x - input ndarray + * @param predicate - predicate function + * @param thisArg - predicate execution context + * @returns output ndarray + * + * @example + * var Float64Array = require( '@stdlib/array/float64' ); + * var isEven = require( '@stdlib/assert/is-even' ).isPrimitive; + * var ndarray = require( '@stdlib/ndarray/ctor' ); + * + * // Create a data buffer: + * var xbuf = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] ); + * + * // Define the shape of the input array: + * var sh = [ 3, 2 ]; + * + * // Define the array strides: + * var sx = [ 2, 1 ]; + * + * // Define the index offset: + * var ox = 0; + * + * // Create an input ndarray: + * var x = new ndarray( 'float64', xbuf, sh, sx, ox, 'row-major' ); + * + * // Perform the operation: + * var out = findLast( x, isEven ); + * // returns + * + * var v = out.get(); + * // returns 6.0 + */ + = typedndarray, ThisArg = unknown>( x: U, predicate: Predicate, thisArg?: ThisParameterType> ): U; + + /** + * Returns a new ndarray containing the last elements which pass a test implemented by a predicate function along one or more ndarray dimensions. + * + * @param x - input ndarray + * @param options - function options + * @param options.dims - list of dimensions over which to perform a reduction + * @param options.keepdims - boolean indicating whether the reduced dimensions should be included in the returned ndarray as singleton dimensions (default: false) + * @param options.sentinelValue - sentinel value + * @param predicate - predicate function + * @param thisArg - predicate execution context + * @returns output ndarray + * + * @example + * var Float64Array = require( '@stdlib/array/float64' ); + * var isEven = require( '@stdlib/assert/is-even' ).isPrimitive; + * var ndarray = require( '@stdlib/ndarray/ctor' ); + * + * // Create a data buffer: + * var xbuf = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] ); + * + * // Define the shape of the input array: + * var sh = [ 3, 2 ]; + * + * // Define the array strides: + * var sx = [ 2, 1 ]; + * + * // Define the index offset: + * var ox = 0; + * + * // Create an input ndarray: + * var x = new ndarray( 'float64', xbuf, sh, sx, ox, 'row-major' ); + * + * var opts = { + * 'dims': [ 0 ] + * }; + * + * // Perform the operation: + * var out = findLast( x, opts, isEven ); + * // returns + * + * var v = out.get(); + * // returns 6.0 + */ + = typedndarray, ThisArg = unknown>( x: U, options: Options, predicate: Predicate, thisArg?: ThisParameterType> ): U; + + /** + * Finds the last elements which pass a test implemented by a predicate function along one or more ndarray dimensions and assigns results to a provided output ndarray. + * + * @param x - input ndarray + * @param out - output ndarray + * @param predicate - predicate function + * @param thisArg - predicate execution context + * @returns output ndarray + * + * @example + * var Float64Array = require( '@stdlib/array/float64' ); + * var ndarray = require( '@stdlib/ndarray/ctor' ); + * var isEven = require( '@stdlib/assert/is-even' ).isPrimitive; + * var empty = require( '@stdlib/ndarray/empty' ); + * + * // Create a data buffer: + * var xbuf = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] ); + * + * // Define the shape of the input array: + * var shape = [ 3, 2 ]; + * + * // Define the array strides: + * var sx = [ 2, 1 ]; + * + * // Define the index offset: + * var ox = 0; + * + * // Create an input ndarray: + * var x = new ndarray( 'float64', xbuf, shape, sx, ox, 'row-major' ); + * + * // Create an output ndarray: + * var y = empty( [], { + * 'dtype': 'float64' + * }); + * + * // Perform the operation: + * var out = findLast.assign( x, y, isEven ); + * // returns + * + * var v = out.get(); + * // returns 6.0 + */ + assign = typedndarray, ThisArg = unknown>( x: ndarray, out: U, predicate: Predicate, thisArg?: ThisParameterType> ): U; + + /** + * Finds the last elements which pass a test implemented by a predicate function along one or more ndarray dimensions and assigns results to a provided output ndarray. + * + * @param x - input ndarray + * @param out - output ndarray + * @param options - function options + * @param options.dims - list of dimensions over which to perform a reduction + * @param options.sentinelValue - sentinel value + * @param predicate - predicate function + * @param thisArg - predicate execution context + * @returns output ndarray + * + * @example + * var Float64Array = require( '@stdlib/array/float64' ); + * var ndarray = require( '@stdlib/ndarray/ctor' ); + * var isEven = require( '@stdlib/assert/is-even' ).isPrimitive; + * var empty = require( '@stdlib/ndarray/empty' ); + * + * // Create a data buffer: + * var xbuf = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] ); + * + * // Define the shape of the input array: + * var shape = [ 3, 2 ]; + * + * // Define the array strides: + * var sx = [ 2, 1 ]; + * + * // Define the index offset: + * var ox = 0; + * + * // Create an input ndarray: + * var x = new ndarray( 'float64', xbuf, shape, sx, ox, 'row-major' ); + * + * // Create an output ndarray: + * var y = empty( [], { + * 'dtype': 'float64' + * }); + * + * // Perform the operation: + * var out = findLast.assign( x, y, {}, isEven ); + * // returns + * + * var v = out.get(); + * // returns 6.0 + */ + assign = typedndarray, V = T, ThisArg = unknown>( x: ndarray, out: U, options: Options, predicate: Predicate, thisArg?: ThisParameterType> ): U; +} + +/** +* Returns a new ndarray containing the last elements which pass a test implemented by a predicate function along one or more ndarray dimensions. +* +* @param x - input ndarray +* @param options - function options +* @param options.dims - list of dimensions over which to perform a reduction +* @param options.keepdims - boolean indicating whether the reduced dimensions should be included in the returned ndarray as singleton dimensions (default: false) +* @param options.sentinelValue - sentinel value +* @param predicate - predicate function +* @param thisArg - predicate execution context +* @returns output ndarray +* +* @example +* var Float64Array = require( '@stdlib/array/float64' ); +* var isEven = require( '@stdlib/assert/is-even' ).isPrimitive; +* var ndarray = require( '@stdlib/ndarray/ctor' ); +* +* // Create a data buffer: +* var xbuf = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] ); +* +* // Define the shape of the input array: +* var sh = [ 3, 2 ]; +* +* // Define the array strides: +* var sx = [ 2, 1 ]; +* +* // Define the index offset: +* var ox = 0; +* +* // Create an input ndarray: +* var x = new ndarray( 'float64', xbuf, sh, sx, ox, 'row-major' ); +* +* // Perform the operation: +* var out = findLast( x, isEven ); +* // returns +* +* var v = out.get(); +* // returns 6.0 +* +* @example +* var Float64Array = require( '@stdlib/array/float64' ); +* var ndarray = require( '@stdlib/ndarray/ctor' ); +* var isEven = require( '@stdlib/assert/is-even' ).isPrimitive; +* var empty = require( '@stdlib/ndarray/empty' ); +* +* // Create a data buffer: +* var xbuf = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] ); +* +* // Define the shape of the input array: +* var shape = [ 3, 2 ]; +* +* // Define the array strides: +* var sx = [ 2, 1 ]; +* +* // Define the index offset: +* var ox = 0; +* +* // Create an input ndarray: +* var x = new ndarray( 'float64', xbuf, shape, sx, ox, 'row-major' ); +* +* // Create an output ndarray: +* var y = empty( [], { +* 'dtype': 'float64' +* }); +* +* // Perform the operation: +* var out = findLast.assign( x, y, isEven ); +* // returns +* +* var v = out.get(); +* // returns 6.0 +*/ +declare var findLast: FindLast; + + +// EXPORTS // + +export = findLast; diff --git a/lib/node_modules/@stdlib/ndarray/find-last/docs/types/test.ts b/lib/node_modules/@stdlib/ndarray/find-last/docs/types/test.ts new file mode 100644 index 000000000000..7f1cc76f105f --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/find-last/docs/types/test.ts @@ -0,0 +1,402 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2025 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* eslint-disable space-in-parens */ + +/// + +import zeros = require( '@stdlib/ndarray/zeros' ); +import empty = require( '@stdlib/ndarray/empty' ); +import findLast = require( './index' ); + +/** +* Predicate function. +* +* @param v - ndarray element +* @returns result +*/ +function clbk( v: number ): boolean { + return v > 0.0; +} + + +// TESTS // + +// The function returns an ndarray... +{ + const x = zeros( [ 2, 2 ], { + 'dtype': 'float64' + }); + + findLast( x, clbk ); // $ExpectType float64ndarray + findLast( x, clbk, {} ); // $ExpectType float64ndarray + findLast( x, { 'keepdims': true }, clbk ); // $ExpectType float64ndarray + findLast( x, { 'keepdims': true }, clbk, {} ); // $ExpectType float64ndarray + findLast( x, { 'dims': [ 0 ] }, clbk ); // $ExpectType float64ndarray + findLast( x, { 'dims': [ 0 ] }, clbk, {} ); // $ExpectType float64ndarray + findLast( x, { 'sentinelValue': 0 }, clbk ); // $ExpectType float64ndarray + findLast( x, { 'sentinelValue': 0 }, clbk, {} ); // $ExpectType float64ndarray +} + +// The compiler throws an error if the function is provided a first argument which is not an ndarray... +{ + findLast( 5, clbk ); // $ExpectError + findLast( true, clbk ); // $ExpectError + findLast( false, clbk ); // $ExpectError + findLast( null, clbk ); // $ExpectError + findLast( undefined, clbk ); // $ExpectError + findLast( {}, clbk ); // $ExpectError + findLast( [ 1 ], clbk ); // $ExpectError + findLast( ( x: number ): number => x, clbk ); // $ExpectError + + findLast( 5, {}, clbk ); // $ExpectError + findLast( true, {}, clbk ); // $ExpectError + findLast( false, {}, clbk ); // $ExpectError + findLast( null, {}, clbk ); // $ExpectError + findLast( undefined, {}, clbk ); // $ExpectError + findLast( {}, {}, clbk ); // $ExpectError + findLast( [ 1 ], {}, clbk ); // $ExpectError + findLast( ( x: number ): number => x, {}, clbk ); // $ExpectError + + findLast( 5, clbk, {} ); // $ExpectError + findLast( true, clbk, {} ); // $ExpectError + findLast( false, clbk, {} ); // $ExpectError + findLast( null, clbk, {} ); // $ExpectError + findLast( undefined, clbk, {} ); // $ExpectError + findLast( {}, clbk, {} ); // $ExpectError + findLast( [ 1 ], clbk, {} ); // $ExpectError + findLast( ( x: number ): number => x, clbk, {} ); // $ExpectError + + findLast( 5, {}, clbk, {} ); // $ExpectError + findLast( true, {}, clbk, {} ); // $ExpectError + findLast( false, {}, clbk, {} ); // $ExpectError + findLast( null, {}, clbk, {} ); // $ExpectError + findLast( undefined, {}, clbk, {} ); // $ExpectError + findLast( {}, {}, clbk, {} ); // $ExpectError + findLast( [ 1 ], {}, clbk, {} ); // $ExpectError + findLast( ( 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 ] ); + + findLast( x, '5', clbk ); // $ExpectError + findLast( x, 5, clbk ); // $ExpectError + findLast( x, true, clbk ); // $ExpectError + findLast( x, false, clbk ); // $ExpectError + findLast( x, null, clbk ); // $ExpectError + findLast( x, [ 1 ], clbk ); // $ExpectError + findLast( x, ( x: number ): number => x, clbk ); // $ExpectError + + findLast( x, '5', clbk, {} ); // $ExpectError + findLast( x, 5, clbk, {} ); // $ExpectError + findLast( x, true, clbk, {} ); // $ExpectError + findLast( x, false, clbk, {} ); // $ExpectError + findLast( x, null, clbk, {} ); // $ExpectError + findLast( x, [ 1 ], clbk, {} ); // $ExpectError + findLast( x, ( x: number ): number => 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 ] ); + + findLast( x, '5' ); // $ExpectError + findLast( x, 5 ); // $ExpectError + findLast( x, true ); // $ExpectError + findLast( x, false ); // $ExpectError + findLast( x, null ); // $ExpectError + findLast( x, [ 1 ] ); // $ExpectError + findLast( x, {} ); // $ExpectError + + findLast( x, '5', {} ); // $ExpectError + findLast( x, 5, {} ); // $ExpectError + findLast( x, true, {} ); // $ExpectError + findLast( x, false, {} ); // $ExpectError + findLast( x, null, {} ); // $ExpectError + findLast( x, [ 1 ], {} ); // $ExpectError + findLast( x, {}, {} ); // $ExpectError + + findLast( x, {}, '5' ); // $ExpectError + findLast( x, {}, 5 ); // $ExpectError + findLast( x, {}, true ); // $ExpectError + findLast( x, {}, false ); // $ExpectError + findLast( x, {}, null ); // $ExpectError + findLast( x, {}, [ 1 ] ); // $ExpectError + findLast( x, {}, {} ); // $ExpectError + + findLast( x, {}, '5', {} ); // $ExpectError + findLast( x, {}, 5, {} ); // $ExpectError + findLast( x, {}, true, {} ); // $ExpectError + findLast( x, {}, false, {} ); // $ExpectError + findLast( x, {}, null, {} ); // $ExpectError + findLast( x, {}, [ 1 ], {} ); // $ExpectError + findLast( x, {}, {}, {} ); // $ExpectError +} + +// The compiler throws an error if the function is provided a `keepdims` option which is not a boolean... +{ + const x = zeros( [ 2, 2 ] ); + + findLast( x, { 'keepdims': '5' }, clbk ); // $ExpectError + findLast( x, { 'keepdims': 5 }, clbk ); // $ExpectError + findLast( x, { 'keepdims': null }, clbk ); // $ExpectError + findLast( x, { 'keepdims': [ 1 ] }, clbk ); // $ExpectError + findLast( x, { 'keepdims': {} }, clbk ); // $ExpectError + findLast( x, { 'keepdims': ( x: number ): number => x }, clbk ); // $ExpectError + + findLast( x, { 'keepdims': '5' }, clbk, {} ); // $ExpectError + findLast( x, { 'keepdims': 5 }, clbk, {} ); // $ExpectError + findLast( x, { 'keepdims': null }, clbk, {} ); // $ExpectError + findLast( x, { 'keepdims': [ 1 ] }, clbk, {} ); // $ExpectError + findLast( x, { 'keepdims': {} }, clbk, {} ); // $ExpectError + findLast( x, { 'keepdims': ( x: number ): number => x }, clbk, {} ); // $ExpectError +} + +// The compiler throws an error if the function is provided a `dims` option which is not an array of numbers... +{ + const x = zeros( [ 2, 2 ] ); + + findLast( x, { 'dims': '5' }, clbk ); // $ExpectError + findLast( x, { 'dims': 5 }, clbk ); // $ExpectError + findLast( x, { 'dims': null }, clbk ); // $ExpectError + findLast( x, { 'dims': true }, clbk ); // $ExpectError + findLast( x, { 'dims': false }, clbk ); // $ExpectError + findLast( x, { 'dims': {} }, clbk ); // $ExpectError + findLast( x, { 'dims': ( x: number ): number => x }, clbk ); // $ExpectError + + findLast( x, { 'dims': '5' }, clbk, {} ); // $ExpectError + findLast( x, { 'dims': 5 }, clbk, {} ); // $ExpectError + findLast( x, { 'dims': null }, clbk, {} ); // $ExpectError + findLast( x, { 'dims': true }, clbk, {} ); // $ExpectError + findLast( x, { 'dims': false }, clbk, {} ); // $ExpectError + findLast( x, { 'dims': {} }, clbk, {} ); // $ExpectError + findLast( x, { 'dims': ( 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 ] ); + + findLast(); // $ExpectError + findLast( x ); // $ExpectError + findLast( x, {}, clbk, {}, {} ); // $ExpectError +} + +// Attached to the function is an `assign` method which returns an ndarray... +{ + const x = zeros( [ 2, 2 ] ); + const y = empty( [], { + 'dtype': 'float64' + }); + + findLast.assign( x, y, clbk ); // $ExpectType float64ndarray + findLast.assign( x, y, clbk, {} ); // $ExpectType float64ndarray + findLast.assign( x, y, {}, clbk ); // $ExpectType float64ndarray + findLast.assign( x, y, { 'dims': [ 0 ] }, clbk ); // $ExpectType float64ndarray + findLast.assign( x, y, {}, clbk, {} ); // $ExpectType float64ndarray + findLast.assign( x, y, { 'dims': [ 0 ] }, clbk, {} ); // $ExpectType float64ndarray + findLast.assign( x, y, { 'sentinelValue': 0 }, clbk ); // $ExpectType float64ndarray + findLast.assign( x, y, { 'sentinelValue': 0 }, clbk, {} ); // $ExpectType float64ndarray +} + +// The compiler throws an error if the `assign` method is provided a first argument which is not an ndarray... +{ + const y = empty( [], { + 'dtype': 'float64' + }); + + findLast.assign( 5, y, clbk ); // $ExpectError + findLast.assign( true, y, clbk ); // $ExpectError + findLast.assign( false, y, clbk ); // $ExpectError + findLast.assign( null, y, clbk ); // $ExpectError + findLast.assign( undefined, y, clbk ); // $ExpectError + findLast.assign( {}, y, clbk ); // $ExpectError + findLast.assign( [ 1 ], y, clbk ); // $ExpectError + findLast.assign( ( x: number ): number => x, y, clbk ); // $ExpectError + + findLast.assign( 5, y, {}, clbk ); // $ExpectError + findLast.assign( true, y, {}, clbk ); // $ExpectError + findLast.assign( false, y, {}, clbk ); // $ExpectError + findLast.assign( null, y, {}, clbk ); // $ExpectError + findLast.assign( undefined, y, {}, clbk ); // $ExpectError + findLast.assign( {}, y, {}, clbk ); // $ExpectError + findLast.assign( [ 1 ], y, {}, clbk ); // $ExpectError + findLast.assign( ( x: number ): number => x, y, {}, clbk ); // $ExpectError + + findLast.assign( 5, y, clbk, {} ); // $ExpectError + findLast.assign( true, y, clbk, {} ); // $ExpectError + findLast.assign( false, y, clbk, {} ); // $ExpectError + findLast.assign( null, y, clbk, {} ); // $ExpectError + findLast.assign( undefined, y, clbk, {} ); // $ExpectError + findLast.assign( {}, y, clbk, {} ); // $ExpectError + findLast.assign( [ 1 ], y, clbk, {} ); // $ExpectError + findLast.assign( ( x: number ): number => x, y, clbk, {} ); // $ExpectError + + findLast.assign( 5, y, {}, clbk, {} ); // $ExpectError + findLast.assign( true, y, {}, clbk, {} ); // $ExpectError + findLast.assign( false, y, {}, clbk, {} ); // $ExpectError + findLast.assign( null, y, {}, clbk, {} ); // $ExpectError + findLast.assign( undefined, y, {}, clbk, {} ); // $ExpectError + findLast.assign( {}, y, {}, clbk, {} ); // $ExpectError + findLast.assign( [ 1 ], y, {}, clbk, {} ); // $ExpectError + findLast.assign( ( x: number ): number => x, y, {}, clbk, {} ); // $ExpectError +} + +// The compiler throws an error if the `assign` method is provided an output argument which is not an ndarray... +{ + const x = zeros( [ 2, 2 ] ); + + findLast.assign( x, 5, clbk ); // $ExpectError + findLast.assign( x, true, clbk ); // $ExpectError + findLast.assign( x, false, clbk ); // $ExpectError + findLast.assign( x, null, clbk ); // $ExpectError + findLast.assign( x, undefined, clbk ); // $ExpectError + findLast.assign( x, {}, clbk ); // $ExpectError + findLast.assign( x, [ 1 ], clbk ); // $ExpectError + findLast.assign( x, ( x: number ): number => x, clbk ); // $ExpectError + + findLast.assign( x, 5, {}, clbk ); // $ExpectError + findLast.assign( x, true, {}, clbk ); // $ExpectError + findLast.assign( x, false, {}, clbk ); // $ExpectError + findLast.assign( x, null, {}, clbk ); // $ExpectError + findLast.assign( x, undefined, {}, clbk ); // $ExpectError + findLast.assign( x, {}, {}, clbk ); // $ExpectError + findLast.assign( x, [ 1 ], {}, clbk ); // $ExpectError + findLast.assign( x, ( x: number ): number => x, {}, clbk ); // $ExpectError + + findLast.assign( x, 5, clbk, {} ); // $ExpectError + findLast.assign( x, true, clbk, {} ); // $ExpectError + findLast.assign( x, false, clbk, {} ); // $ExpectError + findLast.assign( x, null, clbk, {} ); // $ExpectError + findLast.assign( x, undefined, clbk, {} ); // $ExpectError + findLast.assign( x, {}, clbk, {} ); // $ExpectError + findLast.assign( x, [ 1 ], clbk, {} ); // $ExpectError + findLast.assign( x, ( x: number ): number => x, clbk, {} ); // $ExpectError + + findLast.assign( x, 5, {}, clbk, {} ); // $ExpectError + findLast.assign( x, true, {}, clbk, {} ); // $ExpectError + findLast.assign( x, false, {}, clbk, {} ); // $ExpectError + findLast.assign( x, null, {}, clbk, {} ); // $ExpectError + findLast.assign( x, undefined, {}, clbk, {} ); // $ExpectError + findLast.assign( x, {}, {}, clbk, {} ); // $ExpectError + findLast.assign( x, [ 1 ], {}, clbk, {} ); // $ExpectError + findLast.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 ] ); + const y = empty( [], { + 'dtype': 'float64' + }); + + findLast.assign( x, y, '5', clbk ); // $ExpectError + findLast.assign( x, y, 5, clbk ); // $ExpectError + findLast.assign( x, y, true, clbk ); // $ExpectError + findLast.assign( x, y, false, clbk ); // $ExpectError + findLast.assign( x, y, null, clbk ); // $ExpectError + findLast.assign( x, y, [ 1 ], clbk ); // $ExpectError + findLast.assign( x, y, ( x: number ): number => x, clbk ); // $ExpectError + + findLast.assign( x, y, '5', clbk, {} ); // $ExpectError + findLast.assign( x, y, 5, clbk, {} ); // $ExpectError + findLast.assign( x, y, true, clbk, {} ); // $ExpectError + findLast.assign( x, y, false, clbk, {} ); // $ExpectError + findLast.assign( x, y, null, clbk, {} ); // $ExpectError + findLast.assign( x, y, [ 1 ], clbk, {} ); // $ExpectError + findLast.assign( x, y, ( x: number ): number => x, 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 ] ); + const y = empty( [], { + 'dtype': 'float64' + }); + + findLast.assign( x, y, '5' ); // $ExpectError + findLast.assign( x, y, 5 ); // $ExpectError + findLast.assign( x, y, true ); // $ExpectError + findLast.assign( x, y, false ); // $ExpectError + findLast.assign( x, y, null ); // $ExpectError + findLast.assign( x, y, [ 1 ] ); // $ExpectError + findLast.assign( x, y, {} ); // $ExpectError + + findLast.assign( x, y, '5', {} ); // $ExpectError + findLast.assign( x, y, 5, {} ); // $ExpectError + findLast.assign( x, y, true, {} ); // $ExpectError + findLast.assign( x, y, false, {} ); // $ExpectError + findLast.assign( x, y, null, {} ); // $ExpectError + findLast.assign( x, y, [ 1 ], {} ); // $ExpectError + findLast.assign( x, y, {}, {} ); // $ExpectError + + findLast.assign( x, y, {}, '5' ); // $ExpectError + findLast.assign( x, y, {}, 5 ); // $ExpectError + findLast.assign( x, y, {}, true ); // $ExpectError + findLast.assign( x, y, {}, false ); // $ExpectError + findLast.assign( x, y, {}, null ); // $ExpectError + findLast.assign( x, y, {}, [ 1 ] ); // $ExpectError + findLast.assign( x, y, {}, {} ); // $ExpectError + + findLast.assign( x, y, {}, '5', {} ); // $ExpectError + findLast.assign( x, y, {}, 5, {} ); // $ExpectError + findLast.assign( x, y, {}, true, {} ); // $ExpectError + findLast.assign( x, y, {}, false, {} ); // $ExpectError + findLast.assign( x, y, {}, null, {} ); // $ExpectError + findLast.assign( x, y, {}, [ 1 ], {} ); // $ExpectError + findLast.assign( x, y, {}, {}, {} ); // $ExpectError +} + +// The compiler throws an error if the function is provided a `dims` option which is not an array of numbers... +{ + const x = zeros( [ 2, 2 ] ); + const y = empty( [], { + 'dtype': 'float64' + }); + + findLast.assign( x, y, { 'dims': '5' }, clbk ); // $ExpectError + findLast.assign( x, y, { 'dims': 5 }, clbk ); // $ExpectError + findLast.assign( x, y, { 'dims': null }, clbk ); // $ExpectError + findLast.assign( x, y, { 'dims': true }, clbk ); // $ExpectError + findLast.assign( x, y, { 'dims': false }, clbk ); // $ExpectError + findLast.assign( x, y, { 'dims': {} }, clbk ); // $ExpectError + findLast.assign( x, y, { 'dims': ( x: number ): number => x }, clbk ); // $ExpectError + + findLast.assign( x, y, { 'dims': '5' }, clbk, {} ); // $ExpectError + findLast.assign( x, y, { 'dims': 5 }, clbk, {} ); // $ExpectError + findLast.assign( x, y, { 'dims': null }, clbk, {} ); // $ExpectError + findLast.assign( x, y, { 'dims': true }, clbk, {} ); // $ExpectError + findLast.assign( x, y, { 'dims': false }, clbk, {} ); // $ExpectError + findLast.assign( x, y, { 'dims': {} }, clbk, {} ); // $ExpectError + findLast.assign( x, y, { 'dims': ( 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 ] ); + const y = empty( [], { + 'dtype': 'float64' + }); + + findLast.assign(); // $ExpectError + findLast.assign( x ); // $ExpectError + findLast.assign( x, y ); // $ExpectError + findLast.assign( x, y, {}, clbk, {}, {} ); // $ExpectError +} diff --git a/lib/node_modules/@stdlib/ndarray/find-last/examples/index.js b/lib/node_modules/@stdlib/ndarray/find-last/examples/index.js new file mode 100644 index 000000000000..43ba4c18b6b8 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/find-last/examples/index.js @@ -0,0 +1,32 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +var uniform = require( '@stdlib/random/uniform' ); +var isPositive = require( '@stdlib/assert/is-positive-number' ).isPrimitive; +var ndarray2array = require( '@stdlib/ndarray/to-array' ); +var findLast = require( './../lib' ); + +var x = uniform( [ 2, 4, 5 ], -10.0, 10.0, { + 'dtype': 'float64' +}); +console.log( ndarray2array( x ) ); + +var y = findLast( x, isPositive ); +console.log( y.get() ); diff --git a/lib/node_modules/@stdlib/ndarray/find-last/lib/assign.js b/lib/node_modules/@stdlib/ndarray/find-last/lib/assign.js new file mode 100644 index 000000000000..eddb88a94f7d --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/find-last/lib/assign.js @@ -0,0 +1,142 @@ +/** +* @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 isObject = require( '@stdlib/assert/is-plain-object' ); +var isFunction = require( '@stdlib/assert/is-function' ); +var isndarrayLike = require( '@stdlib/assert/is-ndarray-like' ); +var reverse = require( '@stdlib/ndarray/base/reverse' ); +var base = require( '@stdlib/ndarray/find' ).assign; +var format = require( '@stdlib/string/format' ); +var wrap = require( './callback_wrapper.js' ); + + +// MAIN // + +/** +* Finds the last elements which pass a test implemented by a predicate function along one or more ndarray dimensions and assigns results to a provided output ndarray. +* +* @param {ndarray} x - input ndarray +* @param {ndarray} out - output ndarray +* @param {Options} [options] - function options +* @param {IntegerArray} [options.dims] - list of dimensions over which to perform a reduction +* @param {boolean} [options.keepdims=false] - boolean indicating whether the reduced dimensions should be included in the returned ndarray as singleton dimensions +* @param {(*|ndarray)} [options.sentinelValue] - sentinel value +* @param {Function} predicate - predicate function +* @param {*} [thisArg] - predicate function execution context +* @throws {TypeError} first argument must be an ndarray-like object +* @throws {TypeError} second argument must be an ndarray-like object +* @throws {TypeError} options argument must be an object +* @throws {TypeError} predicate argument must be a function +* @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 isEven = require( '@stdlib/assert/is-even' ).isPrimitive; +* var array = require( '@stdlib/ndarray/array' ); +* var empty = require( '@stdlib/ndarray/empty' ); +* +* // Create an input ndarray: +* var x = array( [ [ [ 1.0, 2.0 ], [ 3.0, 4.0 ] ], [ [ 5.0, 6.0 ], [ 7.0, 8.0 ] ] ] ); +* // returns +* +* // Create an output ndarray: +* var y = empty( [], { +* 'dtype': x.dtype +* }); +* +* // Perform reduction: +* var out = assign( x, y, isEven ); +* // returns +* +* var v = out.get(); +* // returns 8.0 +*/ +function assign( x, out, options, predicate, thisArg ) { + var nargs; + var flg; + var ctx; + var cb; + var o; + var f; + var y; + + nargs = arguments.length; + if ( !isndarrayLike( x ) ) { + throw new TypeError( format( 'invalid argument. First argument must be an ndarray-like object. Value: `%s`.', x ) ); + } + if ( !isndarrayLike( out ) ) { + throw new TypeError( format( 'invalid argument. Second argument must be an ndarray-like object. Value: `%s`.', x ) ); + } + // Case: assign( x, out, predicate ) + if ( nargs < 4 ) { + if ( !isFunction( options ) ) { + throw new TypeError( format( 'invalid argument. Callback argument must be a function. Value: `%s`.', options ) ); + } + cb = options; + } + // Case: assign( x, out, options, predicate, thisArg ) + else if ( nargs > 4 ) { + flg = true; + o = options; + cb = predicate; + if ( !isFunction( cb ) ) { + throw new TypeError( format( 'invalid argument. Callback argument must be a function. Value: `%s`.', cb ) ); + } + ctx = thisArg; + } + // Case: assign( x, out, predicate, thisArg ) + else if ( isFunction( options ) ) { + cb = options; + ctx = predicate; + } + // Case: assign( x, out, options, predicate ) + else if ( isFunction( predicate ) ) { + flg = true; + o = options; + cb = predicate; + } + // Case: find( x, out, ???, ??? ) + else { + throw new TypeError( format( 'invalid argument. Callback argument must be a function. Value: `%s`.', predicate ) ); + } + + // Wrap the callback function... + f = wrap( x, cb, ctx ); + + // Create a reversed view of the input ndarray... + y = reverse( x, false ); + + if ( flg ) { + if ( !isObject( o ) ) { + throw new TypeError( format( 'invalid argument. Options argument must be an object. Value: `%s`.', o ) ); + } + return base( y, out, o, f ); + } + return base( y, out, f ); +} + + +// EXPORTS // + +module.exports = assign; diff --git a/lib/node_modules/@stdlib/ndarray/find-last/lib/callback_wrapper.js b/lib/node_modules/@stdlib/ndarray/find-last/lib/callback_wrapper.js new file mode 100644 index 000000000000..b2dfffde432b --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/find-last/lib/callback_wrapper.js @@ -0,0 +1,51 @@ +/** +* @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'; + +// MAIN // + +/** +* Wraps a provided callback function. +* +* @private +* @param {ndarray} arr - input ndarray +* @param {Function} clbk - callback function +* @param {thisArg} thisArg - callback execution context +* @returns {Function} callback wrapper +*/ +function wrap( arr, clbk, thisArg ) { + return wrapper; + + /** + * Invokes a callback function. + * + * @private + * @param {*} v - value + * @param {NonNegativeInteger} idx - current array element index + * @returns {*} result + */ + function wrapper( v, idx ) { + return clbk.call( thisArg, v, idx, arr ); + } +} + + +// EXPORTS // + +module.exports = wrap; diff --git a/lib/node_modules/@stdlib/ndarray/find-last/lib/index.js b/lib/node_modules/@stdlib/ndarray/find-last/lib/index.js new file mode 100644 index 000000000000..1458da8a366b --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/find-last/lib/index.js @@ -0,0 +1,57 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +/** +* Return a new ndarray containing the first elements which pass a test implemented by a predicate function along one or more ndarray dimensions. +* +* @module @stdlib/ndarray/find-last +* +* @example +* var isEven = require( '@stdlib/assert/is-even' ).isPrimitive; +* var array = require( '@stdlib/ndarray/array' ); +* var findLast = require( '@stdlib/ndarray/find-last' ); +* +* // Create an input ndarray: +* var x = array( [ [ [ 1.0, 2.0 ], [ 3.0, 4.0 ] ], [ [ 5.0, 6.0 ], [ 7.0, 8.0 ] ] ] ); +* // throws { '_mode': 'throw', '_submode': [ 'throw' ], '_byteLength': 64, '_bytesPerElement': 8, '_buffer': [ 1, 2, ..., 7, 8 ], '_dtype': 'float64', '_length': 8, '_ndims': 3, '_offset': 0, '_order': 'row-major', '_shape': [ 2, 2, 2 ], '_strides': [ 4, 2, 1 ], '_accessors': false, '_iterationOrder': 1, '_flags': { 'ROW_MAJOR_CONTIGUOUS': true, 'COLUMN_MAJOR_CONTIGUOUS': false, 'READONLY': false }, '__meta_dataview__': null } +* +* // Perform reduction: +* var out = findLast( x, isEven ); +* // throws +* +* var v = out.get(); +* // throws +*/ + +// 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; diff --git a/lib/node_modules/@stdlib/ndarray/find-last/lib/main.js b/lib/node_modules/@stdlib/ndarray/find-last/lib/main.js new file mode 100644 index 000000000000..1790ca6e3a24 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/find-last/lib/main.js @@ -0,0 +1,131 @@ +/** +* @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 isObject = require( '@stdlib/assert/is-plain-object' ); +var isFunction = require( '@stdlib/assert/is-function' ); +var isndarrayLike = require( '@stdlib/assert/is-ndarray-like' ); +var reverse = require( '@stdlib/ndarray/base/reverse' ); +var base = require( '@stdlib/ndarray/find' ); +var format = require( '@stdlib/string/format' ); +var wrap = require( './callback_wrapper.js' ); + + +// MAIN // + +/** +* Returns a new ndarray containing the last elements which pass a test implemented by a predicate function along one or more ndarray dimensions. +* +* @param {ndarray} x - input ndarray +* @param {Options} [options] - function options +* @param {IntegerArray} [options.dims] - list of dimensions over which to perform a reduction +* @param {boolean} [options.keepdims=false] - boolean indicating whether the reduced dimensions should be included in the returned ndarray as singleton dimensions +* @param {(*|ndarray)} [options.sentinelValue] - sentinel value +* @param {Function} predicate - predicate function +* @param {*} [thisArg] - predicate function execution context +* @throws {TypeError} first argument must be an ndarray-like object +* @throws {TypeError} options argument must be an object +* @throws {TypeError} predicate argument must be a function +* @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 isEven = require( '@stdlib/assert/is-even' ).isPrimitive; +* var array = require( '@stdlib/ndarray/array' ); +* +* // Create an input ndarray: +* var x = array( [ [ [ 1.0, 2.0 ], [ 3.0, 4.0 ] ], [ [ 5.0, 6.0 ], [ 7.0, 8.0 ] ] ] ); +* // returns +* +* // Perform reduction: +* var out = findLast( x, isEven ); +* // returns +* +* var v = out.get(); +* // returns 8.0 +*/ +function findLast( x, options, predicate, thisArg ) { + var nargs; + var flg; + var ctx; + var cb; + var o; + var f; + var y; + + nargs = arguments.length; + if ( !isndarrayLike( x ) ) { + throw new TypeError( format( 'invalid argument. First argument must be an ndarray-like object. Value: `%s`.', x ) ); + } + // Case: findLast( x, predicate ) + if ( nargs < 3 ) { + if ( !isFunction( options ) ) { + throw new TypeError( format( 'invalid argument. Callback argument must be a function. Value: `%s`.', options ) ); + } + cb = options; + } + // Case: findLast( x, options, predicate, thisArg ) + else if ( nargs > 3 ) { + flg = true; + o = options; + cb = predicate; + if ( !isFunction( cb ) ) { + throw new TypeError( format( 'invalid argument. Callback argument must be a function. Value: `%s`.', cb ) ); + } + ctx = thisArg; + } + // Case: findLast( x, predicate, thisArg ) + else if ( isFunction( options ) ) { + cb = options; + ctx = predicate; + } + // Case: findLast( x, options, predicate ) + else if ( isFunction( predicate ) ) { + flg = true; + o = options; + cb = predicate; + } + // Case: find( x, ???, ??? ) + else { + throw new TypeError( format( 'invalid argument. Callback argument must be a function. Value: `%s`.', predicate ) ); + } + + // Wrap the callback function... + f = wrap( x, cb, ctx ); + + // Create a reversed view of the input ndarray... + y = reverse( x, false ); + + if ( flg ) { + if ( !isObject( o ) ) { + throw new TypeError( format( 'invalid argument. Options argument must be an object. Value: `%s`.', o ) ); + } + return base( y, o, f ); + } + return base( y, f ); +} + + +// EXPORTS // + +module.exports = findLast; diff --git a/lib/node_modules/@stdlib/ndarray/find-last/package.json b/lib/node_modules/@stdlib/ndarray/find-last/package.json new file mode 100644 index 000000000000..dd6d100ae628 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/find-last/package.json @@ -0,0 +1,65 @@ +{ + "name": "@stdlib/ndarray/find-last", + "version": "0.0.0", + "description": "Return a new ndarray containing the last elements which pass a test implemented by a predicate function along one or more ndarray dimensions.", + "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", + "strided", + "array", + "ndarray", + "find", + "last", + "findlast", + "filter", + "reversefilter", + "reduce", + "reduction" + ], + "__stdlib__": {} +} diff --git a/lib/node_modules/@stdlib/ndarray/find-last/test/test.assign.js b/lib/node_modules/@stdlib/ndarray/find-last/test/test.assign.js new file mode 100644 index 000000000000..cbd7c3422a40 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/find-last/test/test.assign.js @@ -0,0 +1,825 @@ +/** +* @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 Float64Array = require( '@stdlib/array/float64' ); +var ndarray = require( '@stdlib/ndarray/ctor' ); +var empty = require( '@stdlib/ndarray/empty' ); +var ndarray2array = require( '@stdlib/ndarray/to-array' ); +var assign = require( './../lib/assign.js' ); + + +// FUNCTIONS // + +/** +* Callback function. +* +* @private +* @param {number} value - input value +* @returns {boolean} result +*/ +function clbk( value ) { + return value > 0.0; +} + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof assign, '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 y; + var i; + + y = empty( [], { + 'dtype': 'float64' + }); + + 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() { + assign( 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 y; + var i; + + y = empty( [], { + 'dtype': 'float64' + }); + + 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() { + assign( 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 y; + var i; + + y = empty( [], { + 'dtype': 'float64' + }); + + 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() { + assign( 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 y; + var i; + + y = empty( [], { + 'dtype': 'float64' + }); + + 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() { + assign( 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 x; + var i; + + x = empty( [ 2, 2 ], { + 'dtype': 'float64' + }); + + 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() { + assign( x, value, clbk ); + }; + } +}); + +tape( 'the function throws an error if provided a second argument which is not an ndarray-like object (options)', function test( t ) { + var values; + var x; + var i; + + x = empty( [ 2, 2 ], { + 'dtype': 'float64' + }); + + 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() { + assign( x, value, {}, clbk ); + }; + } +}); + +tape( 'the function throws an error if provided a second argument which is not an ndarray-like object (thisArg)', function test( t ) { + var values; + var x; + var i; + + x = empty( [ 2, 2 ], { + 'dtype': 'float64' + }); + + 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() { + assign( x, value, clbk, {} ); + }; + } +}); + +tape( 'the function throws an error if provided a second argument which is not an ndarray-like object (options, thisArg)', function test( t ) { + var values; + var x; + var i; + + x = empty( [ 2, 2 ], { + 'dtype': 'float64' + }); + + 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() { + assign( x, value, {}, clbk, {} ); + }; + } +}); + +tape( 'the function throws an error if provided a callback argument which is not a function', function test( t ) { + var values; + var x; + var y; + var i; + + x = empty( [ 2, 2 ], { + 'dtype': 'float64' + }); + y = empty( [], { + 'dtype': 'float64' + }); + + values = [ + '5', + 5, + NaN, + true, + false, + null, + void 0, + {}, + [] + ]; + + 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() { + assign( x, y, value ); + }; + } +}); + +tape( 'the function throws an error if provided a callback argument which is not a function (options)', function test( t ) { + var values; + var x; + var y; + var i; + + x = empty( [ 2, 2 ], { + 'dtype': 'float64' + }); + y = empty( [ 2 ], { + 'dtype': 'bool' + }); + + values = [ + '5', + 5, + NaN, + true, + false, + null, + void 0, + {}, + [] + ]; + + 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() { + assign( x, y, {}, value ); + }; + } +}); + +tape( 'the function throws an error if provided a callback argument which is not a function (thisArg)', function test( t ) { + var values; + var x; + var y; + var i; + + x = empty( [ 2, 2 ], { + 'dtype': 'float64' + }); + y = empty( [ 2 ], { + 'dtype': 'bool' + }); + + values = [ + '5', + 5, + NaN, + true, + false, + null, + void 0, + {}, + [] + ]; + + 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() { + assign( x, y, value, {} ); + }; + } +}); + +tape( 'the function throws an error if provided a callback argument which is not a function (options, thisArg)', function test( t ) { + var values; + var x; + var y; + var i; + + x = empty( [ 2, 2 ], { + 'dtype': 'float64' + }); + y = empty( [ 2 ], { + 'dtype': 'bool' + }); + + values = [ + '5', + 5, + NaN, + true, + false, + null, + void 0, + {}, + [] + ]; + + 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() { + assign( x, y, {}, value, {} ); + }; + } +}); + +tape( 'the function throws an error if provided an options argument which is not an object', function test( t ) { + var values; + var x; + var y; + var i; + + x = empty( [ 2, 2 ], { + 'dtype': 'float64' + }); + y = empty( [ 2 ], { + 'dtype': 'bool' + }); + + values = [ + '5', + 5, + NaN, + true, + false, + null, + void 0, + [] + ]; + + 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() { + assign( 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 x; + var y; + var i; + + x = empty( [ 2, 2 ], { + 'dtype': 'float64' + }); + y = empty( [ 2 ], { + 'dtype': 'bool' + }); + + values = [ + '5', + 5, + NaN, + true, + false, + null, + void 0, + [] + ]; + + 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() { + assign( x, y, value, clbk, {} ); + }; + } +}); + +tape( 'the function finds the last elements which pass a test implemented by a predicate function along one or more ndarray dimensions (row-major)', function test( t ) { + var expected; + var actual; + var x; + var y; + + x = new ndarray( 'float64', new Float64Array( [ 1.0, -2.0, 3.0, -4.0 ] ), [ 4 ], [ 1 ], 0, 'row-major' ); + y = empty( [], { + 'dtype': 'float64' + }); + + actual = assign( x, y, clbk ); + expected = 3.0; + + t.strictEqual( actual, y, 'returns expected value' ); + t.strictEqual( actual.get(), expected, 'returns expected value' ); + + x = new ndarray( 'float64', new Float64Array( [ 1.0, -2.0, 3.0, -4.0 ] ), [ 4 ], [ 1 ], 0, 'row-major' ); + y = empty( [], { + 'dtype': 'float64' + }); + + actual = assign( x, y, clbk ); + expected = 3.0; + + t.strictEqual( actual, y, 'returns expected value' ); + t.strictEqual( actual.get(), expected, 'returns expected value' ); + t.end(); +}); + +tape( 'the function finds the last elements which pass a test implemented by a predicate function along one or more ndarray dimensions (column-major)', function test( t ) { + var expected; + var actual; + var x; + var y; + + x = new ndarray( 'float64', new Float64Array( [ 1.0, -2.0, 3.0, -4.0 ] ), [ 4 ], [ 1 ], 0, 'column-major' ); + y = empty( [], { + 'dtype': 'float64' + }); + + actual = assign( x, y, clbk ); + expected = 3.0; + + t.strictEqual( actual, y, 'returns expected value' ); + t.strictEqual( actual.get(), expected, 'returns expected value' ); + + x = new ndarray( 'float64', new Float64Array( [ 1.0, -2.0, 3.0, -4.0 ] ), [ 4 ], [ 1 ], 0, 'column-major' ); + y = empty( [], { + 'dtype': 'float64' + }); + + actual = assign( x, y, clbk ); + expected = 3.0; + + t.strictEqual( actual, y, 'returns expected value' ); + t.strictEqual( actual.get(), expected, 'returns expected value' ); + t.end(); +}); + +tape( 'the function supports specifying reduction dimensions (row-major)', function test( t ) { + var expected; + var actual; + var opts; + var x; + var y; + + x = new ndarray( 'float64', new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 ] ), [ 2, 4 ], [ 4, 1 ], 0, 'row-major' ); + + opts = { + 'dims': [ 0 ] + }; + y = empty( [ 4 ], { + 'dtype': 'float64' + }); + actual = assign( x, y, opts, clbk ); + expected = [ 8.0, 7.0, 6.0, 5.0 ]; + t.strictEqual( actual, y, 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + opts = { + 'dims': [ 1 ] + }; + y = empty( [ 2 ], { + 'dtype': 'float64' + }); + actual = assign( x, y, opts, clbk ); + expected = [ 8.0, 4.0 ]; + t.strictEqual( actual, y, 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + opts = { + 'dims': [ 0, 1 ] + }; + y = empty( [], { + 'dtype': 'float64' + }); + actual = assign( x, y, opts, clbk ); + expected = 8.0; + t.strictEqual( actual, y, 'returns expected value' ); + t.deepEqual( actual.get(), expected, 'returns expected value' ); + + opts = { + 'dims': [] + }; + y = empty( [ 2, 4 ], { + 'dtype': 'float64' + }); + actual = assign( x, y, opts, clbk ); + expected = [ [ 8.0, 7.0, 6.0, 5.0 ], [ 4.0, 3.0, 2.0, 1.0 ] ]; + t.strictEqual( actual, y, 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports specifying reduction dimensions (column-major)', function test( t ) { + var expected; + var actual; + var opts; + var x; + var y; + + x = new ndarray( 'float64', new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 ] ), [ 2, 4 ], [ 1, 2 ], 0, 'column-major' ); + + opts = { + 'dims': [ 0 ] + }; + y = empty( [ 4 ], { + 'dtype': 'float64' + }); + actual = assign( x, y, opts, clbk ); + expected = [ 8.0, 6.0, 4.0, 2.0 ]; + + t.strictEqual( actual, y, 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + opts = { + 'dims': [ 1 ] + }; + y = empty( [ 2 ], { + 'dtype': 'float64' + }); + actual = assign( x, y, opts, clbk ); + expected = [ 8.0, 7.0 ]; + + t.strictEqual( actual, y, 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + opts = { + 'dims': [ 0, 1 ] + }; + y = empty( [], { + 'dtype': 'float64' + }); + actual = assign( x, y, opts, clbk ); + expected = 8.0; + + t.strictEqual( actual, y, 'returns expected value' ); + t.deepEqual( actual.get(), expected, 'returns expected value' ); + + opts = { + 'dims': [] + }; + y = empty( [ 2, 4 ], { + 'dtype': 'float64' + }); + actual = assign( x, y, opts, clbk ); + expected = [ [ 8.0, 6.0, 4.0, 2.0 ], [ 7.0, 5.0, 3.0, 1.0 ] ]; + + t.strictEqual( actual, y, 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports providing an execution context', function test( t ) { + var expected; + var indices; + var values; + var arrays; + var actual; + var ctx; + var x; + var y; + + x = new ndarray( 'float64', new Float64Array( [ 1.0, 2.0, 3.0, 4.0 ] ), [ 4 ], [ 1 ], 0, 'row-major' ); + y = empty( [], { + 'dtype': 'float64' + }); + + ctx = { + 'count': 0 + }; + + indices = []; + values = []; + arrays = []; + actual = assign( x, y, predicate, ctx ); + + expected = 4.0; + t.strictEqual( actual, y, 'returns expected value' ); + t.strictEqual( actual.get(), expected, 'returns expected value' ); + t.strictEqual( ctx.count, 1, 'returns expected value' ); + + expected = [ 4.0 ]; + t.deepEqual( values, expected, 'returns expected value' ); + + expected = [ + [ 0 ] + ]; + t.deepEqual( indices, expected, 'returns expected value' ); + + expected = [ x ]; + t.deepEqual( arrays, expected, 'returns expected value' ); + + t.end(); + + function predicate( value, idx, arr ) { + this.count += 1; // eslint-disable-line no-invalid-this + values.push( value ); + indices.push( idx ); + arrays.push( arr ); + return value > 0.0; + } +}); + +tape( 'the function supports providing an execution context (options)', function test( t ) { + var expected; + var indices; + var values; + var arrays; + var actual; + var opts; + var ctx; + var x; + var y; + + x = new ndarray( 'float64', new Float64Array( [ 1.0, 2.0, 3.0, 4.0 ] ), [ 2, 2 ], [ 1, 2 ], 0, 'column-major' ); + y = empty( [ 2 ], { + 'dtype': 'float64' + }); + + ctx = { + 'count': 0 + }; + opts = { + 'dims': [ 0 ] + }; + indices = []; + values = []; + arrays = []; + actual = assign( x, y, opts, predicate, ctx ); + + expected = [ 4.0, 2.0 ]; + t.strictEqual( actual, y, 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + t.strictEqual( ctx.count, 2, 'returns expected value' ); + + expected = [ 4.0, 2.0 ]; + t.deepEqual( values, expected, 'returns expected value' ); + + expected = [ + [ 0, 0 ], + [ 0, 1 ] + ]; + t.deepEqual( indices, expected, 'returns expected value' ); + + expected = [ x, x ]; + t.deepEqual( arrays, expected, 'returns expected value' ); + + t.end(); + + function predicate( value, idx, arr ) { + this.count += 1; // eslint-disable-line no-invalid-this + values.push( value ); + indices.push( idx ); + arrays.push( arr ); + return value > 0.0; + } +}); diff --git a/lib/node_modules/@stdlib/ndarray/find-last/test/test.js b/lib/node_modules/@stdlib/ndarray/find-last/test/test.js new file mode 100644 index 000000000000..85e2fed10409 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/find-last/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 findLast = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof findLast, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'attached to the main export is an `assign` method', function test( t ) { + t.strictEqual( isMethod( findLast, 'assign' ), true, 'returns expected value' ); + t.end(); +}); diff --git a/lib/node_modules/@stdlib/ndarray/find-last/test/test.main.js b/lib/node_modules/@stdlib/ndarray/find-last/test/test.main.js new file mode 100644 index 000000000000..dc05f681d1b8 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/find-last/test/test.main.js @@ -0,0 +1,667 @@ +/** +* @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 Float64Array = require( '@stdlib/array/float64' ); +var ndarray = require( '@stdlib/ndarray/ctor' ); +var empty = require( '@stdlib/ndarray/empty' ); +var isnan = require( '@stdlib/assert/is-nan' ); +var ndarray2array = require( '@stdlib/ndarray/to-array' ); +var findLast = require( './../lib' ); + + +// FUNCTIONS // + +/** +* Callback function. +* +* @private +* @param {number} value - input value +* @returns {boolean} result +*/ +function clbk( value ) { + return value > 0.0; +} + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof findLast, '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() { + findLast( 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() { + findLast( 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() { + findLast( 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() { + findLast( value, {}, clbk, {} ); + }; + } +}); + +tape( 'the function throws an error if provided a callback argument which is not a function', function test( t ) { + var values; + var x; + var i; + + x = empty( [ 2, 2 ], { + 'dtype': 'float64' + }); + + values = [ + '5', + 5, + NaN, + true, + false, + null, + void 0, + {}, + [] + ]; + + 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() { + findLast( x, value ); + }; + } +}); + +tape( 'the function throws an error if provided a callback argument which is not a function (options)', function test( t ) { + var values; + var x; + var i; + + x = empty( [ 2, 2 ], { + 'dtype': 'float64' + }); + + values = [ + '5', + 5, + NaN, + true, + false, + null, + void 0, + {}, + [] + ]; + + 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() { + findLast( x, {}, value ); + }; + } +}); + +tape( 'the function throws an error if provided a callback argument which is not a function (thisArg)', function test( t ) { + var values; + var x; + var i; + + x = empty( [ 2, 2 ], { + 'dtype': 'float64' + }); + + values = [ + '5', + 5, + NaN, + true, + false, + null, + void 0, + {}, + [] + ]; + + 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() { + findLast( x, value, {} ); + }; + } +}); + +tape( 'the function throws an error if provided a callback argument which is not a function (options, thisArg)', function test( t ) { + var values; + var x; + var i; + + x = empty( [ 2, 2 ], { + 'dtype': 'float64' + }); + + values = [ + '5', + 5, + NaN, + true, + false, + null, + void 0, + {}, + [] + ]; + + 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() { + findLast( x, {}, value, {} ); + }; + } +}); + +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 = empty( [ 2, 2 ], { + 'dtype': 'float64' + }); + + values = [ + '5', + 5, + NaN, + true, + false, + null, + void 0, + [] + ]; + + 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() { + findLast( 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 = empty( [ 2, 2 ], { + 'dtype': 'float64' + }); + + values = [ + '5', + 5, + NaN, + true, + false, + null, + void 0, + [] + ]; + + 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() { + findLast( x, value, clbk, {} ); + }; + } +}); + +tape( 'the function finds the last elements which pass a test implemented by a predicate function along one or more ndarray dimensions (row-major)', function test( t ) { + var expected; + var actual; + var x; + + x = new ndarray( 'float64', new Float64Array( [ 1.0, -2.0, 3.0, -4.0 ] ), [ 4 ], [ 1 ], 0, 'row-major' ); + + actual = findLast( x, clbk ); + expected = 3.0; + + t.strictEqual( isndarrayLike( actual ), true, 'returns expected value' ); + t.strictEqual( actual.get(), expected, 'returns expected value' ); + + x = new ndarray( 'float64', new Float64Array( [ -1.0, -2.0, -3.0, -4.0 ] ), [ 4 ], [ 1 ], 0, 'row-major' ); + + actual = findLast( x, clbk ); + + t.strictEqual( isndarrayLike( actual ), true, 'returns expected value' ); + t.strictEqual( isnan( actual.get() ), true, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function finds the last elements which pass a test implemented by a predicate function along one or more ndarray dimensions (column-major)', function test( t ) { + var expected; + var actual; + var x; + + x = new ndarray( 'float64', new Float64Array( [ 1.0, -2.0, 3.0, -4.0 ] ), [ 4 ], [ 1 ], 0, 'column-major' ); + + actual = findLast( x, clbk ); + expected = 3.0; + + t.strictEqual( isndarrayLike( actual ), true, 'returns expected value' ); + t.strictEqual( actual.get(), expected, 'returns expected value' ); + + x = new ndarray( 'float64', new Float64Array( [ -1.0, -2.0, -3.0, -4.0 ] ), [ 4 ], [ 1 ], 0, 'column-major' ); + + actual = findLast( x, clbk ); + + t.strictEqual( isndarrayLike( actual ), true, 'returns expected value' ); + t.strictEqual( isnan( actual.get() ), true, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports specifying reduction dimensions (row-major)', function test( t ) { + var expected; + var actual; + var opts; + var x; + + x = new ndarray( 'float64', new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 ] ), [ 2, 4 ], [ 4, 1 ], 0, 'row-major' ); + + opts = { + 'dims': [ 0 ] + }; + actual = findLast( x, opts, clbk ); + expected = [ 8.0, 7.0, 6.0, 5.0 ]; + t.strictEqual( isndarrayLike( actual ), true, 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + opts = { + 'dims': [ 0 ], + 'keepdims': true + }; + actual = findLast( x, opts, clbk ); + expected = [ [ 8.0, 7.0, 6.0, 5.0 ] ]; + t.strictEqual( isndarrayLike( actual ), true, 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + opts = { + 'dims': [ 1 ] + }; + actual = findLast( x, opts, clbk ); + expected = [ 8.0, 4.0 ]; + t.strictEqual( isndarrayLike( actual ), true, 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + opts = { + 'dims': [ 1 ], + 'keepdims': true + }; + actual = findLast( x, opts, clbk ); + expected = [ [ 8.0 ], [ 4.0 ] ]; + t.strictEqual( isndarrayLike( actual ), true, 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + opts = { + 'dims': [ 0, 1 ] + }; + actual = findLast( x, opts, clbk ); + expected = 8.0; + t.strictEqual( isndarrayLike( actual ), true, 'returns expected value' ); + t.deepEqual( actual.get(), expected, 'returns expected value' ); + + opts = { + 'dims': [ 0, 1 ], + 'keepdims': true + }; + actual = findLast( x, opts, clbk ); + expected = [ [ 8.0 ] ]; + t.strictEqual( isndarrayLike( actual ), true, 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + opts = { + 'dims': [] + }; + actual = findLast( x, opts, clbk ); + expected = [ [ 8.0, 7.0, 6.0, 5.0 ], [ 4.0, 3.0, 2.0, 1.0 ] ]; + t.strictEqual( isndarrayLike( actual ), true, 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + opts = { + 'dims': [], + 'keepdims': true + }; + actual = findLast( x, opts, clbk ); + expected = [ [ 8.0, 7.0, 6.0, 5.0 ], [ 4.0, 3.0, 2.0, 1.0 ] ]; + t.strictEqual( isndarrayLike( actual ), true, 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports specifying reduction dimensions (column-major)', function test( t ) { + var expected; + var actual; + var opts; + var x; + + x = new ndarray( 'float64', new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 ] ), [ 2, 4 ], [ 1, 2 ], 0, 'column-major' ); + + opts = { + 'dims': [ 0 ] + }; + actual = findLast( x, opts, clbk ); + expected = [ 8.0, 6.0, 4.0, 2.0 ]; + t.strictEqual( isndarrayLike( actual ), true, 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + opts = { + 'dims': [ 0 ], + 'keepdims': true + }; + actual = findLast( x, opts, clbk ); + expected = [ [ 8.0, 6.0, 4.0, 2.0 ] ]; + t.strictEqual( isndarrayLike( actual ), true, 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + opts = { + 'dims': [ 1 ] + }; + actual = findLast( x, opts, clbk ); + expected = [ 8.0, 7.0 ]; + t.strictEqual( isndarrayLike( actual ), true, 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + opts = { + 'dims': [ 1 ], + 'keepdims': true + }; + actual = findLast( x, opts, clbk ); + expected = [ [ 8.0 ], [ 7.0 ] ]; + t.strictEqual( isndarrayLike( actual ), true, 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + opts = { + 'dims': [ 0, 1 ] + }; + actual = findLast( x, opts, clbk ); + expected = 8.0; + t.strictEqual( isndarrayLike( actual ), true, 'returns expected value' ); + t.deepEqual( actual.get(), expected, 'returns expected value' ); + + opts = { + 'dims': [ 0, 1 ], + 'keepdims': true + }; + actual = findLast( x, opts, clbk ); + expected = [ [ 8.0 ] ]; + t.strictEqual( isndarrayLike( actual ), true, 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + opts = { + 'dims': [] + }; + actual = findLast( x, opts, clbk ); + expected = [ [ 8.0, 6.0, 4.0, 2.0 ], [ 7.0, 5.0, 3.0, 1.0 ] ]; + t.strictEqual( isndarrayLike( actual ), true, 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + opts = { + 'dims': [], + 'keepdims': true + }; + actual = findLast( x, opts, clbk ); + expected = [ [ 8.0, 6.0, 4.0, 2.0 ], [ 7.0, 5.0, 3.0, 1.0 ] ]; + t.strictEqual( isndarrayLike( actual ), true, 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports providing an execution context', function test( t ) { + var expected; + var indices; + var values; + var arrays; + var actual; + var ctx; + var x; + + x = new ndarray( 'float64', new Float64Array( [ 1.0, 2.0, 3.0, 4.0 ] ), [ 4 ], [ 1 ], 0, 'row-major' ); + + ctx = { + 'count': 0 + }; + + indices = []; + values = []; + arrays = []; + actual = findLast( x, predicate, ctx ); + expected = 4.0; + + t.strictEqual( isndarrayLike( actual ), true, 'returns expected value' ); + t.strictEqual( actual.get(), expected, 'returns expected value' ); + t.strictEqual( ctx.count, 1, 'returns expected value' ); + + expected = [ 4.0 ]; + t.deepEqual( values, expected, 'returns expected value' ); + + expected = [ + [ 0 ] + ]; + t.deepEqual( indices, expected, 'returns expected value' ); + + expected = [ x ]; + t.deepEqual( arrays, expected, 'returns expected value' ); + + t.end(); + + function predicate( value, idx, arr ) { + this.count += 1; // eslint-disable-line no-invalid-this + values.push( value ); + indices.push( idx ); + arrays.push( arr ); + return value > 0.0; + } +}); + +tape( 'the function supports providing an execution context (options)', function test( t ) { + var expected; + var indices; + var values; + var arrays; + var actual; + var opts; + var ctx; + var x; + + x = new ndarray( 'float64', new Float64Array( [ 1.0, 2.0, 3.0, 4.0 ] ), [ 2, 2 ], [ 1, 2 ], 0, 'column-major' ); + + ctx = { + 'count': 0 + }; + opts = { + 'dims': [ 0 ] + }; + indices = []; + values = []; + arrays = []; + actual = findLast( x, opts, predicate, ctx ); + expected = [ 4.0, 2.0 ]; + + t.strictEqual( isndarrayLike( actual ), true, 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + t.strictEqual( ctx.count, 2, 'returns expected value' ); + + expected = [ 4.0, 2.0 ]; + t.deepEqual( values, expected, 'returns expected value' ); + + expected = [ + [ 0, 0 ], + [ 0, 1 ] + ]; + t.deepEqual( indices, expected, 'returns expected value' ); + + expected = [ x, x ]; + t.deepEqual( arrays, expected, 'returns expected value' ); + + t.end(); + + function predicate( value, idx, arr ) { + this.count += 1; // eslint-disable-line no-invalid-this + values.push( value ); + indices.push( idx ); + arrays.push( arr ); + return value > 0.0; + } +});