Skip to content

Commit 8c1df25

Browse files
committed
refactor: refactor example.js, benchmark files, readme and accessors.js
--- 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: na - task: lint_javascript_src status: passed - task: lint_javascript_cli status: na - task: lint_javascript_examples status: passed - task: lint_javascript_tests status: na - 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: na - task: lint_typescript_tests status: passed - task: lint_license_headers status: passed ---
1 parent db90eb5 commit 8c1df25

File tree

6 files changed

+251
-32
lines changed

6 files changed

+251
-32
lines changed

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

+15-5
Original file line numberDiff line numberDiff line change
@@ -157,9 +157,9 @@ The function has the following additional parameters:
157157
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 [variance][variance] for every other element in the strided array starting from the second element
158158

159159
```javascript
160-
var x = [ 2.0, 1.0, 2.0, -2.0, -2.0, 2.0, 3.0, 4.0 ];
160+
var x = [ 2.0, 1.0, 2.0, -2.0, -2.0, 2.0, 3.0, 4.0, NaN, NaN ];
161161

162-
var v = nanvariancech.ndarray( 4, 1, x, 2, 1 );
162+
var v = nanvariancech.ndarray( 5, 1, x, 2, 1 );
163163
// returns 6.25
164164
```
165165

@@ -188,11 +188,19 @@ var v = nanvariancech.ndarray( 4, 1, x, 2, 1 );
188188
<!-- eslint no-undef: "error" -->
189189

