List of usage examples for org.apache.commons.math.linear OpenMapRealMatrix OpenMapRealMatrix
public OpenMapRealMatrix(int rowDimension, int columnDimension)
From source file:net.sf.jtmt.similarity.matrix.AbstractSimilarity.java
public RealMatrix transform(RealMatrix termDocumentMatrix) { int numDocs = termDocumentMatrix.getColumnDimension(); RealMatrix similarityMatrix = new OpenMapRealMatrix(numDocs, numDocs); for (int i = 0; i < numDocs; i++) { RealMatrix sourceDocMatrix = termDocumentMatrix.getSubMatrix(0, termDocumentMatrix.getRowDimension() - 1, i, i); for (int j = 0; j < numDocs; j++) { RealMatrix targetDocMatrix = termDocumentMatrix.getSubMatrix(0, termDocumentMatrix.getRowDimension() - 1, j, j); similarityMatrix.setEntry(i, j, computeSimilarity(sourceDocMatrix, targetDocMatrix)); }//from w ww .ja va2 s. c o m } return similarityMatrix; }
From source file:net.sf.jtmt.similarity.matrix.CosineSimilarity.java
/** * Dot./* w w w. j a v a2s.c o 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 ww .ja va 2s . co 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; }
From source file:net.sf.jtmt.clustering.Cluster.java
/** * Gets the centroid.//from ww w . j a v a 2 s . c o m * * @return the centroid */ public RealMatrix getCentroid() { if (docs.size() == 0) { return null; } RealMatrix d = docs.get(docNames.get(0)); centroid = new OpenMapRealMatrix(d.getRowDimension(), d.getColumnDimension()); for (String docName : docs.keySet()) { RealMatrix docMatrix = docs.get(docName); centroid = centroid.add(docMatrix); } centroid = centroid.scalarMultiply(1.0D / docs.size()); return centroid; }
From source file:org.interpss.core.sparse.UCTE2000SparseMatrixCasesTest.java
private SparseRealMatrix sparseMatrix2Ary(SparseEqnMatrix2x2 eqn) { int n = eqn.getDimension(); System.out.println("J-matrix Demension:" + n); //RealMatrix m = new Array2DRowRealMatrix( n, n ); SparseRealMatrix m = new OpenMapRealMatrix(n, n); int n_2 = n / 2; for (int i = 0; i < n_2; i++) { // index 1-N for (int j = 0; j < n_2; j++) {//index 1-N Matrix_xy mxy = eqn.getA(i + 1, j + 1); if (mxy.xx != 0.0) m.setEntry(2 * i, 2 * j, mxy.xx); if (mxy.xy != 0.0) m.setEntry(2 * i, 2 * j + 1, mxy.xy); if (mxy.yx != 0.0) m.setEntry(2 * i + 1, 2 * j, mxy.yx); if (mxy.yy != 0.0) m.setEntry(2 * i + 1, 2 * j + 1, mxy.yy); }//w w w . jav a2 s. co m } return m; }