Skip to content

Commit f0df313

Browse files
dhruvilmehtakgryte
andauthored
feat: added accessor protocol support to stats/base/meanpn
PR-URL: #5908 Closes: #5650 Reviewed-by: Athan Reines <[email protected]> Signed-off-by: Dhruvil Mehta <[email protected]> Signed-off-by: Athan Reines <[email protected]> Co-authored-by: Athan Reines <[email protected]>
1 parent 3425c5a commit f0df313

File tree

12 files changed

+337
-144
lines changed

12 files changed

+337
-144
lines changed

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

Lines changed: 20 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
33
@license Apache-2.0
44
5-
Copyright (c) 2020 The Stdlib Authors.
5+
Copyright (c) 2025 The Stdlib Authors.
66
77
Licensed under the Apache License, Version 2.0 (the "License");
88
you may not use this file except in compliance with the License.
@@ -51,33 +51,28 @@ The [arithmetic mean][arithmetic-mean] is defined as
5151
var meanpn = require( '@stdlib/stats/base/meanpn' );
5252
```
5353

54-
#### meanpn( N, x, stride )
54+
#### meanpn( N, x, strideX )
5555

56-
Computes the [arithmetic mean][arithmetic-mean] of a strided array `x` using a two-pass error correction algorithm.
56+
Computes the [arithmetic mean][arithmetic-mean] of a strided array using a two-pass error correction algorithm.
5757

5858
```javascript
5959
var x = [ 1.0, -2.0, 2.0 ];
60-
var N = x.length;
6160

62-
var v = meanpn( N, x, 1 );
61+
var v = meanpn( x.length, x, 1 );
6362
// returns ~0.3333
6463
```
6564

6665
The function has the following parameters:
6766

6867
- **N**: number of indexed elements.
6968
- **x**: input [`Array`][mdn-array] or [`typed array`][mdn-typed-array].
70-
- **stride**: index increment for `x`.
69+
- **strideX**: stride length for `x`.
7170

72-
The `N` and `stride` parameters determine which elements in `x` are accessed at runtime. For example, to compute the [arithmetic mean][arithmetic-mean] of every other element in `x`,
71+
The `N` and stride parameters determine which elements in the strided array are accessed at runtime. For example, to compute the [arithmetic mean][arithmetic-mean] of every other element in the input array
7372

7473
```javascript
75-
var floor = require( '@stdlib/math/base/special/floor' );
76-
7774
var x = [ 1.0, 2.0, 2.0, -7.0, -2.0, 3.0, 4.0, 2.0 ];
78-
var N = floor( x.length / 2 );
79-
80-
var v = meanpn( N, x, 2 );
75+
var v = meanpn( 4, x, 2 );
8176
// returns 1.25
8277
```
8378

@@ -87,42 +82,35 @@ Note that indexing is relative to the first index. To introduce an offset, use [
8782

8883
```javascript
8984
var Float64Array = require( '@stdlib/array/float64' );
90-
var floor = require( '@stdlib/math/base/special/floor' );
9185

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

95-
var N = floor( x0.length / 2 );
96-
97-
var v = meanpn( N, x1, 2 );
89+
var v = meanpn( 4, x1, 2 );
9890
// returns 1.25
9991
```
10092

101-
#### meanpn.ndarray( N, x, stride, offset )
93+
#### meanpn.ndarray( N, x, strideX, offsetX )
10294

10395
Computes the [arithmetic mean][arithmetic-mean] of a strided array using a two-pass error correction algorithm and alternative indexing semantics.
10496

10597
```javascript
10698
var x = [ 1.0, -2.0, 2.0 ];
107-
var N = x.length;
10899

109-
var v = meanpn.ndarray( N, x, 1, 0 );
100+
var v = meanpn.ndarray( x.length, x, 1, 0 );
110101
// returns ~0.33333
111102
```
112103

113104
The function has the following additional parameters:
114105

115-
- **offset**: starting index for `x`.
106+
- **offsetX**: starting index for `x`.
116107

117-
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 [arithmetic mean][arithmetic-mean] for every other value in `x` starting from the second value
108+
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 [arithmetic mean][arithmetic-mean] for every other element in the strided array starting from the second element
118109

119110
```javascript
120-
var floor = require( '@stdlib/math/base/special/floor' );
121-
122111
var x = [ 2.0, 1.0, 2.0, -2.0, -2.0, 2.0, 3.0, 4.0 ];
123-
var N = floor( x.length / 2 );
124112

