Skip to content

Commit 117438b

Browse files
committed
Refactor to support a negative stride and clean-up
1 parent 20f2f1b commit 117438b

File tree

6 files changed

+30
-18
lines changed

6 files changed

+30
-18
lines changed

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,8 @@ var z = gnrm2( N, x1, 2 );
9393
// returns 5.0
9494
```
9595

96+
If either `N` or `stride` is less than or equal to `0`, the function returns `0`.
97+
9698
#### gnrm2.ndarray( N, x, stride, offset )
9799

98100
Computes the [L2-norm][l2-norm] of a vector using alternative indexing semantics.
@@ -128,7 +130,7 @@ var z = gnrm2.ndarray( N, x, 2, 1 );
128130

129131
## Notes
130132

131-
- If `N <= 0` or `stride <= 0`, both functions return `0.0`.
133+
- If `N <= 0`, both functions return `0.0`.
132134
- `gnrm2()` corresponds to the [BLAS][blas] level 1 function [`dnrm2`][dnrm2] with the exception that this implementation works with any array type, not just Float64Arrays. Depending on the environment, the typed versions ([`dnrm2`][@stdlib/blas/base/dnrm2], [`snrm2`][@stdlib/blas/base/snrm2], etc.) are likely to be significantly more performant.
133135

134136
</section>
@@ -152,7 +154,7 @@ var i;
152154

153155
x = new Float64Array( 10 );
154156
for ( i = 0; i < x.length; i++ ) {
155-
x[ i ] = round( randu() * 100.0 );
157+
x[ i ] = round( randu()*100.0 );
156158
}
157159
console.log( x );
158160

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ function createBenchmark( len ) {
4343

4444
x = [];
4545
for ( i = 0; i < len; i++ ) {
46-
x.push( ( randu()*10.0 ) - 20.0 );
46+
x.push( ( randu()*20.0 ) - 10.0 );
4747
}
4848
return benchmark;
4949

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ function createBenchmark( len ) {
4343

4444
x = [];
4545
for ( i = 0; i < len; i++ ) {
46-
x.push( ( randu()*10.0 ) - 20.0 );
46+
x.push( ( randu()*20.0 ) - 10.0 );
4747
}
4848
return benchmark;
4949

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ var i;
2828

2929
x = new Float64Array( 10 );
3030
for ( i = 0; i < x.length; i++ ) {
31-
x[ i ] = round( randu() * 10.0 );
31+
x[ i ] = round( randu()*10.0 );
3232
}
3333
console.log( x );
3434

lib/node_modules/@stdlib/blas/base/gnrm2/lib/ndarray.js

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ var pow = require( '@stdlib/math/base/special/pow' );
3232
*
3333
* @param {PositiveInteger} N - number of values over which to compute the L2-norm
3434
* @param {NumericArray} x - input array
35-
* @param {PositiveInteger} stride - stride length
35+
* @param {integer} stride - stride length
3636
* @param {NonNegativeInteger} offset - starting index
3737
* @returns {number} L2-norm
3838
*
@@ -49,27 +49,29 @@ function gnrm2( N, x, stride, offset ) {
4949
var scale;
5050
var ssq;
5151
var ax;
52+
var ix;
5253
var i;
5354

54-
if ( N <= 0 || stride <= 0 ) {
55+
if ( N <= 0 ) {
5556
return 0.0;
5657
}
5758
if ( N === 1 ) {
5859
return abs( x[ offset ] );
5960
}
61+
ix = offset;
6062
scale = 0.0;
6163
ssq = 1.0;
62-
N *= stride;
63-
for ( i = offset; i < N; i += stride ) {
64-
if ( x[ i ] !== 0.0 ) {
65-
ax = abs( x[ i ] );
64+
for ( i = 0; i < N; i++ ) {
65+
if ( x[ ix ] !== 0.0 ) {
66+
ax = abs( x[ ix ] );
6667
if ( scale < ax ) {
6768
ssq = 1.0 + ( ssq * pow( scale/ax, 2 ) );
6869
scale = ax;
6970
} else {
7071
ssq += pow( ax/scale, 2 );
7172
}
7273
}
74+
ix += stride;
7375
}
7476
return scale * sqrt( ssq );
7577
}

lib/node_modules/@stdlib/blas/base/gnrm2/test/test.ndarray.js

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -94,18 +94,26 @@ tape( 'the function supports a `stride` parameter', function test( t ) {
9494
t.end();
9595
});
9696

97-
tape( 'if provided a `stride` parameter less than or equal to `0`, the function returns `0`', function test( t ) {
97+
tape( 'the function supports a negative `stride` parameter', function test( t ) {
98+
var N;
9899
var x;
99100
var z;
100101

101-
x = [ 1.0, -2.0, -4.0, 5.0, 3.0 ];
102-
103-
z = gnrm2( x.length, x, 0, 0 );
104-
t.strictEqual( z, 0.0, 'returns expected value' );
102+
x = [
103+
1.0, // 0
104+
2.0,
105+
2.0, // 1
106+
-7.0,
107+
-2.0, // 2
108+
3.0,
109+
4.0, // 3
110+
2.0
111+
];
105112

106-
z = gnrm2( x.length, x, -1, 0 );
107-
t.strictEqual( z, 0.0, 'returns expected value' );
113+
N = floor( x.length / 2 );
114+
z = gnrm2( N, x, -2, x.length-2 );
108115

116+
t.strictEqual( z, 5.0, 'returns expected value' );
109117
t.end();
110118
});
111119

0 commit comments

Comments
 (0)