Skip to content

Commit a3391a5

Browse files
Pranavchikukgryte
andauthored
Refactor @stdlib/blas/base/scopy (#797)
* update readme * refactor src/ * update include.gypi * update manifest and package.json * refactor lib/ * update examples/ * fix include file in addon.c * refactor benchmarks * refactor tests * update docs * Fix path Co-authored-by: Athan <kgryte@gmail.com>
1 parent 1270e98 commit a3391a5

25 files changed

+241
-360
lines changed

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

Lines changed: 11 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
33
@license Apache-2.0
44
5-
Copyright (c) 2018 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.
@@ -52,18 +52,15 @@ The function has the following parameters:
5252
- **y**: destination [`Float32Array`][mdn-float32array].
5353
- **strideY**: index increment for `y`.
5454

55-
The `N` and `stride` parameters determine how values from `x` are copied into `y`. For example, to copy in reverse order every other value in `x` into the first `N` elements of `y`,
55+
The `N` and stride parameters determine how values from `x` are copied into `y`. For example, to copy in reverse order every other value in `x` into the first `N` elements of `y`,
5656

5757
```javascript
5858
var Float32Array = require( '@stdlib/array/float32' );
59-
var floor = require( '@stdlib/math/base/special/floor' );
6059

6160
var x = new Float32Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] );
6261
var y = new Float32Array( [ 7.0, 8.0, 9.0, 10.0, 11.0, 12.0 ] );
6362

64-
var N = floor( x.length / 2 );
65-
66-
scopy( N, x, -2, y, 1 );
63+
scopy( 3, x, -2, y, 1 );
6764
// y => <Float32Array>[ 5.0, 3.0, 1.0, 10.0, 11.0, 12.0 ]
6865
```
6966

@@ -73,7 +70,6 @@ Note that indexing is relative to the first index. To introduce an offset, use [
7370

7471
```javascript
7572
var Float32Array = require( '@stdlib/array/float32' );
76-
var floor = require( '@stdlib/math/base/special/floor' );
7773

7874
// Initial arrays...
7975
var x0 = new Float32Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] );
@@ -83,10 +79,8 @@ var y0 = new Float32Array( [ 7.0, 8.0, 9.0, 10.0, 11.0, 12.0 ] );
8379
var x1 = new Float32Array( x0.buffer, x0.BYTES_PER_ELEMENT*1 ); // start at 2nd element
8480
var y1 = new Float32Array( y0.buffer, y0.BYTES_PER_ELEMENT*3 ); // start at 4th element
8581

86-
var N = floor( x0.length / 2 );
87-
8882
// Copy in reverse order every other value from `x1` into `y1`...
89-
scopy( N, x1, -2, y1, 1 );
83+
scopy( 3, x1, -2, y1, 1 );
9084
// y0 => <Float32Array>[ 7.0, 8.0, 9.0, 6.0, 4.0, 2.0 ]
9185
```
9286

@@ -109,18 +103,15 @@ The function has the following additional parameters:
109103
- **offsetX**: starting index for `x`.
110104
- **offsetY**: starting index for `y`.
111105

112-
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 copy every other value in `x` starting from the second value into the last `N` elements in `y` where `x[i] = y[n]`, `x[i+2] = y[n-1]`,...,
106+
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 copy every other value in `x` starting from the second value into the last `N` elements in `y` where `x[i] = y[n]`, `x[i+2] = y[n-1]`,...,
113107

114108
```javascript
115109
var Float32Array = require( '@stdlib/array/float32' );
116-
var floor = require( '@stdlib/math/base/special/floor' );
117110

118111
var x = new Float32Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] );
119112
var y = new Float32Array( [ 7.0, 8.0, 9.0, 10.0, 11.0, 12.0 ] );
120113

121-
var N = floor( x.length / 2 );
122-
123-
scopy.ndarray( N, x, 2, 1, y, -1, y.length-1 );
114+
scopy.ndarray( 3, x, 2, 1, y, -1, y.length-1 );
124115
// y => <Float32Array>[ 7.0, 8.0, 9.0, 6.0, 4.0, 2.0 ]
125116
```
126117

@@ -146,22 +137,14 @@ scopy.ndarray( N, x, 2, 1, y, -1, y.length-1 );
146137
<!-- eslint no-undef: "error" -->
147138

148139
```javascript
149-
var randu = require( '@stdlib/random/base/randu' );
150-
var round = require( '@stdlib/math/base/special/round' );
151-
var Float32Array = require( '@stdlib/array/float32' );
140+
var discreteUniform = require( '@stdlib/random/base/discrete-uniform' ).factory;
141+
var filledarrayBy = require( '@stdlib/array/filled-by' );
152142
var scopy = require( '@stdlib/blas/base/scopy' );
153143

154-
var x;
155-
var y;
156-
var i;
157-
158-
x = new Float32Array( 10 );
159-
y = new Float32Array( 10 );
160-
for ( i = 0; i < x.length; i++ ) {
161-
x[ i ] = round( randu()*500.0 );
162-
y[ i ] = -1.0;
163-
}
144+
var x = filledarrayBy( 10, 'float32', discreteUniform( 0, 500 ) );
164145
console.log( x );
146+
147+
var y = filledarrayBy( x.length, 'float32', discreteUniform( 0, 255 ) );
165148
console.log( y );
166149

167150
// Copy elements from `x` into `y` starting from the end of `y`:

lib/node_modules/@stdlib/blas/base/scopy/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) 2018 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 scopy = require( './../lib/scopy.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 scopy = require( './../lib/scopy.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/scopy/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) 2018 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 scopy = tryRequire( resolve( __dirname, './../lib/scopy.native.js' ) );
3636
var opts = {
3737
'skip': ( scopy 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/scopy/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) 2018 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 scopy = 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 scopy = 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/scopy/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) 2018 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 scopy = tryRequire( resolve( __dirname, './../lib/ndarray.native.js' ) );
3636
var opts = {
3737
'skip': ( scopy instanceof Error )
3838
};
39+
var randu = 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', randu );
53+
var y = filledarrayBy( len, 'float32', randu );
6054
return benchmark;
6155

6256
/**

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

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
{{alias}}( N, x, strideX, y, strideY )
33
Copies values from `x` into `y`.
44

5-
The `N` and `stride` parameters determine how values from `x` are copied
5+
The `N` and stride parameters determine how values from `x` are copied
66
into `y`.
77

88
Indexing is relative to the first index. To introduce an offset, use typed
@@ -22,15 +22,15 @@
2222
Index increment for `x`.
2323

2424
y: Float32Array
25-
Destination array.
25+
Output array.
2626

2727
strideY: integer
2828
Index increment for `y`.
2929

3030
Returns
3131
-------
3232
y: Float32Array
33-
Input array `y`.
33+
Output 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 ]
@@ -81,7 +79,7 @@
8179
Starting index for `x`.
8280

8381
y: Float32Array
84-
Destination array.
82+
Output array.
8583

8684
strideY: integer
8785
Index increment for `y`.
@@ -92,7 +90,7 @@
9290
Returns
9391
-------
9492
y: Float32Array
95-
Input array `y`.
93+
Output array.
9694

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

112109
See Also

0 commit comments

Comments
 (0)