Class DoubleMatrixData
java.lang.Object
org.djunits.value.storage.Storage<DoubleMatrixData>
org.djunits.value.vdouble.matrix.data.DoubleMatrixData
- All Implemented Interfaces:
Serializable
,Cloneable
- Direct Known Subclasses:
DoubleMatrixDataDense
,DoubleMatrixDataSparse
Stores the data for a DoubleMatrix and carries out basic operations.
Copyright (c) 2013-2024 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved.
BSD-style license. See DJUNITS License.
- Author:
- Alexander Verbraeck, Peter Knoppers
- See Also:
-
Field Summary
-
Constructor Summary
ConstructorDescriptionDoubleMatrixData
(StorageType storageType) Construct a new DoubleMatrixData store. -
Method Summary
Modifier and TypeMethodDescriptionabstract DoubleMatrixData
assign
(DoubleFunction doubleFunction) Apply an operation to each cell.(package private) abstract DoubleMatrixData
assign
(DoubleFunction2 doubleFunction2, DoubleMatrixData right) Apply a binary operation on a cell by cell basis.protected static double[][]
checkRectangularAndNonNull
(double[][] values) Check that a 2D array of float is not null, not empty and not jagged; i.e. all rows have the same length.protected static <U extends Unit<U>,
S extends DoubleScalar<U, S>>
S[][]checkRectangularAndNonNull
(S[][] values) Check that a 2D array of float is not null, not empty and not jagged; i.e. all rows have the same length.protected void
checkSizes
(DoubleMatrixData other) Check the sizes of this data object and the other data object.int
cols()
Retrieve the column count.protected boolean
Compare contents of a dense and a sparse matrix.final DoubleMatrixData
decrementBy
(DoubleMatrixData decrement) Subtract a matrix from this matrix on a cell-by-cell basis.abstract DoubleMatrixData
divide
(DoubleMatrixData right) Divide two matrices on a cell-by-cell basis.final DoubleMatrixData
divideBy
(DoubleMatrixData right) Divide the values of a matrix by the values of another matrix on a cell-by-cell basis.boolean
abstract double[][]
Create and return a deep copy of the data in dense format.abstract double
getSI
(int row, int col) Retrieve one value from this data.int
hashCode()
final DoubleMatrixData
incrementBy
(DoubleMatrixData right) Add a matrix to this matrix on a cell-by-cell basis.static DoubleMatrixData
instantiate
(double[][] values, Scale scale, StorageType storageType) Instantiate a DoubleMatrixData with the right data type.static <U extends Unit<U>,
S extends DoubleScalar<U, S>>
DoubleMatrixDatainstantiate
(Collection<DoubleSparseValue<U, S>> values, int rows, int cols, StorageType storageType) Instantiate a DoubleMatrixData with the right data type.static <U extends Unit<U>,
S extends DoubleScalar<U, S>>
DoubleMatrixDatainstantiate
(S[][] values, StorageType storageType) Instantiate a DoubleMatrixData with the right data type.abstract DoubleMatrixData
minus
(DoubleMatrixData right) Subtract two matrices on a cell-by-cell basis.final DoubleMatrixData
multiplyBy
(DoubleMatrixData right) Multiply a matrix with the values of another matrix on a cell-by-cell basis.abstract DoubleMatrixData
plus
(DoubleMatrixData right) Add two matrices on a cell-by-cell basis.int
rows()
Retrieve the row count.abstract void
setSI
(int row, int col, double valueSI) Sets a value at the [row, col] point in the matrix.abstract DoubleMatrixData
times
(DoubleMatrixData right) Multiply two matrices on a cell-by-cell basis.abstract DoubleMatrixDataDense
toDense()
Return the data of this matrix in dense storage format.abstract DoubleMatrixDataSparse
toSparse()
Return the data of this matrix in sparse storage format.final double
zSum()
Compute and return the sum of the values of all cells of this matrix.Methods inherited from class org.djunits.value.storage.Storage
cardinality, copy, getStorageType, isDense, isSparse
-
Field Details
-
matrixSI
protected double[] matrixSIthe internal storage of the Matrix; can be sparse or dense. -
rows
protected int rowsthe number of rows of the matrix. -
cols
protected int colsthe number of columns of the matrix.
-
-
Constructor Details
-
DoubleMatrixData
DoubleMatrixData(StorageType storageType) Construct a new DoubleMatrixData store.- Parameters:
storageType
- StorageType; the data type
-
-
Method Details
-
instantiate
public static DoubleMatrixData instantiate(double[][] values, Scale scale, StorageType storageType) throws ValueRuntimeException Instantiate a DoubleMatrixData with the right data type. The double array is of the form d[rows][columns] so each value can be found with d[row][column].- Parameters:
values
- double[][]; the (SI) values to storescale
- Scale; the scale of the unit to use for conversion to SIstorageType
- StorageType; the data type to use- Returns:
- the DoubleMatrixData with the right data type
- Throws:
NullPointerException
- when values are null, or storageType is nullValueRuntimeException
- when values is ragged
-
instantiate
public static <U extends Unit<U>,S extends DoubleScalar<U, DoubleMatrixData instantiateS>> (Collection<DoubleSparseValue<U, S>> values, int rows, int cols, StorageType storageType) throws ValueRuntimeExceptionInstantiate a DoubleMatrixData with the right data type.- Type Parameters:
U
- the unit typeS
- the corresponding scalar type- Parameters:
values
- Collection<DoubleSparseValue<U, S>>; the (sparse [X, Y, SI]) values to storerows
- int; the number of rows of the matrixcols
- int; the number of columns of the matrixstorageType
- StorageType; the data type to use- Returns:
- the DoubleMatrixData with the right data type
- Throws:
NullPointerException
- when values are null, or storageType is nullValueRuntimeException
- when rows < 0 or cols < 0
-
instantiate
public static <U extends Unit<U>,S extends DoubleScalar<U, DoubleMatrixData instantiateS>> (S[][] values, StorageType storageType) throws ValueRuntimeException Instantiate a DoubleMatrixData with the right data type. The double array is of the form d[rows][columns] so each value can be found with d[row][column].- Type Parameters:
U
- the unit typeS
- the corresponding scalar type- Parameters:
values
- S[][]; the values to storestorageType
- StorageType; the data type to use- Returns:
- the DoubleMatrixData with the right data type
- Throws:
NullPointerException
- when values is null, or storageType is nullValueRuntimeException
- when values is ragged
-
rows
public int rows()Retrieve the row count.- Returns:
- int; the number of rows of the matrix
-
cols
public int cols()Retrieve the column count.- Returns:
- int; the number of columns of the matrix
-
toDense
Return the data of this matrix in dense storage format.- Returns:
- DoubleMatrixDataDense; the dense transformation of this data
-
toSparse
Return the data of this matrix in sparse storage format.- Returns:
- DoubleMatrixDataSparse; the sparse transformation of this data
-
getSI
public abstract double getSI(int row, int col) Retrieve one value from this data.- Parameters:
row
- int; the row number to get the value forcol
- int; the column number to get the value for- Returns:
- the value at the [row, col] point
-
setSI
public abstract void setSI(int row, int col, double valueSI) Sets a value at the [row, col] point in the matrix.- Parameters:
row
- int; the row number to set the value forcol
- int; the column number to set the value forvalueSI
- double; the value at the index
-
zSum
public final double zSum()Compute and return the sum of the values of all cells of this matrix.- Returns:
- double; the sum of the values of all cells
-
getDenseMatrixSI
public abstract double[][] getDenseMatrixSI()Create and return a deep copy of the data in dense format. The double array is of the form d[rows][columns] so each value can be found with d[row][column].- Returns:
- double[][]; a safe, dense copy of matrixSI as a matrix
-
checkRectangularAndNonNull
protected static double[][] checkRectangularAndNonNull(double[][] values) throws ValueRuntimeException Check that a 2D array of float is not null, not empty and not jagged; i.e. all rows have the same length.- Parameters:
values
- double[][]; the 2D array to check- Returns:
- the values in case the method is used in a constructor
- Throws:
NullPointerException
- whenvalues
is nullValueRuntimeException
- whenvalues
is jagged
-
checkRectangularAndNonNull
protected static <U extends Unit<U>,S extends DoubleScalar<U, S[][] checkRectangularAndNonNullS>> (S[][] values) throws ValueRuntimeException Check that a 2D array of float is not null, not empty and not jagged; i.e. all rows have the same length.- Type Parameters:
U
- the unit typeS
- the corresponding scalar type- Parameters:
values
- S[][]; the 2D array to check- Returns:
- the values in case the method is used in a constructor
- Throws:
NullPointerException
- whenvalues
is nullValueRuntimeException
- whenvalues
is jagged
-
checkSizes
Check the sizes of this data object and the other data object.- Parameters:
other
- DoubleMatrixData; the other data object- Throws:
ValueRuntimeException
- if matrices have different lengths
-
assign
Apply an operation to each cell.- Parameters:
doubleFunction
- DoubleFunction; the operation to apply- Returns:
- DoubleMatrixData; this (modified) double matrix data object
-
assign
abstract DoubleMatrixData assign(DoubleFunction2 doubleFunction2, DoubleMatrixData right) throws ValueRuntimeException Apply a binary operation on a cell by cell basis.- Parameters:
doubleFunction2
- DoubleFunction2; the binary operation to applyright
- DoubleMatrixData; the right operand for the binary operation- Returns:
- DoubleMatrixData; this (modified) double matrix data object
- Throws:
ValueRuntimeException
- when the sizes of the matrices do not match
-
plus
Add two matrices on a cell-by-cell basis. If both matrices are sparse, a sparse matrix is returned, otherwise a dense matrix is returned.- Parameters:
right
- DoubleMatrixData; the other data object to add- Returns:
- the sum of this data object and the other data object
- Throws:
ValueRuntimeException
- if matrices have different lengths
-
incrementBy
Add a matrix to this matrix on a cell-by-cell basis. The type of matrix (sparse, dense) stays the same.- Parameters:
right
- DoubleMatrixData; the other data object to add- Returns:
- DoubleMatrixData; this modified double matrix data object
- Throws:
ValueRuntimeException
- if matrices have different lengths
-
minus
Subtract two matrices on a cell-by-cell basis. If both matrices are sparse, a sparse matrix is returned, otherwise a dense matrix is returned.- Parameters:
right
- DoubleMatrixData; the other data object to subtract- Returns:
- the sum of this data object and the other data object
- Throws:
ValueRuntimeException
- if matrices have different lengths
-
decrementBy
Subtract a matrix from this matrix on a cell-by-cell basis. The type of matrix (sparse, dense) stays the same.- Parameters:
decrement
- DoubleMatrixData; the amount to subtract- Returns:
- DoubleMatrixData; this modified double matrix data object
- Throws:
ValueRuntimeException
- if matrices have different sizes
-
times
Multiply two matrices on a cell-by-cell basis. If both matrices are dense, a dense matrix is returned, otherwise a sparse matrix is returned.- Parameters:
right
- DoubleMatrixData; the other data object to multiply with- Returns:
- DoubleVectorData; a new double matrix data store holding the result of the multiplications
- Throws:
ValueRuntimeException
- if matrices have different sizes
-
multiplyBy
Multiply a matrix with the values of another matrix on a cell-by-cell basis. The type of matrix (sparse, dense) stays the same.- Parameters:
right
- DoubleMatrixData; the other data object to multiply with- Returns:
- DoubleMatrixData; this modified data store
- Throws:
ValueRuntimeException
- if matrices have different lengths
-
divide
Divide two matrices on a cell-by-cell basis. If this matrix is sparse andright
is dense, a sparse matrix is returned, otherwise a dense matrix is returned.- Parameters:
right
- DoubleMatrixData; the other data object to divide by- Returns:
- DoubleMatrixData; the ratios of the values of this data object and the other data object
- Throws:
ValueRuntimeException
- if matrices have different sizes
-
divideBy
Divide the values of a matrix by the values of another matrix on a cell-by-cell basis. The type of matrix (sparse, dense) stays the same.- Parameters:
right
- DoubleMatrixData; the other data object to divide by- Returns:
- DoubleMatrixData; this modified data store
- Throws:
ValueRuntimeException
- if matrices have different sizes
-
hashCode
public int hashCode() -
compareDenseMatrixWithSparseMatrix
protected boolean compareDenseMatrixWithSparseMatrix(DoubleMatrixDataDense dm, DoubleMatrixDataSparse sm) Compare contents of a dense and a sparse matrix.- Parameters:
dm
- DoubleMatrixDataDense; the dense matrixsm
- DoubleMatrixDataSparse; the sparse matrix- Returns:
- boolean; true if the contents are equal
-
equals
-