125-
var v = meanpn.ndarray( N, x, 2, 1 );
113+
var v = meanpn.ndarray( 4, x, 2, 1 );
126114
// returns 1.25
127115
```
128116

@@ -135,6 +123,7 @@ var v = meanpn.ndarray( N, x, 2, 1 );
135123
## Notes
136124

137125
- If `N <= 0`, both functions return `NaN`.
126+
- 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]).
138127
- Depending on the environment, the typed versions ([`dmeanpn`][@stdlib/stats/base/dmeanpn], [`smeanpn`][@stdlib/stats/base/smeanpn], etc.) are likely to be significantly more performant.
139128

140129
</section>
@@ -148,18 +137,12 @@ var v = meanpn.ndarray( N, x, 2, 1 );
148137
<!-- eslint no-undef: "error" -->
149138

150139
```javascript
151-
var randu = require( '@stdlib/random/base/randu' );
152-
var round = require( '@stdlib/math/base/special/round' );
153-
var Float64Array = require( '@stdlib/array/float64' );
140+
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
154141
var meanpn = require( '@stdlib/stats/base/meanpn' );
155142

156-
var x;
157-
var i;
158-
159-
x = new Float64Array( 10 );
160-
for ( i = 0; i < x.length; i++ ) {
161-
x[ i ] = round( (randu()*100.0) - 50.0 );
162-
}
143+
var x = discreteUniform( 10, -50, 50, {
144+
'dtype': 'float64'
145+
});
163146
console.log( x );
164147

165148
var v = meanpn( x.length, x, 1 );
@@ -210,6 +193,8 @@ console.log( v );
210193

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

196+
[@stdlib/array/base/accessor]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/array/base/accessor
197+
213198
[@neely:1966a]: https://doi.org/10.1145/365719.365958
214199

