Skip to content

Commit 7d64bcb

Browse files
gururaj1512kgryte
andauthored
feat: add support for accessor arrays and refactor stats/base/max-by
PR-URL: #7129 Co-authored-by: Athan Reines <kgryte@gmail.com> Reviewed-by: Athan Reines <kgryte@gmail.com>
1 parent 4f41317 commit 7d64bcb

File tree

13 files changed

+512
-131
lines changed

13 files changed

+512
-131
lines changed

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

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

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

35-
Calculates the maximum value of strided array `x` via a callback function.
35+
Computes the maximum value of a strided array via a callback function.
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 for `x`.
5353
- **clbk**: callback function.
5454
- **thisArg**: execution context (_optional_).
5555

@@ -81,27 +81,23 @@ var cnt = context.count;
8181
// returns 8
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 ];
94-
var N = floor( x.length / 2 );
9592

96-
var v = maxBy( N, x, 2, accessor );
93+
var v = maxBy( 4, x, 2, accessor );
9794
// returns 8.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 = maxBy( N, x1, 2, accessor );
113+
var v = maxBy( 3, x1, 2, accessor );
119114
// returns -4.0
120115
```
121116

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

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

126121
```javascript
127122
function accessor( v ) {
@@ -136,9 +131,9 @@ var v = maxBy.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 ) {
@@ -162,6 +157,7 @@ var v = maxBy.ndarray( 3, x, 1, x.length-3, accessor );
162157
- If `N <= 0`, both functions return `NaN`.
163158
- A provided callback function should return a numeric value.
164159
- If a provided callback function does not return any value (or equivalently, explicitly returns `undefined`), the value is **ignored**.
160+
- 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]).
165161
- When possible, prefer using [`dmax`][@stdlib/stats/strided/dmax], [`smax`][@stdlib/stats/strided/smax], and/or [`max`][@stdlib/stats/base/max], as, depending on the environment, these interfaces are likely to be significantly more performant.
166162

167163
</section>
@@ -175,15 +171,16 @@ var v = maxBy.ndarray( 3, x, 1, x.length-3, accessor );
175171
<!-- eslint no-undef: "error" -->
176172

