Skip to content

Commit 1635a0e

Browse files
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: 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: passed - task: lint_javascript_benchmarks status: passed - 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: passed - task: lint_c_tests_fixtures status: na - task: lint_shell status: na - task: lint_typescript_declarations status: na - task: lint_typescript_tests status: na - task: lint_license_headers status: passed --- --- type: pre_push_report description: Results of running various checks prior to pushing changes. report: - task: run_javascript_examples status: na - task: run_c_examples status: na - task: run_cpp_examples status: na - task: run_javascript_readme_examples status: na - task: run_c_benchmarks status: na - task: run_cpp_benchmarks status: na - task: run_fortran_benchmarks status: na - task: run_javascript_benchmarks status: na - task: run_julia_benchmarks status: na - task: run_python_benchmarks status: na - task: run_r_benchmarks status: na - task: run_javascript_tests status: na ---
1 parent 54148e1 commit 1635a0e

File tree

22 files changed

+414
-234
lines changed

22 files changed

+414
-234
lines changed

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

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

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

5656
Computes the [arithmetic mean][arithmetic-mean] of a single-precision floating-point strided array `x`, ignoring `NaN` values and using extended accumulation.
5757

@@ -61,26 +61,24 @@ var Float32Array = require( '@stdlib/array/float32' );
6161
var x = new Float32Array( [ 1.0, -2.0, NaN, 2.0 ] );
6262
var N = x.length;
6363

64-
var v = sdsnanmean( N, x, 1 );
64+
var v = sdsnanmean( x.length, x, 1 );
6565
// returns ~0.3333
6666
```
6767

6868
The function has the following parameters:
6969

7070
- **N**: number of indexed elements.
7171
- **x**: input [`Float32Array`][@stdlib/array/float32].
72-
- **stride**: index increment for `x`.
72+
- **strideX**: stride length for `x`.
7373

74-
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`,
74+
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`,
7575

7676
```javascript
7777
var Float32Array = require( '@stdlib/array/float32' );
78-
var floor = require( '@stdlib/math/base/special/floor' );
7978

8079
var x = new Float32Array( [ 1.0, 2.0, 2.0, -7.0, -2.0, 3.0, 4.0, 2.0, NaN ] );
81-
var N = floor( x.length / 2 );
8280

83-
var v = sdsnanmean( N, x, 2 );
81+
var v = sdsnanmean( 4, x, 2 );
8482
// returns 1.25
8583
```
8684

@@ -90,45 +88,39 @@ Note that indexing is relative to the first index. To introduce an offset, use [
9088

9189
```javascript
9290
var Float32Array = require( '@stdlib/array/float32' );
93-
var floor = require( '@stdlib/math/base/special/floor' );
9491

9592
var x0 = new Float32Array( [ 2.0, 1.0, 2.0, -2.0, -2.0, 2.0, 3.0, 4.0, NaN ] );
9693
var x1 = new Float32Array( x0.buffer, x0.BYTES_PER_ELEMENT*1 ); // start at 2nd element
9794

98-
var N = floor( x0.length / 2 );
99-
100-
var v = sdsnanmean( N, x1, 2 );
95+
var v = sdsnanmean( 4, x1, 2 );
10196
// returns 1.25
10297
```
10398

104-
#### sdsnanmean.ndarray( N, x, stride, offset )
99+
#### sdsnanmean.ndarray( N, x, strideX, offsetX )
105100

106101
Computes the [arithmetic mean][arithmetic-mean] of a single-precision floating-point strided array, ignoring `NaN` values and using extended accumulation and alternative indexing semantics.
107102

108103
```javascript
109104
var Float32Array = require( '@stdlib/array/float32' );
110105

111106
var x = new Float32Array( [ 1.0, -2.0, NaN, 2.0 ] );
112-
var N = x.length;
113107

114-
var v = sdsnanmean.ndarray( N, x, 1, 0 );
108+
var v = sdsnanmean.ndarray( x.length, x, 1, 0 );
115109
// returns ~0.33333
116110
```
117111

118112
The function has the following additional parameters:
119113

120-
- **offset**: starting index for `x`.
114+
- **offsetX**: starting index for `x`.
121115

122-
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
116+
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 `x` starting from the second element
123117

124118
```javascript
125119
var Float32Array = require( '@stdlib/array/float32' );
126-
var floor = require( '@stdlib/math/base/special/floor' );
127120

