From 53c41ec38d2750b6c216cf852b46ab1c1e9869c5 Mon Sep 17 00:00:00 2001 From: headlessNode Date: Fri, 21 Aug 2026 22:15:35 +0500 Subject: [PATCH] feat: add blas/ext/base/ndarray/sindex-of-truthy --- .../base/ndarray/sindex-of-truthy/README.md | 27 +++- .../sindex-of-truthy/benchmark/benchmark.js | 11 +- .../ndarray/sindex-of-truthy/docs/repl.txt | 9 +- .../sindex-of-truthy/docs/types/index.d.ts | 12 +- .../sindex-of-truthy/docs/types/test.ts | 11 +- .../sindex-of-truthy/examples/index.js | 7 +- .../ndarray/sindex-of-truthy/lib/index.js | 7 +- .../base/ndarray/sindex-of-truthy/lib/main.js | 37 ++++- .../ndarray/sindex-of-truthy/test/test.js | 128 +++++++++++++++++- 9 files changed, 226 insertions(+), 23 deletions(-) diff --git a/lib/node_modules/@stdlib/blas/ext/base/ndarray/sindex-of-truthy/README.md b/lib/node_modules/@stdlib/blas/ext/base/ndarray/sindex-of-truthy/README.md index 2f6b31cbaba2..cb0be2e5f321 100644 --- a/lib/node_modules/@stdlib/blas/ext/base/ndarray/sindex-of-truthy/README.md +++ b/lib/node_modules/@stdlib/blas/ext/base/ndarray/sindex-of-truthy/README.md @@ -42,25 +42,38 @@ Returns the index of the first truthy element in a one-dimensional single-precis ```javascript var Float32Vector = require( '@stdlib/ndarray/vector/float32' ); +var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' ); var x = new Float32Vector( [ 0.0, 0.0, 3.0, 2.0 ] ); -var idx = sindexOfTruthy( [ x ] ); +var fromIndex = scalar2ndarray( 0, { + 'dtype': 'generic' +}); + +var idx = sindexOfTruthy( [ x, fromIndex ] ); // returns 2 ``` 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 Float32Vector = require( '@stdlib/ndarray/vector/float32' ); +var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' ); var x = new Float32Vector( [ 0.0, 0.0, 0.0, 0.0 ] ); -var idx = sindexOfTruthy( [ x ] ); +var fromIndex = scalar2ndarray( 0, { + 'dtype': 'generic' +}); + +var idx = sindexOfTruthy( [ x, fromIndex ] ); // returns -1 ``` @@ -73,6 +86,7 @@ var idx = sindexOfTruthy( [ x ] ); ## Notes - The function explicitly 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 = sindexOfTruthy( [ x ] ); ```javascript var bernoulli = require( '@stdlib/random/bernoulli' ); +var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' ); var ndarray2array = require( '@stdlib/ndarray/to-array' ); var sindexOfTruthy = require( '@stdlib/blas/ext/base/ndarray/sindex-of-truthy' ); @@ -96,7 +111,11 @@ var opts = { var x = bernoulli( [ 10 ], 0.7, opts ); console.log( ndarray2array( x ) ); -var idx = sindexOfTruthy( [ x ] ); +var fromIndex = scalar2ndarray( 0, { + 'dtype': 'generic' +}); + +var idx = sindexOfTruthy( [ x, fromIndex ] ); console.log( idx ); ``` diff --git a/lib/node_modules/@stdlib/blas/ext/base/ndarray/sindex-of-truthy/benchmark/benchmark.js b/lib/node_modules/@stdlib/blas/ext/base/ndarray/sindex-of-truthy/benchmark/benchmark.js index e76d29deff61..bb3dc1ca792c 100644 --- a/lib/node_modules/@stdlib/blas/ext/base/ndarray/sindex-of-truthy/benchmark/benchmark.js +++ b/lib/node_modules/@stdlib/blas/ext/base/ndarray/sindex-of-truthy/benchmark/benchmark.js @@ -22,6 +22,7 @@ var bench = require( '@stdlib/bench' ); var zeros = require( '@stdlib/ndarray/zeros' ); +var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' ); var isInteger = require( '@stdlib/assert/is-integer' ).isPrimitive; var pow = require( '@stdlib/math/base/special/pow' ); var format = require( '@stdlib/string/format' ); @@ -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( 0, { + 'dtype': 'generic' + }); return benchmark; /** @@ -61,7 +68,7 @@ function createBenchmark( len ) { b.tic(); for ( i = 0; i < b.iterations; i++ ) { - out = sindexOfTruthy( [ x ] ); + out = sindexOfTruthy( [ x, fromIndex ] ); if ( out !== out ) { b.fail( 'should return an integer' ); } diff --git a/lib/node_modules/@stdlib/blas/ext/base/ndarray/sindex-of-truthy/docs/repl.txt b/lib/node_modules/@stdlib/blas/ext/base/ndarray/sindex-of-truthy/docs/repl.txt index a585b9cf6d57..cbdbcdfadf8c 100644 --- a/lib/node_modules/@stdlib/blas/ext/base/ndarray/sindex-of-truthy/docs/repl.txt +++ b/lib/node_modules/@stdlib/blas/ext/base/ndarray/sindex-of-truthy/docs/repl.txt @@ -3,6 +3,10 @@ Returns the index of the first truthy element in a one-dimensional single- precision floating-point 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 @@ -11,6 +15,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 ------- @@ -20,7 +26,8 @@ Examples -------- > var x = new {{alias:@stdlib/ndarray/vector/float32}}( [ 0.0, 0.0, 3.0 ] ); - > {{alias}}( [ x ] ) + > var i = {{alias:@stdlib/ndarray/from-scalar}}( 0, { 'dtype': 'generic' } ); + > {{alias}}( [ x, i ] ) 2 See Also diff --git a/lib/node_modules/@stdlib/blas/ext/base/ndarray/sindex-of-truthy/docs/types/index.d.ts b/lib/node_modules/@stdlib/blas/ext/base/ndarray/sindex-of-truthy/docs/types/index.d.ts index 8cde0d384af2..1d7bfda2ffd9 100644 --- a/lib/node_modules/@stdlib/blas/ext/base/ndarray/sindex-of-truthy/docs/types/index.d.ts +++ b/lib/node_modules/@stdlib/blas/ext/base/ndarray/sindex-of-truthy/docs/types/index.d.ts @@ -20,7 +20,7 @@ /// -import { float32ndarray } from '@stdlib/types/ndarray'; +import { float32ndarray, typedndarray } from '@stdlib/types/ndarray'; /** * Returns the index of the first truthy element in a one-dimensional single-precision floating-point ndarray. @@ -30,19 +30,25 @@ import { float32ndarray } 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 Float32Vector = require( '@stdlib/ndarray/vector/float32' ); +* var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' ); * * var x = new Float32Vector( [ 0.0, 0.0, 3.0, 2.0 ] ); * -* var v = sindexOfTruthy( [ x ] ); +* var fromIndex = scalar2ndarray( 0, { +* 'dtype': 'generic' +* }); +* +* var v = sindexOfTruthy( [ x, fromIndex ] ); * // returns 2 */ -declare function sindexOfTruthy( arrays: [ float32ndarray ] ): number; +declare function sindexOfTruthy( arrays: [ float32ndarray, typedndarray ] ): number; // EXPORTS // diff --git a/lib/node_modules/@stdlib/blas/ext/base/ndarray/sindex-of-truthy/docs/types/test.ts b/lib/node_modules/@stdlib/blas/ext/base/ndarray/sindex-of-truthy/docs/types/test.ts index 3e3cd2666908..45026e588450 100644 --- a/lib/node_modules/@stdlib/blas/ext/base/ndarray/sindex-of-truthy/docs/types/test.ts +++ b/lib/node_modules/@stdlib/blas/ext/base/ndarray/sindex-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 sindexOfTruthy = require( './index' ); @@ -29,8 +30,11 @@ import sindexOfTruthy = require( './index' ); const x = zeros( [ 10 ], { 'dtype': 'float32' }); + const fromIndex = scalar2ndarray( 0, { + 'dtype': 'generic' + }); - sindexOfTruthy( [ x ] ); // $ExpectType number + sindexOfTruthy( [ 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 sindexOfTruthy = require( './index' ); const x = zeros( [ 10 ], { 'dtype': 'float32' }); + const fromIndex = scalar2ndarray( 0, { + 'dtype': 'generic' + }); sindexOfTruthy(); // $ExpectError - sindexOfTruthy( [ x ], {} ); // $ExpectError + sindexOfTruthy( [ x, fromIndex ], {} ); // $ExpectError } diff --git a/lib/node_modules/@stdlib/blas/ext/base/ndarray/sindex-of-truthy/examples/index.js b/lib/node_modules/@stdlib/blas/ext/base/ndarray/sindex-of-truthy/examples/index.js index 6f4d1b3940f7..621534075fbd 100644 --- a/lib/node_modules/@stdlib/blas/ext/base/ndarray/sindex-of-truthy/examples/index.js +++ b/lib/node_modules/@stdlib/blas/ext/base/ndarray/sindex-of-truthy/examples/index.js @@ -19,6 +19,7 @@ 'use strict'; var bernoulli = require( '@stdlib/random/bernoulli' ); +var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' ); var ndarray2array = require( '@stdlib/ndarray/to-array' ); var sindexOfTruthy = require( './../lib' ); @@ -29,5 +30,9 @@ var opts = { var x = bernoulli( [ 10 ], 0.7, opts ); console.log( ndarray2array( x ) ); -var idx = sindexOfTruthy( [ x ] ); +var fromIndex = scalar2ndarray( 0, { + 'dtype': 'generic' +}); + +var idx = sindexOfTruthy( [ x, fromIndex ] ); console.log( idx ); diff --git a/lib/node_modules/@stdlib/blas/ext/base/ndarray/sindex-of-truthy/lib/index.js b/lib/node_modules/@stdlib/blas/ext/base/ndarray/sindex-of-truthy/lib/index.js index 460ab10b6093..6bf4bd49701c 100644 --- a/lib/node_modules/@stdlib/blas/ext/base/ndarray/sindex-of-truthy/lib/index.js +++ b/lib/node_modules/@stdlib/blas/ext/base/ndarray/sindex-of-truthy/lib/index.js @@ -25,11 +25,16 @@ * * @example * var Float32Vector = require( '@stdlib/ndarray/vector/float32' ); +* var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' ); * var sindexOfTruthy = require( '@stdlib/blas/ext/base/ndarray/sindex-of-truthy' ); * * var x = new Float32Vector( [ 0.0, 0.0, 3.0, 2.0 ] ); * -* var v = sindexOfTruthy( [ x ] ); +* var fromIndex = scalar2ndarray( 0, { +* 'dtype': 'generic' +* }); +* +* var v = sindexOfTruthy( [ x, fromIndex ] ); * // returns 2 */ diff --git a/lib/node_modules/@stdlib/blas/ext/base/ndarray/sindex-of-truthy/lib/main.js b/lib/node_modules/@stdlib/blas/ext/base/ndarray/sindex-of-truthy/lib/main.js index 52373ed62afc..aeea5c34c1a0 100644 --- a/lib/node_modules/@stdlib/blas/ext/base/ndarray/sindex-of-truthy/lib/main.js +++ b/lib/node_modules/@stdlib/blas/ext/base/ndarray/sindex-of-truthy/lib/main.js @@ -20,10 +20,12 @@ // MODULES // +var ndarraylike2scalar = require( '@stdlib/ndarray/base/ndarraylike2scalar' ); var numelDimension = require( '@stdlib/ndarray/base/numel-dimension' ); var getStride = require( '@stdlib/ndarray/base/stride' ); var getOffset = require( '@stdlib/ndarray/base/offset' ); var getData = require( '@stdlib/ndarray/base/data-buffer' ); +var clipIndex = require( '@stdlib/ndarray/base/clip-index' ); var strided = require( '@stdlib/blas/ext/base/sindex-of-truthy' ).ndarray; @@ -37,21 +39,50 @@ var strided = require( '@stdlib/blas/ext/base/sindex-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 Float32Vector = require( '@stdlib/ndarray/vector/float32' ); +* var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' ); * * var x = new Float32Vector( [ 0.0, 0.0, 3.0, 2.0 ] ); * -* var v = sindexOfTruthy( [ x ] ); +* var fromIndex = scalar2ndarray( 0, { +* 'dtype': 'generic' +* }); +* +* var v = sindexOfTruthy( [ x, fromIndex ] ); * // returns 2 */ function sindexOfTruthy( arrays ) { - var x = arrays[ 0 ]; - return strided( numelDimension( x, 0 ), getData( x ), getStride( x, 0 ), getOffset( x ) ); + var fromIndex; + var stride; + var offset; + var idx; + var N; + var x; + + x = arrays[ 0 ]; + fromIndex = ndarraylike2scalar( arrays[ 1 ] ); + + N = numelDimension( x, 0 ); + fromIndex = clipIndex( fromIndex, N ); + if ( fromIndex >= N ) { + return -1; + } + N -= fromIndex; + + stride = getStride( x, 0 ); + offset = getOffset( x ) + ( stride*fromIndex ); + + idx = strided( N, getData( x ), stride, offset ); + if ( idx >= 0 ) { + idx += fromIndex; + } + return idx; } diff --git a/lib/node_modules/@stdlib/blas/ext/base/ndarray/sindex-of-truthy/test/test.js b/lib/node_modules/@stdlib/blas/ext/base/ndarray/sindex-of-truthy/test/test.js index 37621d3c7e48..ec20301220d0 100644 --- a/lib/node_modules/@stdlib/blas/ext/base/ndarray/sindex-of-truthy/test/test.js +++ b/lib/node_modules/@stdlib/blas/ext/base/ndarray/sindex-of-truthy/test/test.js @@ -22,6 +22,7 @@ var tape = require( 'tape' ); var Float32Array = require( '@stdlib/array/float32' ); +var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' ); var ndarray = require( '@stdlib/ndarray/base/ctor' ); var sindexOfTruthy = require( './../lib' ); @@ -52,52 +53,167 @@ tape( 'main export is a function', function test( t ) { }); tape( 'the function returns the index of the first truthy element', function test( t ) { + var fromIndex; var actual; var x; x = vector( new Float32Array( [ 0.0, 0.0, 3.0, 2.0, 0.0, 1.0 ] ), 6, 1, 0 ); + fromIndex = scalar2ndarray( 0, { + 'dtype': 'generic' + }); - actual = sindexOfTruthy( [ x ] ); + actual = sindexOfTruthy( [ x, fromIndex ] ); t.strictEqual( actual, 2, 'returns expected value' ); x = vector( new Float32Array( [ 0.0, 0.0, 3.0, 2.0, 0.0, 1.0 ] ), 4, 1, 2 ); + fromIndex = scalar2ndarray( 0, { + 'dtype': 'generic' + }); - actual = sindexOfTruthy( [ x ] ); + actual = sindexOfTruthy( [ x, fromIndex ] ); t.strictEqual( actual, 0, 'returns expected value' ); x = vector( new Float32Array( [ 0.0, 0.0, 3.0, 2.0, 0.0, 1.0 ] ), 3, 2, 0 ); + fromIndex = scalar2ndarray( 0, { + 'dtype': 'generic' + }); - actual = sindexOfTruthy( [ x ] ); + actual = sindexOfTruthy( [ x, fromIndex ] ); t.strictEqual( actual, 1, 'returns expected value' ); // Negative stride... x = vector( new Float32Array( [ 0.0, 0.0, 3.0, 2.0, 0.0, 1.0 ] ), 6, -1, 5 ); + fromIndex = scalar2ndarray( 0, { + 'dtype': 'generic' + }); - actual = sindexOfTruthy( [ x ] ); + actual = sindexOfTruthy( [ x, fromIndex ] ); t.strictEqual( actual, 0, 'returns expected value' ); t.end(); }); +tape( 'the function supports a `fromIndex` parameter (nonnegative)', function test( t ) { + var fromIndex; + var actual; + var x; + + x = vector( new Float32Array( [ 1.0, 0.0, 1.0, 0.0, 1.0, 0.0 ] ), 6, 1, 0 ); + + fromIndex = scalar2ndarray( 0, { + 'dtype': 'generic' + }); + actual = sindexOfTruthy( [ x, fromIndex ] ); + t.strictEqual( actual, 0, 'returns expected value' ); + + fromIndex = scalar2ndarray( 1, { + 'dtype': 'generic' + }); + actual = sindexOfTruthy( [ x, fromIndex ] ); + t.strictEqual( actual, 2, 'returns expected value' ); + + fromIndex = scalar2ndarray( 3, { + 'dtype': 'generic' + }); + actual = sindexOfTruthy( [ x, fromIndex ] ); + t.strictEqual( actual, 4, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports a `fromIndex` parameter (negative)', function test( t ) { + var fromIndex; + var actual; + var x; + + x = vector( new Float32Array( [ 0.0, 1.0, 0.0, 1.0, 0.0, 1.0 ] ), 6, 1, 0 ); + + fromIndex = scalar2ndarray( -1, { + 'dtype': 'generic' + }); + actual = sindexOfTruthy( [ x, fromIndex ] ); + t.strictEqual( actual, 5, 'returns expected value' ); + + fromIndex = scalar2ndarray( -2, { + 'dtype': 'generic' + }); + actual = sindexOfTruthy( [ x, fromIndex ] ); + t.strictEqual( actual, 5, 'returns expected value' ); + + fromIndex = scalar2ndarray( -3, { + 'dtype': 'generic' + }); + actual = sindexOfTruthy( [ x, fromIndex ] ); + t.strictEqual( actual, 3, 'returns expected value' ); + + fromIndex = scalar2ndarray( -7, { + 'dtype': 'generic' + }); + actual = sindexOfTruthy( [ x, fromIndex ] ); + t.strictEqual( actual, 1, 'returns expected value' ); + + t.end(); +}); + tape( 'the function ignores falsy elements (e.g., `0`, `NaN`)', function test( t ) { + var fromIndex; var actual; var x; x = vector( new Float32Array( [ 0.0, NaN, 5.0, 0.0 ] ), 4, 1, 0 ); + fromIndex = scalar2ndarray( 0, { + 'dtype': 'generic' + }); - actual = sindexOfTruthy( [ x ] ); + actual = sindexOfTruthy( [ 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; x = vector( new Float32Array( [ 0.0, 0.0, 0.0, 0.0 ] ), 4, 1, 0 ); + fromIndex = scalar2ndarray( 0, { + 'dtype': 'generic' + }); + + actual = sindexOfTruthy( [ x, fromIndex ] ); + t.strictEqual( actual, -1, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function returns `-1` if provided an empty one-dimensional ndarray', function test( t ) { + var fromIndex; + var actual; + var x; + + x = vector( new Float32Array( [] ), 0, 1, 0 ); + fromIndex = scalar2ndarray( 0, { + 'dtype': 'generic' + }); + + actual = sindexOfTruthy( [ x, fromIndex ] ); + t.strictEqual( actual, -1, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function returns `-1` if provided a starting search index which is greater than or equal to number of elements in the input ndarray', function test( t ) { + var fromIndex; + var actual; + var x; + + x = vector( new Float32Array( [ 1.0, 0.0, 1.0, 0.0, 1.0, 0.0 ] ), 6, 1, 0 ); + fromIndex = scalar2ndarray( 7, { + 'dtype': 'generic' + }); - actual = sindexOfTruthy( [ x ] ); + actual = sindexOfTruthy( [ x, fromIndex ] ); t.strictEqual( actual, -1, 'returns expected value' ); t.end();