177173
```javascript
178-
var discreteUniform = require( '@stdlib/random/base/discrete-uniform' ).factory;
179-
var filledarrayBy = require( '@stdlib/array/filled-by' );
174+
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
180175
var maxBy = require( '@stdlib/stats/base/max-by' );
181176

182177
function accessor( v ) {
183178
return v * 2.0;
184179
}
185180

186-
var x = filledarrayBy( 10, 'float64', discreteUniform( -50, 50 ) );
181+
var x = discreteUniform( 10, -50, 50, {
182+
'dtype': 'float64'
183+
});
187184
console.log( x );
188185

189186
var v = maxBy( x.length, x, 1, accessor );
@@ -220,6 +217,8 @@ console.log( v );
220217

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

220+
[@stdlib/array/base/accessor]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/array/base/accessor
221+
223222
<!-- <related-links> -->
224223

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

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

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

2323
var bench = require( '@stdlib/bench' );
24-
var randu = require( '@stdlib/random/base/randu' );
24+
var uniform = require( '@stdlib/random/array/uniform' );
2525
var isnan = require( '@stdlib/math/base/assert/is-nan' );
2626
var pow = require( '@stdlib/math/base/special/pow' );
2727
var pkg = require( './../package.json' ).name;
28-
var maxBy = require( './../lib/max_by.js' );
28+
var maxBy = require( './../lib/main.js' );
29+
30+
31+
// VARIABLES //
32+
33+
var options = {
34+
'dtype': 'generic'
35+
};
2936

3037

3138
// FUNCTIONS //
@@ -49,13 +56,7 @@ function accessor( value ) {
4956
* @returns {Function} benchmark function
5057
*/
5158
function createBenchmark( len ) {
52-
var x;
53-
var i;
54-
55-
x = [];
56-
for ( i = 0; i < len; i++ ) {
57-
x.push( ( randu()*20.0 ) - 10.0 );
58-
}
59+
var x = uniform( len, -10, 10, options );
5960
return benchmark;
6061

6162
function benchmark( b ) {

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

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

2323
var bench = require( '@stdlib/bench' );
24-
var randu = require( '@stdlib/random/base/randu' );
24+
var uniform = require( '@stdlib/random/array/uniform' );
2525
var isnan = require( '@stdlib/math/base/assert/is-nan' );
2626
var pow = require( '@stdlib/math/base/special/pow' );
2727
var pkg = require( './../package.json' ).name;
2828
var maxBy = require( './../lib/ndarray.js' );
2929

3030

31+
// VARIABLES //
32+
33+
var options = {
34+
'dtype': 'generic'
35+
};
36+
37+
3138
// FUNCTIONS //
3239

3340
/**
@@ -49,13 +56,7 @@ function accessor( value ) {
4956
* @returns {Function} benchmark function
5057
*/
5158
function createBenchmark( len ) {
52-
var x;
53-
var i;
54-
55-
x = [];
56-
for ( i = 0; i < len; i++ ) {
57-
x.push( ( randu()*20.0 ) - 10.0 );
58-
}
59+
var x = uniform( len, -10, 10, options );
5960
return benchmark;
6061

6162
function benchmark( b ) {

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

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11

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

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

88
Indexing is relative to the first index. To introduce an offset, use typed
99
array views.
@@ -31,8 +31,8 @@
3131
Input array/collection. If provided an object, the object must be array-
3232
like (excluding strings and functions).
3333

34-
stride: integer
35-
Index increment for `x`.
34+
strideX: integer
35+
Stride length.
3636

3737
clbk: Function
3838
Callback function.
@@ -53,25 +53,24 @@
5353
> {{alias}}( x.length, x, 1, accessor )
5454
8.0
5555

56-
// Using `N` and `stride` parameters:
56+
// Using `N` and stride parameters:
5757
> x = [ -2.0, 1.0, 3.0, -5.0, 4.0, -1.0, -3.0 ];
58-
> var N = {{alias:@stdlib/math/base/special/floor}}( x.length / 2 );
59-
> {{alias}}( N, x, 2, accessor )
58+
> {{alias}}( 3, x, 2, accessor )
6059
8.0
6160

6261
// Using view offsets:
6362
> var x0 = new {{alias:@stdlib/array/float64}}( [ 1.0, -2.0, 3.0, -4.0, 5.0, -6.0 ] );
6463
> var x1 = new {{alias:@stdlib/array/float64}}( x0.buffer, x0.BYTES_PER_ELEMENT*1 );
65-
> N = {{alias:@stdlib/math/base/special/floor}}( x0.length / 2 );
66-
> {{alias}}( N, x1, 2, accessor )
64+
> {{alias}}( 3, x1, 2, accessor )
6765
-4.0
6866

69-
{{alias}}.ndarray( N, x, stride, offset, clbk[, thisArg] )
67+
68+
{{alias}}.ndarray( N, x, strideX, offsetX, clbk[, thisArg] )
7069
Calculates the maximum value of a strided array via a callback function and
7170
using alternative indexing semantics.
7271

7372
While typed array views mandate a view offset based on the underlying
74-
buffer, the `offset` parameter supports indexing semantics based on a
73+
buffer, the offset parameter supports indexing semantics based on a
7574
starting index.
7675

7776
Parameters
@@ -83,11 +82,11 @@
8382
Input array/collection. If provided an object, the object must be array-
8483
like (excluding strings and functions).
8584

86-
stride: integer
87-
Index increment for `x`.
85+
strideX: integer
86+
Stride length.
8887

89-
offset: integer
90-
Starting index of `x`.
88+
offsetX: integer
89+
Starting index.
9190

9291
clbk: Function
9392
Callback function.
@@ -110,8 +109,7 @@
110109

111110
// Using an index offset:
112111
> x = [ 1.0, -2.0, 3.0, -4.0, 5.0, -6.0 ];
113-
> var N = {{alias:@stdlib/math/base/special/floor}}( x.length / 2 );
114-
> {{alias}}.ndarray( N, x, 2, 1, accessor )
112+
> {{alias}}.ndarray( 3, x, 2, 1, accessor )
115113
-4.0
116114

117115
See Also

lib/node_modules/@stdlib/stats/base/max-by/docs/types/index.d.ts

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

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

23-
import { Collection } from '@stdlib/types/array';
23+
import { NumericArray, Collection, AccessorArrayLike } from '@stdlib/types/array';
24+
25+
/**
26+
* Input array.
27+
*/
28+
type InputArray = NumericArray | Collection<number> | AccessorArrayLike<number>;
2429

2530
/**
2631
* Returns an accessed value.
@@ -83,7 +88,7 @@ type Callback<T, U> = Nullary<U> | Unary<T, U> | Binary<T, U> | Ternary<T, U> |
8388
*/
8489
interface Routine {
8590
/**
86-
* Calculates the maximum value of a strided array via a callback function.
91+
* Computes the maximum value of a strided array via a callback function.
8792
*
8893
* ## Notes
8994
*
@@ -98,7 +103,7 @@ interface Routine {
98103
*
99104
* @param N - number of indexed elements
100105
* @param x - input array
101-
* @param stride - stride length
106+
* @param strideX - stride length
102107
* @param clbk - callback
103108
* @param thisArg - execution context
104109
* @returns maximum value
@@ -113,10 +118,10 @@ interface Routine {
113118
* var v = maxBy( x.length, x, 1, accessor );
114119
* // returns 8.0
115120
*/
116-
<T = unknown, U = unknown>( N: number, x: Collection<T>, stride: number, clbk: Callback<T, U>, thisArg?: ThisParameterType<Callback<T, U>> ): number;
121+
<T = unknown, U = unknown>( N: number, x: InputArray, strideX: number, clbk: Callback<T, U>, thisArg?: ThisParameterType<Callback<T, U>> ): number;
117122

118123
/**
119-
* Calculates the maximum value of a strided array via a callback function and using alternative indexing semantics.
124+
* Computes the maximum value of a strided array via a callback function and using alternative indexing semantics.
120125
*
121126
* ## Notes
122127
*
@@ -131,8 +136,8 @@ interface Routine {
131136
*
132137
* @param N - number of indexed elements
133138
* @param x - input array
134-
* @param stride - stride length
135-
* @param offset - starting index
139+
* @param strideX - stride length
140+
* @param offsetX - starting index
136141
* @param clbk - callback
137142
* @param thisArg - execution context
138143
* @returns maximum value
@@ -147,11 +152,11 @@ interface Routine {
147152
* var v = maxBy.ndarray( x.length, x, 1, 0, accessor );
148153
* // returns 8.0
149154
*/
150-
ndarray<T = unknown, U = unknown>( N: number, x: Collection<T>, stride: number, offset: number, clbk: Callback<T, U>, thisArg?: ThisParameterType<Callback<T, U>> ): number;
155+
ndarray<T = unknown, U = unknown>( N: number, x: InputArray, strideX: number, offsetX: number, clbk: Callback<T, U>, thisArg?: ThisParameterType<Callback<T, U>> ): number;
151156
}
152157

153158
/**
154-
* Calculates the maximum value of a strided array via a callback function.
159+
* Computes the maximum value of a strided array via a callback function
155160
*
156161
* ## Notes
157162
*
@@ -166,7 +171,7 @@ interface Routine {
166171
*
167172
* @param N - number of indexed elements
168173
* @param x - input array
169-
* @param stride - stride length
174+
* @param strideX - stride length
170175
* @param clbk - callback
171176
* @param thisArg - execution context
172177
* @returns maximum value

lib/node_modules/@stdlib/stats/base/max-by/docs/types/test.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
* limitations under the License.
1717
*/
1818

19+
import AccessorArray = require( '@stdlib/array/base/accessor' );
1920
import maxBy = require( './index' );
2021

2122
const accessor = (): number => {
@@ -30,7 +31,10 @@ const accessor = (): number => {
3031
const x = new Float64Array( 10 );
3132

3233
maxBy( x.length, x, 1, accessor ); // $ExpectType number
34+
maxBy( x.length, new AccessorArray( x ), 1, accessor ); // $ExpectType number
35+
3336
maxBy( x.length, x, 1, accessor, {} ); // $ExpectType number
37+
maxBy( x.length, new AccessorArray( x ), 1, accessor, {} ); // $ExpectType number
3438
}
3539

3640
// The compiler throws an error if the function is provided a first argument which is not a number...
@@ -70,7 +74,7 @@ const accessor = (): number => {
7074
maxBy( x.length, x, undefined, accessor ); // $ExpectError
7175
maxBy( x.length, x, [], accessor ); // $ExpectError
7276
maxBy( x.length, x, {}, accessor ); // $ExpectError
73-
maxBy( x.length, x, ( x: number, accessor ): number => x, accessor ); // $ExpectError
77+
maxBy( x.length, x, ( x: number ): number => x, accessor ); // $ExpectError
7478
}
7579

7680
// The compiler throws an error if the function is provided a fourth argument which is not a function...
@@ -102,7 +106,10 @@ const accessor = (): number => {
102106
const x = new Float64Array( 10 );
103107

104108
maxBy.ndarray( x.length, x, 1, 0, accessor ); // $ExpectType number
109+
maxBy.ndarray( x.length, new AccessorArray( x ), 1, 0, accessor ); // $ExpectType number
110+
105111
maxBy.ndarray( x.length, x, 1, 0, accessor, {} ); // $ExpectType number
112+
maxBy.ndarray( x.length, new AccessorArray( x ), 1, 0, accessor, {} ); // $ExpectType number
106113
}
107114

108115
// The compiler throws an error if the `ndarray` method is provided a first argument which is not a number...

0 commit comments

Comments
 (0)