main.js (2529B)
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 isFloat64VectorLike = require( '@stdlib/assert/is-float64vector-like' ); 24 var dot = require( './../../base/ddot' ).ndarray; 25 26 27 // MAIN // 28 29 /** 30 * Computes the dot product of two double-precision floating-point vectors. 31 * 32 * @param {VectorLike} x - first input array 33 * @param {VectorLike} y - second input array 34 * @throws {TypeError} first argument must be a 1-dimensional ndarray containing double-precision floating-point numbers 35 * @throws {TypeError} second argument must be a 1-dimensional ndarray containing double-precision floating-point numbers 36 * @throws {RangeError} input arrays must be the same length 37 * @returns {number} dot product 38 * 39 * @example 40 * var Float64Array = require( '@stdlib/array/float64' ); 41 * var array = require( '@stdlib/ndarray/array' ); 42 * 43 * var x = array( new Float64Array( [ 4.0, 2.0, -3.0, 5.0, -1.0 ] ) ); 44 * var y = array( new Float64Array( [ 2.0, 6.0, -1.0, -4.0, 8.0 ] ) ); 45 * 46 * var z = ddot( x, y ); 47 * // returns -5.0 48 */ 49 function ddot( x, y ) { 50 if ( !isFloat64VectorLike( x ) ) { 51 throw new TypeError( 'invalid argument. First argument must be a 1-dimensional ndarray containing double-precision floating-point numbers (i.e., an ndarray whose underlying data buffer is a Float64Array). Value: `' + x + '`.' ); 52 } 53 if ( !isFloat64VectorLike( y ) ) { 54 throw new TypeError( 'invalid argument. Second argument must be a 1-dimensional ndarray containing double-precision floating-point numbers (i.e., an ndarray whose underlying data buffer is a Float64Array). Value: `' + y + '`.' ); 55 } 56 if ( x.length !== y.length ) { 57 throw new RangeError( 'invalid argument. Arrays must be the same length. First argument length: ' + x.length + '. Second argument length: ' + y.length + '.' ); 58 } 59 return dot( x.length, x.data, x.strides[ 0 ], x.offset, y.data, y.strides[ 0 ], y.offset ); // eslint-disable-line max-len 60 } 61 62 63 // EXPORTS // 64 65 module.exports = ddot;