List of usage examples for org.apache.commons.math.linear RealMatrix getFrobeniusNorm
double getFrobeniusNorm();
From source file:net.sf.jtmt.clustering.Cluster.java
/** * Gets the similarity.//from w w w . j a v a 2 s . c om * * @param doc the doc * @return the similarity */ public double getSimilarity(RealMatrix doc) { if (centroid != null) { double dotProduct = centroid.transpose().multiply(doc).getNorm(); double normProduct = centroid.getFrobeniusNorm() * doc.getFrobeniusNorm(); return dotProduct / normProduct; } return 0.0D; }
From source file:net.sf.jtmt.similarity.matrix.CosineSimilarity.java
@Override public double computeSimilarity(RealMatrix sourceDoc, RealMatrix targetDoc) { if (sourceDoc.getRowDimension() != targetDoc.getRowDimension() || sourceDoc.getColumnDimension() != targetDoc.getColumnDimension() || sourceDoc.getColumnDimension() != 1) { throw new IllegalArgumentException("Matrices are not column matrices or not of the same size"); }// www. j a v a 2s.c o m // max col sum, only 1 col, so... double dotProduct = dot(sourceDoc, targetDoc); // sqrt of sum of squares of all elements, only one col, so... double eucledianDist = sourceDoc.getFrobeniusNorm() * targetDoc.getFrobeniusNorm(); return dotProduct / eucledianDist; }