Skip to content

Commit 384a7fe

Browse files
aayush0325kgrytestdlib-bot
authored
feat: add C ndarray interface and refactor implementation for stats/base/dminsorted
PR-URL: #4201 Co-authored-by: Athan Reines <[email protected]> Reviewed-by: Athan Reines <[email protected]> Co-authored-by: stdlib-bot <[email protected]>
1 parent f0b0632 commit 384a7fe

25 files changed

+420
-347
lines changed

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

+118-30
Original file line numberDiff line numberDiff line change
@@ -36,42 +36,38 @@ limitations under the License.
3636
var dminsorted = require( '@stdlib/stats/base/dminsorted' );
3737
```
3838

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

4141
Computes the minimum value of a sorted 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, 3.0 ] );
47-
var N = x.length;
4847

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

5251
x = new Float64Array( [ 3.0, 2.0, 1.0 ] );
53-
N = x.length;
5452

55-
v = dminsorted( N, x, 1 );
53+
v = dminsorted( x.length, x, 1 );
5654
// returns 1.0
5755
```
5856

5957
The function has the following parameters:
6058

6159
- **N**: number of indexed elements.
6260
- **x**: sorted input [`Float64Array`][@stdlib/array/float64].
63-
- **stride**: index increment for `x`.
61+
- **strideX**: stride length for `x`.
6462

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

6765
```javascript
6866
var Float64Array = require( '@stdlib/array/float64' );
69-
var floor = require( '@stdlib/math/base/special/floor' );
7067

7168
var x = new Float64Array( [ 1.0, 2.0, 2.0, -7.0, 3.0, 3.0, 4.0, 2.0 ] );
72-
var N = floor( x.length / 2 );
7369

74-
var v = dminsorted( N, x, 2 );
70+
var v = dminsorted( 4, x, 2 );
7571
// returns 1.0
7672
```
7773

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

8278
```javascript
8379
var Float64Array = require( '@stdlib/array/float64' );
84-
var floor = require( '@stdlib/math/base/special/floor' );
8580

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

89-
var N = floor( x0.length / 2 );
90-
91-
var v = dminsorted( N, x1, 2 );
84+
var v = dminsorted( 4, x1, 2 );
9285
// returns 1.0
9386
```
9487

95-
#### dminsorted.ndarray( N, x, stride, offset )
88+
#### dminsorted.ndarray( N, x, strideX, offsetX )
9689

9790
Computes the minimum value of a sorted double-precision floating-point strided array using alternative indexing semantics.
9891

9992
```javascript
10093
var Float64Array = require( '@stdlib/array/float64' );
10194

10295
var x = new Float64Array( [ 1.0, 2.0, 3.0 ] );
103-
var N = x.length;
10496

105-
var v = dminsorted.ndarray( N, x, 1, 0 );
97+
var v = dminsorted.ndarray( x.length, x, 1, 0 );
10698
// returns 1.0
10799
```
108100

109101
The function has the following additional parameters:
110102

111-
- **offset**: starting index for `x`.
103+
- **offsetX**: starting index for `x`.
112104

113-
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 value for every other value in `x` starting from the second value
105+
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 value for every other element in `x` starting from the second element
114106

115107
```javascript
116108
var Float64Array = require( '@stdlib/array/float64' );
117-
var floor = require( '@stdlib/math/base/special/floor' );
118109

119110
var x = new Float64Array( [ 2.0, 1.0, 2.0, 2.0, -2.0, 2.0, 3.0, 4.0 ] );
120-
var N = floor( x.length / 2 );
121111

122-
var v = dminsorted.ndarray( N, x, 2, 1 );
112+
var v = dminsorted.ndarray( 4, x, 2, 1 );
123113
// returns 1.0
124114
```
125115

@@ -145,16 +135,13 @@ var v = dminsorted.ndarray( N, x, 2, 1 );
145135
<!-- eslint no-undef: "error" -->
146136

