README.md (6222B)
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 # gdot 22 23 > Calculate the dot product of two vectors. 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@6cf4829ce9c06ba9fa207a2ea3b395266f86a259/lib/node_modules/@stdlib/blas/base/gdot/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 gdot = require( '@stdlib/blas/base/gdot' ); 48 ``` 49 50 #### gdot( N, x, strideX, y, strideY ) 51 52 Calculates the dot product of vectors `x` and `y`. 53 54 ```javascript 55 var x = [ 4.0, 2.0, -3.0, 5.0, -1.0 ]; 56 var y = [ 2.0, 6.0, -1.0, -4.0, 8.0 ]; 57 58 var z = gdot( x.length, x, 1, y, 1 ); 59 // returns -5.0 60 ``` 61 62 The function has the following parameters: 63 64 - **N**: number of indexed elements. 65 - **x**: first input [`Array`][mdn-array] or [`typed array`][mdn-typed-array]. 66 - **strideX**: index increment for `x`. 67 - **y**: second input [`Array`][mdn-array] or [`typed array`][mdn-typed-array]. 68 - **strideY**: index increment for `y`. 69 70 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, 71 72 ```javascript 73 var floor = require( '@stdlib/math/base/special/floor' ); 74 75 var x = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; 76 var y = [ 1.0, 1.0, 1.0, 1.0, 1.0, 1.0 ]; 77 78 var N = floor( x.length / 2 ); 79 80 var z = gdot( N, x, 2, y, -1 ); 81 // returns 9.0 82 ``` 83 84 Note that indexing is relative to the first index. To introduce an offset, use [`typed array`][mdn-typed-array] views. 85 86 <!-- eslint-disable stdlib/capitalized-comments --> 87 88 ```javascript 89 var Float64Array = require( '@stdlib/array/float64' ); 90 var floor = require( '@stdlib/math/base/special/floor' ); 91 92 // Initial arrays... 93 var x0 = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] ); 94 var y0 = new Float64Array( [ 7.0, 8.0, 9.0, 10.0, 11.0, 12.0 ] ); 95 96 // Create offset views... 97 var x1 = new Float64Array( x0.buffer, x0.BYTES_PER_ELEMENT*1 ); // start at 2nd element 98 var y1 = new Float64Array( y0.buffer, y0.BYTES_PER_ELEMENT*3 ); // start at 4th element 99 100 var N = floor( x0.length / 2 ); 101 102 var z = gdot( N, x1, -2, y1, 1 ); 103 // returns 128.0 104 ``` 105 106 #### gdot.ndarray( N, x, strideX, offsetX, y, strideY, offsetY ) 107 108 Calculates the dot product of `x` and `y` using alternative indexing semantics. 109 110 ```javascript 111 var x = [ 4.0, 2.0, -3.0, 5.0, -1.0 ]; 112 var y = [ 2.0, 6.0, -1.0, -4.0, 8.0 ]; 113 114 var z = gdot.ndarray( x.length, x, 1, 0, y, 1, 0 ); 115 // returns -5.0 116 ``` 117 118 The function has the following additional parameters: 119 120 - **offsetX**: starting index for `x`. 121 - **offsetY**: starting index for `y`. 122 123 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 124 125 ```javascript 126 var floor = require( '@stdlib/math/base/special/floor' ); 127 128 var x = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; 129 var y = [ 7.0, 8.0, 9.0, 10.0, 11.0, 12.0 ]; 130 131 var N = floor( x.length / 2 ); 132 133 var z = gdot.ndarray( N, x, 2, 1, y, -1, y.length-1 ); 134 // returns 128.0 135 ``` 136 137 </section> 138 139 <!-- /.usage --> 140 141 <section class="notes"> 142 143 ## Notes 144 145 - If `N <= 0` both functions return `0.0`. 146 - `gdot()` corresponds to the [BLAS][blas] level 1 function [`ddot`][ddot] with the exception that this implementation works with any array type, not just Float64Arrays. Depending on the environment, the typed versions ([`ddot`][@stdlib/blas/base/ddot], [`sdot`][@stdlib/blas/base/sdot], etc.) are likely to be significantly more performant. 147 148 </section> 149 150 <!-- /.notes --> 151 152 <section class="examples"> 153 154 ## Examples 155 156 <!-- eslint no-undef: "error" --> 157 158 ```javascript 159 var randu = require( '@stdlib/random/base/randu' ); 160 var round = require( '@stdlib/math/base/special/round' ); 161 var Float64Array = require( '@stdlib/array/float64' ); 162 var Uint8ClampedArray = require( '@stdlib/array/uint8c' ); 163 var gdot = require( '@stdlib/blas/base/gdot' ); 164 165 var x; 166 var y; 167 var i; 168 169 x = new Float64Array( 10 ); 170 y = new Uint8ClampedArray( 10 ); 171 for ( i = 0; i < x.length; i++ ) { 172 x[ i ] = round( randu()*500.0 ); 173 y[ i ] = round( randu()*255.0 ); 174 } 175 console.log( x ); 176 console.log( y ); 177 178 // Compute the dot product: 179 var dot = gdot.ndarray( x.length, x, 1, 0, y, -1, y.length-1 ); 180 console.log( dot ); 181 ``` 182 183 </section> 184 185 <!-- /.examples --> 186 187 <section class="links"> 188 189 [dot-product]: https://en.wikipedia.org/wiki/Dot_product 190 191 [blas]: http://www.netlib.org/blas 192 193 [ddot]: http://www.netlib.org/lapack/explore-html/de/da4/group__double__blas__level1.html 194 195 [mdn-array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array 196 197 [mdn-typed-array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray 198 199 [@stdlib/blas/base/ddot]: https://www.npmjs.com/package/@stdlib/blas/tree/main/base/ddot 200 201 [@stdlib/blas/base/sdot]: https://www.npmjs.com/package/@stdlib/blas/tree/main/base/sdot 202 203 </section> 204 205 <!-- /.links -->