Class MatrixReader

java.lang.Object
org.moeaframework.analysis.io.MatrixReader
All Implemented Interfaces:
Closeable, AutoCloseable, Iterable<double[]>, Iterator<double[]>
Direct Known Subclasses:
MetricFileReader

public class MatrixReader extends Object implements Iterable<double[]>, Iterator<double[]>, Closeable
Reader of files containing matrices. A matrix contains numerical data separated into rows and columns. The values in a row are separated by whitespace.

The file can contain commented lines starting with '#' characters.

Parsing stops at the first error. Check the error flag to determine if an error was encountered. An exception may or may not be thrown, depending on the type of error and the value of the suppressExceptions flag. If exceptions are suppressed, a warning message will be printed.

  • Constructor Details

    • MatrixReader

      public MatrixReader(File file) throws FileNotFoundException
      Constructs a reader for loading a matrix contained in the specified file.
      Parameters:
      file - the file containing the matrix
      Throws:
      FileNotFoundException - if the file was not found
    • MatrixReader

      public MatrixReader(File file, int numberOfColumns) throws FileNotFoundException
      Constructs a reader for loading a matrix contained in the specified file.
      Parameters:
      file - the file containing the matrix
      numberOfColumns - the expected number of columns; or -1 if the matrix has no fixed column count
      Throws:
      FileNotFoundException - if the file was not found
    • MatrixReader

      public MatrixReader(Reader reader)
      Constructs a reader for loading a matrix accessed through the underlying reader.
      Parameters:
      reader - the underlying reader
    • MatrixReader

      public MatrixReader(Reader reader, int numberOfColumns)
      Constructs a reader for loading a matrix accessed through the underlying reader.
      Parameters:
      reader - the underlying reader
      numberOfColumns - the expected number of columns; or -1 if the matrix has no fixed column count
  • Method Details

    • load

      public static double[][] load(File file) throws FileNotFoundException, IOException
      Loads the content of the file into a matrix.
      Parameters:
      file - the file containing the matrix
      Returns:
      the matrix
      Throws:
      FileNotFoundException - if the file was not found
      IOException - if an I/O error occurred while reading the file
    • iterator

      public Iterator<double[]> iterator()
      Specified by:
      iterator in interface Iterable<double[]>
    • hasNext

      public boolean hasNext()
      Specified by:
      hasNext in interface Iterator<double[]>
    • next

      public double[] next()
      Specified by:
      next in interface Iterator<double[]>
    • remove

      public void remove()
      Specified by:
      remove in interface Iterator<double[]>
    • close

      public void close() throws IOException
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Closeable
      Throws:
      IOException
    • readAll

      public double[][] readAll()
      Reads the entire contents of the file.
      Returns:
      the entire contents of the file