Skip to content

Commit ae34f8c

Browse files
Kaushikgtmkgrytestdlib-botgururaj1512
authored
feat: refactor and add protocol support to stats/base/nanmin-by
PR-URL: #6464 Closes: #5660 Co-authored-by: Athan Reines <[email protected]> Reviewed-by: Athan Reines <[email protected]> Co-authored-by: stdlib-bot <[email protected]> Co-authored-by: Gururaj Gurram <[email protected]>
1 parent 309db06 commit ae34f8c

File tree

13 files changed

+537
-144
lines changed

13 files changed

+537
-144
lines changed

lib/node_modules/@stdlib/stats/base/nanmin-by/README.md

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,9 @@ limitations under the License.
3030
var nanminBy = require( '@stdlib/stats/base/nanmin-by' );
3131
```
3232

33-
#### nanminBy( N, x, stride, clbk\[, thisArg] )
33+
#### nanminBy( N, x, strideX, clbk\[, thisArg] )
3434

35-
Calculates the minimum value of strided array `x` via a callback function, ignoring `NaN` values.
35+
Computes the minimum value of a strided array via a callback function, ignoring `NaN` values.
3636

3737
```javascript
3838
function accessor( v ) {
@@ -49,7 +49,7 @@ The function has the following parameters:
4949

5050
- **N**: number of indexed elements.
5151
- **x**: input [`Array`][mdn-array], [`typed array`][mdn-typed-array], or an array-like object (excluding strings and functions).
52-
- **stride**: index increment.
52+
- **strideX**: stride length.
5353
- **clbk**: callback function.
5454
- **thisArg**: execution context (_optional_).
5555

@@ -81,27 +81,23 @@ var cnt = context.count;
8181
// returns 10
8282
```
8383

84-
The `N` and `stride` parameters determine which elements in `x` are accessed at runtime. For example, to access every other element
84+
The `N` and stride parameters determine which elements in the strided array are accessed at runtime. For example, to access every other element
8585

8686
```javascript
87-
var floor = require( '@stdlib/math/base/special/floor' );
88-
8987
function accessor( v ) {
9088
return v * 2.0;
9189
}
9290

9391
var x = [ -2.0, 1.0, 3.0, -5.0, 4.0, 0.0, -1.0, -3.0, NaN, NaN ];
94-
var N = floor( x.length / 2 );
9592

96-
var v = nanminBy( N, x, 2, accessor );
93+
var v = nanminBy( 5, x, 2, accessor );
9794
// returns -4.0
9895
```
9996

10097
Note that indexing is relative to the first index. To introduce an offset, use [`typed array`][mdn-typed-array] views.
10198

10299
```javascript
103100
var Float64Array = require( '@stdlib/array/float64' );
104-
var floor = require( '@stdlib/math/base/special/floor' );
105101

106102
function accessor( v ) {
107103
return v * 2.0;
@@ -112,16 +108,15 @@ var x0 = new Float64Array( [ 1.0, -2.0, 3.0, -4.0, 5.0, -6.0 ] );
112108

113109
// Create an offset view...
114110
var x1 = new Float64Array( x0.buffer, x0.BYTES_PER_ELEMENT*1 ); // start at 2nd element
115-
var N = floor( x0.length/2 );
116111

117112
// Access every other element...
118-
var v = nanminBy( N, x1, 2, accessor );
113+
var v = nanminBy( 3, x1, 2, accessor );
119114
// returns -12.0
120115
```
121116

122-
#### nanminBy.ndarray( N, x, stride, offset, clbk\[, thisArg] )
117+
#### nanminBy.ndarray( N, x, strideX, offsetX, clbk\[, thisArg] )
123118

124-
Calculates the minimum value of strided array `x` via a callback function, ignoring `NaN` values and using alternative indexing semantics.
119+
Computes the minimum value of a strided array via a callback function, ignoring `NaN` values and using alternative indexing semantics.
125120

126121
```javascript
127122
function accessor( v ) {
@@ -136,9 +131,9 @@ var v = nanminBy.ndarray( x.length, x, 1, 0, accessor );
136131

137132
The function has the following additional parameters:
138133

139-
- **offset**: starting index.
134+
- **offsetX**: starting index.
140135

141-
While [`typed array`][mdn-typed-array] views mandate a view offset based on the underlying `buffer`, the `offset` parameter supports indexing semantics based on a starting index. For example, to access only the last three elements of `x`
136+
While [`typed array`][mdn-typed-array] views mandate a view offset based on the underlying buffer, the offset parameter supports indexing semantics based on a starting index. For example, to access only the last three elements of `x`
142137

143138
```javascript
144139
function accessor( v ) {
@@ -163,6 +158,7 @@ var v = nanminBy.ndarray( 3, x, 1, x.length-3, accessor );
163158
- A provided callback function should return a numeric value.
164159
- If a provided callback function returns `NaN`, the value is ignored.
165160
- If a provided callback function does not return any value (or equivalently, explicitly returns `undefined`), the value is ignored.
161+
- Both functions support array-like objects having getter and setter accessors for array element access (e.g., [`@stdlib/array/base/accessor`][@stdlib/array/base/accessor]).
166162
- When possible, prefer using [`dnanmin`][@stdlib/stats/strided/dnanmin], [`snanmin`][@stdlib/stats/strided/snanmin], and/or [`nanmin`][@stdlib/stats/base/nanmin], as, depending on the environment, these interfaces are likely to be significantly more performant.
167163

168164
</section>
@@ -176,23 +172,23 @@ var v = nanminBy.ndarray( 3, x, 1, x.length-3, accessor );
176172
<!-- eslint no-undef: "error" -->
177173

178174
```javascript
179-
var discreteUniform = require( '@stdlib/random/base/discrete-uniform' );
180-
var randu = require( '@stdlib/random/base/randu' );
175+
var uniform = require( '@stdlib/random/base/uniform' );
181176
var filledarrayBy = require( '@stdlib/array/filled-by' );
177+
var bernoulli = require( '@stdlib/random/base/bernoulli' );
182178
var nanminBy = require( '@stdlib/stats/base/nanmin-by' );
183179

184-
function fill() {
185-
if ( randu() < 0.2 ) {
180+
function rand() {
181+
if ( bernoulli( 0.8 )< 0.2 ) {
186182
return NaN;
187183
}
188-
return discreteUniform( -50, 50 );
184+
return uniform( -50, 50 );
189185
}
190186

191187
function accessor( v ) {
192188
return v * 2.0;
193189
}
194190

195-
var x = filledarrayBy( 10, 'float64', fill );
191+
var x = filledarrayBy( 10, 'float64', rand );
196192
console.log( x );
197193

198194
var v = nanminBy( x.length, x, 1, accessor );
@@ -229,6 +225,8 @@ console.log( v );
229225

230226
[mdn-typed-array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray
231227

228+
[@stdlib/array/base/accessor]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/array/base/accessor
229+
232230
<!-- <related-links> -->
233231

234232
[@stdlib/stats/strided/dnanmin]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/strided/dnanmin

lib/node_modules/@stdlib/stats/base/nanmin-by/benchmark/benchmark.js

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

2323
var bench = require( '@stdlib/bench' );
24-
var randu = require( '@stdlib/random/base/randu' );
24+
var uniform = require( '@stdlib/random/base/uniform' );
25+
var filledarrayBy = require( '@stdlib/array/filled-by' );
26+
var bernoulli = require( '@stdlib/random/base/bernoulli' );
2527
var isnan = require( '@stdlib/math/base/assert/is-nan' );
2628
var pow = require( '@stdlib/math/base/special/pow' );
2729
var pkg = require( './../package.json' ).name;
28-
var nanminBy = require( './../lib/nanmin_by.js' );
30+
var nanminBy = require( './../lib/main.js' );
2931

3032

3133
// FUNCTIONS //
@@ -41,6 +43,19 @@ function accessor( value ) {
4143
return value * 2.0;
4244
}
4345

46+
/**
47+
* Returns a random number.
48+
*
49+
* @private
50+
* @returns {number} random number
51+
*/
52+
function rand() {
53+
if ( bernoulli( 0.8 ) < 1 ) {
54+
return NaN;
55+
}
56+
return uniform( -50.0, 50.0 );
57+
}
58+
4459
/**
4560
* Create a benchmark function.
4661
*
@@ -49,17 +64,7 @@ function accessor( value ) {
4964
* @returns {Function} benchmark function
5065
*/
5166
function createBenchmark( len ) {
52-
var x;
53-
var i;
54-
55-
x = [];
56-
for ( i = 0; i < len; i++ ) {
57-
if ( randu() < 0.2 ) {
58-
x.push( NaN );
59-
} else {
60-
x.push( ( randu()*20.0 ) - 10.0 );
61-
}
62-
}
67+
var x = filledarrayBy( len, 'generic', rand );
6368
return benchmark;
6469

6570
function benchmark( b ) {

lib/node_modules/@stdlib/stats/base/nanmin-by/benchmark/benchmark.ndarray.js

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@
2121
// MODULES //
2222

2323
var bench = require( '@stdlib/bench' );
24-
var randu = require( '@stdlib/random/base/randu' );
24+
var uniform = require( '@stdlib/random/base/uniform' );
25+
var filledarrayBy = require( '@stdlib/array/filled-by' );
26+
var bernoulli = require( '@stdlib/random/base/bernoulli' );
2527
var isnan = require( '@stdlib/math/base/assert/is-nan' );
2628
var pow = require( '@stdlib/math/base/special/pow' );
2729
var pkg = require( './../package.json' ).name;
@@ -41,6 +43,19 @@ function accessor( value ) {
4143
return value * 2.0;
4244
}
4345

46+
/**
47+
* Returns a random number.
48+
*
49+
* @private
50+
* @returns {number} random number
51+
*/
52+
function rand() {
53+
if ( bernoulli( 0.8 ) < 1 ) {
54+
return NaN;
55+
}
56+
return uniform( -50.0, 50.0 );
57+
}
58+
4459
/**
4560
* Create a benchmark function.
4661
*
@@ -49,17 +64,7 @@ function accessor( value ) {
4964
* @returns {Function} benchmark function
5065
*/
5166
function createBenchmark( len ) {
52-
var x;
53-
var i;
54-
55-
x = [];
56-
for ( i = 0; i < len; i++ ) {
57-
if ( randu() < 0.2 ) {
58-
x.push( NaN );
59-
} else {
60-
x.push( ( randu()*20.0 ) - 10.0 );
61-
}
62-
}
67+
var x = filledarrayBy( len, 'generic', rand );
6368
return benchmark;
6469

6570
function benchmark( b ) {

lib/node_modules/@stdlib/stats/base/nanmin-by/docs/repl.txt

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11

2-
{{alias}}( N, x, stride, clbk[, thisArg] )
3-
Calculates the minimum value of a strided array via a callback function,
2+
{{alias}}( N, x, strideX, clbk[, thisArg] )
3+
Computes the minimum value of a strided array via a callback function,
44
ignoring `NaN` values.
55

6-
The `N` and `stride` parameters determine which elements in `x` are accessed
7-
at runtime.
6+
The `N` and stride parameters determine which elements in the strided array
7+
are accessed at runtime.
88

99
Indexing is relative to the first index. To introduce an offset, use typed
1010
array views.
1111

12-
If `N <= 0`, the function returns `x` unchanged.
12+
If `N <= 0`, the function returns `NaN`.
1313

1414
The callback function is provided four arguments:
1515

@@ -34,8 +34,8 @@
3434
Input array/collection. If provided an object, the object must be array-
3535
like (excluding strings and functions).
3636

37-
stride: integer
38-
Index increment for `x`.
37+
strideX: integer
38+
Stride length.
3939

4040
clbk: Function
4141
Callback function.
@@ -56,25 +56,24 @@
5656
> {{alias}}( x.length, x, 1, accessor )
5757
-10.0
5858

59-
// Using `N` and `stride` parameters:
59+
// Using `N` and stride parameters:
6060
> x = [ -2.0, 1.0, 3.0, -5.0, 4.0, -1.0, NaN, -3.0 ];
61-
> var N = {{alias:@stdlib/math/base/special/floor}}( x.length / 2 );
62-
> {{alias}}( N, x, 2, accessor )
61+
> {{alias}}( 4, x, 2, accessor )
6362
-4.0
6463

6564
// Using view offsets:
6665
> var x0 = new {{alias:@stdlib/array/float64}}( [ 1.0, -2.0, 3.0, -4.0, 5.0, -6.0 ] );
6766
> var x1 = new {{alias:@stdlib/array/float64}}( x0.buffer, x0.BYTES_PER_ELEMENT*1 );
68-
> N = {{alias:@stdlib/math/base/special/floor}}( x0.length / 2 );
69-
> {{alias}}( N, x1, 2, accessor )
67+
> {{alias}}( 3, x1, 2, accessor )
7068
-12.0
7169

72-
{{alias}}.ndarray( N, x, stride, offset, clbk[, thisArg] )
73-
Calculates the minimum value of a strided array via a callback function,
70+
71+
{{alias}}.ndarray( N, x, strideX, offsetX, clbk[, thisArg] )
72+
Computes the minimum value of a strided array via a callback function,
7473
ignoring `NaN` values and using alternative indexing semantics.
7574

7675
While typed array views mandate a view offset based on the underlying
77-
buffer, the `offset` parameter supports indexing semantics based on a
76+
buffer, the offset parameter supports indexing semantics based on a
7877
starting index.
7978

8079
Parameters
@@ -86,10 +85,10 @@
8685
Input array/collection. If provided an object, the object must be array-
8786
like (excluding strings and functions).
8887

89-
stride: integer
90-
Index increment for `x`.
88+
strideX: integer
89+
Stride length.
9190

92-
offset: integer
91+
offsetX: integer
9392
Starting index of `x`.
9493

9594
clbk: Function
@@ -113,8 +112,7 @@
113112

114113
// Using an index offset:
115114
> x = [ 1.0, -2.0, 3.0, -4.0, 5.0, -6.0 ];
116-
> var N = {{alias:@stdlib/math/base/special/floor}}( x.length / 2 );
117-
> {{alias}}.ndarray( N, x, 2, 1, accessor )
115+
> {{alias}}.ndarray( 3, x, 2, 1, accessor )
118116
-12.0
119117

120118
See Also

0 commit comments

Comments
 (0)