Skip to content

Commit 77c3ddb

Browse files
committed
feat: add C implementation for stats/base/ndarray/snanmax
1 parent 0a3bfa2 commit 77c3ddb

20 files changed

Lines changed: 1848 additions & 3 deletions

File tree

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

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

103103
<!-- /.examples -->
104104

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

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,9 @@ var filledarrayBy = require( '@stdlib/array/filled-by' );
2727
var isnanf = require( '@stdlib/math/base/assert/is-nanf' );
2828
var pow = require( '@stdlib/math/base/special/pow' );
2929
var ndarray = require( '@stdlib/ndarray/base/ctor' );
30+
var format = require( '@stdlib/string/format' );
3031
var pkg = require( './../package.json' ).name;
31-
var snanmax = require( './../lib' );
32+
var snanmax = require( './../lib/main.js' );
3233

3334

3435
// FUNCTIONS //
@@ -109,7 +110,7 @@ function main() {
109110
for ( i = min; i <= max; i++ ) {
110111
len = pow( 10, i );
111112
f = createBenchmark( len );
112-
bench( pkg+':len='+len, f );
113+
bench( format( '%s:len=%d', pkg, len ), f );
113114
}
114115
}
115116

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

0 commit comments

Comments
 (0)