Quantity Table with Absolute Quantities¶
Introduction¶
Absolute quantity tables are 2-dimensional tables with data of absolute quantities of the same type. They can be regarded as generic data containers, suitable for data interfaces or method input types. In a sense, it acts like an AbsMatrixNxM without the ability to carry out matrix and vector calculations, and without the Hadamard (entry-by-entry) operations.
The AbsQuantityTable supports dense storage in a double[] or float[] array, or sparse storage, where values are stored with an integer-based row-column index and a double or float value. Since the sparse storage involves quite some overhead, tables need to have a significant percentage of 0-values (40-50% or more) for using sparse storage to make sense.
Quantity table operations¶
The generic methods of an AbsQuantityTable are:
int rows()returns the number of rows of the quantity table.int cols()returns the number of columns of the quantity table.getDisplayUnit()returns the display unit of the entireAbsQuantityTable.setDisplayUnit(unit)sets a new display unit for the entireAbsQuantityTablebased on a strongly typedunit.setDisplayUnit(string)sets a new display unit for the entireAbsQuantityTablebased on aStringrepresentation of the unit.getReference()returns the reference point for all absolute quantities in theAbsQuantityTable.boolean isRelative()returns whether the underlyingAbsQuantityis relative or not. Note thatAbsQuantityTableonly stores absolute quantities.boolean isAbsolute()returns whether the underlyingAbsQuantityis absolute or not. Note thatAbsQuantityTableonly stores absolute quantities.transpose()returns a newAbsQuantityTablewhere the rows and columns are swapped.
Addition and subtraction of absolute vectors and matrices follow the rules for the absolute quantity:
absQuantityTable.add(quantity)returns a newabsQuantityTablewhere all entries of ofabsQuantityTablehave been increased by the (relative) quantity. ThedisplayUnitandreferenceof the resulting absolute vector are taken fromabsQuantityTable.absQuantityTable1.add(relQuantityTable2)returns a newabsQuantityTablewhere all entries ofrelQuantityTable2have been added to the corresponding entries ofabsQuantityTable1. ThedisplayUnitandreferenceof the resulting absolute vector are taken fromabsQuantityTable1. The number of rows and columns ofabsQuantityTable1andrelQuantityTable2have to be equal, of course.absQuantityTable.subtract(quantity)returns a newabsQuantityTablewhere all entries of ofabsQuantityTablehave been decreased by the (relative) quantity. ThedisplayUnitandreferenceof the resulting absolute vector are taken fromabsQuantityTable.absQuantityTable1.subtract(relQuantityTable2)returns a newabsQuantityTablewhere all entries ofrelQuantityTable2have been subtracted from the corresponding entries ofabsQuantityTable1. ThedisplayUnitandreferenceof the resulting absolute vector are taken fromabsQuantityTable1. The number of rows and columns ofabsQuantityTable1andrelQuantityTable2have to be equal, of course.absQuantityTable.subtract(absQuantity)returns a new relativeQuantityTablewhere all entries of ofabsQuantityTablehave been decreased by the provided absolute quantity. ThedisplayUnitof the resulting relative vector is taken fromabsQuantityTable.absQuantityTable1.subtract(absQuantityTable2)returns a new relativeQuantityTablewhere all entries ofabsQuantityTable2have been subtracted from the corresponding entries ofabsQuantityTable1. ThedisplayUnitof the resulting absolute vector is taken fromabsQuantityTable1. The number of rows and columns ofabsQuantityTable1andabsQuantityTable2have to be equal, of course.
Obtaining values of quantity table entries¶
Several methods exist to get access to the entries of an AbsQuantityTable. When single entries, rows or columns are retrieved, two versions of the methods exist: a version where the row and column number are 0-based, and a version where the row and column number are 1-based. The 1-based methods have a name that starts with m for matrix, since the entry numbering of a matrix start with m11, and not with m00. So, there is an si(row, col) method where row ranges from 0 to table.rows()-1 and col ranges from 0 to table.cols()-1, and an msi(mRow, mCol) method where mRow ranges from 1 up to and including table.rows() and mCol ranges from 1 up to and including table.cols.
Quantity-based value methods return a value A that is consistent with the absolute quantity stored in the AbsQuantityTable. Suppose aqt is an AbsQuantityTable<Time, Duration>. The result of the operation aqt.get(1,3) will then be a strongly typed Time quantity. The letter A in the methods below indicates that strongly typed absolute quantity such as Time. The letter Q indicates the relative quantity belonging to the absolute quantity, such as Duration as the relative quantity for Time.
If the underlying relative quantity table is needed, where all values are of type Q and relative to the reference point of the absolute quantity table, the method getRelativeVecMat() can be used. As an example, for an AbsQuantityTable<Time, Duration>, the getRelativeVecMat() method returns a QuantityTable<Duration> with the same dimensions, SI-content, and display unit.
A AbsQuantityTable contains the following methods to obtain its values:
SI-based value methods¶
double[][] getSiGrid()returns a 2-dimensionaldouble[][]array with the SI-values of the entries in the quantity table. The SI-values are relative to the reference point of the quantity table.double[] getSiArray()returns the values of the quantity table in SI-units as a row-majordouble[]array with the same length as the quantity table. This means that for a quantity table with n rows and m columns, the data is stored as [a11, a12, ..., a1m, a21, a22, ..., a2m, ..., an1, an2, ..., anm]. The SI-values are relative to the reference point of the quantity table.double si(int row, int col)returns the SI-value of the entry at the 0-based row and column. The SI-value is relative to the reference point of the quantity table.double msi(int mRow, int mCol)returns the SI-value of the entry at the 1-based row indicated bymRowand 1-based column indicated bymCol. The SI-value is relative to the reference point of the quantity table.
Quantity-based value methods¶
A[][] getScalarGrid()returns a 2-dimensional strongly typed absolute quantity array that represents the quantity table. The absolute quantities in the array will all have the samedisplayUnitand reference point as the originalAbsQuantityTable.A[] getScalarArray()returns a 1-dimensional strongly typed row-major absolute quantity array that represents the quantity table. The absolute quantities in the array will all have the samedisplayUnitand reference point as the originalAbsQuantityTable.A get(int row, int col)returns the absolute quantity representation of the entry at the 0-based row and column. The returnedAbsQuantitywill have the samedisplayUnitand reference point as the originalAbsQuantityTable.A mget(int mRow, int mCol)returns the absolute quantity representation of the entry at the 1-based row indicated bymRowand 1-based column indicated bymCol. The returnedAbsQuantitywill have the samedisplayUnitand reference point as the originalAbsQuantityTable.getRelativeVecMat()returns the 'embedded' relative vector or matrix, whose values are relative to the reference point of theAbsVector. The size and type of the returned vector are congruent with the type of theAbsVector.
Retrieving quantity table rows¶
AbsVectorN.Row getRowVector(int row)retrieves the quantity table row at the 0-basedrowas a row-vector with the samedisplayUnitand reference point as the originalAbsQuantityTable.AbsVectorN.Row mgetRowVector(int mRow)retrieves the quantity table row at the 1-basedmRowas a row-vector with the samedisplayUnitand reference point as the originalAbsQuantityTable.A[] getRowScalars(int row)retrieves the quantity table row at the 0-basedrowas an array of quantities, where the quantities in the array have the samedisplayUnitand reference point as the original quantity table.A[] mgetRowScalars(int mRow)retrieves the quantity table row at the 1-basedmRowas an array of quantities, where the quantities in the array have the samedisplayUnitand reference point as the original matrix. Note that the resultingA[]array is 0-based.double[] getRowSi(int row)retrieves the quantity table row at the 0-basedrowas adouble[]array with SI-values. The values are relative to the reference point of the quantity table.double[] mgetRowSi(int mRow)retrieves the quantity table row at the 1-basedmRowas adouble[]array with SI-values. Note that the resultingdouble[]array is 0-based. The values are relative to the reference point of the quantity table.
Retrieving quantity table columns¶
AbsVectorN.Col getColumnVector(int col)retrieves the quantity table column at the 0-basedcolas a column-vector with the samedisplayUnitand reference point as the originalAbsQuantityTable.AbsVectorN.Col mgetColumnVector(int mCol)retrieves the quantity table column at the 1-basedmColas a column-vector with the samedisplayUnitand reference point as the originalAbsQuantityTable.A[] getColumnScalars(int col)retrieves the quantity table column at the 0-basedcolas an array of quantities, where the quantities in the array have the samedisplayUnitand reference point as the original quantity table.A[] mgetColumnScalars(int mCol)retrieves the quantity table column at the 1-basedmColas an array of quantities, where the quantities in the array have the samedisplayUnitand reference point as the original quantity table. Note that the resultingA[]array is 0-based.double[] getColumnSi(int col)retrieves the quantity table column at the 0-basedcolas adouble[]array with SI-values. The values are relative to the reference point of the quantity table.double[] mgetColumnSi(int mCol)retrieves the quantity table column at the 1-basedmColas adouble[]array with SI-values. Note that the resultingdouble[]array is 0-based. The values are relative to the reference point of the quantity table.
Mathematical operations for AbsQuantityTable¶
A AbsQuantityTable implements several mathematical operations. The most important ones are:
A mean()returns the mean quantity value of the entries of theAbsQuantityTableas a strongly typedAbsQuantity.A min()returns the minimum quantity value of the entries of theAbsQuantityTableas a strongly typedAbsQuantity.A max()returns the maximum quantity value of the entries of theAbsQuantityTableas a strongly typedAbsQuantity.A median()returns the median quantity value of the entries of theAbsQuantityTableas a strongly typedAbsQuantity. The median value is the value of the middle entry when all entries have been sorted on their SI-values. When the number of entries in the quantity table is even, the average of the two values that together make up the middle is returned.
Transforming the AbsQuantityTable¶
AbsQuantityTable objects do not implement matrix operations such as determinant, matrix multiplication, etc. If an AbsQuantityTable at some point needs to be used for matrix operations, the asVector and asMatrix methods can transform the AbsQuantityTable into an AbsMatrix or column or row AbsVector of any of the types. For this, the AbsQuantityTable implements the asAbsMatrix1x1(), asAbsMatrix2x2(), asAbsMatrix3x3(), asAbsMatrixNxN(), asAbsMatrixNxM(), asAbsVector1(), asAbsVector2Row(), asAbsVector2Col(), asAbsVector3Row(), asAbsVector3Col(), asAbsVectorNRow(), and asAbsVectorNCol() methods. These methods will check the consistency of the quantity table size with the desired vector or matrix type at runtime.
Reversely, AbsMatrix or column or row AbsVector instances can all be turned into an AbsQuantityTable with the asAbsQuantityTable() method.
Creating an AbsQuantityTable¶
The AbsQuantityTable class can be used to represent quantity tables of any size (1x1 and up). Data can be stored as single-precision float variable, or as double-precision double values. Both dense storage (store every number) and sparse storage (only store non-zero values) are possible.
Several methods exist to instantiate an AbsQuantityTable.
The DataGridSi-based methods store the data in the dataGridSi object, which can be DenseDoubleDataSi, SparseDoubleDataSi, DenseFloatDataSi, or SparseFloatDataSi. These objects are instantiated through one of their of(), ofSi() or constructor methods. For some of and ofSi methods, the number of rows and columns of the quantity table need to be provided for the DataGridSi object to know the shape of the quantity table. A double[6] array of SI values can represent a 2x3 quantity table, but also a 3x2 quantity table or a 1x6 or 6x1 quantity table. All four shapes can be stored in the DataGridSi object by providing the number of rows and columns.
The array-based methods use a row-major array. This means that the data is presented "row-by-row", so, {m11, m12, m13, m21, m22, m23} for a 2x3 quantity table. A (r,c) value is retrieved by m[index], index = r * cols() + c where r, c are 0-based indices.
The grid-based methods count the rows in the 'outer' (first) array [r][], and the columns in the 'inner' second array [][c]. A (r,c)value is retrieved by m[r][c]. 'Ragged' grids are not allowed and result in an IllegalArgumentException.
new AbsQuantityTable<A, Q>(DataGridSi dataSi, Unit displayUnit, Reference reference)
creates anAbsQuantityTablebased on aDataGridSistorage object, a display unit and a reference point. More information can be found in the storage section.AbsQuantityTable.of(DataGridSi dataSi, Unit displayUnit, Reference reference)
creates anAbsQuantityTablebased on aDataGridSistorage object, a display unit and a reference point. More information can be found in the storage section.AbsQuantityTable.of(double[] dataInUnit, int rows, int cols, Unit unit, Reference reference)
creates anAbsQuantityTablebased on a row-major array with values expressed in the given unit. The data is relative to the provided reference point. The length of the array needs to be equal torows * cols.AbsQuantityTable.of(double[][] gridInUnit, Unit unit, Reference reference)
creates anAbsQuantityTablebased on a grid (array of arrays) with values expressed in the given unit. The data is relative to the provided reference point. The grid cannot be 'ragged'.AbsQuantityTable.ofSi(double[] dataSi, int rows, int cols, Unit displayUnit, Reference reference)
creates anAbsQuantityTablebased on a row-major array with SI-values for the quantities. The length of the array needs to be equal torows * cols.AbsQuantityTable.ofSi(double[][] gridSi, Unit displayUnit, Reference reference)
creates anAbsQuantityTablebased on a grid (array of arrays) with with SI-values for the quantities, a display unit and a reference point. The grid cannot be 'ragged'.AbsQuantityTable.of(Q[] data, int rows, int cols, Reference reference)
creates anAbsQuantityTablebased on a row-major array with quantities, and a reference point for the values. The length of the array needs to be equal torows * cols.AbsQuantityTable.of(Q[][] grid, Reference reference)
creates anAbsQuantityTablebased on a grid (array of arrays) with with quantities, and a reference point for the values. The grid cannot be 'ragged'.AbsQuantityTable.of(A[] data, int rows, int cols)
creates anAbsQuantityTablebased on a row-major array with absolute quantities. The length of the array needs to be equal torows * cols.AbsQuantityTable.of(A[][] grid)
creates anAbsQuantityTablebased on a grid (array of arrays) with with absolute quantities. The grid cannot be 'ragged'.AbsQuantityTable.of(QuantityTable<Q> relativeTable, Reference reference)
creates anAbsQuantityTablebased on a relative quantity table, and a reference point for the values.