Skip to content

Commit 8ddacd0

Browse files
committed
Add generalized BLAS routine to multiply a vector by a constant
1 parent 60a01b5 commit 8ddacd0

File tree

13 files changed

+1289
-0
lines changed

13 files changed

+1289
-0
lines changed
Lines changed: 181 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,181 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2020 The Stdlib Authors.
6+
7+
Licensed under the Apache License, Version 2.0 (the "License");
8+
you may not use this file except in compliance with the License.
9+
You may obtain a copy of the License at
10+
11+
http://www.apache.org/licenses/LICENSE-2.0
12+
13+
Unless required by applicable law or agreed to in writing, software
14+
distributed under the License is distributed on an "AS IS" BASIS,
15+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
See the License for the specific language governing permissions and
17+
limitations under the License.
18+
19+
-->
20+
21+
# gscal
22+
23+
> Multiply a vector `x` by a constant `alpha`.
24+
25+
<section class="usage">
26+
27+
## Usage
28+
29+
```javascript
30+
var gscal = require( '@stdlib/blas/base/gscal' );
31+
```
32+
33+
#### gscal( N, alpha, x, stride )
34+
35+
Multiplies a vector `x` by a constant `alpha`.
36+
37+
```javascript
38+
var x = [ -2.0, 1.0, 3.0, -5.0, 4.0, 0.0, -1.0, -3.0 ];
39+
40+
gscal( x.length, 5.0, x, 1 );
41+
// x => [ -10.0, 5.0, 15.0, -25.0, 20.0, 0.0, -5.0, -15.0 ]
42+
```
43+
44+
The function accepts the following parameters:
45+
46+
- **N**: number of indexed elements.
47+
- **alpha**: `numeric` constant
48+
- **x**: input [`Array`][mdn-array] or [`typed array`][mdn-typed-array].
49+
- **stride**: index increment.
50+
51+
The `N` and `stride` parameters determine which elements in `x` are accessed at runtime. For example, to multiply every other value by a constant
52+
53+
```javascript
54+
var floor = require( '@stdlib/math/base/special/floor' );
55+
56+
var x = [ -2.0, 1.0, 3.0, -5.0, 4.0, 0.0, -1.0, -3.0 ];
57+
58+
var N = floor( x.length / 2 );
59+
var alpha = 5.0;
60+
var stride = 2;
61+
62+
gscal( N, alpha, x, stride );
63+
// x => [ -10.0, 1.0, 15.0, -5.0, 20.0, 0.0, -5.0, -3.0 ]
64+
```
65+
66+
Note that indexing is relative to the first index. To introduce an offset, use [`typed array`][mdn-typed-array] views.
67+
68+
```javascript
69+
var Float64Array = require( '@stdlib/array/float64' );
70+
var floor = require( '@stdlib/math/base/special/floor' );
71+
72+
// Initial array...
73+
var x0 = new Float64Array( [ 1.0, -2.0, 3.0, -4.0, 5.0, -6.0 ] );
74+
75+
// Create an offset view...
76+
var x1 = new Float64Array( x0.buffer, x0.BYTES_PER_ELEMENT*1 ); // start at 2nd element
77+
78+
var N = floor( x0.length / 2 );
79+
var alpha = 5.0;
80+
var stride = 2;
81+
82+
// Scale every other value...
83+
gscal( N, alpha, x1, stride );
84+
// x0 => <Float64Array>[ 1.0, -10.0, 3.0, -20.0, 5.0, -30.0 ]
85+
```
86+
87+
#### gscal.ndarray( N, alpha, x, stride, offset )
88+
89+
Multiplies a vector `x` by a constant `alpha`, with alternative indexing semantics.
90+
91+
```javascript
92+
var x = [ -2.0, 1.0, 3.0, -5.0, 4.0, 0.0, -1.0, -3.0 ];
93+
94+
gscal.ndarray( x.length, 5.0, x, 1, 0 );
95+
// x => [ -10.0, 5.0, 15.0, -25.0, 20.0, 0.0, -5.0, -15.0 ]
96+
```
97+
98+
The function accepts the following additional parameters:
99+
100+
- **offset**: starting index.
101+
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 multiply the last three elements of `x` by a constant
103+
104+
```javascript
105+
var x = [ 1.0, -2.0, 3.0, -4.0, 5.0, -6.0 ];
106+
var alpha = 5.0;
107+
108+
gscal.ndarray( 3, alpha, x, 1, x.length-3 );
109+
// x => [ 1.0, -2.0, 3.0, -20.0, 25.0, -30.0 ]
110+
```
111+
112+
</section>
113+
114+
<!-- /.usage -->
115+
116+
<section class="notes">
117+
118+
## Notes
119+
120+
- If `N <= 0` or `stride <= 0`, both functions return `x` unchanged.
121+
- `gscal()` corresponds to the [BLAS][blas] level 1 function [`dscal`][dscal] with the exception that this implementation works with any array type, not just Float64Arrays. Depending on the environment, the typed versions ([`dscal`][@stdlib/blas/base/dscal], [`sscal`][@stdlib/blas/base/sscal], etc.) are likely to be significantly more performant.
122+
123+
</section>
124+
125+
<!-- /.notes -->
126+
127+
<section class="examples">
128+
129+
## Examples
130+
131+
<!-- eslint no-undef: "error" -->
132+
133+
```javascript
134+
var round = require( '@stdlib/math/base/special/round' );
135+
var randu = require( '@stdlib/random/base/randu' );
136+
var Float64Array = require( '@stdlib/array/float64' );
137+
var gscal = require( '@stdlib/blas/base/gscal' );
138+
139+
var rand;
140+
var sign;
141+
var x;
142+
var i;
143+
144+
x = new Float64Array( 100 );
145+
for ( i = 0; i < x.length; i++ ) {
146+
rand = round( randu()*100.0 );
147+
sign = randu();
148+
if ( sign < 0.5 ) {
149+
sign = -1.0;
150+
} else {
151+
sign = 1.0;
152+
}
153+
x[ i ] = sign * rand;
154+
}
155+
console.log( x );
156+
157+
gscal( x.length, 5.0, x, 1 );
158+
console.log( x );
159+
```
160+
161+
</section>
162+
163+
<!-- /.examples -->
164+
165+
<section class="links">
166+
167+
[blas]: http://www.netlib.org/blas
168+
169+
[dscal]: http://www.netlib.org/lapack/explore-html/df/d28/group__double__blas__level1.html
170+
171+
[mdn-array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array
172+
173+
[mdn-typed-array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray
174+
175+
[@stdlib/blas/base/dscal]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/blas/base/dscal
176+
177+
[@stdlib/blas/base/sscal]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/blas/base/sscal
178+
179+
</section>
180+
181+
<!-- /.links -->
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2020 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
'use strict';
20+
21+
// MODULES //
22+
23+
var bench = require( '@stdlib/bench' );
24+
var randu = require( '@stdlib/random/base/randu' );
25+
var isnan = require( '@stdlib/math/base/assert/is-nan' );
26+
var pow = require( '@stdlib/math/base/special/pow' );
27+
var Float64Array = require( '@stdlib/array/float64' );
28+
var pkg = require( './../package.json' ).name;
29+
var gscal = require( './../lib/main.js' );
30+
31+
32+
// FUNCTIONS //
33+
34+
/**
35+
* Create a benchmark function.
36+
*
37+
* @private
38+
* @param {PositiveInteger} len - array length
39+
* @returns {Function} benchmark function
40+
*/
41+
function createBenchmark( len ) {
42+
var x;
43+
var i;
44+
45+
x = new Float64Array( len );
46+
for ( i = 0; i < len; i++ ) {
47+
x[ i ] = ( randu()*10.0 ) - 20.0;
48+
}
49+
return benchmark;
50+
51+
function benchmark( b ) {
52+
var y;
53+
var i;
54+
55+
b.tic();
56+
for ( i = 0; i < b.iterations; i++ ) {
57+
y = gscal( x.length, 5.0, x, 1 );
58+
if ( isnan( y ) ) {
59+
b.fail( 'should not return NaN' );
60+
}
61+
}
62+
b.toc();
63+
if ( isnan(y) ) {
64+
b.fail( 'should not return NaN' );
65+
}
66+
b.pass( 'benchmark finished' );
67+
b.end();
68+
}
69+
}
70+
71+
72+
// MAIN //
73+
74+
function main() {
75+
var len;
76+
var min;
77+
var max;
78+
var f;
79+
var i;
80+
81+
min = 1; // 10^min
82+
max = 6; // 10^max
83+
84+
for ( i = min; i <= max; i++ ) {
85+
len = pow( 10, i );
86+
f = createBenchmark( len );
87+
bench( pkg+':len='+len, f );
88+
}
89+
}
90+
91+
main();
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2020 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
'use strict';
20+
21+
// MODULES //
22+
23+
var bench = require( '@stdlib/bench' );
24+
var randu = require( '@stdlib/random/base/randu' );
25+
var isnan = require( '@stdlib/math/base/assert/is-nan' );
26+
var pow = require( '@stdlib/math/base/special/pow' );
27+
var Float64Array = require( '@stdlib/array/float64' );
28+
var pkg = require( './../package.json' ).name;
29+
var gscal = require( './../lib/main.js' ).ndarray;
30+
31+
32+
// FUNCTIONS //
33+
34+
/**
35+
* Create a benchmark function.
36+
*
37+
* @private
38+
* @param {PositiveInteger} len - array length
39+
* @returns {Function} benchmark function
40+
*/
41+
function createBenchmark( len ) {
42+
var x;
43+
var i;
44+
45+
x = new Float64Array( len );
46+
for ( i = 0; i < len; i++ ) {
47+
x[ i ] = ( randu()*10.0 ) - 20.0;
48+
}
49+
return benchmark;
50+
51+
function benchmark( b ) {
52+
var y;
53+
var i;
54+
55+
b.tic();
56+
for ( i = 0; i < b.iterations; i++ ) {
57+
y = gscal( x.length, 5.0, x, 1, 0 );
58+
if ( isnan( y ) ) {
59+
b.fail( 'should not return NaN' );
60+
}
61+
}
62+
b.toc();
63+
if ( isnan(y) ) {
64+
b.fail( 'should not return NaN' );
65+
}
66+
b.pass( 'benchmark finished' );
67+
b.end();
68+
}
69+
}
70+
71+
72+
// MAIN //
73+
74+
function main() {
75+
var len;
76+
var min;
77+
var max;
78+
var f;
79+
var i;
80+
81+
min = 1; // 10^min
82+
max = 6; // 10^max
83+
84+
for ( i = min; i <= max; i++ ) {
85+
len = pow( 10, i );
86+
f = createBenchmark( len );
87+
bench( pkg+':ndarray:len='+len, f );
88+
}
89+
}
90+
91+
main();

0 commit comments

Comments
 (0)