public static Matrix plusInplace(Matrix mat, double etat) { int nrows = mat.getNumRows(); int ncols = mat.getNumColumns(); for (int r = 0; r < nrows; r++) { for (int c = 0; c < ncols; c++) { mat.setElement(r, c, mat.getElement(r, c) + etat); } } return mat; }
public static double colSparcity(Matrix mat) { double ncols = mat.getNumColumns(); double nsparse = 0; for (int c = 0; c < ncols; c++) { if (mat.getColumn(c).sum() == 0) { nsparse++; } } return nsparse / ncols; }
public static double absSum(Matrix mat) { double tot = 0; int nrows = mat.getNumRows(); int ncols = mat.getNumColumns(); for (int r = 0; r < nrows; r++) { for (int c = 0; c < ncols; c++) { tot += Math.abs(mat.getElement(r, c)); } } return tot; }
public static Matrix abs(Matrix mat) { Matrix ret = mat.clone(); int nrows = ret.getNumRows(); int ncols = ret.getNumColumns(); for (int r = 0; r < nrows; r++) { for (int c = 0; c < ncols; c++) { ret.setElement(r, c, Math.abs(mat.getElement(r, c))); } } return ret; }
@Test public void testMatlabFile() throws IOException { MatlabFileDataGenerator gen = new MatlabFileDataGenerator(matfile); int nusers = -1; int nwords = -1; int ntasks = -1; for (int i = 0; i < gen.size(); i++) { Pair<Matrix> XY = gen.generate(); Matrix X = XY.firstObject(); Matrix Y = XY.secondObject(); if (nusers == -1) { nusers = X.getNumColumns(); nwords = X.getNumRows(); ntasks = Y.getNumColumns(); } else { assertTrue(nusers == X.getNumColumns()); assertTrue(nwords == X.getNumRows()); assertTrue(ntasks == Y.getNumColumns()); } } }
/** * Creates a new {@code MatrixBasedTermSimilarityNetwork}. * * @param termIndex The index of terms that contains the nodes of the network. * @param similarities The square matrix of similarities between terms. Must have a number of rows * and columns equal to the number of terms in the term index. */ public MatrixBasedTermSimilarityNetwork(final TermIndex termIndex, final Matrix similarities) { super(); if (similarities.getNumRows() != termIndex.getTermCount() || similarities.getNumColumns() != termIndex.getTermCount()) { throw new DimensionalityMismatchException( "the number of terms in the term index must match the " + "dimensions of the square similarities matrix"); } this.setTermIndex(termIndex); this.setSimilarities(similarities); }
public static Vector diag(Matrix mat) { Vector ret; if (mat.getNumColumns() > mat.getNumRows()) { ret = mat.getRow(0); } else { ret = mat.getColumn(0); } int rowcol = ret.getDimensionality(); for (int rc = 0; rc < rowcol; rc++) { ret.setElement(rc, mat.getElement(rc, rc)); } return ret; }
public static Matrix vstack(MatrixFactory<? extends Matrix> matrixFactory, Matrix... matricies) { int nrows = 0; int ncols = 0; for (Matrix matrix : matricies) { nrows += matrix.getNumRows(); ncols = matrix.getNumColumns(); } Matrix ret = matrixFactory.createMatrix(nrows, ncols); int currentRow = 0; for (Matrix matrix : matricies) { ret.setSubMatrix(currentRow, 0, matrix); currentRow += matrix.getNumRows(); } return ret; }
public double sumLoss( List<Pair<Matrix>> pairs, Matrix u, Matrix w, Matrix bias, BilinearLearnerParameters params) { LossFunction loss = params.getTyped(BilinearLearnerParameters.LOSS); loss = new MatLossFunction(loss); double total = 0; int i = 0; int ntasks = 0; for (Pair<Matrix> pair : pairs) { Matrix X = pair.firstObject(); Matrix Y = pair.secondObject(); SparseMatrix Yexp = BilinearSparseOnlineLearner.expandY(Y); Matrix expectedAll = u.transpose().times(X.transpose()).times(w); loss.setY(Yexp); loss.setX(expectedAll); if (bias != null) loss.setBias(bias); logger.debug("Testing pair: " + i); total += loss.eval(null); // Assums an identity w. i++; ntasks += Y.getNumColumns(); } total /= ntasks; return Math.sqrt(total); }
// Note this method isn't static, because we want to override to make // it parallelized --krdixon protected void normalizeTransitionMatrix(Matrix A) { final int k = A.getNumColumns(); for (int j = 0; j < k; j++) { normalizeTransitionMatrix(A, j); } }