Skip to content
24 changes: 11 additions & 13 deletions lib/node_modules/@stdlib/stats/base/max-by/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ limitations under the License.
var maxBy = require( '@stdlib/stats/base/max-by' );
```

#### maxBy( N, x, stride, clbk\[, thisArg] )
#### maxBy( N, x, strideX, clbk\[, thisArg] )

Calculates the maximum value of strided array `x` via a callback function.

Expand All @@ -49,7 +49,7 @@ The function has the following parameters:

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

Expand Down Expand Up @@ -81,27 +81,23 @@ var cnt = context.count;
// returns 8
```

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

```javascript
var floor = require( '@stdlib/math/base/special/floor' );

function accessor( v ) {
return v * 2.0;
}

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

var v = maxBy( N, x, 2, accessor );
var v = maxBy( 4, x, 2, accessor );
// returns 8.0
```

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

```javascript
var Float64Array = require( '@stdlib/array/float64' );
var floor = require( '@stdlib/math/base/special/floor' );

function accessor( v ) {
return v * 2.0;
Expand All @@ -112,14 +108,13 @@ var x0 = new Float64Array( [ 1.0, -2.0, 3.0, -4.0, 5.0, -6.0 ] );

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

// Access every other element...
var v = maxBy( N, x1, 2, accessor );
var v = maxBy( 3, x1, 2, accessor );
// returns -4.0
```

#### maxBy.ndarray( N, x, stride, offset, clbk\[, thisArg] )
#### maxBy.ndarray( N, x, strideX, offsetX, clbk\[, thisArg] )

Calculates the maximum value of strided array `x` via a callback function and using alternative indexing semantics.

Expand All @@ -136,9 +131,9 @@ var v = maxBy.ndarray( x.length, x, 1, 0, accessor );

The function has the following additional parameters:

- **offset**: starting index.
- **offsetX**: starting index.

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`
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 the strided array

```javascript
function accessor( v ) {
Expand All @@ -160,6 +155,7 @@ var v = maxBy.ndarray( 3, x, 1, x.length-3, accessor );
## Notes

- If `N <= 0`, both functions return `NaN`.
- 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]).
- A provided callback function should return a numeric value.
- If a provided callback function does not return any value (or equivalently, explicitly returns `undefined`), the value is **ignored**.
- When possible, prefer using [`dmax`][@stdlib/stats/strided/dmax], [`smax`][@stdlib/stats/base/smax], and/or [`max`][@stdlib/stats/base/max], as, depending on the environment, these interfaces are likely to be significantly more performant.
Expand Down Expand Up @@ -220,6 +216,8 @@ console.log( v );

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

[@stdlib/array/base/accessor]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/array/base/accessor

<!-- <related-links> -->

[@stdlib/stats/strided/dmax]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/strided/dmax
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,20 @@
// MODULES //

var bench = require( '@stdlib/bench' );
var randu = require( '@stdlib/random/base/randu' );
var uniform = require( '@stdlib/random/array/uniform' );
var isnan = require( '@stdlib/math/base/assert/is-nan' );
var pow = require( '@stdlib/math/base/special/pow' );
var pkg = require( './../package.json' ).name;
var maxBy = require( './../lib/max_by.js' );


// VARIABLES //

var options = {
'dtype': 'generic'
};


// FUNCTIONS //

