Skip to content

Commit 28dff9b

Browse files
feat: add C implementation for stats/base/ndarray/smeankbn2
1 parent b9396e3 commit 28dff9b

20 files changed

Lines changed: 1806 additions & 126 deletions

File tree

‎lib/node_modules/@stdlib/stats/base/ndarray/smeankbn2/README.md‎

Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,153 @@ console.log( v );
107107

108108
<!-- /.examples -->
109109

110+
<!-- C interface documentation. -->
111+
112+
* * *
113+
114+
<section class="c">
115+
116+
## C APIs
117+
118+
<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->
119+
120+
<section class="intro">
121+
122+
</section>
123+
124+
<!-- /.intro -->
125+
126+
<!-- C usage documentation. -->
127+
128+
<section class="usage">
129+
130+
### Usage
131+
132+
```c
133+
#include "stdlib/stats/base/ndarray/smeankbn2.h"
134+
```
135+
136+
#### stdlib_stats_smeankbn2( arrays )
137+
138+
Computes the arithmetic mean of a one-dimensional single-precision floating-point ndarray using a second-order iterative Kahan–Babuška algorithm.
139+
140+
```c
141+
#include "stdlib/ndarray/ctor.h"
142+
#include "stdlib/ndarray/dtypes.h"
143+
#include "stdlib/ndarray/index_modes.h"
144+
#include "stdlib/ndarray/orders.h"
145+
#include "stdlib/ndarray/base/bytes_per_element.h"
146+
#include <stdint.h>
147+
148+
// Create an ndarray:
149+
const float data[] = { 1.0f, 3.0f, 4.0f, 2.0f };
150+
int64_t shape[] = { 4 };
151+
int64_t strides[] = { STDLIB_NDARRAY_FLOAT32_BYTES_PER_ELEMENT };
152+
int8_t submodes[] = { STDLIB_NDARRAY_INDEX_ERROR };
153+
154+
struct ndarray *x = stdlib_ndarray_allocate( STDLIB_NDARRAY_FLOAT32, (uint8_t *)data, 1, shape, strides, 0, STDLIB_NDARRAY_ROW_MAJOR, STDLIB_NDARRAY_INDEX_ERROR, 1, submodes );
155+
156+
// Compute the arithmetic mean:
157+
const struct ndarray *arrays[] = { x };
158+
float v = stdlib_stats_smeankbn2( arrays );
159+
// returns 2.5f
160+
161+
// Free allocated memory:
162+
stdlib_ndarray_free( x );
163+
```
164+
165+
The function accepts the following arguments:
166+
167+
- **arrays**: `[in] struct ndarray**` list containing a one-dimensional input ndarray.
168+
169+
```c
170+
float stdlib_stats_smeankbn2( const struct ndarray *arrays[] );
171+
```
172+
173+
</section>
174+
175+
<!-- /.usage -->
176+
177+
<!-- C API usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
178+
179+
<section class="notes">
180+
181+
</section>
182+
183+
<!-- /.notes -->
184+
185+
<!-- C API usage examples. -->
186+
187+
<section class="examples">
188+
189+
### Examples
190+
191+
```c
192+
#include "stdlib/stats/base/ndarray/smeankbn2.h"
193+
#include "stdlib/ndarray/ctor.h"
194+
#include "stdlib/ndarray/dtypes.h"
195+
#include "stdlib/ndarray/index_modes.h"
196+
#include "stdlib/ndarray/orders.h"
197+
#include "stdlib/ndarray/base/bytes_per_element.h"
198+
#include <stdint.h>
199+
#include <stdlib.h>
200+
#include <stdio.h>
201+
202+
int main( void ) {
203+
// Create a data buffer:
204+
const float data[] = { 1.0f, -2.0f, 3.0f, -4.0f, 5.0f, -6.0f, 7.0f, -8.0f };
205+
206+
// Specify the number of array dimensions:
207+
const int64_t ndims = 1;
208+
209+
// Specify the array shape:
210+
int64_t shape[] = { 4 };
211+
212+
// Specify the array strides:
213+
int64_t strides[] = { 2*STDLIB_NDARRAY_FLOAT32_BYTES_PER_ELEMENT };
214+
215+
// Specify the byte offset:
216+
const int64_t offset = 0;
217+
218+
// Specify the array order:
219+
const enum STDLIB_NDARRAY_ORDER order = STDLIB_NDARRAY_ROW_MAJOR;
220+
221+
// Specify the index mode:
222+
const enum STDLIB_NDARRAY_INDEX_MODE imode = STDLIB_NDARRAY_INDEX_ERROR;
223+
224+
// Specify the subscript index modes:
225+
int8_t submodes[] = { STDLIB_NDARRAY_INDEX_ERROR };
226+
const int64_t nsubmodes = 1;
227+
228+
// Create an ndarray:
229+
struct ndarray *x = stdlib_ndarray_allocate( STDLIB_NDARRAY_FLOAT32, (uint8_t *)data, ndims, shape, strides, offset, order, imode, nsubmodes, submodes );
230+
if ( x == NULL ) {
231+
fprintf( stderr, "Error allocating memory.\n" );
232+
exit( 1 );
233+
}
234+
235+
// Define a list of ndarrays:
236+
const struct ndarray *arrays[] = { x };
237+
238+
// Compute the arithmetic mean:
239+
float v = stdlib_stats_smeankbn2( arrays );
240+
241+
// Print the result:
242+
printf( "mean: %f\n", v );
243+
244+
// Free allocated memory:
245+
stdlib_ndarray_free( x );
246+
}
247+
```
248+
249+
</section>
250+
251+
<!-- /.examples -->
252+
253+
</section>
254+
255+
<!-- /.c -->
256+
110257
* * *
111258
112259
<section class="references">

