Skip to content

Commit 3edb15d

Browse files
gururaj1512kgrytestdlib-bot
authored
feat: add C ndarray interface and refactor implementation for stats/base/snanmskmin
PR-URL: #7237 Co-authored-by: Athan Reines <[email protected]> Reviewed-by: Athan Reines <[email protected]> Co-authored-by: stdlib-bot <[email protected]>
1 parent f03bce1 commit 3edb15d

22 files changed

+450
-415
lines changed

lib/node_modules/@stdlib/stats/base/snanmskmin/README.md

Lines changed: 160 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ limitations under the License.
1818
1919
-->
2020

21+
<!-- lint disable maximum-heading-length -->
22+
2123
# snanmskmin
2224

2325
> Calculate the minimum value of a single-precision floating-point strided array according to a mask, ignoring `NaN` values.
@@ -38,7 +40,7 @@ var snanmskmin = require( '@stdlib/stats/base/snanmskmin' );
3840

3941
#### snanmskmin( N, x, strideX, mask, strideMask )
4042

41-
Computes the minimum value of a single-precision floating-point strided array `x` according to a `mask`, ignoring `NaN` values.
43+
Computes the minimum value of a single-precision floating-point strided array according to a `mask`, ignoring `NaN` values.
4244

4345
```javascript
4446
var Float32Array = require( '@stdlib/array/float32' );
@@ -55,22 +57,20 @@ The function has the following parameters:
5557

5658
- **N**: number of indexed elements.
5759
- **x**: input [`Float32Array`][@stdlib/array/float32].
58-
- **strideX**: index increment for `x`.
60+
- **strideX**: stride length for `x`.
5961
- **mask**: mask [`Uint8Array`][@stdlib/array/uint8]. If a `mask` array element is `0`, the corresponding element in `x` is considered valid and **included** in computation. If a `mask` array element is `1`, the corresponding element in `x` is considered invalid/missing and **excluded** from computation.
60-
- **strideMask**: index increment for `mask`.
62+
- **strideMask**: stride length for `mask`.
6163

62-
The `N` and `stride` parameters determine which elements are accessed at runtime. For example, to compute the minimum value of every other element in `x`,
64+
The `N` and stride parameters determine which elements in the strided arrays are accessed at runtime. For example, to compute the minimum value of every other element in `x`,
6365

6466
```javascript
6567
var Float32Array = require( '@stdlib/array/float32' );
6668
var Uint8Array = require( '@stdlib/array/uint8' );
67-
var floor = require( '@stdlib/math/base/special/floor' );
6869

6970
var x = new Float32Array( [ 1.0, 2.0, 7.0, -2.0, -4.0, 3.0, -5.0, -6.0 ] );
7071
var mask = new Uint8Array( [ 0, 0, 0, 0, 0, 0, 1, 1 ] );
71-
var N = floor( x.length / 2 );
7272

73-
var v = snanmskmin( N, x, 2, mask, 2 );
73+
var v = snanmskmin( 4, x, 2, mask, 2 );
7474
// returns -4.0
7575
```
7676

@@ -81,17 +81,14 @@ Note that indexing is relative to the first index. To introduce offsets, use [`t
8181
```javascript
8282
var Float32Array = require( '@stdlib/array/float32' );
8383
var Uint8Array = require( '@stdlib/array/uint8' );
84-
var floor = require( '@stdlib/math/base/special/floor' );
8584

8685
var x0 = new Float32Array( [ 2.0, 1.0, -2.0, -2.0, 3.0, 4.0, -5.0, -6.0 ] );
8786
var x1 = new Float32Array( x0.buffer, x0.BYTES_PER_ELEMENT*1 ); // start at 2nd element
8887

8988
var mask0 = new Uint8Array( [ 0, 0, 0, 0, 0, 0, 1, 1 ] );
9089
var mask1 = new Uint8Array( mask0.buffer, mask0.BYTES_PER_ELEMENT*1 ); // start at 2nd element
9190

92-
var N = floor( x0.length / 2 );
93-
94-
var v = snanmskmin( N, x1, 2, mask1, 2 );
91+
var v = snanmskmin( 4, x1, 2, mask1, 2 );
9592
// returns -2.0
9693
```
9794

@@ -115,18 +112,16 @@ The function has the following additional parameters:
115112
- **offsetX**: starting index for `x`.
116113
- **offsetMask**: starting index for `mask`.
117114

