Skip to content

Commit 578678c

Browse files
authored
feat!: add start and end parameter support to blas/ext/base/ndarray/dfill-nan
BREAKING CHANGE: add `start` and `end` support To maintain previous behavior, provide additional zero-dimensional ndarrays containing the value `0` and the number of elements, respectively. PR-URL: #14671 Reviewed-by: Athan Reines <kgryte@gmail.com> Closes: stdlib-js/metr-issue-tracker#1248
1 parent 3058b5c commit 578678c

9 files changed

Lines changed: 416 additions & 26 deletions

File tree

‎lib/node_modules/@stdlib/blas/ext/base/ndarray/dfill-nan/README.md‎

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,16 @@ var alpha = scalar2ndarray( 0.0, {
5050
'dtype': 'float64'
5151
});
5252

53-
dfillNaN( [ x, alpha ] );
54-
// x => <ndarray>[ 0.0, -2.0, 3.0, 0.0, 4.0, -6.0 ]
53+
var start = scalar2ndarray( 0, {
54+
'dtype': 'generic'
55+
});
56+
57+
var end = scalar2ndarray( 2, {
58+
'dtype': 'generic'
59+
});
60+
61+
dfillNaN( [ x, alpha, start, end ] );
62+
// x => <ndarray>[ 0.0, -2.0, 3.0, NaN, 4.0, -6.0 ]
5563
```
5664

5765
The function has the following parameters:
@@ -60,6 +68,8 @@ The function has the following parameters:
6068

6169
- a one-dimensional input ndarray.
6270
- a zero-dimensional ndarray containing the scalar constant.
71+
- a zero-dimensional ndarray containing the starting index (inclusive).
72+
- a zero-dimensional ndarray containing the ending index (exclusive).
6373

6474
</section>
6575

@@ -70,6 +80,7 @@ The function has the following parameters:
7080
## Notes
7181

7282
- The input ndarray is modified **in-place** (i.e., the input ndarray is **mutated**).
83+
- If a specified `start` or `end` index is negative, the function resolves the respective index by counting backward from the last element (where `-1` refers to the last element).
7384

7485
</section>
7586

@@ -92,13 +103,23 @@ var opts = {
92103
'dtype': 'float64'
93104
};
94105

95-
var x = nans( [ 10 ], opts );
106+
var x = nans( [ 20 ], opts );
96107
console.log( ndarray2array( x ) );
97108

98109
var alpha = scalar2ndarray( 5.0, opts );
99110
console.log( 'Alpha: %d', ndarraylike2scalar( alpha ) );
100111

101-
dfillNaN( [ x, alpha ] );
112+
var start = scalar2ndarray( 5, {
113+
'dtype': 'generic'
114+
});
115+
console.log( 'Start Index: %d', ndarraylike2scalar( start ) );
116+
117+
var end = scalar2ndarray( 15, {
118+
'dtype': 'generic'
119+
});
120+
console.log( 'End Index: %d', ndarraylike2scalar( end ) );
121+
122+
dfillNaN( [ x, alpha, start, end ] );
102123
console.log( ndarray2array( x ) );
103124
```
104125

