class Entropy {
public static void main(String[] args) {
final int arraySize = 1024*1024*64;
double[] p = new double[arraySize];
for (int i = 0; i < arraySize; i++) {
p[i] = 1.0 - Math.random();
}
final long frequency = info.yeppp.Library.getTimerFrequency();
final long startTimeNaive = info.yeppp.Library.getTimerTicks();
final double entropyNaive = computeEntropyNaive(p);
final long endTimeNaive = info.yeppp.Library.getTimerTicks();
final long startTimeYeppp = info.yeppp.Library.getTimerTicks();
final double entropyYeppp = computeEntropyYeppp(p);
final long endTimeYeppp = info.yeppp.Library.getTimerTicks();
System.out.println("Naive implementation:");
System.out.println(String.format("\tEntropy = %f", entropyNaive));
System.out.println(String.format("\tTime = %f secs", ulongToDouble(endTimeNaive - startTimeNaive) / ulongToDouble(frequency)));
System.out.println("Yeppp! implementation:");
System.out.println(String.format("\tEntropy = %f", entropyYeppp));
System.out.println(String.format("\tTime = %f secs", ulongToDouble(endTimeYeppp - startTimeYeppp) / ulongToDouble(frequency)));
}
private static double computeEntropyNaive(final double[] probabilities) {
double entropy = 0.0;
for (int i = 0; i < probabilities.length; i++) {
final double p = probabilities[i];
entropy -= p * Math.log(p);
}
return entropy;
}
private static double computeEntropyYeppp(final double[] p) {
double entropy = 0.0;
final int blockSize = 1000;
double[] logP = new double[blockSize];
for (int index = 0; index < p.length; index += blockSize) {
int blockLength = Math.min(blockSize, p.length - index);
info.yeppp.Math.Log_V64f_V64f(p, index, logP, 0, blockLength);
final double dotProduct = info.yeppp.Core.DotProduct_V64fV64f_S64f(p, index, logP, 0, blockLength);
entropy -= dotProduct;
}
return entropy;
}
private static double ulongToDouble(long n) {
return (double)(n & 0x7FFFFFFFFFFFFFFFL) - (double)(n & 0x8000000000000000L);
}
}