Skip to content

Commit 710ea41

Browse files
Pranavchikukgryte
andauthored
refactor: update add-on, benchmarks, tests, and examples
This commit migrates `@stdlib/blas/base/snrm2` to use current project conventions for authoring add-ons, as discussed in #788. PR-URL: #800 Co-authored-by: Athan Reines <kgryte@gmail.com> Reviewed-by: Athan Reines <kgryte@gmail.com>
1 parent 2468ebc commit 710ea41

26 files changed

+182
-261
lines changed

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

Lines changed: 9 additions & 23 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.
@@ -67,16 +67,14 @@ The function has the following parameters:
6767
- **x**: input [`Float32Array`][@stdlib/array/float32].
6868
- **stride**: index increment for `x`.
6969

70-
The `N` and `stride` parameters determine which elements in `x` are accessed at runtime. For example, to compute the [L2-norm][l2-norm] of every other element in `x`,
70+
The `N` and stride parameters determine which elements in the strided array are accessed at runtime. For example, to compute the [L2-norm][l2-norm] of every other element in `x`,
7171

7272
```javascript
7373
var Float32Array = require( '@stdlib/array/float32' );
74-
var floor = require( '@stdlib/math/base/special/floor' );
7574

7675
var x = new Float32Array( [ 1.0, 2.0, 2.0, -7.0, -2.0, 3.0, 4.0, 2.0 ] );
77-
var N = floor( x.length / 2 );
7876

79-
var z = snrm2( N, x, 2 );
77+
var z = snrm2( 4, x, 2 );
8078
// returns 5.0
8179
```
8280

