Skip to content

Commit 4048215

Browse files
feat: refactored and added protocol support to stats/base/nanmeanwd
--- type: pre_commit_static_analysis_report description: Results of running static analysis checks when committing changes. report: - task: lint_filenames status: passed - task: lint_editorconfig status: passed - task: lint_markdown status: passed - task: lint_package_json status: na - task: lint_repl_help status: passed - task: lint_javascript_src status: passed - task: lint_javascript_cli status: na - task: lint_javascript_examples status: passed - task: lint_javascript_tests status: passed - task: lint_javascript_benchmarks status: passed - task: lint_python status: na - task: lint_r status: na - task: lint_c_src status: na - task: lint_c_examples status: na - task: lint_c_benchmarks status: na - task: lint_c_tests_fixtures status: na - task: lint_shell status: na - task: lint_typescript_declarations status: passed - task: lint_typescript_tests status: passed - task: lint_license_headers status: passed ---
1 parent d20e133 commit 4048215

File tree

13 files changed

+366
-152
lines changed

13 files changed

+366
-152
lines changed

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

Lines changed: 24 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ The [arithmetic mean][arithmetic-mean] is defined as
5151
var nanmeanwd = require( '@stdlib/stats/base/nanmeanwd' );
5252
```
5353

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

5656
Computes the [arithmetic mean][arithmetic-mean] of a strided array `x`, ignoring `NaN` values and using Welford's algorithm.
5757

@@ -67,38 +67,32 @@ The function has the following parameters:
6767

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

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`,
72+
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 `x`,
7373

7474
```javascript
75-
var floor = require( '@stdlib/math/base/special/floor' );
76-
7775
var x = [ 1.0, 2.0, 2.0, -7.0, -2.0, 3.0, 4.0, 2.0, NaN ];
78-
var N = floor( x.length / 2 );
7976

80-
var v = nanmeanwd( N, x, 2 );
77+
var v = nanmeanwd( 5, x, 2 );
8178
// returns 1.25
8279
```
8380

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

86-
<!-- eslint-disable stdlib/capitalized-comments -->
83+
<!-- eslint-disable stdlib/capitalized-comments, max-len -->
8784

8885
```javascript
8986
var Float64Array = require( '@stdlib/array/float64' );
90-
var floor = require( '@stdlib/math/base/special/floor' );
9187

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

95-
var N = floor( x0.length / 2 );
96-
97-
var v = nanmeanwd( N, x1, 2 );
91+
var v = nanmeanwd( 5, x1, 2 );
9892
// returns 1.25
9993
```
10094

101-
#### nanmeanwd.ndarray( N, x, stride, offset )
95+
#### nanmeanwd.ndarray( N, x, strideX, offsetX )
10296

10397
Computes the [arithmetic mean][arithmetic-mean] of a strided array, ignoring `NaN` values and using Welford's algorithm and alternative indexing semantics.
10498

@@ -112,17 +106,16 @@ var v = nanmeanwd.ndarray( N, x, 1, 0 );
112106

113107
The function has the following additional parameters:
114108

115-
- **offset**: starting index for `x`.
109+
- **offsetX**: starting index for `x`.
116110

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
111+
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
118112

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

122-
var x = [ 2.0, 1.0, 2.0, -2.0, -2.0, 2.0, 3.0, 4.0, NaN ];
123-
var N = floor( x.length / 2 );
116+
var x = [ 2.0, 1.0, 2.0, -2.0, -2.0, 2.0, 3.0, 4.0, NaN, NaN ];
124117

125-
var v = nanmeanwd.ndarray( N, x, 2, 1 );
118+
var v = nanmeanwd.ndarray( 5, x, 2, 1 );
126119
// returns 1.25
127120
```
128121

@@ -135,6 +128,7 @@ var v = nanmeanwd.ndarray( N, x, 2, 1 );
135128
## Notes
136129

137130
- If `N <= 0`, both functions return `NaN`.
131+
- - 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]).
138132
- If every indexed element is `NaN`, both functions return `NaN`.
139133
- Depending on the environment, the typed versions ([`dnanmeanwd`][@stdlib/stats/base/dnanmeanwd], [`snanmeanwd`][@stdlib/stats/base/snanmeanwd], etc.) are likely to be significantly more performant.
140134

@@ -149,22 +143,19 @@ var v = nanmeanwd.ndarray( N, x, 2, 1 );
149143
<!-- eslint no-undef: "error" -->
150144

151145
```javascript
152-
var randu = require( '@stdlib/random/base/randu' );
153-
var round = require( '@stdlib/math/base/special/round' );
154-
var Float64Array = require( '@stdlib/array/float64' );
146+
var uniform = require( '@stdlib/random/base/uniform' );
147+
var filledarrayBy = require( '@stdlib/array/filled-by' );
155148
var nanmeanwd = require( '@stdlib/stats/base/nanmeanwd' );
149+
var bernoulli = require( '@stdlib/random/base/bernoulli' );
156150

