Skip to content

Commit 9e08b03

Browse files
ujjwalv01kgryte
andauthored
feat: add C implementation for @stdlib/stats/base/ndarray/dmeanli
PR-URL: #13826 Co-authored-by: Athan Reines <kgryte@gmail.com> Reviewed-by: Athan Reines <kgryte@gmail.com> Signed-off-by: Athan Reines <kgryte@gmail.com>
1 parent 8b8c879 commit 9e08b03

19 files changed

Lines changed: 1787 additions & 116 deletions

File tree

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

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

110110
<!-- /.examples -->
111111

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