UNPKG

xen-dev-utils

Version:

Utility functions used by the Scale Workshop ecosystem

62 lines 1.86 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.norm = exports.dotPrecise = exports.dot = void 0; const sum_precise_1 = require("./polyfills/sum-precise"); /** * Calculate the inner (dot) product of two arrays of real numbers. * @param a The first array of numbers. * @param b The second array of numbers. * @returns The dot product. */ function dot(a, b) { const numComponents = Math.min(a.length, b.length); let result = 0; for (let i = 0; i < numComponents; ++i) { result += a[i] * b[i]; } return result; } exports.dot = dot; /** * Calculate the inner (dot) product of two arrays of real numbers. * The resulting terms are summed accurately using Shewchuk's algorithm. * @param a The first array of numbers. * @param b The second array of numbers. * @returns The dot product. */ function dotPrecise(a, b) { const numComponents = Math.min(a.length, b.length); function* terms() { for (let i = 0; i < numComponents; ++i) { yield a[i] * b[i]; } } return (0, sum_precise_1.sum)(terms()); } exports.dotPrecise = dotPrecise; /** * Calculate the norm (vector length) of an array of real numbers. * @param array The array to measure. * @param type Type of measurement. * @returns The length of the vector. */ function norm(array, type = 'euclidean') { let result = 0; for (let i = 0; i < array.length; ++i) { if (type === 'taxicab') { result += Math.abs(array[i]); } else if (type === 'maximum') { result = Math.max(result, Math.abs(array[i])); } else { result += array[i] * array[i]; } } if (type === 'euclidean') { return Math.sqrt(result); } return result; } exports.norm = norm; //# sourceMappingURL=number-array.js.map