Skip to content

Commit 14021a5

Browse files
authored
feat: add C ndarray API and refactor blas/ext/base/dnansumpw
PR-URL: #3202 Reviewed-by: Athan Reines <kgryte@gmail.com>
1 parent c328fc5 commit 14021a5

23 files changed

+452
-227
lines changed

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

+125-11
Original file line numberDiff line numberDiff line change
@@ -36,27 +36,26 @@ limitations under the License.
3636
var dnansumpw = require( '@stdlib/blas/ext/base/dnansumpw' );
3737
```
3838

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

4141
Computes the sum of double-precision floating-point strided array elements, ignoring `NaN` values and using pairwise summation.
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 = dnansumpw( N, x, 1 );
48+
var v = dnansumpw( 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 the strided array are accessed at runtime. For example, to compute the sum 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 sum of every other element:
6059

6160
```javascript
6261
var Float64Array = require( '@stdlib/array/float64' );
@@ -81,7 +80,7 @@ var v = dnansumpw( 4, x1, 2 );
8180
// returns 5.0
8281
```
8382

84-
#### dnansumpw.ndarray( N, x, stride, offset )
83+
#### dnansumpw.ndarray( N, x, strideX, offsetX )
8584

8685
Computes the sum of double-precision floating-point strided array elements, ignoring `NaN` values and using pairwise summation and alternative indexing semantics.
8786

@@ -90,15 +89,15 @@ var Float64Array = require( '@stdlib/array/float64' );
9089

9190
var x = new Float64Array( [ 1.0, -2.0, NaN, 2.0 ] );
9291

93-
var v = dnansumpw.ndarray( 4, x, 1, 0 );
92+
var v = dnansumpw.ndarray( x.length, x, 1, 0 );
9493
// returns 1.0
9594
```
9695

9796
The function has the following additional parameters:
9897

99-
- **offset**: starting index for `x`.
98+
- **offsetX**: starting index for `x`.
10099

101-
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 `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 sum of every other element starting from the second element:
102101

103102
```javascript
104103
var Float64Array = require( '@stdlib/array/float64' );
@@ -136,14 +135,14 @@ var bernoulli = require( '@stdlib/random/base/bernoulli' );
136135
var filledarrayBy = require( '@stdlib/array/filled-by' );
137136
var dnansumpw = require( '@stdlib/blas/ext/base/dnansumpw' );
138137

139-
function clbk() {
138+
function rand() {
140139
if ( bernoulli( 0.7 ) > 0 ) {
141140
return discreteUniform( 0, 100 );
142141
}
143142
return NaN;
144143
}
145144

146-
var x = filledarrayBy( 10, 'float64', clbk );
145+
var x = filledarrayBy( 10, 'float64', rand );
147146
console.log( x );
148147

149148
var v = dnansumpw( x.length, x, 1 );
@@ -154,8 +153,123 @@ console.log( v );
154153

155154
<!-- /.examples -->
156155

156+
<!-- C interface documentation. -->
157+
157158
* * *
158159

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

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

+14-8
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,19 @@ var dnansumpw = require( './../lib/dnansumpw.js' );
3232

3333
// FUNCTIONS //
3434

35+
/**
36+
* Returns a random number.
37+
*
38+
* @private
39+
* @returns {number} random number
40+
*/
41+
function rand() {
42+
if ( bernoulli( 0.8 ) > 0 ) {
43+
return uniform( -10.0, 10.0 );
44+
}
45+
return NaN;
46+
}
47+
3548
/**
3649
* Creates a benchmark function.
3750
*
@@ -40,16 +53,9 @@ var dnansumpw = require( './../lib/dnansumpw.js' );
4053
* @returns {Function} benchmark function
4154
*/
4255
function createBenchmark( len ) {
43-
var x = filledarrayBy( len, 'float64', clbk );
56+
var x = filledarrayBy( len, 'float64', rand );
4457
return benchmark;
4558

46-
function clbk() {
47-
if ( bernoulli( 0.7 ) > 0 ) {
48-
return uniform( -100.0, 100.0 );
49-
}
50-
return NaN;
51-
}
52-
5359
function benchmark( b ) {
5460
var v;
5561
var i;

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

+14-8
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,19 @@ var opts = {
4141

4242
// FUNCTIONS //
4343

44+
/**
45+
* Returns a random number.
46+
*
47+
* @private
48+
* @returns {number} random number
49+
*/
50+
function rand() {
51+
if ( bernoulli( 0.8 ) > 0 ) {
52+
return uniform( -10.0, 10.0 );
53+
}
54+
return NaN;
55+
}
56+
4457
/**
4558
* Creates a benchmark function.
4659
*
@@ -49,16 +62,9 @@ var opts = {
4962
* @returns {Function} benchmark function
5063
*/
5164
function createBenchmark( len ) {
52-
var x = filledarrayBy( len, 'float64', clbk );
65+
var x = filledarrayBy( len, 'float64', rand );
5366
return benchmark;
5467

55-
function clbk() {
56-
if ( bernoulli( 0.7 ) > 0 ) {
57-
return uniform( -100.0, 100.0 );
58-
}
59-
return NaN;
60-
}
61-
6268
function benchmark( b ) {
6369
var v;
6470
var i;

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

+14-8
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,19 @@ var dnansumpw = require( './../lib/ndarray.js' );
3232

3333
// FUNCTIONS //
3434

35+
/**
36+
* Returns a random number.
37+
*
38+
* @private
39+
* @returns {number} random number
40+
*/
41+
function rand() {
42+
if ( bernoulli( 0.8 ) > 0 ) {
43+
return uniform( -10.0, 10.0 );
44+
}
45+
return NaN;
46+
}
47+
3548
/**
3649
* Creates a benchmark function.
3750
*
@@ -40,16 +53,9 @@ var dnansumpw = require( './../lib/ndarray.js' );
4053
* @returns {Function} benchmark function
4154
*/
4255
function createBenchmark( len ) {
43-
var x = filledarrayBy( len, 'float64', clbk );
56+
var x = filledarrayBy( len, 'float64', rand );
4457
return benchmark;
4558

46-
function clbk() {
47-
if ( bernoulli( 0.7 ) > 0 ) {
48-
return uniform( -100.0, 100.0 );
49-
}
50-
return NaN;
51-
}
52-
5359
function benchmark( b ) {
5460
var v;
5561
var i;

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

+14-8
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,19 @@ var opts = {
4141

4242
// FUNCTIONS //
4343

44+
/**
45+
* Returns a random number.
46+
*
47+
* @private
48+
* @returns {number} random number
49+
*/
50+
function rand() {
51+
if ( bernoulli( 0.8 ) > 0 ) {
52+
return uniform( -10.0, 10.0 );
53+
}
54+
return NaN;
55+
}
56+
4457
/**
4558
* Creates a benchmark function.
4659
*
@@ -49,16 +62,9 @@ var opts = {
4962
* @returns {Function} benchmark function
5063
*/
5164
function createBenchmark( len ) {
52-
var x = filledarrayBy( len, 'float64', clbk );
65+
var x = filledarrayBy( len, 'float64', rand );
5366
return benchmark;
5467

55-
function clbk() {
56-
if ( bernoulli( 0.7 ) > 0 ) {
57-
return uniform( -100.0, 100.0 );
58-
}
59-
return NaN;
60-
}
61-
6268
function benchmark( b ) {
6369
var v;
6470
var i;

0 commit comments

Comments
 (0)