Skip to content

Commit 0682ac1

Browse files
MeKaustubh07kgryteSachinn-64
authored
feat: add C implementation for stats/base/ndarray/dmean
PR-URL: stdlib-js#10052 Co-authored-by: Athan Reines <kgryte@gmail.com> Reviewed-by: Athan Reines <kgryte@gmail.com> Co-authored-by: Sachin Pangal <sachinprogramming62@gmail.com> Reviewed-by: Sachin Pangal <sachinprogramming62@gmail.com>
1 parent ebf2a0a commit 0682ac1

File tree

20 files changed

+1810
-131
lines changed

20 files changed

+1810
-131
lines changed

lib/node_modules/@stdlib/stats/base/ndarray/dmean/README.md

Lines changed: 148 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
33
@license Apache-2.0
44
5-
Copyright (c) 2025 The Stdlib Authors.
5+
Copyright (c) 2026 The Stdlib Authors.
66
77
Licensed under the Apache License, Version 2.0 (the "License");
88
you may not use this file except in compliance with the License.
@@ -110,6 +110,153 @@ console.log( v );
110110

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

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

lib/node_modules/@stdlib/stats/base/ndarray/dmean/benchmark/benchmark.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/**
22
* @license Apache-2.0
33
*
4-
* Copyright (c) 2025 The Stdlib Authors.
4+
* Copyright (c) 2026 The Stdlib Authors.
55
*
66
* Licensed under the Apache License, Version 2.0 (the "License");
77
* you may not use this file except in compliance with the License.
@@ -25,8 +25,9 @@ var uniform = require( '@stdlib/random/array/uniform' );
2525
var isnan = require( '@stdlib/math/base/assert/is-nan' );
2626
var pow = require( '@stdlib/math/base/special/pow' );
2727
var ndarray = require( '@stdlib/ndarray/base/ctor' );
28+
var format = require( '@stdlib/string/format' );
2829
var pkg = require( './../package.json' ).name;
29-
var dmean = require( './../lib' );
30+
var dmean = require( './../lib/main.js' );
3031

3132

3233
// VARIABLES //
@@ -101,7 +102,7 @@ function main() {
101102
for ( i = min; i <= max; i++ ) {
102103
len = pow( 10, i );
103104
f = createBenchmark( len );
104-
bench( pkg+':len='+len, f );
105+
bench( format( '%s:len=%d', pkg, len ), f );
105106
}
106107
}
107108

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

0 commit comments

Comments
 (0)