Skip to content

Commit b3d3e84

Browse files
aayush0325kgrytestdlib-bot
authored andcommitted
feat: add C ndarray interface and refactor implementation for stats/base/smskmax
PR-URL: stdlib-js#4401 Co-authored-by: Athan Reines <[email protected]> Reviewed-by: Athan Reines <[email protected]> Co-authored-by: stdlib-bot <[email protected]>
1 parent 29af008 commit b3d3e84

23 files changed

+458
-426
lines changed

Diff for: lib/node_modules/@stdlib/stats/base/smskmax/README.md

+139-34
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ var smskmax = require( '@stdlib/stats/base/smskmax' );
3838

3939
#### smskmax( N, x, strideX, mask, strideMask )
4040

41-
Computes the maximum value of a single-precision floating-point strided array `x` according to a `mask`.
41+
Computes the maximum value of a single-precision floating-point strided array according to a mask.
4242

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

5656
- **N**: number of indexed elements.
5757
- **x**: input [`Float32Array`][@stdlib/array/float32].
58-
- **strideX**: index increment for `x`.
58+
- **strideX**: stride length for `x`.
5959
- **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`.
60+
- **strideMask**: stride length for `mask`.
6161

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

6464
```javascript
6565
var Float32Array = require( '@stdlib/array/float32' );
6666
var Uint8Array = require( '@stdlib/array/uint8' );
67-
var floor = require( '@stdlib/math/base/special/floor' );
6867

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

73-
var v = smskmax( N, x, 2, mask, 2 );
71+
var v = smskmax( 4, x, 2, mask, 2 );
7472
// returns 4.0
7573
```
7674

@@ -81,23 +79,20 @@ Note that indexing is relative to the first index. To introduce offsets, use [`t
8179
```javascript
8280
var Float32Array = require( '@stdlib/array/float32' );
8381
var Uint8Array = require( '@stdlib/array/uint8' );
84-
var floor = require( '@stdlib/math/base/special/floor' );
8582

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

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

92-
var N = floor( x0.length / 2 );
93-
94-
var v = smskmax( N, x1, 2, mask1, 2 );
89+
var v = smskmax( 4, x1, 2, mask1, 2 );
9590
// returns 4.0
9691
```
9792

9893
#### smskmax.ndarray( N, x, strideX, offsetX, mask, strideMask, offsetMask )
9994

100-
Computes the maximum value of a single-precision floating-point strided array according to a `mask` and using alternative indexing semantics.
95+
Computes the maximum value of a single-precision floating-point strided array according to a mask and using alternative indexing semantics.
10196

10297
```javascript
10398
var Float32Array = require( '@stdlib/array/float32' );
@@ -115,18 +110,16 @@ The function has the following additional parameters:
115110
- **offsetX**: starting index for `x`.
116111
- **offsetMask**: starting index for `mask`.
117112

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 maximum value for every other value in `x` starting from the second value
113+
While [`typed array`][mdn-typed-array] views mandate a view offset based on the underlying buffer, the offset parameters support indexing semantics based on a starting indices. For example, to calculate the maximum value for every other element in `x` starting from the second element
119114

120115
```javascript
121116
var Float32Array = require( '@stdlib/array/float32' );
122117
var Uint8Array = require( '@stdlib/array/uint8' );
123-
var floor = require( '@stdlib/math/base/special/floor' );
124118

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

129-
var v = smskmax.ndarray( N, x, 2, 1, mask, 2, 1 );
122+
var v = smskmax.ndarray( 4, x, 2, 1, mask, 2, 1 );
130123
// returns 4.0
131124
```
132125

@@ -151,26 +144,19 @@ var v = smskmax.ndarray( N, x, 2, 1, mask, 2, 1 );
151144
<!-- eslint no-undef: "error" -->
152145

153146
```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' );
147+
var uniform = require( '@stdlib/random/array/uniform' );
148+
var bernoulli = require( '@stdlib/random/array/bernoulli' );
158149
var smskmax = require( '@stdlib/stats/base/smskmax' );
159150

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-
x[ i ] = round( (randu()*100.0) - 50.0 );
173-
}
151+
var uniformOptions = {
152+
'dtype': 'float32'
153+
};
154+
var bernoulliOptions = {
155+
'dtype': 'uint8'
156+
};
157+
158+
var x = uniform( 10, -50.0, 50.0, uniformOptions );
159+
var mask = bernoulli( x.length, 0.2, bernoulliOptions );
174160
console.log( x );
175161
console.log( mask );
176162

