Skip to content

Commit 5e2b384

Browse files
headlessNodekgryte
andauthored
feat!: add fromIndex support to blas/ext/base/ndarray/dfirst-index-equal
BREAKING CHANGE: add `fromIndex` support To maintain previous behavior, provide a zero-dimensional ndarray containing the value `0`. PR-URL: #14407 Co-authored-by: Athan Reines <kgryte@gmail.com> Reviewed-by: Athan Reines <kgryte@gmail.com> Signed-off-by: Athan Reines <kgryte@gmail.com> Closes: stdlib-js/metr-issue-tracker#1173
1 parent d681a2e commit 5e2b384

9 files changed

Lines changed: 257 additions & 28 deletions

File tree

‎lib/node_modules/@stdlib/blas/ext/base/ndarray/dfirst-index-equal/README.md‎

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,16 @@ Returns the index of the first element in a one-dimensional double-precision flo
4242

4343
```javascript
4444
var Float64Vector = require( '@stdlib/ndarray/vector/float64' );
45+
var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' );
4546

4647
var x = new Float64Vector( [ 1.0, 2.0, 3.0, 4.0 ] );
4748
var y = new Float64Vector( [ 0.0, 0.0, 3.0, 0.0 ] );
4849

49-
var idx = dfirstIndexEqual( [ x, y ] );
50+
var fromIndex = scalar2ndarray( 0, {
51+
'dtype': 'generic'
52+
});
53+
54+
var idx = dfirstIndexEqual( [ x, y, fromIndex ] );
5055
// returns 2
5156
```
5257

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

5762
- first one-dimensional input ndarray.
5863
- second one-dimensional input ndarray.
64+
- a zero-dimensional ndarray containing the index from which to begin searching.
5965

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

6268
```javascript
6369
var Float64Vector = require( '@stdlib/ndarray/vector/float64' );
70+
var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' );
6471

6572
var x = new Float64Vector( [ 1.0, 2.0, 3.0, 4.0 ] );
6673
var y = new Float64Vector( [ 5.0, 6.0, 7.0, 8.0 ] );
6774

68-
var idx = dfirstIndexEqual( [ x, y ] );
75+
var fromIndex = scalar2ndarray( 0, {
76+
'dtype': 'generic'
77+
});
78+
79+
var idx = dfirstIndexEqual( [ x, y, fromIndex ] );
6980
// returns -1
7081
```
7182

@@ -77,6 +88,7 @@ var idx = dfirstIndexEqual( [ x, y ] );
7788

7889
## Notes
7990

91+
- 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).
8092
- 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.
8193

8294
</section>
@@ -91,7 +103,9 @@ var idx = dfirstIndexEqual( [ x, y ] );
91103

92104
```javascript
93105
var discreteUniform = require( '@stdlib/random/discrete-uniform' );
106+
var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' );
94107
var ndarray2array = require( '@stdlib/ndarray/to-array' );
108+
var ndarraylike2scalar = require( '@stdlib/ndarray/ndarraylike2scalar' );
95109
var dfirstIndexEqual = require( '@stdlib/blas/ext/base/ndarray/dfirst-index-equal' );
96110

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

106-
var idx = dfirstIndexEqual( [ x, y ] );
120+
var fromIndex = scalar2ndarray( 0, {
121+
'dtype': 'generic'
122+
});
123+
console.log( 'From Index:', ndarraylike2scalar( fromIndex ) );
124+
125+
var idx = dfirstIndexEqual( [ x, y, fromIndex ] );
107126
console.log( idx );
108127
```
109128

