Skip to content

Commit 0674975

Browse files
committed
feat: add accessor array support and refactor stats/base/nanmeanpn
--- 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 282d01f commit 0674975

File tree

12 files changed

+427
-179
lines changed

12 files changed

+427
-179
lines changed

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

Lines changed: 25 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -67,62 +67,52 @@ 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 arrays 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' );
75+
var x = [ 1.0, 2.0, 2.0, -7.0, -2.0, 3.0, 4.0, 2.0, NaN, NaN ];
7676

77-
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 );
79-
80-
var v = nanmeanpn( N, x, 2 );
77+
var v = nanmeanpn( 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 = nanmeanpn( N, x1, 2 );
91+
var v = nanmeanpn( 5, x1, 2 );
9892
// returns 1.25
9993
```
10094

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

10397
Computes the [arithmetic mean][arithmetic-mean] of a strided array, ignoring `NaN` values and using a two-pass error correction algorithm and alternative indexing semantics.
10498

10599
```javascript
106100
var x = [ 1.0, -2.0, NaN, 2.0 ];
107-
var N = x.length;
108101

109-
var v = nanmeanpn.ndarray( N, x, 1, 0 );
102+
var v = nanmeanpn.ndarray( 4, x, 1, 0 );
110103
// returns ~0.33333
111104
```
112105

113106
The function has the following additional parameters:
114107

115-
- **offset**: starting index for `x`.
108+
- **offsetX**: starting index for `x`.
116109

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
110+
While [`typed array`][mdn-typed-array] views mandate a view offset based on the underlying buffer, the offset parameters support indexing semantics based on a starting indices. For example, to calculate the [arithmetic mean][arithmetic-mean] for every other value in `x` starting from the second value
118111

119112
```javascript
120-
var floor = require( '@stdlib/math/base/special/floor' );
121-
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 );
113+
var x = [ 2.0, 1.0, 2.0, -2.0, -2.0, 2.0, 3.0, 4.0, NaN, NaN ];
124114

125-
var v = nanmeanpn.ndarray( N, x, 2, 1 );
115+
var v = nanmeanpn.ndarray( 5, x, 2, 1 );
126116
// returns 1.25
127117
```
128118

@@ -136,6 +126,7 @@ var v = nanmeanpn.ndarray( N, x, 2, 1 );
136126

137127
- If `N <= 0`, both functions return `NaN`.
138128
- If every indexed element is `NaN`, both functions return `NaN`.
129+
- 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]).
139130
- Depending on the environment, the typed versions ([`dnanmeanpn`][@stdlib/stats/base/dnanmeanpn], [`snanmeanpn`][@stdlib/stats/base/snanmeanpn], etc.) are likely to be significantly more performant.
140131

141132
</section>
@@ -149,22 +140,19 @@ var v = nanmeanpn.ndarray( N, x, 2, 1 );
149140
<!-- eslint no-undef: "error" -->
150141

151142
```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' );
143+
var uniform = require( '@stdlib/random/base/uniform' );
144+
var filledarrayBy = require( '@stdlib/array/filled-by' );
145+
var bernoulli = require( '@stdlib/random/base/bernoulli' );
155146
var nanmeanpn = require( '@stdlib/stats/base/nanmeanpn' );
156147

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 );
148+
function rand() {
149+
if ( bernoulli( 0.8 ) < 1 ) {
150+
return NaN;
166151
}
152+
return uniform( -50.0, 50.0 );
167153
}
154+
155+
var x = filledarrayBy( 10, 'generic', rand );
168156
console.log( x );
169157

170158
var v = nanmeanpn( x.length, x, 1 );
@@ -229,6 +217,8 @@ console.log( v );
229217

230218
[@stdlib/stats/base/snanmeanpn]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/base/snanmeanpn
231219

220+
[@stdlib/array/base/accessor]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/array/base/accessor
221+
232222
<!-- </related-links> -->
233223

234224
</section>

lib/node_modules/@stdlib/stats/base/nanmeanpn/benchmark/benchmark.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 nanmeanpn = require( './../lib/nanmeanpn.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 nanmeanpn = require( './../lib/nanmeanpn.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, 'generic', rand );
5257
return benchmark;
5358

5459
function benchmark( b ) {

lib/node_modules/@stdlib/stats/base/nanmeanpn/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 nanmeanpn = 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 nanmeanpn = 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, 'generic', rand );
5257
return benchmark;
5358

5459
function benchmark( b ) {

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

Lines changed: 16 additions & 20 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 a two-pass error 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 in the strided arrays
7+
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
-------
@@ -33,25 +33,22 @@
3333
--------
3434
// Standard Usage:
3535
> var x = [ 1.0, -2.0, NaN, 2.0 ];
36-
> {{alias}}( x.length, x, 1 )
36+
> {{alias}}( 4, x, 1 )
3737
~0.3333
3838

39-
// Using `N` and `stride` parameters:
39+
// 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}}( 3, x, 2 )
4442
~0.3333
4543

4644
// Using view offsets:
4745
> var x0 = new {{alias:@stdlib/array/float64}}( [ 1.0, -2.0, 3.0, 2.0, 5.0, -1.0, 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}}( 3, 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 a two-pass error correction algorithm and alternative indexing
5754
semantics.
@@ -68,10 +65,10 @@
6865
x: Array<number>|TypedArray
6966
Input array.
7067

71-
stride: integer
72-
Index increment.
68+
strideX: integer
69+
Stride length.
7370

74-
offset: integer
71+
offsetX: integer
7572
Starting index.
7673

7774
Returns
@@ -83,13 +80,12 @@
8380
--------
8481
// Standard Usage:
8582
> var x =[ 1.0, -2.0, NaN, 2.0 ];
86-
> {{alias}}.ndarray( x.length, x, 1, 0 )
83+
> {{alias}}.ndarray( 4, x, 1, 0 )
8784
~0.3333
8885

8986
// Using offset parameter:
9087
> var x = [ 1.0, -2.0, 3.0, 2.0, 5.0, -1.0, NaN ];
91-
> var N = {{alias:@stdlib/math/base/special/floor}}( x.length / 2 );
92-
> {{alias}}.ndarray( N, x, 2, 1 )
88+
> {{alias}}.ndarray( 3, x, 2, 1 )
9389
~-0.3333
9490

9591
See Also

lib/node_modules/@stdlib/stats/base/nanmeanpn/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 `nanmeanpn`.
@@ -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 = nanmeanpn( 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 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 = nanmeanpn.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, offsetX: number ): number;
6166
}
6267

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

Lines changed: 3 additions & 2 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 nanmeanpn = require( './index' );
2021

2122

@@ -25,7 +26,7 @@ import nanmeanpn = require( './index' );
2526
{
2627
const x = new Float64Array( 10 );
2728

28-
nanmeanpn( x.length, x, 1 ); // $ExpectType number
29+
nanmeanpn( x.length, new AccessorArray( x ), 1 ); // $ExpectType number
2930
}
3031

3132
// The compiler throws an error if the function is provided a first argument which is not a number...
@@ -84,7 +85,7 @@ import nanmeanpn = require( './index' );
8485
{
8586
const x = new Float64Array( 10 );
8687

87-
nanmeanpn.ndarray( x.length, x, 1, 0 ); // $ExpectType number
88+
nanmeanpn.ndarray( x.length, new AccessorArray( x ), 1, 0 ); // $ExpectType number
8889
}
8990

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

0 commit comments

Comments
 (0)