Skip to content

Commit 275c3bc

Browse files
committed
feat: add C implementation for stats/base/ndarray/smaxsorted
--- type: pre_commit_static_analysis_report description: Results of running static analysis checks when committing changes. report: - task: lint_filenames status: passed - task: lint_editorconfig status: passed - task: lint_markdown status: na - task: lint_package_json status: na - task: lint_repl_help status: na - task: lint_javascript_src status: na - task: lint_javascript_cli status: na - task: lint_javascript_examples status: na - task: lint_javascript_tests status: na - task: lint_javascript_benchmarks status: na - task: lint_python status: na - task: lint_r status: na - task: lint_c_src status: na - task: lint_c_examples status: na - task: lint_c_benchmarks status: na - task: lint_c_tests_fixtures status: na - task: lint_shell status: na - task: lint_typescript_declarations status: passed - task: lint_typescript_tests status: na - task: lint_license_headers status: passed ---
1 parent 0a3bfa2 commit 275c3bc

File tree

20 files changed

+1810
-135
lines changed

20 files changed

+1810
-135
lines changed

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

Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,151 @@ console.log( v );
9696

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

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

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,9 @@ var linspace = require( '@stdlib/array/linspace' );
2525
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' );
28+
var format = require( '@stdlib/string/format' );
2829
var pkg = require( './../package.json' ).name;
29-
var smaxsorted = require( './../lib' );
30+
var smaxsorted = 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 linspace = require( '@stdlib/array/linspace' );
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 smaxsorted = tryRequire( resolve( __dirname, './../lib/native.js' ) );
37+
var opts = {
38+
'skip': ( smaxsorted 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 = linspace( -len/2, len/2, len, 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 = smaxsorted( [ 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)