/**
Expand All @@ -49,13 +56,7 @@ function accessor( value ) {
* @returns {Function} benchmark function
*/
function createBenchmark( len ) {
var x;
var i;

x = [];
for ( i = 0; i < len; i++ ) {
x.push( ( randu()*20.0 ) - 10.0 );
}
var x = uniform( len, -10, 10, options );
return benchmark;

function benchmark( b ) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,20 @@
// MODULES //

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


// VARIABLES //

var options = {
'dtype': 'generic'
};


// FUNCTIONS //

/**
Expand All @@ -49,13 +56,7 @@ function accessor( value ) {
* @returns {Function} benchmark function
*/
function createBenchmark( len ) {
var x;
var i;

x = [];
for ( i = 0; i < len; i++ ) {
x.push( ( randu()*20.0 ) - 10.0 );
}
var x = uniform( len, -10, 10, options );
return benchmark;

function benchmark( b ) {
Expand Down
27 changes: 12 additions & 15 deletions lib/node_modules/@stdlib/stats/base/max-by/docs/repl.txt
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@

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

The `N` and `stride` parameters determine which elements in `x` are accessed
The `N` and stride parameters determine which elements in the strided array are accessed
at runtime.

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

stride: integer
Index increment for `x`.
strideX: integer
Stride length for `x`.

clbk: Function
Callback function.
Expand All @@ -53,20 +53,18 @@
> {{alias}}( x.length, x, 1, accessor )
8.0

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

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

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

Expand All @@ -83,10 +81,10 @@
Input array/collection. If provided an object, the object must be array-
like (excluding strings and functions).

stride: integer
Index increment for `x`.
strideX: integer
Stride length for `x`.

offset: integer
offsetX: integer
Starting index of `x`.

clbk: Function
Expand All @@ -110,8 +108,7 @@

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

See Also
Expand Down
15 changes: 10 additions & 5 deletions lib/node_modules/@stdlib/stats/base/max-by/docs/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,12 @@

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

import { Collection } from '@stdlib/types/array';
import { NumericArray, Collection, AccessorArrayLike } from '@stdlib/types/array';

/**
* Input array.
*/
type InputArray = NumericArray | Collection<number> | AccessorArrayLike<number>;

/**
* Returns an accessed value.
Expand Down Expand Up @@ -131,8 +136,8 @@ interface Routine {
*
* @param N - number of indexed elements
* @param x - input array
* @param stride - stride length
* @param offset - starting index
* @param strideX - stride length
* @param offsetX - starting index
* @param clbk - callback
* @param thisArg - execution context
* @returns maximum value
Expand All @@ -147,7 +152,7 @@ interface Routine {
* var v = maxBy.ndarray( x.length, x, 1, 0, accessor );
* // returns 8.0
*/
ndarray<T = unknown, U = unknown>( N: number, x: Collection<T>, stride: number, offset: number, clbk: Callback<T, U>, thisArg?: ThisParameterType<Callback<T, U>> ): number;
ndarray<T = unknown, U = unknown>( N: number, x: Collection<T>, strideX: number, offsetX: number, clbk: Callback<T, U>, thisArg?: ThisParameterType<Callback<T, U>> ): number;
}

/**
Expand All @@ -166,7 +171,7 @@ interface Routine {
*
* @param N - number of indexed elements
* @param x - input array
* @param stride - stride length
* @param strideX - stride length
* @param clbk - callback
* @param thisArg - execution context
* @returns maximum value
Expand Down
3 changes: 3 additions & 0 deletions lib/node_modules/@stdlib/stats/base/max-by/docs/types/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
* limitations under the License.
*/

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

const accessor = (): number => {
Expand All @@ -31,6 +32,7 @@ const accessor = (): number => {

maxBy( x.length, x, 1, accessor ); // $ExpectType number
maxBy( x.length, x, 1, accessor, {} ); // $ExpectType number
maxBy( x.length, new AccessorArray( x ), 1, accessor, {} ); // $ExpectType number
}

// The compiler throws an error if the function is provided a first argument which is not a number...
Expand Down Expand Up @@ -103,6 +105,7 @@ const accessor = (): number => {

maxBy.ndarray( x.length, x, 1, 0, accessor ); // $ExpectType number
maxBy.ndarray( x.length, x, 1, 0, accessor, {} ); // $ExpectType number
maxBy.ndarray( x.length, new AccessorArray( x ), 1, 0, accessor, {} ); // $ExpectType number
}

// The compiler throws an error if the `ndarray` method is provided a first argument which is not a number...
Expand Down
13 changes: 10 additions & 3 deletions lib/node_modules/@stdlib/stats/base/max-by/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,18 @@

// MODULES //

var maxBy = require( './main.js' );
var setReadOnly = require( '@stdlib/utils/define-nonenumerable-read-only-property' );
var main = require( './main.js' );
var ndarray = require( './ndarray.js' );


// MAIN //

setReadOnly( main, 'ndarray', ndarray );


// EXPORTS //

module.exports = maxBy;
module.exports = main;

// exports: { "ndarray": "maxBy.ndarray" }
// exports: { "ndarray": "main.ndarray" }

Check failure on line 67 in lib/node_modules/@stdlib/stats/base/max-by/lib/index.js

View workflow job for this annotation

GitHub Actions / Lint Changed Files

Newline required at end of file but not found
30 changes: 24 additions & 6 deletions lib/node_modules/@stdlib/stats/base/max-by/lib/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,34 @@

// MODULES //

var setReadOnly = require( '@stdlib/utils/define-nonenumerable-read-only-property' );
var maxBy = require( './max_by.js' );
var stride2offset = require( '@stdlib/strided/base/stride2offset' );
var ndarray = require( './ndarray.js' );


// MAIN //

setReadOnly( maxBy, 'ndarray', ndarray );


// EXPORTS //
/**
* Calculates the maximum value of a strided array via a callback function.
*
* @param {PositiveInteger} N - number of indexed elements
* @param {NumericArray} x - input array
* @param {integer} strideX - stride length
* @param {Callback} clbk - callback function
* @param {*} [thisArg] - execution context
* @returns {number} maximum value
*
* @example
* var x = [ -2.0, 1.0, 3.0, -5.0, 4.0, 0.0, -1.0, -3.0 ];
*
* function accessor( v ) {
* return v * 2.0;
* }
*
* var v = maxBy( x.length, x, 1, accessor );
* // returns 8.0
*/
function maxBy( N, x, strideX, clbk, thisArg ) {
return ndarray( N, x, strideX, stride2offset( N, strideX ), clbk, thisArg );
}

module.exports = maxBy;
Loading
Loading