Class VectorMatrix<Q extends Quantity<Q,U>,U extends UnitInterface<U,Q>,VM extends VectorMatrix<Q,U,VM,SI,H>,SI extends VectorMatrix<SIQuantity,SIUnit,SI,?,?>,H extends VectorMatrix<?,?,?,?,?>>

java.lang.Object
org.djunits.vecmat.def.VectorMatrix<Q,U,VM,SI,H>
Type Parameters:
Q - the quantity type
U - the unit type
VM - the 'SELF' vector or matrix type
SI - the vector or matrix type with generics <SIQuantity, SIUnit<
H - the generic vector or matrix type with generics <?, ?< for Hadamard operations
All Implemented Interfaces:
Serializable, Additive<VM>, Scalable<VM>, Value<U,VM>, Hadamard<H,SI>
Direct Known Subclasses:
Matrix, QuantityTable

public abstract class VectorMatrix<Q extends Quantity<Q,U>,U extends UnitInterface<U,Q>,VM extends VectorMatrix<Q,U,VM,SI,H>,SI extends VectorMatrix<SIQuantity,SIUnit,SI,?,?>,H extends VectorMatrix<?,?,?,?,?>> extends Object implements Value<U,VM>, Scalable<VM>, Additive<VM>, Hadamard<H,SI>
VectorMatrix contains a number of standard operations on vectors and matrices of relative quantities.

Copyright (c) 2025-2026 Delft University of Technology, Jaffalaan 5, 2628 BX Delft, the Netherlands. All rights reserved. See for project information https://djunits.org. The DJUNITS project is distributed under a three-clause BSD-style license.