‎lib/node_modules/@stdlib/blas/ext/base/ndarray/dfill-nan/benchmark/benchmark.js‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,10 +63,18 @@ function clbk( value, indices ) {
6363
*/
6464
function createBenchmark( len ) {
6565
var alpha;
66+
var start;
67+
var end;
6668
var x;
6769

6870
x = fillBy( zeros( [ len ], options ), clbk );
6971
alpha = scalar2ndarray( 0.0, options );
72+
start = scalar2ndarray( 0, {
73+
'dtype': 'generic'
74+
});
75+
end = scalar2ndarray( len, {
76+
'dtype': 'generic'
77+
});
7078
return benchmark;
7179

7280
/**
@@ -81,7 +89,7 @@ function createBenchmark( len ) {
8189

8290
b.tic();
8391
for ( i = 0; i < b.iterations; i++ ) {
84-
out = dfillNaN( [ x, alpha ] );
92+
out = dfillNaN( [ x, alpha, start, end ] );
8593
if ( typeof out !== 'object' ) {
8694
b.fail( 'should return an ndarray' );
8795
}

‎lib/node_modules/@stdlib/blas/ext/base/ndarray/dfill-nan/docs/repl.txt‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,19 @@
66
The input ndarray is modified *in-place* (i.e., the input ndarray is
77
*mutated*).
88

9+
If a specified `start` or `end` index is negative, the function resolves
10+
the respective index by counting backward from the last element (where `-1`
11+
refers to the last element).
12+
913
Parameters
1014
----------
1115
arrays: ArrayLikeObject<ndarray>
1216
Array-like object containing the following ndarrays:
1317

1418
- a one-dimensional input ndarray.
1519
- a zero-dimensional ndarray containing the scalar constant.
20+
- a zero-dimensional ndarray containing the starting index (inclusive).
21+
- a zero-dimensional ndarray containing the ending index (exclusive).
1622

1723
Returns
1824
-------
@@ -23,8 +29,10 @@
2329
--------
2430
> var x = new {{alias:@stdlib/ndarray/vector/float64}}( [ NaN, -2.0, 3.0, NaN, 4.0, -6.0 ] );
2531
> var alpha = {{alias:@stdlib/ndarray/from-scalar}}( 0.0, { 'dtype': 'float64' } );
26-
> {{alias}}( [ x, alpha ] )
27-
<ndarray>[ 0.0, -2.0, 3.0, 0.0, 4.0, -6.0 ]
32+
> var st = {{alias:@stdlib/ndarray/from-scalar}}( 0, { 'dtype': 'generic' } );
33+
> var en = {{alias:@stdlib/ndarray/from-scalar}}( 2, { 'dtype': 'generic' } );
34+
> {{alias}}( [ x, alpha, st, en ] )
35+
<ndarray>[ 0.0, -2.0, 3.0, NaN, 4.0, -6.0 ]
2836

2937
See Also
3038
--------

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

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ import { float64ndarray, typedndarray } from '@stdlib/types/ndarray';
3131
*
3232
* - a one-dimensional input ndarray.
3333
* - a zero-dimensional ndarray containing the scalar constant.
34+
* - a zero-dimensional ndarray containing the starting index (inclusive).
35+
* - a zero-dimensional ndarray containing the ending index (exclusive).
3436
*
3537
* @param arrays - array-like object containing ndarrays
3638
* @returns input ndarray
@@ -45,10 +47,18 @@ import { float64ndarray, typedndarray } from '@stdlib/types/ndarray';
4547
* 'dtype': 'float64'
4648
* });
4749
*
48-
* var out = dfillNaN( [ x, alpha ] );
49-
* // returns <ndarray>[ 0.0, -2.0, 3.0, 0.0, 4.0, -6.0 ]
50+
* var start = scalar2ndarray( 0, {
51+
* 'dtype': 'generic'
52+
* });
53+
*
54+
* var end = scalar2ndarray( 2, {
55+
* 'dtype': 'generic'
56+
* });
57+
*
58+
* var out = dfillNaN( [ x, alpha, start, end ] );
59+
* // returns <ndarray>[ 0.0, -2.0, 3.0, NaN, 4.0, -6.0 ]
5060
*/
51-
declare function dfillNaN( arrays: [ float64ndarray, typedndarray<number> ] ): float64ndarray;
61+
declare function dfillNaN( arrays: [ float64ndarray, typedndarray<number>, typedndarray<number>, typedndarray<number> ] ): float64ndarray;
5262

5363

5464
// EXPORTS //

‎lib/node_modules/@stdlib/blas/ext/base/ndarray/dfill-nan/docs/types/test.ts‎

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,14 @@ import dfillNaN = require( './index' );
3333
const alpha = scalar2ndarray( 0.0, {
3434
'dtype': 'float64'
3535
});
36+
const start = scalar2ndarray( 0, {
37+
'dtype': 'generic'
38+
});
39+
const end = scalar2ndarray( 10, {
40+
'dtype': 'generic'
41+
});
3642

37-
dfillNaN( [ x, alpha ] ); // $ExpectType float64ndarray
43+
dfillNaN( [ x, alpha, start, end ] ); // $ExpectType float64ndarray
3844
}
3945

4046
// The compiler throws an error if the function is provided a first argument which is not an array of ndarrays...
@@ -58,7 +64,13 @@ import dfillNaN = require( './index' );
5864
const alpha = scalar2ndarray( 0.0, {
5965
'dtype': 'float64'
6066
});
67+
const start = scalar2ndarray( 0, {
68+
'dtype': 'generic'
69+
});
70+
const end = scalar2ndarray( 10, {
71+
'dtype': 'generic'
72+
});
6173

6274
dfillNaN(); // $ExpectError
63-
dfillNaN( [ x, alpha ], {} ); // $ExpectError
75+
dfillNaN( [ x, alpha, start, end ], {} ); // $ExpectError
6476
}

‎lib/node_modules/@stdlib/blas/ext/base/ndarray/dfill-nan/examples/index.js‎

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,21 @@ var opts = {
2828
'dtype': 'float64'
2929
};
3030

31-
var x = nans( [ 10 ], opts );
31+
var x = nans( [ 20 ], opts );
3232
console.log( ndarray2array( x ) );
3333

