Skip to content

Commit da9fc9e

Browse files
aayush0325stdlib-bot
authored andcommitted
feat: add C ndarray interface and refactor implementation for stats/base/smaxsorted
PR-URL: stdlib-js#4481 Reviewed-by: Athan Reines <[email protected]> Co-authored-by: stdlib-bot <[email protected]>
1 parent e51f2ef commit da9fc9e

25 files changed

+440
-351
lines changed

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

+133-29
Original file line numberDiff line numberDiff line change
@@ -44,34 +44,30 @@ Computes the maximum value of a sorted single-precision floating-point strided a
4444
var Float32Array = require( '@stdlib/array/float32' );
4545

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

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

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

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

5957
The function has the following parameters:
6058

6159
- **N**: number of indexed elements.
6260
- **x**: sorted input [`Float32Array`][@stdlib/array/float32].
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 maximum 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 maximum value of every other element in `x`,
6664

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

7168
var x = new Float32Array( [ 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 = smaxsorted( N, x, 2 );
70+
var v = smaxsorted( 4, x, 2 );
7571
// returns 4.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 Float32Array = require( '@stdlib/array/float32' );
84-
var floor = require( '@stdlib/math/base/special/floor' );
8580

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

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

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

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

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

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

105-
var v = smaxsorted.ndarray( N, x, 1, 0 );
97+
var v = smaxsorted.ndarray( x.length, x, 1, 0 );
10698
// returns 3.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 maximum 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 maximum value for every other element in `x` starting from the second element
114106

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

119110
var x = new Float32Array( [ 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 = smaxsorted.ndarray( N, x, 2, 1 );
112+
var v = smaxsorted.ndarray( 4, x, 2, 1 );
123113
// returns 4.0
124114
```
125115

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

147137
```javascript
148-
var Float32Array = require( '@stdlib/array/float32' );
138+
var linspace = require( '@stdlib/array/linspace' );
149139
var smaxsorted = require( '@stdlib/stats/base/smaxsorted' );
150140

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

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

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

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

lib/node_modules/@stdlib/stats/base/smaxsorted/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 isnanf = require( '@stdlib/math/base/assert/is-nanf' );
25+
var linspace = require( '@stdlib/array/linspace' );
2526
var pow = require( '@stdlib/math/base/special/pow' );
26-
var Float32Array = require( '@stdlib/array/float32' );
2727
var pkg = require( './../package.json' ).name;
2828
var smaxsorted = require( './../lib/smaxsorted.js' );
2929

3030

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

3340
/**
@@ -38,13 +45,7 @@ var smaxsorted = require( './../lib/smaxsorted.js' );
3845
* @returns {Function} benchmark function
3946
*/
4047
function createBenchmark( len ) {
41-
var x;
42-
var i;
43-
44-
x = new Float32Array( 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/smaxsorted/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 isnanf = require( '@stdlib/math/base/assert/is-nanf' );
26+
var linspace = require( '@stdlib/array/linspace' );
2627
var pow = require( '@stdlib/math/base/special/pow' );
27-
var Float32Array = require( '@stdlib/array/float32' );
2828
var tryRequire = require( '@stdlib/utils/try-require' );
2929
var pkg = require( './../package.json' ).name;
3030

@@ -35,6 +35,9 @@ var smaxsorted = tryRequire( resolve( __dirname, './../lib/smaxsorted.native.js'
3535
var opts = {
3636
'skip': ( smaxsorted instanceof Error )
3737
};
38+
var options = {
39+
'dtype': 'float32'
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 Float32Array( 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/smaxsorted/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 isnanf = require( '@stdlib/math/base/assert/is-nanf' );
25+
var linspace = require( '@stdlib/array/linspace' );
2526
var pow = require( '@stdlib/math/base/special/pow' );
26-
var Float32Array = require( '@stdlib/array/float32' );
2727
var pkg = require( './../package.json' ).name;
2828
var smaxsorted = require( './../lib/ndarray.js' );
2929

3030

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

3340
/**
@@ -38,13 +45,7 @@ var smaxsorted = require( './../lib/ndarray.js' );
3845
* @returns {Function} benchmark function
3946
*/
4047
function createBenchmark( len ) {
41-
var x;
42-
var i;
43-
44-
x = new Float32Array( 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/smaxsorted/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 isnanf = require( '@stdlib/math/base/assert/is-nanf' );
26+
var linspace = require( '@stdlib/array/linspace' );
2627
var pow = require( '@stdlib/math/base/special/pow' );
27-
var Float32Array = require( '@stdlib/array/float32' );
2828
var tryRequire = require( '@stdlib/utils/try-require' );
2929
var pkg = require( './../package.json' ).name;
3030

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