Skip to content

Commit 045a348

Browse files
feat: add C ndarray API and refactor blas/ext/base/dnannsum
PR-URL: #3197 Reviewed-by: Athan Reines <[email protected]> Co-authored-by: stdlib-bot <[email protected]>
1 parent 35ea456 commit 045a348

File tree

19 files changed

+356
-117
lines changed

19 files changed

+356
-117
lines changed

lib/node_modules/@stdlib/blas/ext/base/dnannsum/README.md

Lines changed: 136 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -54,11 +54,11 @@ The function has the following parameters:
5454

5555
- **N**: number of indexed elements.
5656
- **x**: input [`Float64Array`][@stdlib/array/float64].
57-
- **strideX**: index increment for `x`.
57+
- **strideX**: stride length for `x`.
5858
- **out**: output [`Float64Array`][@stdlib/array/float64] whose first element is the sum and whose second element is the number of non-NaN elements.
59-
- **strideOut**: index increment for `out`.
59+
- **strideOut**: stride length for `out`.
6060

61-
The `N` and `stride` parameters determine which elements in the strided array are accessed at runtime. For example, to compute the sum of every other element in the strided array,
61+
The `N` and stride parameters determine which elements in the strided array are accessed at runtime. For example, to compute the sum of every other element in the strided array:
6262

6363
```javascript
6464
var Float64Array = require( '@stdlib/array/float64' );
@@ -106,7 +106,7 @@ The function has the following additional parameters:
106106
- **offsetX**: starting index for `x`.
107107
- **offsetOut**: starting index for `out`.
108108

109-
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 sum of every other value in the strided array starting from the second value
109+
While [`typed array`][mdn-typed-array] views mandate a view offset based on the underlying buffer, the offset parameters support indexing semantics based on starting indices. For example, to calculate the sum of every other element starting from the second element:
110110

111111
```javascript
112112
var Float64Array = require( '@stdlib/array/float64' );
@@ -145,14 +145,14 @@ var filledarrayBy = require( '@stdlib/array/filled-by' );
145145
var Float64Array = require( '@stdlib/array/float64' );
146146
var dnannsum = require( '@stdlib/blas/ext/base/dnannsum' );
147147

