Class DoubleMatrixData

java.lang.Object
org.djunits.value.storage.AbstractStorage<DoubleMatrixData>
org.djunits.value.vdouble.matrix.data.DoubleMatrixData
All Implemented Interfaces:
Serializable, Cloneable
Direct Known Subclasses:
DoubleMatrixDataDense, DoubleMatrixDataSparse

public abstract class DoubleMatrixData
extends AbstractStorage<DoubleMatrixData>
implements Serializable
Stores the data for a DoubleMatrix and carries out basic operations.

Copyright (c) 2013-2020 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved.
BSD-style license. See OpenTrafficSim License.

Author:
Alexander Verbraeck, Peter Knoppers
See Also:
Serialized Form
  • Field Details

    • matrixSI

      protected double[] matrixSI
      the internal storage of the Matrix; can be sparse or dense.
    • rows

      protected int rows
      the number of rows of the matrix.
    • cols

      protected int cols
      the 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 store
      scale - Scale; the scale of the unit to use for conversion to SI
      storageType - StorageType; the data type to use
      Returns:
      the DoubleMatrixData with the right data type
      Throws:
      ValueRuntimeException - when values are null, or storageType is null
    • instantiate

      public static <U extends Unit<U>,​ S extends DoubleScalarInterface<U,​ S>> DoubleMatrixData instantiate​(Collection<DoubleSparseValue<U,​S>> values, int rows, int cols, StorageType storageType) throws ValueRuntimeException
      Instantiate a DoubleMatrixData with the right data type.
      Type Parameters:
      U - the unit type
      S - the corresponding scalar type
      Parameters:
      values - Collection<DoubleSparseValue<U, S>>; the (sparse [X, Y, SI]) values to store
      rows - int; the number of rows of the matrix
      cols - int; the number of columns of the matrix
      storageType - StorageType; the data type to use
      Returns:
      the DoubleMatrixData with the right data type
      Throws:
      ValueRuntimeException - when values are null, or storageType is null
    • instantiate

      public static <U extends Unit<U>,​ S extends DoubleScalarInterface<U,​ S>> DoubleMatrixData instantiate​(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 type
      S - the corresponding scalar type
      Parameters:
      values - S[][]; the values to store
      storageType - StorageType; the data type to use
      Returns:
      the DoubleMatrixData with the right data type
      Throws:
      ValueRuntimeException - when values is null, or storageType is null
    • 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

      public abstract DoubleMatrixDataDense toDense()
      Return the data of this matrix in dense storage format.
      Returns:
      DoubleMatrixDataDense; the dense transformation of this data
    • toSparse

      public abstract DoubleMatrixDataSparse 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 for
      col - 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 for
      col - int; the column number to set the value for
      valueSI - 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
    • checkRectangularAndNonEmpty

      protected static double[][] checkRectangularAndNonEmpty​(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 - when values is null
      ValueRuntimeException - when values is empty, or jagged
    • checkRectangularAndNonEmpty

      protected static <U extends Unit<U>,​ S extends DoubleScalarInterface<U,​ S>> S[][] checkRectangularAndNonEmpty​(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 type
      S - 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 - when values is null
      ValueRuntimeException - when values is empty, or jagged
    • checkSizes

      protected void checkSizes​(DoubleMatrixData other) throws ValueRuntimeException
      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

      public abstract DoubleMatrixData assign​(DoubleFunction doubleFunction)
      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 apply
      right - 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

      public abstract DoubleMatrixData plus​(DoubleMatrixData right) throws ValueRuntimeException
      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

      public final DoubleMatrixData incrementBy​(DoubleMatrixData right) throws ValueRuntimeException
      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

      public abstract DoubleMatrixData minus​(DoubleMatrixData right) throws ValueRuntimeException
      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

      public final DoubleMatrixData decrementBy​(DoubleMatrixData decrement) throws ValueRuntimeException
      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

      public abstract DoubleMatrixData times​(DoubleMatrixData right) throws ValueRuntimeException
      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

      public final DoubleMatrixData multiplyBy​(DoubleMatrixData right) throws ValueRuntimeException
      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

      public abstract DoubleMatrixData divide​(DoubleMatrixData right) throws ValueRuntimeException
      Divide two matrices on a cell-by-cell basis. If this matrix is sparse and right 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

      public final DoubleMatrixData divideBy​(DoubleMatrixData right) throws ValueRuntimeException
      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()
      Overrides:
      hashCode in class Object
    • compareDenseMatrixWithSparseMatrix

      protected boolean compareDenseMatrixWithSparseMatrix​(DoubleMatrixDataDense dm, DoubleMatrixDataSparse sm)
      Compare contents of a dense and a sparse matrix.
      Parameters:
      dm - DoubleMatrixDataDense; the dense matrix
      sm - DoubleMatrixDataSparse; the sparse matrix
      Returns:
      boolean; true if the contents are equal
    • equals

      public boolean equals​(Object obj)
      Overrides:
      equals in class Object