@Test(expected = Test.None.class) public void testHarwell() throws Exception { System.out.println("HB exchange format"); FloatSparseMatrix data = FloatSparseMatrix.harwell(smile.util.Paths.getTestData("matrix/5by5_rua.hb")); assertEquals(13, data.size()); assertEquals(5, data.nrows()); assertEquals(5, data.ncols()); assertEquals(11.0, data.get(0, 0), 1E-7); assertEquals(31.0, data.get(2, 0), 1E-7); assertEquals(51.0, data.get(4, 0), 1E-7); assertEquals(55.0, data.get(4, 4), 1E-7); }
public static FloatSparseMatrix harwell(Path path) throws IOException { logger.info("Reads sparse matrix file '{}'", path.toAbsolutePath()); try (InputStream stream = Files.newInputStream(path); Scanner scanner = new Scanner(stream)) { String line = scanner.nextLine(); logger.info(line); line = scanner.nextLine().trim(); logger.info(line); String[] tokens = line.split("\\s+"); int RHSCRD = Integer.parseInt(tokens[4]); line = scanner.nextLine().trim(); logger.info(line); if (!line.startsWith("R")) { throw new UnsupportedOperationException("SparseMatrix supports only real-valued matrix."); } tokens = line.split("\\s+"); int nrows = Integer.parseInt(tokens[1]); int ncols = Integer.parseInt(tokens[2]); int nz = Integer.parseInt(tokens[3]); line = scanner.nextLine(); logger.info(line); if (RHSCRD > 0) { line = scanner.nextLine(); logger.info(line); } int[] colIndex = new int[ncols + 1]; int[] rowIndex = new int[nz]; float[] data = new float[nz]; for (int i = 0; i <= ncols; i++) { colIndex[i] = scanner.nextInt() - 1; } for (int i = 0; i < nz; i++) { rowIndex[i] = scanner.nextInt() - 1; } for (int i = 0; i < nz; i++) { data[i] = scanner.nextFloat(); } return new FloatSparseMatrix(nrows, ncols, data, rowIndex, colIndex); } }
FloatSparseMatrix extends SMatrix implements Iterable<FloatSparseMatrix.Entry> { public static FloatSparseMatrix harwell(Path path) throws IOException { logger.info("Reads sparse matrix file '{}'", path.toAbsolutePath()); try (InputStream stream = Files.newInputStream(path); Scanner scanner = new Scanner(stream)) { String line = scanner.nextLine(); logger.info(line); line = scanner.nextLine().trim(); logger.info(line); String[] tokens = line.split("\\s+"); int RHSCRD = Integer.parseInt(tokens[4]); line = scanner.nextLine().trim(); logger.info(line); if (!line.startsWith("R")) { throw new UnsupportedOperationException("SparseMatrix supports only real-valued matrix."); } tokens = line.split("\\s+"); int nrows = Integer.parseInt(tokens[1]); int ncols = Integer.parseInt(tokens[2]); int nz = Integer.parseInt(tokens[3]); line = scanner.nextLine(); logger.info(line); if (RHSCRD > 0) { line = scanner.nextLine(); logger.info(line); } int[] colIndex = new int[ncols + 1]; int[] rowIndex = new int[nz]; float[] data = new float[nz]; for (int i = 0; i <= ncols; i++) { colIndex[i] = scanner.nextInt() - 1; } for (int i = 0; i < nz; i++) { rowIndex[i] = scanner.nextInt() - 1; } for (int i = 0; i < nz; i++) { data[i] = scanner.nextFloat(); } return new FloatSparseMatrix(nrows, ncols, data, rowIndex, colIndex); } } }
FloatSparseMatrix extends SMatrix implements Iterable<FloatSparseMatrix.Entry> { public static FloatSparseMatrix harwell(Path path) throws IOException { logger.info("Reads sparse matrix file '{}'", path.toAbsolutePath()); try (InputStream stream = Files.newInputStream(path); Scanner scanner = new Scanner(stream)) { String line = scanner.nextLine(); logger.info(line); line = scanner.nextLine().trim(); logger.info(line); String[] tokens = line.split("\\s+"); int RHSCRD = Integer.parseInt(tokens[4]); line = scanner.nextLine().trim(); logger.info(line); if (!line.startsWith("R")) { throw new UnsupportedOperationException("SparseMatrix supports only real-valued matrix."); } tokens = line.split("\\s+"); int nrows = Integer.parseInt(tokens[1]); int ncols = Integer.parseInt(tokens[2]); int nz = Integer.parseInt(tokens[3]); line = scanner.nextLine(); logger.info(line); if (RHSCRD > 0) { line = scanner.nextLine(); logger.info(line); } int[] colIndex = new int[ncols + 1]; int[] rowIndex = new int[nz]; float[] data = new float[nz]; for (int i = 0; i <= ncols; i++) { colIndex[i] = scanner.nextInt() - 1; } for (int i = 0; i < nz; i++) { rowIndex[i] = scanner.nextInt() - 1; } for (int i = 0; i < nz; i++) { data[i] = scanner.nextFloat(); } return new FloatSparseMatrix(nrows, ncols, data, rowIndex, colIndex); } } private  FloatSparseMatrix(int m, int n, int nvals);  FloatSparseMatrix(int m, int n, float[] nonzeros, int[] rowIndex, int[] colIndex);  FloatSparseMatrix(float[][] A);  FloatSparseMatrix(float[][] A, float tol); }
FloatSparseMatrix extends SMatrix implements Iterable<FloatSparseMatrix.Entry> { public static FloatSparseMatrix harwell(Path path) throws IOException { logger.info("Reads sparse matrix file '{}'", path.toAbsolutePath()); try (InputStream stream = Files.newInputStream(path); Scanner scanner = new Scanner(stream)) { String line = scanner.nextLine(); logger.info(line); line = scanner.nextLine().trim(); logger.info(line); String[] tokens = line.split("\\s+"); int RHSCRD = Integer.parseInt(tokens[4]); line = scanner.nextLine().trim(); logger.info(line); if (!line.startsWith("R")) { throw new UnsupportedOperationException("SparseMatrix supports only real-valued matrix."); } tokens = line.split("\\s+"); int nrows = Integer.parseInt(tokens[1]); int ncols = Integer.parseInt(tokens[2]); int nz = Integer.parseInt(tokens[3]); line = scanner.nextLine(); logger.info(line); if (RHSCRD > 0) { line = scanner.nextLine(); logger.info(line); } int[] colIndex = new int[ncols + 1]; int[] rowIndex = new int[nz]; float[] data = new float[nz]; for (int i = 0; i <= ncols; i++) { colIndex[i] = scanner.nextInt() - 1; } for (int i = 0; i < nz; i++) { rowIndex[i] = scanner.nextInt() - 1; } for (int i = 0; i < nz; i++) { data[i] = scanner.nextFloat(); } return new FloatSparseMatrix(nrows, ncols, data, rowIndex, colIndex); } } private  FloatSparseMatrix(int m, int n, int nvals);  FloatSparseMatrix(int m, int n, float[] nonzeros, int[] rowIndex, int[] colIndex);  FloatSparseMatrix(float[][] A);  FloatSparseMatrix(float[][] A, float tol); @Override FloatSparseMatrix clone(); @Override int nrows(); @Override int ncols(); @Override long size(); Stream<Entry> nonzeros(); Stream<Entry> nonzeros(int beginColumn, int endColumn); @Override Iterator<Entry> iterator(); Iterator<Entry> iterator(int beginColumn, int endColumn); void forEachNonZero(DoubleConsumer consumer); void forEachNonZero(int beginColumn, int endColumn, FloatConsumer consumer); float get(int index); float set(int index, float value); @Override float get(int i, int j); @Override FloatSparseMatrix set(int i, int j, float x); @Override void mv(Transpose trans, float alpha, float[] x, float beta, float[] y); @Override void mv(float[] work, int inputOffset, int outputOffset); @Override void tv(float[] work, int inputOffset, int outputOffset); FloatSparseMatrix transpose(); FloatSparseMatrix mm(FloatSparseMatrix B); FloatSparseMatrix ata(); FloatSparseMatrix aat(); @Override float[] diag(); static FloatSparseMatrix harwell(Path path); static FloatSparseMatrix rutherford(Path path); static FloatSparseMatrix text(Path path); }
FloatSparseMatrix extends SMatrix implements Iterable<FloatSparseMatrix.Entry> { public static FloatSparseMatrix harwell(Path path) throws IOException { logger.info("Reads sparse matrix file '{}'", path.toAbsolutePath()); try (InputStream stream = Files.newInputStream(path); Scanner scanner = new Scanner(stream)) { String line = scanner.nextLine(); logger.info(line); line = scanner.nextLine().trim(); logger.info(line); String[] tokens = line.split("\\s+"); int RHSCRD = Integer.parseInt(tokens[4]); line = scanner.nextLine().trim(); logger.info(line); if (!line.startsWith("R")) { throw new UnsupportedOperationException("SparseMatrix supports only real-valued matrix."); } tokens = line.split("\\s+"); int nrows = Integer.parseInt(tokens[1]); int ncols = Integer.parseInt(tokens[2]); int nz = Integer.parseInt(tokens[3]); line = scanner.nextLine(); logger.info(line); if (RHSCRD > 0) { line = scanner.nextLine(); logger.info(line); } int[] colIndex = new int[ncols + 1]; int[] rowIndex = new int[nz]; float[] data = new float[nz]; for (int i = 0; i <= ncols; i++) { colIndex[i] = scanner.nextInt() - 1; } for (int i = 0; i < nz; i++) { rowIndex[i] = scanner.nextInt() - 1; } for (int i = 0; i < nz; i++) { data[i] = scanner.nextFloat(); } return new FloatSparseMatrix(nrows, ncols, data, rowIndex, colIndex); } } private  FloatSparseMatrix(int m, int n, int nvals);  FloatSparseMatrix(int m, int n, float[] nonzeros, int[] rowIndex, int[] colIndex);  FloatSparseMatrix(float[][] A);  FloatSparseMatrix(float[][] A, float tol); @Override FloatSparseMatrix clone(); @Override int nrows(); @Override int ncols(); @Override long size(); Stream<Entry> nonzeros(); Stream<Entry> nonzeros(int beginColumn, int endColumn); @Override Iterator<Entry> iterator(); Iterator<Entry> iterator(int beginColumn, int endColumn); void forEachNonZero(DoubleConsumer consumer); void forEachNonZero(int beginColumn, int endColumn, FloatConsumer consumer); float get(int index); float set(int index, float value); @Override float get(int i, int j); @Override FloatSparseMatrix set(int i, int j, float x); @Override void mv(Transpose trans, float alpha, float[] x, float beta, float[] y); @Override void mv(float[] work, int inputOffset, int outputOffset); @Override void tv(float[] work, int inputOffset, int outputOffset); FloatSparseMatrix transpose(); FloatSparseMatrix mm(FloatSparseMatrix B); FloatSparseMatrix ata(); FloatSparseMatrix aat(); @Override float[] diag(); static FloatSparseMatrix harwell(Path path); static FloatSparseMatrix rutherford(Path path); static FloatSparseMatrix text(Path path);  }