Java Matrix matrixKLDivergence(double m1[][], double m2[][])

Here you can find the source of matrixKLDivergence(double m1[][], double m2[][])

Description

matrix KL Divergence

License

Open Source License

Declaration

public static double matrixKLDivergence(double m1[][], double m2[][]) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

public class Main {
    public static double matrixKLDivergence(double m1[][], double m2[][]) {
        if (m1.length != m2.length)
            return Double.MAX_VALUE;
        double avgKL = 0.0;
        for (int i = 0; i < m1.length; i++) {
            if (m1[i].length != m2[i].length)
                return Double.MAX_VALUE;
            avgKL += vectorKLDivergence(m1[i], m2[i]);
        }/*from w w w .ja  va  2s  . c o  m*/
        return avgKL / m1.length;
    }

    public static double vectorKLDivergence(double v1[], double v2[]) {
        if (v1.length != v2.length)
            return Double.MAX_VALUE;
        double kl = 0.0;
        for (int i = 0; i < v1.length; i++) {
            kl += v1[i] * Math.log(v1[i] / v2[i]);
        }
        return kl;
    }
}

Related

  1. matrixConcatUL(double[][] UMatrix, double[][] LMatrix)
  2. matrixDestructAdd(double[][] m1, double[][] m2)
  3. matrixDeterminant(final float[] m, final int m_offset)
  4. matrixEquals(int[][] firstMatrix, int[][] secondMatrix)
  5. matrixIsWellformed(int numLabels, int[][] matrix)
  6. matrixModule(double[][] matrix, int module)
  7. matrixTimesVector(double[][] matrix, double[] vector)