Skip to content

feat: add support for accessor arrays and refactor stats/base/mskmax #5356

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 7 commits into from
Feb 21, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 22 additions & 34 deletions lib/node_modules/@stdlib/stats/base/mskmax/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,20 +52,17 @@ The function has the following parameters:

- **N**: number of indexed elements.
- **x**: input [`Array`][mdn-array] or [`typed array`][mdn-typed-array].
- **strideX**: index increment for `x`.
- **strideX**: stride length for `x`.
- **mask**: mask [`Array`][mdn-array] or [`typed array`][mdn-typed-array]. If a `mask` array element is `0`, the corresponding element in `x` is considered valid and **included** in computation. If a `mask` array element is `1`, the corresponding element in `x` is considered invalid/missing and **excluded** from computation.
- **strideMask**: index increment for `mask`.
- **strideMask**: stride length for `mask`.

The `N` and `stride` parameters determine which elements 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 arrays 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, -7.0, -2.0, 4.0, 3.0, 5.0, 6.0 ];
var mask = [ 0, 0, 0, 0, 0, 0, 1, 1 ];
var N = floor( x.length / 2 );

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

Expand All @@ -76,17 +73,14 @@ Note that indexing is relative to the first index. To introduce offsets, use [`t
```javascript
var Float64Array = require( '@stdlib/array/float64' );
var Uint8Array = require( '@stdlib/array/uint8' );
var floor = require( '@stdlib/math/base/special/floor' );

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

var mask0 = new Uint8Array( [ 0, 0, 0, 0, 0, 0, 1, 1 ] );
var mask1 = new Uint8Array( mask0.buffer, mask0.BYTES_PER_ELEMENT*1 ); // start at 2nd element

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

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

Expand All @@ -107,16 +101,13 @@ The function has the following additional parameters:
- **offsetX**: starting index for `x`.
- **offsetMask**: starting index for `mask`.

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 parameters support indexing semantics based on starting indices. 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, 3.0, 4.0, 5.0, 6.0 ];
var mask = [ 0, 0, 0, 0, 0, 0, 1, 1 ];
var N = floor( x.length / 2 );

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

Expand All @@ -130,6 +121,8 @@ var v = mskmax.ndarray( N, x, 2, 1, mask, 2, 1 );

- If `N <= 0`, both functions return `NaN`.
- Depending on the environment, the typed versions ([`dmskmax`][@stdlib/stats/base/dmskmax], [`smskmax`][@stdlib/stats/base/smskmax], etc.) are likely to be significantly more performant.
- 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 @@ -142,27 +135,18 @@ var v = mskmax.ndarray( N, x, 2, 1, mask, 2, 1 );
<!-- eslint no-undef: "error" -->

