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