Skip to content

Instantly share code, notes, and snippets.

@cuongdev
Created April 13, 2018 14:22
Show Gist options
  • Save cuongdev/662690f37720e3b9e4589eb5c25f60a8 to your computer and use it in GitHub Desktop.
Save cuongdev/662690f37720e3b9e4589eb5c25f60a8 to your computer and use it in GitHub Desktop.
cosine
/**
* Tính cosine similarity two vector using UJMP lib
*
* @param m1
* @param m2
* @return
*/
public static double getCosineSimilartiy(double[] m1, double[] m2) {
double aiSum = 0;
double a2Sum = 0;
double b2Sum = 0;
int i;
for (i = m1.length; --i >= 0; ) {
double a = m1[i];
double b = m2[i];
aiSum += a * b;
a2Sum += a * a;
b2Sum += b * b;
}
return aiSum / (Math.sqrt(a2Sum) * Math.sqrt(b2Sum));
}
/**
* Tính cosine similarity two vector using UJMP lib
*
* @param m1
* @param m2
* @return
*/
public static double getCosineSimilartiyMultiThread(double[] m1, double[] m2) {
double aiSum = 0;
double a2Sum = 0;
double b2Sum = 0;
int i;
int cores = Runtime.getRuntime().availableProcessors() - 1;
ExecutorService exe = Executors.newFixedThreadPool(cores);
int numChunk = (int) Math.ceil(m1.length / (double) cores);
List<Callable<double[]>> task = new ArrayList<>();
for (i = 0; i < cores; i++) {
task.add(new DoAsync(m1, m2, numChunk * i, numChunk * (i + 1)));
}
try {
List<Future<double[]>> futures = exe.invokeAll(task);
for (Future<double[]> f : futures) {
double[] d = f.get();
aiSum += d[0];
a2Sum += d[1];
b2Sum += d[2];
}
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
exe.shutdownNow();
return aiSum / (Math.sqrt(a2Sum) * Math.sqrt(b2Sum));
}
static class DoAsync implements Callable<double[]> {
double[] m1;
double[] m2;
int offset;
int limit;
public DoAsync(double[] m1, double[] m2, int offset, int limit) {
this.m1 = m1;
this.m2 = m2;
this.offset = offset;
this.limit = limit;
}
@Override
public double[] call() throws Exception {
double[] result = new double[]{
0.0, 0.0, 0.0
};
for (int i = offset; i < limit && i < m1.length; i++) {
result[0] += m1[i] * m2[i];
result[1] += m1[i] * m1[i];
result[2] += m2[i] * m2[i];
}
return result;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment