parent
11e4a67708
commit
a71e01cca7
@ -0,0 +1,82 @@
|
|||||||
|
/**
|
||||||
|
* Calculates the Euclidean distance between two points in n-dimensional space.
|
||||||
|
*
|
||||||
|
* @param {number[]} a - The first point, represented as an array of numbers.
|
||||||
|
* @param {number[]} b - The second point, represented as an array of numbers.
|
||||||
|
* @returns {number} The Euclidean distance between the two points.
|
||||||
|
*/
|
||||||
|
function euclideanDistance(a, b) {
|
||||||
|
return Math.sqrt(a.reduce((acc, val, i) => acc + (val - b[i]) ** 2, 0));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Calculates the Manhattan distance (L1) between two points in n-dimensional space.
|
||||||
|
*
|
||||||
|
* @param {number[]} a - The first point, represented as an array of numbers.
|
||||||
|
* @param {number[]} b - The second point, represented as an array of numbers.
|
||||||
|
* @returns {number} The Manhattan distance between the two points.
|
||||||
|
*/
|
||||||
|
function manhattanDistance(a, b) {
|
||||||
|
return a.reduce((acc, val, i) => acc + Math.abs(val - b[i]), 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Calculates the Maximum distance (L∞) between two points in n-dimensional space.
|
||||||
|
*
|
||||||
|
* @param {number[]} a - The first point, represented as an array of numbers.
|
||||||
|
* @param {number[]} b - The second point, represented as an array of numbers.
|
||||||
|
* @returns {number} The Maximum distance between the two points.
|
||||||
|
*/
|
||||||
|
function maximumDistance(a, b) {
|
||||||
|
return Math.max(...a.map((val, i) => Math.abs(val - b[i])));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Calculates the Minkowski distance (Lp) between two points in n-dimensional space.
|
||||||
|
*
|
||||||
|
* @param {number[]} a - The first point, represented as an array of numbers.
|
||||||
|
* @param {number[]} b - The second point, represented as an array of numbers.
|
||||||
|
* @param {number} p - The order of the Minkowski distance.
|
||||||
|
* @returns {number} The Minkowski distance between the two points.
|
||||||
|
*/
|
||||||
|
function minkowskiDistance(a, b, p) {
|
||||||
|
return Math.pow(a.reduce((acc, val, i) => acc + Math.pow(Math.abs(val - b[i]), p), 0), 1 / p);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Calculates the quadratic distance between two points in n-dimensional space.
|
||||||
|
*
|
||||||
|
* @param {number[]} a - The first point, represented as an array of numbers.
|
||||||
|
* @param {number[]} b - The second point, represented as an array of numbers.
|
||||||
|
* @returns {number} The quadratic distance between the two points.
|
||||||
|
*/
|
||||||
|
function quadraticDistance(a, b) {
|
||||||
|
return a.reduce((acc, val, i) => acc + (val - b[i]) ** 2, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Calculates the angle between two points in n-dimensional space.
|
||||||
|
*
|
||||||
|
* @param {number[]} a - The first point, represented as an array of numbers.
|
||||||
|
* @param {number[]} b - The second point, represented as an array of numbers.
|
||||||
|
* @returns {number} The angle between the two points in radians.
|
||||||
|
*/
|
||||||
|
function angleDistance(a, b) {
|
||||||
|
const dotProduct = a.reduce((acc, val, i) => acc + val * b[i], 0);
|
||||||
|
const magnitudeA = Math.sqrt(a.reduce((acc, val) => acc + val ** 2, 0));
|
||||||
|
const magnitudeB = Math.sqrt(b.reduce((acc, val) => acc + val ** 2, 0));
|
||||||
|
|
||||||
|
return Math.acos(dotProduct / (magnitudeA * magnitudeB));
|
||||||
|
}
|
||||||
|
|
||||||
|
const distanceFunctions = {
|
||||||
|
'euclidean': euclideanDistance,
|
||||||
|
'manhattan': manhattanDistance,
|
||||||
|
'maximum': maximumDistance,
|
||||||
|
'minkowski': minkowskiDistance,
|
||||||
|
'quadratic': quadraticDistance,
|
||||||
|
'angle': angleDistance
|
||||||
|
};
|
||||||
|
|
||||||
|
//export { euclideanDistance, manhattanDistance, maximumDistance, minkowskiDistance, quadraticDistance, angleDistance };
|
||||||
|
module.exports = distanceFunctions;
|
||||||
@ -0,0 +1,29 @@
|
|||||||
|
/**
|
||||||
|
* Calculates the centroid of an array of entries.
|
||||||
|
* The centroid is the point that is the mean of all the points in the array.
|
||||||
|
* The mean is calculated using the weight of each point, which defaults to 1.
|
||||||
|
* @param {Object[]} entries - The array of entries to calculate the centroid from.
|
||||||
|
* @param {function} [distanceFunction] - A distance function to use to calculate the weight of each point.
|
||||||
|
* @returns {number[]} The centroid of the array of entries.
|
||||||
|
*/
|
||||||
|
function calculateCentroid(entries, distanceFunction) {
|
||||||
|
if (entries.length === 0)
|
||||||
|
return;
|
||||||
|
|
||||||
|
const length = entries[0].point.length;
|
||||||
|
const sum = new Array(length).fill(0);
|
||||||
|
let totalWeight = 0;
|
||||||
|
|
||||||
|
for (const entry of entries) {
|
||||||
|
const weight = entry.radius || 1;
|
||||||
|
//const weight = distanceFunction(entry.point, this.point);
|
||||||
|
for (let i = 0; i < length; i++) {
|
||||||
|
sum[i] += entry.point[i] * weight;
|
||||||
|
}
|
||||||
|
totalWeight += weight;
|
||||||
|
}
|
||||||
|
|
||||||
|
return sum.map(val => val / totalWeight);
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = { calculateCentroid };
|
||||||
Loading…
Reference in new issue