Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ var gfindIndex = require( '@stdlib/blas/ext/base/ndarray/gfind-index' );
Returns the index of the first element in a one-dimensional ndarray which passes a test implemented by a predicate function.

```javascript
var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' );
var vector = require( '@stdlib/ndarray/vector/ctor' );

function isEven( v ) {
Expand All @@ -49,13 +50,18 @@ function isEven( v ) {

var x = vector( [ 1.0, 3.0, 4.0, 2.0 ], 'generic' );

var idx = gfindIndex( [ x ], isEven );
var fromIndex = scalar2ndarray( 0, {
'dtype': 'generic'
});

var idx = gfindIndex( [ x, fromIndex ], isEven );
// returns 2
```

If no element passes a test implemented by a predicate function, the function returns `-1`.

```javascript
var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' );
var vector = require( '@stdlib/ndarray/vector/ctor' );

function isEven( v ) {
Expand All @@ -64,14 +70,23 @@ function isEven( v ) {

var x = vector( [ 1.0, 3.0, 5.0, 7.0 ], 'generic' );

var idx = gfindIndex( [ x ], isEven );
var fromIndex = scalar2ndarray( 0, {
'dtype': 'generic'
});

var idx = gfindIndex( [ x, fromIndex ], isEven );
// returns -1
```

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.

- **clbk**: callback function.

- **thisArg**: callback execution context (_optional_).

The callback function is provided the following arguments:
Expand All @@ -83,6 +98,7 @@ The callback function is provided the following arguments:
To set the callback execution context, provide a `thisArg`.

```javascript
var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' );
var vector = require( '@stdlib/ndarray/vector/ctor' );

function isEven( v ) {
Expand All @@ -95,7 +111,11 @@ var ctx = {
'count': 0
};

var v = gfindIndex( [ x ], isEven, ctx );
var fromIndex = scalar2ndarray( 0, {
'dtype': 'generic'
});

var v = gfindIndex( [ x, fromIndex ], isEven, ctx );
// returns 2

var count = ctx.count;
Expand All @@ -111,6 +131,7 @@ var count = ctx.count;
## Notes

- If provided an empty one-dimensional ndarray, the function returns `-1`.
- 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).

</section>

Expand All @@ -124,6 +145,7 @@ var count = ctx.count;

```javascript
var discreteUniform = require( '@stdlib/random/discrete-uniform' );
var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' );
var ndarray2array = require( '@stdlib/ndarray/to-array' );
var gfindIndex = require( '@stdlib/blas/ext/base/ndarray/gfind-index' );