118-
While [`typed array`][mdn-typed-array] views mandate a view offset based on the underlying `buffer`, the `offset` parameter supports indexing semantics based on a starting index. For example, to calculate the minimum value for every other value in `x` starting from the second value
115+
While [`typed array`][mdn-typed-array] views mandate a view offset based on the underlying buffer, offset parameters support indexing semantics based on starting indices. For example, to calculate the minimum value for every other element in `x` starting from the second element
119116

120117
```javascript
121118
var Float32Array = require( '@stdlib/array/float32' );
122119
var Uint8Array = require( '@stdlib/array/uint8' );
123-
var floor = require( '@stdlib/math/base/special/floor' );
124120

125121
var x = new Float32Array( [ 2.0, 1.0, -2.0, -2.0, 3.0, 4.0, -5.0, -6.0 ] );
126122
var mask = new Uint8Array( [ 0, 0, 0, 0, 0, 0, 1, 1 ] );
127-
var N = floor( x.length / 2 );
128123

129-
var v = snanmskmin.ndarray( N, x, 2, 1, mask, 2, 1 );
124+
var v = snanmskmin.ndarray( 4, x, 2, 1, mask, 2, 1 );
130125
// returns -2.0
131126
```
132127

@@ -151,31 +146,22 @@ var v = snanmskmin.ndarray( N, x, 2, 1, mask, 2, 1 );
151146
<!-- eslint no-undef: "error" -->
152147