148-
function clbk() {
148+
function rand() {
149149
if ( bernoulli( 0.7 ) > 0 ) {
150150
return discreteUniform( 0, 100 );
151151
}
152152
return NaN;
153153
}
154154

155-
var x = filledarrayBy( 10, 'float64', clbk );
155+
var x = filledarrayBy( 10, 'float64', rand );
156156
console.log( x );
157157

158158
var out = new Float64Array( 2 );
@@ -164,6 +164,136 @@ console.log( out );
164164

165165
<!-- /.examples -->
166166

167+
<!-- C interface documentation. -->
168+
169+
* * *
170+
171+
<section class="c">
172+
173+
## C APIs
174+
175+
<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->
176+
177+
<section class="intro">
178+
179+
</section>
180+
181+
<!-- /.intro -->
182+
183+
<!-- C usage documentation. -->
184+
185+
<section class="usage">
186+
187+
### Usage
188+
189+
```c
190+
#include "stdlib/blas/ext/base/dnannsum.h"
191+
```
192+
193+
#### stdlib_strided_dnannsum( N, \*X, strideX, \*n )
194+
195+
Computes the sum of double-precision floating-point strided array elements, ignoring `NaN` values.
196+
197+
```c
198+
#include "stdlib/blas/base/shared.h"
199+
200+
const double x[] = { 1.0, 2.0, 0.0/0.0, 4.0 };
201+
CBLAS_INT n = 0;
202+
203+
double v = stdlib_strided_dnannsum( 4, x, 1, &n );
204+
// returns 7.0
205+
```
206+
207+
The function accepts the following arguments:
208+
209+
- **N**: `[in] CBLAS_INT` number of indexed elements.
210+
- **X**: `[in] double*` input array.
211+
- **strideX**: `[in] CBLAS_INT` stride length for `X`.
212+
- **n**: `[out] CBLAS_INT*` pointer for storing the number of non-NaN elements.
213+
214+
```c
215+
double stdlib_strided_dnannsum( const CBLAS_INT N, const double *X, const CBLAS_INT strideX, CBLAS_INT *n );
216+
```
217+
218+
#### stdlib_strided_dnannsum_ndarray( N, \*X, strideX, offsetX, \*n )
219+
220+
Computes the sum of double-precision floating-point strided array elements, ignoring `NaN` values and using alternative indexing semantics.
221+
222+
```c
223+
#include "stdlib/blas/base/shared.h"
224+
225+
const double x[] = { 1.0, 2.0, 0.0/0.0, 4.0 };
226+
CBLAS_INT n = 0;
227+
228+
double v = stdlib_strided_dnannsum_ndarray( 4, x, 1, 0, &n );
229+
// returns 7.0
230+
```
231+
232+
The function accepts the following arguments:
233+
234+
- **N**: `[in] CBLAS_INT` number of indexed elements.
235+
- **X**: `[in] double*` input array.
236+
- **strideX**: `[in] CBLAS_INT` stride length for `X`.
237+
- **offsetX**: `[in] CBLAS_INT` starting index for `X`.
238+
- **n**: `[out] CBLAS_INT*` pointer for storing the number of non-NaN elements.
239+
240+
```c
241+
double stdlib_strided_dnannsum_ndarray( const CBLAS_INT N, const double *X, const CBLAS_INT strideX, const CBLAS_INT offsetX, CBLAS_INT *n );
242+
```
243+
244+
</section>
245+
246+
<!-- /.usage -->
247+
248+
<!-- C API usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
249+
250+
<section class="notes">
251+
252+
</section>
253+
254+
<!-- /.notes -->
255+
256+
<!-- C API usage examples. -->
257+
258+
<section class="examples">
259+
260+
### Examples
261+
262+
```c
263+
#include "stdlib/blas/ext/base/dnannsum.h"
264+
#include "stdlib/blase/base/shared.h"
265+
#include <stdio.h>
266+
267+
int main( void ) {
268+
// Create a strided array:
269+
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 };
270+
271+
// Specify the number of elements:
272+
const int N = 5;
273+
274+
// Specify the stride length:
275+
const int strideX = 2;
276+
277+
// Initialize a variable for storing the number of non-NaN elements:
278+
CBLAS_INT n = 0;
279+
280+
// Compute the sum:
281+
double v = stdlib_strided_dnannsum( N, x, strideX, &n );
282+
283+
// Print the result:
284+
printf( "sum: %lf\n", v );
285+
printf( "n: %"CBLAS_IFMT"\n", n );
286+
}
287+
```
288+
289+
</section>
290+
291+
<!-- /.examples -->
292+
293+
</section>
294+
295+
<!-- /.c -->
296+
167297
<section class="references">
168298
169299
</section>

lib/node_modules/@stdlib/blas/ext/base/dnannsum/benchmark/benchmark.js

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,19 @@ var dnannsum = require( './../lib/dnannsum.js' );
3333

3434
// FUNCTIONS //
3535

36+
/**
37+
* Returns a random number.
38+
*
39+
* @private
40+
* @returns {number} random number
41+
*/
42+
function rand() {
43+
if ( bernoulli( 0.7 ) > 0 ) {
44+
return discreteUniform( -10, 10 );
45+
}
46+
return NaN;
47+
}
48+
3649
/**
3750
* Creates a benchmark function.
3851
*
@@ -44,17 +57,10 @@ function createBenchmark( len ) {
4457
var out;
4558
var x;
4659

47-
x = filledarrayBy( len, 'float64', clbk );
60+
x = filledarrayBy( len, 'float64', rand );
4861
out = new Float64Array( 2 );
4962
return benchmark;
5063

51-
function clbk() {
52-
if ( bernoulli( 0.7 ) > 0 ) {
53-
return discreteUniform( -10, 10 );
54-
}
55-
return NaN;
56-
}
57-
5864
function benchmark( b ) {
5965
var i;
6066

lib/node_modules/@stdlib/blas/ext/base/dnannsum/benchmark/benchmark.native.js

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,19 @@ var opts = {
4242

4343
// FUNCTIONS //
4444

45+
/**
46+
* Returns a random number.
47+
*
48+
* @private
49+
* @returns {number} random number
50+
*/
51+
function rand() {
52+
if ( bernoulli( 0.7 ) > 0 ) {
53+
return discreteUniform( -10, 10 );
54+
}
55+
return NaN;
56+
}
57+
4558
/**
4659
* Creates a benchmark function.
4760
*
@@ -53,17 +66,10 @@ function createBenchmark( len ) {
5366
var out;
5467
var x;
5568

56-
x = filledarrayBy( len, 'float64', clbk );
69+
x = filledarrayBy( len, 'float64', rand );
5770
out = new Float64Array( 2 );
5871
return benchmark;
5972

60-
function clbk() {
61-
if ( bernoulli( 0.7 ) > 0 ) {
62-
return discreteUniform( -10, 10 );
63-
}
64-
return NaN;
65-
}
66-
6773
function benchmark( b ) {
6874
var i;
6975

lib/node_modules/@stdlib/blas/ext/base/dnannsum/benchmark/benchmark.ndarray.js

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,19 @@ var dnannsum = require( './../lib/ndarray.js' );
3333

3434
// FUNCTIONS //
3535

36+
/**
37+
* Returns a random number.
38+
*
39+
* @private
40+
* @returns {number} random number
41+
*/
42+
function rand() {
43+
if ( bernoulli( 0.7 ) > 0 ) {
44+
return discreteUniform( -10, 10 );
45+
}
46+
return NaN;
47+
}
48+
3649
/**
3750
* Creates a benchmark function.
3851
*
@@ -44,17 +57,10 @@ function createBenchmark( len ) {
4457
var out;
4558
var x;
4659

47-
x = filledarrayBy( len, 'float64', clbk );
60+
x = filledarrayBy( len, 'float64', rand );
4861
out = new Float64Array( 2 );
4962
return benchmark;
5063

51-
function clbk() {
52-
if ( bernoulli( 0.7 ) > 0 ) {
53-
return discreteUniform( -10, 10 );
54-
}
55-
return NaN;
56-
}
57-
5864
function benchmark( b ) {
5965
var i;
6066

lib/node_modules/@stdlib/blas/ext/base/dnannsum/benchmark/benchmark.ndarray.native.js

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,19 @@ var opts = {
4242

4343
// FUNCTIONS //
4444

45+
/**
46+
* Returns a random number.
47+
*
48+
* @private
49+
* @returns {number} random number
50+
*/
51+
function rand() {
52+
if ( bernoulli( 0.7 ) > 0 ) {
53+
return discreteUniform( -10, 10 );
54+
}
55+
return NaN;
56+
}
57+
4558
/**
4659
* Creates a benchmark function.
4760
*
@@ -53,17 +66,10 @@ function createBenchmark( len ) {
5366
var out;
5467
var x;
5568

56-
x = filledarrayBy( len, 'float64', clbk );
69+
x = filledarrayBy( len, 'float64', rand );
5770
out = new Float64Array( 2 );
5871
return benchmark;
5972

60-
function clbk() {
61-
if ( bernoulli( 0.7 ) > 0 ) {
62-
return discreteUniform( -10, 10 );
63-
}
64-
return NaN;
65-
}
66-
6773
function benchmark( b ) {
6874
var i;
6975

0 commit comments

Comments
 (0)