FileBasedMatrix extends AbstractMatrix { public void setData(File f, boolean loadNow) throws IOException { Preconditions.checkArgument(f.length() == rows * columns * 8L, "File " + f + " is wrong length"); for (int i = 0; i < (rows + rowsPerBlock - 1) / rowsPerBlock; i++) { long start = i * rowsPerBlock * columns * 8L; long size = rowsPerBlock * columns * 8L; MappedByteBuffer buf = new FileInputStream(f).getChannel().map(FileChannel.MapMode.READ_ONLY, start, Math.min(f.length() - start, size)); if (loadNow) { buf.load(); } addData(buf.asDoubleBuffer()); } } FileBasedMatrix(int rows, int columns); void setData(File f, boolean loadNow); static void writeMatrix(File f, Matrix m); @Override Matrix assignColumn(int column, Vector other); @Override Matrix assignRow(int row, Vector other); @Override double getQuick(int row, int column); @Override Matrix like(); @Override Matrix like(int rows, int columns); @Override void setQuick(int row, int column, double value); @Override Matrix viewPart(int[] offset, int[] size);  }
@Test public void testSetData() throws IOException { File f = File.createTempFile("matrix", ".m", getTestTempDir()); f.deleteOnExit(); Matrix m0 = new DenseMatrix(100000, 30); MultiNormal gen = new MultiNormal(30); for (MatrixSlice row : m0) { row.vector().assign(gen.sample()); } FileBasedMatrix.writeMatrix(f, m0); FileBasedMatrix m = new FileBasedMatrix(100000, 30); m.setData(f, true); assertEquals(0, m0.minus(m).aggregate(Functions.MAX, Functions.ABS), 1.0e-8); int i = 0; for (MatrixSlice row : m) { assertEquals(0, row.vector().minus(m0.viewRow(i++)).norm(1), 1.0e-8); } }