Skip to content

Commit 5dfd41d

Browse files
feat: add C implementation for stats/base/ndarray/srangeabs
PR-URL: stdlib-js#10240 Co-authored-by: Philipp Burckhardt <pburckhardt@outlook.com> Reviewed-by: Philipp Burckhardt <pburckhardt@outlook.com> Signed-off-by: Bhargav Dabhade <bhargava2005dabhade@gmail.com> Signed-off-by: Philipp Burckhardt <pburckhardt@outlook.com>
1 parent 564b95d commit 5dfd41d

File tree

20 files changed

+1807
-129
lines changed

20 files changed

+1807
-129
lines changed

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

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

9898
<!-- /.examples -->
9999

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

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,12 @@
2222

2323
var bench = require( '@stdlib/bench' );
2424
var uniform = require( '@stdlib/random/array/uniform' );
25-
var isnan = require( '@stdlib/math/base/assert/is-nan' );
25+
var isnanf = require( '@stdlib/math/base/assert/is-nanf' );
2626
var pow = require( '@stdlib/math/base/special/pow' );
2727
var ndarray = require( '@stdlib/ndarray/base/ctor' );
2828
var format = require( '@stdlib/string/format' );
2929
var pkg = require( './../package.json' ).name;
30-
var srangeabs = require( './../lib' );
30+
var srangeabs = require( './../lib/main.js' );
3131

3232

3333
// VARIABLES //
@@ -68,12 +68,12 @@ function createBenchmark( len ) {
6868
b.tic();
6969
for ( i = 0; i < b.iterations; i++ ) {
7070
v = srangeabs( [ x ] );
71-
if ( isnan( v ) ) {
71+
if ( isnanf( v ) ) {
7272
b.fail( 'should not return NaN' );
7373
}
7474
}
7575
b.toc();
76-
if ( isnan( v ) ) {
76+
if ( isnanf( v ) ) {
7777
b.fail( 'should not return NaN' );
7878
}
7979
b.pass( 'benchmark finished' );
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 isnanf = require( '@stdlib/math/base/assert/is-nanf' );
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 srangeabs = tryRequire( resolve( __dirname, './../lib/native.js' ) );
37+
var opts = {
38+
'skip': ( srangeabs instanceof Error )
39+
};
40+
var options = {
41+
'dtype': 'float32'
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 = srangeabs( [ x ] );
76+
if ( isnanf( v ) ) {
77+
b.fail( 'should not return NaN' );
78+
}
79+
}
80+
b.toc();
81+
if ( isnanf( 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)