Skip to content

Commit ae30838

Browse files
authored
Refactor @stdlib/blas/base/sswap (#794)
* refactor readme * refactor src/ * update readme * update manifest.json and package.json * refactor lib/ * refactor examples * refactor benchmarks * refactor tests * refactor docs/
1 parent 053ce44 commit ae30838

21 files changed

+241
-352
lines changed

lib/node_modules/@stdlib/blas/base/sswap/README.md

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
33
@license Apache-2.0
44
5-
Copyright (c) 2020 The Stdlib Authors.
5+
Copyright (c) 2023 The Stdlib Authors.
66
77
Licensed under the Apache License, Version 2.0 (the "License");
88
you may not use this file except in compliance with the License.
@@ -53,7 +53,7 @@ The function has the following parameters:
5353
- **y**: second input [`Float32Array`][mdn-float32array].
5454
- **strideY**: index increment for `y`.
5555

56-
The `N` and `stride` parameters determine how values from `x` and `y` are accessed at runtime. For example, to swap in reverse order every other value in `x` with the first `N` elements of `y`,
56+
The `N` and stride parameters determine how values from the strided arrays are accessed at runtime. For example, to swap in reverse order every other value in `x` with the first `N` elements of `y`,
5757

5858
```javascript
5959
var Float32Array = require( '@stdlib/array/float32' );
@@ -107,7 +107,7 @@ The function has the following additional parameters:
107107
- **offsetX**: starting index for `x`.
108108
- **offsetY**: starting index for `y`.
109109

110-
While [`typed array`][mdn-typed-array] views mandate a view offset based on the underlying `buffer`, the `offsetX` and `offsetY` parameters support indexing semantics based on starting indices. For example, to swap every other value in `x` starting from the second value with the last `N` elements in `y` where `x[i] = y[n]`, `x[i+2] = y[n-1]`,...,
110+
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 swap every other value in `x` starting from the second value with the last `N` elements in `y` where `x[i] = y[n]`, `x[i+2] = y[n-1]`,...,
111111

112112
```javascript
113113
var Float32Array = require( '@stdlib/array/float32' );
@@ -142,22 +142,14 @@ sswap.ndarray( 3, x, 2, 1, y, -1, y.length-1 );
142142
<!-- eslint no-undef: "error" -->
143143

144144
```javascript
145-
var randu = require( '@stdlib/random/base/randu' );
146-
var round = require( '@stdlib/math/base/special/round' );
147-
var Float32Array = require( '@stdlib/array/float32' );
145+
var discreteUniform = require( '@stdlib/random/base/discrete-uniform' ).factory;
146+
var filledarrayBy = require( '@stdlib/array/filled-by' );
148147
var sswap = require( '@stdlib/blas/base/sswap' );
149148

150-
var x;
151-
var y;
152-
var i;
153-
154-
x = new Float32Array( 10 );
155-
y = new Float32Array( 10 );
156-
for ( i = 0; i < x.length; i++ ) {
157-
x[ i ] = round( randu()*500.0 );
158-
y[ i ] = round( randu()*255.0 );
159-
}
149+
var x = filledarrayBy( 10, 'float32', discreteUniform( 0, 500 ) );
160150
console.log( x );
151+
152+
var y = filledarrayBy( x.length, 'float32', discreteUniform( 0, 255 ) );
161153
console.log( y );
162154

163155
// Swap elements in `x` and `y` starting from the end of `y`:

lib/node_modules/@stdlib/blas/base/sswap/benchmark/benchmark.js

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/**
22
* @license Apache-2.0
33
*
4-
* Copyright (c) 2020 The Stdlib Authors.
4+
* Copyright (c) 2023 The Stdlib Authors.
55
*
66
* Licensed under the Apache License, Version 2.0 (the "License");
77
* you may not use this file except in compliance with the License.
@@ -21,14 +21,19 @@
2121
// MODULES //
2222

2323
var bench = require( '@stdlib/bench' );
24-
var randu = require( '@stdlib/random/base/randu' );
24+
var uniform = require( '@stdlib/random/base/uniform' ).factory;
25+
var filledarrayBy = require( '@stdlib/array/filled-by' );
2526
var isnan = require( '@stdlib/math/base/assert/is-nan' );
2627
var pow = require( '@stdlib/math/base/special/pow' );
27-
var Float32Array = require( '@stdlib/array/float32' );
2828
var pkg = require( './../package.json' ).name;
2929
var sswap = require( './../lib/sswap.js' );
3030

3131

32+
// VARIABLES //
33+
34+
var rand = uniform( -10000.0, 10000.0 );
35+
36+
3237
// FUNCTIONS //
3338

3439
/**
@@ -39,15 +44,8 @@ var sswap = require( './../lib/sswap.js' );
3944
* @returns {Function} benchmark function
4045
*/
4146
function createBenchmark( len ) {
42-
var x;
43-
var y;
44-
var i;
45-
46-
x = new Float32Array( len );
47-
y = new Float32Array( len );
48-
for ( i = 0; i < x.length; i++ ) {
49-
x[ i ] = ( randu()*20000.0 ) - 10000.0;
50-
}
47+
var x = filledarrayBy( len, 'float32', rand );
48+
var y = filledarrayBy( len, 'float32', rand );
5149
return benchmark;
5250

5351
/**

lib/node_modules/@stdlib/blas/base/sswap/benchmark/benchmark.native.js

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/**
22
* @license Apache-2.0
33
*
4-
* Copyright (c) 2020 The Stdlib Authors.
4+
* Copyright (c) 2023 The Stdlib Authors.
55
*
66
* Licensed under the Apache License, Version 2.0 (the "License");
77
* you may not use this file except in compliance with the License.
@@ -22,10 +22,10 @@
2222

2323
var resolve = require( 'path' ).resolve;
2424
var bench = require( '@stdlib/bench' );
25-
var randu = require( '@stdlib/random/base/randu' );
25+
var uniform = require( '@stdlib/random/base/uniform' ).factory;
26+
var filledarrayBy = require( '@stdlib/array/filled-by' );
2627
var isnan = require( '@stdlib/math/base/assert/is-nan' );
2728
var pow = require( '@stdlib/math/base/special/pow' );
28-
var Float32Array = require( '@stdlib/array/float32' );
2929
var tryRequire = require( '@stdlib/utils/try-require' );
3030
var pkg = require( './../package.json' ).name;
3131

@@ -36,6 +36,7 @@ var sswap = tryRequire( resolve( __dirname, './../lib/sswap.native.js' ) );
3636
var opts = {
3737
'skip': ( sswap instanceof Error )
3838
};
39+
var rand = uniform( -10000.0, 10000.0 );
3940

4041

4142
// FUNCTIONS //
@@ -48,15 +49,8 @@ var opts = {
4849
* @returns {Function} benchmark function
4950
*/
5051
function createBenchmark( len ) {
51-
var x;
52-
var y;
53-
var i;
54-
55-
x = new Float32Array( len );
56-
y = new Float32Array( len );
57-
for ( i = 0; i < x.length; i++ ) {
58-
x[ i ] = ( randu()*20000.0 ) - 10000.0;
59-
}
52+
var x = filledarrayBy( len, 'float32', rand );
53+
var y = filledarrayBy( len, 'float32', rand );
6054
return benchmark;
6155

6256
/**

lib/node_modules/@stdlib/blas/base/sswap/benchmark/benchmark.ndarray.js

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/**
22
* @license Apache-2.0
33
*
4-
* Copyright (c) 2020 The Stdlib Authors.
4+
* Copyright (c) 2023 The Stdlib Authors.
55
*
66
* Licensed under the Apache License, Version 2.0 (the "License");
77
* you may not use this file except in compliance with the License.
@@ -21,14 +21,19 @@
2121
// MODULES //
2222

2323
var bench = require( '@stdlib/bench' );
24-
var randu = require( '@stdlib/random/base/randu' );
24+
var uniform = require( '@stdlib/random/base/uniform' ).factory;
25+
var filledarrayBy = require( '@stdlib/array/filled-by' );
2526
var isnan = require( '@stdlib/math/base/assert/is-nan' );
2627
var pow = require( '@stdlib/math/base/special/pow' );
27-
var Float32Array = require( '@stdlib/array/float32' );
2828
var pkg = require( './../package.json' ).name;
2929
var sswap = require( './../lib/ndarray.js' );
3030

3131

32+
// VARIABLES //
33+
34+
var rand = uniform( -10000.0, 10000.0 );
35+
36+
3237
// FUNCTIONS //
3338

3439
/**
@@ -39,15 +44,8 @@ var sswap = require( './../lib/ndarray.js' );
3944
* @returns {Function} benchmark function
4045
*/
4146
function createBenchmark( len ) {
42-
var x;
43-
var y;
44-
var i;
45-
46-
x = new Float32Array( len );
47-
y = new Float32Array( len );
48-
for ( i = 0; i < x.length; i++ ) {
49-
x[ i ] = ( randu()*20000.0 ) - 10000.0;
50-
}
47+
var x = filledarrayBy( len, 'float32', rand );
48+
var y = filledarrayBy( len, 'float32', rand );
5149
return benchmark;
5250

5351
/**

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

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/**
22
* @license Apache-2.0
33
*
4-
* Copyright (c) 2020 The Stdlib Authors.
4+
* Copyright (c) 2023 The Stdlib Authors.
55
*
66
* Licensed under the Apache License, Version 2.0 (the "License");
77
* you may not use this file except in compliance with the License.
@@ -22,10 +22,10 @@
2222

2323
var resolve = require( 'path' ).resolve;
2424
var bench = require( '@stdlib/bench' );
25-
var randu = require( '@stdlib/random/base/randu' );
25+
var uniform = require( '@stdlib/random/base/uniform' ).factory;
26+
var filledarrayBy = require( '@stdlib/array/filled-by' );
2627
var isnan = require( '@stdlib/math/base/assert/is-nan' );
2728
var pow = require( '@stdlib/math/base/special/pow' );
28-
var Float32Array = require( '@stdlib/array/float32' );
2929
var tryRequire = require( '@stdlib/utils/try-require' );
3030
var pkg = require( './../package.json' ).name;
3131

@@ -36,6 +36,7 @@ var sswap = tryRequire( resolve( __dirname, './../lib/ndarray.native.js' ) );
3636
var opts = {
3737
'skip': ( sswap instanceof Error )
3838
};
39+
var rand = uniform( -10000.0, 10000.0 );
3940

4041

4142
// FUNCTIONS //
@@ -48,15 +49,8 @@ var opts = {
4849
* @returns {Function} benchmark function
4950
*/
5051
function createBenchmark( len ) {
51-
var x;
52-
var y;
53-
var i;
54-
55-
x = new Float32Array( len );
56-
y = new Float32Array( len );
57-
for ( i = 0; i < x.length; i++ ) {
58-
x[ i ] = ( randu()*20000.0 ) - 10000.0;
59-
}
52+
var x = filledarrayBy( len, 'float32', rand );
53+
var y = filledarrayBy( len, 'float32', rand );
6054
return benchmark;
6155

6256
/**

lib/node_modules/@stdlib/blas/base/sswap/docs/repl.txt

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
Parameters
1414
----------
1515
N: integer
16-
Number of values to swap.
16+
Number of values.
1717

1818
x: Float32Array
1919
First input array.
@@ -30,7 +30,7 @@
3030
Returns
3131
-------
3232
y: Float32Array
33-
Input array `y`.
33+
Second input array.
3434

3535
Examples
3636
--------
@@ -43,17 +43,15 @@
4343
// Advanced indexing:
4444
> x = new {{alias:@stdlib/array/float32}}( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] );
4545
> y = new {{alias:@stdlib/array/float32}}( [ 7.0, 8.0, 9.0, 10.0, 11.0, 12.0 ] );
46-
> var N = {{alias:@stdlib/math/base/special/floor}}( x.length / 2 );
47-
> {{alias}}( N, x, -2, y, 1 )
46+
> {{alias}}( 3, x, -2, y, 1 )
4847
<Float32Array>[ 5.0, 3.0, 1.0, 10.0, 11.0, 12.0 ]
4948

5049
// Using typed array views:
5150
> var x0 = new {{alias:@stdlib/array/float32}}( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] );
5251
> var y0 = new {{alias:@stdlib/array/float32}}( [ 7.0, 8.0, 9.0, 10.0, 11.0, 12.0 ] );
5352
> var x1 = new {{alias:@stdlib/array/float32}}( x0.buffer, x0.BYTES_PER_ELEMENT*1 );
5453
> var y1 = new {{alias:@stdlib/array/float32}}( y0.buffer, y0.BYTES_PER_ELEMENT*3 );
55-
> N = {{alias:@stdlib/math/base/special/floor}}( x0.length / 2 );
56-
> {{alias}}( N, x1, -2, y1, 1 )
54+
> {{alias}}( 3, x1, -2, y1, 1 )
5755
<Float32Array>[ 6.0, 4.0, 2.0 ]
5856
> y0
5957
<Float32Array>[ 7.0, 8.0, 9.0, 6.0, 4.0, 2.0 ]
@@ -64,13 +62,13 @@
6462
indexing semantics.
6563

6664
While typed array views mandate a view offset based on the underlying
67-
buffer, the `offset` parameters support indexing semantics based on starting
65+
buffer, the offset parameters support indexing semantics based on starting
6866
indices.
6967

7068
Parameters
7169
----------
7270
N: integer
73-
Number of values to swap.
71+
Number of values.
7472

7573
x: Float32Array
7674
First input array.
@@ -93,7 +91,7 @@
9391
Returns
9492
-------
9593
y: Float32Array
96-
Input array `y`.
94+
Second input array.
9795

9896
Examples
9997
--------
@@ -106,8 +104,7 @@
106104
// Advanced indexing:
107105
> x = new {{alias:@stdlib/array/float32}}( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] );
108106
> y = new {{alias:@stdlib/array/float32}}( [ 7.0, 8.0, 9.0, 10.0, 11.0, 12.0 ] );
109-
> var N = {{alias:@stdlib/math/base/special/floor}}( x.length / 2 );
110-
> {{alias}}.ndarray( N, x, 2, 1, y, -1, y.length-1 )
107+
> {{alias}}.ndarray( 3, x, 2, 1, y, -1, y.length-1 )
111108
<Float32Array>[ 7.0, 8.0, 9.0, 6.0, 4.0, 2.0 ]
112109

113110
See Also

lib/node_modules/@stdlib/blas/base/sswap/docs/types/index.d.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
* @license Apache-2.0
33
*
4-
* Copyright (c) 2020 The Stdlib Authors.
4+
* Copyright (c) 2023 The Stdlib Authors.
55
*
66
* Licensed under the Apache License, Version 2.0 (the "License");
77
* you may not use this file except in compliance with the License.
@@ -25,7 +25,7 @@ interface Routine {
2525
/**
2626
* Interchanges two single-precision floating-point vectors.
2727
*
28-
* @param N - number of values to swap
28+
* @param N - number of values
2929
* @param x - first input array
3030
* @param strideX - `x` stride length
3131
* @param y - second input array
@@ -47,7 +47,7 @@ interface Routine {
4747
/**
4848
* Interchanges two single-precision floating-point vectors using alternative indexing semantics.
4949
*
50-
* @param N - number of values to swap
50+
* @param N - number of values
5151
* @param x - first input array
5252
* @param strideX - `x` stride length
5353
* @param offsetX - starting index for `x`

lib/node_modules/@stdlib/blas/base/sswap/examples/index.js

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/**
22
* @license Apache-2.0
33
*
4-
* Copyright (c) 2020 The Stdlib Authors.
4+
* Copyright (c) 2023 The Stdlib Authors.
55
*
66
* Licensed under the Apache License, Version 2.0 (the "License");
77
* you may not use this file except in compliance with the License.
@@ -18,22 +18,14 @@
1818

1919
'use strict';
2020

21-
var randu = require( '@stdlib/random/base/randu' );
22-
var round = require( '@stdlib/math/base/special/round' );
23-
var Float32Array = require( '@stdlib/array/float32' );
21+
var discreteUniform = require( '@stdlib/random/base/discrete-uniform' ).factory;
22+
var filledarrayBy = require( '@stdlib/array/filled-by' );
2423
var sswap = require( './../lib' );
2524

26-
var x;
27-
var y;
28-
var i;
29-
30-
x = new Float32Array( 10 );
31-
y = new Float32Array( 10 );
32-
for ( i = 0; i < x.length; i++ ) {
33-
x[ i ] = round( randu()*500.0 );
34-
y[ i ] = round( randu()*255.0 );
35-
}
25+
var x = filledarrayBy( 10, 'float32', discreteUniform( 0, 500 ) );
3626
console.log( x );
27+
28+
var y = filledarrayBy( x.length, 'float32', discreteUniform( 0, 255 ) );
3729
console.log( y );
3830

3931
// Swap elements in `x` and `y` starting from the end of `y`:

0 commit comments

Comments
 (0)