190190
```javascript
191-
var normal = require( '@stdlib/random/array/normal' );
191+
var uniform = require( '@stdlib/random/base/uniform' );
192+
var filledarrayBy = require( '@stdlib/array/filled-by' );
193+
var bernoulli = require( '@stdlib/random/base/bernoulli' );
192194
var nanvariancech = require( '@stdlib/stats/base/nanvariancech' );
193195

194-
var x = normal( 10, 0, 1 );
195-
x[4] = NaN;
196+
function rand() {
197+
if ( bernoulli( 0.8 ) < 1 ) {
198+
return NaN;
199+
}
200+
return uniform( -50.0, 50.0 );
201+
}
202+
203+
var x = filledarrayBy( 10, 'generic', rand );
196204
console.log( x );
197205

198206
var v = nanvariancech( x.length, 1, x, 1 );
@@ -266,6 +274,8 @@ console.log( v );
266274

267275
[@stdlib/stats/base/variancech]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/base/variancech
268276

277+
[@stdlib/array/base/accessor]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/array/base/accessor
278+
269279
<!-- </related-links> -->
270280

271281
</section>

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

+18-7
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@
2121
// MODULES //
2222

2323
var bench = require( '@stdlib/bench' );
24-
var normal = require( '@stdlib/random/array/normal' );
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,19 +32,28 @@ var nanvariancech = require( './../lib/nanvariancech.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
*
3651
* @private
3752
* @param {PositiveInteger} len - array length
3853
* @returns {Function} benchmark function
3954
*/
40-
function createBenchmark() {
41-
var x = normal( 10, 0, 1, {
42-
'dtype': 'float64'
43-
});
44-
x[4] = NaN;
45-
55+
function createBenchmark( len ) {
56+
var x = filledarrayBy( len, 'generic', rand );
4657
return benchmark;
4758

4859
function benchmark( b ) {

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

+18-7
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@
2121
// MODULES //
2222

2323
var bench = require( '@stdlib/bench' );
24-
var normal = require( '@stdlib/random/array/normal' );
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,19 +32,28 @@ var nanvariancech = 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
*
3651
* @private
3752
* @param {PositiveInteger} len - array length
3853
* @returns {Function} benchmark function
3954
*/
40-
function createBenchmark() {
41-
var x = normal( 10, 0, 1, {
42-
'dtype': 'float64'
43-
});
44-
x[4] = NaN;
45-
55+
function createBenchmark( len ) {
56+
var x = filledarrayBy( len, 'generic', rand );
4657
return benchmark;
4758

4859
function benchmark( b ) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,187 @@
1+
/*
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2020 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
import AccessorArray = require( '@stdlib/array/base/accessor' );
20+
import nanvariancech = require( './index' );
21+
22+
// TESTS //
23+
24+
// The function returns a number...
25+
{
26+
const x = new Float64Array( 10 );
27+
28+
nanvariancech( x.length, 1, new AccessorArray( x ), 1 ); // $ExpectType number
29+
}
30+
31+
// The compiler throws an error if the function is provided a first argument which is not a number...
32+
{
33+
const x = new Float64Array( 10 );
34+
35+
nanvariancech( '10', 1, x, 1 ); // $ExpectError
36+
nanvariancech( true, 1, x, 1 ); // $ExpectError
37+
nanvariancech( false, 1, x, 1 ); // $ExpectError
38+
nanvariancech( null, 1, x, 1 ); // $ExpectError
39+
nanvariancech( undefined, 1, x, 1 ); // $ExpectError
40+
nanvariancech( [], 1, x, 1 ); // $ExpectError
41+
nanvariancech( {}, 1, x, 1 ); // $ExpectError
42+
nanvariancech( ( x: number ): number => x, 1, x, 1 ); // $ExpectError
43+
}
44+
45+
// The compiler throws an error if the function is provided a second argument which is not a number...
46+
{
47+
const x = new Float64Array( 10 );
48+
49+
nanvariancech( x.length, '10', x, 1 ); // $ExpectError
50+
nanvariancech( x.length, true, x, 1 ); // $ExpectError
51+
nanvariancech( x.length, false, x, 1 ); // $ExpectError
52+
nanvariancech( x.length, null, x, 1 ); // $ExpectError
53+
nanvariancech( x.length, undefined, x, 1 ); // $ExpectError
54+
nanvariancech( x.length, [], x, 1 ); // $ExpectError
55+
nanvariancech( x.length, {}, x, 1 ); // $ExpectError
56+
nanvariancech( x.length, ( x: number ): number => x, x, 1 ); // $ExpectError
57+
}
58+
59+
// The compiler throws an error if the function is provided a third argument which is not a numeric array...
60+
{
61+
const x = new Float64Array( 10 );
62+
63+
nanvariancech( x.length, 1, 10, 1 ); // $ExpectError
64+
nanvariancech( x.length, 1, '10', 1 ); // $ExpectError
65+
nanvariancech( x.length, 1, true, 1 ); // $ExpectError
66+
nanvariancech( x.length, 1, false, 1 ); // $ExpectError
67+
nanvariancech( x.length, 1, null, 1 ); // $ExpectError
68+
nanvariancech( x.length, 1, undefined, 1 ); // $ExpectError
69+
nanvariancech( x.length, 1, [ '1' ], 1 ); // $ExpectError
70+
nanvariancech( x.length, 1, {}, 1 ); // $ExpectError
71+
nanvariancech( x.length, 1, ( x: number ): number => x, 1 ); // $ExpectError
72+
}
73+
74+
// The compiler throws an error if the function is provided a fourth argument which is not a number...
75+
{
76+
const x = new Float64Array( 10 );
77+
78+
nanvariancech( x.length, 1, x, '10' ); // $ExpectError
79+
nanvariancech( x.length, 1, x, true ); // $ExpectError
80+
nanvariancech( x.length, 1, x, false ); // $ExpectError
81+
nanvariancech( x.length, 1, x, null ); // $ExpectError
82+
nanvariancech( x.length, 1, x, undefined ); // $ExpectError
83+
nanvariancech( x.length, 1, x, [] ); // $ExpectError
84+
nanvariancech( x.length, 1, x, {} ); // $ExpectError
85+
nanvariancech( x.length, 1, x, ( x: number ): number => x ); // $ExpectError
86+
}
87+
88+
// The compiler throws an error if the function is provided an unsupported number of arguments...
89+
{
90+
const x = new Float64Array( 10 );
91+
92+
nanvariancech(); // $ExpectError
93+
nanvariancech( x.length ); // $ExpectError
94+
nanvariancech( x.length, 1 ); // $ExpectError
95+
nanvariancech( x.length, 1, x ); // $ExpectError
96+
nanvariancech( x.length, 1, x, 1, 10 ); // $ExpectError
97+
}
98+
99+
// Attached to main export is an `ndarray` method which returns a number...
100+
{
101+
const x = new Float64Array( 10 );
102+
103+
nanvariancech.ndarray( x.length, 1, new AccessorArray( x ), 1, 0 ); // $ExpectType number
104+
}
105+
106+
// The compiler throws an error if the `ndarray` method is provided a first argument which is not a number...
107+
{
108+
const x = new Float64Array( 10 );
109+
110+
nanvariancech.ndarray( '10', 1, x, 1, 0 ); // $ExpectError
111+
nanvariancech.ndarray( true, 1, x, 1, 0 ); // $ExpectError
112+
nanvariancech.ndarray( false, 1, x, 1, 0 ); // $ExpectError
113+
nanvariancech.ndarray( null, 1, x, 1, 0 ); // $ExpectError
114+
nanvariancech.ndarray( undefined, 1, x, 1, 0 ); // $ExpectError
115+
nanvariancech.ndarray( [], 1, x, 1, 0 ); // $ExpectError
116+
nanvariancech.ndarray( {}, 1, x, 1, 0 ); // $ExpectError
117+
nanvariancech.ndarray( ( x: number ): number => x, 1, x, 1, 0 ); // $ExpectError
118+
}
119+
120+
// The compiler throws an error if the `ndarray` method is provided a second argument which is not a number...
121+
{
122+
const x = new Float64Array( 10 );
123+
124+
nanvariancech.ndarray( x.length, '10', x, 1, 0 ); // $ExpectError
125+
nanvariancech.ndarray( x.length, true, x, 1, 0 ); // $ExpectError
126+
nanvariancech.ndarray( x.length, false, x, 1, 0 ); // $ExpectError
127+
nanvariancech.ndarray( x.length, null, x, 1, 0 ); // $ExpectError
128+
nanvariancech.ndarray( x.length, undefined, x, 1, 0 ); // $ExpectError
129+
nanvariancech.ndarray( x.length, [], x, 1, 0 ); // $ExpectError
130+
nanvariancech.ndarray( x.length, {}, x, 1, 0 ); // $ExpectError
131+
nanvariancech.ndarray( x.length, ( x: number ): number => x, x, 1, 0 ); // $ExpectError
132+
}
133+
134+
// The compiler throws an error if the `ndarray` method is provided a third argument which is not a numeric array...
135+
{
136+
const x = new Float64Array( 10 );
137+
138+
nanvariancech.ndarray( x.length, 1, 10, 1, 0 ); // $ExpectError
139+
nanvariancech.ndarray( x.length, 1, '10', 1, 0 ); // $ExpectError
140+
nanvariancech.ndarray( x.length, 1, true, 1, 0 ); // $ExpectError
141+
nanvariancech.ndarray( x.length, 1, false, 1, 0 ); // $ExpectError
142+
nanvariancech.ndarray( x.length, 1, null, 1, 0 ); // $ExpectError
143+
nanvariancech.ndarray( x.length, 1, undefined, 1, 0 ); // $ExpectError
144+
nanvariancech.ndarray( x.length, 1, [ '1' ], 1, 0 ); // $ExpectError
145+
nanvariancech.ndarray( x.length, 1, {}, 1, 0 ); // $ExpectError
146+
nanvariancech.ndarray( x.length, 1, ( x: number ): number => x, 1, 0 ); // $ExpectError
147+
}
148+
149+
// The compiler throws an error if the `ndarray` method is provided a fourth argument which is not a number...
150+
{
151+
const x = new Float64Array( 10 );
152+
153+
nanvariancech.ndarray( x.length, 1, x, '10', 0 ); // $ExpectError
154+
nanvariancech.ndarray( x.length, 1, x, true, 0 ); // $ExpectError
155+
nanvariancech.ndarray( x.length, 1, x, false, 0 ); // $ExpectError
156+
nanvariancech.ndarray( x.length, 1, x, null, 0 ); // $ExpectError
157+
nanvariancech.ndarray( x.length, 1, x, undefined, 0 ); // $ExpectError
158+
nanvariancech.ndarray( x.length, 1, x, [], 0 ); // $ExpectError
159+
nanvariancech.ndarray( x.length, 1, x, {}, 0 ); // $ExpectError
160+
nanvariancech.ndarray( x.length, 1, x, ( x: number ): number => x, 0 ); // $ExpectError
161+
}
162+
163+
// The compiler throws an error if the `ndarray` method is provided a fifth argument which is not a number...
164+
{
165+
const x = new Float64Array( 10 );
166+
167+
nanvariancech.ndarray( x.length, 1, x, 1, '10' ); // $ExpectError
168+
nanvariancech.ndarray( x.length, 1, x, 1, true ); // $ExpectError
169+
nanvariancech.ndarray( x.length, 1, x, 1, false ); // $ExpectError
170+
nanvariancech.ndarray( x.length, 1, x, 1, null ); // $ExpectError
171+
nanvariancech.ndarray( x.length, 1, x, 1, undefined ); // $ExpectError
172+
nanvariancech.ndarray( x.length, 1, x, 1, [] ); // $ExpectError
173+
nanvariancech.ndarray( x.length, 1, x, 1, {} ); // $ExpectError
174+
nanvariancech.ndarray( x.length, 1, x, 1, ( x: number ): number => x ); // $ExpectError
175+
}
176+
177+
// The compiler throws an error if the `ndarray` method is provided an unsupported number of arguments...
178+
{
179+
const x = new Float64Array( 10 );
180+
181+
nanvariancech.ndarray(); // $ExpectError
182+
nanvariancech.ndarray( x.length ); // $ExpectError
183+
nanvariancech.ndarray( x.length, 1 ); // $ExpectError
184+
nanvariancech.ndarray( x.length, 1, x ); // $ExpectError
185+
nanvariancech.ndarray( x.length, 1, x, 1 ); // $ExpectError
186+
nanvariancech.ndarray( x.length, 1, x, 1, 0, 10 ); // $ExpectError
187+
}

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

+10-6
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,19 @@
1818

1919
'use strict';
2020

21-
var normal = require( '@stdlib/random/array/normal' );
22-
var Float64Array = require( '@stdlib/array/float64' );
21+
var uniform = require( '@stdlib/random/base/uniform' );
22+
var filledarrayBy = require( '@stdlib/array/filled-by' );
23+
var bernoulli = require( '@stdlib/random/base/bernoulli' );
2324
var nanvariancech = require( './../lib' );
2425

25-
var x = normal( 10, 0, 1, {
26-
'dtype':'float64'
27-
} );
28-
x[4] = NaN;
26+
function rand() {
27+
if ( bernoulli( 0.8 ) > 1 ) {
28+
return NaN;
29+
}
30+
return uniform( -50.0, 50.0 );
31+
}
2932

33+
var x = filledarrayBy( 10, 'generic', rand );
3034
console.log( x );
3135

3236
var v = nanvariancech( x.length, 1, x, 1 );

lib/node_modules/@stdlib/stats/base/nanvariancech/lib/accessors.js

+3-7
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818

1919
'use strict';
2020

21-
2221
// MAIN //
2322

2423
/**
@@ -48,9 +47,9 @@
4847
* var arraylike2object = require( '@stdlib/array/base/arraylike2object' );
4948
* var toAccessorArray = require( '@stdlib/array/base/to-accessor-array' );
5049
*
51-
* var x = arraylike2object( [ 2.0, 1.0, 2.0, -2.0, -2.0, 2.0, 3.0, 4.0, NaN, NaN ] );
50+
* var x = toAccessorArray( [ 2.0, 1.0, 2.0, -2.0, -2.0, 2.0, 3.0, 4.0, NaN, NaN ] );
5251
*
53-
* var v = nanvariancech( 5, 1, toAccessorArray( x ), 2, 1 );
52+
* var v = nanvariancech( 5, 1, arraylike2object( x ), 2, 1 );
5453
* // returns 6.25
5554
*/
5655
function nanvariancech( N, correction, x, strideX, offsetX ) {
@@ -72,9 +71,6 @@ function nanvariancech( N, correction, x, strideX, offsetX ) {
7271
// Cache references to element accessors:
7372
xget = x.accessors[ 0 ];
7473

75-
if ( N <= 0 ) {
76-
return NaN;
77-
}
7874
if ( N === 1 || strideX === 0 ) {
7975
v = xget( xbuf, offsetX );
8076
if ( v === v && N-correction > 0.0 ) {
@@ -86,7 +82,7 @@ function nanvariancech( N, correction, x, strideX, offsetX ) {
8682

8783
// Find an estimate for the mean...
8884
for ( i = 0; i < N; i++ ) {
89-
v = xget( xbuf, ix);
85+
v = xget( xbuf, ix );
9086
if ( v === v ) {
9187
mu = v;
9288
break;

0 commit comments

Comments
 (0)