Skip to content

Commit c365bf2

Browse files
feat: add C ndarray interface and refactor implementation for stats/base/dminabs
PR-URL: #4197 Co-authored-by: stdlib-bot <[email protected]> Reviewed-by: Philipp Burckhardt <[email protected]>
1 parent 1aa67dd commit c365bf2

26 files changed

+468
-401
lines changed

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

Lines changed: 116 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -36,36 +36,33 @@ limitations under the License.
3636
var dminabs = require( '@stdlib/stats/base/dminabs' );
3737
```
3838

39-
#### dminabs( N, x, stride )
39+
#### dminabs( N, x, strideX )
4040

4141
Computes the minimum absolute value of a double-precision floating-point strided array `x`.
4242

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

4646
var x = new Float64Array( [ 1.0, -2.0, 2.0 ] );
47-
var N = x.length;
4847

49-
var v = dminabs( N, x, 1 );
48+
var v = dminabs( x.length, x, 1 );
5049
// returns 1.0
5150
```
5251

5352
The function has the following parameters:
5453

5554
- **N**: number of indexed elements.
5655
- **x**: input [`Float64Array`][@stdlib/array/float64].
57-
- **stride**: index increment for `x`.
56+
- **stride**: stride length for `x`.
5857

59-
The `N` and `stride` parameters determine which elements in `x` are accessed at runtime. For example, to compute the minimum absolute value of every other element in `x`,
58+
The `N` and stride parameters determine which elements in the strided array are accessed at runtime. For example, to compute the minimum absolute value of every other element in `x`,
6059

6160
```javascript
6261
var Float64Array = require( '@stdlib/array/float64' );
63-
var floor = require( '@stdlib/math/base/special/floor' );
6462

6563
var x = new Float64Array( [ 1.0, 2.0, 2.0, -7.0, -2.0, 3.0, 4.0, 2.0 ] );
66-
var N = floor( x.length / 2 );
6764

68-
var v = dminabs( N, x, 2 );
65+
var v = dminabs( 4, x, 2 );
6966
// returns 1.0
7067
```
7168

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

7673
```javascript
7774
var Float64Array = require( '@stdlib/array/float64' );
78-
var floor = require( '@stdlib/math/base/special/floor' );
7975

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

83-
var N = floor( x0.length / 2 );
84-
85-
var v = dminabs( N, x1, 2 );
79+
var v = dminabs( 4, x1, 2 );
8680
// returns 1.0
8781
```
8882

89-
#### dminabs.ndarray( N, x, stride, offset )
83+
#### dminabs.ndarray( N, x, strideX, offsetX )
9084

9185
Computes the minimum absolute value of a double-precision floating-point strided array using alternative indexing semantics.
9286

9387
```javascript
9488
var Float64Array = require( '@stdlib/array/float64' );
9589

9690
var x = new Float64Array( [ 1.0, -2.0, 2.0 ] );
97-
var N = x.length;
9891

99-
var v = dminabs.ndarray( N, x, 1, 0 );
92+
var v = dminabs.ndarray( x.length, x, 1, 0 );
10093
// returns 1.0
10194
```
10295

10396
The function has the following additional parameters:
10497

105-
- **offset**: starting index for `x`.
98+
- **offsetX**: starting index for `x`.
10699

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 minimum absolute value for every other value in `x` starting from the second value
100+
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 minimum absolute value for every other element in `x` starting from the second element
108101

109102
```javascript
110103
var Float64Array = require( '@stdlib/array/float64' );
111-
var floor = require( '@stdlib/math/base/special/floor' );
112104

113105
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 );
115106

116-
var v = dminabs.ndarray( N, x, 2, 1 );
107+
var v = dminabs.ndarray( 4, x, 2, 1 );
117108
// returns 1.0
118109
```
119110

@@ -138,18 +129,12 @@ var v = dminabs.ndarray( N, x, 2, 1 );
138129
<!-- eslint no-undef: "error" -->
139130

140131
```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' );
132+
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
144133
var dminabs = require( '@stdlib/stats/base/dminabs' );
145134

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-
}
135+
var x = discreteUniform( 10, -50, 50, {
136+
'dtype': 'float64'
137+
});
153138
console.log( x );
154139

155140
var v = dminabs( x.length, x, 1 );
@@ -160,6 +145,107 @@ console.log( v );
160145

161146
<!-- /.examples -->
162147

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

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

3130

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

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