Skip to content

Commit 036fccd

Browse files
authored
feat: add C ndarray API and refactor blas/ext/base/sapxsum
PR-URL: #4812 Reviewed-by: Athan Reines <kgryte@gmail.com>
1 parent a1edb83 commit 036fccd

24 files changed

+375
-173
lines changed

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

Lines changed: 138 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ limitations under the License.
2020

2121
# sapxsum
2222

23-
> Add a constant to each single-precision floating-point strided array element and compute the sum.
23+
> Add a scalar constant to each single-precision floating-point strided array element and compute the sum.
2424
2525
<section class="intro">
2626

@@ -36,27 +36,27 @@ limitations under the License.
3636
var sapxsum = require( '@stdlib/blas/ext/base/sapxsum' );
3737
```
3838

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

41-
Adds a constant to each single-precision floating-point strided array element and computes the sum.
41+
Adds a scalar constant to each single-precision floating-point strided array element and computes the sum.
4242

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

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

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

5352
The function has the following parameters:
5453

5554
- **N**: number of indexed elements.
55+
- **alpha**: scalar constant.
5656
- **x**: input [`Float32Array`][@stdlib/array/float32].
57-
- **stride**: index increment for `x`.
57+
- **strideX**: stride length.
5858

59-
The `N` and stride parameters determine which elements in the strided array are accessed at runtime. For example, to access every other element in `x`,
59+
The `N` and stride parameters determine which elements in the strided array are accessed at runtime. For example, to access every other element:
6060

6161
```javascript
6262
var Float32Array = require( '@stdlib/array/float32' );
@@ -81,25 +81,24 @@ var v = sapxsum( 4, 5.0, x1, 2 );
8181
// returns 25.0
8282
```
8383

84-
#### sapxsum.ndarray( N, alpha, x, stride, offset )
84+
#### sapxsum.ndarray( N, alpha, x, strideX, offsetX )
8585

86-
Adds a constant to each single-precision floating-point strided array element and computes the sum using alternative indexing semantics.
86+
Adds a scalar constant to each single-precision floating-point strided array element and computes the sum using alternative indexing semantics.
8787

8888
```javascript
8989
var Float32Array = require( '@stdlib/array/float32' );
9090

9191
var x = new Float32Array( [ 1.0, -2.0, 2.0 ] );
92-
var N = x.length;
9392

94-
var v = sapxsum.ndarray( N, 5.0, x, 1, 0 );
93+
var v = sapxsum.ndarray( x.length, 5.0, x, 1, 0 );
9594
// returns 16.0
9695
```
9796

9897
The function has the following additional parameters:
9998

100-
- **offset**: starting index for `x`.
99+
- **offsetX**: starting index.
101100

102-
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 access every other value in `x` starting from the second value
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 access every other element starting from the second element:
103102

104103
```javascript
105104
var Float32Array = require( '@stdlib/array/float32' );
@@ -131,18 +130,141 @@ var v = sapxsum.ndarray( 4, 5.0, x, 2, 1 );
131130
<!-- eslint no-undef: "error" -->
132131

