README.md (6703B)
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 # sdsdot 22 23 > Calculate the dot product of two single-precision floating-point vectors with extended accumulation. 24 25 <section class="intro"> 26 27 The [dot product][dot-product] (or scalar product) is defined as 28 29 <!-- <equation class="equation" label="eq:dot_product" align="center" raw="\mathbf{x}\cdot\mathbf{y} = \sum_{i=0}^{N-1} x_i y_i = x_0 y_0 + x_1 y_1 + \ldots + x_{N-1} y_{N-1}" alt="Dot product definition."> --> 30 31 <div class="equation" align="center" data-raw-text="\mathbf{x}\cdot\mathbf{y} = \sum_{i=0}^{N-1} x_i y_i = x_0 y_0 + x_1 y_1 + \ldots + x_{N-1} y_{N-1}" data-equation="eq:dot_product"> 32 <img src="https://cdn.jsdelivr.net/gh/stdlib-js/stdlib@37cdbad9473fd050c8f7185c122a33d0f680b6c6/lib/node_modules/@stdlib/blas/base/sdsdot/docs/img/equation_dot_product.svg" alt="Dot product definition."> 33 <br> 34 </div> 35 36 <!-- </equation> --> 37 38 </section> 39 40 <!-- /.intro --> 41 42 <section class="usage"> 43 44 ## Usage 45 46 ```javascript 47 var sdsdot = require( '@stdlib/blas/base/sdsdot' ); 48 ``` 49 50 #### sdsdot( N, scalar, x, strideX, y, strideY ) 51 52 Calculates the dot product of vectors `x` and `y` with extended accumulation. 53 54 ```javascript 55 var Float32Array = require( '@stdlib/array/float32' ); 56 57 var x = new Float32Array( [ 4.0, 2.0, -3.0, 5.0, -1.0 ] ); 58 var y = new Float32Array( [ 2.0, 6.0, -1.0, -4.0, 8.0 ] ); 59 60 var z = sdsdot( x.length, 0.0, x, 1, y, 1 ); 61 // returns -5.0 62 ``` 63 64 The function has the following parameters: 65 66 - **N**: number of indexed elements. 67 - **scalar**: scalar constant added to the dot product. 68 - **x**: input [`Float32Array`][@stdlib/array/float32]. 69 - **strideX**: index increment for `x`. 70 - **y**: input [`Float32Array`][@stdlib/array/float32]. 71 - **strideY**: index increment for `y`. 72 73 The `N` and `stride` parameters determine which elements in `x` and `y` are accessed at runtime. For example, to calculate the dot product of every other value in `x` and the first `N` elements of `y` in reverse order, 74 75 ```javascript 76 var Float32Array = require( '@stdlib/array/float32' ); 77 var floor = require( '@stdlib/math/base/special/floor' ); 78 79 var x = new Float32Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] ); 80 var y = new Float32Array( [ 1.0, 1.0, 1.0, 1.0, 1.0, 1.0 ] ); 81 82 var N = floor( x.length / 2 ); 83 84 var z = sdsdot( N, 0.0, x, 2, y, -1 ); 85 // returns 9.0 86 ``` 87 88 Note that indexing is relative to the first index. To introduce an offset, use [`typed array`][mdn-typed-array] views. 89 90 <!-- eslint-disable stdlib/capitalized-comments --> 91 92 ```javascript 93 var Float32Array = require( '@stdlib/array/float32' ); 94 var floor = require( '@stdlib/math/base/special/floor' ); 95 96 // Initial arrays... 97 var x0 = new Float32Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] ); 98 var y0 = new Float32Array( [ 7.0, 8.0, 9.0, 10.0, 11.0, 12.0 ] ); 99 100 // Create offset views... 101 var x1 = new Float32Array( x0.buffer, x0.BYTES_PER_ELEMENT*1 ); // start at 2nd element 102 var y1 = new Float32Array( y0.buffer, y0.BYTES_PER_ELEMENT*3 ); // start at 4th element 103 104 var N = floor( x0.length / 2 ); 105 106 var z = sdsdot( N, 0.0, x1, -2, y1, 1 ); 107 // returns 128.0 108 ``` 109 110 #### sdsdot.ndarray( N, x, strideX, offsetX, y, strideY, offsetY ) 111 112 Calculates the dot product of vectors `x` and `y` with extended accumulation and using alternative indexing semantics. 113 114 ```javascript 115 var Float32Array = require( '@stdlib/array/float32' ); 116 117 var x = new Float32Array( [ 4.0, 2.0, -3.0, 5.0, -1.0 ] ); 118 var y = new Float32Array( [ 2.0, 6.0, -1.0, -4.0, 8.0 ] ); 119 120 var z = sdsdot.ndarray( x.length, 0.0, x, 1, 0, y, 1, 0 ); 121 // returns -5.0 122 ``` 123 124 The function has the following additional parameters: 125 126 - **offsetX**: starting index for `x`. 127 - **offsetY**: starting index for `y`. 128 129 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 calculate the dot product of every other value in `x` starting from the second value with the last 3 elements in `y` in reverse order 130 131 ```javascript 132 var Float32Array = require( '@stdlib/array/float32' ); 133 var floor = require( '@stdlib/math/base/special/floor' ); 134 135 var x = new Float32Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] ); 136 var y = new Float32Array( [ 7.0, 8.0, 9.0, 10.0, 11.0, 12.0 ] ); 137 138 var N = floor( x.length / 2 ); 139 140 var z = sdsdot.ndarray( N, 0.0, x, 2, 1, y, -1, y.length-1 ); 141 // returns 128.0 142 ``` 143 144 </section> 145 146 <!-- /.usage --> 147 148 <section class="notes"> 149 150 ## Notes 151 152 - If `N <= 0`, both functions return `scalar`. 153 - `sdsdot()` corresponds to the [BLAS][blas] level 1 function [`sdsdot`][sdsdot]. 154 155 </section> 156 157 <!-- /.notes --> 158 159 <section class="examples"> 160 161 ## Examples 162 163 <!-- eslint no-undef: "error" --> 164 165 ```javascript 166 var randu = require( '@stdlib/random/base/randu' ); 167 var round = require( '@stdlib/math/base/special/round' ); 168 var Float32Array = require( '@stdlib/array/float32' ); 169 var sdsdot = require( '@stdlib/blas/base/sdsdot' ); 170 171 var x; 172 var y; 173 var i; 174 175 x = new Float32Array( 10 ); 176 y = new Float32Array( 10 ); 177 for ( i = 0; i < x.length; i++ ) { 178 x[ i ] = round( randu() * 100.0 ); 179 y[ i ] = round( randu() * 10.0 ); 180 } 181 console.log( x ); 182 console.log( y ); 183 184 var z = sdsdot( x.length, 0.0, x, 1, y, -1 ); 185 console.log( z ); 186 ``` 187 188 </section> 189 190 <!-- /.examples --> 191 192 * * * 193 194 <section class="references"> 195 196 ## References 197 198 - Lawson, Charles L., Richard J. Hanson, Fred T. Krogh, and David Ronald Kincaid. 1979. "Algorithm 539: Basic Linear Algebra Subprograms for Fortran Usage \[F1]." _ACM Transactions on Mathematical Software_ 5 (3). New York, NY, USA: Association for Computing Machinery: 324–25. doi:[10.1145/355841.355848][@lawson:1979a]. 199 200 </section> 201 202 <!-- /.references --> 203 204 <section class="links"> 205 206 [dot-product]: https://en.wikipedia.org/wiki/Dot_product 207 208 [blas]: http://www.netlib.org/blas 209 210 [sdsdot]: http://www.netlib.org/lapack/explore-html/df/d28/group__single__blas__level1.html 211 212 [@stdlib/array/float32]: https://www.npmjs.com/package/@stdlib/array-float32 213 214 [mdn-typed-array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray 215 216 [@lawson:1979a]: https://doi.org/10.1145/355841.355848 217 218 </section> 219 220 <!-- /.links -->