Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,23 @@

// MODULES //

var join = require( 'path' ).join;
var tryRequire = require( '@stdlib/utils/try-require' );
var isError = require( '@stdlib/assert/is-error' );
var main = require( './main.js' );


// MAIN //

var snanmskmax;
var tmp = tryRequire( join( __dirname, './native.js' ) );
if ( isError( tmp ) ) {
snanmskmax = main;
} else {
snanmskmax = tmp;
}


// EXPORTS //

module.exports = main;
module.exports = snanmskmax;
128 changes: 127 additions & 1 deletion lib/node_modules/@stdlib/stats/base/ndarray/snanmskmin/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

@license Apache-2.0

Copyright (c) 2025 The Stdlib Authors.
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.
Expand Down Expand Up @@ -103,6 +103,132 @@ console.log( v );

<!-- /.examples -->

<!-- C API documentation. -->

<section class="c">

## C APIs

<section class="intro">

</section>

<!-- /.intro -->

<section class="usage">

### Usage

```c
#include "stdlib/stats/base/ndarray/snanmskmin.h"
```

#### stdlib_stats_snanmskmin( arrays )

Compute the minimum value of a one-dimensional single-precision floating-point ndarray according to a mask, ignoring `NaN` values.

```c
#include "stdlib/ndarray/ctor.h"
#include "stdlib/ndarray/dtypes.h"
#include "stdlib/ndarray/orders.h"
#include "stdlib/ndarray/index_modes.h"
#include <stdint.h>

// ...

// Create ndarray and mask...
struct ndarray *x = stdlib_ndarray_allocate( STDLIB_NDARRAY_FLOAT32, (uint8_t *)data, ndims, shape, xstrides, offset, order, imode, nsubmodes, submodes );
struct ndarray *mask = stdlib_ndarray_allocate( STDLIB_NDARRAY_UINT8, (uint8_t *)mdata, ndims, shape, mstrides, offset, order, imode, nsubmodes, submodes );

const struct ndarray *arrays[] = { x, mask };

float v = stdlib_stats_snanmskmin( arrays );

// ...

// Free memory:
stdlib_ndarray_free( x );
stdlib_ndarray_free( mask );
```

The function accepts the following arguments:

- **arrays**: `[2](const struct ndarray *)` array containing ndarrays (a one-dimensional input ndarray and a one-dimensional mask ndarray).

```c
float stdlib_stats_snanmskmin( const struct ndarray *arrays[] );
```

</section>

<!-- /.usage -->

<section class="notes">

</section>

<!-- /.notes -->

<section class="examples">

### Examples

```c
#include "stdlib/stats/base/ndarray/snanmskmin.h"
#include "stdlib/ndarray/ctor.h"
#include "stdlib/ndarray/dtypes.h"
#include "stdlib/ndarray/orders.h"
#include "stdlib/ndarray/index_modes.h"
#include <stdint.h>
#include <stdio.h>

int main( void ) {
float x[] = { 1.0f, -2.0f, 4.0f, 2.0f, 0.0f/0.0f };
uint8_t mask[] = { 0, 0, 1, 0, 0 };

int64_t ndims = 1;
int64_t shape[] = { 5 };
int64_t strides[] = { 4 };
int64_t mstrides[] = { 1 };
int64_t offset = 0;
enum STDLIB_NDARRAY_ORDER order = STDLIB_NDARRAY_ROW_MAJOR;
enum STDLIB_NDARRAY_INDEX_MODE imode = STDLIB_NDARRAY_INDEX_ERROR;
int8_t submodes[] = { imode };
int64_t nsubmodes = 1;

struct ndarray *X = stdlib_ndarray_allocate( STDLIB_NDARRAY_FLOAT32, (uint8_t *)x, ndims, shape, strides, offset, order, imode, nsubmodes, submodes );
if ( X == NULL ) {
printf( "Error allocating memory.\n" );
return 1;
}

struct ndarray *Mask = stdlib_ndarray_allocate( STDLIB_NDARRAY_UINT8, (uint8_t *)mask, ndims, shape, mstrides, offset, order, imode, nsubmodes, submodes );
if ( Mask == NULL ) {
printf( "Error allocating memory.\n" );
stdlib_ndarray_free( X );
return 1;
}

const struct ndarray *arrays[] = { X, Mask };

float v = stdlib_stats_snanmskmin( arrays );
printf( "min: %f\n", v );

stdlib_ndarray_free( X );
stdlib_ndarray_free( Mask );

return 0;
}
```

</section>

<!-- /.examples -->

</section>

<!-- /.c -->

<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->

<section class="related">
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* @license Apache-2.0
*
* Copyright (c) 2025 The Stdlib Authors.
* 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.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
/**
* @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 fillBy = require( '@stdlib/ndarray/fill-by' );
var zeros = require( '@stdlib/ndarray/zeros' );
var isnanf = require( '@stdlib/math/base/assert/is-nanf' );
var pow = require( '@stdlib/math/base/special/pow' );
var format = require( '@stdlib/string/format' );
var tryRequire = require( '@stdlib/utils/try-require' );
var isError = require( '@stdlib/assert/is-error' );
var pkg = require( './../package.json' ).name;

var snanmskmin = tryRequire( resolve( __dirname, './../lib/native.js' ) );
var opts = {
'skip': isError( snanmskmin )
};


// VARIABLES //

var options = {
'dtype': 'float32'
};
var moptions = {
'dtype': 'uint8'
};


// FUNCTIONS //

/**
* Returns a random number.
*
* @private
* @returns {number} random number
*/
function rand() {
if ( bernoulli( 0.8 ) < 1 ) {
return NaN;
}
return uniform( -10.0, 10.0 );
}

/**
* Returns a random mask value.
*
* @private
* @returns {integer} random mask value
*/
function mrand() {
return bernoulli( 0.2 );
}

/**
* Creates a benchmark function.
*
* @private
* @param {PositiveInteger} len - array length
* @returns {Function} benchmark function
*/
function createBenchmark( len ) {
var mask;
var x;

x = fillBy( zeros( [ len ], options ), rand );
mask = fillBy( zeros( [ len ], moptions ), mrand );

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++ ) {
x.set( i%len, i );
v = snanmskmin( [ x, mask ] );
if ( isnanf( v ) ) {
b.fail( 'should not return NaN' );
}
}
b.toc();
if ( isnanf( 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();
Loading