-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
feat: add C implementation for stats/base/ndarray/dnanmidrange
#10203
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: develop
Are you sure you want to change the base?
Changes from all commits
bf931e2
45bb681
332c205
cfb890d
b510fdc
b1a3c12
d22d023
4cba23e
e4faa66
c3b1ebb
0cd3b87
1d43e48
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -104,6 +104,153 @@ console.log( v ); | |||||
|
|
||||||
| <!-- /.examples --> | ||||||
|
|
||||||
| <!-- C interface documentation. --> | ||||||
|
|
||||||
| * * * | ||||||
|
|
||||||
| <section class="c"> | ||||||
|
|
||||||
| ## C APIs | ||||||
|
|
||||||
| <!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. --> | ||||||
|
|
||||||
| <section class="intro"> | ||||||
|
|
||||||
| </section> | ||||||
|
|
||||||
| <!-- /.intro --> | ||||||
|
|
||||||
| <!-- C usage documentation. --> | ||||||
|
|
||||||
| <section class="usage"> | ||||||
|
|
||||||
| ### Usage | ||||||
|
|
||||||
| ```c | ||||||
| #include "stdlib/stats/base/ndarray/dnanmidrange.h" | ||||||
| ``` | ||||||
|
|
||||||
| stdlib_stats_dnanmidrange( arrays ) | ||||||
|
|
||||||
| Computes the mid-range of a one-dimensional double-precision floating-point ndarray, ignoring NaN values. | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
|
|
||||||
| ```c | ||||||
| #include "stdlib/ndarray/ctor.h" | ||||||
| #include "stdlib/ndarray/dtypes.h" | ||||||
| #include "stdlib/ndarray/index_modes.h" | ||||||
| #include "stdlib/ndarray/orders.h" | ||||||
| #include "stdlib/ndarray/base/bytes_per_element.h" | ||||||
| #include <stdint.h> | ||||||
|
|
||||||
| // Create an ndarray: | ||||||
| const double data[] = { 1.0, NaN, -2.0, 3.0, -4.0 }; | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Instead of using NAN, we should prefer the expression 0.0/0.0 and remove the math.h include, as Athan mentioned here: 5a64342#r179146895 |
||||||
| int64_t shape[] = { 5 }; | ||||||
| int64_t strides[] = { STDLIB_NDARRAY_FLOAT64_BYTES_PER_ELEMENT }; | ||||||
| int8_t submodes[] = { STDLIB_NDARRAY_INDEX_ERROR }; | ||||||
|
|
||||||
| 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 ); | ||||||
|
|
||||||
| // Compute the mid-range: | ||||||
| const struct ndarray *arrays[] = { x }; | ||||||
| double v = stdlib_stats_dnanmidrange( arrays ); | ||||||
| // returns -0.5 | ||||||
|
|
||||||
| // Free allocated memory: | ||||||
| stdlib_ndarray_free( x ); | ||||||
| ``` | ||||||
|
|
||||||
| The function accepts the following arguments: | ||||||
|
|
||||||
| - **arrays**: `[in] struct ndarray**` list containing a one-dimensional input ndarray. | ||||||
|
|
||||||
| ```c | ||||||
| double stdlib_stats_dnanmidrange( const struct ndarray *arrays[] ); | ||||||
| ``` | ||||||
|
|
||||||
| </section> | ||||||
|
|
||||||
| <!-- /.usage --> | ||||||
|
|
||||||
| <!-- C API usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> | ||||||
|
|
||||||
| <section class="notes"> | ||||||
|
|
||||||
| </section> | ||||||
|
|
||||||
| <!-- /.notes --> | ||||||
|
|
||||||
| <!-- C API usage examples. --> | ||||||
|
|
||||||
| <section class="examples"> | ||||||
|
|
||||||
| ### Examples | ||||||
|
|
||||||
| ```c | ||||||
| #include "stdlib/stats/base/ndarray/dnanmidrange.h" | ||||||
| #include "stdlib/ndarray/ctor.h" | ||||||
| #include "stdlib/ndarray/dtypes.h" | ||||||
| #include "stdlib/ndarray/index_modes.h" | ||||||
| #include "stdlib/ndarray/orders.h" | ||||||
| #include "stdlib/ndarray/base/bytes_per_element.h" | ||||||
| #include <stdint.h> | ||||||
| #include <stdlib.h> | ||||||
| #include <stdio.h> | ||||||
|
|
||||||
| int main( void ) { | ||||||
| // Create a data buffer (includes NaN): | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| const double data[] = { 1.0, -2.0, 3.0, NaN, 5.0, -6.0, 7.0, -8.0 }; | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same thing here --
Suggested change
|
||||||
|
|
||||||
| // Specify the number of array dimensions: | ||||||
| const int64_t ndims = 1; | ||||||
|
|
||||||
| // Specify the array shape: | ||||||
| int64_t shape[] = { 4 }; | ||||||
|
|
||||||
| // Specify the array strides: | ||||||
| int64_t strides[] = { 2*STDLIB_NDARRAY_FLOAT64_BYTES_PER_ELEMENT }; | ||||||
|
|
||||||
| // Specify the byte offset: | ||||||
| const int64_t offset = 0; | ||||||
|
|
||||||
| // Specify the array order: | ||||||
| const enum STDLIB_NDARRAY_ORDER order = STDLIB_NDARRAY_ROW_MAJOR; | ||||||
|
|
||||||
| // Specify the index mode: | ||||||
| const enum STDLIB_NDARRAY_INDEX_MODE imode = STDLIB_NDARRAY_INDEX_ERROR; | ||||||
|
|
||||||
| // Specify the subscript index modes: | ||||||
| int8_t submodes[] = { STDLIB_NDARRAY_INDEX_ERROR }; | ||||||
| const int64_t nsubmodes = 1; | ||||||
|
|
||||||
| // Create an ndarray: | ||||||
| struct ndarray *x = stdlib_ndarray_allocate( STDLIB_NDARRAY_FLOAT64, (uint8_t *)data, ndims, shape, strides, offset, order, imode, nsubmodes, submodes ); | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| if ( x == NULL ) { | ||||||
| fprintf( stderr, "Error allocating memory.\n" ); | ||||||
| exit( 1 ); | ||||||
| } | ||||||
|
|
||||||
| // Define a list of ndarrays: | ||||||
| const struct ndarray *arrays[] = { x }; | ||||||
|
|
||||||
| // Compute the NaN-ignoring midrange: | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| double v = stdlib_stats_dnanmidrange( arrays ); | ||||||
|
|
||||||
| // Print the result: | ||||||
| printf( "midrange: %lf\n", v ); | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
|
|
||||||
| // Free allocated memory: | ||||||
| stdlib_ndarray_free( x ); | ||||||
| } | ||||||
| ``` | ||||||
|
|
||||||
| </section> | ||||||
|
|
||||||
| <!-- /.examples --> | ||||||
|
|
||||||
| </section> | ||||||
|
|
||||||
| <!-- /.c --> | ||||||
|
|
||||||
| <!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. --> | ||||||
|
|
||||||
| <section class="related"> | ||||||
|
|
||||||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,126 @@ | ||||||||||||||||||||
| /** | ||||||||||||||||||||
| * @license Apache-2.0 | ||||||||||||||||||||
| * | ||||||||||||||||||||
| * Copyright (c) 2026 The Stdlib Authors. | ||||||||||||||||||||
| * | ||||||||||||||||||||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||||||||||||||||||||
| * you may not use this file except in compliance with the License. | ||||||||||||||||||||
| * You may obtain a copy of the License at | ||||||||||||||||||||
| * | ||||||||||||||||||||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||||||||||||||||||||
| * | ||||||||||||||||||||
| * Unless required by applicable law or agreed to in writing, software | ||||||||||||||||||||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||||||||||||||||||||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||||||||||||||||||||
| * See the License for the specific language governing permissions and | ||||||||||||||||||||
| * limitations under the License. | ||||||||||||||||||||
| */ | ||||||||||||||||||||
|
|
||||||||||||||||||||
| 'use strict'; | ||||||||||||||||||||
|
|
||||||||||||||||||||
| // MODULES // | ||||||||||||||||||||
|
|
||||||||||||||||||||
| var resolve = require( 'path' ).resolve; | ||||||||||||||||||||
| var bench = require( '@stdlib/bench' ); | ||||||||||||||||||||
| var uniform = require( '@stdlib/random/base/uniform' ); | ||||||||||||||||||||
| var bernoulli = require( '@stdlib/random/base/bernoulli' ); | ||||||||||||||||||||
| var filledarrayBy = require( '@stdlib/array/filled-by' ); | ||||||||||||||||||||
| var isnan = require( '@stdlib/math/base/assert/is-nan' ); | ||||||||||||||||||||
| var pow = require( '@stdlib/math/base/special/pow' ); | ||||||||||||||||||||
| var format = require( '@stdlib/string/format' ); | ||||||||||||||||||||
| var ndarray = require( '@stdlib/ndarray/base/ctor' ); | ||||||||||||||||||||
| var tryRequire = require( '@stdlib/utils/try-require' ); | ||||||||||||||||||||
| var pkg = require( './../package.json' ).name; | ||||||||||||||||||||
|
|
||||||||||||||||||||
|
|
||||||||||||||||||||
| // VARIABLES // | ||||||||||||||||||||
|
|
||||||||||||||||||||
| var dnanmidrange = tryRequire( resolve( __dirname, './../lib/native.js' ) ); | ||||||||||||||||||||
| var opts = { | ||||||||||||||||||||
| 'skip': ( dnanmidrange instanceof Error ) | ||||||||||||||||||||
| }; | ||||||||||||||||||||
|
Comment on lines
+39
to
+41
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||||
|
|
||||||||||||||||||||
|
|
||||||||||||||||||||
| // FUNCTIONS // | ||||||||||||||||||||
|
|
||||||||||||||||||||
| /** | ||||||||||||||||||||
| * Returns a random number. | ||||||||||||||||||||
| * | ||||||||||||||||||||
| * @private | ||||||||||||||||||||
| * @returns {number} random number or `NaN` | ||||||||||||||||||||
| */ | ||||||||||||||||||||
| function rand() { | ||||||||||||||||||||
| if ( bernoulli( 0.8 ) < 1 ) { | ||||||||||||||||||||
| return NaN; | ||||||||||||||||||||
| } | ||||||||||||||||||||
| return uniform( -10.0, 10.0 ); | ||||||||||||||||||||
| } | ||||||||||||||||||||
|
|
||||||||||||||||||||
| /** | ||||||||||||||||||||
| * Creates a benchmark function. | ||||||||||||||||||||
| * | ||||||||||||||||||||
| * @private | ||||||||||||||||||||
| * @param {PositiveInteger} len - array length | ||||||||||||||||||||
| * @returns {Function} benchmark function | ||||||||||||||||||||
| */ | ||||||||||||||||||||
| function createBenchmark( len ) { | ||||||||||||||||||||
| var xbuf; | ||||||||||||||||||||
| var x; | ||||||||||||||||||||
|
|
||||||||||||||||||||
| xbuf = filledarrayBy( len, 'float64', rand ); | ||||||||||||||||||||
| x = new ndarray( 'float64', xbuf, [ len ], [ 1 ], 0, 'row-major' ); | ||||||||||||||||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||||
|
|
||||||||||||||||||||
| return benchmark; | ||||||||||||||||||||
|
|
||||||||||||||||||||
| /** | ||||||||||||||||||||
| * Benchmark function. | ||||||||||||||||||||
| * | ||||||||||||||||||||
| * @private | ||||||||||||||||||||
| * @param {Benchmark} b - benchmark instance | ||||||||||||||||||||
| */ | ||||||||||||||||||||
| function benchmark( b ) { | ||||||||||||||||||||
| var v; | ||||||||||||||||||||
| var i; | ||||||||||||||||||||
|
|
||||||||||||||||||||
| b.tic(); | ||||||||||||||||||||
| for ( i = 0; i < b.iterations; i++ ) { | ||||||||||||||||||||
| v = dnanmidrange( [ x ] ); | ||||||||||||||||||||
| if ( isnan( v ) ) { | ||||||||||||||||||||
| b.fail( 'should not return NaN' ); | ||||||||||||||||||||
| } | ||||||||||||||||||||
| } | ||||||||||||||||||||
| b.toc(); | ||||||||||||||||||||
| if ( isnan( v ) ) { | ||||||||||||||||||||
| b.fail( 'should not return NaN' ); | ||||||||||||||||||||
| } | ||||||||||||||||||||
| b.pass( 'benchmark finished' ); | ||||||||||||||||||||
| b.end(); | ||||||||||||||||||||
| } | ||||||||||||||||||||
| } | ||||||||||||||||||||
|
|
||||||||||||||||||||
|
|
||||||||||||||||||||
| // MAIN // | ||||||||||||||||||||
|
|
||||||||||||||||||||
| /** | ||||||||||||||||||||
| * Main execution sequence. | ||||||||||||||||||||
| * | ||||||||||||||||||||
| * @private | ||||||||||||||||||||
| */ | ||||||||||||||||||||
| function main() { | ||||||||||||||||||||
| var len; | ||||||||||||||||||||
| var min; | ||||||||||||||||||||
| var max; | ||||||||||||||||||||
| var f; | ||||||||||||||||||||
| var i; | ||||||||||||||||||||
|
|
||||||||||||||||||||
| min = 1; // 10^min | ||||||||||||||||||||
| max = 6; // 10^max | ||||||||||||||||||||
|
|
||||||||||||||||||||
| for ( i = min; i <= max; i++ ) { | ||||||||||||||||||||
| len = pow( 10, i ); | ||||||||||||||||||||
| f = createBenchmark( len ); | ||||||||||||||||||||
| bench( format( '%s::native:len=%d', pkg, len ), opts, f ); | ||||||||||||||||||||
| } | ||||||||||||||||||||
| } | ||||||||||||||||||||
|
|
||||||||||||||||||||
| main(); | ||||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This needs to be formatted as a
####heading to match our convention for C function signatures. See, e.g., thedmaxREADME for reference.