Skip to content

feat: add support for accessor arrays and refactor stats/base/maxsorted #5343

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Feb 21, 2025
43 changes: 15 additions & 28 deletions lib/node_modules/@stdlib/stats/base/maxsorted/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ limitations under the License.
var maxsorted = require( '@stdlib/stats/base/maxsorted' );
```

#### maxsorted( N, x, stride )
#### maxsorted( N, x, strideX )

Computes the maximum value of a sorted strided array `x`.

Expand All @@ -58,17 +58,14 @@ The function has the following parameters:

- **N**: number of indexed elements.
- **x**: sorted input [`Array`][mdn-array] or [`typed array`][mdn-typed-array].
- **stride**: index increment for `x`.
- **strideX**: stride length for `x`.

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

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

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

var v = maxsorted( N, x, 2 );
var v = maxsorted( 4, x, 2 );
// returns 4.0
```

Expand All @@ -78,42 +75,35 @@ Note that indexing is relative to the first index. To introduce an offset, use [

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

var x0 = new Float64Array( [ 2.0, 1.0, 2.0, 2.0, -2.0, 2.0, 3.0, 4.0 ] );
var x1 = new Float64Array( x0.buffer, x0.BYTES_PER_ELEMENT*1 ); // start at 2nd element

var N = floor( x0.length / 2 );

var v = maxsorted( N, x1, 2 );
var v = maxsorted( 4, x1, 2 );
// returns 4.0
```

#### maxsorted.ndarray( N, x, stride, offset )
#### maxsorted.ndarray( N, x, strideX, offsetX )

Computes the maximum value of a sorted strided array using alternative indexing semantics.

```javascript
var x = [ 1.0, 2.0, 3.0 ];
var N = x.length;

var v = maxsorted.ndarray( N, x, 1, 0 );
var v = maxsorted.ndarray( x.length, x, 1, 0 );
// returns 3.0
```

The function has the following additional parameters:

- **offset**: starting index for `x`.
- **offsetX**: starting index for `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 calculate the maximum value for every other value in `x` starting from the second value
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 calculate the maximum value for every other value in `x` starting from the second value

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

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

var v = maxsorted.ndarray( N, x, 2, 1 );
var v = maxsorted.ndarray( 4, x, 2, 1 );
// returns 4.0
```

Expand All @@ -127,6 +117,7 @@ var v = maxsorted.ndarray( N, x, 2, 1 );

- If `N <= 0`, both functions return `NaN`.
- The input strided array must be sorted in either **strictly** ascending or descending order.
- 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]).

</section>

Expand All @@ -139,16 +130,10 @@ var v = maxsorted.ndarray( N, x, 2, 1 );
<!-- eslint no-undef: "error" -->

```javascript
var Float64Array = require( '@stdlib/array/float64' );
var linspace = require( '@stdlib/array/base/linspace' );
var maxsorted = require( '@stdlib/stats/base/maxsorted' );

var x;
var i;

x = new Float64Array( 10 );
for ( i = 0; i < x.length; i++ ) {
x[ i ] = i - 5.0;
}
var x = linspace( -50.0, 50.0, 10 );
console.log( x );

var v = maxsorted( x.length, x, 1 );
Expand Down Expand Up @@ -184,6 +169,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/dmaxsorted]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/strided/dmaxsorted
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

