main.js (3942B)
1 /** 2 * @license Apache-2.0 3 * 4 * Copyright (c) 2020 The Stdlib Authors. 5 * 6 * Licensed under the Apache License, Version 2.0 (the "License"); 7 * you may not use this file except in compliance with the License. 8 * You may obtain a copy of the License at 9 * 10 * http://www.apache.org/licenses/LICENSE-2.0 11 * 12 * Unless required by applicable law or agreed to in writing, software 13 * distributed under the License is distributed on an "AS IS" BASIS, 14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 * See the License for the specific language governing permissions and 16 * limitations under the License. 17 */ 18 19 'use strict'; 20 21 // MODULES // 22 23 var isndarrayLike = require( '@stdlib/assert/is-ndarray-like' ); 24 var isCollection = require( '@stdlib/assert/is-collection' ); 25 var isFloat64Array = require( '@stdlib/assert/is-float64array' ); 26 var isFloat32Array = require( '@stdlib/assert/is-float32array' ); 27 var ddot = require( './../../base/ddot' ).ndarray; 28 var sdot = require( './../../base/sdot' ).ndarray; 29 var dot = require( './../../base/gdot' ).ndarray; 30 31 32 // MAIN // 33 34 /** 35 * Computes the dot product of vectors `x` and `y`. 36 * 37 * ## Notes 38 * 39 * - In general, for best performance, especially for large vectors, provide 1-dimensional `ndarrays` whose underlying data type is either `float64` or `float32`. 40 * 41 * @param {(Collection|VectorLike)} x - first input array 42 * @param {(Collection|VectorLike)} y - second input array 43 * @throws {TypeError} first argument must be either an array-like object or a 1-dimensional ndarray 44 * @throws {TypeError} second argument must be either an array-like object or a 1-dimensional ndarray 45 * @throws {RangeError} input arrays must be the same length 46 * @returns {number} dot product 47 * 48 * @example 49 * var x = [ 4.0, 2.0, -3.0, 5.0, -1.0 ]; 50 * var y = [ 2.0, 6.0, -1.0, -4.0, 8.0 ]; 51 * 52 * var z = gdot( x, y ); 53 * // returns -5.0 54 */ 55 function gdot( x, y ) { 56 var isxf64; 57 var isxf32; 58 var isyf64; 59 var isyf32; 60 var isxa; 61 var isxv; 62 var isya; 63 var isyv; 64 65 isxa = isndarrayLike( x ); 66 isya = isndarrayLike( y ); 67 isxv = isxa && x.ndims === 1 && x.strides.length === 1; // is ndarray-like vector? 68 isyv = isya && y.ndims === 1 && y.strides.length === 1; // is ndarray-like vector? 69 if ( isxv ) { 70 isxf64 = isFloat64Array( x.data ); 71 isxf32 = ( isxf64 ) ? false : isFloat32Array( x.data ); 72 } else if ( isxa === false && isCollection( x ) ) { 73 isxf64 = isFloat64Array( x ); 74 isxf32 = ( isxf64 ) ? false : isFloat32Array( x ); 75 } else { 76 throw new TypeError( 'invalid argument. First argument must be either an array-like object or a 1-dimensional ndarray. Value: `' + x + '`.' ); 77 } 78 if ( isyv ) { 79 isyf64 = isFloat64Array( y.data ); 80 isyf32 = ( isyf64 ) ? false : isFloat32Array( y.data ); 81 } else if ( isya === false && isCollection( y ) ) { 82 isyf64 = isFloat64Array( y ); 83 isyf32 = ( isyf64 ) ? false : isFloat32Array( y ); 84 } else { 85 throw new TypeError( 'invalid argument. Second argument must be either an array-like object or a 1-dimensional ndarray. Value: `' + y + '`.' ); 86 } 87 if ( x.length !== y.length ) { 88 throw new RangeError( 'invalid argument. Arrays must be the same length. First argument length: ' + x.length + '. Second argument length: ' + y.length + '.' ); 89 } 90 if ( isxv && isyv ) { 91 if ( isxf64 && isyf64 ) { 92 return ddot( x.length, x.data, x.strides[ 0 ], x.offset, y.data, y.strides[ 0 ], y.offset ); // eslint-disable-line max-len 93 } 94 if ( isxf32 && isyf32 ) { 95 return sdot( x.length, x.data, x.strides[ 0 ], x.offset, y.data, y.strides[ 0 ], y.offset ); // eslint-disable-line max-len 96 } 97 return dot( x.length, x.data, x.strides[ 0 ], x.offset, y.data, y.strides[ 0 ], y.offset ); // eslint-disable-line max-len 98 } 99 if ( isxv ) { 100 return dot( x.length, x.data, x.strides[ 0 ], x.offset, y, 1, 0 ); 101 } 102 if ( isyv ) { 103 return dot( x.length, x, 1, 0, y.data, y.strides[ 0 ], y.offset ); 104 } 105 return dot( x.length, x, 1, 0, y, 1, 0 ); 106 } 107 108 109 // EXPORTS // 110 111 module.exports = gdot;