‎lib/node_modules/@stdlib/blas/ext/base/ndarray/dfirst-index-equal/benchmark/benchmark.js‎

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
var bench = require( '@stdlib/bench' );
2424
var isInteger = require( '@stdlib/assert/is-integer' ).isPrimitive;
2525
var pow = require( '@stdlib/math/base/special/pow' );
26+
var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' );
2627
var oneTo = require( '@stdlib/blas/ext/one-to' );
2728
var zeros = require( '@stdlib/ndarray/zeros' );
2829
var format = require( '@stdlib/string/format' );
@@ -47,8 +48,15 @@ var options = {
4748
* @returns {Function} benchmark function
4849
*/
4950
function createBenchmark( len ) {
50-
var x = oneTo( [ len ], options );
51-
var y = zeros( [ len ], options );
51+
var fromIndex;
52+
var x;
53+
var y;
54+
55+
x = oneTo( [ len ], options );
56+
y = zeros( [ len ], options );
57+
fromIndex = scalar2ndarray( 0, {
58+
'dtype': 'generic'
59+
});
5260
return benchmark;
5361

5462
/**
@@ -63,7 +71,7 @@ function createBenchmark( len ) {
6371

6472
b.tic();
6573
for ( i = 0; i < b.iterations; i++ ) {
66-
out = dfirstIndexEqual( [ x, y ] );
74+
out = dfirstIndexEqual( [ x, y, fromIndex ] );
6775
if ( out !== out ) {
6876
b.fail( 'should return an integer' );
6977
}

‎lib/node_modules/@stdlib/blas/ext/base/ndarray/dfirst-index-equal/docs/repl.txt‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@
44
precision floating-point ndarray equal to a corresponding element in
55
another one-dimensional double-precision floating-point ndarray.
66

7+
If a specified starting search index is negative, the function resolves the
8+
starting search index by counting backward from the last element (where `-1`
9+
refers to the last element).
10+
711
When comparing elements, the function checks for equality using the strict
812
equality operator `===`. As a consequence, `NaN` values are considered
913
distinct, and `-0` and `+0` are considered the same.
@@ -15,6 +19,8 @@
1519

1620
- first one-dimensional input ndarray.
1721
- second one-dimensional input ndarray.
22+
- a zero-dimensional ndarray containing the index from which to begin
23+
searching.
1824

1925
Returns
2026
-------
@@ -25,7 +31,8 @@
2531
--------
2632
> var x = new {{alias:@stdlib/ndarray/vector/float64}}( [ 1.0, 2.0, 3.0, 4.0 ] );
2733
> var y = new {{alias:@stdlib/ndarray/vector/float64}}( [ 0.0, 0.0, 3.0, 0.0 ] );
28-
> {{alias}}( [ x, y ] )
34+
> var i = {{alias:@stdlib/ndarray/from-scalar}}( 0, { 'dtype': 'generic' } );
35+
> {{alias}}( [ x, y, i ] )
2936
2
3037

3138
See Also

‎lib/node_modules/@stdlib/blas/ext/base/ndarray/dfirst-index-equal/docs/types/index.d.ts‎

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020

2121
/// <reference types="@stdlib/types"/>
2222

23-
import { float64ndarray } from '@stdlib/types/ndarray';
23+
import { float64ndarray, typedndarray } from '@stdlib/types/ndarray';
2424

2525
/**
2626
* Returns the index of the first element in a one-dimensional double-precision floating-point ndarray equal to a corresponding element in another one-dimensional double-precision floating-point ndarray.
@@ -31,6 +31,7 @@ import { float64ndarray } from '@stdlib/types/ndarray';
3131
*
3232
* - first one-dimensional input ndarray.
3333
* - second one-dimensional input ndarray.
34+
* - a zero-dimensional ndarray containing the index from which to begin searching.
3435
*
3536
* - 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.
3637
*
@@ -39,14 +40,19 @@ import { float64ndarray } from '@stdlib/types/ndarray';
3940
*
4041
* @example
4142
* var Float64Vector = require( '@stdlib/ndarray/vector/float64' );
43+
* var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' );
4244
*
4345
* var x = new Float64Vector( [ 1.0, 2.0, 3.0, 4.0 ] );
4446
* var y = new Float64Vector( [ 0.0, 0.0, 3.0, 0.0 ] );
4547
*
46-
* var idx = dfirstIndexEqual( [ x, y ] );
48+
* var fromIndex = scalar2ndarray( 0, {
49+
* 'dtype': 'generic'
50+
* });
51+
*
52+
* var idx = dfirstIndexEqual( [ x, y, fromIndex ] );
4753
* // returns 2
4854
*/
49-
declare function dfirstIndexEqual( arrays: [ float64ndarray, float64ndarray ] ): number;
55+
declare function dfirstIndexEqual( arrays: [ float64ndarray, float64ndarray, typedndarray<number> ] ): number;
5056

5157

5258
// EXPORTS //

‎lib/node_modules/@stdlib/blas/ext/base/ndarray/dfirst-index-equal/docs/types/test.ts‎

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
/* eslint-disable space-in-parens */
2020

2121
import zeros = require( '@stdlib/ndarray/zeros' );
22+
import scalar2ndarray = require( '@stdlib/ndarray/from-scalar' );
2223
import dfirstIndexEqual = require( './index' );
2324

2425

@@ -32,8 +33,11 @@ import dfirstIndexEqual = require( './index' );
3233
const y = zeros( [ 10 ], {
3334
'dtype': 'float64'
3435
});
36+
const fromIndex = scalar2ndarray( 0, {
37+
'dtype': 'generic'
38+
});
3539

36-
dfirstIndexEqual( [ x, y ] ); // $ExpectType number
40+
dfirstIndexEqual( [ x, y, fromIndex ] ); // $ExpectType number
3741
}
3842

3943
// The compiler throws an error if the function is provided a first argument which is not an array of ndarrays...
@@ -57,7 +61,10 @@ import dfirstIndexEqual = require( './index' );
5761
const y = zeros( [ 10 ], {
5862
'dtype': 'float64'
5963
});
64+
const fromIndex = scalar2ndarray( 0, {
65+
'dtype': 'generic'
66+
});
6067

6168
dfirstIndexEqual(); // $ExpectError
62-
dfirstIndexEqual( [ x, y ], {} ); // $ExpectError
69+
dfirstIndexEqual( [ x, y, fromIndex ], {} ); // $ExpectError
6370
}

‎lib/node_modules/@stdlib/blas/ext/base/ndarray/dfirst-index-equal/examples/index.js‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@
1919
'use strict';
2020