128121
var x = new Float32Array( [ 2.0, 1.0, 2.0, -2.0, -2.0, 2.0, 3.0, 4.0, NaN ] );
129-
var N = floor( x.length / 2 );
130122

131-
var v = sdsnanmean.ndarray( N, x, 2, 1 );
123+
var v = sdsnanmean.ndarray( 4, x, 2, 1 );
132124
// returns 1.25
133125
```
134126

@@ -155,23 +147,18 @@ var v = sdsnanmean.ndarray( N, x, 2, 1 );
155147
<!-- eslint no-undef: "error" -->
156148

157149
```javascript
158-
var randu = require( '@stdlib/random/base/randu' );
159-
var round = require( '@stdlib/math/base/special/round' );
160-
var Float32Array = require( '@stdlib/array/float32' );
150+
var uniform = require( '@stdlib/random/base/uniform' );
151+
var filledarrayBy = require( '@stdlib/array/filled-by' );
152+
var bernoulli = require( '@stdlib/random/base/bernoulli' );
161153
var sdsnanmean = require( '@stdlib/stats/base/sdsnanmean' );
162154

163-
var x;
164-
var i;
165-
166-
x = new Float32Array( 10 );
167-
for ( i = 0; i < x.length; i++ ) {
168-
if ( randu() < 0.2 ) {
169-
x[ i ] = NaN;
170-
} else {
171-
x[ i ] = round( (randu()*100.0) - 50.0 );
155+
function rand() {
156+
if ( bernoulli( 0.8 ) < 1 ) {
157+
return NaN;
172158
}
159+
return uniform( -50.0, 50.0 );
173160
}
174-
console.log( x );
161+
var x = filledarrayBy( 10, 'float32', rand );
175162

176163
var v = sdsnanmean( x.length, x, 1 );
177164
console.log( v );
@@ -181,6 +168,123 @@ console.log( v );
181168

182169
<!-- /.examples -->
183170

171+
<!-- C interface documentation. -->
172+
173+
* * *
174+
175+
<section class="c">
176+
177+
## C APIs
178+
179+
<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->
180+
181+
<section class="intro">
182+
183+
</section>
184+
185+
<!-- /.intro -->
186+
187+
<!-- C usage documentation. -->
188+
189+
<section class="usage">
190+
191+
### Usage
192+
193+
```c
194+
#include "stdlib/stats/base/sdsnanmean.h"
195+
```
196+
197+
#### stdlib_strided_sdsnanmean( N, \*X, strideX )
198+
199+
Computes the [arithmetic mean][arithmetic-mean] of a single-precision floating-point strided array, ignoring `NaN` values and using extended accumulation.
200+
201+
```c
202+
const float x[] = { 1.0f, 2.0f, 3.0f, 0.0f/0.0f };
203+
204+
float v = stdlib_strided_sdsnanmean( 4, x, 1 );
205+
// returns 2.0f
206+
```
207+
208+
The function accepts the following arguments:
209+
210+
- **N**: `[in] CBLAS_INT` number of indexed elements.
211+
- **X**: `[in] float*` input array.
212+
- **strideX**: `[in] CBLAS_INT` stride length for `X`.
213+
214+
```c
215+
float stdlib_strided_sdsnanmean( const CBLAS_INT N, const float *X, const CBLAS_INT strideX );
216+
```
217+
218+
#### stdlib_strided_sdsnanmean_ndarray( N, \*X, strideX, offsetX )
219+
220+
Computes the [arithmetic mean][arithmetic-mean] of a single-precision floating-point strided array, ignoring `NaN` values and using extended accumulation and alternative indexing semantics.
221+
222+
```c
223+
const float x[] = { 1.0f, 2.0f, 3.0f, 0.0f/0.0f };
224+
225+
float v = stdlib_strided_sdsnanmean_ndarray( 4, x, 1, 0 );
226+
// returns 2.0f
227+
```
228+
229+
The function accepts the following arguments:
230+
231+
- **N**: `[in] CBLAS_INT` number of indexed elements.
232+
- **X**: `[in] float*` input array.
233+
- **strideX**: `[in] CBLAS_INT` stride length for `X`.
234+
- **offsetX**: `[in] CBLAS_INT` starting index for `X`.
235+
236+
```c
237+
float stdlib_strided_sdsnanmean_ndarray( const CBLAS_INT N, const float *X, const CBLAS_INT strideX, const CBLAS_INT offsetX );
238+
```
239+
240+
</section>
241+
242+
<!-- /.usage -->
243+
244+
<!-- C API usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
245+
246+
<section class="notes">
247+
248+
</section>
249+
250+
<!-- /.notes -->
251+
252+
<!-- C API usage examples. -->
253+
254+
<section class="examples">
255+
256+
### Examples
257+
258+
```c
259+
#include "stdlib/stats/base/sdsnanmean.h"
260+
#include <stdio.h>
261+
262+
int main( void ) {
263+
// Create a strided array:
264+
const float x[] = { 1.0, 2.0, 0.0/0.0, 3.0, 0.0/0.0, 4.0, 5.0, 6.0, 0.0/0.0, 7.0, 8.0, 0.0/0.0 };
265+
266+
// Specify the number of elements:
267+
const int N = 6;
268+
269+
// Specify the stride length:
270+
const int strideX = 2;
271+
272+
// Compute the arithmetic mean:
273+
float v = stdlib_strided_sdsnanmean( N, x, strideX );
274+
275+
// Print the result:
276+
printf( "mean: %f\n", v );
277+
}
278+
```
279+
280+
</section>
281+
282+
<!-- /.examples -->
283+
284+
</section>
285+
286+
<!-- /.c -->
287+
184288
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
185289
186290
<section class="related">

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

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -21,16 +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' );
27-
var Float32Array = require( '@stdlib/array/float32' );
2829
var pkg = require( './../package.json' ).name;
2930
var sdsnanmean = require( './../lib/sdsnanmean.js' );
3031

3132

3233
// FUNCTIONS //
3334

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+
3448
/**
3549
* Creates a benchmark function.
3650
*
@@ -39,17 +53,7 @@ var sdsnanmean = require( './../lib/sdsnanmean.js' );
3953
* @returns {Function} benchmark function
4054
*/
4155
function createBenchmark( len ) {
42-
var x;
43-
var i;
44-
45-
x = new Float32Array( len );
46-
for ( i = 0; i < x.length; i++ ) {
47-
if ( randu() < 0.2 ) {
48-
x[ i ] = NaN;
49-
} else {
50-
x[ i ] = ( randu()*20.0 ) - 10.0;
51-
}
52-
}
56+
var x = filledarrayBy( len, 'float32', rand );
5357
return benchmark;
5458

5559
function benchmark( b ) {

lib/node_modules/@stdlib/stats/base/sdsnanmean/benchmark/benchmark.native.js

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,11 @@
2222

2323
var resolve = require( 'path' ).resolve;
2424
var bench = require( '@stdlib/bench' );
25-
var randu = require( '@stdlib/random/base/randu' );
25+
var uniform = require( '@stdlib/random/base/uniform' );
26+
var bernoulli = require( '@stdlib/random/base/bernoulli' );
27+
var filledarrayBy = require( '@stdlib/array/filled-by' );
2628
var isnan = require( '@stdlib/math/base/assert/is-nan' );
2729
var pow = require( '@stdlib/math/base/special/pow' );
28-
var Float32Array = require( '@stdlib/array/float32' );
2930
var tryRequire = require( '@stdlib/utils/try-require' );
3031
var pkg = require( './../package.json' ).name;
3132

@@ -40,6 +41,19 @@ var opts = {
4041

4142
// FUNCTIONS //
4243

44+
/**
45+
* Returns a random value or `NaN`.
46+
*
47+
* @private
48+
* @returns {number} random number or `NaN`
49+
*/
50+
function rand() {
51+
if ( bernoulli( 0.8 ) < 1 ) {
52+
return NaN;
53+
}
54+
return uniform( -10.0, 10.0 );
55+
}
56+
4357
/**
4458
* Creates a benchmark function.
4559
*
@@ -48,17 +62,7 @@ var opts = {
4862
* @returns {Function} benchmark function
4963
*/
5064
function createBenchmark( len ) {
51-
var x;
52-
var i;
53-
54-
x = new Float32Array( len );
55-
for ( i = 0; i < x.length; i++ ) {
56-
if ( randu() < 0.2 ) {
57-
x[ i ] = NaN;
58-
} else {
59-
x[ i ] = ( randu()*20.0 ) - 10.0;
60-
}
61-
}
65+
var x = filledarrayBy( len, 'float32', rand );
6266
return benchmark;
6367

6468
function benchmark( b ) {

0 commit comments

Comments
 (0)