147137
```javascript
148-
var Float64Array = require( '@stdlib/array/float64' );
138+
var linspace = require( '@stdlib/array/linspace' );
149139
var dminsorted = require( '@stdlib/stats/base/dminsorted' );
150140

151-
var x;
152-
var i;
153-
154-
x = new Float64Array( 10 );
155-
for ( i = 0; i < x.length; i++ ) {
156-
x[ i ] = i - 5.0;
157-
}
141+
var options = {
142+
'dtype': 'float64'
143+
};
144+
var x = linspace( -5.0, 5.0, 10, options );
158145
console.log( x );
159146

160147
var v = dminsorted( x.length, x, 1 );
@@ -165,6 +152,107 @@ console.log( v );
165152

166153
<!-- /.examples -->
167154

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

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

+9-8
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,19 @@
2222

2323
var bench = require( '@stdlib/bench' );
2424
var isnan = require( '@stdlib/math/base/assert/is-nan' );
25+
var linspace = require( '@stdlib/array/linspace' );
2526
var pow = require( '@stdlib/math/base/special/pow' );
26-
var Float64Array = require( '@stdlib/array/float64' );
2727
var pkg = require( './../package.json' ).name;
2828
var dminsorted = require( './../lib/dminsorted.js' );
2929

3030

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

3340
/**
@@ -38,13 +45,7 @@ var dminsorted = require( './../lib/dminsorted.js' );
3845
* @returns {Function} benchmark function
3946
*/
4047
function createBenchmark( len ) {
41-
var x;
42-
var i;
43-
44-
x = new Float64Array( len );
45-
for ( i = 0; i < x.length; i++ ) {
46-
x[ i ] = i;
47-
}
48+
var x = linspace( -len/2, len/2, len, options );
4849
return benchmark;
4950

5051
function benchmark( b ) {

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

+5-8
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@
2323
var resolve = require( 'path' ).resolve;
2424
var bench = require( '@stdlib/bench' );
2525
var isnan = require( '@stdlib/math/base/assert/is-nan' );
26+
var linspace = require( '@stdlib/array/linspace' );
2627
var pow = require( '@stdlib/math/base/special/pow' );
27-
var Float64Array = require( '@stdlib/array/float64' );
2828
var tryRequire = require( '@stdlib/utils/try-require' );
2929
var pkg = require( './../package.json' ).name;
3030

@@ -35,6 +35,9 @@ var dminsorted = tryRequire( resolve( __dirname, './../lib/dminsorted.native.js'
3535
var opts = {
3636
'skip': ( dminsorted instanceof Error )
3737
};
38+
var options = {
39+
'dtype': 'float64'
40+
};
3841

3942

4043
// FUNCTIONS //
@@ -47,13 +50,7 @@ var opts = {
4750
* @returns {Function} benchmark function
4851
*/
4952
function createBenchmark( len ) {
50-
var x;
51-
var i;
52-
53-
x = new Float64Array( len );
54-
for ( i = 0; i < x.length; i++ ) {
55-
x[ i ] = i;
56-
}
53+
var x = linspace( -len/2, len/2, len, options );
5754
return benchmark;
5855

5956
function benchmark( b ) {

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

+9-8
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,19 @@
2222

2323
var bench = require( '@stdlib/bench' );
2424
var isnan = require( '@stdlib/math/base/assert/is-nan' );
25+
var linspace = require( '@stdlib/array/linspace' );
2526
var pow = require( '@stdlib/math/base/special/pow' );
26-
var Float64Array = require( '@stdlib/array/float64' );
2727
var pkg = require( './../package.json' ).name;
2828
var dminsorted = require( './../lib/ndarray.js' );
2929

3030

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

3340
/**
@@ -38,13 +45,7 @@ var dminsorted = require( './../lib/ndarray.js' );
3845
* @returns {Function} benchmark function
3946
*/
4047
function createBenchmark( len ) {
41-
var x;
42-
var i;
43-
44-
x = new Float64Array( len );
45-
for ( i = 0; i < x.length; i++ ) {
46-
x[ i ] = i;
47-
}
48+
var x = linspace( -len/2, len/2, len, options );
4849
return benchmark;
4950

5051
function benchmark( b ) {

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

+5-8
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@
2323
var resolve = require( 'path' ).resolve;
2424
var bench = require( '@stdlib/bench' );
2525
var isnan = require( '@stdlib/math/base/assert/is-nan' );
26+
var linspace = require( '@stdlib/array/linspace' );
2627
var pow = require( '@stdlib/math/base/special/pow' );
27-
var Float64Array = require( '@stdlib/array/float64' );
2828
var tryRequire = require( '@stdlib/utils/try-require' );
2929
var pkg = require( './../package.json' ).name;
3030

@@ -35,6 +35,9 @@ var dminsorted = tryRequire( resolve( __dirname, './../lib/ndarray.native.js' )
3535
var opts = {
3636
'skip': ( dminsorted instanceof Error )
3737
};
38+
var options = {
39+
'dtype': 'float64'
40+
};
3841

3942

4043
// FUNCTIONS //
@@ -47,13 +50,7 @@ var opts = {
4750
* @returns {Function} benchmark function
4851
*/
4952
function createBenchmark( len ) {
50-
var x;
51-
var i;
52-
53-
x = new Float64Array( len );
54-
for ( i = 0; i < x.length; i++ ) {
55-
x[ i ] = i;
56-
}
53+
var x = linspace( -len/2, len/2, len, options );
5754
return benchmark;
5855

5956
function benchmark( b ) {

0 commit comments

Comments
 (0)