215200
[@schubert:2018a]: https://doi.org/10.1145/3221269.3223036

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

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/**
22
* @license Apache-2.0
33
*
4-
* Copyright (c) 2020 The Stdlib Authors.
4+
* Copyright (c) 2025 The Stdlib Authors.
55
*
66
* Licensed under the Apache License, Version 2.0 (the "License");
77
* you may not use this file except in compliance with the License.
@@ -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 meanpn = require( './../lib/meanpn.js' );
28+
var meanpn = require( './../lib/main.js' );
29+
30+
31+
// VARIABLES //
32+
33+
var options = {
34+
'dtype': 'generic'
35+
};
2936

3037

3138
// FUNCTIONS //
@@ -38,13 +45,7 @@ var meanpn = require( './../lib/meanpn.js' );
3845
* @returns {Function} benchmark function
3946
*/
4047
function createBenchmark( len ) {
41-
var x;
42-
var i;
43-
44-
x = [];
45-
for ( i = 0; i < len; i++ ) {
46-
x.push( ( randu()*20.0 ) - 10.0 );
47-
}
48+
var x = uniform( len, -10, 10, options );
4849
return benchmark;
4950

5051
function benchmark( b ) {

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

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11

2-
{{alias}}( N, x, stride )
2+
{{alias}}( N, x, strideX )
33
Computes the arithmetic mean of a strided array using a two-pass error
44
correction algorithm.
55

6-
The `N` and `stride` parameters determine which elements in `x` are accessed
7-
at runtime.
6+
The `N` and stride parameters determine which elements are accessed at
7+
runtime.
88

99
Indexing is relative to the first index. To introduce an offset, use a typed
1010
array view.
@@ -19,8 +19,8 @@
1919
x: Array<number>|TypedArray
2020
Input array.
2121

22-
stride: integer
23-
Index increment.
22+
strideX: integer
23+
Stride length.
2424

2525
Returns
2626
-------
@@ -36,25 +36,22 @@
3636

3737
// Using `N` and `stride` parameters:
3838
> x = [ -2.0, 1.0, 1.0, -5.0, 2.0, -1.0 ];
39-
> var N = {{alias:@stdlib/math/base/special/floor}}( x.length / 2 );
40-
> var stride = 2;
41-
> {{alias}}( N, x, stride )
39+
> {{alias}}( 3, x, 2 )
4240
~0.3333
4341

4442
// Using view offsets:
4543
> var x0 = new {{alias:@stdlib/array/float64}}( [ 1.0, -2.0, 3.0, 2.0, 5.0, -1.0 ] );
4644
> var x1 = new {{alias:@stdlib/array/float64}}( x0.buffer, x0.BYTES_PER_ELEMENT*1 );
47-
> N = {{alias:@stdlib/math/base/special/floor}}( x0.length / 2 );
48-
> stride = 2;
49-
> {{alias}}( N, x1, stride )
45+
> {{alias}}( 3, x1, 2 )
5046
~-0.3333
5147

52-
{{alias}}.ndarray( N, x, stride, offset )
48+
49+
{{alias}}.ndarray( N, x, strideX, offsetX )
5350
Computes the arithmetic mean of a strided array using a two-pass error
5451
correction algorithm and alternative indexing semantics.
5552

5653
While typed array views mandate a view offset based on the underlying
57-
buffer, the `offset` parameter supports indexing semantics based on a
54+
buffer, the offset parameter supports indexing semantics based on a
5855
starting index.
5956

6057
Parameters
@@ -65,10 +62,10 @@
6562
x: Array<number>|TypedArray
6663
Input array.
6764

68-
stride: integer
69-
Index increment.
65+
strideX: integer
66+
Stride length.
7067

71-
offset: integer
68+
offsetX: integer
7269
Starting index.
7370

7471
Returns
@@ -85,8 +82,7 @@
8582

8683
// Using offset parameter:
8784
> var x = [ 1.0, -2.0, 3.0, 2.0, 5.0, -1.0 ];
88-
> var N = {{alias:@stdlib/math/base/special/floor}}( x.length / 2 );
89-
> {{alias}}.ndarray( N, x, 2, 1 )
85+
> {{alias}}.ndarray( 3, x, 2, 1 )
9086
~-0.3333
9187

9288
See Also

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

Lines changed: 12 additions & 7 deletions
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 `meanpn`.
@@ -31,7 +36,7 @@ interface Routine {
3136
*
3237
* @param N - number of indexed elements
3338
* @param x - input array
34-
* @param stride - stride length
39+
* @param strideX - stride length
3540
* @returns arithmetic mean
3641
*
3742
* @example
@@ -40,15 +45,15 @@ interface Routine {
4045
* var v = meanpn( x.length, x, 1 );
4146
* // returns ~0.3333
4247
*/
43-
( N: number, x: NumericArray, stride: number ): number;
48+
( N: number, x: InputArray, stride: number ): number;
4449

4550
/**
4651
* Computes the arithmetic mean of a strided array using a two-pass error correction algorithm and alternative indexing semantics.
4752
*
4853
* @param N - number of indexed elements
4954
* @param x - input array
50-
* @param stride - stride length
51-
* @param offset - starting index
55+
* @param strideX - stride length
56+
* @param offsetX - starting index
5257
* @returns arithmetic mean
5358
*
5459
* @example
@@ -57,15 +62,15 @@ interface Routine {
5762
* var v = meanpn.ndarray( x.length, x, 1, 0 );
5863
* // returns ~0.3333
5964
*/
60-
ndarray( N: number, x: NumericArray, stride: number, offset: number ): number;
65+
ndarray( N: number, x: InputArray, stride: number, offsetX: number ): number;
6166
}
6267

6368
/**
6469
* Computes the arithmetic mean of a strided array using a two-pass error correction algorithm.
6570
*
6671
* @param N - number of indexed elements
6772
* @param x - input array
68-
* @param stride - stride length
73+
* @param strideX - stride length
6974
* @returns arithmetic mean
7075
*
7176
* @example

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
* @license Apache-2.0
33
*
4-
* Copyright (c) 2020 The Stdlib Authors.
4+
* Copyright (c) 2025 The Stdlib Authors.
55
*
66
* Licensed under the Apache License, Version 2.0 (the "License");
77
* you may not use this file except in compliance with the License.
@@ -16,6 +16,7 @@
1616
* limitations under the License.
1717
*/
1818

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

2122

@@ -26,6 +27,7 @@ import meanpn = require( './index' );
2627
const x = new Float64Array( 10 );
2728

2829
meanpn( x.length, x, 1 ); // $ExpectType number
30+
meanpn( x.length, new AccessorArray ( x ), 1 ); // $ExpectType number
2931
}
3032

3133
// The compiler throws an error if the function is provided a first argument which is not a number...
@@ -85,6 +87,7 @@ import meanpn = require( './index' );
8587
const x = new Float64Array( 10 );
8688

8789
meanpn.ndarray( x.length, x, 1, 0 ); // $ExpectType number
90+
meanpn.ndarray( x.length, new AccessorArray ( x ), 1, 0 ); // $ExpectType number
8891
}
8992

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

lib/node_modules/@stdlib/stats/base/meanpn/examples/index.js

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/**
22
* @license Apache-2.0
33
*
4-
* Copyright (c) 2020 The Stdlib Authors.
4+
* Copyright (c) 2025 The Stdlib Authors.
55
*
66
* Licensed under the Apache License, Version 2.0 (the "License");
77
* you may not use this file except in compliance with the License.
@@ -18,18 +18,12 @@
1818

1919
'use strict';
2020

21-
var randu = require( '@stdlib/random/base/randu' );
22-
var round = require( '@stdlib/math/base/special/round' );
23-
var Float64Array = require( '@stdlib/array/float64' );
21+
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
2422
var meanpn = require( './../lib' );
2523

26-
var x;
27-
var i;
28-
29-
x = new Float64Array( 10 );
30-
for ( i = 0; i < x.length; i++ ) {
31-
x[ i ] = round( (randu()*100.0) - 50.0 );
32-
}
24+
var x = discreteUniform( 10, -50, 50, {
25+
'dtype': 'float64'
26+
});
3327
console.log( x );
3428

3529
var v = meanpn( x.length, x, 1 );

0 commit comments

Comments
 (0)