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,12 +41,17 @@ var glastIndexEqual = require( '@stdlib/blas/ext/base/ndarray/glast-index-equal'
Returns the index of the last element in a one-dimensional ndarray equal to a corresponding element in another one-dimensional ndarray.

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

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

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

var idx = glastIndexEqual( [ 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 matching elements, the function returns `-1`.

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

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

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

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

Expand All @@ -77,6 +88,7 @@ var idx = glastIndexEqual( [ 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 for equality using the strict equality operator `===`. As a consequence, `NaN` values are considered distinct, and `-0` and `+0` are considered the same.

</section>
Expand All @@ -91,6 +103,7 @@ var idx = glastIndexEqual( [ 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 glastIndexEqual = require( '@stdlib/blas/ext/base/ndarray/glast-index-equal' );

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

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

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@
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 zeros = require( '@stdlib/array/zeros' );
var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' );
var ones = require( '@stdlib/ndarray/ones' );
var zeros = require( '@stdlib/ndarray/zeros' );
var format = require( '@stdlib/string/format' );
var pkg = require( './../package.json' ).name;
var glastIndexEqual = require( './../lib' );
Expand All @@ -48,8 +48,15 @@ var options = {
* @returns {Function} benchmark function
*/
function createBenchmark( len ) {
var x = vector( ones( len, options.dtype ), options.dtype );
var y = vector( zeros( len, options.dtype ), options.dtype );
var fromIndex;
var x;
var y;

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

/**
Expand All @@ -64,7 +71,7 @@ function createBenchmark( len ) {

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
out = glastIndexEqual( [ x, y ] );
out = glastIndexEqual( [ 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 last element in a one-dimensional ndarray equal to
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 for equality using the strict
equality operator `===`. As a consequence, `NaN` values are considered
distinct, and `-0` and `+0` are considered the same.
Expand All @@ -14,6 +18,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 @@ -24,7 +30,8 @@
--------
> var x = {{alias:@stdlib/ndarray/vector/ctor}}( [ 1.0, 2.0, 3.0, 4.0 ], 'generic' );
> var y = {{alias:@stdlib/ndarray/vector/ctor}}( [ 0.0, 0.0, 3.0, 0.0 ], 'generic' );
> {{alias}}( [ x, y ] )
> var i = {{alias:@stdlib/ndarray/from-scalar}}( 3, { 'dtype': 'generic' } );
> {{alias}}( [ x, y, i ] )
2

See Also
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,22 +31,28 @@ 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 for equality using the strict equality operator `===`. As a consequence, `NaN` values are considered distinct, and `-0` and `+0` are considered the same.
*
* @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( [ 1.0, 2.0, 3.0, 4.0 ], 'generic' );
* var y = vector( [ 0.0, 0.0, 3.0, 0.0 ], 'generic' );
*
* var idx = glastIndexEqual( [ x, y ] );
* var fromIndex = scalar2ndarray( 3, {
* 'dtype': 'generic'
* });
*
* var idx = glastIndexEqual( [ x, y, fromIndex ] );
* // returns 2
*/
declare function glastIndexEqual( arrays: [ typedndarray<unknown>, typedndarray<unknown> ] ): number;
declare function glastIndexEqual( 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 glastIndexEqual = require( './index' );


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

glastIndexEqual( [ x, y ] ); // $ExpectType number
glastIndexEqual( [ 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 glastIndexEqual = require( './index' );
const y = zeros( [ 10 ], {
'dtype': 'generic'
});
const fromIndex = scalar2ndarray( 0, {
'dtype': 'generic'
});

glastIndexEqual(); // $ExpectError
glastIndexEqual( [ x, y ], {} ); // $ExpectError
glastIndexEqual( [ x, y, fromIndex ], {} ); // $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 glastIndexEqual = require( './../lib' );

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

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

var idx = glastIndexEqual( [ x, y, fromIndex ] );
console.log( idx );
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,18 @@
* @module @stdlib/blas/ext/base/ndarray/glast-index-equal
*
* @example
* var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' );
* var vector = require( '@stdlib/ndarray/vector/ctor' );
* var glastIndexEqual = require( '@stdlib/blas/ext/base/ndarray/glast-index-equal' );
*
* var x = vector( [ 1.0, 2.0, 3.0, 4.0 ], 'generic' );
* var y = vector( [ 0.0, 0.0, 3.0, 0.0 ], 'generic' );
*
* var idx = glastIndexEqual( [ x, y ] );
* var fromIndex = scalar2ndarray( 3, {
* 'dtype': 'generic'
* });
*
* var idx = glastIndexEqual( [ x, y, fromIndex ] );
* // returns 2
*/

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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' );
Expand All @@ -38,25 +40,43 @@ var strided = require( '@stdlib/blas/ext/base/glast-index-equal' ).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 for equality using the strict equality operator `===`. As a consequence, `NaN` values are considered distinct, and `-0` and `+0` are considered the same.
*
* @param {ArrayLikeObject<Object>} 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( [ 1.0, 2.0, 3.0, 4.0 ], 'generic' );
* var y = vector( [ 0.0, 0.0, 3.0, 0.0 ], 'generic' );
*
* var idx = glastIndexEqual( [ x, y ] );
* var fromIndex = scalar2ndarray( 3, {
* 'dtype': 'generic'
* });
*
* var idx = glastIndexEqual( [ x, y, fromIndex ] );
* // returns 2
*/
function glastIndexEqual( 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 N;
var x;
var y;

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

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 ), getData( y ), getStride( y, 0 ), getOffset( y ) ); // eslint-disable-line max-len
}


Expand Down
Loading