Skip to content

Commit 417e653

Browse files
feat: add support for accessor arrays and refactor stats/base/mskmax
PR-URL: #5356 Co-authored-by: Athan Reines <[email protected]> Reviewed-by: Athan Reines <[email protected]>
1 parent 47119e4 commit 417e653

File tree

12 files changed

+560
-183
lines changed

12 files changed

+560
-183
lines changed

lib/node_modules/@stdlib/stats/base/mskmax/README.md

+22-34
Original file line numberDiff line numberDiff line change
@@ -52,20 +52,17 @@ The function has the following parameters:
5252

5353
- **N**: number of indexed elements.
5454
- **x**: input [`Array`][mdn-array] or [`typed array`][mdn-typed-array].
55-
- **strideX**: index increment for `x`.
55+
- **strideX**: stride length for `x`.
5656
- **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.
57-
- **strideMask**: index increment for `mask`.
57+
- **strideMask**: stride length for `mask`.
5858

59-
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`,
59+
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`,
6060

6161
```javascript
62-
var floor = require( '@stdlib/math/base/special/floor' );
63-
6462
var x = [ 1.0, 2.0, -7.0, -2.0, 4.0, 3.0, 5.0, 6.0 ];
6563
var mask = [ 0, 0, 0, 0, 0, 0, 1, 1 ];
66-
var N = floor( x.length / 2 );
6764

68-
var v = mskmax( N, x, 2, mask, 2 );
65+
var v = mskmax( 4, x, 2, mask, 2 );
6966
// returns 4.0
7067
```
7168

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

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

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

87-
var N = floor( x0.length / 2 );
88-
89-
var v = mskmax( N, x1, 2, mask1, 2 );
83+
var v = mskmax( 4, x1, 2, mask1, 2 );
9084
// returns 4.0
9185
```
9286

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

110-
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
104+
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
111105

112106
```javascript
113-
var floor = require( '@stdlib/math/base/special/floor' );
114-
115107
var x = [ 2.0, 1.0, -2.0, -2.0, 3.0, 4.0, 5.0, 6.0 ];
116108
var mask = [ 0, 0, 0, 0, 0, 0, 1, 1 ];
117-
var N = floor( x.length / 2 );
118109

119-
var v = mskmax.ndarray( N, x, 2, 1, mask, 2, 1 );
110+
var v = mskmax.ndarray( 4, x, 2, 1, mask, 2, 1 );
120111
// returns 4.0
121112
```
122113

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

131122
- If `N <= 0`, both functions return `NaN`.
132123
- 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.
124+
- 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]).
125+
133126

134127
</section>
135128

@@ -142,27 +135,18 @@ var v = mskmax.ndarray( N, x, 2, 1, mask, 2, 1 );
142135
<!-- eslint no-undef: "error" -->
143136

144137
```javascript
145-
var randu = require( '@stdlib/random/base/randu' );
146-
var round = require( '@stdlib/math/base/special/round' );
147-
var Float64Array = require( '@stdlib/array/float64' );
148-
var Uint8Array = require( '@stdlib/array/uint8' );
138+
var uniform = require( '@stdlib/random/array/uniform' );
139+
var bernoulli = require( '@stdlib/random/array/bernoulli' );
149140
var mskmax = require( '@stdlib/stats/base/mskmax' );
150141

151-
var mask;
152-
var x;
153-
var i;
154-
155-
x = new Float64Array( 10 );
156-
mask = new Uint8Array( x.length );
157-
for ( i = 0; i < x.length; i++ ) {
158-
if ( randu() < 0.2 ) {
159-
mask[ i ] = 1;
160-
} else {
161-
mask[ i ] = 0;
162-
}
163-
x[ i ] = round( (randu()*100.0) - 50.0 );
164-
}
142+
var x = uniform( 10, -50.0, 50.0, {
143+
'dtype': 'float64'
144+
});
165145
console.log( x );
146+
147+
var mask = bernoulli( x.length, 0.2, {
148+
'dtype': 'uint8'
149+
});
166150
console.log( mask );
167151

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

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