var bench = require( '@stdlib/bench' );
var isnan = require( '@stdlib/math/base/assert/is-nan' );
var linspace = require( '@stdlib/array/base/linspace' );
var pow = require( '@stdlib/math/base/special/pow' );
var pkg = require( './../package.json' ).name;
var maxsorted = require( './../lib/maxsorted.js' );
Expand All @@ -37,13 +38,7 @@ var maxsorted = require( './../lib/maxsorted.js' );
* @returns {Function} benchmark function
*/
function createBenchmark( len ) {
var x;
var i;

x = [];
for ( i = 0; i < len; i++ ) {
x.push( i );
}
var x = linspace( 0.0, len, len );
return benchmark;

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

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

Expand All @@ -37,13 +38,7 @@ var maxsorted = require( './../lib/ndarray.js' );
* @returns {Function} benchmark function
*/
function createBenchmark( len ) {
var x;
var i;

x = [];
for ( i = 0; i < len; i++ ) {
x.push( i );
}
var x = linspace( 0.0, len, len );
return benchmark;

function benchmark( b ) {
Expand Down
32 changes: 14 additions & 18 deletions lib/node_modules/@stdlib/stats/base/maxsorted/docs/repl.txt
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@

{{alias}}( N, x, stride )
{{alias}}( N, x, strideX )
Computes the maximum value of a sorted strided array.

The input strided array must be sorted in either strictly ascending or
descending order.

The `N` and `stride` parameters determine which elements in `x` are accessed
at runtime.
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 a typed
array view.
Expand All @@ -21,8 +21,8 @@
x: Array<number>|TypedArray
Sorted input array.

stride: integer
Index increment.
strideX: integer
Stride length.

Returns
-------
Expand All @@ -36,22 +36,19 @@
> {{alias}}( x.length, x, 1 )
3.0

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

// Using view offsets:
> var x0 = new {{alias:@stdlib/array/float64}}( [ 1.0, -2.0, 3.0, 2.0, 5.0, 3.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 );
> stride = 2;
> {{alias}}( N, x1, stride )
> {{alias}}( 3, x1, 2 )
3.0

{{alias}}.ndarray( N, x, stride, offset )

{{alias}}.ndarray( N, x, strideX, offsetX )
Computes the maximum value of a sorted strided array using alternative
indexing semantics.

Expand All @@ -67,10 +64,10 @@
x: Array<number>|TypedArray
Sorted input array.

stride: integer
Index increment.
strideX: integer
Stride length.

offset: integer
offsetX: integer
Starting index.

Returns
Expand All @@ -87,8 +84,7 @@

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

See Also
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,12 @@

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

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

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

/**
* Interface describing `maxsorted`.
Expand All @@ -31,7 +36,7 @@ interface Routine {
*
* @param N - number of indexed elements
* @param x - sorted input array
* @param stride - stride length
* @param strideX - stride length
* @returns maximum value
*
* @example
Expand All @@ -40,15 +45,15 @@ interface Routine {
* var v = maxsorted( x.length, x, 1 );
* // returns 3.0
*/
( N: number, x: NumericArray, stride: number ): number;
( N: number, x: InputArray, strideX: number ): number;

/**
* Computes the maximum value of a sorted strided array using alternative indexing semantics.
*
* @param N - number of indexed elements
* @param x - sorted input array
* @param stride - stride length
* @param offset - starting index
* @param strideX - stride length
* @param offsetX - starting index
* @returns maximum value
*
* @example
Expand All @@ -57,15 +62,15 @@ interface Routine {
* var v = maxsorted.ndarray( x.length, x, 1, 0 );
* // returns 3.0
*/
ndarray( N: number, x: NumericArray, stride: number, offset: number ): number;
ndarray( N: number, x: InputArray, strideX: number, offsetX: number ): number;
}

/**
* Computes the maximum value of a sorted strided array.
*
* @param N - number of indexed elements
* @param x - sorted input array
* @param stride - stride length
* @param strideX - stride length
* @returns maximum value
*
* @example
Expand Down
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 maxsorted = require( './index' );


Expand All @@ -26,6 +27,7 @@ import maxsorted = require( './index' );
const x = new Float64Array( 10 );

maxsorted( x.length, x, 1 ); // $ExpectType number
maxsorted( x.length, new AccessorArray( x ), 1 ); // $ExpectType number
}

// The compiler throws an error if the function is provided a first argument which is not a number...
Expand Down Expand Up @@ -86,6 +88,7 @@ import maxsorted = require( './index' );
const x = new Float64Array( 10 );

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

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

'use strict';

var Float64Array = require( '@stdlib/array/float64' );
var linspace = require( '@stdlib/array/base/linspace' );
var maxsorted = require( './../lib' );

var x;
var i;

x = new Float64Array( 10 );
for ( i = 0; i < x.length; i++ ) {
x[ i ] = i - 5.0;
}
var x = linspace( -50.0, 50.0, 10 );
console.log( x );

var v = maxsorted( x.length, x, 1 );
Expand Down
Loading