‎lib/node_modules/@stdlib/stats/base/ndarray/smeankbn2/benchmark/benchmark.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ var isnanf = require( '@stdlib/math/base/assert/is-nanf' );
2626
var pow = require( '@stdlib/math/base/special/pow' );
2727
var format = require( '@stdlib/string/format' );
2828
var pkg = require( './../package.json' ).name;
29-
var smeankbn2 = require( './../lib' );
29+
var smeankbn2 = require( './../lib/main.js' );
3030

3131

3232
// VARIABLES //
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2026 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+
// MODULES //
22+
23+
var resolve = require( 'path' ).resolve;
24+
var bench = require( '@stdlib/bench' );
25+
var uniform = require( '@stdlib/random/uniform' );
26+
var isnanf = require( '@stdlib/math/base/assert/is-nanf' );
27+
var pow = require( '@stdlib/math/base/special/pow' );
28+
var format = require( '@stdlib/string/format' );
29+
var tryRequire = require( '@stdlib/utils/try-require' );
30+
var pkg = require( './../package.json' ).name;
31+
32+
33+
// VARIABLES //
34+
35+
var smeankbn2 = tryRequire( resolve( __dirname, './../lib/native.js' ) );
36+
var opts = {
37+
'skip': ( smeankbn2 instanceof Error )
38+
};
39+
var options = {
40+
'dtype': 'float32'
41+
};
42+
43+
44+
// FUNCTIONS //
45+
46+
/**
47+
* Creates a benchmark function.
48+
*
49+
* @private
50+
* @param {PositiveInteger} len - array length
51+
* @returns {Function} benchmark function
52+
*/
53+
function createBenchmark( len ) {
54+
var x = uniform( [ len ], -10.0, 10.0, options );
55+
return benchmark;
56+
57+
/**
58+
* Benchmark function.
59+
*
60+
* @private
61+
* @param {Benchmark} b - benchmark instance
62+
*/
63+
function benchmark( b ) {
64+
var v;
65+
var i;
66+
67+
b.tic();
68+
for ( i = 0; i < b.iterations; i++ ) {
69+
v = smeankbn2( [ x ] );
70+
if ( isnanf( v ) ) {
71+
b.fail( 'should not return NaN' );
72+
}
73+
}
74+
b.toc();
75+
if ( isnanf( v ) ) {
76+
b.fail( 'should not return NaN' );
77+
}
78+
b.pass( 'benchmark finished' );
79+
b.end();
80+
}
81+
}
82+
83+
84+
// MAIN //
85+
86+
/**
87+
* Main execution sequence.
88+
*
89+
* @private
90+
*/
91+
function main() {
92+
var len;
93+
var min;
94+
var max;
95+
var f;
96+
var i;
97+
98+
min = 1; // 10^min
99+
max = 6; // 10^max
100+
101+
for ( i = min; i <= max; i++ ) {
102+
len = pow( 10, i );
103+
f = createBenchmark( len );
104+
bench( format( '%s::native:len=%d', pkg, len ), opts, f );
105+
}
106+
}
107+
108+
main();

0 commit comments

Comments
 (0)