Skip to content

Commit d24bef1

Browse files
committed
added accessor array
1 parent 9e91f81 commit d24bef1

File tree

4 files changed

+97
-6
lines changed

4 files changed

+97
-6
lines changed
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2025 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+
'use strict';
20+
21+
// MAIN //
22+
23+
/**
24+
* Computes the variance of a strided array using a one-pass trial mean algorithm.
25+
*
26+
* @param {PositiveInteger} N - number of indexed elements
27+
* @param {number} correction - degrees of freedom adjustment
28+
* @param {Object} x - input array object
29+
* @param {Function} x.get - accessor function
30+
* @param {integer} stride - stride length
31+
* @param {NonNegativeInteger} offset - starting index
32+
* @returns {number} variance
33+
*/
34+
function variancech(N, correction, x, stride, offset) {
35+
var sum;
36+
var ix;
37+
var i;
38+
39+
if (N <= 0) {
40+
return NaN;
41+
}
42+
if (N === 1 || stride === 0) {
43+
return 0.0;
44+
}
45+
ix = offset;
46+
sum = 0.0;
47+
48+
// Calculate mean
49+
for (i = 0; i < N; i++) {
50+
sum += x.get(ix);
51+
ix += stride;
52+
}
53+
var mean = sum / N;
54+
55+
// Calculate variance
56+
ix = offset;
57+
var sum2 = 0.0;
58+
for (i = 0; i < N; i++) {
59+
var delta = x.get(ix) - mean;
60+
sum2 += delta * delta;
61+
ix += stride;
62+
}
63+
return sum2 / (N - correction);
64+
}
65+
66+
// EXPORTS //
67+
module.exports = variancech;

lib/node_modules/@stdlib/stats/base/variancech/lib/index.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,10 @@
4545

4646
// MODULES //
4747

48-
var main = require( './main.js' );
49-
48+
var main = require('./main.js');
49+
var ndarray = require('./ndarray.js');
5050

5151
// EXPORTS //
5252

5353
module.exports = main;
54-
55-
// exports: { "ndarray": "main.ndarray" }
54+
module.exports.ndarray = ndarray;

lib/node_modules/@stdlib/stats/base/variancech/lib/main.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,25 @@ var ndarray = require( './ndarray.js' );
2929

3030
setReadOnly( variancech, 'ndarray', ndarray );
3131

32+
/**
33+
* Computes the variance of a strided array using a one-pass trial mean algorithm.
34+
*
35+
* @param {PositiveInteger} N - number of indexed elements
36+
* @param {number} correction - degrees of freedom adjustment
37+
* @param {NumericArray} x - input array
38+
* @param {integer} stride - stride length
39+
* @returns {number} variance
40+
*
41+
* @example
42+
* var x = [ 1.0, -2.0, 2.0 ];
43+
*
44+
* var v = variancech( x.length, 1, x, 1 );
45+
* // returns ~4.3333
46+
*/
47+
48+
function variancech(N, correction, x, stride) {
49+
return ndarray(N, correction, x, stride, 0);
50+
}
3251

3352
// EXPORTS //
3453

lib/node_modules/@stdlib/stats/base/variancech/lib/ndarray.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,11 @@
1818

1919
'use strict';
2020

21-
// MAIN //
21+
// MODULES //
22+
var isAccessorArray = require('@stdlib/array/base/assert/is-accessor-array');
23+
var accessors = require('./accessors.js');
2224

25+
// MAIN //
2326
/**
2427
* Computes the variance of a strided array using a one-pass trial mean algorithm.
2528
*
@@ -50,7 +53,10 @@
5053
* var v = variancech( N, 1, x, 2, 1 );
5154
* // returns 6.25
5255
*/
53-
function variancech( N, correction, x, stride, offset ) {
56+
function variancech(N, correction, x, stride, offset) {
57+
if (isAccessorArray(x)) {
58+
return accessors(N, correction, x, stride, offset);
59+
}
5460
var mu;
5561
var ix;
5662
var M2;

0 commit comments

Comments
 (0)