Class MatrixNxM<Q extends Quantity<Q,U>,U extends UnitInterface<U,Q>>

Type Parameters:
Q - the quantity type
U - the unit type
All Implemented Interfaces:
Serializable, Additive<MatrixNxM<Q,U>>, Scalable<MatrixNxM<Q,U>>, Value<U,MatrixNxM<Q,U>>, Hadamard<MatrixNxM<?,?>,MatrixNxM<SIQuantity,SIUnit>>

public class MatrixNxM<Q extends Quantity<Q,U>,U extends UnitInterface<U,Q>> extends Matrix<Q,U,MatrixNxM<Q,U>,MatrixNxM<SIQuantity,SIUnit>,MatrixNxM<?,?>>
MatrixNxM implements a matrix with NxM real-valued entries. The matrix is immutable, except for the display unit, which can be changed. Internal storage can be float or double, and dense or sparse. MatrixNxN and VectorN extend from this class.

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 Details

    • MatrixNxM

      public MatrixNxM(DataGridSi<?> dataSi, U displayUnit)
      Create a new NxM Matrix with a unit, based on a DataGrid storage object that contains SI data.
      Parameters:
      dataSi - the data of the matrix, in SI unit.
      displayUnit - the display unit to use
      Throws:
      IllegalArgumentException - when the number of rows or columns does not have a positive value
  • Method Details

    • of

      public static <Q extends Quantity<Q, U>, U extends UnitInterface<U, Q>> MatrixNxM<Q,U> of(double[] valueArrayInUnit, int rows, int cols, U displayUnit)
      Create a new MatrixNxM with a unit, based on a 1-dimensional double array.
      Type Parameters:
      Q - the quantity type
      U - the unit type
      Parameters:
      valueArrayInUnit - the matrix values {a11, a12, ..., a1M, aN2, ..., aNM} expressed in the display unit
      displayUnit - the display unit to use
      rows - the number of rows in the valueArray
      cols - the number of columns in the valueArray
      Returns:
      a new MatrixNxM with a unit
      Throws:
      IllegalArgumentException - when rows or cols is not positive, or when the number of entries in valueArray is not equal to rows*cols
    • of

      public static <Q extends Quantity<Q, U>, U extends UnitInterface<U, Q>> MatrixNxM<Q,U> of(double[][] valueGridInUnit, U displayUnit)
      Create a new MatrixNxM with a unit, based on a 2-dimensional double grid.
      Type Parameters:
      Q - the quantity type
      U - the unit type
      Parameters:
      valueGridInUnit - the matrix values {{a11, a12, a1M}, ..., {aN1, aN2, aNM}} expressed in the display unit
      displayUnit - the display unit to use
      Returns:
      a new MatrixNxM with a unit
      Throws:
      IllegalArgumentException - when valueGrid has 0 rows, or when the number of columns for one of the rows is not equal to the number of columns in another row
    • of

      public static <Q extends Quantity<Q, U>, U extends UnitInterface<U, Q>> MatrixNxM<Q,U> of(Q[][] quantityGrid, U displayUnit)
      Create a new MatrixNxM with a unit, based on a 2-dimensional quantity grid.
      Type Parameters:
      Q - the quantity type
      U - the unit type
      Parameters:
      quantityGrid - the matrix values {{a11, a12, ..., a1M}, {aN2, ..., aNM}}, each with their own unit
      displayUnit - the display unit to use for the resulting matrix
      Returns:
      a new MatrixNxM with a unit
      Throws:
      IllegalArgumentException - when rows or cols is not positive, or when the number of entries in quantityGrid is not equal to rows*cols
    • instantiateSi

      public MatrixNxM<Q,U> instantiateSi(double[] siNew)
      Description copied from class: VectorMatrix
      Return a new vector or matrix with the given SI or BASE values.
      Specified by:
      instantiateSi in class VectorMatrix<Q extends Quantity<Q,U>,U extends UnitInterface<U,Q>,MatrixNxM<Q extends Quantity<Q,U>,U extends UnitInterface<U,Q>>,MatrixNxM<SIQuantity,SIUnit>,MatrixNxM<?,?>>
      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 MatrixNxM<SIQuantity,SIUnit> instantiateSi(double[] siNew, SIUnit siUnit)
      Description copied from class: VectorMatrix
      Return a new vector or matrix in SI-units with the given SI or BASE values.
      Specified by:
      instantiateSi in class VectorMatrix<Q extends Quantity<Q,U>,U extends UnitInterface<U,Q>,MatrixNxM<Q extends Quantity<Q,U>,U extends UnitInterface<U,Q>>,MatrixNxM<SIQuantity,SIUnit>,MatrixNxM<?,?>>
      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
    • getDataGrid

      public DataGridSi<?> getDataGrid()
      Return the internal datagrid object, so we can retrieve data from it.
      Returns:
      the internal datagrid object
    • si

      public double[] si()
      Description copied from class: VectorMatrix
      Return a row-major array of SI-values for this matrix or vector. Note that this is NOT a safe copy.
      Specified by:
      si in class VectorMatrix<Q extends Quantity<Q,U>,U extends UnitInterface<U,Q>,MatrixNxM<Q extends Quantity<Q,U>,U extends UnitInterface<U,Q>>,MatrixNxM<SIQuantity,SIUnit>,MatrixNxM<?,?>>
      Returns:
      the row-major array of SI-values
    • si

      public double si(int row, int col) throws IndexOutOfBoundsException
      Description copied from class: VectorMatrix
      Return the si-value at position (row, col), where both row and col are 0-based values.
      Specified by:
      si in class VectorMatrix<Q extends Quantity<Q,U>,U extends UnitInterface<U,Q>,MatrixNxM<Q extends Quantity<Q,U>,U extends UnitInterface<U,Q>>,MatrixNxM<SIQuantity,SIUnit>,MatrixNxM<?,?>>
      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.
    • getRowVector

      public VectorN.Row<Q,U> getRowVector(int row)
      Description copied from class: VectorMatrix
      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.
      Specified by:
      getRowVector in class VectorMatrix<Q extends Quantity<Q,U>,U extends UnitInterface<U,Q>,MatrixNxM<Q extends Quantity<Q,U>,U extends UnitInterface<U,Q>>,MatrixNxM<SIQuantity,SIUnit>,MatrixNxM<?,?>>
      Parameters:
      row - the row number to retrieve (0-based)
      Returns:
      a row vector with the data at the given row
    • mgetRowVector

      public VectorN.Row<Q,U> mgetRowVector(int mRow)
      Description copied from class: VectorMatrix
      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.
      Specified by:
      mgetRowVector in class VectorMatrix<Q extends Quantity<Q,U>,U extends UnitInterface<U,Q>,MatrixNxM<Q extends Quantity<Q,U>,U extends UnitInterface<U,Q>>,MatrixNxM<SIQuantity,SIUnit>,MatrixNxM<?,?>>
      Parameters:
      mRow - the row number to retrieve (1-based)
      Returns:
      a row vector with the data at the given row
    • getColumnVector

      public VectorN.Col<Q,U> getColumnVector(int col)
      Description copied from class: VectorMatrix
      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.
      Specified by:
      getColumnVector in class VectorMatrix<Q extends Quantity<Q,U>,U extends UnitInterface<U,Q>,MatrixNxM<Q extends Quantity<Q,U>,U extends UnitInterface<U,Q>>,MatrixNxM<SIQuantity,SIUnit>,MatrixNxM<?,?>>
      Parameters:
      col - the column number to retrieve (0-based)
      Returns:
      a column vector with the data at the given column
    • mgetColumnVector

      public VectorN.Col<Q,U> mgetColumnVector(int mCol)
      Description copied from class: VectorMatrix
      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.
      Specified by:
      mgetColumnVector in class VectorMatrix<Q extends Quantity<Q,U>,U extends UnitInterface<U,Q>,MatrixNxM<Q extends Quantity<Q,U>,U extends UnitInterface<U,Q>>,MatrixNxM<SIQuantity,SIUnit>,MatrixNxM<?,?>>
      Parameters:
      mCol - the column number to retrieve (1-based)
      Returns:
      a column vector with the data at the given column
    • getRowSi

      public double[] getRowSi(int row)
      Description copied from class: VectorMatrix
      Return an array with SI-values for the given row (0-based) from the vector or matrix.
      Specified by:
      getRowSi in class VectorMatrix<Q extends Quantity<Q,U>,U extends UnitInterface<U,Q>,MatrixNxM<Q extends Quantity<Q,U>,U extends UnitInterface<U,Q>>,MatrixNxM<SIQuantity,SIUnit>,MatrixNxM<?,?>>
      Parameters:
      row - the row number to retrieve (0-based)
      Returns:
      an array with SI-values with the data at the given row
    • getColumnSi

      public double[] getColumnSi(int col)
      Description copied from class: VectorMatrix
      Return an array with SI-values for the given column (0-based) from the vector or matrix.
      Specified by:
      getColumnSi in class VectorMatrix<Q extends Quantity<Q,U>,U extends UnitInterface<U,Q>,MatrixNxM<Q extends Quantity<Q,U>,U extends UnitInterface<U,Q>>,MatrixNxM<SIQuantity,SIUnit>,MatrixNxM<?,?>>
      Parameters:
      col - the column number to retrieve (0-based)
      Returns:
      an array with SI-values with the data at the given column
    • rows

      public int rows()
      Description copied from class: VectorMatrix
      Return the number of rows.
      Specified by:
      rows in class VectorMatrix<Q extends Quantity<Q,U>,U extends UnitInterface<U,Q>,MatrixNxM<Q extends Quantity<Q,U>,U extends UnitInterface<U,Q>>,MatrixNxM<SIQuantity,SIUnit>,MatrixNxM<?,?>>
      Returns:
      the number of rows
    • cols

      public int cols()
      Description copied from class: VectorMatrix
      Return the number of columns.
      Specified by:
      cols in class VectorMatrix<Q extends Quantity<Q,U>,U extends UnitInterface<U,Q>,MatrixNxM<Q extends Quantity<Q,U>,U extends UnitInterface<U,Q>>,MatrixNxM<SIQuantity,SIUnit>,MatrixNxM<?,?>>
      Returns:
      the number of columns
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • equals

      public boolean equals(Object obj)
      Overrides:
      equals in class Object
    • multiply

      public MatrixNxM<SIQuantity,SIUnit> multiply(Matrix1x1<?,?> matrix)
      Multiply this vector or matrix with a Matrix1x1, resulting in a MatrixNxM. The multiplication is a (Mx1) x (1x1) matrix multiplication resulting in an (Mx1) matrix.
      Parameters:
      matrix - the matrix to multiply with
      Returns:
      a MatrixNxM of an SIQuantity as the result of the matrix multiplication
      Throws:
      IllegalArgumentException - when the number of columns of this matrix does not equal the number of rows of the matrix or vector to multiply with
    • multiply

      public MatrixNxM<SIQuantity,SIUnit> multiply(Matrix2x2<?,?> matrix)
      Multiply this vector or matrix with a Matrix2x2, resulting in a MatrixNxM. The multiplication is a (Mx2) x (2x2) matrix multiplication resulting in an (Mx2) matrix.
      Parameters:
      matrix - the matrix to multiply with
      Returns:
      a MatrixNxM of an SIQuantity as the result of the matrix multiplication
      Throws:
      IllegalArgumentException - when the number of columns of this matrix does not equal the number of rows of the matrix or vector to multiply with
    • multiply

      public MatrixNxM<SIQuantity,SIUnit> multiply(Matrix3x3<?,?> matrix)
      Multiply this vector or matrix with a Matrix3x3, resulting in a MatrixNxM. The multiplication is a (Mx3) x (3x3) matrix multiplication resulting in an (Mx3) matrix.
      Parameters:
      matrix - the matrix to multiply with
      Returns:
      a MatrixNxM of an SIQuantity as the result of the matrix multiplication
      Throws:
      IllegalArgumentException - when the number of columns of this matrix does not equal the number of rows of the matrix or vector to multiply with
    • multiply

      public MatrixNxM<SIQuantity,SIUnit> multiply(MatrixNxN<?,?> matrix)
      Multiply this vector or matrix with a MatrixNxM, resulting in a MatrixNxM. The multiplication is a (NxM) x (MxP) matrix multiplication resulting in an (NxP) matrix.
      Parameters:
      matrix - the matrix to multiply with
      Returns:
      a MatrixNxM of an SIQuantity as the result of the matrix multiplication
      Throws:
      IllegalArgumentException - when the number of columns of this matrix does not equal the number of rows of the matrix or vector to multiply with
    • multiply

      public MatrixNxM<SIQuantity,SIUnit> multiply(Vector1<?,?> vector)
      Multiply this vector or matrix with a Vector1, resulting in a MatrixNxM. The multiplication is a (Mx1) x (1x1) matrix multiplication resulting in an (Mx1) matrix.
      Parameters:
      vector - the vector to multiply with
      Returns:
      a MatrixNxM of an SIQuantity as the result of the matrix multiplication
      Throws:
      IllegalArgumentException - when the number of columns of this matrix does not equal the number of rows of the vector to multiply with
    • multiply

      public VectorN.Col<SIQuantity,SIUnit> multiply(Vector2.Col<?,?> vector)
      Multiply this vector or matrix with a Vector2.Col, resulting in a Vector2.Col. The multiplication is a (Mx2) x (2x1) matrix multiplication resulting in an (Mx1) column vector.
      Parameters:
      vector - the vector to multiply with
      Returns:
      a VectorN.Col of an SIQuantity as the result of the matrix multiplication
      Throws:
      IllegalArgumentException - when the number of columns of this matrix does not equal the number of rows of the vector to multiply with
    • multiply

      public VectorN.Col<SIQuantity,SIUnit> multiply(Vector3.Col<?,?> vector)
      Multiply this vector or matrix with a Vector3.Col, resulting in a Vector3.Col. The multiplication is a (Mx3) x (3x1) matrix multiplication resulting in an (Mx1) column vector.
      Parameters:
      vector - the vector to multiply with
      Returns:
      a VectorN.Col of an SIQuantity as the result of the matrix multiplication
      Throws:
      IllegalArgumentException - when the number of columns of this matrix does not equal the number of rows of the vector to multiply with
    • multiply

      public VectorN.Col<SIQuantity,SIUnit> multiply(VectorN.Col<?,?> vector)
      Multiply this vector or matrix with a VectorN.Col, resulting in a VectorN.Col. The multiplication is a (MxN) x (Nx1) matrix multiplication resulting in an (Mx1) column vector.
      Parameters:
      vector - the vector to multiply with
      Returns:
      a VectorN.Col of an SIQuantity as the result of the matrix multiplication
      Throws:
      IllegalArgumentException - when the number of columns of this matrix does not equal the number of rows of the vector to multiply with
    • as

      public <TQ extends Quantity<TQ, TU>, TU extends UnitInterface<TU, TQ>> MatrixNxM<TQ,TU> as(TU targetUnit) throws IllegalArgumentException
      Return the matrix 'as' a matrix with a known quantity, using a unit to express the result in. Throw a Runtime exception when the SI units of this vector and the target vector do not match.
      Type Parameters:
      TQ - target quantity type
      TU - target unit type
      Parameters:
      targetUnit - the unit to convert the matrix to
      Returns:
      a matrix typed in the target matrix class
      Throws:
      IllegalArgumentException - when the units do not match
    • asMatrix1x1

      public Matrix1x1<Q,U> asMatrix1x1()
      Convert this matrix to a Matrix1x1. The shape must be 1 x 1.
      Returns:
      a Matrix1x1 with identical SI data and display unit
      Throws:
      IllegalStateException - if this matrix is not 1 x 1
    • asMatrix2x2

      public Matrix2x2<Q,U> asMatrix2x2()
      Convert this matrix to a Matrix2x2. The shape must be 2 x 2.
      Returns:
      a Matrix2x2 with identical SI data and display unit
      Throws:
      IllegalStateException - if this matrix is not 2 x 2
    • asMatrix3x3

      public Matrix3x3<Q,U> asMatrix3x3()
      Convert this matrix to a Matrix3x3. The shape must be 3 x 3.
      Returns:
      a Matrix3x3 with identical SI data and display unit
      Throws:
      IllegalStateException - if this matrix is not 3 x 3
    • asMatrixNxN

      public MatrixNxN<Q,U> asMatrixNxN()
      Convert this matrix to a MatrixNxN. The shape must be square.
      Returns:
      a MatrixNxN with identical SI data and display unit
      Throws:
      IllegalStateException - if this matrix is not square
    • asVector1

      public Vector1<Q,U> asVector1()
      Return this matrix as a 1-element column vector. Shape must be 1 x 1.
      Returns:
      a Vector1 with identical SI data and display unit
      Throws:
      IllegalStateException - if shape is not 1 x 1
    • asVector2Col

      public Vector2.Col<Q,U> asVector2Col()
      Return this matrix as a 2-element column vector. Shape must be 2 x 1.
      Returns:
      a Vector2.Col with identical SI data and display unit
      Throws:
      IllegalStateException - if shape is not 2 x 1
    • asVector3Col

      public Vector3.Col<Q,U> asVector3Col()
      Return this matrix as a 3-element column vector. Shape must be 3 x 1.
      Returns:
      a Vector3.Col with identical SI data and display unit
      Throws:
      IllegalStateException - if shape is not 3 x 1
    • asVectorNCol

      public VectorN.Col<Q,U> asVectorNCol()
      Return this matrix as an N-element column vector. Shape must be N x 1.
      Returns:
      a VectorN.Col with identical SI data and display unit
      Throws:
      IllegalStateException - if cols() != 1
    • asVector2Row

      public Vector2.Row<Q,U> asVector2Row()
      Return this matrix as a 2-element row vector. Shape must be 1 x 2.
      Returns:
      a Vector2.Row with identical SI data and display unit
      Throws:
      IllegalStateException - if shape is not 1 x 2
    • asVector3Row

      public Vector3.Row<Q,U> asVector3Row()
      Return this matrix as a 3-element row vector. Shape must be 1 x 3.
      Returns:
      a Vector3.Row with identical SI data and display unit
      Throws:
      IllegalStateException - if shape is not 1 x 3
    • asVectorNRow

      public VectorN.Row<Q,U> asVectorNRow()
      Return this matrix as an N-element row vector. Shape must be 1 x N.
      Returns:
      a VectorN.Row with identical SI data and display unit
      Throws:
      IllegalStateException - if rows() != 1