/** * Returns an empty matrix of the same underlying class as the receiver and of the specified * size. * * @param rows the int number of rows * @param columns the int number of columns */ @Override public Matrix like(int rows, int columns) { return new SparseRowMatrix(rows, columns); }
@Override public Matrix like(int rows, int columns) { return new SparseRowMatrix(rows, columns, randomAccessRows); }
@Override public Matrix transpose() { SparseRowMatrix srm = new SparseRowMatrix(columns, rows); for (int i = 0; i < columns; i++) { Vector col = columnVectors[i]; if (col.getNumNonZeroElements() > 0) // this should already be optimized srm.assignRow(i, col); } return srm; }
/** * Return an empty matrix of the same underlying class as the receiver * * @return a Matrix */ @Override public Matrix like() { return new SparseRowMatrix(rowSize(), columnSize()); }
/** * Subclasses must override to return an appropriately sparse or dense result * * @param rows the row cardinality * @param columns the column cardinality * @return a Matrix */ @Override protected Matrix matrixLike(int rows, int columns) { if (vector.isDense()) { return new DenseMatrix(rows, columns); } else { return new SparseRowMatrix(rows, columns); } }
@Override public Matrix like() { return new SparseRowMatrix(rowSize(), columnSize(), randomAccessRows); }
rowVectors[row] = VectorWritable.readVector(in, vectorFlags, columns); matrix = new SparseRowMatrix(rows, columns, rowVectors, true, !sequential); } else { matrix = new SparseMatrix(rows, columns);
@Test public void testSparseRowMatrixWritable() throws Exception { Matrix m = new SparseRowMatrix(5, 5); m.set(1, 2, 3.0); m.set(3, 4, 5.0); Map<String, Integer> bindings = new HashMap<>(); bindings.put("A", 0); bindings.put("B", 1); bindings.put("C", 2); bindings.put("D", 3); bindings.put("default", 4); m.setRowLabelBindings(bindings); m.setColumnLabelBindings(bindings); doTestMatrixWritableEquals(m); }
@Override public Matrix matrixFactory(double[][] values) { Matrix matrix = new SparseRowMatrix(values.length, values[0].length); for (int row = 0; row < matrix.rowSize(); row++) { for (int col = 0; col < matrix.columnSize(); col++) { matrix.setQuick(row, col, values[row][col]); } } return matrix; }
@Test public void testBigMatrix() throws IOException { // only run this test if -DrunSlowTests is used. Also requires 4GB or more of heap. Assume.assumeNotNull(System.getProperty("runSlowTests")); Matrix m0 = new SparseRowMatrix(ROWS, COLUMNS); Random gen = RandomUtils.getRandom(); for (int i = 0; i < 1000; i++) { m0.set(gen.nextInt(ROWS), gen.nextInt(COLUMNS), matrixValue(i)); } File f = File.createTempFile("foo", ".m", getTestTempDir()); f.deleteOnExit(); System.out.printf("Starting to write to %s\n", f.getAbsolutePath()); FileBasedMatrix.writeMatrix(f, m0); System.out.printf("done\n"); System.out.printf("File is %.1f MB\n", f.length() / 1.0e6); FileBasedMatrix m1 = new FileBasedMatrix(ROWS, COLUMNS); System.out.printf("Starting read\n"); m1.setData(f, false); gen = RandomUtils.getRandom(); for (int i = 0; i < 1000; i++) { assertEquals(matrixValue(i), m1.get(gen.nextInt(ROWS), gen.nextInt(COLUMNS)), 0.0); } System.out.printf("done\n"); }
int entriesPerRow, double entryMean) { Matrix m = new SparseRowMatrix(numRows, numCols);
Matrix x = new SparseRowMatrix(1000, 2000, false); for (int i = 0; i < 1000; i++) { int[] values = new int[1000]; Matrix y = new SparseRowMatrix(2000, 1000, false); for (int i = 0; i < 2000; i++) { int[] values = new int[1000];
@Test public void testSetData() throws IOException { File f = File.createTempFile("matrix", ".m", getTestTempDir()); f.deleteOnExit(); Random gen = RandomUtils.getRandom(); Matrix m0 = new SparseRowMatrix(10, 21); for (MatrixSlice row : m0) { int len = (int) Math.ceil(-15 * Math.log(1 - gen.nextDouble())); for (int i = 0; i < len; i++) { row.vector().set(gen.nextInt(21), 1); } } FileBasedSparseBinaryMatrix.writeMatrix(f, m0); FileBasedSparseBinaryMatrix m = new FileBasedSparseBinaryMatrix(10, 21); m.setData(f); for (MatrixSlice row : m) { Vector diff = row.vector().minus(m0.viewRow(row.index())); double error = diff.norm(1); if (error > 1.0e-14) { System.out.printf("%s\n", diff); } assertEquals(0, error, 1.0e-14); } } }
Matrix x = new SparseRowMatrix(1000, 2000, false); for (int i = 0; i < 1000; i++) { int[] values = new int[1000];
@Test(timeout=50000) public void testTimesOtherSparseEfficiency() { Random raw = RandomUtils.getRandom(); Gamma gen = new Gamma(0.1, 0.1, raw); // build a sequential sparse matrix and a diagonal matrix and multiply them Matrix x = new SparseRowMatrix(1000, 2000, false); for (int i = 0; i < 1000; i++) { int[] values = new int[1000]; for (int k = 0; k < 1000; k++) { int j = (int) Math.min(1000, gen.nextDouble()); values[j]++; } for (int j = 0; j < 1000; j++) { if (values[j] > 0) { x.set(i, j, values[j]); } } } Vector d = new DenseVector(2000).assign(Functions.random()); Matrix y = new DiagonalMatrix(d); long t0 = System.nanoTime(); Matrix z = x.times(y); double elapsedTime = (System.nanoTime() - t0) * 1e-6; System.out.printf("done in %.1f ms\n", elapsedTime); for (MatrixSlice row : z) { for (Vector.Element element : row.nonZeroes()) { assertEquals(x.get(row.index(), element.index()) * d.get(element.index()), element.get(), 1e-12); } } }
@Test(timeout=50000) public void testTimesCorrect() { Random raw = RandomUtils.getRandom(); // build two large sequential sparse matrices and multiply them Matrix x = new SparseRowMatrix(100, 2000, false) .assign(Functions.random()); Matrix y = new SparseRowMatrix(2000, 100, false) .assign(Functions.random()); Matrix xd = new DenseMatrix(100, 2000).assign(x); Matrix yd = new DenseMatrix(2000, 100).assign(y); assertEquals(0, xd.times(yd).minus(x.times(y)).aggregate(Functions.PLUS, Functions.ABS), 1e-15); assertEquals(0, x.times(yd).minus(x.times(y)).aggregate(Functions.PLUS, Functions.ABS), 1e-15); assertEquals(0, xd.times(y).minus(x.times(y)).aggregate(Functions.PLUS, Functions.ABS), 1e-15); } }
public void train(Vector document, Vector docTopicCounts, boolean update, int numDocTopicIters) { while (true) { try { workQueue.put(new TrainerRunnable(readModel, update ? writeModel : null, document, docTopicCounts, new SparseRowMatrix(numTopics, numTerms, true), numDocTopicIters)); return; } catch (InterruptedException e) { log.warn("Interrupted waiting to submit document to work queue: {}", document, e); } } }
public void trainSync(Vector document, Vector docTopicCounts, boolean update, int numDocTopicIters) { new TrainerRunnable(readModel, update ? writeModel : null, document, docTopicCounts, new SparseRowMatrix(numTopics, numTerms, true), numDocTopicIters).run(); }
public void trainSync(Vector document, Vector docTopicCounts, boolean update, int numDocTopicIters) { new TrainerRunnable(readModel, update ? writeModel : null, document, docTopicCounts, new SparseRowMatrix(numTopics, numTerms, true), numDocTopicIters).run(); }
@Override public Matrix matrixFactory(double[][] values) { Matrix matrix = new SparseRowMatrix(values.length, values[0].length); for (int row = 0; row < matrix.rowSize(); row++) { for (int col = 0; col < matrix.columnSize(); col++) { matrix.setQuick(row, col, values[row][col]); } } return matrix; }