package utils.array;


import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.util.Arrays;

public class ArrayUtil {

	private static Logger log = LogManager.getLogger(ArrayUtil.class);

	public static void reverse(int[] array) {
		for (int i = 0; i < array.length / 2; i++) {
			int temp = array[i];
			array[i] = array[array.length - i - 1];
			array[array.length - i - 1] = temp;
		}
	}
    
	public static void reverse(double[] array) {
		for (int i = 0; i < array.length / 2; i++) {
			double temp = array[i];
			array[i] = array[array.length - i - 1];
			array[array.length - i - 1] = temp;
		}
	}

	public static double innerProduct(double[] x1, double[] x2) {

		double result = 0;
		for (int i = 0; i < x1.length; i++) {
			if (x1[i] == 0.0D || x2[i] == 0.0D) continue;
			result += x1[i] * x2[i];
		}

		return result;
	}

	public static double[] arraySubtract(double[] x1, double[] x2) {

		double[] result = new double[x1.length];
		for (int i = 0; i < x1.length; i++) {
			result[i] = x1[i] - x2[i];
		}
		return result;
	}

	public static double euclidean(double[] x1, double[] x2) {
		double[] x3 = arraySubtract(x1, x2);
		double result = 0;
		for (double x: x3) if (x != 0) result += x * x;
		return result;
	}
}

@@ -1317,11 +1317,216 @@
 t;%0D%0A%09%7D%0D%0A
+%0D%0A%0D%0A%09public static double KLDivergence(float%5B%5D p, float%5B%5D q) %7B%0D%0A%09%09double result = 0;%0D%0A%09%09for (int i = 0; i %3C p.length; i ++)%0D%0A%09%09%09if (p%5Bi%5D %3E 0) result += p%5Bi%5D * Math.log(p%5Bi%5D / q%5Bi%5D);%0D%0A%09%09return result;%0D%0A%09%7D%0D%0A
 %7D%0D%0A

add KL divergence