@@ -182,6 +168,125 @@ console.log( v );
182168

183169
<!-- /.examples -->
184170

171+
<!-- C usage documentation. -->
172+
173+
<section class="usage">
174+
175+
### Usage
176+
177+
```c
178+
#include "stdlib/stats/base/smskmax.h"
179+
```
180+
181+
#### stdlib_strided_smskmax( N, \*X, strideX, \*Mask, strideMask )
182+
183+
Computes the maximum value of a single-precision floating-point strided array according to a mask.
184+
185+
```c
186+
#include <stdint.h>
187+
188+
const float x[] = { 1.0f, -2.0f, 2.0f };
189+
const uint8_t mask[] = { 0, 1, 0 };
190+
191+
float v = stdlib_strided_smskmax( 3, x, 1, mask, 1 );
192+
// returns 2.0f
193+
```
194+
195+
The function accepts the following arguments:
196+
197+
- **N**: `[in] CBLAS_INT` number of indexed elements.
198+
- **X**: `[in] float*` input array.
199+
- **strideX**: `[in] CBLAS_INT` stride length for `X`.
200+
- **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.
201+
- **strideMask**: `[in] CBLAS_INT` stride length for `Mask`.
202+
203+
```c
204+
float stdlib_strided_smskmax( const CBLAS_INT N, const float *X, const CBLAS_INT strideX, const uint8_t *Mask, const CBLAS_INT strideMask );
205+
```
206+
207+
<!-- lint disable maximum-heading-length -->
208+
209+
#### stdlib_strided_smskmax_ndarray( N, \*X, strideX, offsetX, \*Mask, strideMask, offsetMask )
210+
211+
Computes the maximum value of a single-precision floating-point strided array according to a mask and using alternative indexing semantics.
212+
213+
```c
214+
#include <stdint.h>
215+
216+
const float x[] = { 1.0f, -2.0f, 2.0f };
217+
const uint8_t mask[] = { 0, 1, 0 };
218+
219+
float v = stdlib_strided_smskmax( 3, x, 1, 0, mask, 1, 0 );
220+
// returns 2.0f
221+
```
222+
223+
The function accepts the following arguments:
224+
225+
- **N**: `[in] CBLAS_INT` number of indexed elements.
226+
- **X**: `[in] float*` input array.
227+
- **strideX**: `[in] CBLAS_INT` stride length for `X`.
228+
- **offsetX**: `[in] CBLAS_INT` starting index for `X`.
229+
- **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.
230+
- **strideMask**: `[in] CBLAS_INT` stride length for `Mask`.
231+
- **offsetMask**: `[in] CBLAS_INT` starting index for `Mask`.
232+
233+
```c
234+
float stdlib_strided_smskmax_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 );
235+
```
236+
237+
</section>
238+
239+
<!-- /.usage -->
240+
241+
<!-- C API usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
242+
243+
<section class="notes">
244+
245+
</section>
246+
247+
<!-- /.notes -->
248+
249+
<!-- C API usage examples. -->
250+
251+
<section class="examples">
252+
253+
### Examples
254+
255+
```c
256+
#include "stdlib/stats/base/smskmax.h"
257+
#include <stdint.h>
258+
#include <stdio.h>
259+
260+
int main( void ) {
261+
// Create a strided array:
262+
const float x[] = { 1.0f, 2.0f, 3.0f, 4.0f, 5.0f, 6.0f, 7.0f, 8.0f, 9.0f, 10.0f };
263+
264+
// Create a mask array:
265+
const uint8_t mask[] = { 0, 0, 0, 0, 0, 0, 0, 0, 1, 1 };
266+
267+
// Specify the number of elements:
268+
const int N = 5;
269+
270+
// Specify the stride lengths:
271+
const int strideX = 2;
272+
const int strideMask = 2;
273+
274+
// Compute the maximum value:
275+
float v = stdlib_strided_smskmax( N, x, strideX, mask, strideMask );
276+
277+
// Print the result:
278+
printf( "max: %f\n", v );
279+
}
280+
```
281+
282+
</section>
283+
284+
<!-- /.examples -->
285+
286+
</section>
287+
288+
<!-- /.c -->
289+
185290
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
186291
187292
<section class="related">

Diff for: lib/node_modules/@stdlib/stats/base/smskmax/benchmark/benchmark.js

+14-17
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,24 @@
2121
// MODULES //
2222