133132
```javascript
134-
var discreteUniform = require( '@stdlib/random/base/discrete-uniform' ).factory;
135-
var filledarrayBy = require( '@stdlib/array/filled-by' );
133+
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
136134
var sapxsum = require( '@stdlib/blas/ext/base/sapxsum' );
137135

138-
var x = filledarrayBy( 10, 'float32', discreteUniform( 0, 100 ) );
136+
var x = discreteUniform( 10, -100, 100, {
137+
'dtype': 'float32'
138+
});
139139
console.log( x );
140+
141+
var v = sapxsum( x.length, 5.0, x, 1 );
142+
console.log( v );
143+
```
144+
145+
</section>
146+
147+
<!-- /.examples -->
148+
149+
<!-- C interface documentation. -->
150+
151+
* * *
152+
153+
<section class="c">
154+
155+
## C APIs
156+
157+
<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->
158+
159+
<section class="intro">
160+
161+
</section>
162+
163+
<!-- /.intro -->
164+
165+
<!-- C usage documentation. -->
166+
167+
<section class="usage">
168+
169+
### Usage
170+
171+
```c
172+
#include "stdlib/blas/ext/base/sapxsum.h"
173+
```
174+
175+
#### stdlib_strided_sapxsum( N, alpha, \*X, strideX )
176+
177+
Adds a scalar constant to each single-precision floating-point strided array element and computes the sum.
178+
179+
```c
180+
const float x[] = { 1.0f, 2.0f, 3.0f, 4.0f };
181+
182+
float v = stdlib_strided_sapxsum( 4, 5.0f, x, 1 );
183+
// returns 30.0f
184+
```
185+
186+
The function accepts the following arguments:
187+
188+
- **N**: `[in] CBLAS_INT` number of indexed elements.
189+
- **alpha**: `[in] float` scalar constant.
190+
- **X**: `[in] float*` input array.
191+
- **strideX**: `[in] CBLAS_INT` stride length.
192+
193+
```c
194+
float stdlib_strided_sapxsum( const CBLAS_INT N, const float alpha, const float *X, const CBLAS_INT strideX );
195+
```
196+
197+
#### stdlib_strided_sapxsum_ndarray( N, alpha, \*X, strideX, offsetX )
198+
199+
Adds a scalar constant to each single-precision floating-point strided array element and computes the sum using alternative indexing semantics.
200+
201+
```c
202+
const float x[] = { 1.0f, 2.0f, 3.0f, 4.0f };
203+
204+
float v = stdlib_strided_sapxsum_ndarray( 4, 5.0f, x, 1, 0 );
205+
// returns 30.0f
206+
```
207+
208+
The function accepts the following arguments:
209+
210+
- **N**: `[in] CBLAS_INT` number of indexed elements.
211+
- **alpha**: `[in] float` scalar constant.
212+
- **X**: `[in] float*` input array.
213+
- **strideX**: `[in] CBLAS_INT` stride length.
214+
- **offsetX**: `[in] CBLAS_INT` starting index.
215+
216+
```c
217+
float stdlib_strided_sapxsum_ndarray( const CBLAS_INT N, const float alpha, const float *X, const CBLAS_INT strideX, const CBLAS_INT offsetX );
218+
```
219+
220+
</section>
221+
222+
<!-- /.usage -->
223+
224+
<!-- C API usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
225+
226+
<section class="notes">
227+
228+
</section>
229+
230+
<!-- /.notes -->
231+
232+
<!-- C API usage examples. -->
233+
234+
<section class="examples">
235+
236+
### Examples
237+
238+
```c
239+
#include "stdlib/blas/ext/base/sapxsum.h"
240+
#include <stdio.h>
241+
242+
int main( void ) {
243+
// Create a strided array:
244+
const float x[] = { 1.0f, -2.0f, 3.0f, -4.0f, 5.0f, -6.0f, 7.0f, -8.0f };
245+
246+
// Specify the number of indexed elements:
247+
const int N = 8;
248+
249+
// Specify a stride:
250+
const int strideX = 1;
251+
252+
// Compute the sum:
253+
float v = stdlib_strided_sapxsum( N, 5.0f, x, strideX );
254+
255+
// Print the result:
256+
printf( "Sum: %f\n", v );
257+
}
140258
```
141259
142260
</section>
143261
144262
<!-- /.examples -->
145263
264+
</section>
265+
266+
<!-- /.c -->
267+
146268
<section class="references">
147269
148270
</section>

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,16 @@
2323
var bench = require( '@stdlib/bench' );
2424
var isnan = require( '@stdlib/math/base/assert/is-nan' );
2525
var pow = require( '@stdlib/math/base/special/pow' );
26-
var uniform = require( '@stdlib/random/base/uniform' ).factory;
27-
var filledarrayBy = require( '@stdlib/array/filled-by' );
26+
var uniform = require( '@stdlib/random/array/uniform' );
2827
var pkg = require( './../package.json' ).name;
2928
var sapxsum = require( './../lib/sapxsum.js' );
3029

3130

3231
// VARIABLES //
3332

34-
var rand = uniform( -10.0, 10.0 );
33+
var options = {
34+
'dtype': 'float32'
35+
};
3536

3637

