package com.objecteye.utils; import org.springframework.stereotype.Component; @Component public class CompareDistance { /** * 求两个double[]数组的欧式距离 * * @param vector1 数组1 * @param distance 归一化之后的距离 * @param vector2 数组2 * @param distance1 归一化之后的距离 * @return 距离 */ public double simDistance(double[] vector1, double distance, double[] vector2, double distance1) { double score = 0; for (int i = 0; i < vector1.length; i++) { score += vector1[i] * vector2[i]; } score = score / (distance * distance1); return score < 0 ? 0 : score; } /** * 数组归一化之后的距离 * * @param vector1 数组 * @return 归一化之后的距离 */ public double getDistance(double[] vector1) { double distance = 0; double maxa = 0; for (int j = 0; j < vector1.length; j++) { if (vector1[j] > maxa) { maxa = vector1[j]; } } for (int j = 0; j < vector1.length; j++) { vector1[j] = (vector1[j] / maxa); distance += vector1[j] * vector1[j]; // distance += (vector1[j] / maxa) * (vector1[j] / maxa); } return Math.sqrt(distance); } }