2121
var discreteUniform = require( '@stdlib/random/discrete-uniform' );
22+
var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' );
2223
var ndarray2array = require( '@stdlib/ndarray/to-array' );
24+
var ndarraylike2scalar = require( '@stdlib/ndarray/ndarraylike2scalar' );
2325
var dfirstIndexEqual = require( './../lib' );
2426

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

34-
var idx = dfirstIndexEqual( [ x, y ] );
36+
var fromIndex = scalar2ndarray( 0, {
37+
'dtype': 'generic'
38+
});
39+
console.log( 'From Index:', ndarraylike2scalar( fromIndex ) );
40+
41+
var idx = dfirstIndexEqual( [ x, y, fromIndex ] );
3542
console.log( idx );

‎lib/node_modules/@stdlib/blas/ext/base/ndarray/dfirst-index-equal/lib/index.js‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,17 @@
2525
*
2626
* @example
2727
* var Float64Vector = require( '@stdlib/ndarray/vector/float64' );
28+
* var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' );
2829
* var dfirstIndexEqual = require( '@stdlib/blas/ext/base/ndarray/dfirst-index-equal' );
2930
*
3031
* var x = new Float64Vector( [ 1.0, 2.0, 3.0, 4.0 ] );
3132
* var y = new Float64Vector( [ 0.0, 0.0, 3.0, 0.0 ] );
3233
*
33-
* var idx = dfirstIndexEqual( [ x, y ] );
34+
* var fromIndex = scalar2ndarray( 0, {
35+
* 'dtype': 'generic'
36+
* });
37+
*
38+
* var idx = dfirstIndexEqual( [ x, y, fromIndex ] );
3439
* // returns 2
3540
*/
3641

‎lib/node_modules/@stdlib/blas/ext/base/ndarray/dfirst-index-equal/lib/main.js‎

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,12 @@
2020

2121
// MODULES //
2222

23+
var ndarraylike2scalar = require( '@stdlib/ndarray/base/ndarraylike2scalar' );
2324
var numelDimension = require( '@stdlib/ndarray/base/numel-dimension' );
2425
var getStride = require( '@stdlib/ndarray/base/stride' );
2526
var getOffset = require( '@stdlib/ndarray/base/offset' );
2627
var getData = require( '@stdlib/ndarray/base/data-buffer' );
28+
var clipIndex = require( '@stdlib/ndarray/base/clip-index' );
2729
var strided = require( '@stdlib/blas/ext/base/dfirst-index-equal' ).ndarray;
2830

2931

@@ -38,6 +40,7 @@ var strided = require( '@stdlib/blas/ext/base/dfirst-index-equal' ).ndarray;
3840
*
3941
* - first one-dimensional input ndarray.
4042
* - second one-dimensional input ndarray.
43+
* - a zero-dimensional ndarray containing the index from which to begin searching.
4144
*
4245
* - 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.
4346
*
@@ -46,17 +49,50 @@ var strided = require( '@stdlib/blas/ext/base/dfirst-index-equal' ).ndarray;
4649
*
4750
* @example
4851
* var Float64Vector = require( '@stdlib/ndarray/vector/float64' );
52+
* var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' );
4953
*
5054
* var x = new Float64Vector( [ 1.0, 2.0, 3.0, 4.0 ] );
5155
* var y = new Float64Vector( [ 0.0, 0.0, 3.0, 0.0 ] );
5256
*
53-
* var idx = dfirstIndexEqual( [ x, y ] );
57+
* var fromIndex = scalar2ndarray( 0, {
58+
* 'dtype': 'generic'
59+
* });
60+
*
61+
* var idx = dfirstIndexEqual( [ x, y, fromIndex ] );
5462
* // returns 2
5563
*/
5664
function dfirstIndexEqual( arrays ) {
57-
var x = arrays[ 0 ];
58-
var y = arrays[ 1 ];
59-
return strided( numelDimension( x, 0 ), getData( x ), getStride( x, 0 ), getOffset( x ), getData( y ), getStride( y, 0 ), getOffset( y ) ); // eslint-disable-line max-len
65+
var fromIndex;
66+
var idx;
67+
var sx;
68+
var sy;
69+
var ox;
70+
var oy;
71+
var N;
72+
var x;
73+
var y;
74+
75+
x = arrays[ 0 ];
76+
y = arrays[ 1 ];
77+
fromIndex = ndarraylike2scalar( arrays[ 2 ] );
78+
79+
N = numelDimension( x, 0 );
80+
fromIndex = clipIndex( fromIndex, N );
81+
if ( fromIndex >= N ) {
82+
return -1;
83+
}
84+
N -= fromIndex;
85+
86+
sx = getStride( x, 0 );
87+
ox = getOffset( x ) + ( sx*fromIndex );
88+
sy = getStride( y, 0 );
89+
oy = getOffset( y ) + ( sy*fromIndex );
90+
91+
idx = strided( N, getData( x ), sx, ox, getData( y ), sy, oy );
92+
if ( idx >= 0 ) {
93+
idx += fromIndex;
94+
}
95+
return idx;
6096
}
6197

6298

0 commit comments

Comments
 (0)