Skip to content

Commit 297cd66

Browse files
aayush0325kgryte
andauthored
feat: add C ndarray interface and refactor implementation for stats/base/srange
PR-URL: #4297 Co-authored-by: Athan Reines <kgryte@gmail.com> Reviewed-by: Athan Reines <kgryte@gmail.com>
1 parent 9f86bc6 commit 297cd66

25 files changed

+434
-362
lines changed

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

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

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

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

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

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

51-
var v = srange( N, x, 1 );
50+
var v = srange( 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 [`Float32Array`][@stdlib/array/float32].
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 Float32Array = require( '@stdlib/array/float32' );
65-
var floor = require( '@stdlib/math/base/special/floor' );
6664

6765
var x = new Float32Array( [ 1.0, 2.0, 2.0, -7.0, -2.0, 3.0, 4.0, 2.0 ] );
68-
var N = floor( x.length / 2 );
6966

70-
var v = srange( N, x, 2 );
67+
var v = srange( 4, x, 2 );
7168
// returns 6.0
7269
```
7370

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

7875
```javascript
7976
var Float32Array = require( '@stdlib/array/float32' );
80-
var floor = require( '@stdlib/math/base/special/floor' );
8177

8278
var x0 = new Float32Array( [ 2.0, 1.0, 2.0, -2.0, -2.0, 2.0, 3.0, 4.0 ] );
8379
var x1 = new Float32Array( x0.buffer, x0.BYTES_PER_ELEMENT*1 ); // start at 2nd element
8480

85-
var N = floor( x0.length / 2 );
86-
87-
var v = srange( N, x1, 2 );
81+
var v = srange( 4, x1, 2 );
8882
// returns 6.0
8983
```
9084

91-
#### srange.ndarray( N, x, stride, offset )
85+
#### srange.ndarray( N, x, strideX, offsetX )
9286

9387
Computes the [range][range] of a single-precision floating-point strided array using alternative indexing semantics.
9488

9589
```javascript
9690
var Float32Array = require( '@stdlib/array/float32' );
9791

9892
var x = new Float32Array( [ 1.0, -2.0, 2.0 ] );
99-
var N = x.length;
10093

101-
var v = srange.ndarray( N, x, 1, 0 );
94+
var v = srange.ndarray( x.length, x, 1, 0 );
10295
// returns 4.0
10396
```
10497

10598
The function has the following additional parameters:
10699

107-
- **offset**: starting index for `x`.
100+
- **offsetX**: starting index for `x`.
108101

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
102+
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
110103

111104
```javascript
112105
var Float32Array = require( '@stdlib/array/float32' );
113-
var floor = require( '@stdlib/math/base/special/floor' );
114106

115107
var x = new Float32Array( [ 2.0, 1.0, 2.0, -2.0, -2.0, 2.0, 3.0, 4.0 ] );
116-
var N = floor( x.length / 2 );
117108

118-
var v = srange.ndarray( N, x, 2, 1 );
109+
var v = srange.ndarray( 4, x, 2, 1 );
119110
// returns 6.0
120111
```
121112

@@ -140,18 +131,12 @@ var v = srange.ndarray( N, x, 2, 1 );
140131
<!-- eslint no-undef: "error" -->
141132

142133
```javascript
143-
var randu = require( '@stdlib/random/base/randu' );
144-
var round = require( '@stdlib/math/base/special/round' );
145-
var Float32Array = require( '@stdlib/array/float32' );
134+
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
146135
var srange = require( '@stdlib/stats/base/srange' );
147136

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

157142
var v = srange( x.length, x, 1 );
@@ -162,6 +147,123 @@ console.log( v );
162147

163148
<!-- /.examples -->
164149

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

lib/node_modules/@stdlib/stats/base/srange/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 isnanf = require( '@stdlib/math/base/assert/is-nanf' );
2626
var pow = require( '@stdlib/math/base/special/pow' );
27-
var Float32Array = require( '@stdlib/array/float32' );
2827
var pkg = require( './../package.json' ).name;
2928
var srange = require( './../lib/srange.js' );
3029

3130

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

3440
/**
@@ -39,13 +45,7 @@ var srange = require( './../lib/srange.js' );
3945
* @returns {Function} benchmark function
4046
*/
4147
function createBenchmark( len ) {
42-
var x;
43-
var i;
44-
45-
x = new Float32Array( 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/srange/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 isnanf = require( '@stdlib/math/base/assert/is-nanf' );
2727
var pow = require( '@stdlib/math/base/special/pow' );
28-
var Float32Array = require( '@stdlib/array/float32' );
2928
var tryRequire = require( '@stdlib/utils/try-require' );
3029
var pkg = require( './../package.json' ).name;
3130

@@ -36,6 +35,9 @@ var srange = tryRequire( resolve( __dirname, './../lib/srange.native.js' ) );
3635
var opts = {
3736
'skip': ( srange instanceof Error )
3837
};
38+
var options = {
39+
'dtype': 'float32'
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 Float32Array( 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/srange/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 isnanf = require( '@stdlib/math/base/assert/is-nanf' );
2626
var pow = require( '@stdlib/math/base/special/pow' );
27-
var Float32Array = require( '@stdlib/array/float32' );
2827
var pkg = require( './../package.json' ).name;
2928
var srange = require( './../lib/ndarray.js' );
3029

3130

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

3440
/**
@@ -39,13 +45,7 @@ var srange = require( './../lib/ndarray.js' );
3945
* @returns {Function} benchmark function
4046
*/
4147
function createBenchmark( len ) {
42-
var x;
43-
var i;
44-
45-
x = new Float32Array( 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/srange/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 isnanf = require( '@stdlib/math/base/assert/is-nanf' );
2727
var pow = require( '@stdlib/math/base/special/pow' );
28-
var Float32Array = require( '@stdlib/array/float32' );
2928
var tryRequire = require( '@stdlib/utils/try-require' );
3029
var pkg = require( './../package.json' ).name;
3130

@@ -36,6 +35,9 @@ var srange = tryRequire( resolve( __dirname, './../lib/ndarray.native.js' ) );
3635
var opts = {
3736
'skip': ( srange instanceof Error )
3837
};
38+
var options = {
39+
'dtype': 'float32'
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 Float32Array( 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)