Skip to content

Commit 836075d

Browse files
authored
feat: add C ndarray interface and refactor implementation for stats/base/drange
PR-URL: #4239 Reviewed-by: Philipp Burckhardt <[email protected]> Signed-off-by: Aayush Khanna <[email protected]>
1 parent 3d7cff6 commit 836075d

23 files changed

+362
-253
lines changed

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

Lines changed: 132 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -38,36 +38,34 @@ The [**range**][range] is defined as the difference between the maximum and mini
3838
var drange = require( '@stdlib/stats/base/drange' );
3939
```
4040

41-
#### drange( N, x, stride )
41+
#### drange( N, x, strideX )
4242

4343
Computes the [range][range] of a double-precision floating-point strided array `x`.
4444

4545
```javascript
4646
var Float64Array = require( '@stdlib/array/float64' );
4747

4848
var x = new Float64Array( [ 1.0, -2.0, 2.0 ] );
49-
var N = x.length;
5049

51-
var v = drange( N, x, 1 );
50+
var v = drange( x.length, x, 1 );
5251
// returns 4.0
5352
```
5453

5554
The function has the following parameters:
5655

5756
- **N**: number of indexed elements.
5857
- **x**: input [`Float64Array`][@stdlib/array/float64].
59-
- **stride**: index increment for `x`.
58+
- **strideX**: index increment for `x`.
6059

61-
The `N` and `stride` parameters determine which elements in `x` are accessed at runtime. For example, to compute the [range][range] of every other element in `x`,
60+
The `N` and stride parameters determine which elements in the strided array are accessed at runtime. For example, to compute the [range][range] of every other element in `x`,
6261

6362
```javascript
6463
var Float64Array = require( '@stdlib/array/float64' );
6564
var floor = require( '@stdlib/math/base/special/floor' );
6665

6766
var x = new Float64Array( [ 1.0, 2.0, 2.0, -7.0, -2.0, 3.0, 4.0, 2.0 ] );
68-
var N = floor( x.length / 2 );
6967

70-
var v = drange( N, x, 2 );
68+
var v = drange( 4, x, 2 );
7169
// returns 6.0
7270
```
7371

@@ -77,45 +75,39 @@ Note that indexing is relative to the first index. To introduce an offset, use [
7775

7876
```javascript
7977
var Float64Array = require( '@stdlib/array/float64' );
80-
var floor = require( '@stdlib/math/base/special/floor' );
8178

8279
var x0 = new Float64Array( [ 2.0, 1.0, 2.0, -2.0, -2.0, 2.0, 3.0, 4.0 ] );
8380
var x1 = new Float64Array( x0.buffer, x0.BYTES_PER_ELEMENT*1 ); // start at 2nd element
8481

85-
var N = floor( x0.length / 2 );
86-
87-
var v = drange( N, x1, 2 );
82+
var v = drange( 4, x1, 2 );
8883
// returns 6.0
8984
```
9085

91-
#### drange.ndarray( N, x, stride, offset )
86+
#### drange.ndarray( N, x, strideX, offsetX )
9287

9388
Computes the [range][range] of a double-precision floating-point strided array using alternative indexing semantics.
9489

9590
```javascript
9691
var Float64Array = require( '@stdlib/array/float64' );
9792

9893
var x = new Float64Array( [ 1.0, -2.0, 2.0 ] );
99-
var N = x.length;
10094

101-
var v = drange.ndarray( N, x, 1, 0 );
95+
var v = drange.ndarray( x.length, x, 1, 0 );
10296
// returns 4.0
10397
```
10498

10599
The function has the following additional parameters:
106100

107-
- **offset**: starting index for `x`.
101+
- **offsetX**: starting index for `x`.
108102

109-
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
103+
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 element in `x` starting from the second element
110104

111105
```javascript
112106
var Float64Array = require( '@stdlib/array/float64' );
113-
var floor = require( '@stdlib/math/base/special/floor' );
114107

115108
var x = new Float64Array( [ 2.0, 1.0, 2.0, -2.0, -2.0, 2.0, 3.0, 4.0 ] );
116-
var N = floor( x.length / 2 );
117109

118-
var v = drange.ndarray( N, x, 2, 1 );
110+
var v = drange.ndarray( 4, x, 2, 1 );
119111
// returns 6.0
120112
```
121113

@@ -140,18 +132,12 @@ var v = drange.ndarray( N, x, 2, 1 );
140132
<!-- eslint no-undef: "error" -->
141133

142134
```javascript
143-
var randu = require( '@stdlib/random/base/randu' );
144-
var round = require( '@stdlib/math/base/special/round' );
145-
var Float64Array = require( '@stdlib/array/float64' );
135+
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
146136
var drange = require( '@stdlib/stats/base/drange' );
147137

