Skip to content

Commit c128f6d

Browse files
committed
feat: add c implementation for stats/base/ndarray/dmskmax
--- 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: passed - task: lint_c_examples status: passed - task: lint_c_benchmarks status: passed - 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 baaed47 commit c128f6d

File tree

19 files changed

+2037
-208
lines changed

19 files changed

+2037
-208
lines changed

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

Lines changed: 168 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,174 @@ console.log( v );
107107

108108
<!-- /.examples -->
109109

110+
<!-- C interface documentation. -->
111+
112+
* * *
113+
114+
<section class="c">
115+
116+
## C APIs
117+
118+
<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->
119+
120+
<section class="intro">
121+
122+
</section>
123+
124+
<!-- /.intro -->
125+
126+
<!-- C usage documentation. -->
127+
128+
<section class="usage">
129+
130+
### Usage
131+
132+
```c
133+
#include "stdlib/stats/base/ndarray/dmskmax.h"
134+
```
135+
136+
#### stdlib_stats_dmskmax( arrays )
137+
138+
Computes the maximum value of a one-dimensional double-precision floating-point ndarray according to a mask.
139+
140+
```c
141+
#include "stdlib/ndarray/ctor.h"
142+
#include "stdlib/ndarray/dtypes.h"
143+
#include "stdlib/ndarray/index_modes.h"
144+
#include "stdlib/ndarray/orders.h"
145+
#include "stdlib/ndarray/base/bytes_per_element.h"
146+
#include <stdint.h>
147+
148+
// Create an ndarray:
149+
const double data[] = { 1.0, 2.0, 3.0, 4.0 };
150+
int64_t shape[] = { 4 };
151+
int64_t strides[] = { STDLIB_NDARRAY_FLOAT64_BYTES_PER_ELEMENT };
152+
int8_t submodes[] = { STDLIB_NDARRAY_INDEX_ERROR };
153+
154+
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 );
155+
156+
// Create a mask ndarray:
157+
const uint8_t mask_data[] = { 0, 0, 1, 0 };
158+
int64_t mask_strides[] = { STDLIB_NDARRAY_UINT8_BYTES_PER_ELEMENT };
159+
160+
struct ndarray *mask = stdlib_ndarray_allocate( STDLIB_NDARRAY_UINT8, (uint8_t *)mask_data, 1, shape, mask_strides, 0, STDLIB_NDARRAY_ROW_MAJOR, STDLIB_NDARRAY_INDEX_ERROR, 1, submodes );
161+
162+
// Compute the maximum value:
163+
const struct ndarray *arrays[] = { x, mask };
164+
double v = stdlib_stats_dmskmax( arrays );
165+
// returns 2.0
166+
167+
// Free allocated memory:
168+
stdlib_ndarray_free( x );
169+
stdlib_ndarray_free( mask );
170+
```
171+
172+
The function accepts the following arguments:
173+
174+
- **arrays**: `[in] struct ndarray**` list containing a one-dimensional input ndarray and a one-dimensional mask ndarray.
175+
176+
```c
177+
double stdlib_stats_dmskmax( const struct ndarray *arrays[] );
178+
```
179+
180+
</section>
181+
182+
<!-- /.usage -->
183+
184+
<!-- C API usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
185+
186+
<section class="notes">
187+
188+
</section>
189+
190+
<!-- /.notes -->
191+
192+
<!-- C API usage examples. -->
193+
194+
<section class="examples">
195+
196+
### Examples
197+
198+
```c
199+
#include "stdlib/stats/base/ndarray/dmskmax.h"
200+
#include "stdlib/ndarray/ctor.h"
201+
#include "stdlib/ndarray/dtypes.h"
202+
#include "stdlib/ndarray/index_modes.h"
203+
#include "stdlib/ndarray/orders.h"
204+
#include "stdlib/ndarray/base/bytes_per_element.h"
205+
#include <stdint.h>
206+
#include <stdlib.h>
207+
#include <stdio.h>
208+
209+
int main( void ) {
210+
// Create a data buffer:
211+
const double data[] = { 1.0, -2.0, 3.0, -4.0, 5.0, -6.0, 7.0, -8.0 };
212+
213+
// Create a mask buffer:
214+
const uint8_t mask_data[] = { 0, 0, 0, 0, 0, 0, 1, 1 };
215+
216+
// Specify the number of array dimensions:
217+
const int64_t ndims = 1;
218+
219+
// Specify the array shape:
220+
int64_t shape[] = { 4 };
221+
222+
// Specify the array strides:
223+
int64_t strides[] = { 2*STDLIB_NDARRAY_FLOAT64_BYTES_PER_ELEMENT };
224+
225+
// Specify the mask strides:
226+
int64_t mask_strides[] = { 2*STDLIB_NDARRAY_UINT8_BYTES_PER_ELEMENT };
227+
228+
// Specify the byte offset:
229+
const int64_t offset = 0;
230+
231+
// Specify the array order:
232+
const enum STDLIB_NDARRAY_ORDER order = STDLIB_NDARRAY_ROW_MAJOR;
233+
234+
// Specify the index mode:
235+
const enum STDLIB_NDARRAY_INDEX_MODE imode = STDLIB_NDARRAY_INDEX_ERROR;
236+
237+
// Specify the subscript index modes:
238+
int8_t submodes[] = { STDLIB_NDARRAY_INDEX_ERROR };
239+
const int64_t nsubmodes = 1;
240+
241+
// Create an ndarray:
242+
struct ndarray *x = stdlib_ndarray_allocate( STDLIB_NDARRAY_FLOAT64, (uint8_t *)data, ndims, shape, strides, offset, order, imode, nsubmodes, submodes );
243+
if ( x == NULL ) {
244+
fprintf( stderr, "Error allocating memory.\n" );
245+
exit( 1 );
246+
}
247+
248+
// Create a mask ndarray:
249+
struct ndarray *mask = stdlib_ndarray_allocate( STDLIB_NDARRAY_UINT8, (uint8_t *)mask_data, ndims, shape, mask_strides, offset, order, imode, nsubmodes, submodes );
250+
if ( mask == NULL ) {
251+
fprintf( stderr, "Error allocating memory.\n" );
252+
exit( 1 );
253+
}
254+
255+
// Define a list of ndarrays:
256+
const struct ndarray *arrays[] = { x, mask };
257+
258+
// Compute the maximum value:
259+
double v = stdlib_stats_dmskmax( arrays );
260+
261+
// Print the result:
262+
printf( "max: %lf\n", v );
263+
264+
// Free allocated memory:
265+
stdlib_ndarray_free( x );
266+
stdlib_ndarray_free( mask );
267+
}
268+
```
269+
270+
</section>
271+
272+
<!-- /.examples -->
273+
274+
</section>
275+
276+
<!-- /.c -->
277+
110278
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
111279
112280
<section class="related">
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
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 bernoulli = require( '@stdlib/random/array/bernoulli' );
27+
var isnan = require( '@stdlib/math/base/assert/is-nan' );
28+
var pow = require( '@stdlib/math/base/special/pow' );
29+
var ndarray = require( '@stdlib/ndarray/base/ctor' );
30+
var format = require( '@stdlib/string/format' );
31+
var tryRequire = require( '@stdlib/utils/try-require' );
32+
var pkg = require( './../package.json' ).name;
33+
34+
35+
// VARIABLES //
36+
37+
var dmskmax = tryRequire( resolve( __dirname, './../lib/native.js' ) );
38+
var opts = {
39+
'skip': ( dmskmax instanceof Error )
40+
};
41+
var options = {
42+
'dtype': 'float64'
43+
};
44+
var moptions = {
45+
'dtype': 'uint8'
46+
};
47+
48+
49+
// FUNCTIONS //
50+
51+
/**
52+
* Creates a benchmark function.
53+
*
54+
* @private
55+
* @param {PositiveInteger} len - array length
56+
* @returns {Function} benchmark function
57+
*/
58+
function createBenchmark( len ) {
59+
var xbuf;
60+
var mbuf;
61+
var mask;
62+
var x;
63+
64+
xbuf = uniform( len, -10.0, 10.0, options );
65+
mbuf = bernoulli( len, 0.05, moptions );
66+
x = new ndarray( options.dtype, xbuf, [ len ], [ 1 ], 0, 'row-major' );
67+
mask = new ndarray( moptions.dtype, mbuf, [ len ], [ 1 ], 0, 'row-major' );
68+
69+
return benchmark;
70+
71+
/**
72+
* Benchmark function.
73+
*
74+
* @private
75+
* @param {Benchmark} b - benchmark instance
76+
*/
77+
function benchmark( b ) {
78+
var v;
79+
var i;
80+
81+
b.tic();
82+
for ( i = 0; i < b.iterations; i++ ) {
83+
v = dmskmax( [ x, mask ] );
84+
if ( isnan( v ) ) {
85+
// We don't fail here as v can be NaN if all elements are masked
86+
}
87+
}
88+
b.toc();
89+
if ( isnan( v ) ) {
90+
// We don't fail here as v can be NaN if all elements are masked
91+
}
92+
b.pass( 'benchmark finished' );
93+
b.end();
94+
}
95+
}
96+
97+
98+
// MAIN //
99+
100+
/**
101+
* Main execution sequence.
102+
*
103+
* @private
104+
*/
105+
function main() {
106+
var len;
107+
var min;
108+
var max;
109+
var f;
110+
var i;
111+
112+
min = 1; // 10^min
113+
max = 6; // 10^min
114+
115+
for ( i = min; i <= max; i++ ) {
116+
len = pow( 10, i );
117+
f = createBenchmark( len );
118+
bench( format( '%s::native:len=%d', pkg, len ), opts, f );
119+
}
120+
}
121+
122+
main();

0 commit comments

Comments
 (0)