153148
```javascript
154-
var randu = require( '@stdlib/random/base/randu' );
155-
var round = require( '@stdlib/math/base/special/round' );
156-
var Float32Array = require( '@stdlib/array/float32' );
157-
var Uint8Array = require( '@stdlib/array/uint8' );
149+
var uniform = require( '@stdlib/random/base/uniform' );
150+
var bernoulli = require( '@stdlib/random/base/bernoulli' );
151+
var filledarrayBy = require( '@stdlib/array/filled-by' );
158152
var snanmskmin = require( '@stdlib/stats/base/snanmskmin' );
159153

160-
var mask;
161-
var x;
162-
var i;
163-
164-
x = new Float32Array( 10 );
165-
mask = new Uint8Array( x.length );
166-
for ( i = 0; i < x.length; i++ ) {
167-
if ( randu() < 0.2 ) {
168-
mask[ i ] = 1;
169-
} else {
170-
mask[ i ] = 0;
171-
}
172-
if ( randu() < 0.1 ) {
173-
x[ i ] = NaN;
174-
} else {
175-
x[ i ] = round( (randu()*100.0) - 50.0 );
154+
function rand() {
155+
if ( bernoulli( 0.8 ) < 1 ) {
156+
return NaN;
176157
}
158+
return uniform( -50.0, 50.0 );
177159
}
160+
161+
var x = filledarrayBy( 10, 'float32', rand );
178162
console.log( x );
163+
164+
var mask = filledarrayBy( x.length, 'uint8', bernoulli.factory( 0.2 ) );
179165
console.log( mask );
180166

181167
var v = snanmskmin( x.length, x, 1, mask, 1 );
@@ -186,6 +172,145 @@ console.log( v );
186172

187173
<!-- /.examples -->
188174

175+
<!-- C interface documentation. -->
176+
177+
* * *
178+
179+
<section class="c">
180+
181+
## C APIs
182+
183+
<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->
184+
185+
<section class="intro">
186+
187+
</section>
188+
189+
<!-- /.intro -->
190+
191+
<!-- C usage documentation. -->
192+
193+
<section class="usage">
194+
195+
### Usage
196+
197+
```c
198+
#include "stdlib/stats/base/snanmskmin.h"
199+
```
200+
201+
#### stdlib_strided_snanmskmin( N, \*X, strideX, \*Mask, strideMask )
202+
203+
Computes the minimum value of a single-precision floating-point strided array according to a `mask`, ignoring `NaN` values.
204+
205+
```c
206+
#include <stdint.h>
207+
208+
const float x[] = { 1.0f, -2.0f, 4.0f, 2.0f, 0.0f/0.0f };
209+
const uint8_t mask[] = { 0, 0, 1, 0, 0 };
210+
211+
float v = stdlib_strided_snanmskmin( 5, x, 1, mask, 1 );
212+
// returns -2.0f
213+
```
214+
215+
The function accepts the following arguments:
216+
217+
- **N**: `[in] CBLAS_INT` number of indexed elements.
218+
- **X**: `[in] float*` input array.
219+
- **strideX**: `[in] CBLAS_INT` stride length for `X`.
220+
- **Mask**: `[in] uint8_t*` mask array. If a `Mask` array element is `0`, the corresponding element in `X` is considered valid and included in computation. If a `Mask` array element is `1`, the corresponding element in `X` is considered invalid/missing and excluded from computation.
221+
- **strideMask**: `[in] CBLAS_INT` stride length for `Mask`.
222+
223+
```c
224+
float stdlib_strided_snanmskmin( const CBLAS_INT N, const float *X, const CBLAS_INT strideX, const uint8_t *Mask, const CBLAS_INT strideMask );
225+
```
226+
227+
#### stdlib_strided_snanmskmin_ndarray( N, \*X, strideX, offsetX, \*Mask, strideMask, offsetMask )
228+
229+
Computes the minimum value of a single-precision floating-point strided array according to a `mask`, ignoring `NaN` values and using alternative indexing semantics.
230+
231+
```c
232+
#include <stdint.h>
233+
234+
const float x[] = { 1.0f, -2.0f, 4.0f, 2.0f, 0.0f/0.0f };
235+
const uint8_t mask[] = { 0, 0, 1, 0, 0 };
236+
237+
float v = stdlib_strided_snanmskmin_ndarray( 5, x, 1, 0, mask, 1, 0 );
238+
// returns -2.0f
239+
```
240+
241+
The function accepts the following arguments:
242+
243+
- **N**: `[in] CBLAS_INT` number of indexed elements.
244+
- **X**: `[in] float*` input array.
245+
- **strideX**: `[in] CBLAS_INT` stride length for `X`.
246+
- **offsetX**: `[in] CBLAS_INT` starting index for `X`.
247+
- **Mask**: `[in] uint8_t*` mask array. If a `Mask` array element is `0`, the corresponding element in `X` is considered valid and included in computation. If a `Mask` array element is `1`, the corresponding element in `X` is considered invalid/missing and excluded from computation.
248+
- **strideMask**: `[in] CBLAS_INT` stride length for `Mask`.
249+
- **offsetMask**: `[in] CBLAS_INT` starting index for `Mask`.
250+
251+
```c
252+
float stdlib_strided_snanmskmin_ndarray( const CBLAS_INT N, const float *X, const CBLAS_INT strideX, const CBLAS_INT offsetX, const uint8_t *Mask, const CBLAS_INT strideMask, const CBLAS_INT offsetMask );
253+
```
254+
255+
</section>
256+
257+
<!-- /.usage -->
258+
259+
<!-- C API usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
260+
261+
<section class="notes">
262+
263+
</section>
264+
265+
<!-- /.notes -->
266+
267+
<!-- C API usage examples. -->
268+
269+
<section class="examples">
270+
271+
### Examples
272+
273+
```c
274+
#include "stdlib/stats/base/snanmskmin.h"
275+
#include <stdint.h>
276+
#include <stdio.h>
277+
278+
int main( void ) {
279+
// Create a strided array:
280+
const float x[] = { 1.0f, 2.0f, 3.0f, 4.0f, 5.0f, 6.0f, 7.0f, 8.0f, 0.0f/0.0f, 0.0f/0.0f };
281+
282+
// Create a mask array:
283+
const uint8_t mask[] = { 0, 0, 0, 0, 0, 0, 0, 0, 1, 1 };
284+
285+
// Specify the number of elements:
286+
const int N = 5;
287+
288+
// Specify the stride lengths:
289+
const int strideX = 2;
290+
const int strideMask = 2;
291+
292+
// Compute the minimum value:
293+
float v = stdlib_strided_snanmskmin( N, x, strideX, mask, strideMask );
294+
295+
// Print the result:
296+
printf( "min: %f\n", v );
297+
}
298+
```
299+
300+
</section>
301+
302+
<!-- /.examples -->
303+
304+
</section>
305+
306+
<!-- /.c -->
307+
308+
<section class="references">
309+
310+
</section>
311+
312+
<!-- /.references -->
313+
189314
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
190315
191316
<section class="related">

lib/node_modules/@stdlib/stats/base/snanmskmin/benchmark/benchmark.js

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -21,17 +21,30 @@
2121
// MODULES //
2222

2323
var bench = require( '@stdlib/bench' );
24-
var randu = require( '@stdlib/random/base/randu' );
24+
var uniform = require( '@stdlib/random/base/uniform' );
25+
var bernoulli = require( '@stdlib/random/base/bernoulli' );
26+
var filledarrayBy = require( '@stdlib/array/filled-by' );
2527
var isnanf = require( '@stdlib/math/base/assert/is-nanf' );
2628
var pow = require( '@stdlib/math/base/special/pow' );
27-
var Float32Array = require( '@stdlib/array/float32' );
28-
var Uint8Array = require( '@stdlib/array/uint8' );
2929
var pkg = require( './../package.json' ).name;
3030
var snanmskmin = require( './../lib/snanmskmin.js' );
3131

3232

3333
// FUNCTIONS //
3434

35+
/**
36+
* Returns a random value or `NaN`.
37+
*
38+
* @private
39+
* @returns {number} random number or `NaN`
40+
*/
41+
function rand() {
42+
if ( bernoulli( 0.8 ) < 1 ) {
43+
return NaN;
44+
}
45+
return uniform( -10.0, 10.0 );
46+
}
47+
3548
/**
3649
* Creates a benchmark function.
3750
*
@@ -42,18 +55,9 @@ var snanmskmin = require( './../lib/snanmskmin.js' );
4255
function createBenchmark( len ) {
4356
var mask;
4457
var x;
45-
var i;
4658

47-
x = new Float32Array( len );
48-
mask = new Uint8Array( len );
49-
for ( i = 0; i < x.length; i++ ) {
50-
if ( randu() < 0.2 ) {
51-
mask[ i ] = 1;
52-
} else {
53-
mask[ i ] = 0;
54-
}
55-
x[ i ] = ( randu()*20.0 ) - 10.0;
56-
}
59+
x = filledarrayBy( len, 'float32', rand );
60+
mask = filledarrayBy( len, 'uint8', bernoulli.factory( 0.2 ) );
5761
return benchmark;
5862

5963
function benchmark( b ) {

lib/node_modules/@stdlib/stats/base/snanmskmin/benchmark/benchmark.native.js

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,11 @@
2222

2323
var resolve = require( 'path' ).resolve;
2424
var bench = require( '@stdlib/bench' );
25-
var randu = require( '@stdlib/random/base/randu' );
25+
var uniform = require( '@stdlib/random/base/uniform' );
26+
var bernoulli = require( '@stdlib/random/base/bernoulli' );
27+
var filledarrayBy = require( '@stdlib/array/filled-by' );
2628
var isnanf = require( '@stdlib/math/base/assert/is-nanf' );
2729
var pow = require( '@stdlib/math/base/special/pow' );
28-
var Float32Array = require( '@stdlib/array/float32' );
29-
var Uint8Array = require( '@stdlib/array/uint8' );
3030
var tryRequire = require( '@stdlib/utils/try-require' );
3131
var pkg = require( './../package.json' ).name;
3232

@@ -41,6 +41,19 @@ var opts = {
4141

4242
// FUNCTIONS //
4343

44+
/**
45+
* Returns a random value or `NaN`.
46+
*
47+
* @private
48+
* @returns {number} random number or `NaN`
49+
*/
50+
function rand() {
51+
if ( bernoulli( 0.8 ) < 1 ) {
52+
return NaN;
53+
}
54+
return uniform( -10.0, 10.0 );
55+
}
56+
4457
/**
4558
* Creates a benchmark function.
4659
*
@@ -51,18 +64,9 @@ var opts = {
5164
function createBenchmark( len ) {
5265
var mask;
5366
var x;
54-
var i;
5567

56-
x = new Float32Array( len );
57-
mask = new Uint8Array( len );
58-
for ( i = 0; i < x.length; i++ ) {
59-
if ( randu() < 0.2 ) {
60-
mask[ i ] = 1;
61-
} else {
62-
mask[ i ] = 0;
63-
}
64-
x[ i ] = ( randu()*20.0 ) - 10.0;
65-
}
68+
x = filledarrayBy( len, 'float32', rand );
69+
mask = filledarrayBy( len, 'uint8', bernoulli.factory( 0.2 ) );
6670
return benchmark;
6771

6872
function benchmark( b ) {

0 commit comments

Comments
 (0)