All files / blas/tools/dot-factory/lib main.js

98.05% Statements 202/206
90.62% Branches 29/32
100% Functions 3/3
98.05% Lines 202/206

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 2071x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 43x 43x 43x 9x 9x 43x 9x 9x 43x 43x 43x 43x 43x 43x 43x 43x 43x 43x 43x 154x 154x 43x 43x 43x 43x 43x 43x 43x 43x 43x 43x 43x 43x 43x 43x 43x 43x 43x 43x 43x 87x 87x 87x 87x 87x 87x 87x 87x 87x 87x 87x 87x 87x 87x 87x 87x 87x 87x 20x 20x 87x 20x 20x 47x 47x 47x 47x 47x 47x 47x 47x 47x 87x     87x     47x 87x 37x 37x 24x 24x 87x 10x 10x 23x 23x 23x 87x 8x 8x 15x 15x 87x 2x 2x 13x 13x 13x 87x 1x 1x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 87x 8x 8x 8x 8x 4x 4x 4x 4x 4x 87x 16x 16x 16x 16x 16x 4x 87x 43x 1x 1x 1x 1x 1x  
/**
* @license Apache-2.0
*
* Copyright (c) 2024 The Stdlib Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*    http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
 
'use strict';
 
// MODULES //
 
var isFunction = require( '@stdlib/assert/is-function' );
var isndarrayLikeWithDataType = require( '@stdlib/assert/is-ndarray-like-with-data-type' );
var isndarrayLike = require( '@stdlib/assert/is-ndarray-like' );
var isNegativeInteger = require( '@stdlib/assert/is-negative-integer' ).isPrimitive;
var isDataType = require( '@stdlib/ndarray/base/assert/is-data-type' );
var min = require( '@stdlib/math/base/special/fast/min' );
var without = require( '@stdlib/array/base/without' );
var numel = require( '@stdlib/ndarray/base/numel' );
var normalizeIndex = require( '@stdlib/ndarray/base/normalize-index' );
var maybeBroadcastArrays = require( '@stdlib/ndarray/base/maybe-broadcast-arrays' );
var ndarraylike2ndarray = require( '@stdlib/ndarray/base/ndarraylike2ndarray' );
var nditerStacks = require( '@stdlib/ndarray/iter/stacks' );
var empty = require( '@stdlib/ndarray/empty' );
var format = require( '@stdlib/string/format' );
 
 
// MAIN //
 
/**
* Returns a function which computes the dot product of two floating-point vectors.
*
* @param {Function} base - "base" function which computes the dot product
* @param {(String|null)} dtype - array data type
* @throws {TypeError} first argument must be a function
* @throws {TypeError} second argument must be a data type
* @returns {Function} function wrapper
*
* @example
* var Float64Array = require( '@stdlib/array/float64' );
* var array = require( '@stdlib/ndarray/array' );
* var ddot = require( '@stdlib/blas/base/ddot' ).ndarray;
*
* var dot = factory( ddot, 'float64' );
*
* var x = array( new Float64Array( [ 4.0, 2.0, -3.0, 5.0, -1.0 ] ) );
* var y = array( new Float64Array( [ 2.0, 6.0, -1.0, -4.0, 8.0 ] ) );
*
* var z = dot( x, y );
* // returns <ndarray>
*
* var v = z.get();
* // returns -5.0
*/
function factory( base, dtype ) {
	var isValid;
	if ( !isFunction( base ) ) {
		throw new TypeError( format( 'invalid argument. First argument must be a function. Value: `%s`.', base ) );
	}
	if ( !isDataType( dtype ) && dtype !== null ) {
		throw new TypeError( format( 'invalid argument. Second argument must be a data type. Value: `%s`.', dtype ) );
	}
	isValid = ( dtype ) ? isValidWrapper : isndarrayLike;
	return dot;
 
	/**
	* Tests if an input value is an ndarray-like object having a specified data type.
	*
	* @private
	* @param {*} value - value to test
	* @returns {boolean} boolean indicating if an input value is an ndarray-like object having a specified data type
	*/
	function isValidWrapper( value ) {
		return isndarrayLikeWithDataType( value, dtype );
	}
 
	/**
	* Computes the dot product.
	*
	* @private
	* @param {ndarrayLike} x - first input array
	* @param {ndarrayLike} y - second input array
	* @param {NegativeInteger} [dim=-1] - dimension for which to compute the dot product
	* @throws {TypeError} first argument must be an ndarray
	* @throws {TypeError} first argument must have at least one dimension
	* @throws {TypeError} second argument must be an ndarray
	* @throws {TypeError} second argument must have at least one dimension
	* @throws {TypeError} third argument must be a negative integer
	* @throws {Error} input arrays must be broadcast compatible
	* @throws {RangeError} the size of the contracted dimension must be the same for both input arrays
	* @throws {RangeError} third argument is out-of-bounds
	* @returns {ndarray} ndarray containing the dot product
	*/
	function dot( x, y ) {
		var dim;
		var xsh;
		var ysh;
		var osh;
		var xit;
		var yit;
		var out;
		var tmp;
		var xc;
		var yc;
		var vx;
		var vy;
		var dm;
		var S;
		var v;
		var i;
 
		if ( !isValid( x ) ) {
			throw new TypeError( format( 'invalid argument. First argument must be an ndarray-like object having a supported data type. Value: `%s`.', x ) );
		}
		if ( !isValid( y ) ) {
			throw new TypeError( format( 'invalid argument. Second argument must be an ndarray-like object having a supported data type. Value: `%s`.', y ) );
		}
		// Convert the input arrays to "base" ndarrays:
		xc = ndarraylike2ndarray( x );
		yc = ndarraylike2ndarray( y );
 
		// Resolve the input array shapes:
		xsh = xc.shape;
		ysh = yc.shape;
 
		// Validate that we've been provided non-zero-dimensional arrays...
		if ( xsh.length < 1 ) {
			throw new TypeError( format( 'invalid argument. First argument must have at least one dimension.' ) );
		}
		if ( ysh.length < 1 ) {
			throw new TypeError( format( 'invalid argument. Second argument must have at least one dimension.' ) );
		}
		// Validate that the dimension argument is a negative integer...
		if ( arguments.length > 2 ) {
			dim = arguments[ 2 ];
			if ( !isNegativeInteger( dim ) ) {
				throw new TypeError( format( 'invalid argument. Third argument must be a negative integer. Value: `%s`.', dim ) );
			}
		} else {
			dim = -1;
		}
		// Validate that a provided dimension index is within bounds **before** broadcasting...
		dm = min( xsh.length, ysh.length ) - 1;
		dim = normalizeIndex( dim, dm );
		if ( dim === -1 ) {
			throw new RangeError( format( 'invalid argument. Third argument must be a value on the interval: [%d,%d]. Value: `%d`.', -dm, -1, arguments[ 2 ] ) );
		}
		// Validate that the contracted dimension size is the same for both input arrays...
		S = xsh[ dim ];
		if ( ysh[ dim ] !== S ) {
			throw new RangeError( format( 'invalid argument. The size of the contracted dimension must be the same for both input ndarrays. Dim(%s,%d) = %d. Dim(%s,%d) = %d.', 'x', dim, S, 'y', dim, ysh[ dim ] ) );
		}
		// Broadcast the input arrays to a common shape....
		try {
			tmp = maybeBroadcastArrays( [ xc, yc ] );
		} catch ( err ) { // eslint-disable-line no-unused-vars
			throw new Error( format( 'invalid arguments. Input ndarrays must be broadcast compatible. Shape(%s) = (%s). Shape(%s) = (%s).', 'x', xsh.join( ',' ), 'y', ysh.join( ',' ) ) );
		}
		xc = tmp[ 0 ];
		yc = tmp[ 1 ];
 
		// Resolve the output array shape by excluding the contracted dimension:
		osh = without( xc.shape, dim );
 
		// Allocate an empty output array:
		out = empty( osh, {
			'dtype': xc.dtype,
			'order': xc.order
		});
 
		// If we are only provided one-dimensional input arrays, we can skip iterating over stacks...
		if ( osh.length === 0 ) {
			v = base( S, xc.data, xc.strides[0], xc.offset, yc.data, yc.strides[0], yc.offset ); // eslint-disable-line max-len
			out.iset( v );
			return out;
		}
		// Create iterators for iterating over stacks of vectors:
		xit = nditerStacks( xc, [ dim ] );
		yit = nditerStacks( yc, [ dim ] );
 
		// Compute the dot product for each pair of vectors...
		for ( i = 0; i < numel( osh ); i++ ) {
			vx = xit.next().value;
			vy = yit.next().value;
			v = base( S, vx.data, vx.strides[0], vx.offset, vy.data, vy.strides[0], vy.offset ); // eslint-disable-line max-len
			out.iset( i, v );
		}
		return out;
	}
}
 
 
// EXPORTS //
 
module.exports = factory;