Skip to content

Commit 3fcf22f

Browse files
authored
feat: add C ndarray interface and refactor implementation for stats/base/dmidrange
PR-URL: #4340 Reviewed-by: Philipp Burckhardt <[email protected]>
1 parent 7d8aba0 commit 3fcf22f

25 files changed

+418
-348
lines changed

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

Lines changed: 114 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 dmidrange = require( '@stdlib/stats/base/dmidrange' );
3939
```
4040

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

4343
Computes the [mid-range][mid-range] of a double-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 [`Float64Array`][@stdlib/array/float64].
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 Float64Array = require( '@stdlib/array/float64' );
64-
var floor = require( '@stdlib/math/base/special/floor' );
6564

6665
var x = new Float64Array( [ 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 = dmidrange( N, x, 2 );
67+
var v = dmidrange( 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 Float64Array = require( '@stdlib/array/float64' );
79-
var floor = require( '@stdlib/math/base/special/floor' );
8077

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

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

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

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

@@ -102,18 +97,16 @@ var v = dmidrange.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 Float64Array = require( '@stdlib/array/float64' );
111-
var floor = require( '@stdlib/math/base/special/floor' );
112106

113107
var x = new Float64Array( [ 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 = dmidrange.ndarray( N, x, 2, 1 );
109+
var v = dmidrange.ndarray( 4, x, 2, 1 );
117110
// returns 1.0
118111
```
119112

@@ -138,18 +131,12 @@ var v = dmidrange.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 Float64Array = require( '@stdlib/array/float64' );
134+
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
144135
var dmidrange = require( '@stdlib/stats/base/dmidrange' );
145136

146-
var x;
147-
var i;
148-
149-
x = new Float64Array( 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': 'float64'
139+
});
153140
console.log( x );
154141

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

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

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

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

3130

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

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