Package dev.nm.misc.datastructure
Class MathTable
- java.lang.Object
-
- dev.nm.misc.datastructure.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.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description classMathTable.RowA row is indexed by a number and contains multiple values.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidaddRow(double index, double[] values)Adds a row to the table.voidaddRows(double[][] data)Adds rows by adouble[][].doubleget(double i, int j)Gets a particular table entry at [i,j].doubleget(double i, String header)Gets a particular table entry at [i, "header"].String[]getHeaders()Gets the column names.double[]getIndices()Gets a copy of the row indices.MathTable.RowgetRowOnOrAfter(double i)Gets the row corresponding to a row index.MathTable.RowgetRowOnOrBefore(double i)Gets the row corresponding to a row index.Iterator<MathTable.Row>getRowsOnOrAfter(double i)Gets the rows having the row index value equal to or just bigger thani.Iterator<MathTable.Row>getRowsOnOrBefore(double i)Gets the rows having the row index value equal to or just smaller thani.intnColumns()Gets the number of columns 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 indexvalues- the row values
-
addRows
public void addRows(double[][] data)
Adds rows by adouble[][]. 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} };represents1.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- adouble[][]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- ifiis 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- ifiis 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 thani. The returnedIteratorallows iterating the rows in reversed order starting from the matching row.- Parameters:
i- the row index- Returns:
- an
IteratorofRows 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 thani. The returnedIteratorallows iterating the rows starting from the matching row.- Parameters:
i- the row index- Returns:
- an
IteratorofRows 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 toi, 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 indexj- a column index, counting from 1- Returns:
- the value at [i,j]
- Throws:
NoSuchElementException- ifrowValueis 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 toi, 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 indexheader- the column name- Returns:
- the value at [i, "header"]
- Throws:
NoSuchElementException- ifiis outside the table range
-
-