3434
var alpha = scalar2ndarray( 5.0, opts );
3535
console.log( 'Alpha: %d', ndarraylike2scalar( alpha ) );
3636

37-
dfillNaN( [ x, alpha ] );
37+
var start = scalar2ndarray( 5, {
38+
'dtype': 'generic'
39+
});
40+
console.log( 'Start Index: %d', ndarraylike2scalar( start ) );
41+
42+
var end = scalar2ndarray( 15, {
43+
'dtype': 'generic'
44+
});
45+
console.log( 'End Index: %d', ndarraylike2scalar( end ) );
46+
47+
dfillNaN( [ x, alpha, start, end ] );
3848
console.log( ndarray2array( x ) );

‎lib/node_modules/@stdlib/blas/ext/base/ndarray/dfill-nan/lib/index.js‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,16 @@
3434
* 'dtype': 'float64'
3535
* });
3636
*
37-
* var out = dfillNaN( [ x, alpha ] );
38-
* // returns <ndarray>[ 0.0, -2.0, 3.0, 0.0, 4.0, -6.0 ]
37+
* var start = scalar2ndarray( 0, {
38+
* 'dtype': 'generic'
39+
* });
40+
*
41+
* var end = scalar2ndarray( 2, {
42+
* 'dtype': 'generic'
43+
* });
44+
*
45+
* var out = dfillNaN( [ x, alpha, start, end ] );
46+
* // returns <ndarray>[ 0.0, -2.0, 3.0, NaN, 4.0, -6.0 ]
3947
*/
4048

4149
// MODULES //

‎lib/node_modules/@stdlib/blas/ext/base/ndarray/dfill-nan/lib/main.js‎

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,12 @@
2121
// MODULES //
2222

2323
var numelDimension = require( '@stdlib/ndarray/base/numel-dimension' );
24+
var clipIndex = require( '@stdlib/ndarray/base/clip-index' );
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' );
27-
var strided = require( '@stdlib/blas/ext/base/dfill-nan' ).ndarray;
2828
var ndarraylike2scalar = require( '@stdlib/ndarray/base/ndarraylike2scalar' );
29+
var strided = require( '@stdlib/blas/ext/base/dfill-nan' ).ndarray;
2930

3031

3132
// MAIN //
@@ -39,6 +40,8 @@ var ndarraylike2scalar = require( '@stdlib/ndarray/base/ndarraylike2scalar' );
3940
*
4041
* - a one-dimensional input ndarray.
4142
* - a zero-dimensional ndarray containing the scalar constant.
43+
* - a zero-dimensional ndarray containing the starting index (inclusive).
44+
* - a zero-dimensional ndarray containing the ending index (exclusive).
4245
*
4346
* @param {ArrayLikeObject<Object>} arrays - array-like object containing ndarrays
4447
* @returns {ndarray} input ndarray
@@ -53,16 +56,39 @@ var ndarraylike2scalar = require( '@stdlib/ndarray/base/ndarraylike2scalar' );
5356
* 'dtype': 'float64'
5457
* });
5558
*
56-
* var out = dfillNaN( [ x, alpha ] );
57-
* // returns <ndarray>[ 0.0, -2.0, 3.0, 0.0, 4.0, -6.0 ]
59+
* var start = scalar2ndarray( 0, {
60+
* 'dtype': 'generic'
61+
* });
62+
*
63+
* var end = scalar2ndarray( 2, {
64+
* 'dtype': 'generic'
65+
* });
66+
*
67+
* var out = dfillNaN( [ x, alpha, start, end ] );
68+
* // returns <ndarray>[ 0.0, -2.0, 3.0, NaN, 4.0, -6.0 ]
5869
*/
5970
function dfillNaN( arrays ) {
71+
var stride;
72+
var offset;
6073
var alpha;
74+
var start;
75+
var end;
76+
var N;
6177
var x;
6278

6379
x = arrays[ 0 ];
6480
alpha = ndarraylike2scalar( arrays[ 1 ] );
65-
strided( numelDimension( x, 0 ), alpha, getData( x ), getStride( x, 0 ), getOffset( x ) ); // eslint-disable-line max-len
81+
82+
N = numelDimension( x, 0 );
83+
start = clipIndex( ndarraylike2scalar( arrays[ 2 ] ), N );
84+
end = clipIndex( ndarraylike2scalar( arrays[ 3 ] ), N );
85+
if ( start >= end ) {
86+
return x;
87+
}
88+
stride = getStride( x, 0 );
89+
offset = getOffset( x ) + ( stride*start );
90+
91+
strided( end-start, alpha, getData( x ), stride, offset );
6692
return x;
6793
}
6894

0 commit comments

Comments
 (0)