List of usage examples for org.apache.commons.math.linear RealMatrix getNorm
double getNorm();
From source file:net.sf.jtmt.similarity.matrix.JaccardSimilarity.java
@Override public double computeSimilarity(RealMatrix source, RealMatrix target) { double intersection = 0.0D; for (int i = 0; i < source.getRowDimension(); i++) { intersection += Math.min(source.getEntry(i, 0), target.getEntry(i, 0)); }/*from w w w. ja va 2 s.c om*/ if (intersection > 0.0D) { double union = source.getNorm() + target.getNorm() - intersection; return intersection / union; } else { return 0.0D; } }
From source file:net.sf.jtmt.similarity.matrix.CosineSimilarity.java
/** * Dot.//from w ww .ja va 2 s . co m * * @param source the source * @param target the target * @return the double */ private double dot(RealMatrix source, RealMatrix target) { int maxRows = source.getRowDimension(); int maxCols = source.getColumnDimension(); RealMatrix dotProduct = new OpenMapRealMatrix(maxRows, maxCols); for (int row = 0; row < maxRows; row++) { for (int col = 0; col < maxCols; col++) { dotProduct.setEntry(row, col, source.getEntry(row, col) * target.getEntry(row, col)); } } return dotProduct.getNorm(); }
From source file:net.sf.jtmt.similarity.matrix.Searcher.java
/** * Gets the query matrix./*from w w w . j a va 2s . c o m*/ * * @param query the query * @return the query matrix */ private RealMatrix getQueryMatrix(String query) { RealMatrix queryMatrix = new OpenMapRealMatrix(terms.size(), 1); String[] queryTerms = query.split("\\s+"); for (String queryTerm : queryTerms) { int termIdx = 0; for (String term : terms) { if (queryTerm.equalsIgnoreCase(term)) { queryMatrix.setEntry(termIdx, 0, 1.0D); } termIdx++; } } queryMatrix = queryMatrix.scalarMultiply(1 / queryMatrix.getNorm()); return queryMatrix; }