@@ -0,0 +1,1669 @@
+package test.blog;%0A%0Aimport blog.common.numerical.MatrixFactory;%0Aimport blog.common.numerical.MatrixLib;%0A%0A%0A/**%0A * Compare performance for two ways to compute the log determinant.%0A *%0A * This is a standalone program.%0A */%0Apublic class BenchmarkLogDet %7B%0A  public static void main(String%5B%5D args) %7B%0A    MatrixLib matrix = MatrixFactory.eye(400).timesScale(0.001);%0A    final int trials = 300;%0A    double result = 0;%0A%0A    // Using eigenvalue decomposition:%0A    long nanoStart = System.nanoTime();%0A    for (int t = 0; t %3C trials; t++) %7B%0A      result = computeLogDetUsingEigenDecomp(matrix);%0A    %7D%0A    double secPerTrialEigenDecomp = (System.nanoTime() - nanoStart) / (trials * 1e9);%0A    System.out.println(result);%0A    System.out.println(%22eigen decomp: %22 + secPerTrialEigenDecomp + %22 sec / trial%22);%0A%0A    // Using Cholesky decomposition:%0A    nanoStart = System.nanoTime();%0A    for (int t = 0; t %3C trials; t++) %7B%0A      result = computeLogDetUsingCholeskyDecomp(matrix);%0A    %7D%0A    double secPerTrialCholeskyDecomp = (System.nanoTime() - nanoStart) / (trials * 1e9);%0A    System.out.println(result);%0A    System.out.println(%22chol  decomp: %22 + secPerTrialCholeskyDecomp + %22 sec / trial%22);%0A  %7D%0A%0A  public static double computeLogDetUsingEigenDecomp(MatrixLib matrix) %7B%0A    double logDet = 0.0;%0A    for (double val : matrix.eigenvals()) %7B%0A      logDet += Math.log(val);%0A    %7D%0A    return logDet;%0A  %7D%0A%0A  public static double computeLogDetUsingCholeskyDecomp(MatrixLib matrix) %7B%0A    MatrixLib chol = matrix.choleskyFactor();%0A    double logDet = 0.0;%0A    for (int i = 0; i %3C chol.colLen(); i++) %7B%0A      logDet += Math.log(chol.elementAt(i, i));%0A    %7D%0A    logDet *= 2;%0A    return logDet;%0A  %7D%0A%7D%0A

benchmark two ways of computing the log determinant