Skip to content

Commit eb05b7c

Browse files
aayush0325kgrytestdlib-bot
authored
feat: add C ndarray interface and refactor implementation for stats/base/dmskrange
PR-URL: #4376 Co-authored-by: Athan Reines <[email protected]> Reviewed-by: Athan Reines <[email protected]> Co-authored-by: stdlib-bot <[email protected]>
1 parent d5b451f commit eb05b7c

23 files changed

+452
-427
lines changed

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

Lines changed: 136 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ var dmskrange = require( '@stdlib/stats/base/dmskrange' );
4040

4141
#### dmskrange( N, x, strideX, mask, strideMask )
4242

43-
Computes the [range][range] of a double-precision floating-point strided array `x` according to a `mask`.
43+
Computes the [range][range] of a double-precision floating-point strided array according to a mask.
4444

4545
```javascript
4646
var Float64Array = require( '@stdlib/array/float64' );
@@ -61,18 +61,16 @@ The function has the following parameters:
6161
- **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.
6262
- **strideMask**: index increment for `mask`.
6363

64-
The `N` and `stride` parameters determine which elements are accessed at runtime. For example, to compute the [range][range] 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 [range][range] of every other element in `x`,
6565

6666
```javascript
6767
var Float64Array = require( '@stdlib/array/float64' );
6868
var Uint8Array = require( '@stdlib/array/uint8' );
69-
var floor = require( '@stdlib/math/base/special/floor' );
7069

7170
var x = new Float64Array( [ 1.0, 2.0, -7.0, -2.0, 4.0, 3.0, 5.0, 6.0 ] );
7271
var mask = new Uint8Array( [ 0, 0, 0, 0, 0, 0, 1, 1 ] );
73-
var N = floor( x.length / 2 );
7472

75-
var v = dmskrange( N, x, 2, mask, 2 );
73+
var v = dmskrange( 4, x, 2, mask, 2 );
7674
// returns 11.0
7775
```
7876

@@ -83,17 +81,13 @@ Note that indexing is relative to the first index. To introduce offsets, use [`t
8381
```javascript
8482
var Float64Array = require( '@stdlib/array/float64' );
8583
var Uint8Array = require( '@stdlib/array/uint8' );
86-
var floor = require( '@stdlib/math/base/special/floor' );
8784

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

9188
var mask0 = new Uint8Array( [ 0, 0, 0, 0, 0, 0, 1, 1 ] );
9289
var mask1 = new Uint8Array( mask0.buffer, mask0.BYTES_PER_ELEMENT*1 ); // start at 2nd element
93-
94-
var N = floor( x0.length / 2 );
95-
96-
var v = dmskrange( N, x1, 2, mask1, 2 );
90+
var v = dmskrange( 4, x1, 2, mask1, 2 );
9791
// returns 6.0
9892
```
9993

@@ -117,18 +111,16 @@ The function has the following additional parameters:
117111
- **offsetX**: starting index for `x`.
118112
- **offsetMask**: starting index for `mask`.
119113

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

122116
```javascript
123117
var Float64Array = require( '@stdlib/array/float64' );
124118
var Uint8Array = require( '@stdlib/array/uint8' );
125-
var floor = require( '@stdlib/math/base/special/floor' );
126119

127120
var x = new Float64Array( [ 2.0, 1.0, -2.0, -2.0, 3.0, 4.0, 5.0, 6.0 ] );
128121
var mask = new Uint8Array( [ 0, 0, 0, 0, 0, 0, 1, 1 ] );
129-
var N = floor( x.length / 2 );
130122

131-
var v = dmskrange.ndarray( N, x, 2, 1, mask, 2, 1 );
123+
var v = dmskrange.ndarray( 4, x, 2, 1, mask, 2, 1 );
132124
// returns 6.0
133125
```
134126

@@ -153,26 +145,19 @@ var v = dmskrange.ndarray( N, x, 2, 1, mask, 2, 1 );
153145
<!-- eslint no-undef: "error" -->
154146

155147
```javascript
156-
var randu = require( '@stdlib/random/base/randu' );
157-
var round = require( '@stdlib/math/base/special/round' );
158-
var Float64Array = require( '@stdlib/array/float64' );
159-
var Uint8Array = require( '@stdlib/array/uint8' );
148+
var uniform = require( '@stdlib/random/array/uniform' );
149+
var bernoulli = require( '@stdlib/random/array/bernoulli' );
160150
var dmskrange = require( '@stdlib/stats/base/dmskrange' );
161151

162-
var mask;
163-
var x;
164-
var i;
165-
166-
x = new Float64Array( 10 );
167-
mask = new Uint8Array( x.length );
168-
for ( i = 0; i < x.length; i++ ) {
169-
if ( randu() < 0.2 ) {
170-
mask[ i ] = 1;
171-
} else {
172-
mask[ i ] = 0;
173-
}
174-
x[ i ] = round( (randu()*100.0) - 50.0 );
175-
}
152+
var uniformOptions = {
153+
'dtype': 'float64'
154+
};
155+
var bernoulliOptions = {
156+
'dtype': 'uint8'
157+
};
158+
159+
var x = uniform( 10, -50.0, 50.0, uniformOptions );
160+
var mask = bernoulli( x.length, 0.2, bernoulliOptions );
176161
console.log( x );
177162
console.log( mask );
178163

@@ -184,6 +169,125 @@ console.log( v );
184169

185170
<!-- /.examples -->
186171

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

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

Lines changed: 14 additions & 17 deletions
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 isnan = require( '@stdlib/math/base/assert/is-nan' );
2627
var pow = require( '@stdlib/math/base/special/pow' );
27-
var Float64Array = require( '@stdlib/array/float64' );
28-
var Uint8Array = require( '@stdlib/array/uint8' );
2928
var pkg = require( './../package.json' ).name;
3029
var dmskrange = require( './../lib/dmskrange.js' );
3130

3231

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

3544
/**
@@ -40,20 +49,8 @@ var dmskrange = require( './../lib/dmskrange.js' );
4049
* @returns {Function} benchmark function
4150
*/
4251
function createBenchmark( len ) {
43-
var mask;
44-
var x;
45-
var i;
46-
47-
x = new Float64Array( 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 ) {

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

Lines changed: 10 additions & 17 deletions
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 isnan = require( '@stdlib/math/base/assert/is-nan' );
2728
var pow = require( '@stdlib/math/base/special/pow' );
28-
var Float64Array = require( '@stdlib/array/float64' );
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 dmskrange = tryRequire( resolve( __dirname, './../lib/dmskrange.native.js' )
3736
var opts = {
3837
'skip': ( dmskrange instanceof Error )
3938
};
39+
var uniformOptions = {
40+
'dtype': 'float64'
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 Float64Array( 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 ) {

lib/node_modules/@stdlib/stats/base/dmskrange/benchmark/benchmark.ndarray.js

Lines changed: 14 additions & 17 deletions
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 isnan = require( '@stdlib/math/base/assert/is-nan' );
2627
var pow = require( '@stdlib/math/base/special/pow' );
27-
var Float64Array = require( '@stdlib/array/float64' );
28-
var Uint8Array = require( '@stdlib/array/uint8' );
2928
var pkg = require( './../package.json' ).name;
3029
var dmskrange = require( './../lib/ndarray.js' );
3130

3231

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

3544
/**
@@ -40,20 +49,8 @@ var dmskrange = 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 Float64Array( 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)