3738
// FUNCTIONS //
@@ -44,7 +45,7 @@ var rand = uniform( -10.0, 10.0 );
4445
* @returns {Function} benchmark function
4546
*/
4647
function createBenchmark( len ) {
47-
var x = filledarrayBy( len, 'float32', rand );
48+
var x = uniform( len, -100, 100, options );
4849
return benchmark;
4950

5051
function benchmark( b ) {

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,7 @@ var resolve = require( 'path' ).resolve;
2424
var bench = require( '@stdlib/bench' );
2525
var isnan = require( '@stdlib/math/base/assert/is-nan' );
2626
var pow = require( '@stdlib/math/base/special/pow' );
27-
var uniform = require( '@stdlib/random/base/uniform' ).factory;
28-
var filledarrayBy = require( '@stdlib/array/filled-by' );
27+
var uniform = require( '@stdlib/random/array/uniform' );
2928
var tryRequire = require( '@stdlib/utils/try-require' );
3029
var pkg = require( './../package.json' ).name;
3130

@@ -36,7 +35,9 @@ var sapxsum = tryRequire( resolve( __dirname, './../lib/sapxsum.native.js' ) );
3635
var opts = {
3736
'skip': ( sapxsum instanceof Error )
3837
};
39-
var rand = uniform( -10.0, 10.0 );
38+
var options = {
39+
'dtype': 'float32'
40+
};
4041

4142

4243
// FUNCTIONS //
@@ -49,7 +50,7 @@ var rand = uniform( -10.0, 10.0 );
4950
* @returns {Function} benchmark function
5051
*/
5152
function createBenchmark( len ) {
52-
var x = filledarrayBy( len, 'float32', rand );
53+
var x = uniform( len, -100, 100, options );
5354
return benchmark;
5455

5556
function benchmark( b ) {

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,16 @@
2323
var bench = require( '@stdlib/bench' );
2424
var isnan = require( '@stdlib/math/base/assert/is-nan' );
2525
var pow = require( '@stdlib/math/base/special/pow' );
26-
var uniform = require( '@stdlib/random/base/uniform' ).factory;
27-
var filledarrayBy = require( '@stdlib/array/filled-by' );
26+
var uniform = require( '@stdlib/random/array/uniform' );
2827
var pkg = require( './../package.json' ).name;
2928
var sapxsum = require( './../lib/ndarray.js' );
3029

3130

3231
// VARIABLES //
3332

34-
var rand = uniform( -10.0, 10.0 );
33+
var options = {
34+
'dtype': 'float32'
35+
};
3536

3637

3738
// FUNCTIONS //
@@ -44,7 +45,7 @@ var rand = uniform( -10.0, 10.0 );
4445
* @returns {Function} benchmark function
4546
*/
4647
function createBenchmark( len ) {
47-
var x = filledarrayBy( len, 'float32', rand );
48+
var x = uniform( len, -100, 100, options );
4849
return benchmark;
4950

5051
function benchmark( b ) {

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,7 @@ var resolve = require( 'path' ).resolve;
2424
var bench = require( '@stdlib/bench' );
2525
var isnan = require( '@stdlib/math/base/assert/is-nan' );
2626
var pow = require( '@stdlib/math/base/special/pow' );
27-
var uniform = require( '@stdlib/random/base/uniform' ).factory;
28-
var filledarrayBy = require( '@stdlib/array/filled-by' );
27+
var uniform = require( '@stdlib/random/array/uniform' );
2928
var tryRequire = require( '@stdlib/utils/try-require' );
3029
var pkg = require( './../package.json' ).name;
3130

@@ -36,7 +35,9 @@ var sapxsum = tryRequire( resolve( __dirname, './../lib/ndarray.native.js' ) );
3635
var opts = {
3736
'skip': ( sapxsum instanceof Error )
3837
};
39-
var rand = uniform( -10.0, 10.0 );
38+
var options = {
39+
'dtype': 'float32'
40+
};
4041

4142

4243
// FUNCTIONS //
@@ -49,7 +50,7 @@ var rand = uniform( -10.0, 10.0 );
4950
* @returns {Function} benchmark function
5051
*/
5152
function createBenchmark( len ) {
52-
var x = filledarrayBy( len, 'float32', rand );
53+
var x = uniform( len, -100, 100, options );
5354
return benchmark;
5455

5556
function benchmark( b ) {

0 commit comments

Comments
 (0)