README.md (4986B)
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 has the following parameters: 45 46 - **N**: number of indexed elements. 47 - **alpha**: scalar 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 If either `N` or `stride` is less than or equal to `0`, the function returns `x` unchanged. 88 89 #### gscal.ndarray( N, alpha, x, stride, offset ) 90 91 Multiplies a vector `x` by a constant `alpha` using alternative indexing semantics. 92 93 ```javascript 94 var x = [ -2.0, 1.0, 3.0, -5.0, 4.0, 0.0, -1.0, -3.0 ]; 95 96 gscal.ndarray( x.length, 5.0, x, 1, 0 ); 97 // x => [ -10.0, 5.0, 15.0, -25.0, 20.0, 0.0, -5.0, -15.0 ] 98 ``` 99 100 The function has the following additional parameters: 101 102 - **offset**: starting index. 103 104 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 105 106 ```javascript 107 var x = [ 1.0, -2.0, 3.0, -4.0, 5.0, -6.0 ]; 108 var alpha = 5.0; 109 110 gscal.ndarray( 3, alpha, x, 1, x.length-3 ); 111 // x => [ 1.0, -2.0, 3.0, -20.0, 25.0, -30.0 ] 112 ``` 113 114 </section> 115 116 <!-- /.usage --> 117 118 <section class="notes"> 119 120 ## Notes 121 122 - If `N <= 0`, both functions return `x` unchanged. 123 - `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. 124 125 </section> 126 127 <!-- /.notes --> 128 129 <section class="examples"> 130 131 ## Examples 132 133 <!-- eslint no-undef: "error" --> 134 135 ```javascript 136 var round = require( '@stdlib/math/base/special/round' ); 137 var randu = require( '@stdlib/random/base/randu' ); 138 var Float64Array = require( '@stdlib/array/float64' ); 139 var gscal = require( '@stdlib/blas/base/gscal' ); 140 141 var rand; 142 var sign; 143 var x; 144 var i; 145 146 x = new Float64Array( 100 ); 147 for ( i = 0; i < x.length; i++ ) { 148 rand = round( randu()*100.0 ); 149 sign = randu(); 150 if ( sign < 0.5 ) { 151 sign = -1.0; 152 } else { 153 sign = 1.0; 154 } 155 x[ i ] = sign * rand; 156 } 157 console.log( x ); 158 159 gscal( x.length, 5.0, x, 1 ); 160 console.log( x ); 161 ``` 162 163 </section> 164 165 <!-- /.examples --> 166 167 <section class="links"> 168 169 [blas]: http://www.netlib.org/blas 170 171 [dscal]: http://www.netlib.org/lapack/explore-html/de/da4/group__double__blas__level1.html 172 173 [mdn-array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array 174 175 [mdn-typed-array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray 176 177 [@stdlib/blas/base/dscal]: https://www.npmjs.com/package/@stdlib/blas/tree/main/base/dscal 178 179 [@stdlib/blas/base/sscal]: https://www.npmjs.com/package/@stdlib/blas/tree/main/base/sscal 180 181 </section> 182 183 <!-- /.links -->