Skip to content

Commit 3d7cff6

Browse files
authored
feat: add C ndarray interface and refactor implementation for stats/base/dnanminabs
PR-URL: #4238 Reviewed-by: Philipp Burckhardt <[email protected]>
1 parent c365bf2 commit 3d7cff6

File tree

19 files changed

+393
-262
lines changed

19 files changed

+393
-262
lines changed

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

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

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

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

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

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

49-
var v = dnanminabs( N, x, 1 );
48+
var v = dnanminabs( 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+
- **strideX**: 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, -7.0, -2.0, 4.0, 3.0, NaN, NaN ] );
66-
var N = floor( x.length / 2 );
6764

68-
var v = dnanminabs( N, x, 2 );
65+
var v = dnanminabs( 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, 3.0, 4.0, NaN, NaN ] );
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 = dnanminabs( N, x1, 2 );
79+
var v = dnanminabs( 4, x1, 2 );
8680
// returns 1.0
8781
```
8882

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

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

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

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

99-
var v = dnanminabs.ndarray( N, x, 1, 0 );
92+
var v = dnanminabs.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, 3.0, 4.0, NaN, NaN ] );
114-
var N = floor( x.length / 2 );
115106

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

@@ -164,6 +155,123 @@ console.log( v );
164155

165156
<!-- /.examples -->
166157

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

lib/node_modules/@stdlib/stats/base/dnanminabs/benchmark/c/benchmark.length.c

Lines changed: 52 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ static double rand_double( void ) {
9494
* @param len array length
9595
* @return elapsed time in seconds
9696
*/
97-
static double benchmark( int iterations, int len ) {
97+
static double benchmark1( int iterations, int len ) {
9898
double elapsed;
9999
double x[ len ];
100100
double v;
@@ -111,6 +111,7 @@ static double benchmark( int iterations, int len ) {
111111
v = 0.0;
112112
t = tic();
113113
for ( i = 0; i < iterations; i++ ) {
114+
// cppcheck-suppress uninitvar
114115
v = stdlib_strided_dnanminabs( len, x, 1 );
115116
if ( v != v ) {
116117
printf( "should not return NaN\n" );
@@ -124,6 +125,44 @@ static double benchmark( int iterations, int len ) {
124125
return elapsed;
125126
}
126127

128+
/**
129+
* Runs a benchmark.
130+
*
131+
* @param iterations number of iterations
132+
* @param len array length
133+
* @return elapsed time in seconds
134+
*/
135+
static double benchmark2( int iterations, int len ) {
136+
double elapsed;
137+
double x[ len ];
138+
double v;
139+
double t;
140+
int i;
141+
142+
for ( i = 0; i < len; i++ ) {
143+
if ( rand_double() < 0.2 ) {
144+
x[ i ] = 0.0 / 0.0; // NaN
145+
} else {
146+
x[ i ] = ( rand_double() * 20000.0 ) - 10000.0;
147+
}
148+
}
149+
v = 0.0;
150+
t = tic();
151+
for ( i = 0; i < iterations; i++ ) {
152+
// cppcheck-suppress uninitvar
153+
v = stdlib_strided_dnanminabs_ndarray( len, x, 1, 0 );
154+
if ( v != v ) {
155+
printf( "should not return NaN\n" );
156+
break;
157+
}
158+
}
159+
elapsed = tic() - t;
160+
if ( v != v ) {
161+
printf( "should not return NaN\n" );
162+
}
163+
return elapsed;
164+
}
165+
127166
/**
128167
* Main execution sequence.
129168
*/
@@ -146,7 +185,18 @@ int main( void ) {
146185
for ( j = 0; j < REPEATS; j++ ) {
147186
count += 1;
148187
printf( "# c::%s:len=%d\n", NAME, len );
149-
elapsed = benchmark( iter, len );
188+
elapsed = benchmark1( iter, len );
189+
print_results( iter, elapsed );
190+
printf( "ok %d benchmark finished\n", count );
191+
}
192+
}
193+
for ( i = MIN; i <= MAX; i++ ) {
194+
len = pow( 10, i );
195+
iter = ITERATIONS / pow( 10, i-1 );
196+
for ( j = 0; j < REPEATS; j++ ) {
197+
count += 1;
198+
printf( "# c::%s:ndarray:len=%d\n", NAME, len );
199+
elapsed = benchmark2( iter, len );
150200
print_results( iter, elapsed );
151201
printf( "ok %d benchmark finished\n", count );
152202
}

lib/node_modules/@stdlib/stats/base/dnanminabs/docs/repl.txt

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11

2-
{{alias}}( N, x, stride )
2+
{{alias}}( N, x, strideX )
33
Computes the minimum absolute value of a double-precision floating-point
44
strided array, ignoring `NaN` values.
55

6-
The `N` and `stride` parameters determine which elements in `x` are accessed
7-
at runtime.
6+
The `N` and stride parameters determine which elements in the strided array
7+
are accessed at runtime.
88

99
Indexing is relative to the first index. To introduce an offset, use a typed
1010
array view.
@@ -19,8 +19,8 @@
1919
x: Float64Array
2020
Input array.
2121

22-
stride: integer
23-
Index increment.
22+
strideX: integer
23+
Stride Length.
2424

2525
Returns
2626
-------
@@ -34,22 +34,19 @@
3434
> {{alias}}( x.length, x, 1 )
3535
1.0
3636

37-
// Using `N` and `stride` parameters:
37+
// Using `N` and stride parameters:
3838
> x = new {{alias:@stdlib/array/float64}}( [ -2.0, 1.0, 1.0, -5.0, 2.0, -1.0, NaN ] );
39-
> var N = {{alias:@stdlib/math/base/special/floor}}( x.length / 2 );
40-
> var stride = 2;
41-
> {{alias}}( N, x, stride )
39+
> {{alias}}( 3, x, 2 )
4240
1.0
4341

4442
// Using view offsets:
4543
> var x0 = new {{alias:@stdlib/array/float64}}( [ 1.0, -2.0, 3.0, 2.0, 5.0, -1.0, NaN ] );
4644
> var x1 = new {{alias:@stdlib/array/float64}}( x0.buffer, x0.BYTES_PER_ELEMENT*1 );
47-
> N = {{alias:@stdlib/math/base/special/floor}}( x0.length / 2 );
48-
> stride = 2;
49-
> {{alias}}( N, x1, stride )
45+
> {{alias}}( 3, x1, 2 )
5046
1.0
5147

52-
{{alias}}.ndarray( N, x, stride, offset )
48+
49+
{{alias}}.ndarray( N, x, strideX, offsetX )
5350
Computes the minimum absolute value of a double-precision floating-point
5451
strided array, ignoring `NaN` values and using alternative indexing
5552
semantics.
@@ -66,10 +63,10 @@
6663
x: Float64Array
6764
Input array.
6865

69-
stride: integer
70-
Index increment.
66+
strideX: integer
67+
Stride Length.
7168

72-
offset: integer
69+
offsetX: integer
7370
Starting index.
7471

7572
Returns
@@ -86,8 +83,7 @@
8683

8784
// Using offset parameter:
8885
> var x = new {{alias:@stdlib/array/float64}}( [ 1.0, -2.0, 3.0, 2.0, 5.0, -1.0, NaN ] );
89-
> var N = {{alias:@stdlib/math/base/special/floor}}( x.length / 2 );
90-
> {{alias}}.ndarray( N, x, 2, 1 )
86+
> {{alias}}.ndarray( 3, x, 2, 1 )
9187
1.0
9288

9389
See Also

0 commit comments

Comments
 (0)