Package dev.nm.misc.datastructure
Class MathTable
java.lang.Object
dev.nm.misc.datastructure.MathTable
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 ClassesModifier and TypeClassDescriptionclass
A row is indexed by a number and contains multiple values. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoid
addRow
(double index, double[] values) Adds a row to the table.void
addRows
(double[][] data) Adds rows by adouble[][]
.double
get
(double i, int j) Gets a particular table entry at [i,j].double
Gets a particular table entry at [i, "header"].String[]
Gets the column names.double[]
Gets a copy of the row indices.getRowOnOrAfter
(double i) Gets the row corresponding to a row index.getRowOnOrBefore
(double i) Gets the row corresponding to a row index.getRowsOnOrAfter
(double i) Gets the rows having the row index value equal to or just bigger thani
.getRowsOnOrBefore
(double i) Gets the rows having the row index value equal to or just smaller thani
.int
nColumns()
Gets the number of columns in the table.
-
Constructor Details
-
MathTable
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 Details
-
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
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
- ifi
is smaller than the first row index
-
getRowOnOrAfter
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
- ifi
is bigger than the last row index value
-
getRowsOnOrBefore
Gets the rows having the row index value equal to or just smaller thani
. The returnedIterator
allows iterating the rows in reversed order starting from the matching row.- Parameters:
i
- the row index- Returns:
- an
Iterator
ofRow
s at or above the matching row
-
getRowsOnOrAfter
Gets the rows having the row index value equal to or just bigger thani
. The returnedIterator
allows iterating the rows starting from the matching row.- Parameters:
i
- the row index- Returns:
- an
Iterator
ofRow
s 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
- ifrowValue
is outside the table range
-
get
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
- ifi
is outside the table range
-