148-
var x;
149-
var i;
150-
151-
x = new Float64Array( 10 );
152-
for ( i = 0; i < x.length; i++ ) {
153-
x[ i ] = round( (randu()*100.0) - 50.0 );
154-
}
138+
var x = discreteUniform( 10, -50, 50, {
139+
'dtype': 'float64'
140+
});
155141
console.log( x );
156142

157143
var v = drange( x.length, x, 1 );
@@ -162,6 +148,123 @@ console.log( v );
162148

163149
<!-- /.examples -->
164150

151+
<!-- C interface documentation. -->
152+
153+
* * *
154+
155+
<section class="c">
156+
157+
## C APIs
158+
159+
<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->
160+
161+
<section class="intro">
162+
163+
</section>
164+
165+
<!-- /.intro -->
166+
167+
<!-- C usage documentation. -->
168+
169+
<section class="usage">
170+
171+
### Usage
172+
173+
```c
174+
#include "stdlib/stats/base/drange.h"
175+
```
176+
177+
#### stdlib_strided_drange( N, \*X, strideX )
178+
179+
Computes the [range][range] of a double-precision floating-point strided array `x`.
180+
181+
```c
182+
const double x[] = { 1.0, -2.0, 3.0, -4.0 };
183+
184+
double v = stdlib_strided_drange( 4, x, 1 );
185+
// returns 4.0
186+
```
187+
188+
The function accepts the following arguments:
189+
190+
- **N**: `[in] CBLAS_INT` number of indexed elements.
191+
- **X**: `[in] double*` input array.
192+
- **strideX**: `[in] CBLAS_INT` stride length for `X`.
193+
194+
```c
195+
double stdlib_strided_drange( const CBLAS_INT N, const double *X, const CBLAS_INT strideX );
196+
```
197+
198+
#### stdlib_strided_drange_ndarray( N, \*X, strideX, offsetX )
199+
200+
Computes the [range][range] of a double-precision floating-point strided array using alternative indexing semantics.
201+
202+
```c
203+
const double x[] = { 1.0, -2.0, 3.0, -4.0 };
204+
205+
double v = stdlib_strided_drange_ndarray( 4, x, 1, 0 );
206+
// returns 7.0
207+
```
208+
209+
The function accepts the following arguments:
210+
211+
- **N**: `[in] CBLAS_INT` number of indexed elements.
212+
- **X**: `[in] double*` input array.
213+
- **strideX**: `[in] CBLAS_INT` stride length for `X`.
214+
- **offsetX**: `[in] CBLAS_INT` starting index for `X`.
215+
216+
```c
217+
double stdlib_strided_drange_ndarray( const CBLAS_INT N, const double *X, const CBLAS_INT strideX, const CBLAS_INT offsetX );
218+
```
219+
220+
</section>
221+
222+
<!-- /.usage -->
223+
224+
<!-- C API usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
225+
226+
<section class="notes">
227+
228+
</section>
229+
230+
<!-- /.notes -->
231+
232+
<!-- C API usage examples. -->
233+
234+
<section class="examples">
235+
236+
### Examples
237+
238+
```c
239+
#include "stdlib/stats/base/drange.h"
240+
#include <stdio.h>
241+
242+
int main( void ) {
243+
// Create a strided array:
244+
const double x[] = { 1.0, -2.0, -3.0, 4.0, -5.0, -6.0, 7.0, 8.0 };
245+
246+
// Specify the number of elements:
247+
const int N = 4;
248+
249+
// Specify the stride length:
250+
const int strideX = 2;
251+
252+
// Compute the range:
253+
double v = stdlib_strided_drange( N, x, strideX );
254+
255+
// Print the result:
256+
printf( "range: %lf\n", v );
257+
}
258+
```
259+
260+
</section>
261+
262+
<!-- /.examples -->
263+
264+
</section>
265+
266+
<!-- /.c -->
267+
165268
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
166269
167270
<section class="related">

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

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,20 @@
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' );
2525
var isnan = require( '@stdlib/math/base/assert/is-nan' );
2626
var pow = require( '@stdlib/math/base/special/pow' );
27-
var Float64Array = require( '@stdlib/array/float64' );
2827
var pkg = require( './../package.json' ).name;
2928
var drange = require( './../lib/drange.js' );
3029

