README.md (3695B)
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 # sdot 22 23 > Calculate the dot product of two single-precision floating-point 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@03fff24f5a7ba807a292f08cfef75ed0748e40de/lib/node_modules/@stdlib/blas/sdot/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 sdot = require( '@stdlib/blas/sdot' ); 48 ``` 49 50 #### sdot( x, y ) 51 52 Calculates the dot product of vectors `x` and `y`. 53 54 ```javascript 55 var Float32Array = require( '@stdlib/array/float32' ); 56 var array = require( '@stdlib/ndarray/array' ); 57 58 var x = array( new Float32Array( [ 4.0, 2.0, -3.0, 5.0, -1.0 ] ) ); 59 var y = array( new Float32Array( [ 2.0, 6.0, -1.0, -4.0, 8.0 ] ) ); 60 61 var z = sdot( x, y ); 62 // returns -5.0 63 ``` 64 65 The function has the following parameters: 66 67 - **x**: a 1-dimensional [`ndarray`][@stdlib/ndarray/array] whose underlying data type is `float32`. 68 - **y**: a 1-dimensional [`ndarray`][@stdlib/ndarray/array] whose underlying data type is `float32`. 69 70 If provided empty vectors, the function returns `0.0`. 71 72 ```javascript 73 var Float32Array = require( '@stdlib/array/float32' ); 74 var array = require( '@stdlib/ndarray/array' ); 75 76 var x = array( new Float32Array() ); 77 var y = array( new Float32Array() ); 78 79 var z = sdot( x, y ); 80 // returns 0.0 81 ``` 82 83 </section> 84 85 <!-- /.usage --> 86 87 <section class="notes"> 88 89 ## Notes 90 91 - `sdot()` provides a higher-level interface to the [BLAS][blas] level 1 function [`sdot`][@stdlib/blas/base/sdot]. 92 93 </section> 94 95 <!-- /.notes --> 96 97 <section class="examples"> 98 99 ## Examples 100 101 <!-- eslint no-undef: "error" --> 102 103 ```javascript 104 var discreteUniform = require( '@stdlib/random/base/discrete-uniform' ); 105 var Float32Array = require( '@stdlib/array/float32' ); 106 var array = require( '@stdlib/ndarray/array' ); 107 var sdot = require( '@stdlib/blas/sdot' ); 108 109 var x = array( new Float32Array( 10 ) ); 110 var y = array( new Float32Array( 10 ) ); 111 112 var rand1 = discreteUniform.factory( 0, 100 ); 113 var rand2 = discreteUniform.factory( 0, 10 ); 114 115 var i; 116 for ( i = 0; i < x.length; i++ ) { 117 x.set( i, rand1() ); 118 y.set( i, rand2() ); 119 } 120 console.log( x.toString() ); 121 console.log( y.toString() ); 122 123 var z = sdot( x, y ); 124 console.log( z ); 125 ``` 126 127 </section> 128 129 <!-- /.examples --> 130 131 <section class="links"> 132 133 [dot-product]: https://en.wikipedia.org/wiki/Dot_product 134 135 [blas]: http://www.netlib.org/blas 136 137 [@stdlib/blas/base/sdot]: https://www.npmjs.com/package/@stdlib/blas/tree/main/base/sdot 138 139 [@stdlib/ndarray/array]: https://www.npmjs.com/package/@stdlib/ndarray-array 140 141 </section> 142 143 <!-- /.links -->