```javascript
var randu = require( '@stdlib/random/base/randu' );
var round = require( '@stdlib/math/base/special/round' );
var Float64Array = require( '@stdlib/array/float64' );
var Uint8Array = require( '@stdlib/array/uint8' );
var uniform = require( '@stdlib/random/array/uniform' );
var bernoulli = require( '@stdlib/random/array/bernoulli' );
var mskmax = require( '@stdlib/stats/base/mskmax' );

var mask;
var x;
var i;

x = new Float64Array( 10 );
mask = new Uint8Array( x.length );
for ( i = 0; i < x.length; i++ ) {
if ( randu() < 0.2 ) {
mask[ i ] = 1;
} else {
mask[ i ] = 0;
}
x[ i ] = round( (randu()*100.0) - 50.0 );
}
var x = uniform( 10, -50.0, 50.0, {
'dtype': 'float64'
});
console.log( x );

var mask = bernoulli( x.length, 0.2, {
'dtype': 'uint8'
});
console.log( mask );

var v = mskmax( x.length, x, 1, mask, 1 );
Expand Down Expand Up @@ -197,6 +181,10 @@ console.log( v );

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


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


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

<!-- <related-links> -->
Expand Down
26 changes: 11 additions & 15 deletions lib/node_modules/@stdlib/stats/base/mskmax/benchmark/benchmark.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,21 @@
// MODULES //

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


// VARIABLES //

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


// FUNCTIONS //

/**
Expand All @@ -38,20 +46,8 @@ var mskmax = require( './../lib/mskmax.js' );
* @returns {Function} benchmark function
*/
function createBenchmark( len ) {
var mask;
var x;
var i;

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

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

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


// VARIABLES //

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


// FUNCTIONS //

/**
Expand All @@ -38,20 +46,8 @@ var mskmax = require( './../lib/ndarray.js' );
* @returns {Function} benchmark function
*/
function createBenchmark( len ) {
var mask;
var x;
var i;

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

function benchmark( b ) {
Expand Down
24 changes: 11 additions & 13 deletions lib/node_modules/@stdlib/stats/base/mskmax/docs/repl.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
{{alias}}( N, x, strideX, mask, strideMask )
Computes the maximum value of a strided array according to a mask.

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

Indexing is relative to the first index. To introduce offsets, use a typed
array views.
Expand All @@ -25,13 +25,13 @@
Input array.

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

mask: Array<number>|TypedArray
Mask array.

strideMask: integer
Index increment for `mask`.
Stride length for `mask`.

Returns
-------
Expand All @@ -46,22 +46,21 @@
> {{alias}}( x.length, x, 1, mask, 1 )
2.0

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

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


{{alias}}.ndarray( N, x, strideX, offsetX, mask, strideMask, offsetMask )
Computes the maximum value of a strided array according to a mask and using
alternative indexing semantics.
Expand All @@ -79,7 +78,7 @@
Input array.

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

offsetX: integer
Starting index for `x`.
Expand All @@ -88,7 +87,7 @@
Mask array.

strideMask: integer
Index increment for `mask`.
Stride length for `mask`.

offsetMask: integer
Starting index for `mask`.
Expand All @@ -109,8 +108,7 @@
// Using offset parameter:
> x = [ 1.0, -2.0, 3.0, 2.0, 5.0, -1.0, 4.0 ];
> mask = [ 0, 0, 0, 0, 0, 0, 1 ];
> var N = {{alias:@stdlib/math/base/special/floor}}( x.length / 2 );
> {{alias}}.ndarray( N, x, 2, 1, mask, 2, 1 )
> {{alias}}.ndarray( 3, x, 2, 1, mask, 2, 1 )
2.0

See Also
Expand Down
23 changes: 14 additions & 9 deletions lib/node_modules/@stdlib/stats/base/mskmax/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 { NumericArray } from '@stdlib/types/array';
import { NumericArray, Collection, AccessorArrayLike } from '@stdlib/types/array';

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

/**
* Interface describing `mskmax`.
Expand All @@ -31,9 +36,9 @@ interface Routine {
*
* @param N - number of indexed elements
* @param x - input array
* @param strideX - `x` stride length
* @param strideX - stride length for `x`
* @param mask - mask array
* @param strideMask - `mask` stride length
* @param strideMask - stride length for `mask`
* @returns maximum value
*
* @example
Expand All @@ -43,17 +48,17 @@ interface Routine {
* var v = mskmax( x.length, x, 1, mask, 1 );
* // returns 2.0
*/
( N: number, x: NumericArray, strideX: number, mask: NumericArray, strideMask: number ): number;
( N: number, x: InputArray, strideX: number, mask: InputArray, strideMask: number ): number;

/**
* Computes the maximum value of a strided array according to a mask and using alternative indexing semantics.
*
* @param N - number of indexed elements
* @param x - input array
* @param strideX - `x` stride length
* @param strideX - stride length for `x`
* @param offsetX - `x` starting index
* @param mask - mask array
* @param strideMask - `mask` stride length
* @param strideMask - stride length for `mask`
* @param offsetMask - `mask` starting index
* @returns maximum value
*
Expand All @@ -64,17 +69,17 @@ interface Routine {
* var v = mskmax.ndarray( x.length, x, 1, 0, mask, 1, 0 );
* // returns 2.0
*/
ndarray( N: number, x: NumericArray, strideX: number, offsetX: number, mask: NumericArray, strideMask: number, offsetMask: number ): number;
ndarray( N: number, x: InputArray, strideX: number, offsetX: number, mask: InputArray, strideMask: number, offsetMask: number ): number;
}

/**
* Computes the maximum value of a strided array according to a mask.
*
* @param N - number of indexed elements
* @param x - input array
* @param strideX - `x` stride length
* @param strideX - stride length for `x`
* @param mask - mask array
* @param strideMask - `mask` stride length
* @param strideMask - stride length for `mask`
* @returns maximum value
*
* @example
Expand Down
3 changes: 3 additions & 0 deletions lib/node_modules/@stdlib/stats/base/mskmax/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 mskmax = require( './index' );


Expand All @@ -27,6 +28,7 @@ import mskmax = require( './index' );
const mask = new Uint8Array( 10 );

mskmax( x.length, x, 1, mask, 1 ); // $ExpectType number
mskmax( x.length, new AccessorArray( x ), 1, mask, 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 @@ -123,6 +125,7 @@ import mskmax = require( './index' );
const mask = new Uint8Array( 10 );

mskmax.ndarray( x.length, x, 1, 0, mask, 1, 0 ); // $ExpectType number
mskmax.ndarray( x.length, new AccessorArray( x ), 1, 0, new AccessorArray( mask ), 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
Loading