Skip to content
Open
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
140 changes: 138 additions & 2 deletions lib/node_modules/@stdlib/stats/base/ndarray/smskrange/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 @@ -40,7 +40,7 @@ var smskrange = require( '@stdlib/stats/base/ndarray/smskrange' );

#### smskrange( arrays )

Computes the [range][range] of a one-dimensional single-precision floating-point ndarray according to a mask.
Compute the [range][range] of a one-dimensional single-precision floating-point ndarray according to a mask.

```javascript
var Float32Vector = require( '@stdlib/ndarray/vector/float32' );
Expand Down Expand Up @@ -105,6 +105,142 @@ console.log( v );

<!-- /.examples -->

<section class="c">

## C APIs

<section class="intro">

</section>

<!-- /.intro -->

<section class="usage">

### Usage

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

#### stdlib_stats_smskrange( arrays )

Compute the range of a one-dimensional single-precision floating-point ndarray according to a mask.

```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_smskrange( 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_smskrange( const struct ndarray *arrays[] );
```

</section>

<!-- /.usage -->

<section class="notes">

</section>

<!-- /.notes -->

<section class="examples">

### Examples

```c
#include "stdlib/stats/base/ndarray/smskrange.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 };
uint8_t mask[] = { 0, 0, 1, 0 };

int64_t ndims = 1;
int64_t shape[] = { 4 };
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_smskrange( arrays );
printf( "Range: %f\n", v );

stdlib_ndarray_free( X );
stdlib_ndarray_free( Mask );

return 0;
}
```

</section>

<!-- /.examples -->

<section class="related">

</section>

<!-- /.related -->

<section class="links">

</section>

<!-- /.links -->

</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,118 @@
/**
* @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 join = require( 'path' ).join;
var bench = require( '@stdlib/bench' );
var isnanf = require( '@stdlib/math/base/assert/is-nanf' );
var pow = require( '@stdlib/math/base/special/pow' );
var bernoulli = require( '@stdlib/random/bernoulli' );
var uniform = require( '@stdlib/random/uniform' );
var format = require( '@stdlib/string/format' );
var tryRequire = require( '@stdlib/utils/try-require' );
var pkg = require( './../package.json' ).name;
var smskrange = tryRequire( join( __dirname, './../lib/native.js' ) );
var opts = {
'skip': ( smskrange instanceof Error )
};


// VARIABLES //

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


// FUNCTIONS //

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

x = uniform( [ len ], -100.0, 100.0, xoptions );
mask = bernoulli( [ len ], 0.2, moptions );

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 = smskrange( [ 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