3130

31+
// VARIABLES //
32+
33+
var options = {
34+
'dtype': 'float64'
35+
};
36+
37+
3238
// FUNCTIONS //
3339

3440
/**
@@ -39,13 +45,7 @@ var drange = require( './../lib/drange.js' );
3945
* @returns {Function} benchmark function
4046
*/
4147
function createBenchmark( len ) {
42-
var x;
43-
var i;
44-
45-
x = new Float64Array( len );
46-
for ( i = 0; i < x.length; i++ ) {
47-
x[ i ] = ( randu()*20.0 ) - 10.0;
48-
}
48+
var x = uniform( len, -10.0, 10.0, options );
4949
return benchmark;
5050

5151
function benchmark( b ) {

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

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,9 @@
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' );
2626
var isnan = require( '@stdlib/math/base/assert/is-nan' );
2727
var pow = require( '@stdlib/math/base/special/pow' );
28-
var Float64Array = require( '@stdlib/array/float64' );
2928
var tryRequire = require( '@stdlib/utils/try-require' );
3029
var pkg = require( './../package.json' ).name;
3130

@@ -36,6 +35,9 @@ var drange = tryRequire( resolve( __dirname, './../lib/drange.native.js' ) );
3635
var opts = {
3736
'skip': ( drange instanceof Error )
3837
};
38+
var options = {
39+
'dtype': 'float64'
40+
};
3941

4042

4143
// FUNCTIONS //
@@ -48,13 +50,7 @@ var opts = {
4850
* @returns {Function} benchmark function
4951
*/
5052
function createBenchmark( len ) {
51-
var x;
52-
var i;
53-
54-
x = new Float64Array( len );
55-
for ( i = 0; i < x.length; i++ ) {
56-
x[ i ] = ( randu()*20.0 ) - 10.0;
57-
}
53+
var x = uniform( len, -10.0, 10.0, options );
5854
return benchmark;
5955

6056
function benchmark( b ) {

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

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,20 @@
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' );
2525
var isnan = require( '@stdlib/math/base/assert/is-nan' );
2626
var pow = require( '@stdlib/math/base/special/pow' );
27-
var Float64Array = require( '@stdlib/array/float64' );
2827
var pkg = require( './../package.json' ).name;
2928
var drange = require( './../lib/ndarray.js' );
3029

3130

31+
// VARIABLES //
32+
33+
var options = {
34+
'dtype': 'float64'
35+
};
36+
37+
3238
// FUNCTIONS //
3339

3440
/**
@@ -39,13 +45,7 @@ var drange = require( './../lib/ndarray.js' );
3945
* @returns {Function} benchmark function
4046
*/
4147
function createBenchmark( len ) {
42-
var x;
43-
var i;
44-
45-
x = new Float64Array( len );
46-
for ( i = 0; i < x.length; i++ ) {
47-
x[ i ] = ( randu()*20.0 ) - 10.0;
48-
}
48+
var x = uniform( len, -10.0, 10.0, options );
4949
return benchmark;
5050

5151
function benchmark( b ) {

lib/node_modules/@stdlib/stats/base/drange/benchmark/benchmark.ndarray.native.js

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,9 @@
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' );
2626
var isnan = require( '@stdlib/math/base/assert/is-nan' );
2727
var pow = require( '@stdlib/math/base/special/pow' );
28-
var Float64Array = require( '@stdlib/array/float64' );
2928
var tryRequire = require( '@stdlib/utils/try-require' );
3029
var pkg = require( './../package.json' ).name;
3130

@@ -36,6 +35,9 @@ var drange = tryRequire( resolve( __dirname, './../lib/ndarray.native.js' ) );
3635
var opts = {
3736
'skip': ( drange instanceof Error )
3837
};
38+
var options = {
39+
'dtype': 'float64'
40+
};
3941

4042

4143
// FUNCTIONS //
@@ -48,13 +50,7 @@ var opts = {
4850
* @returns {Function} benchmark function
4951
*/
5052
function createBenchmark( len ) {
51-
var x;
52-
var i;
53-
54-
x = new Float64Array( len );
55-
for ( i = 0; i < x.length; i++ ) {
56-
x[ i ] = ( randu()*20.0 ) - 10.0;
57-
}
53+
var x = uniform( len, -10.0, 10.0, options );
5854
return benchmark;
5955

6056
function benchmark( b ) {

0 commit comments

Comments
 (0)