Skip to content

Commit f0bb34b

Browse files
committed
feat: add C ndarray interface and refactor implementation
--- 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: na - 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: na - task: lint_python status: na - task: lint_r status: na - task: lint_c_src status: passed - task: lint_c_examples status: passed - 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: na - task: lint_license_headers status: passed ---
1 parent 861cd7f commit f0bb34b

File tree

17 files changed

+166
-197
lines changed

17 files changed

+166
-197
lines changed

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

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11

2-
{{alias}}( N, correction, x, stride )
2+
{{alias}}( N, correction, x, strideX )
33
Computes the variance of a single-precision floating-point strided array
44
using a one-pass trial mean algorithm.
55

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

99
Indexing is relative to the first index. To introduce an offset, use a typed
@@ -31,8 +31,8 @@
3131
x: Float32Array
3232
Input array.
3333

34-
stride: integer
35-
Index increment.
34+
strideX: integer
35+
Stride Length.
3636

3737
Returns
3838
-------
@@ -46,22 +46,19 @@
4646
> {{alias}}( x.length, 1, x, 1 )
4747
~4.3333
4848

49-
// Using `N` and `stride` parameters:
49+
// Using `N` and stride parameters:
5050
> x = new {{alias:@stdlib/array/float32}}( [ -2.0, 1.0, 1.0, -5.0, 2.0, -1.0 ] );
51-
> var N = {{alias:@stdlib/math/base/special/floor}}( x.length / 2 );
52-
> var stride = 2;
53-
> {{alias}}( N, 1, x, stride )
51+
> {{alias}}( 3, 1, x, 2 )
5452
~4.3333
5553

5654
// Using view offsets:
5755
> var x0 = new {{alias:@stdlib/array/float32}}( [ 1.0, -2.0, 3.0, 2.0, 5.0, -1.0 ] );
5856
> var x1 = new {{alias:@stdlib/array/float32}}( x0.buffer, x0.BYTES_PER_ELEMENT*1 );
59-
> N = {{alias:@stdlib/math/base/special/floor}}( x0.length / 2 );
60-
> stride = 2;
61-
> {{alias}}( N, 1, x1, stride )
57+
> {{alias}}( 3, 1, x1, 2 )
6258
~4.3333
6359

64-
{{alias}}.ndarray( N, correction, x, stride, offset )
60+
61+
{{alias}}.ndarray( N, correction, x, strideX, offsetX )
6562
Computes the variance of a single-precision floating-point strided array
6663
using a one-pass trial mean algorithm and alternative indexing semantics.
6764

@@ -89,10 +86,10 @@
8986
x: Float32Array
9087
Input array.
9188

92-
stride: integer
93-
Index increment.
89+
strideX: integer
90+
Stride Length.
9491

95-
offset: integer
92+
offsetX: integer
9693
Starting index.
9794

9895
Returns
@@ -109,8 +106,7 @@
109106

110107
// Using offset parameter:
111108
> var x = new {{alias:@stdlib/array/float32}}( [ 1.0, -2.0, 3.0, 2.0, 5.0, -1.0 ] );
112-
> var N = {{alias:@stdlib/math/base/special/floor}}( x.length / 2 );
113-
> {{alias}}.ndarray( N, 1, x, 2, 1 )
109+
> {{alias}}.ndarray( 3, 1, x, 2, 1 )
114110
~4.3333
115111

