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 @@ -42,11 +42,16 @@ Returns the index of the first element in a one-dimensional ndarray which is les

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

var x = vector( [ 0.0, 0.0, 0.0, 0.0 ], 'generic' );
var y = vector( [ 0.0, 0.0, 1.0, 0.0 ], 'generic' );

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

var idx = gfirstIndexLessThan( [ x, y, fromIndex ] );
// returns 2
```

Expand All @@ -56,16 +61,22 @@ The function has the following parameters:

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

If the function is unable to find an element in the first one-dimensional input ndarray which is less than a corresponding element in the second one-dimensional input ndarray, the function returns `-1`.

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

var x = vector( [ 5.0, 6.0, 7.0, 8.0 ], 'generic' );
var y = vector( [ 1.0, 2.0, 3.0, 4.0 ], 'generic' );

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

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

Expand All @@ -77,6 +88,7 @@ var idx = gfirstIndexLessThan( [ x, y ] );

## Notes

- 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).
- When comparing elements, the function checks whether an element in the first one-dimensional input ndarray is less than a corresponding element in the second one-dimensional input ndarray using the less-than operator `<`. As a consequence, comparisons involving `NaN` always evaluate to `false`.

</section>
Expand All @@ -91,7 +103,9 @@ var idx = gfirstIndexLessThan( [ x, y ] );

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

var opts = {
Expand All @@ -103,7 +117,12 @@ console.log( ndarray2array( x ) );
var y = discreteUniform( [ 10 ], 0, 10, opts );
console.log( ndarray2array( y ) );

var idx = gfirstIndexLessThan( [ x, y ] );
var fromIndex = scalar2ndarray( 0, {
'dtype': 'generic'
});
console.log( 'From Index:', ndarraylike2scalar( fromIndex ) );

var idx = gfirstIndexLessThan( [ x, y, fromIndex ] );
console.log( idx );
```

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@
var bench = require( '@stdlib/bench' );
var isInteger = require( '@stdlib/assert/is-integer' ).isPrimitive;
var pow = require( '@stdlib/math/base/special/pow' );
var vector = require( '@stdlib/ndarray/vector/ctor' );
var ones = require( '@stdlib/array/ones' );
var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' );
var ones = require( '@stdlib/ndarray/ones' );
var format = require( '@stdlib/string/format' );
var pkg = require( './../package.json' ).name;
var gfirstIndexLessThan = require( './../lib' );
Expand All @@ -47,8 +47,15 @@ var options = {
* @returns {Function} benchmark function
*/
function createBenchmark( len ) {
var x = vector( ones( len, options.dtype ), options.dtype );
var y = vector( ones( len, options.dtype ), options.dtype );
var fromIndex;
var x;
var y;

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

/**
Expand All @@ -63,7 +70,7 @@ function createBenchmark( len ) {

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
out = gfirstIndexLessThan( [ x, y ] );
out = gfirstIndexLessThan( [ x, y, fromIndex ] );
if ( out !== out ) {
b.fail( 'should return an integer' );
}
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 is
less than a corresponding element in another 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).

When comparing elements, the function checks whether an element in the first
one-dimensional input ndarray is less than a corresponding element in the
second one-dimensional input ndarray using the less-than operator `<`. As a
Expand All @@ -15,6 +19,8 @@

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

Returns
-------
Expand All @@ -25,7 +31,8 @@
--------
> var x = {{alias:@stdlib/ndarray/vector/ctor}}( [ 0.0, 0.0, 0.0, 0.0 ], 'generic' );
> var y = {{alias:@stdlib/ndarray/vector/ctor}}( [ 0.0, 0.0, 1.0, 0.0 ], 'generic' );
> {{alias}}( [ x, y ] )
> var i = {{alias:@stdlib/ndarray/from-scalar}}( 0, { 'dtype': 'generic' } );
> {{alias}}( [ x, y, i ] )
2

See Also
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import { typedndarray } from '@stdlib/types/ndarray';
*
* - first one-dimensional input ndarray.
* - second one-dimensional input ndarray.
* - a zero-dimensional ndarray containing the index from which to begin searching.
*
* - When comparing elements, the function checks whether an element in the first one-dimensional input ndarray is less than a corresponding element in the second one-dimensional input ndarray using the less-than operator `<`. As a consequence, comparisons involving `NaN` always evaluate to `false`.
*
Expand All @@ -39,14 +40,19 @@ import { typedndarray } from '@stdlib/types/ndarray';
*
* @example
* var vector = require( '@stdlib/ndarray/vector/ctor' );
* var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' );
*
* var x = vector( [ 0.0, 0.0, 0.0, 0.0 ], 'generic' );
* var y = vector( [ 0.0, 0.0, 1.0, 0.0 ], 'generic' );
*
* var idx = gfirstIndexLessThan( [ x, y ] );
* var fromIndex = scalar2ndarray( 0, {
* 'dtype': 'generic'
* });
*
* var idx = gfirstIndexLessThan( [ x, y, fromIndex ] );
* // returns 2
*/
declare function gfirstIndexLessThan<T = unknown>( arrays: [ typedndarray<T>, typedndarray<T> ] ): number;
declare function gfirstIndexLessThan( arrays: [ typedndarray<unknown>, typedndarray<unknown>, typedndarray<number> ] ): 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 gfirstIndexLessThan = require( './index' );


Expand All @@ -32,8 +33,11 @@ import gfirstIndexLessThan = require( './index' );
const y = zeros( [ 10 ], {
'dtype': 'generic'
});
const fromIndex = scalar2ndarray( 0, {
'dtype': 'generic'
});

gfirstIndexLessThan( [ x, y ] ); // $ExpectType number
gfirstIndexLessThan( [ x, y, fromIndex ] ); // $ExpectType number
}

// The compiler throws an error if the function is provided a first argument which is not an array of ndarrays...
Expand All @@ -57,7 +61,10 @@ import gfirstIndexLessThan = require( './index' );
const y = zeros( [ 10 ], {
'dtype': 'generic'
});
const fromIndex = scalar2ndarray( 0, {
'dtype': 'generic'
});

gfirstIndexLessThan(); // $ExpectError
gfirstIndexLessThan( [ x, y ], {} ); // $ExpectError
gfirstIndexLessThan( [ x, y, fromIndex ], {} ); // $ExpectError
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@
'use strict';

var discreteUniform = require( '@stdlib/random/discrete-uniform' );
var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' );
var ndarray2array = require( '@stdlib/ndarray/to-array' );
var ndarraylike2scalar = require( '@stdlib/ndarray/ndarraylike2scalar' );
var gfirstIndexLessThan = require( './../lib' );

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

var idx = gfirstIndexLessThan( [ x, y ] );
var fromIndex = scalar2ndarray( 0, {
'dtype': 'generic'
});
console.log( 'From Index:', ndarraylike2scalar( fromIndex ) );

var idx = gfirstIndexLessThan( [ x, y, fromIndex ] );
console.log( idx );
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,17 @@
*
* @example
* var vector = require( '@stdlib/ndarray/vector/ctor' );
* var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' );
* var gfirstIndexLessThan = require( '@stdlib/blas/ext/base/ndarray/gfirst-index-less-than' );
*
* var x = vector( [ 0.0, 0.0, 0.0, 0.0 ], 'generic' );
* var y = vector( [ 0.0, 0.0, 1.0, 0.0 ], 'generic' );
*
* var idx = gfirstIndexLessThan( [ x, y ] );
* var fromIndex = scalar2ndarray( 0, {
* 'dtype': 'generic'
* });
*
* var idx = gfirstIndexLessThan( [ x, y, fromIndex ] );
* // returns 2
*/

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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/gfirst-index-less-than' ).ndarray;


Expand All @@ -38,6 +40,7 @@ var strided = require( '@stdlib/blas/ext/base/gfirst-index-less-than' ).ndarray;
*
* - first one-dimensional input ndarray.
* - second one-dimensional input ndarray.
* - a zero-dimensional ndarray containing the index from which to begin searching.
*
* - When comparing elements, the function checks whether an element in the first one-dimensional input ndarray is less than a corresponding element in the second one-dimensional input ndarray using the less-than operator `<`. As a consequence, comparisons involving `NaN` always evaluate to `false`.
*
Expand All @@ -46,17 +49,50 @@ var strided = require( '@stdlib/blas/ext/base/gfirst-index-less-than' ).ndarray;
*
* @example
* var vector = require( '@stdlib/ndarray/vector/ctor' );
* var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' );
*
* var x = vector( [ 0.0, 0.0, 0.0, 0.0 ], 'generic' );
* var y = vector( [ 0.0, 0.0, 1.0, 0.0 ], 'generic' );
*
* var idx = gfirstIndexLessThan( [ x, y ] );
* var fromIndex = scalar2ndarray( 0, {
* 'dtype': 'generic'
* });
*
* var idx = gfirstIndexLessThan( [ x, y, fromIndex ] );
* // returns 2
*/
function gfirstIndexLessThan( arrays ) {
var x = arrays[ 0 ];
var y = arrays[ 1 ];
return strided( numelDimension( x, 0 ), getData( x ), getStride( x, 0 ), getOffset( x ), getData( y ), getStride( y, 0 ), getOffset( y ) ); // eslint-disable-line max-len
var fromIndex;
var idx;
var sx;
var sy;
var ox;
var oy;
var N;
var x;
var y;

x = arrays[ 0 ];
y = arrays[ 1 ];
fromIndex = ndarraylike2scalar( arrays[ 2 ] );

N = numelDimension( x, 0 );
fromIndex = clipIndex( fromIndex, N );
if ( fromIndex >= N ) {
return -1;
}
N -= fromIndex;

sx = getStride( x, 0 );
ox = getOffset( x ) + ( sx*fromIndex );
sy = getStride( y, 0 );
oy = getOffset( y ) + ( sy*fromIndex );

idx = strided( N, getData( x ), sx, ox, getData( y ), sy, oy );
if ( idx >= 0 ) {
idx += fromIndex;
}
return idx;
}


Expand Down
Loading