Skip to content

Commit 4212a2f

Browse files
committed
feat: add C ndarray interface and refactor implementation
--- type: pre_commit_static_analysis_report description: Results of running static analysis checks when committing changes. report: - task: lint_filenames status: passed - task: lint_editorconfig status: passed - task: lint_markdown status: passed - task: lint_package_json status: na - task: lint_repl_help status: passed - task: lint_javascript_src status: passed - task: lint_javascript_cli status: na - task: lint_javascript_examples status: na - task: lint_javascript_tests status: passed - task: lint_javascript_benchmarks status: na - task: lint_python status: na - task: lint_r status: na - task: lint_c_src status: passed - task: lint_c_examples status: passed - task: lint_c_benchmarks status: passed - task: lint_c_tests_fixtures status: na - task: lint_shell status: na - task: lint_typescript_declarations status: passed - task: lint_typescript_tests status: na - task: lint_license_headers status: passed ---
1 parent 04fda1b commit 4212a2f

File tree

20 files changed

+390
-309
lines changed

20 files changed

+390
-309
lines changed

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

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

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

4141
Computes the maximum 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 = dnanmax( N, x, 1 );
48+
var v = dnanmax( 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 [`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 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 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 = dnanmax( N, x, 2 );
65+
var v = dnanmax( 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 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 = dnanmax( N, x1, 2 );
79+
var v = dnanmax( 4, x1, 2 );
8680
// returns 4.0
8781
```
8882

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

9185
Computes the maximum 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 = dnanmax.ndarray( N, x, 1, 0 );
92+
var v = dnanmax.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 value in `x` starting from the second value
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 = dnanmax.ndarray( N, x, 2, 1 );
107+
var v = dnanmax.ndarray( 4, x, 2, 1 );
117108
// returns 4.0
118109
```
119110

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

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

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

lib/node_modules/@stdlib/stats/base/dnanmax/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_dnanmax( 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_dnanmax_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/dnanmax/docs/repl.txt

Lines changed: 12 additions & 16 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 maximum value of a double-precision floating-point strided
44
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,7 +19,7 @@
1919
x: Float64Array
2020
Input array.
2121

22-
stride: integer
22+
strideX: integer
2323
Index increment.
2424

2525
Returns
@@ -34,22 +34,19 @@
3434
> {{alias}}( x.length, x, 1 )
3535
2.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
2.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
2.0
5147

52-
{{alias}}.ndarray( N, x, stride, offset )
48+
49+
{{alias}}.ndarray( N, x, strideX, offsetX )
5350
Computes the maximum value of a double-precision floating-point strided
5451
array, ignoring `NaN` values and using alternative indexing semantics.
5552

@@ -65,10 +62,10 @@
6562
x: Float64Array
6663
Input array.
6764

68-
stride: integer
65+
strideX: integer
6966
Index increment.
7067

71-
offset: integer
68+
offsetX: integer
7269
Starting index.
7370

7471
Returns
@@ -85,8 +82,7 @@
8582

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

9288
See Also

0 commit comments

Comments
 (0)