116112
See Also

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ interface Routine {
2828
* @param N - number of indexed elements
2929
* @param correction - degrees of freedom adjustment
3030
* @param x - input array
31-
* @param stride - stride length
31+
* @param strideX - stride length
3232
* @returns variance
3333
*
3434
* @example
@@ -39,16 +39,16 @@ interface Routine {
3939
* var v = svariancech( x.length, 1, x, 1 );
4040
* // returns ~4.3333
4141
*/
42-
( N: number, correction: number, x: Float32Array, stride: number ): number;
42+
( N: number, correction: number, x: Float32Array, strideX: number ): number;
4343

4444
/**
4545
* Computes the variance of a single-precision floating-point strided array using a one-pass trial mean algorithm and alternative indexing semantics.
4646
*
4747
* @param N - number of indexed elements
4848
* @param correction - degrees of freedom adjustment
4949
* @param x - input array
50-
* @param stride - stride length
51-
* @param offset - starting index
50+
* @param strideX - stride length
51+
* @param offsetX - starting index
5252
* @returns variance
5353
*
5454
* @example
@@ -59,7 +59,7 @@ interface Routine {
5959
* var v = svariancech.ndarray( x.length, 1, x, 1, 0 );
6060
* // returns ~4.3333
6161
*/
62-
ndarray( N: number, correction: number, x: Float32Array, stride: number, offset: number ): number;
62+
ndarray( N: number, correction: number, x: Float32Array, strideX: number, offsetX: number ): number;
6363
}
6464

6565
/**
@@ -68,7 +68,7 @@ interface Routine {
6868
* @param N - number of indexed elements
6969
* @param correction - degrees of freedom adjustment
7070
* @param x - input array
71-
* @param stride - stride length
71+
* @param strideX - stride length
7272
* @returns variance
7373
*
7474
* @example

lib/node_modules/@stdlib/stats/base/svariancech/examples/c/example.c

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,21 +17,20 @@
1717
*/
1818

1919
#include "stdlib/stats/base/svariancech.h"
20-
#include <stdint.h>
2120
#include <stdio.h>
2221

2322
int main( void ) {
2423
// Create a strided array:
25-
float x[] = { 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 };
24+
const float x[] = { 1.0f, 2.0f, 3.0f, 4.0f, 5.0f, 6.0f, 7.0f, 8.0f };
2625

2726
// Specify the number of elements:
28-
int64_t N = 4;
27+
const int N = 4;
2928

3029
// Specify the stride length:
31-
int64_t stride = 2;
30+
const int strideX = 2;
3231

3332
// Compute the variance:
34-
float v = stdlib_strided_svariancech( N, 1.0f, x, stride );
33+
float v = stdlib_strided_svariancech( N, 1.0f, x, strideX );
3534

3635
// Print the result:
3736
printf( "sample variance: %f\n", v );

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

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -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 Float32Array = require( '@stdlib/array/float32' );
21+
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
2422
var svariancech = require( './../lib' );
2523

26-
var x;
27-
var i;
28-
29-
x = new Float32Array( 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': 'float32'
26+
});
3327
console.log( x );
3428

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

lib/node_modules/@stdlib/stats/base/svariancech/include/stdlib/stats/base/svariancech.h

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
#ifndef STDLIB_STATS_BASE_SVARIANCECH_H
2020
#define STDLIB_STATS_BASE_SVARIANCECH_H
2121

22-
#include <stdint.h>
22+
#include "stdlib/blas/base/shared.h"
2323

2424
/*
2525
* If C++, prevent name mangling so that the compiler emits a binary file having undecorated names, thus mirroring the behavior of a C compiler.
@@ -31,7 +31,12 @@ extern "C" {
3131
/**
3232
* Computes the variance of a single-precision floating-point strided array using a one-pass trial mean algorithm.
3333
*/
34-
float stdlib_strided_svariancech( const int64_t N, const float correction, const float *X, const int64_t stride );
34+
float API_SUFFIX(stdlib_strided_svariancech)( const CBLAS_INT N, const float correction, const float *X, const CBLAS_INT strideX );
35+
36+
/**
37+
* Computes the variance of a single-precision floating-point strided array using a one-pass trial mean algorithm and alternative indexing semantics.
38+
*/
39+
float API_SUFFIX(stdlib_strided_svariancech_ndarray)( const CBLAS_INT N, const float correction, const float *X, const CBLAS_INT strideX, const CBLAS_INT offsetX );
3540

3641
#ifdef __cplusplus
3742
}

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

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,20 +28,17 @@
2828
* var svariancech = require( '@stdlib/stats/base/svariancech' );
2929
*
3030
* var x = new Float32Array( [ 1.0, -2.0, 2.0 ] );
31-
* var N = x.length;
3231
*
33-
* var v = svariancech( N, 1, x, 1 );
32+
* var v = svariancech( x.length, 1, x, 1 );
3433
* // returns ~4.3333
3534
*
3635
* @example
3736
* var Float32Array = require( '@stdlib/array/float32' );
38-
* var floor = require( '@stdlib/math/base/special/floor' );
3937
* var svariancech = require( '@stdlib/stats/base/svariancech' );
4038
*
4139
* var x = new Float32Array( [ 2.0, 1.0, 2.0, -2.0, -2.0, 2.0, 3.0, 4.0 ] );
42-
* var N = floor( x.length / 2 );
4340
*
44-
* var v = svariancech.ndarray( N, 1, x, 2, 1 );
41+
* var v = svariancech.ndarray( 4, 1, x, 2, 1 );
4542
* // returns 6.25
4643
*/
4744

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

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -42,21 +42,19 @@ var float64ToFloat32 = require( '@stdlib/number/float64/base/to-float32' );
4242
* @param {PositiveInteger} N - number of indexed elements
4343
* @param {number} correction - degrees of freedom adjustment
4444
* @param {Float32Array} x - input array
45-
* @param {integer} stride - stride length
46-
* @param {NonNegativeInteger} offset - starting index
45+
* @param {integer} strideX - stride length
46+
* @param {NonNegativeInteger} offsetX - starting index
4747
* @returns {number} variance
4848
*
4949
* @example
5050
* var Float32Array = require( '@stdlib/array/float32' );
51-
* var floor = require( '@stdlib/math/base/special/floor' );
5251
*
5352
* var x = new Float32Array( [ 2.0, 1.0, 2.0, -2.0, -2.0, 2.0, 3.0, 4.0 ] );
54-
* var N = floor( x.length / 2 );
5553
*
56-
* var v = svariancech( N, 1, x, 2, 1 );
54+
* var v = svariancech( 4, 1, x, 2, 1 );
5755
* // returns 6.25
5856
*/
59-
function svariancech( N, correction, x, stride, offset ) {
57+
function svariancech( N, correction, x, strideX, offsetX ) {
6058
var mu;
6159
var ix;
6260
var M2;
@@ -69,14 +67,14 @@ function svariancech( N, correction, x, stride, offset ) {
6967
if ( N <= 0 || n <= 0.0 ) {
7068
return NaN;
7169
}
72-
if ( N === 1 || stride === 0 ) {
70+
if ( N === 1 || strideX === 0 ) {
7371
return 0.0;
7472
}
75-
ix = offset;
73+
ix = offsetX;
7674

7775
// Use an estimate for the mean:
7876
mu = x[ ix ];
79-
ix += stride;
77+
ix += strideX;
8078

8179
// Compute the variance...
8280
M2 = 0.0;
@@ -85,7 +83,7 @@ function svariancech( N, correction, x, stride, offset ) {
8583
d = float64ToFloat32( x[ ix ] - mu );
8684
M2 = float64ToFloat32( M2 + float64ToFloat32( d*d ) );
8785
M = float64ToFloat32( M + d );
88-
ix += stride;
86+
ix += strideX;
8987
}
9088
return float64ToFloat32( float64ToFloat32(M2/n) - float64ToFloat32( float64ToFloat32(M/N)*float64ToFloat32(M/n) ) ); // eslint-disable-line max-len
9189
}

lib/node_modules/@stdlib/stats/base/svariancech/lib/ndarray.native.js

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,7 @@
2020

2121
// MODULES //
2222

23-
var Float32Array = require( '@stdlib/array/float32' );
24-
var addon = require( './svariancech.native.js' );
23+
var addon = require( './../src/addon.node' );
2524

2625

2726
// MAIN //
@@ -32,27 +31,20 @@ var addon = require( './svariancech.native.js' );
3231
* @param {PositiveInteger} N - number of indexed elements
3332
* @param {number} correction - degrees of freedom adjustment
3433
* @param {Float32Array} x - input array
35-
* @param {integer} stride - stride length
36-
* @param {NonNegativeInteger} offset - starting index
34+
* @param {integer} strideX - stride length
35+
* @param {NonNegativeInteger} offsetX - starting index
3736
* @returns {number} variance
3837
*
3938
* @example
4039
* var Float32Array = require( '@stdlib/array/float32' );
41-
* var floor = require( '@stdlib/math/base/special/floor' );
4240
*
4341
* var x = new Float32Array( [ 2.0, 1.0, 2.0, -2.0, -2.0, 2.0, 3.0, 4.0 ] );
44-
* var N = floor( x.length / 2 );
4542
*
46-
* var v = svariancech( N, 1, x, 2, 1 );
43+
* var v = svariancech( 4, 1, x, 2, 1 );
4744
* // returns 6.25
4845
*/
49-
function svariancech( N, correction, x, stride, offset ) {
50-
var view;
51-
if ( stride < 0 ) {
52-
offset += (N-1) * stride;
53-
}
54-
view = new Float32Array( x.buffer, x.byteOffset+(x.BYTES_PER_ELEMENT*offset), x.length-offset ); // eslint-disable-line max-len
55-
return addon( N, correction, view, stride );
46+
function svariancech( N, correction, x, strideX, offsetX ) {
47+
return addon.ndarray( N, correction, x, strideX, offsetX );
5648
}
5749

5850

lib/node_modules/@stdlib/stats/base/svariancech/lib/svariancech.js

Lines changed: 6 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@
2020

2121
// MODULES //
2222

23-
var float64ToFloat32 = require( '@stdlib/number/float64/base/to-float32' );
23+
var stride2offset = require( '@stdlib/strided/base/stride2offset' );
24+
var ndarray = require( './ndarray.js' );
2425

2526

2627
// MAIN //
@@ -42,53 +43,19 @@ var float64ToFloat32 = require( '@stdlib/number/float64/base/to-float32' );
4243
* @param {PositiveInteger} N - number of indexed elements
4344
* @param {number} correction - degrees of freedom adjustment
4445
* @param {Float32Array} x - input array
45-
* @param {integer} stride - stride length
46+
* @param {integer} strideX - stride length
4647
* @returns {number} variance
4748
*
4849
* @example
4950
* var Float32Array = require( '@stdlib/array/float32' );
5051
*
5152
* var x = new Float32Array( [ 1.0, -2.0, 2.0 ] );
52-
* var N = x.length;
5353
*
54-
* var v = svariancech( N, 1, x, 1 );
54+
* var v = svariancech( x.length, 1, x, 1 );
5555
* // returns ~4.3333
5656
*/
57-
function svariancech( N, correction, x, stride ) {
58-
var mu;
59-
var ix;
60-
var M2;
61-
var M;
62-
var d;
63-
var n;
64-
var i;
65-
66-
n = N - correction;
67-
if ( N <= 0 || n <= 0.0 ) {
68-
return NaN;
69-
}
70-
if ( N === 1 || stride === 0 ) {
71-
return 0.0;
72-
}
73-
if ( stride < 0 ) {
74-
ix = (1-N) * stride;
75-
} else {
76-
ix = 0;
77-
}
78-
// Use an estimate for the mean:
79-
mu = x[ ix ];
80-
ix += stride;
81-
82-
// Compute the variance...
83-
M2 = 0.0;
84-
M = 0.0;
85-
for ( i = 1; i < N; i++ ) {
86-
d = float64ToFloat32( x[ ix ] - mu );
87-
M2 = float64ToFloat32( M2 + float64ToFloat32( d*d ) );
88-
M = float64ToFloat32( M + d );
89-
ix += stride;
90-
}
91-
return float64ToFloat32( float64ToFloat32(M2/n) - float64ToFloat32( float64ToFloat32(M/N)*float64ToFloat32(M/n) ) ); // eslint-disable-line max-len
57+
function svariancech( N, correction, x, strideX ) {
58+
return ndarray( N, correction, x, strideX, stride2offset( N, strideX ) );
9259
}
9360

9461

0 commit comments

Comments
 (0)