Skip to content

Commit 86a3364

Browse files
authored
feat: add C ndarray interface and refactor implementation for stats/base/snanmax
PR-URL: #4287 Reviewed-by: Philipp Burckhardt <pburckhardt@outlook.com>
1 parent 82745a5 commit 86a3364

25 files changed

+479
-392
lines changed

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

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

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

4141
Computes the maximum value of a single-precision floating-point strided array `x`, ignoring `NaN` values.
4242

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

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

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

5352
The function has the following parameters:
5453

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

59-
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`,
58+
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`,
6059

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

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

68-
var v = snanmax( N, x, 2 );
65+
var v = snanmax( 4, x, 2 );
6966
// returns 4.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 Float32Array = require( '@stdlib/array/float32' );
78-
var floor = require( '@stdlib/math/base/special/floor' );
7975

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

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

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

9185
Computes the maximum value of a single-precision floating-point strided array, ignoring `NaN` values and using alternative indexing semantics.
9286

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

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

99-
var v = snanmax.ndarray( N, x, 1, 0 );
92+
var v = snanmax.ndarray( x.length, x, 1, 0 );
10093
// returns 2.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 maximum 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 maximum value for every other element in `x` starting from the second element
108101

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

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

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

@@ -138,22 +129,12 @@ var v = snanmax.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 Float32Array = require( '@stdlib/array/float32' );
132+
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
144133
var snanmax = require( '@stdlib/stats/base/snanmax' );
145134

146-
var x;
147-
var i;
148-
149-
x = new Float32Array( 10 );
150-
for ( i = 0; i < x.length; i++ ) {
151-
if ( randu() < 0.2 ) {
152-
x[ i ] = NaN;
153-
} else {
154-
x[ i ] = round( (randu()*100.0) - 50.0 );
155-
}
156-
}
135+
var x = discreteUniform( 10, -50, 50, {
136+
'dtype': 'float32'
137+
});
157138
console.log( x );
158139

159140
var v = snanmax( x.length, x, 1 );
@@ -164,6 +145,123 @@ console.log( v );
164145

165146
<!-- /.examples -->
166147

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

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

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,29 @@
2121
// MODULES //
2222

2323
var bench = require( '@stdlib/bench' );
24-
var randu = require( '@stdlib/random/base/randu' );
24+
var uniform = require( '@stdlib/random/base/uniform' );
25+
var bernoulli = require( '@stdlib/random/base/bernoulli' );
26+
var filledarrayBy = require( '@stdlib/array/filled-by' );
2527
var isnan = require( '@stdlib/math/base/assert/is-nan' );
2628
var pow = require( '@stdlib/math/base/special/pow' );
27-
var Float32Array = require( '@stdlib/array/float32' );
2829
var pkg = require( './../package.json' ).name;
2930
var snanmax = require( './../lib/snanmax.js' );
3031

3132

3233
// FUNCTIONS //
3334

35+
/**
36+
* Returns a random value or `NaN`.
37+
*
38+
* @returns {number} random number or `NaN`
39+
*/
40+
function rand() {
41+
if ( bernoulli( 0.2 ) ) {
42+
return NaN;
43+
}
44+
return uniform( -10.0, 10.0 );
45+
}
46+
3447
/**
3548
* Creates a benchmark function.
3649
*
@@ -39,17 +52,7 @@ var snanmax = require( './../lib/snanmax.js' );
3952
* @returns {Function} benchmark function
4053
*/
4154
function createBenchmark( len ) {
42-
var x;
43-
var i;
44-
45-
x = new Float32Array( len );
46-
for ( i = 0; i < x.length; i++ ) {
47-
if ( randu() < 0.2 ) {
48-
x[ i ] = NaN;
49-
} else {
50-
x[ i ] = ( randu()*20.0 ) - 10.0;
51-
}
52-
}
55+
var x = filledarrayBy( len, 'float32', rand );
5356
return benchmark;
5457

5558
function benchmark( b ) {

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

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,11 @@
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/base/uniform' );
26+
var bernoulli = require( '@stdlib/random/base/bernoulli' );
27+
var filledarrayBy = require( '@stdlib/array/filled-by' );
2628
var isnan = require( '@stdlib/math/base/assert/is-nan' );
2729
var pow = require( '@stdlib/math/base/special/pow' );
28-
var Float32Array = require( '@stdlib/array/float32' );
2930
var tryRequire = require( '@stdlib/utils/try-require' );
3031
var pkg = require( './../package.json' ).name;
3132

@@ -40,6 +41,18 @@ var opts = {
4041

4142
// FUNCTIONS //
4243

44+
/**
45+
* Returns a random value or `NaN`.
46+
*
47+
* @returns {number} random number or `NaN`
48+
*/
49+
function rand() {
50+
if ( bernoulli( 0.2 ) ) {
51+
return NaN;
52+
}
53+
return uniform( -10.0, 10.0 );
54+
}
55+
4356
/**
4457
* Creates a benchmark function.
4558
*
@@ -48,17 +61,7 @@ var opts = {
4861
* @returns {Function} benchmark function
4962
*/
5063
function createBenchmark( len ) {
51-
var x;
52-
var i;
53-
54-
x = new Float32Array( len );
55-
for ( i = 0; i < x.length; i++ ) {
56-
if ( randu() < 0.2 ) {
57-
x[ i ] = NaN;
58-
} else {
59-
x[ i ] = ( randu()*20.0 ) - 10.0;
60-
}
61-
}
64+
var x = filledarrayBy( len, 'float32', rand );
6265
return benchmark;
6366

6467
function benchmark( b ) {

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

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,29 @@
2121
// MODULES //
2222

2323
var bench = require( '@stdlib/bench' );
24-
var randu = require( '@stdlib/random/base/randu' );
24+
var uniform = require( '@stdlib/random/base/uniform' );
25+
var bernoulli = require( '@stdlib/random/base/bernoulli' );
26+
var filledarrayBy = require( '@stdlib/array/filled-by' );
2527
var isnan = require( '@stdlib/math/base/assert/is-nan' );
2628
var pow = require( '@stdlib/math/base/special/pow' );
27-
var Float32Array = require( '@stdlib/array/float32' );
2829
var pkg = require( './../package.json' ).name;
2930
var snanmax = require( './../lib/ndarray.js' );
3031

3132

3233
// FUNCTIONS //
3334

35+
/**
36+
* Returns a random value or `NaN`.
37+
*
38+
* @returns {number} random number or `NaN`
39+
*/
40+
function rand() {
41+
if ( bernoulli( 0.2 ) ) {
42+
return NaN;
43+
}
44+
return uniform( -10.0, 10.0 );
45+
}
46+
3447
/**
3548
* Creates a benchmark function.
3649
*
@@ -39,17 +52,7 @@ var snanmax = require( './../lib/ndarray.js' );
3952
* @returns {Function} benchmark function
4053
*/
4154
function createBenchmark( len ) {
42-
var x;
43-
var i;
44-
45-
x = new Float32Array( len );
46-
for ( i = 0; i < x.length; i++ ) {
47-
if ( randu() < 0.2 ) {
48-
x[ i ] = NaN;
49-
} else {
50-
x[ i ] = ( randu()*20.0 ) - 10.0;
51-
}
52-
}
55+
var x = filledarrayBy( len, 'float32', rand );
5356
return benchmark;
5457

5558
function benchmark( b ) {

0 commit comments

Comments
 (0)