README.md (5670B)
1 <!-- 2 3 @license Apache-2.0 4 5 Copyright (c) 2018 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 # saxpy 22 23 > Multiply a vector `x` by a constant `alpha` and add the result to `y`. 24 25 <section class="usage"> 26 27 ## Usage 28 29 ```javascript 30 var saxpy = require( '@stdlib/blas/base/saxpy' ); 31 ``` 32 33 #### saxpy( N, alpha, x, strideX, y, strideY ) 34 35 Multiplies a vector `x` by a constant `alpha` and adds the result to `y`. 36 37 ```javascript 38 var Float32Array = require( '@stdlib/array/float32' ); 39 40 var x = new Float32Array( [ 1.0, 2.0, 3.0, 4.0, 5.0 ] ); 41 var y = new Float32Array( [ 1.0, 1.0, 1.0, 1.0, 1.0 ] ); 42 var alpha = 5.0; 43 44 saxpy( x.length, alpha, x, 1, y, 1 ); 45 // y => <Float32Array>[ 6.0, 11.0, 16.0, 21.0, 26.0 ] 46 ``` 47 48 The function has the following parameters: 49 50 - **N**: number of indexed elements. 51 - **alpha**: `numeric` constant. 52 - **x**: input [`Float32Array`][mdn-float32array]. 53 - **strideX**: index increment for `x`. 54 - **y**: input [`Float32Array`][mdn-float32array]. 55 - **strideY**: index increment for `y`. 56 57 The `N` and `stride` parameters determine which elements in `x` and `y` are accessed at runtime. For example, to multiply every other value in `x` by `alpha` and add the result to the first `N` elements of `y` in reverse order, 58 59 ```javascript 60 var Float32Array = require( '@stdlib/array/float32' ); 61 var floor = require( '@stdlib/math/base/special/floor' ); 62 63 var x = new Float32Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] ); 64 var y = new Float32Array( [ 1.0, 1.0, 1.0, 1.0, 1.0, 1.0 ] ); 65 66 var alpha = 5.0; 67 var N = floor( x.length / 2 ); 68 69 saxpy( N, alpha, x, 2, y, -1 ); 70 // y => <Float32Array>[ 26.0, 16.0, 6.0, 1.0, 1.0, 1.0 ] 71 ``` 72 73 Note that indexing is relative to the first index. To introduce an offset, use [`typed array`][mdn-typed-array] views. 74 75 <!-- eslint-disable stdlib/capitalized-comments --> 76 77 ```javascript 78 var Float32Array = require( '@stdlib/array/float32' ); 79 var floor = require( '@stdlib/math/base/special/floor' ); 80 81 // Initial arrays... 82 var x0 = new Float32Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] ); 83 var y0 = new Float32Array( [ 7.0, 8.0, 9.0, 10.0, 11.0, 12.0 ] ); 84 85 // Create offset views... 86 var x1 = new Float32Array( x0.buffer, x0.BYTES_PER_ELEMENT*1 ); // start at 2nd element 87 var y1 = new Float32Array( y0.buffer, y0.BYTES_PER_ELEMENT*3 ); // start at 4th element 88 89 var N = floor( x0.length / 2 ); 90 91 saxpy( N, 5.0, x1, -2, y1, 1 ); 92 // y0 => <Float32Array>[ 7.0, 8.0, 9.0, 40.0, 31.0, 22.0 ] 93 ``` 94 95 #### saxpy.ndarray( N, alpha, x, strideX, offsetX, y, strideY, offsetY ) 96 97 Multiplies a vector `x` by a constant `alpha` and adds the result to `y` using alternative indexing semantics. 98 99 ```javascript 100 var Float32Array = require( '@stdlib/array/float32' ); 101 102 var x = new Float32Array( [ 1.0, 2.0, 3.0, 4.0, 5.0 ] ); 103 var y = new Float32Array( [ 1.0, 1.0, 1.0, 1.0, 1.0 ] ); 104 var alpha = 5.0; 105 106 saxpy.ndarray( x.length, alpha, x, 1, 0, y, 1, 0 ); 107 // y => <Float32Array>[ 6.0, 11.0, 16.0, 21.0, 26.0 ] 108 ``` 109 110 The function has the following additional parameters: 111 112 - **offsetX**: starting index for `x`. 113 - **offsetY**: starting index for `y`. 114 115 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 multiply every other value in `x` by a constant `alpha` starting from the second value and add to the last `N` elements in `y` where `x[i] -> y[n]`, `x[i+2] -> y[n-1]`,..., 116 117 ```javascript 118 var Float32Array = require( '@stdlib/array/float32' ); 119 var floor = require( '@stdlib/math/base/special/floor' ); 120 121 var x = new Float32Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] ); 122 var y = new Float32Array( [ 7.0, 8.0, 9.0, 10.0, 11.0, 12.0 ] ); 123 124 var alpha = 5.0; 125 var N = floor( x.length / 2 ); 126 127 saxpy.ndarray( N, alpha, x, 2, 1, y, -1, y.length-1 ); 128 // y => <Float32Array>[ 7.0, 8.0, 9.0, 40.0, 31.0, 22.0 ] 129 ``` 130 131 </section> 132 133 <!-- /.usage --> 134 135 <section class="notes"> 136 137 ## Notes 138 139 - If `N <= 0` or `alpha == 0`, both functions return `y` unchanged. 140 - `saxpy()` corresponds to the [BLAS][blas] level 1 function [`saxpy`][saxpy]. 141 142 </section> 143 144 <!-- /.notes --> 145 146 <section class="examples"> 147 148 ## Examples 149 150 <!-- eslint no-undef: "error" --> 151 152 ```javascript 153 var randu = require( '@stdlib/random/base/randu' ); 154 var round = require( '@stdlib/math/base/special/round' ); 155 var Float32Array = require( '@stdlib/array/float32' ); 156 var saxpy = require( '@stdlib/blas/base/saxpy' ); 157 158 var x; 159 var y; 160 var i; 161 162 x = new Float32Array( 10 ); 163 y = new Float32Array( 10 ); 164 for ( i = 0; i < x.length; i++ ) { 165 x[ i ] = round( randu()*100.0 ); 166 y[ i ] = round( randu()*10.0 ); 167 } 168 console.log( x ); 169 console.log( y ); 170 171 saxpy.ndarray( x.length, 5.0, x, 1, 0, y, -1, y.length-1 ); 172 console.log( y ); 173 ``` 174 175 </section> 176 177 <!-- /.examples --> 178 179 <section class="links"> 180 181 [blas]: http://www.netlib.org/blas 182 183 [saxpy]: http://www.netlib.org/lapack/explore-html/df/d28/group__single__blas__level1.html 184 185 [mdn-float32array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Float32Array 186 187 [mdn-typed-array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray 188 189 </section> 190 191 <!-- /.links -->