157-
var x;
158-
var i;
159-
160-
x = new Float64Array( 10 );
161-
for ( i = 0; i < x.length; i++ ) {
162-
if ( randu() < 0.2 ) {
163-
x[ i ] = NaN;
164-
} else {
165-
x[ i ] = round( (randu()*100.0) - 50.0 );
151+
function rand() {
152+
if ( bernoulli( 0.8 ) < 1 ) {
153+
return NaN;
166154
}
155+
return uniform( -50.0, 50.0 );
167156
}
157+
158+
var x = filledarrayBy( 10, 'float64', rand );
168159
console.log( x );
169160

170161
var v = nanmeanwd( x.length, x, 1 );
@@ -215,6 +206,8 @@ console.log( v );
215206

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

209+
[@stdlib/array/base/accessor]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/array/base/accessor
210+
218211
[@welford:1962a]: https://doi.org/10.1080/00401706.1962.10490022
219212

220213
[@vanreeken:1968a]: https://doi.org/10.1145/362929.362961

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

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,30 @@
2121
// MODULES //
2222

2323
var bench = require( '@stdlib/bench' );
24-
var randu = require( '@stdlib/random/base/randu' );
24+
var uniform = require( '@stdlib/random/base/uniform' );
25+
var bernoulli = require( '@stdlib/random/base/bernoulli' );
26+
var filledarrayBy = require( '@stdlib/array/filled-by' );
2527
var isnan = require( '@stdlib/math/base/assert/is-nan' );
2628
var pow = require( '@stdlib/math/base/special/pow' );
2729
var pkg = require( './../package.json' ).name;
28-
var nanmeanwd = require( './../lib/nanmeanwd.js' );
30+
var nanmeanwd = require( './../lib/main.js' );
2931

3032

3133
// FUNCTIONS //
3234

35+
/**
36+
* Returns a random value or `NaN`.
37+
*
38+
* @private
39+
* @returns {number} random number or `NaN`
40+
*/
41+
function rand() {
42+
if ( bernoulli( 0.8 ) < 1 ) {
43+
return NaN;
44+
}
45+
return uniform( -10.0, 10.0 );
46+
}
47+
3348
/**
3449
* Creates a benchmark function.
3550
*
@@ -38,17 +53,7 @@ var nanmeanwd = require( './../lib/nanmeanwd.js' );
3853
* @returns {Function} benchmark function
3954
*/
4055
function createBenchmark( len ) {
41-
var x;
42-
var i;
43-
44-
x = [];
45-
for ( i = 0; i < len; i++ ) {
46-
if ( randu() < 0.2 ) {
47-
x.push( NaN );
48-
} else {
49-
x.push( ( randu()*20.0 ) - 10.0 );
50-
}
51-
}
56+
var x = filledarrayBy( len, 'float64', rand );
5257
return benchmark;
5358

5459
function benchmark( b ) {

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

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@
2121
// MODULES //
2222

2323
var bench = require( '@stdlib/bench' );
24-
var randu = require( '@stdlib/random/base/randu' );
24+
var uniform = require( '@stdlib/random/base/uniform' );
25+
var bernoulli = require( '@stdlib/random/base/bernoulli' );
26+
var filledarrayBy = require( '@stdlib/array/filled-by' );
2527
var isnan = require( '@stdlib/math/base/assert/is-nan' );
2628
var pow = require( '@stdlib/math/base/special/pow' );
2729
var pkg = require( './../package.json' ).name;
@@ -30,6 +32,19 @@ var nanmeanwd = require( './../lib/ndarray.js' );
3032

3133
// FUNCTIONS //
3234

35+
/**
36+
* Returns a random value or `NaN`.
37+
*
38+
* @private
39+
* @returns {number} random number or `NaN`
40+
*/
41+
function rand() {
42+
if ( bernoulli( 0.8 ) < 1 ) {
43+
return NaN;
44+
}
45+
return uniform( -10.0, 10.0 );
46+
}
47+
3348
/**
3449
* Creates a benchmark function.
3550
*
@@ -38,17 +53,7 @@ var nanmeanwd = require( './../lib/ndarray.js' );
3853
* @returns {Function} benchmark function
3954
*/
4055
function createBenchmark( len ) {
41-
var x;
42-
var i;
43-
44-
x = [];
45-
for ( i = 0; i < len; i++ ) {
46-
if ( randu() < 0.2 ) {
47-
x.push( NaN );
48-
} else {
49-
x.push( ( randu()*20.0 ) - 10.0 );
50-
}
51-
}
56+
var x = filledarrayBy( len, 'float64', rand );
5257
return benchmark;
5358

5459
function benchmark( b ) {

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

Lines changed: 15 additions & 19 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, ignoring `NaN` values and
44
using Welford's 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
7+
in the strided array are accessed at runtime.
88

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

24-
stride: integer
25-
Index increment.
24+
strideX: integer
25+
Stride length.
2626

2727
Returns
2828
-------
@@ -38,20 +38,17 @@
3838

3939
// Using `N` and `stride` parameters:
4040
> x = [ -2.0, 1.0, 1.0, -5.0, 2.0, -1.0, NaN ];
41-
> var N = {{alias:@stdlib/math/base/special/floor}}( x.length / 2 );
42-
> var stride = 2;
43-
> {{alias}}( N, x, stride )
41+
> {{alias}}( 4, x, 2 )
4442
~0.3333
4543

4644
// Using view offsets:
47-
> var x0 = new {{alias:@stdlib/array/float64}}( [ 1.0, -2.0, 3.0, 2.0, 5.0, -1.0, NaN ] );
45+
> var x0 = new {{alias:@stdlib/array/float64}}( [ 1.0, -2.0, 3.0, 2.0, 5.0, -1.0, NaN, NaN ] );
4846
> var x1 = new {{alias:@stdlib/array/float64}}( x0.buffer, x0.BYTES_PER_ELEMENT*1 );
49-
> N = {{alias:@stdlib/math/base/special/floor}}( x0.length / 2 );
50-
> stride = 2;
51-
> {{alias}}( N, x1, stride )
47+
> {{alias}}( 4, x1, 2 )
5248
~-0.3333
5349

54-
{{alias}}.ndarray( N, x, stride, offset )
50+
51+
{{alias}}.ndarray( N, x, strideX, offsetX )
5552
Computes the arithmetic mean of a strided array, ignoring `NaN` values and
5653
using Welford's algorithm and alternative indexing semantics.
5754

@@ -67,10 +64,10 @@
6764
x: Array<number>|TypedArray
6865
Input array.
6966

70-
stride: integer
71-
Index increment.
67+
strideX: integer
68+
Stride length.
7269

73-
offset: integer
70+
offsetX: integer
7471
Starting index.
7572

7673
Returns
@@ -86,9 +83,8 @@
8683
~0.3333
8784

8885
// Using offset parameter:
89-
> var x = [ 1.0, -2.0, 3.0, 2.0, 5.0, -1.0, NaN ];
90-
> var N = {{alias:@stdlib/math/base/special/floor}}( x.length / 2 );
91-
> {{alias}}.ndarray( N, x, 2, 1 )
86+
> var x = [ 1.0, -2.0, 3.0, 2.0, 5.0, -1.0, NaN, NaN ];
87+
> {{alias}}.ndarray( 4, x, 2, 1 )
9288
~-0.3333
9389

9490
See Also

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

Lines changed: 10 additions & 5 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 `nanmeanwd`.
@@ -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,7 +45,7 @@ interface Routine {
4045
* var v = nanmeanwd( x.length, x, 1 );
4146
* // returns ~0.3333
4247
*/
43-
( N: number, x: NumericArray, stride: number ): number;
48+
( N: number, x: InputArray, strideX: number ): number;
4449

4550
/**
4651
* Computes the arithmetic mean of a strided array, ignoring `NaN` values and using Welford's algorithm and alternative indexing semantics.
@@ -57,15 +62,15 @@ interface Routine {
5762
* var v = nanmeanwd.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, strideX: number, offset: number ): number;
6166
}
6267

6368
/**
6469
* Computes the arithmetic mean of a strided array, ignoring `NaN` values and using Welford's 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/nanmeanwd/docs/types/test.ts

Lines changed: 5 additions & 0 deletions
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 nanmeanwd = require( './index' );
2021

2122

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

2829
nanmeanwd( x.length, x, 1 ); // $ExpectType number
30+
nanmeanwd( x.length, new AccessorArray( x ), 1 ); // $ExpectType number
31+
2932
}
3033

3134
// The compiler throws an error if the function is provided a first argument which is not a number...
@@ -85,6 +88,8 @@ import nanmeanwd = require( './index' );
8588
const x = new Float64Array( 10 );
8689

8790
nanmeanwd.ndarray( x.length, x, 1, 0 ); // $ExpectType number
91+
nanmeanwd.ndarray( x.length, new AccessorArray( x ), 1, 0 ); // $ExpectType number
92+
8893
}
8994

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

0 commit comments

Comments
 (0)