diff --git a/lib/node_modules/@stdlib/blas/ext/base/ndarray/glast-index-of-truthy/README.md b/lib/node_modules/@stdlib/blas/ext/base/ndarray/glast-index-of-truthy/README.md index 1f0ff533903b..17a189074569 100644 --- a/lib/node_modules/@stdlib/blas/ext/base/ndarray/glast-index-of-truthy/README.md +++ b/lib/node_modules/@stdlib/blas/ext/base/ndarray/glast-index-of-truthy/README.md @@ -41,26 +41,39 @@ var glastIndexOfTruthy = require( '@stdlib/blas/ext/base/ndarray/glast-index-of- Returns the index of the last truthy element in a one-dimensional ndarray. ```javascript +var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' ); var vector = require( '@stdlib/ndarray/vector/ctor' ); var x = vector( [ 0.0, 3.0, 0.0, 2.0 ], 'generic' ); -var idx = glastIndexOfTruthy( [ x ] ); +var fromIndex = scalar2ndarray( 3, { + 'dtype': 'generic' +}); + +var idx = glastIndexOfTruthy( [ x, fromIndex ] ); // returns 3 ``` The function has the following parameters: -- **arrays**: array-like object containing a one-dimensional input ndarray. +- **arrays**: array-like object containing the following ndarrays: + + - a one-dimensional input ndarray. + - a zero-dimensional ndarray containing the index from which to begin searching. If the function is unable to find a truthy element, the function returns `-1`. ```javascript +var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' ); var vector = require( '@stdlib/ndarray/vector/ctor' ); var x = vector( [ 0.0, 0.0, 0.0, 0.0 ], 'generic' ); -var idx = glastIndexOfTruthy( [ x ] ); +var fromIndex = scalar2ndarray( 3, { + 'dtype': 'generic' +}); + +var idx = glastIndexOfTruthy( [ x, fromIndex ] ); // returns -1 ``` @@ -73,6 +86,7 @@ var idx = glastIndexOfTruthy( [ x ] ); ## Notes - The function treats `NaN` values as falsy. +- If a specified starting search index is negative, the function resolves the starting search index by counting backward from the last element (where `-1` refers to the last element). @@ -86,6 +100,7 @@ var idx = glastIndexOfTruthy( [ x ] ); ```javascript var discreteUniform = require( '@stdlib/random/discrete-uniform' ); +var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' ); var ndarray2array = require( '@stdlib/ndarray/to-array' ); var glastIndexOfTruthy = require( '@stdlib/blas/ext/base/ndarray/glast-index-of-truthy' ); @@ -96,7 +111,11 @@ var opts = { var x = discreteUniform( [ 10 ], 0, 5, opts ); console.log( ndarray2array( x ) ); -var idx = glastIndexOfTruthy( [ x ] ); +var fromIndex = scalar2ndarray( 9, { + 'dtype': 'generic' +}); + +var idx = glastIndexOfTruthy( [ x, fromIndex ] ); console.log( idx ); ``` diff --git a/lib/node_modules/@stdlib/blas/ext/base/ndarray/glast-index-of-truthy/benchmark/benchmark.js b/lib/node_modules/@stdlib/blas/ext/base/ndarray/glast-index-of-truthy/benchmark/benchmark.js index 8b983c2a36d1..7b26f4aa14d0 100644 --- a/lib/node_modules/@stdlib/blas/ext/base/ndarray/glast-index-of-truthy/benchmark/benchmark.js +++ b/lib/node_modules/@stdlib/blas/ext/base/ndarray/glast-index-of-truthy/benchmark/benchmark.js @@ -21,9 +21,10 @@ // MODULES // var bench = require( '@stdlib/bench' ); -var zeros = require( '@stdlib/ndarray/zeros' ); var isInteger = require( '@stdlib/assert/is-integer' ).isPrimitive; var pow = require( '@stdlib/math/base/special/pow' ); +var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' ); +var zeros = require( '@stdlib/ndarray/zeros' ); var format = require( '@stdlib/string/format' ); var pkg = require( './../package.json' ).name; var glastIndexOfTruthy = require( './../lib' ); @@ -46,7 +47,13 @@ var options = { * @returns {Function} benchmark function */ function createBenchmark( len ) { - var x = zeros( [ len ], options ); + var fromIndex; + var x; + + x = zeros( [ len ], options ); + fromIndex = scalar2ndarray( len - 1, { + 'dtype': 'generic' + }); return benchmark; /** @@ -61,7 +68,7 @@ function createBenchmark( len ) { b.tic(); for ( i = 0; i < b.iterations; i++ ) { - out = glastIndexOfTruthy( [ x ] ); + out = glastIndexOfTruthy( [ x, fromIndex ] ); if ( out !== out ) { b.fail( 'should return an integer' ); } diff --git a/lib/node_modules/@stdlib/blas/ext/base/ndarray/glast-index-of-truthy/docs/repl.txt b/lib/node_modules/@stdlib/blas/ext/base/ndarray/glast-index-of-truthy/docs/repl.txt index a49f0ce2f105..a52ca95b6fc9 100644 --- a/lib/node_modules/@stdlib/blas/ext/base/ndarray/glast-index-of-truthy/docs/repl.txt +++ b/lib/node_modules/@stdlib/blas/ext/base/ndarray/glast-index-of-truthy/docs/repl.txt @@ -2,6 +2,10 @@ {{alias}}( arrays ) Returns the index of the last truthy element in a one-dimensional ndarray. + If a specified starting search index is negative, the function resolves the + starting search index by counting backward from the last element (where `-1` + refers to the last element). + If unable to find a truthy element, the function returns `-1`. Parameters @@ -10,6 +14,8 @@ Array-like object containing the following ndarrays: - a one-dimensional input ndarray. + - a zero-dimensional ndarray containing the index from which to begin + searching. Returns ------- @@ -19,7 +25,8 @@ Examples -------- > var x = {{alias:@stdlib/ndarray/vector/ctor}}( [ 0.0, 2.0, 0.0 ], 'generic' ); - > {{alias}}( [ x ] ) + > var i = {{alias:@stdlib/ndarray/from-scalar}}( 2, { 'dtype': 'generic' } ); + > {{alias}}( [ x, i ] ) 1 See Also diff --git a/lib/node_modules/@stdlib/blas/ext/base/ndarray/glast-index-of-truthy/docs/types/index.d.ts b/lib/node_modules/@stdlib/blas/ext/base/ndarray/glast-index-of-truthy/docs/types/index.d.ts index 4101c60ae14a..b6345e403bdd 100644 --- a/lib/node_modules/@stdlib/blas/ext/base/ndarray/glast-index-of-truthy/docs/types/index.d.ts +++ b/lib/node_modules/@stdlib/blas/ext/base/ndarray/glast-index-of-truthy/docs/types/index.d.ts @@ -30,19 +30,25 @@ import { typedndarray } from '@stdlib/types/ndarray'; * - The function expects the following ndarrays: * * - a one-dimensional input ndarray. +* - a zero-dimensional ndarray containing the index from which to begin searching. * * @param arrays - array-like object containing ndarrays * @returns index * * @example +* var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' ); * var vector = require( '@stdlib/ndarray/vector/ctor' ); * * var x = vector( [ 0.0, 3.0, 0.0, 2.0 ], 'generic' ); * -* var v = glastIndexOfTruthy( [ x ] ); +* var fromIndex = scalar2ndarray( 3, { +* 'dtype': 'generic' +* }); +* +* var v = glastIndexOfTruthy( [ x, fromIndex ] ); * // returns 3 */ -declare function glastIndexOfTruthy( arrays: [ typedndarray ] ): number; +declare function glastIndexOfTruthy( arrays: [ typedndarray, typedndarray ] ): number; // EXPORTS // diff --git a/lib/node_modules/@stdlib/blas/ext/base/ndarray/glast-index-of-truthy/docs/types/test.ts b/lib/node_modules/@stdlib/blas/ext/base/ndarray/glast-index-of-truthy/docs/types/test.ts index cc234ffb6817..57026cf79079 100644 --- a/lib/node_modules/@stdlib/blas/ext/base/ndarray/glast-index-of-truthy/docs/types/test.ts +++ b/lib/node_modules/@stdlib/blas/ext/base/ndarray/glast-index-of-truthy/docs/types/test.ts @@ -19,6 +19,7 @@ /* eslint-disable space-in-parens */ import zeros = require( '@stdlib/ndarray/zeros' ); +import scalar2ndarray = require( '@stdlib/ndarray/from-scalar' ); import glastIndexOfTruthy = require( './index' ); @@ -29,8 +30,11 @@ import glastIndexOfTruthy = require( './index' ); const x = zeros( [ 10 ], { 'dtype': 'generic' }); + const fromIndex = scalar2ndarray( 0, { + 'dtype': 'generic' + }); - glastIndexOfTruthy( [ x ] ); // $ExpectType number + glastIndexOfTruthy( [ x, fromIndex ] ); // $ExpectType number } // The compiler throws an error if the function is provided a first argument which is not an array of ndarrays... @@ -51,7 +55,10 @@ import glastIndexOfTruthy = require( './index' ); const x = zeros( [ 10 ], { 'dtype': 'generic' }); + const fromIndex = scalar2ndarray( 0, { + 'dtype': 'generic' + }); glastIndexOfTruthy(); // $ExpectError - glastIndexOfTruthy( [ x ], {} ); // $ExpectError + glastIndexOfTruthy( [ x, fromIndex ], {} ); // $ExpectError } diff --git a/lib/node_modules/@stdlib/blas/ext/base/ndarray/glast-index-of-truthy/examples/index.js b/lib/node_modules/@stdlib/blas/ext/base/ndarray/glast-index-of-truthy/examples/index.js index e7bda77b24ef..54e223e143dd 100644 --- a/lib/node_modules/@stdlib/blas/ext/base/ndarray/glast-index-of-truthy/examples/index.js +++ b/lib/node_modules/@stdlib/blas/ext/base/ndarray/glast-index-of-truthy/examples/index.js @@ -19,6 +19,7 @@ 'use strict'; var discreteUniform = require( '@stdlib/random/discrete-uniform' ); +var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' ); var ndarray2array = require( '@stdlib/ndarray/to-array' ); var glastIndexOfTruthy = require( './../lib' ); @@ -29,5 +30,9 @@ var opts = { var x = discreteUniform( [ 10 ], 0, 5, opts ); console.log( ndarray2array( x ) ); -var idx = glastIndexOfTruthy( [ x ] ); +var fromIndex = scalar2ndarray( 9, { + 'dtype': 'generic' +}); + +var idx = glastIndexOfTruthy( [ x, fromIndex ] ); console.log( idx ); diff --git a/lib/node_modules/@stdlib/blas/ext/base/ndarray/glast-index-of-truthy/lib/index.js b/lib/node_modules/@stdlib/blas/ext/base/ndarray/glast-index-of-truthy/lib/index.js index 4db6bff95338..52063d6cd8aa 100644 --- a/lib/node_modules/@stdlib/blas/ext/base/ndarray/glast-index-of-truthy/lib/index.js +++ b/lib/node_modules/@stdlib/blas/ext/base/ndarray/glast-index-of-truthy/lib/index.js @@ -24,12 +24,17 @@ * @module @stdlib/blas/ext/base/ndarray/glast-index-of-truthy * * @example +* var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' ); * var vector = require( '@stdlib/ndarray/vector/ctor' ); * var glastIndexOfTruthy = require( '@stdlib/blas/ext/base/ndarray/glast-index-of-truthy' ); * * var x = vector( [ 0.0, 3.0, 0.0, 2.0 ], 'generic' ); * -* var v = glastIndexOfTruthy( [ x ] ); +* var fromIndex = scalar2ndarray( 3, { +* 'dtype': 'generic' +* }); +* +* var v = glastIndexOfTruthy( [ x, fromIndex ] ); * // returns 3 */ diff --git a/lib/node_modules/@stdlib/blas/ext/base/ndarray/glast-index-of-truthy/lib/main.js b/lib/node_modules/@stdlib/blas/ext/base/ndarray/glast-index-of-truthy/lib/main.js index b09580d4a2ad..85e950c7d5ec 100644 --- a/lib/node_modules/@stdlib/blas/ext/base/ndarray/glast-index-of-truthy/lib/main.js +++ b/lib/node_modules/@stdlib/blas/ext/base/ndarray/glast-index-of-truthy/lib/main.js @@ -20,7 +20,9 @@ // MODULES // +var ndarraylike2scalar = require( '@stdlib/ndarray/base/ndarraylike2scalar' ); var numelDimension = require( '@stdlib/ndarray/base/numel-dimension' ); +var clipUpperIndex = require( '@stdlib/ndarray/base/clip-upper-index' ); var getStride = require( '@stdlib/ndarray/base/stride' ); var getOffset = require( '@stdlib/ndarray/base/offset' ); var getData = require( '@stdlib/ndarray/base/data-buffer' ); @@ -37,21 +39,38 @@ var strided = require( '@stdlib/blas/ext/base/glast-index-of-truthy' ).ndarray; * - The function expects the following ndarrays: * * - a one-dimensional input ndarray. +* - a zero-dimensional ndarray containing the index from which to begin searching. * * @param {ArrayLikeObject} arrays - array-like object containing ndarrays * @returns {integer} index * * @example +* var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' ); * var vector = require( '@stdlib/ndarray/vector/ctor' ); * * var x = vector( [ 0.0, 3.0, 0.0, 2.0 ], 'generic' ); * -* var v = glastIndexOfTruthy( [ x ] ); +* var fromIndex = scalar2ndarray( 3, { +* 'dtype': 'generic' +* }); +* +* var v = glastIndexOfTruthy( [ x, fromIndex ] ); * // returns 3 */ function glastIndexOfTruthy( arrays ) { - var x = arrays[ 0 ]; - return strided( numelDimension( x, 0 ), getData( x ), getStride( x, 0 ), getOffset( x ) ); // eslint-disable-line max-len + var fromIndex; + var N; + var x; + + x = arrays[ 0 ]; + fromIndex = ndarraylike2scalar( arrays[ 1 ] ); + + N = numelDimension( x, 0 ); + fromIndex = clipUpperIndex( fromIndex, ( fromIndex < 0 ) ? N : N-1 ); + if ( fromIndex < 0 ) { + return -1; + } + return strided( fromIndex+1, getData( x ), getStride( x, 0 ), getOffset( x ) ); // eslint-disable-line max-len } diff --git a/lib/node_modules/@stdlib/blas/ext/base/ndarray/glast-index-of-truthy/test/test.js b/lib/node_modules/@stdlib/blas/ext/base/ndarray/glast-index-of-truthy/test/test.js index 39287f8b8c49..f05f89516190 100644 --- a/lib/node_modules/@stdlib/blas/ext/base/ndarray/glast-index-of-truthy/test/test.js +++ b/lib/node_modules/@stdlib/blas/ext/base/ndarray/glast-index-of-truthy/test/test.js @@ -22,6 +22,7 @@ var tape = require( 'tape' ); var ndarray = require( '@stdlib/ndarray/base/ctor' ); +var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' ); var glastIndexOfTruthy = require( './../lib' ); @@ -56,61 +57,92 @@ tape( 'the function has an arity of 1', function test( t ) { }); tape( 'the function returns the index of the last truthy element in a one-dimensional ndarray', function test( t ) { + var fromIndex; var actual; var x; + fromIndex = scalar2ndarray( 5, { + 'dtype': 'generic' + }); + x = vector( [ 0.0, 3.0, 0.0, 2.0, 0.0, 0.0 ], 6, 1, 0 ); - actual = glastIndexOfTruthy( [ x ] ); + actual = glastIndexOfTruthy( [ x, fromIndex ] ); t.strictEqual( actual, 3, 'returns expected value' ); x = vector( [ 1.0, 0.0, 0.0, 0.0 ], 4, 1, 0 ); - actual = glastIndexOfTruthy( [ x ] ); + fromIndex = scalar2ndarray( 3, { + 'dtype': 'generic' + }); + actual = glastIndexOfTruthy( [ x, fromIndex ] ); t.strictEqual( actual, 0, 'returns expected value' ); x = vector( [ 0.0, 0.0, 0.0, 1.0 ], 4, 1, 0 ); - actual = glastIndexOfTruthy( [ x ] ); + fromIndex = scalar2ndarray( 3, { + 'dtype': 'generic' + }); + actual = glastIndexOfTruthy( [ x, fromIndex ] ); t.strictEqual( actual, 3, 'returns expected value' ); t.end(); }); tape( 'the function ignores falsy elements (e.g., `0`, `NaN`)', function test( t ) { + var fromIndex; var actual; var x; + fromIndex = scalar2ndarray( 4, { + 'dtype': 'generic' + }); + x = vector( [ 0.0, NaN, 5.0, 0.0, NaN ], 5, 1, 0 ); - actual = glastIndexOfTruthy( [ x ] ); + actual = glastIndexOfTruthy( [ x, fromIndex ] ); t.strictEqual( actual, 2, 'returns expected value' ); t.end(); }); tape( 'the function returns `-1` if unable to find a truthy element', function test( t ) { + var fromIndex; var actual; var x; + fromIndex = scalar2ndarray( 3, { + 'dtype': 'generic' + }); + x = vector( [ 0.0, 0.0, 0.0, 0.0 ], 4, 1, 0 ); - actual = glastIndexOfTruthy( [ x ] ); + actual = glastIndexOfTruthy( [ x, fromIndex ] ); t.strictEqual( actual, -1, 'returns expected value' ); t.end(); }); -tape( 'if provided an empty vector, the function returns `-1`', function test( t ) { +tape( 'the function returns `-1` if provided an empty input ndarray', function test( t ) { + var fromIndex; var actual; var x; x = vector( [], 0, 1, 0 ); - actual = glastIndexOfTruthy( [ x ] ); + fromIndex = scalar2ndarray( 0, { + 'dtype': 'generic' + }); + + actual = glastIndexOfTruthy( [ x, fromIndex ] ); t.strictEqual( actual, -1, 'returns expected value' ); t.end(); }); tape( 'the function supports one-dimensional ndarrays having non-unit strides', function test( t ) { + var fromIndex; var actual; var x; + fromIndex = scalar2ndarray( 3, { + 'dtype': 'generic' + }); + x = [ 0.0, // 0 2.0, @@ -122,16 +154,21 @@ tape( 'the function supports one-dimensional ndarrays having non-unit strides', 2.0 ]; - actual = glastIndexOfTruthy( [ vector( x, 4, 2, 0 ) ] ); + actual = glastIndexOfTruthy( [ vector( x, 4, 2, 0 ), fromIndex ] ); t.strictEqual( actual, 3, 'returns expected value' ); t.end(); }); tape( 'the function supports one-dimensional ndarrays having negative strides', function test( t ) { + var fromIndex; var actual; var x; + fromIndex = scalar2ndarray( 3, { + 'dtype': 'generic' + }); + x = [ 1.0, // 3 2.0, @@ -143,16 +180,21 @@ tape( 'the function supports one-dimensional ndarrays having negative strides', 2.0 ]; - actual = glastIndexOfTruthy( [ vector( x, 4, -2, 6 ) ] ); + actual = glastIndexOfTruthy( [ vector( x, 4, -2, 6 ), fromIndex ] ); t.strictEqual( actual, 3, 'returns expected value' ); t.end(); }); tape( 'the function supports one-dimensional ndarrays having non-zero offsets', function test( t ) { + var fromIndex; var actual; var x; + fromIndex = scalar2ndarray( 3, { + 'dtype': 'generic' + }); + x = [ 2.0, 1.0, // 0 @@ -164,8 +206,112 @@ tape( 'the function supports one-dimensional ndarrays having non-zero offsets', 0.0 // 3 ]; - actual = glastIndexOfTruthy( [ vector( x, 4, 2, 1 ) ] ); + actual = glastIndexOfTruthy( [ vector( x, 4, 2, 1 ), fromIndex ] ); t.strictEqual( actual, 2, 'returns expected value' ); t.end(); }); + +tape( 'the function supports a nonnegative starting search index', function test( t ) { + var fromIndex; + var actual; + var x; + + x = vector( [ 0.0, 1.0, 0.0, 2.0, 0.0, 3.0 ], 6, 1, 0 ); + + fromIndex = scalar2ndarray( 5, { + 'dtype': 'generic' + }); + actual = glastIndexOfTruthy( [ x, fromIndex ] ); + t.strictEqual( actual, 5, 'returns expected value' ); + + fromIndex = scalar2ndarray( 4, { + 'dtype': 'generic' + }); + actual = glastIndexOfTruthy( [ x, fromIndex ] ); + t.strictEqual( actual, 3, 'returns expected value' ); + + fromIndex = scalar2ndarray( 2, { + 'dtype': 'generic' + }); + actual = glastIndexOfTruthy( [ x, fromIndex ] ); + t.strictEqual( actual, 1, 'returns expected value' ); + + fromIndex = scalar2ndarray( 0, { + 'dtype': 'generic' + }); + actual = glastIndexOfTruthy( [ x, fromIndex ] ); + t.strictEqual( actual, -1, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports a negative starting search index', function test( t ) { + var fromIndex; + var actual; + var x; + + x = vector( [ 0.0, 1.0, 0.0, 2.0, 0.0, 3.0 ], 6, 1, 0 ); + + fromIndex = scalar2ndarray( -1, { + 'dtype': 'generic' + }); + actual = glastIndexOfTruthy( [ x, fromIndex ] ); + t.strictEqual( actual, 5, 'returns expected value' ); + + fromIndex = scalar2ndarray( -2, { + 'dtype': 'generic' + }); + actual = glastIndexOfTruthy( [ x, fromIndex ] ); + t.strictEqual( actual, 3, 'returns expected value' ); + + fromIndex = scalar2ndarray( -3, { + 'dtype': 'generic' + }); + actual = glastIndexOfTruthy( [ x, fromIndex ] ); + t.strictEqual( actual, 3, 'returns expected value' ); + + fromIndex = scalar2ndarray( -5, { + 'dtype': 'generic' + }); + actual = glastIndexOfTruthy( [ x, fromIndex ] ); + t.strictEqual( actual, 1, 'returns expected value' ); + + fromIndex = scalar2ndarray( -6, { + 'dtype': 'generic' + }); + actual = glastIndexOfTruthy( [ x, fromIndex ] ); + t.strictEqual( actual, -1, 'returns expected value' ); + + fromIndex = scalar2ndarray( -7, { + 'dtype': 'generic' + }); + actual = glastIndexOfTruthy( [ x, fromIndex ] ); + t.strictEqual( actual, -1, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function clamps a starting search index which is greater than or equal to the number of elements in the input ndarray', function test( t ) { + var fromIndex; + var actual; + var x; + + x = vector( [ 0.0, 1.0, 0.0, 2.0, 0.0, 3.0 ], 6, 1, 0 ); + + fromIndex = scalar2ndarray( 6, { + 'dtype': 'generic' + }); + + actual = glastIndexOfTruthy( [ x, fromIndex ] ); + t.strictEqual( actual, 5, 'returns expected value' ); + + fromIndex = scalar2ndarray( 7, { + 'dtype': 'generic' + }); + + actual = glastIndexOfTruthy( [ x, fromIndex ] ); + t.strictEqual( actual, 5, 'returns expected value' ); + + t.end(); +});