Skip to content

Commit a6aa849

Browse files
authored
feat: add C ndarray interface and refactor implementation for stats/base/smidrange
PR-URL: #4341 Reviewed-by: Philipp Burckhardt <[email protected]>
1 parent a2ed277 commit a6aa849

25 files changed

+436
-352
lines changed

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

Lines changed: 130 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ The [**mid-range**][mid-range], or **mid-extreme**, is the arithmetic mean of th
3838
var smidrange = require( '@stdlib/stats/base/smidrange' );
3939
```
4040

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

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

@@ -55,18 +55,16 @@ The function has the following parameters:
5555

5656
- **N**: number of indexed elements.
5757
- **x**: input [`Float32Array`][@stdlib/array/float32].
58-
- **stride**: index increment for `x`.
58+
- **strideX**: stride length for `x`.
5959

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

6262
```javascript
6363
var Float32Array = require( '@stdlib/array/float32' );
64-
var floor = require( '@stdlib/math/base/special/floor' );
6564

6665
var x = new Float32Array( [ 1.0, 2.0, 2.0, -7.0, -2.0, 3.0, 4.0, 2.0 ] );
67-
var N = floor( x.length / 2 );
6866

69-
var v = smidrange( N, x, 2 );
67+
var v = smidrange( 4, x, 2 );
7068
// returns 1.0
7169
```
7270

@@ -76,18 +74,15 @@ Note that indexing is relative to the first index. To introduce an offset, use [
7674

7775
```javascript
7876
var Float32Array = require( '@stdlib/array/float32' );
79-
var floor = require( '@stdlib/math/base/special/floor' );
8077

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

84-
var N = floor( x0.length / 2 );
85-
86-
var v = smidrange( N, x1, 2 );
81+
var v = smidrange( 4, x1, 2 );
8782
// returns 1.0
8883
```
8984

90-
#### smidrange.ndarray( N, x, stride, offset )
85+
#### smidrange.ndarray( N, x, strideX, offsetX )
9186

9287
Computes the [mid-range][mid-range] of a single-precision floating-point strided array using alternative indexing semantics.
9388

@@ -102,18 +97,16 @@ var v = smidrange.ndarray( x.length, x, 1, 0 );
10297

10398
The function has the following additional parameters:
10499

105-
- **offset**: starting index for `x`.
100+
- **offsetX**: starting index for `x`.
106101

107-
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 [mid-range][mid-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 [mid-range][mid-range] for every other element in `x` starting from the second element
108103

109104
```javascript
110105
var Float32Array = require( '@stdlib/array/float32' );
111-
var floor = require( '@stdlib/math/base/special/floor' );
112106

113107
var x = new Float32Array( [ 2.0, 1.0, 2.0, -2.0, -2.0, 2.0, 3.0, 4.0 ] );
114-
var N = floor( x.length / 2 );
115108

116-
var v = smidrange.ndarray( N, x, 2, 1 );
109+
var v = smidrange.ndarray( 4, x, 2, 1 );
117110
// returns 1.0
118111
```
119112

@@ -138,18 +131,12 @@ var v = smidrange.ndarray( N, x, 2, 1 );
138131
<!-- eslint no-undef: "error" -->
139132

140133
```javascript
141-
var randu = require( '@stdlib/random/base/randu' );
142-
var round = require( '@stdlib/math/base/special/round' );
143-
var Float32Array = require( '@stdlib/array/float32' );
134+
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
144135
var smidrange = require( '@stdlib/stats/base/smidrange' );
145136

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

155142
var v = smidrange( x.length, x, 1 );
@@ -160,6 +147,123 @@ console.log( v );
160147

161148
<!-- /.examples -->
162149

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/smidrange.h"
174+
```
175+
176+
#### stdlib_strided_smidrange( N, \*X, strideX )
177+
178+
Computes the [mid-range][mid-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_smidrange( 4, x, 1 );
184+
// returns 2.5f
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_smidrange( const CBLAS_INT N, const float *X, const CBLAS_INT strideX );
195+
```
196+
197+
#### stdlib_strided_smidrange_ndarray( N, \*X, strideX, offsetX )
198+
199+
Computes the [mid-range][mid-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_smidrange_ndarray( 4, x, 1, 0 );
205+
// returns 2.5f
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_smidrange_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/smidrange.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 mid-range:
252+
float v = stdlib_strided_smidrange( N, x, strideX );
253+
254+
// Print the result:
255+
printf( "mid-range: %f\n", v );
256+
}
257+
```
258+
259+
</section>
260+
261+
<!-- /.examples -->
262+
263+
</section>
264+
265+
<!-- /.c -->
266+
163267
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
164268
165269
<section class="related">

lib/node_modules/@stdlib/stats/base/smidrange/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 smidrange = require( './../lib/smidrange.js' );
3029

3130

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

3440
/**
@@ -39,13 +45,7 @@ var smidrange = require( './../lib/smidrange.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/smidrange/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 smidrange = tryRequire( resolve( __dirname, './../lib/smidrange.native.js' )
3635
var opts = {
3736
'skip': ( smidrange 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/smidrange/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 smidrange = 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 smidrange = 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/smidrange/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 smidrange = tryRequire( resolve( __dirname, './../lib/ndarray.native.js' ) )
3635
var opts = {
3736
'skip': ( smidrange 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 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)