Author:
Alexander Verbraeck
See Also:
  • Constructor Summary

    Constructors
    Constructor
    Description
    VectorMatrix(U displayUnit)
    Create a new vector or matrix with a unit.
  • Method Summary

    Modifier and Type
    Method
    Description
    abs()
    Make the SI-values of this element absolute.
    add(Q increment)
    Return the a vector or matrix with entries that contain the sum of the element and the increment.
    add(VM other)
    Add an element to this element, based on the SI-values.
    Convert this vector or matrix to a MatrixNxM.
    Convert this vector or matrix to a QuantityTable.
    protected void
    checkCol(int col)
    Check if the 0-based column is within bounds.
    protected void
    checkRow(int row)
    Check if the 0-based row is within bounds.
    abstract int
    Return the number of columns.
    Divide the elements of this vector, matrix or table on an element-by-element basis with those of another collection of the same type (but possibly representing another quantity).
    Multiply the elements of this vector, matrix or table by the given quantity.
    get(int row, int col)
    Return the quantity at position (row, col), where both row and col are 0-based values.
    Q[]
    Retrieve a column (0-based) from the matrix as an array of scalars.
    abstract double[]
    getColumnSi(int col)
    Return an array with SI-values for the given column (0-based) from the vector or matrix.
    abstract Vector<Q,U,?,?,?>
    getColumnVector(int col)
    Return a quantity column (0-based) from the vector or matrix.
    Retrieve the unit of this Value.
    Q[]
    getRowScalars(int row)
    Retrieve a row (0-based) from the matrix as an array of scalars.
    abstract double[]
    getRowSi(int row)
    Return an array with SI-values for the given row (0-based) from the vector or matrix.
    abstract Vector<Q,U,?,?,?>
    getRowVector(int row)
    Return a quantity row (0-based) from the vector or matrix.
    Q[][]
    Return the vector or matrix as a 2D array of scalars.
    abstract VM
    instantiateSi(double[] siNew)
    Return a new vector or matrix with the given SI or BASE values.
    abstract SI
    instantiateSi(double[] siNew, SIUnit siUnit)
    Return a new vector or matrix in SI-units with the given SI or BASE values.
    Invert the vector, matrix or table on an element-by-element basis.
    boolean
    Indicate whether this is a Relative Value.
    max()
    Return the maximum value of the elements of the vector or matrix.
    protected void
    mcheckCol(int mCol)
    Check if the 1-based column is within bounds.
    protected void
    mcheckRow(int mRow)
    Check if the 1-based row is within bounds.
    Return the mean value of the elements of the vector or matrix.
    Return the median value of the elements of the vector or matrix.
    mget(int mRow, int mCol)
    Return the quantity at position (row, col), where both row and col are 1-based values.
    Q[]
    Retrieve a column (1-based) from the matrix as an array of scalars.
    double[]
    mgetColumnSi(int mCol)
    Return an array with SI-values for the given column (1-based) from the vector or matrix.
    abstract Vector<Q,U,?,?,?>
    mgetColumnVector(int mCol)
    Return a quantity column (1-based) from the vector or matrix.
    Q[]
    mgetRowScalars(int mRow)
    Retrieve a row (1-based) from the matrix as an array of scalars.
    double[]
    mgetRowSi(int mRow)
    Return an array with SI-values for the given row (1-based) from the vector or matrix.
    abstract Vector<Q,U,?,?,?>
    mgetRowVector(int mRow)
    Return a quantity row (1-based) from the vector or matrix.
    min()
    Return the minimum value of the elements of the vector or matrix.
    Return the largest value of the elements of the vector or matrix.
    double
    msi(int mRow, int mCol)
    Return the si-value at position (row, col), where both row and col are 1-based values.
    Multiply the elements of this vector, matrix or table on an element-by-element basis with those of another collection of the same type (but possibly representing another quantity).
    multiplyElements(Quantity<?,?> quantity)
    Multiply the elements of this vector, matrix or table by the given quantity.
    Multiply the elements of this vector, matrix or table by the given quantity.
    Negate this element, based on the SI-values.
    abstract int
    Return the number of rows.
    scaleBy(double factor)
    Scale the element by a dimensionless factor, based on the SI-values.
    setDisplayUnit(U newUnit)
    Set a new display unit for the value.
    abstract double[]
    si()
    Return a row-major array of SI-values for this matrix or vector.
    abstract double
    si(int row, int col)
    Return the si-value at position (row, col), where both row and col are 0-based values.
    subtract(Q decrement)
    Return the a vector or matrix with entries that contain the value minus the decrement.
    subtract(VM other)
    Subtract an element from this element, based on the SI-values.
    sum()
    Return the sum of the values of the elements of the vector or matrix.
    Return a concise description of this value.
    toString(U withUnit)
    Return a concise description of this value with the values expressed in the specified unit.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait

    Methods inherited from interface org.djunits.vecmat.operations.Hadamard

    divideElements

    Methods inherited from interface org.djunits.value.Scalable

    divideBy

    Methods inherited from interface org.djunits.value.Value

    isAbsolute, setDisplayUnit
  • Constructor Details

    • VectorMatrix

      public VectorMatrix(U displayUnit)
      Create a new vector or matrix with a unit.
      Parameters:
      displayUnit - the display unit to use
  • Method Details

    • getDisplayUnit

      public U getDisplayUnit()
      Description copied from interface: Value
      Retrieve the unit of this Value.
      Specified by:
      getDisplayUnit in interface Value<Q extends Quantity<Q,U>,U extends UnitInterface<U,Q>>
      Returns:
      the unit of this Value
    • setDisplayUnit

      public VM setDisplayUnit(U newUnit)
      Description copied from interface: Value
      Set a new display unit for the value. Internally, the value will not changed since it is stored in a base unit.
      Specified by:
      setDisplayUnit in interface Value<Q extends Quantity<Q,U>,U extends UnitInterface<U,Q>>
      Parameters:
      newUnit - the new display unit of this value
      Returns:
      'this' for fluent design
    • rows

      public abstract int rows()
      Return the number of rows.
      Returns:
      the number of rows
    • cols

      public abstract int cols()
      Return the number of columns.
      Returns:
      the number of columns
    • instantiateSi

      public abstract VM instantiateSi(double[] siNew)
      Return a new vector or matrix with the given SI or BASE values.
      Parameters:
      siNew - the values for the new vector or matrix in row-major format
      Returns:
      a new matrix with the provided SI or BASE values
    • instantiateSi

      public abstract SI instantiateSi(double[] siNew, SIUnit siUnit)
      Return a new vector or matrix in SI-units with the given SI or BASE values.
      Parameters:
      siNew - the values for the new vector or matrix in row-major format
      siUnit - the new unit for the new vector or matrix
      Returns:
      a new matrix with the provided SI or BASE values
    • si

      public abstract double[] si()
      Return a row-major array of SI-values for this matrix or vector. Note that this is NOT a safe copy.
      Returns:
      the row-major array of SI-values
    • si

      public abstract double si(int row, int col) throws IndexOutOfBoundsException
      Return the si-value at position (row, col), where both row and col are 0-based values.
      Parameters:
      row - the row (0-based)
      col - the column (0-based)
      Returns:
      the si-value at position (row, col)
      Throws:
      IndexOutOfBoundsException - when row or col < 0 or larger than number of rows/columns - 1.
    • msi

      public double msi(int mRow, int mCol) throws IndexOutOfBoundsException
      Return the si-value at position (row, col), where both row and col are 1-based values.
      Parameters:
      mRow - the row (1-based)
      mCol - the column (1-based)
      Returns:
      the si-value at position (row, col)
      Throws:
      IndexOutOfBoundsException - when row or col < 1 or larger than number of rows/columns.
    • get

      public Q get(int row, int col) throws IndexOutOfBoundsException
      Return the quantity at position (row, col), where both row and col are 0-based values.
      Parameters:
      row - the row (0-based)
      col - the column (0-based)
      Returns:
      the quantity at position (row, col)
      Throws:
      IndexOutOfBoundsException - when row or col < 0 or larger than number of rows/columns - 1.
    • mget

      public Q mget(int mRow, int mCol) throws IndexOutOfBoundsException
      Return the quantity at position (row, col), where both row and col are 1-based values.
      Parameters:
      mRow - the row (1-based)
      mCol - the column (1-based)
      Returns:
      the quantity at position (row, col)
      Throws:
      IndexOutOfBoundsException - when row or col < 1 or larger than number of rows/columns.
    • getScalarGrid

      public Q[][] getScalarGrid()
      Return the vector or matrix as a 2D array of scalars.
      Returns:
      a new Q[rows()][cols()] array; entry [i-1][j-1] contains get(i, j).
    • getRowVector

      public abstract Vector<Q,U,?,?,?> getRowVector(int row)
      Return a quantity row (0-based) from the vector or matrix. Note that the specific vector to return can be tightened by the implementing class.
      Parameters:
      row - the row number to retrieve (0-based)
      Returns:
      a row vector with the data at the given row
    • mgetRowVector

      public abstract Vector<Q,U,?,?,?> mgetRowVector(int mRow)
      Return a quantity row (1-based) from the vector or matrix. Note that the specific vector to return can be tightened by the implementing class.
      Parameters:
      mRow - the row number to retrieve (1-based)
      Returns:
      a row vector with the data at the given row
    • getColumnVector

      public abstract Vector<Q,U,?,?,?> getColumnVector(int col)
      Return a quantity column (0-based) from the vector or matrix. Note that the specific vector to return can be tightened by the implementing class.
      Parameters:
      col - the column number to retrieve (0-based)
      Returns:
      a column vector with the data at the given column
    • mgetColumnVector

      public abstract Vector<Q,U,?,?,?> mgetColumnVector(int mCol)
      Return a quantity column (1-based) from the vector or matrix. Note that the specific vector to return can be tightened by the implementing class.
      Parameters:
      mCol - the column number to retrieve (1-based)
      Returns:
      a column vector with the data at the given column
    • getRowSi

      public abstract double[] getRowSi(int row)
      Return an array with SI-values for the given row (0-based) from the vector or matrix.
      Parameters:
      row - the row number to retrieve (0-based)
      Returns:
      an array with SI-values with the data at the given row
    • mgetRowSi

      public double[] mgetRowSi(int mRow)
      Return an array with SI-values for the given row (1-based) from the vector or matrix.
      Parameters:
      mRow - the row number to retrieve (1-based)
      Returns:
      an array with SI-values with the data at the given row
    • getColumnSi

      public abstract double[] getColumnSi(int col)
      Return an array with SI-values for the given column (0-based) from the vector or matrix.
      Parameters:
      col - the column number to retrieve (0-based)
      Returns:
      an array with SI-values with the data at the given column
    • mgetColumnSi

      public double[] mgetColumnSi(int mCol)
      Return an array with SI-values for the given column (1-based) from the vector or matrix.
      Parameters:
      mCol - the column number to retrieve (1-based)
      Returns:
      an array with SI-values with the data at the given column
    • isRelative

      public boolean isRelative()
      Description copied from interface: Value
      Indicate whether this is a Relative Value.
      Specified by:
      isRelative in interface Value<Q extends Quantity<Q,U>,U extends UnitInterface<U,Q>>
      Returns:
      whether this is a Relative Value
    • checkRow

      protected void checkRow(int row)
      Check if the 0-based row is within bounds.
      Parameters:
      row - the 0-based row to check
      Throws:
      IndexOutOfBoundsException - when row is out of bounds
    • checkCol

      protected void checkCol(int col)
      Check if the 0-based column is within bounds.
      Parameters:
      col - the 0-based column to check
      Throws:
      IndexOutOfBoundsException - when column is out of bounds
    • mcheckRow

      protected void mcheckRow(int mRow)
      Check if the 1-based row is within bounds.
      Parameters:
      mRow - the 1-based row to check
      Throws:
      IndexOutOfBoundsException - when row is out of bounds
    • mcheckCol

      protected void mcheckCol(int mCol)
      Check if the 1-based column is within bounds.
      Parameters:
      mCol - the 1-based column to check
      Throws:
      IndexOutOfBoundsException - when column is out of bounds
    • getRowScalars

      public Q[] getRowScalars(int row) throws IndexOutOfBoundsException
      Retrieve a row (0-based) from the matrix as an array of scalars.
      Parameters:
      row - row of the values to retrieve (0-based)
      Returns:
      the row as a Scalar array
      Throws:
      IndexOutOfBoundsException - in case row is out of bounds
    • mgetRowScalars

      public Q[] mgetRowScalars(int mRow) throws IndexOutOfBoundsException
      Retrieve a row (1-based) from the matrix as an array of scalars.
      Parameters:
      mRow - row of the values to retrieve (1-based)
      Returns:
      the row as a Scalar array
      Throws:
      IndexOutOfBoundsException - in case row is out of bounds
    • getColumnScalars

      public Q[] getColumnScalars(int col) throws IndexOutOfBoundsException
      Retrieve a column (0-based) from the matrix as an array of scalars.
      Parameters:
      col - column of the values to retrieve (0-based)
      Returns:
      the column as a Scalar array
      Throws:
      IndexOutOfBoundsException - in case column is out of bounds
    • mgetColumnScalars

      public Q[] mgetColumnScalars(int mCol) throws IndexOutOfBoundsException
      Retrieve a column (1-based) from the matrix as an array of scalars.
      Parameters:
      mCol - column of the values to retrieve (1-based)
      Returns:
      the column as a Scalar array
      Throws:
      IndexOutOfBoundsException - in case column is out of bounds
    • mean

      public Q mean()
      Return the mean value of the elements of the vector or matrix.
      Returns:
      the mean value of the elements of the vector or matrix
    • min

      public Q min()
      Return the minimum value of the elements of the vector or matrix.
      Returns:
      the minimum value of the elements of the vector or matrix
    • max

      public Q max()
      Return the maximum value of the elements of the vector or matrix.
      Returns:
      the maximum value of the elements of the vector or matrix
    • mode

      public Q mode()
      Return the largest value of the elements of the vector or matrix.
      Returns:
      the largest value of the elements of the vector or matrix
    • median

      public Q median()
      Return the median value of the elements of the vector or matrix.
      Returns:
      the median value of the elements of the vector or matrix
    • sum

      public Q sum()
      Return the sum of the values of the elements of the vector or matrix.
      Returns:
      the sum of the values of the elements of the vector or matrix
    • add

      public VM add(Q increment)
      Return the a vector or matrix with entries that contain the sum of the element and the increment.
      Parameters:
      increment - the quantity by which to increase the values of the vector or matrix
      Returns:
      a vector or matrix with elements that are incremented by the given quantity
    • subtract

      public VM subtract(Q decrement)
      Return the a vector or matrix with entries that contain the value minus the decrement.
      Parameters:
      decrement - the quantity by which to decrease the values of the vector or matrix
      Returns:
      a vector or matrix with elements that are decremented by the given quantity
    • add

      public VM add(VM other)
      Description copied from interface: Additive
      Add an element to this element, based on the SI-values. Note that this can lead to unwanted effects for elements with Offset scales: 10 °C + 5 °C = 288.15 °C and not 15 °C.
      Specified by:
      add in interface Additive<Q extends Quantity<Q,U>>
      Parameters:
      other - the element to add to this element
      Returns:
      the sum of the SI-values of this element and the SI-values of the other element
    • subtract

      public VM subtract(VM other)
      Description copied from interface: Additive
      Subtract an element from this element, based on the SI-values.
      Specified by:
      subtract in interface Additive<Q extends Quantity<Q,U>>
      Parameters:
      other - the element to subtract from this element
      Returns:
      a new element with the SI-values of this element minus the SI-values of the other element
    • negate

      public VM negate()
      Description copied from interface: Additive
      Negate this element, based on the SI-values. Note that this can lead to unwanted effects for elements with Offset scales. A temperature in Celsius that is negated, will have its Kelvin temperatures negated.
      Specified by:
      negate in interface Additive<Q extends Quantity<Q,U>>
      Returns:
      a new element with all of its SI-values negated
    • abs

      public VM abs()
      Description copied from interface: Additive
      Make the SI-values of this element absolute. A new element wil be returned. Note that this can lead to unwanted effects for elements with Offset scales. A temperature of -10 degrees Celsius will have its Kelvin temperatures negated.
      Specified by:
      abs in interface Additive<Q extends Quantity<Q,U>>
      Returns:
      a new element with the absolute value for all of its SI-values
    • scaleBy

      public VM scaleBy(double factor)
      Description copied from interface: Scalable
      Scale the element by a dimensionless factor, based on the SI-values. Note that this can lead to unwanted effects for elements with Offset scales. 2 * 10 °C = 293.15 °C and not 20 °C.
      Specified by:
      scaleBy in interface Scalable<Q extends Quantity<Q,U>>
      Parameters:
      factor - the dimensionless scale factor
      Returns:
      a new element with all of its SI-values scaled by the dimensionless factor
    • multiplyElements

      public VM multiplyElements(Dimensionless quantity)
      Multiply the elements of this vector, matrix or table by the given quantity. This is actually a Hadamard operation, but since it is equivalent to a scaleBy operation, it is included in this interface.
      Parameters:
      quantity - the scalar quantity to multiply by
      Returns:
      a vector, matrix or table where the elements have been multiplied by the given quantity
    • divideElements

      public VM divideElements(Dimensionless quantity)
      Multiply the elements of this vector, matrix or table by the given quantity. This is actually a Hadamard operation, but since it is equivalent to a scaleBy operation, it is included in this interface.
      Parameters:
      quantity - the scalar quantity to multiply by
      Returns:
      a vector, matrix or table where the elements have been multiplied by the given quantity
    • invertElements

      public SI invertElements()
      Description copied from interface: Hadamard
      Invert the vector, matrix or table on an element-by-element basis.
      Specified by:
      invertElements in interface Hadamard<Q extends Quantity<Q,U>,U extends UnitInterface<U,Q>>
      Returns:
      a vector, matrix or table with an element-by-element inversion (1/value) of the elements
    • multiplyElements

      public SI multiplyElements(H other)
      Description copied from interface: Hadamard
      Multiply the elements of this vector, matrix or table on an element-by-element basis with those of another collection of the same type (but possibly representing another quantity).
      Specified by:
      multiplyElements in interface Hadamard<Q extends Quantity<Q,U>,U extends UnitInterface<U,Q>>
      Parameters:
      other - the other collection of the same type
      Returns:
      a vector, matrix or table with an element-by-element multiplication of its elements
    • divideElements

      public SI divideElements(H other)
      Description copied from interface: Hadamard
      Divide the elements of this vector, matrix or table on an element-by-element basis with those of another collection of the same type (but possibly representing another quantity).
      Specified by:
      divideElements in interface Hadamard<Q extends Quantity<Q,U>,U extends UnitInterface<U,Q>>
      Parameters:
      other - the other collection of the same type
      Returns:
      a vector, matrix or table with an element-by-element division of this collection's elements and other's elements
    • multiplyElements

      public SI multiplyElements(Quantity<?,?> quantity)
      Description copied from interface: Hadamard
      Multiply the elements of this vector, matrix or table by the given quantity.
      Specified by:
      multiplyElements in interface Hadamard<Q extends Quantity<Q,U>,U extends UnitInterface<U,Q>>
      Parameters:
      quantity - the scalar quantity to multiply by
      Returns:
      a vector, matrix or table where the elements have been multiplied by the given quantity
    • asMatrixNxM

      public MatrixNxM<Q,U> asMatrixNxM()
      Convert this vector or matrix to a MatrixNxM.
      Returns:
      a MatrixNxN with identical SI data and display unit
    • asQuantityTable

      public QuantityTable<Q,U> asQuantityTable()
      Convert this vector or matrix to a QuantityTable.
      Returns:
      a QuantityTable with identical SI data and display unit
    • toString

      public String toString(U withUnit)
      Description copied from interface: Value
      Return a concise description of this value with the values expressed in the specified unit.
      Specified by:
      toString in interface Value<Q extends Quantity<Q,U>,U extends UnitInterface<U,Q>>
      Parameters:
      withUnit - the unit into which the values are converted for display
      Returns:
      a String with the value contents expressed in the specified unit
    • toString

      public String toString()
      Description copied from interface: Value
      Return a concise description of this value.
      Specified by:
      toString in interface Value<Q extends Quantity<Q,U>,U extends UnitInterface<U,Q>>
      Overrides:
      toString in class Object
      Returns:
      a String with the value contents, without the type, with the unit attached.