184+
185+
[@stdlib/array/base/accessor]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/array/base/accessor
186+
187+
200188
[mdn-typed-array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray
201189

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

lib/node_modules/@stdlib/stats/base/mskmax/benchmark/benchmark.js

+11-15
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,21 @@
2121
// MODULES //
2222

2323
var bench = require( '@stdlib/bench' );
24-
var randu = require( '@stdlib/random/base/randu' );
2524
var isnan = require( '@stdlib/math/base/assert/is-nan' );
25+
var uniform = require( '@stdlib/random/array/uniform' );
26+
var bernoulli = require( '@stdlib/random/array/bernoulli' );
2627
var pow = require( '@stdlib/math/base/special/pow' );
2728
var pkg = require( './../package.json' ).name;
2829
var mskmax = require( './../lib/mskmax.js' );
2930

3031

32+
// VARIABLES //
33+
34+
var options = {
35+
'dtype': 'generic'
36+
};
37+
38+
3139
// FUNCTIONS //
3240

3341
/**
@@ -38,20 +46,8 @@ var mskmax = require( './../lib/mskmax.js' );
3846
* @returns {Function} benchmark function
3947
*/
4048
function createBenchmark( len ) {
41-
var mask;
42-
var x;
43-
var i;
44-
45-
x = [];
46-
mask = [];
47-
for ( i = 0; i < len; i++ ) {
48-
if ( randu() < 0.2 ) {
49-
mask.push( 1 );
50-
} else {
51-
mask.push( 0 );
52-
}
53-
x.push( ( randu()*20.0 ) - 10.0 );
54-
}
49+
var mask = bernoulli( len, 0.2, options );
50+
var x = uniform( len, -10.0, 10.0, options );
5551
return benchmark;
5652

5753
function benchmark( b ) {

lib/node_modules/@stdlib/stats/base/mskmax/benchmark/benchmark.ndarray.js

+11-15
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,21 @@
2121
// MODULES //
2222

2323
var bench = require( '@stdlib/bench' );
24-
var randu = require( '@stdlib/random/base/randu' );
2524
var isnan = require( '@stdlib/math/base/assert/is-nan' );
25+
var uniform = require( '@stdlib/random/array/uniform' );
26+
var bernoulli = require( '@stdlib/random/array/bernoulli' );
2627
var pow = require( '@stdlib/math/base/special/pow' );
2728
var pkg = require( './../package.json' ).name;
2829
var mskmax = require( './../lib/ndarray.js' );
2930

3031

32+
// VARIABLES //
33+
34+
var options = {
35+
'dtype': 'generic'
36+
};
37+
38+
3139
// FUNCTIONS //
3240

3341
/**
@@ -38,20 +46,8 @@ var mskmax = require( './../lib/ndarray.js' );
3846
* @returns {Function} benchmark function
3947
*/
4048
function createBenchmark( len ) {
41-
var mask;
42-
var x;
43-
var i;
44-
45-
x = [];
46-
mask = [];
47-
for ( i = 0; i < len; i++ ) {
48-
if ( randu() < 0.2 ) {
49-
mask.push( 1 );
50-
} else {
51-
mask.push( 0 );
52-
}
53-
x.push( ( randu()*20.0 ) - 10.0 );
54-
}
49+
var mask = bernoulli( len, 0.2, options );
50+
var x = uniform( len, -10.0, 10.0, options );
5551
return benchmark;
5652

5753
function benchmark( b ) {

lib/node_modules/@stdlib/stats/base/mskmax/docs/repl.txt

+11-13
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
{{alias}}( N, x, strideX, mask, strideMask )
33
Computes the maximum value of a strided array according to a mask.
44

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

88
Indexing is relative to the first index. To introduce offsets, use a typed
99
array views.
@@ -25,13 +25,13 @@
2525
Input array.
2626

2727
strideX: integer
28-
Index increment for `x`.
28+
Stride length for `x`.
2929

3030
mask: Array<number>|TypedArray
3131
Mask array.
3232

3333
strideMask: integer
34-
Index increment for `mask`.
34+
Stride length for `mask`.
3535

3636
Returns
3737
-------
@@ -46,22 +46,21 @@
4646
> {{alias}}( x.length, x, 1, mask, 1 )
4747
2.0
4848

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

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

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

8180
strideX: integer
82-
Index increment for `x`.
81+
Stride length for `x`.
8382

8483
offsetX: integer
8584
Starting index for `x`.
@@ -88,7 +87,7 @@
8887
Mask array.
8988

9089
strideMask: integer
91-
Index increment for `mask`.
90+
Stride length for `mask`.
9291

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

116114
See Also

lib/node_modules/@stdlib/stats/base/mskmax/docs/types/index.d.ts

+14-9
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,12 @@
2020

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

23-
import { NumericArray } 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
* Interface describing `mskmax`.
@@ -31,9 +36,9 @@ interface Routine {
3136
*
3237
* @param N - number of indexed elements
3338
* @param x - input array
34-
* @param strideX - `x` stride length
39+
* @param strideX - stride length for `x`
3540
* @param mask - mask array
36-
* @param strideMask - `mask` stride length
41+
* @param strideMask - stride length for `mask`
3742
* @returns maximum value
3843
*
3944
* @example
@@ -43,17 +48,17 @@ interface Routine {
4348
* var v = mskmax( x.length, x, 1, mask, 1 );
4449
* // returns 2.0
4550
*/
46-
( N: number, x: NumericArray, strideX: number, mask: NumericArray, strideMask: number ): number;
51+
( N: number, x: InputArray, strideX: number, mask: InputArray, strideMask: number ): number;
4752

4853
/**
4954
* Computes the maximum value of a strided array according to a mask and using alternative indexing semantics.
5055
*
5156
* @param N - number of indexed elements
5257
* @param x - input array
53-
* @param strideX - `x` stride length
58+
* @param strideX - stride length for `x`
5459
* @param offsetX - `x` starting index
5560
* @param mask - mask array
56-
* @param strideMask - `mask` stride length
61+
* @param strideMask - stride length for `mask`
5762
* @param offsetMask - `mask` starting index
5863
* @returns maximum value
5964
*
@@ -64,17 +69,17 @@ interface Routine {
6469
* var v = mskmax.ndarray( x.length, x, 1, 0, mask, 1, 0 );
6570
* // returns 2.0
6671
*/
67-
ndarray( N: number, x: NumericArray, strideX: number, offsetX: number, mask: NumericArray, strideMask: number, offsetMask: number ): number;
72+
ndarray( N: number, x: InputArray, strideX: number, offsetX: number, mask: InputArray, strideMask: number, offsetMask: number ): number;
6873
}
6974

7075
/**
7176
* Computes the maximum value of a strided array according to a mask.
7277
*
7378
* @param N - number of indexed elements
7479
* @param x - input array
75-
* @param strideX - `x` stride length
80+
* @param strideX - stride length for `x`
7681
* @param mask - mask array
77-
* @param strideMask - `mask` stride length
82+
* @param strideMask - stride length for `mask`
7883
* @returns maximum value
7984
*
8085
* @example

lib/node_modules/@stdlib/stats/base/mskmax/docs/types/test.ts

+3
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 mskmax = require( './index' );
2021

2122

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

2930
mskmax( x.length, x, 1, mask, 1 ); // $ExpectType number
31+
mskmax( x.length, new AccessorArray( x ), 1, mask, 1 ); // $ExpectType number
3032
}
3133

3234
// The compiler throws an error if the function is provided a first argument which is not a number...
@@ -123,6 +125,7 @@ import mskmax = require( './index' );
123125
const mask = new Uint8Array( 10 );
124126

125127
mskmax.ndarray( x.length, x, 1, 0, mask, 1, 0 ); // $ExpectType number
128+
mskmax.ndarray( x.length, new AccessorArray( x ), 1, 0, new AccessorArray( mask ), 1, 0 ); // $ExpectType number
126129
}
127130

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

0 commit comments

Comments
 (0)