Expand All @@ -138,7 +160,11 @@ var opts = {
var x = discreteUniform( [ 10 ], -100, 100, opts );
console.log( ndarray2array( x ) );

var idx = gfindIndex( [ x ], isEven );
var fromIndex = scalar2ndarray( 0, {
'dtype': 'generic'
});

var idx = gfindIndex( [ x, fromIndex ], isEven );
console.log( idx );
```

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

var bench = require( '@stdlib/bench' );
var uniform = require( '@stdlib/random/uniform' );
var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' );
var isnan = require( '@stdlib/math/base/assert/is-nan' );
var pow = require( '@stdlib/math/base/special/pow' );
var format = require( '@stdlib/string/format' );
Expand Down Expand Up @@ -57,7 +58,13 @@ function clbk( v ) {
* @returns {Function} benchmark function
*/
function createBenchmark( len ) {
var x = uniform( [ len ], 0.0, 100.0, options );
var fromIndex;
var x;

x = uniform( [ len ], 0.0, 100.0, options );
fromIndex = scalar2ndarray( 0, {
'dtype': 'generic'
});
return benchmark;

/**
Expand All @@ -72,7 +79,7 @@ function createBenchmark( len ) {

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
v = gfindIndex( [ x ], clbk );
v = gfindIndex( [ x, fromIndex ], clbk );
if ( isnan( v ) ) {
b.fail( 'should not return NaN' );
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
Returns the index of the first element in a one-dimensional ndarray which
passes a test implemented by a predicate function.

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 provided an empty ndarray or no element passes a test implemented by a
predicate function, the function returns `-1`.

Expand All @@ -15,7 +19,11 @@
Parameters
----------
arrays: ArrayLikeObject<ndarray>
Array-like object containing a one-dimensional input ndarray.
Array-like object containing the following ndarrays:

- a one-dimensional input ndarray.
- a zero-dimensional ndarray containing the index from which to begin
searching.

clbk: Function
Callback function.
Expand All @@ -31,8 +39,9 @@
Examples
--------
> var x = {{alias:@stdlib/ndarray/vector/ctor}}( [ 1.0, -2.0, 2.0 ], 'generic' );
> var i = {{alias:@stdlib/ndarray/from-scalar}}( 0, { 'dtype': 'generic' } );
> function f ( v ) { return v % 2.0 === 0.0; };
> {{alias}}( [ x ], f )
> {{alias}}( [ x, i ], f )
1

See Also
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ type Predicate<T, U, ThisArg> = Nullary<ThisArg> | Unary<T, ThisArg> | Binary<T,
* - The function expects the following ndarrays:
*
* - a one-dimensional input ndarray.
* - a zero-dimensional ndarray containing the index from which to begin searching.
*
* - If no element passes a test implemented by a predicate function, the function returns `-1`.
*
Expand All @@ -83,6 +84,7 @@ type Predicate<T, U, ThisArg> = Nullary<ThisArg> | Unary<T, ThisArg> | Binary<T,
* @returns index
*
* @example
* var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' );
* var vector = require( '@stdlib/ndarray/vector/ctor' );
*
* function clbk( v ) {
Expand All @@ -91,10 +93,14 @@ type Predicate<T, U, ThisArg> = Nullary<ThisArg> | Unary<T, ThisArg> | Binary<T,
*
* var x = vector( [ 1.0, 3.0, 4.0, 2.0 ], 'generic' );
*
* var v = gfindIndex( [ x ], clbk );
* var fromIndex = scalar2ndarray( 0, {
* 'dtype': 'generic'
* });
*
* var v = gfindIndex( [ x, fromIndex ], clbk );
* // returns 2
*/
declare function gfindIndex<T = unknown, U extends typedndarray<T> = typedndarray<T>, ThisArg = unknown>( arrays: [ U ], clbk: Predicate<T, U, ThisArg>, thisArg?: ThisParameterType<Predicate<T, U, ThisArg>> ): number;
declare function gfindIndex<T = unknown, U extends typedndarray<T> = typedndarray<T>, ThisArg = unknown>( arrays: [ U, typedndarray<number> ], clbk: Predicate<T, U, ThisArg>, thisArg?: ThisParameterType<Predicate<T, U, ThisArg>> ): number;


// EXPORTS //
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
/* eslint-disable space-in-parens */

import zeros = require( '@stdlib/ndarray/zeros' );
import scalar2ndarray = require( '@stdlib/ndarray/from-scalar' );
import gfindIndex = require( './index' );

/**
Expand All @@ -27,7 +28,7 @@
* @param value - ndarray element
* @returns result
*/
function clbk( value: any ): boolean {

Check warning on line 31 in lib/node_modules/@stdlib/blas/ext/base/ndarray/gfind-index/docs/types/test.ts

View workflow job for this annotation

GitHub Actions / Lint Changed Files

Unexpected any. Specify a different type
return value % 2.0 === 0.0;
}

Expand All @@ -39,9 +40,12 @@
const x = zeros( [ 10 ], {
'dtype': 'generic'
});
const fromIndex = scalar2ndarray( 0, {
'dtype': 'generic'
});

gfindIndex( [ x ], clbk ); // $ExpectType number
gfindIndex( [ x ], clbk, {} ); // $ExpectType number
gfindIndex( [ x, fromIndex ], clbk ); // $ExpectType number
gfindIndex( [ x, fromIndex ], clbk, {} ); // $ExpectType number
}

// The compiler throws an error if the function is provided a first argument which is not an array of ndarrays...
Expand Down Expand Up @@ -72,24 +76,27 @@
const x = zeros( [ 10 ], {
'dtype': 'generic'
});
const fromIndex = scalar2ndarray( 0, {
'dtype': 'generic'
});

gfindIndex( [ x ], '10' ); // $ExpectError
gfindIndex( [ x ], 10 ); // $ExpectError
gfindIndex( [ x ], true ); // $ExpectError
gfindIndex( [ x ], false ); // $ExpectError
gfindIndex( [ x ], null ); // $ExpectError
gfindIndex( [ x ], undefined ); // $ExpectError
gfindIndex( [ x ], [] ); // $ExpectError
gfindIndex( [ x ], {} ); // $ExpectError
gfindIndex( [ x, fromIndex ], '10' ); // $ExpectError
gfindIndex( [ x, fromIndex ], 10 ); // $ExpectError
gfindIndex( [ x, fromIndex ], true ); // $ExpectError
gfindIndex( [ x, fromIndex ], false ); // $ExpectError
gfindIndex( [ x, fromIndex ], null ); // $ExpectError
gfindIndex( [ x, fromIndex ], undefined ); // $ExpectError
gfindIndex( [ x, fromIndex ], [] ); // $ExpectError
gfindIndex( [ x, fromIndex ], {} ); // $ExpectError

gfindIndex( [ x ], '10', {} ); // $ExpectError
gfindIndex( [ x ], 10, {} ); // $ExpectError
gfindIndex( [ x ], true, {} ); // $ExpectError
gfindIndex( [ x ], false, {} ); // $ExpectError
gfindIndex( [ x ], null, {} ); // $ExpectError
gfindIndex( [ x ], undefined, {} ); // $ExpectError
gfindIndex( [ x ], [], {} ); // $ExpectError
gfindIndex( [ x ], {}, {} ); // $ExpectError
gfindIndex( [ x, fromIndex ], '10', {} ); // $ExpectError
gfindIndex( [ x, fromIndex ], 10, {} ); // $ExpectError
gfindIndex( [ x, fromIndex ], true, {} ); // $ExpectError
gfindIndex( [ x, fromIndex ], false, {} ); // $ExpectError
gfindIndex( [ x, fromIndex ], null, {} ); // $ExpectError
gfindIndex( [ x, fromIndex ], undefined, {} ); // $ExpectError
gfindIndex( [ x, fromIndex ], [], {} ); // $ExpectError
gfindIndex( [ x, fromIndex ], {}, {} ); // $ExpectError
}

// The compiler throws an error if the function is provided an unsupported number of arguments...
Expand All @@ -98,6 +105,10 @@
'dtype': 'generic'
});

const fromIndex = scalar2ndarray( 0, {
'dtype': 'generic'
});

gfindIndex(); // $ExpectError
gfindIndex( [ x ], clbk, {}, {} ); // $ExpectError
gfindIndex( [ x, fromIndex ], clbk, {}, {} ); // $ExpectError
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 gfindIndex = require( './../lib' );

Expand All @@ -33,5 +34,9 @@ var opts = {
var x = discreteUniform( [ 10 ], -100, 100, opts );
console.log( ndarray2array( x ) );

var idx = gfindIndex( [ x ], isEven );
var fromIndex = scalar2ndarray( 0, {
'dtype': 'generic'
});

var idx = gfindIndex( [ x, fromIndex ], isEven );
console.log( idx );
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
* @module @stdlib/blas/ext/base/ndarray/gfind-index
*
* @example
* var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' );
* var vector = require( '@stdlib/ndarray/vector/ctor' );
* var gfindIndex = require( '@stdlib/blas/ext/base/ndarray/gfind-index' );
*
Expand All @@ -33,7 +34,11 @@
*
* var x = vector( [ 1.0, 3.0, 4.0, 2.0 ], 'generic' );
*
* var v = gfindIndex( [ x ], clbk );
* var fromIndex = scalar2ndarray( 0, {
* 'dtype': 'generic'
* });
*
* var v = gfindIndex( [ x, fromIndex ], clbk );
* // returns 2
*/

Expand Down
Loading
Loading