@@ -86,14 +84,11 @@ Note that indexing is relative to the first index. To introduce an offset, use [
8684

8785
```javascript
8886
var Float32Array = require( '@stdlib/array/float32' );
89-
var floor = require( '@stdlib/math/base/special/floor' );
9087

9188
var x0 = new Float32Array( [ 2.0, 1.0, 2.0, -2.0, -2.0, 2.0, 3.0, 4.0 ] );
9289
var x1 = new Float32Array( x0.buffer, x0.BYTES_PER_ELEMENT*1 ); // start at 2nd element
9390

94-
var N = floor( x0.length / 2 );
95-
96-
var z = snrm2( N, x1, 2 );
91+
var z = snrm2( 4, x1, 2 );
9792
// returns 5.0
9893
```
9994

@@ -117,16 +112,14 @@ The function has the following additional parameters:
117112

118113
- **offset**: starting index for `x`.
119114

120-
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 [L2-norm][l2-norm] for every other value in `x` starting from the second value
115+
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 [L2-norm][l2-norm] for every other value in `x` starting from the second value
121116

122117
```javascript
123118
var Float32Array = require( '@stdlib/array/float32' );
124-
var floor = require( '@stdlib/math/base/special/floor' );
125119

126120
var x = new Float32Array( [ 2.0, 1.0, 2.0, -2.0, -2.0, 2.0, 3.0, 4.0 ] );
127-
var N = floor( x.length / 2 );
128121

129-
var z = snrm2.ndarray( N, x, 2, 1 );
122+
var z = snrm2.ndarray( 4, x, 2, 1 );
130123
// returns 5.0
131124
```
132125

@@ -152,18 +145,11 @@ var z = snrm2.ndarray( N, x, 2, 1 );
152145
<!-- eslint no-undef: "error" -->
153146

154147
```javascript
155-
var randu = require( '@stdlib/random/base/randu' );
156-
var round = require( '@stdlib/math/base/special/round' );
157-
var Float32Array = require( '@stdlib/array/float32' );
148+
var discreteUniform = require( '@stdlib/random/base/discrete-uniform' ).factory;
149+
var filledarrayBy = require( '@stdlib/array/filled-by' );
158150
var snrm2 = require( '@stdlib/blas/base/snrm2' );
159151

160-
var x;
161-
var i;
162-
163-
x = new Float32Array( 10 );
164-
for ( i = 0; i < x.length; i++ ) {
165-
x[ i ] = round( randu()*100.0 );
166-
}
152+
var x = filledarrayBy( 10, 'float32', discreteUniform( 0, 100 ) );
167153
console.log( x );
168154

169155
var z = snrm2( x.length, x, 1 );

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

Lines changed: 9 additions & 10 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 snrm2 = require( './../lib/snrm2.js' );
3030

3131

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

3439
/**
@@ -39,13 +44,7 @@ var snrm2 = require( './../lib/snrm2.js' );
3944
* @returns {Function} benchmark function
4045
*/
4146
function createBenchmark( len ) {
42-
var x;
43-
var i;
44-
45-
x = new Float32Array( len );
46-
for ( i = 0; i < x.length; i++ ) {
47-
x[ i ] = ( randu()*20.0 ) - 10.0;
48-
}
47+
var x = filledarrayBy( len, 'float32', rand );
4948
return benchmark;
5049

5150
function benchmark( b ) {

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

Lines changed: 5 additions & 10 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 snrm2 = tryRequire( resolve( __dirname, './../lib/snrm2.native.js' ) );
3636
var opts = {
3737
'skip': ( snrm2 instanceof Error )
3838
};
39+
var rand = uniform( -10.0, 10.0 );
3940

4041

4142
// FUNCTIONS //
@@ -48,13 +49,7 @@ var opts = {
4849
* @returns {Function} benchmark function
4950
*/
5051
function createBenchmark( len ) {
51-
var x;
52-
var i;
53-
54-
x = new Float32Array( len );
55-
for ( i = 0; i < x.length; i++ ) {
56-
x[ i ] = ( randu()*20.0 ) - 10.0;
57-
}
52+
var x = filledarrayBy( len, 'float32', rand );
5853
return benchmark;
5954

6055
function benchmark( b ) {

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

Lines changed: 9 additions & 10 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 snrm2 = require( './../lib/ndarray.js' );
3030

3131

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

3439
/**
@@ -39,13 +44,7 @@ var snrm2 = require( './../lib/ndarray.js' );
3944
* @returns {Function} benchmark function
4045
*/
4146
function createBenchmark( len ) {
42-
var x;
43-
var i;
44-
45-
x = new Float32Array( len );
46-
for ( i = 0; i < x.length; i++ ) {
47-
x[ i ] = ( randu()*20.0 ) - 10.0;
48-
}
47+
var x = filledarrayBy( len, 'float32', rand );
4948
return benchmark;
5049

5150
function benchmark( b ) {

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

Lines changed: 5 additions & 10 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 snrm2 = tryRequire( resolve( __dirname, './../lib/ndarray.native.js' ) );
3636
var opts = {
3737
'skip': ( snrm2 instanceof Error )
3838
};
39+
var rand = uniform( -10.0, 10.0 );
3940

4041

4142
// FUNCTIONS //
@@ -48,13 +49,7 @@ var opts = {
4849
* @returns {Function} benchmark function
4950
*/
5051
function createBenchmark( len ) {
51-
var x;
52-
var i;
53-
54-
x = new Float32Array( len );
55-
for ( i = 0; i < x.length; i++ ) {
56-
x[ i ] = ( randu()*20.0 ) - 10.0;
57-
}
52+
var x = filledarrayBy( len, 'float32', rand );
5853
return benchmark;
5954

6055
function benchmark( b ) {

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

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
{{alias}}( N, x, stride )
33
Computes the L2-norm of a single-precision floating-point vector.
44

5-
The `N` and `stride` parameters determine which elements in `x` are accessed
6-
at runtime.
5+
The `N` and stride parameters determine which elements in the strided arrays
6+
are accessed at runtime.
77

88
Indexing is relative to the first index. To introduce an offset, use a typed
99
array view.
@@ -35,19 +35,18 @@
3535

3636
// Using `N` and `stride` parameters:
3737
> x = new {{alias:@stdlib/array/float32}}( [ -2.0, 1.0, 1.0, -5.0, 2.0, -1.0 ] );
38-
> var N = {{alias:@stdlib/math/base/special/floor}}( x.length / 2 );
3938
> var stride = 2;
40-
> {{alias}}( N, x, stride )
39+
> {{alias}}( 3, x, stride )
4140
3.0
4241

4342
// Using view offsets:
4443
> var x0 = new {{alias:@stdlib/array/float32}}( [ 1.0, -2.0, 3.0, 2.0, 5.0, -1.0 ] );
4544
> var x1 = new {{alias:@stdlib/array/float32}}( x0.buffer, x0.BYTES_PER_ELEMENT*1 );
46-
> N = {{alias:@stdlib/math/base/special/floor}}( x0.length / 2 );
4745
> stride = 2;
48-
> {{alias}}( N, x1, stride )
46+
> {{alias}}( 3, x1, stride )
4947
3.0
5048

49+
5150
{{alias}}.ndarray( N, x, stride, offset )
5251
Computes the L2-norm of a single-precision floating-point vector using
5352
alternative indexing semantics.
@@ -84,8 +83,7 @@
8483

8584
// Using offset parameter:
8685
> var x = new {{alias:@stdlib/array/float32}}( [ 1.0, -2.0, 3.0, 2.0, 5.0, -1.0 ] );
87-
> var N = {{alias:@stdlib/math/base/special/floor}}( x.length / 2 );
88-
> {{alias}}.ndarray( N, x, 2, 1 )
86+
> {{alias}}.ndarray( 3, x, 2, 1 )
8987
3.0
9088

9189
See Also

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

Lines changed: 4 additions & 4 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
* Computes the L2-norm of a single-precision floating-point vector.
2727
*
28-
* @param N - number of values over which to compute the L2-norm
28+
* @param N - number of values
2929
* @param x - input array
3030
* @param stride - stride length
3131
* @returns L2-norm
@@ -43,7 +43,7 @@ interface Routine {
4343
/**
4444
* Computes the L2-norm of a single-precision floating-point vector using alternative indexing semantics.
4545
*
46-
* @param N - number of values over which to compute the L2-norm
46+
* @param N - number of values
4747
* @param x - input array
4848
* @param stride - stride length
4949
* @param offset - starting index
@@ -63,7 +63,7 @@ interface Routine {
6363
/**
6464
* Computes the L2-norm of a single-precision floating-point vector.
6565
*
66-
* @param N - number of values over which to compute the L2-norm
66+
* @param N - number of values
6767
* @param x - input array
6868
* @param stride - stride length
6969
* @returns L2-norm

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

Lines changed: 4 additions & 11 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,18 +18,11 @@
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 snrm2 = require( './../lib' );
2524

26-
var x;
27-
var i;
28-
29-
x = new Float32Array( 10 );
30-
for ( i = 0; i < x.length; i++ ) {
31-
x[ i ] = round( randu()*10.0 );
32-
}
25+
var x = filledarrayBy( 10, 'float32', discreteUniform( 0, 100 ) );
3326
console.log( x );
3427

3528
var z = snrm2( x.length, x, 1 );

lib/node_modules/@stdlib/blas/base/snrm2/include.gypi

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# @license Apache-2.0
22
#
3-
# Copyright (c) 2018 The Stdlib Authors.
3+
# Copyright (c) 2023 The Stdlib Authors.
44
#
55
# Licensed under the Apache License, Version 2.0 (the "License");
66
# you may not use this file except in compliance with the License.
@@ -52,7 +52,7 @@
5252

5353
# Source files:
5454
'src_files': [
55-
'<(src_dir)/addon.cpp',
55+
'<(src_dir)/addon.c',
5656
'<!@(node -e "var arr = require(\'@stdlib/utils/library-manifest\')(\'./manifest.json\',{\'os\':\'<(OS)\',\'blas\':\'<(blas)\'},{\'basedir\':process.cwd(),\'paths\':\'posix\'}).src; for ( var i = 0; i < arr.length; i++ ) { console.log( arr[ i ] ); }")',
5757
],
5858

0 commit comments

Comments
 (0)