SuanShu, a Java numerical and statistical library

com.numericalmethod.suanshu.datastructure
Class MathTable

java.lang.Object
  extended by com.numericalmethod.suanshu.datastructure.MathTable

public class MathTable
extends java.lang.Object

This class represents a mathematical table of numbers. The rows are indexed by real numbers.


Nested Class Summary
 class MathTable.Row
          This class represents a row in a mathematical table.
 
Constructor Summary
MathTable(int nColumns)
          Construct an empty table with a fixed number of columns.
MathTable(java.lang.String... headers)
          Construct an empty table with headers.
 
Method Summary
 void addRow(double rowValue, double[] columnValues)
          Add a row to the table.
 void addRows(double[][] data)
          Add rows by a 2D double array, double[][].
 double get(double rowValue, int columnIndex)
          Get a particular table entry at [rowValue, columnIndex].
 double get(double rowValue, java.lang.String header)
          Get a particular table entry at [rowValue, "header"].
 MathTable.Row getRowOnOrAfter(double rowValue)
          Get the row corresponding to the row index value.
 MathTable.Row getRowOnOrBefore(double rowValue)
          Get the row corresponding to the row index value.
 java.util.Iterator<MathTable.Row> getRowsOnOrAfter(double rowValue)
          Get the rows having a row index value equal to or bigger than the specified rowValue.
 java.util.Iterator<MathTable.Row> getRowsOnOrBefore(double rowValue)
          Get the rows having a row index value equal to or smaller than the specified rowValue.
 java.lang.String[] headers()
          Get the set of the names of the columns.
 double[] keys()
          Get a copy of the set of row index values.
 int nColumns()
          Get the number of columns in the table.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

MathTable

public MathTable(java.lang.String... headers)
Construct an empty table with headers.

Parameters:
headers - the names of the columns; they must be unique

MathTable

public MathTable(int nColumns)
Construct an empty table with a fixed number of columns.

The headers will be given default names.

Parameters:
nColumns - the number of columns
Method Detail

headers

public java.lang.String[] headers()
Get the set of the names of the columns.

Returns:
the headers

nColumns

public int nColumns()
Get the number of columns in the table.

Returns:
the number of columns

addRow

public void addRow(double rowValue,
                   double[] columnValues)
Add a row to the table.

Parameters:
rowValue - the row index value
columnValues - the values in the row

addRows

public void addRows(double[][] data)
Add rows by a 2D double array, double[][]. The first element in each row/double[] is the index value for the row.

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.

keys

public double[] keys()
Get a copy of the set of row index values.

Returns:
the set of row index values

getRowOnOrBefore

public MathTable.Row getRowOnOrBefore(double rowValue)
Get the row corresponding to the row index value. If there is no row matching the exact value, the row with the biggest index value but smaller than the specified value is returned. If the specified value is smaller than the first row index value, null is returned.

Parameters:
rowValue - a row index value
Returns:
the corresponding row
Throws:
java.util.NoSuchElementException - if rowValue is smaller than the smallest row index value

getRowOnOrAfter

public MathTable.Row getRowOnOrAfter(double rowValue)
Get the row corresponding to the row index value. If there is no row matching the exact value, the row with the smallest index value but bigger than the specified value is returned. If the specified value is smaller than the first row index value, null is returned.

Parameters:
rowValue - a row index value
Returns:
the corresponding row
Throws:
java.util.NoSuchElementException - if rowValue is bigger than the biggest row index value

getRowsOnOrBefore

public java.util.Iterator<MathTable.Row> getRowsOnOrBefore(double rowValue)
Get the rows having a row index value equal to or smaller than the specified rowValue. The user can iterate the rows using the returned Iterator in reversed order starting from the matching row.

Parameters:
rowValue - the row index value
Returns:
an Iterator of rows at or above the matching row

getRowsOnOrAfter

public java.util.Iterator<MathTable.Row> getRowsOnOrAfter(double rowValue)
Get the rows having a row index value equal to or bigger than the specified rowValue. The user can iterate the rows using the returned Iterator starting from the matching row.

Parameters:
rowValue - the row index value
Returns:
an Iterator of rows at or below the matching row

get

public double get(double rowValue,
                  int columnIndex)
Get a particular table entry at [rowValue, columnIndex]. If there is no matching row to rowValue, by default, we use linear interpolation between the row above and below.

A subclass may override this behavior to provide customized interpolation.

Parameters:
rowValue - a row value index
columnIndex - a column index, counting from 1
Returns:
a particular table entry at [rowValue, columnIndex]
Throws:
java.util.NoSuchElementException - if rowValue is outside the table range

get

public double get(double rowValue,
                  java.lang.String header)
Get a particular table entry at [rowValue, "header"]. If there is no matching row to rowValue, by default, we use linear interpolation between the row above and below.

A subclass may override this behavior to provide customized interpolation.

Parameters:
rowValue - a row value index
header - the name of a column
Returns:
a particular table entry at [rowValue, "header"]
Throws:
java.util.NoSuchElementException - if rowValue is outside the table range

SuanShu, a Java numerical and statistical library

Copyright © 2011 Numerical Method Inc. Ltd. All Rights Reserved.