Skip to content

Commit 892e69e

Browse files
committed
feat: add C implementation for stats/base/ndarray/smaxabs
--- 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: passed - task: lint_package_json status: passed - task: lint_repl_help status: na - task: lint_javascript_src status: passed - task: lint_javascript_cli status: na - task: lint_javascript_examples status: na - task: lint_javascript_tests status: passed - task: lint_javascript_benchmarks status: passed - task: lint_python status: na - task: lint_r status: na - task: lint_c_src status: missing_dependencies - task: lint_c_examples status: missing_dependencies - task: lint_c_benchmarks status: missing_dependencies - 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 ee4e852 commit 892e69e

File tree

20 files changed

+1796
-161
lines changed

20 files changed

+1796
-161
lines changed

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

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

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

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

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

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +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' );
28+
var format = require( '@stdlib/string/format' );
2829
var pkg = require( './../package.json' ).name;
29-
var smaxabs = require( './../lib' );
30+
var smaxabs = require( './../lib/main.js' );
3031

3132

3233
// VARIABLES //
@@ -45,12 +46,12 @@ var options = {
4546
* @param {PositiveInteger} len - array length
4647
* @returns {Function} benchmark function
4748
*/
48-
function createBenchmark( len ) {
49+
function createBenchmark(len) {
4950
var xbuf;
5051
var x;
5152

52-
xbuf = uniform( len, -10.0, 10.0, options );
53-
x = new ndarray( options.dtype, xbuf, [ len ], [ 1 ], 0, 'row-major' );
53+
xbuf = uniform(len, -10.0, 10.0, options);
54+
x = new ndarray(options.dtype, xbuf, [len], [1], 0, 'row-major');
5455

5556
return benchmark;
5657

@@ -60,22 +61,22 @@ function createBenchmark( len ) {
6061
* @private
6162
* @param {Benchmark} b - benchmark instance
6263
*/
63-
function benchmark( b ) {
64+
function benchmark(b) {
6465
var v;
6566
var i;
6667

6768
b.tic();
68-
for ( i = 0; i < b.iterations; i++ ) {
69-
v = smaxabs( [ x ] );
70-
if ( isnan( v ) ) {
71-
b.fail( 'should not return NaN' );
69+
for (i = 0; i < b.iterations; i++) {
70+
v = smaxabs([x]);
71+
if (isnanf(v)) {
72+
b.fail('should not return NaN');
7273
}
7374
}
7475
b.toc();
75-
if ( isnan( v ) ) {
76-
b.fail( 'should not return NaN' );
76+
if (isnanf(v)) {
77+
b.fail('should not return NaN');
7778
}
78-
b.pass( 'benchmark finished' );
79+
b.pass('benchmark finished');
7980
b.end();
8081
}
8182
}
@@ -98,10 +99,10 @@ function main() {
9899
min = 1; // 10^min
99100
max = 6; // 10^max
100101

101-
for ( i = min; i <= max; i++ ) {
102-
len = pow( 10, i );
103-
f = createBenchmark( len );
104-
bench( pkg+':len='+len, f );
102+
for (i = min; i <= max; i++) {
103+
len = pow(10, i);
104+
f = createBenchmark(len);
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 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 smaxabs = tryRequire(resolve(__dirname, './../lib/native.js'));
37+
var opts = {
38+
'skip': (smaxabs 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 = smaxabs([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)