2323
var bench = require( '@stdlib/bench' );
24-
var randu = require( '@stdlib/random/base/randu' );
24+
var uniform = require( '@stdlib/random/array/uniform' );
25+
var bernoulli = require( '@stdlib/random/array/bernoulli' );
2526
var isnanf = require( '@stdlib/math/base/assert/is-nanf' );
2627
var pow = require( '@stdlib/math/base/special/pow' );
27-
var Float32Array = require( '@stdlib/array/float32' );
28-
var Uint8Array = require( '@stdlib/array/uint8' );
2928
var pkg = require( './../package.json' ).name;
3029
var smskmax = require( './../lib/smskmax.js' );
3130

3231

32+
// VARIABLES //
33+
34+
var uniformOptions = {
35+
'dtype': 'float32'
36+
};
37+
var bernoulliOptions = {
38+
'dtype': 'uint8'
39+
};
40+
41+
3342
// FUNCTIONS //
3443

3544
/**
@@ -40,20 +49,8 @@ var smskmax = require( './../lib/smskmax.js' );
4049
* @returns {Function} benchmark function
4150
*/
4251
function createBenchmark( len ) {
43-
var mask;
44-
var x;
45-
var i;
46-
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-
}
52+
var mask = bernoulli( len, 0.2, bernoulliOptions );
53+
var x = uniform( len, -10.0, 10.0, uniformOptions );
5754
return benchmark;
5855

5956
function benchmark( b ) {

Diff for: lib/node_modules/@stdlib/stats/base/smskmax/benchmark/benchmark.native.js

+10-17
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,10 @@
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/array/uniform' );
26+
var bernoulli = require( '@stdlib/random/array/bernoulli' );
2627
var isnanf = require( '@stdlib/math/base/assert/is-nanf' );
2728
var pow = require( '@stdlib/math/base/special/pow' );
28-
var Float32Array = require( '@stdlib/array/float32' );
29-
var Uint8Array = require( '@stdlib/array/uint8' );
3029
var tryRequire = require( '@stdlib/utils/try-require' );
3130
var pkg = require( './../package.json' ).name;
3231

@@ -37,6 +36,12 @@ var smskmax = tryRequire( resolve( __dirname, './../lib/smskmax.native.js' ) );
3736
var opts = {
3837
'skip': ( smskmax instanceof Error )
3938
};
39+
var uniformOptions = {
40+
'dtype': 'float32'
41+
};
42+
var bernoulliOptions = {
43+
'dtype': 'uint8'
44+
};
4045

4146

4247
// FUNCTIONS //
@@ -49,20 +54,8 @@ var opts = {
4954
* @returns {Function} benchmark function
5055
*/
5156
function createBenchmark( len ) {
52-
var mask;
53-
var x;
54-
var i;
55-
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-
}
57+
var mask = bernoulli( len, 0.2, bernoulliOptions );
58+
var x = uniform( len, -10.0, 10.0, uniformOptions );
6659
return benchmark;
6760

6861
function benchmark( b ) {

Diff for: lib/node_modules/@stdlib/stats/base/smskmax/benchmark/benchmark.ndarray.js

+14-17
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,24 @@
2121
// MODULES //
2222

2323
var bench = require( '@stdlib/bench' );
24-
var randu = require( '@stdlib/random/base/randu' );
24+
var uniform = require( '@stdlib/random/array/uniform' );
25+
var bernoulli = require( '@stdlib/random/array/bernoulli' );
2526
var isnanf = require( '@stdlib/math/base/assert/is-nanf' );
2627
var pow = require( '@stdlib/math/base/special/pow' );
27-
var Float32Array = require( '@stdlib/array/float32' );
28-
var Uint8Array = require( '@stdlib/array/uint8' );
2928
var pkg = require( './../package.json' ).name;
3029
var smskmax = require( './../lib/ndarray.js' );
3130

3231

32+
// VARIABLES //
33+
34+
var uniformOptions = {
35+
'dtype': 'float32'
36+
};
37+
var bernoulliOptions = {
38+
'dtype': 'uint8'
39+
};
40+
41+
3342
// FUNCTIONS //
3443

3544
/**
@@ -40,20 +49,8 @@ var smskmax = require( './../lib/ndarray.js' );
4049
* @returns {Function} benchmark function
4150
*/
4251
function createBenchmark( len ) {
43-
var mask;
44-
var x;
45-
var i;
46-
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-
}
52+
var mask = bernoulli( len, 0.2, bernoulliOptions );
53+
var x = uniform( len, -10.0, 10.0, uniformOptions );
5754
return benchmark;
5855

5956
function benchmark( b ) {

0 commit comments

Comments
 (0)