Class MathTable


  • public class MathTable
    extends Object
    A mathematical table consists of numbers showing the results of calculation with varying arguments. It can be used to simplify and drastically speed up computation. We use them to archive results for, for example, quantiles of difficult distribution functions, often computed by slow Monte Carlo simulation.

    This implementation provides various ways of looking up a table, esp. when an index is not exactly found in the table.

    • Constructor Detail

      • MathTable

        public MathTable​(String... headers)
        Constructs an empty table by headers.
        Parameters:
        headers - the column names; they must be unique
      • MathTable

        public MathTable​(int nColumns)
        Constructs an empty table. The headers are given default names.
        Parameters:
        nColumns - the number of columns
    • Method Detail

      • getHeaders

        public String[] getHeaders()
        Gets the column names.
        Returns:
        the headers
      • nColumns

        public int nColumns()
        Gets the number of columns in the table.
        Returns:
        the number of columns
      • addRow

        public void addRow​(double index,
                           double[] values)
        Adds a row to the table.
        Parameters:
        index - the row index
        values - the row values
      • addRows

        public void addRows​(double[][] data)
        Adds rows by a double[][]. The first number in each row/double[] is the row index. For example,
         new double[][]{
         {1.0, 1.1, 1.2, 1.3},
         {2.0, 2.1, 2.2, 2.3},
         {3.0, 3.1, 3.2, 3.3},
         {4.0, 4.1, 4.2, 4.3}
         };
         
        represents
         1.0: {1.1, 1.2, 1.3}//row 1
         2.0: {2.1, 2.2, 2.3}//row 2
         3.0: {3.1, 3.2, 3.3}//row 3
         4.0: {4.1, 4.2, 4.3}//row 4
         
        Parameters:
        data - a double[][] of table entries.
      • getIndices

        public double[] getIndices()
        Gets a copy of the row indices.
        Returns:
        a copy of the row indices
      • getRowOnOrBefore

        public MathTable.Row getRowOnOrBefore​(double i)
        Gets the row corresponding to a row index. If there is no row matching the exact index, the row with the biggest index but smaller than the specified index is returned.
        Parameters:
        i - a row index
        Returns:
        the corresponding row
        Throws:
        NoSuchElementException - if i is smaller than the first row index
      • getRowOnOrAfter

        public MathTable.Row getRowOnOrAfter​(double i)
        Gets the row corresponding to a row index. If there is no row matching the exact value, the row with the smallest index value but bigger than the specified value is returned.
        Parameters:
        i - a row index
        Returns:
        the corresponding row
        Throws:
        NoSuchElementException - if i is bigger than the last row index value
      • getRowsOnOrBefore

        public Iterator<MathTable.Row> getRowsOnOrBefore​(double i)
        Gets the rows having the row index value equal to or just smaller than i. The returned Iterator allows iterating the rows in reversed order starting from the matching row.
        Parameters:
        i - the row index
        Returns:
        an Iterator of Rows at or above the matching row
      • getRowsOnOrAfter

        public Iterator<MathTable.Row> getRowsOnOrAfter​(double i)
        Gets the rows having the row index value equal to or just bigger than i. The returned Iterator allows iterating the rows starting from the matching row.
        Parameters:
        i - the row index
        Returns:
        an Iterator of Rows at or below the matching row
      • get

        public double get​(double i,
                          int j)
        Gets a particular table entry at [i,j]. If there is no matching row to i, by default, we use linear interpolation between the row above and below. If i is smaller than the first row index, we return the value at [1,j]. A subclass may override this behavior to customize interpolation.
        Parameters:
        i - a row index
        j - a column index, counting from 1
        Returns:
        the value at [i,j]
        Throws:
        NoSuchElementException - if rowValue is outside the table range
      • get

        public double get​(double i,
                          String header)
        Gets a particular table entry at [i, "header"]. If there is no matching row to i, by default, we use linear interpolation between the row above and below. If i is smaller than the first row index, we return the value at [1,"header"]. A subclass may override this behavior to customize interpolation.
        Parameters:
        i - a row value index
        header - the column name
        Returns:
        the value at [i, "header"]
        Throws:
        NoSuchElementException - if i is outside the table range