Package org.djunits.vecmat.dnxm
Class MatrixNxM<Q extends Quantity<Q,U>,U extends UnitInterface<U,Q>>
java.lang.Object
org.djunits.vecmat.def.VectorMatrix<Q,U,M,SI,H>
org.djunits.vecmat.def.Matrix<Q,U,MatrixNxM<Q,U>,MatrixNxM<SIQuantity,SIUnit>,MatrixNxM<?,?>>
org.djunits.vecmat.dnxm.MatrixNxM<Q,U>
- Type Parameters:
Q- the quantity typeU- 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 Summary
ConstructorsConstructorDescriptionMatrixNxM(DataGridSi<?> dataSi, U displayUnit) Create a new NxM Matrix with a unit, based on a DataGrid storage object that contains SI data. -
Method Summary
Modifier and TypeMethodDescription<TQ extends Quantity<TQ,TU>, TU extends UnitInterface<TU, TQ>>
MatrixNxM<TQ,TU> as(TU targetUnit) Return the matrix 'as' a matrix with a known quantity, using a unit to express the result in.Convert this matrix to aMatrix1x1.Convert this matrix to aMatrix2x2.Convert this matrix to aMatrix3x3.Convert this matrix to aMatrixNxN.Return this matrix as a 1-element column vector.Return this matrix as a 2-element column vector.Return this matrix as a 2-element row vector.Return this matrix as a 3-element column vector.Return this matrix as a 3-element row vector.Return this matrix as an N-element column vector.Return this matrix as an N-element row vector.intcols()Return the number of columns.booleandouble[]getColumnSi(int col) Return an array with SI-values for the given column (0-based) from the vector or matrix.getColumnVector(int col) Return a quantity column (0-based) from the vector or matrix.DataGridSi<?>Return the internal datagrid object, so we can retrieve data from it.double[]getRowSi(int row) Return an array with SI-values for the given row (0-based) from the vector or matrix.getRowVector(int row) Return a quantity row (0-based) from the vector or matrix.inthashCode()instantiateSi(double[] siNew) Return a new vector or matrix with the given SI or BASE values.instantiateSi(double[] siNew, SIUnit siUnit) Return a new vector or matrix in SI-units with the given SI or BASE values.mgetColumnVector(int mCol) Return a quantity column (1-based) from the vector or matrix.mgetRowVector(int mRow) Return a quantity row (1-based) from the vector or matrix.Multiply this vector or matrix with a Matrix1x1, resulting in a MatrixNxM.Multiply this vector or matrix with a Vector1, resulting in a MatrixNxM.Multiply this vector or matrix with a Matrix2x2, resulting in a MatrixNxM.multiply(Vector2.Col<?, ?> vector) Multiply this vector or matrix with a Vector2.Col, resulting in a Vector2.Col.Multiply this vector or matrix with a Matrix3x3, resulting in a MatrixNxM.multiply(Vector3.Col<?, ?> vector) Multiply this vector or matrix with a Vector3.Col, resulting in a Vector3.Col.Multiply this vector or matrix with a MatrixNxM, resulting in a MatrixNxM.multiply(VectorN.Col<?, ?> vector) Multiply this vector or matrix with a VectorN.Col, resulting in a VectorN.Col.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.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.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.introws()Return the number of rows.double[]si()Return a row-major array of SI-values for this matrix or vector.doublesi(int row, int col) Return the si-value at position (row, col), where both row and col are 0-based values.Methods inherited from class org.djunits.vecmat.def.Matrix
checkMultiply, checkMultiply, multiplyMethods inherited from class org.djunits.vecmat.def.VectorMatrix
abs, add, add, asMatrixNxM, asQuantityTable, checkCol, checkRow, divideElements, divideElements, get, getColumnScalars, getDisplayUnit, getRowScalars, getScalarGrid, invertElements, isRelative, max, mcheckCol, mcheckRow, mean, median, mget, mgetColumnScalars, mgetColumnSi, mgetRowScalars, mgetRowSi, min, mode, msi, multiplyElements, multiplyElements, multiplyElements, negate, scaleBy, setDisplayUnit, subtract, subtract, sum, toString, toStringMethods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, waitMethods inherited from interface org.djunits.vecmat.operations.Hadamard
divideElementsMethods inherited from interface org.djunits.value.Value
isAbsolute, setDisplayUnit
-
Constructor Details
-
MatrixNxM
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>, MatrixNxM<Q,U extends UnitInterface<U, 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 typeU- the unit type- Parameters:
valueArrayInUnit- the matrix values {a11, a12, ..., a1M, aN2, ..., aNM} expressed in the display unitdisplayUnit- the display unit to userows- the number of rows in the valueArraycols- 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>, MatrixNxM<Q,U extends UnitInterface<U, 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 typeU- the unit type- Parameters:
valueGridInUnit- the matrix values {{a11, a12, a1M}, ..., {aN1, aN2, aNM}} expressed in the display unitdisplayUnit- 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>, MatrixNxM<Q,U extends UnitInterface<U, 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 typeU- the unit type- Parameters:
quantityGrid- the matrix values {{a11, a12, ..., a1M}, {aN2, ..., aNM}}, each with their own unitdisplayUnit- 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
Description copied from class:VectorMatrixReturn a new vector or matrix with the given SI or BASE values.- Specified by:
instantiateSiin classVectorMatrix<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
Description copied from class:VectorMatrixReturn a new vector or matrix in SI-units with the given SI or BASE values.- Specified by:
instantiateSiin classVectorMatrix<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 formatsiUnit- the new unit for the new vector or matrix- Returns:
- a new matrix with the provided SI or BASE values
-
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:VectorMatrixReturn a row-major array of SI-values for this matrix or vector. Note that this is NOT a safe copy. -
si
Description copied from class:VectorMatrixReturn the si-value at position (row, col), where both row and col are 0-based values.- Specified by:
siin classVectorMatrix<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
Description copied from class:VectorMatrixReturn 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:
getRowVectorin classVectorMatrix<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
Description copied from class:VectorMatrixReturn 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:
mgetRowVectorin classVectorMatrix<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
Description copied from class:VectorMatrixReturn 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:
getColumnVectorin classVectorMatrix<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
Description copied from class:VectorMatrixReturn 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:
mgetColumnVectorin classVectorMatrix<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:VectorMatrixReturn an array with SI-values for the given row (0-based) from the vector or matrix.- Specified by:
getRowSiin classVectorMatrix<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:VectorMatrixReturn an array with SI-values for the given column (0-based) from the vector or matrix.- Specified by:
getColumnSiin classVectorMatrix<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:VectorMatrixReturn the number of rows. -
cols
public int cols()Description copied from class:VectorMatrixReturn the number of columns. -
hashCode
public int hashCode() -
equals
-
multiply
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
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
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
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
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
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
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
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>, MatrixNxM<TQ,TU extends UnitInterface<TU, 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 typeTU- 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
Convert this matrix to aMatrix1x1. The shape must be 1 x 1.- Returns:
- a
Matrix1x1with identical SI data and display unit - Throws:
IllegalStateException- if this matrix is not 1 x 1
-
asMatrix2x2
Convert this matrix to aMatrix2x2. The shape must be 2 x 2.- Returns:
- a
Matrix2x2with identical SI data and display unit - Throws:
IllegalStateException- if this matrix is not 2 x 2
-
asMatrix3x3
Convert this matrix to aMatrix3x3. The shape must be 3 x 3.- Returns:
- a
Matrix3x3with identical SI data and display unit - Throws:
IllegalStateException- if this matrix is not 3 x 3
-
asMatrixNxN
Convert this matrix to aMatrixNxN. The shape must be square.- Returns:
- a
MatrixNxNwith identical SI data and display unit - Throws:
IllegalStateException- if this matrix is not square
-
asVector1
Return this matrix as a 1-element column vector. Shape must be 1 x 1.- Returns:
- a
Vector1with identical SI data and display unit - Throws:
IllegalStateException- if shape is not 1 x 1
-
asVector2Col
Return this matrix as a 2-element column vector. Shape must be 2 x 1.- Returns:
- a
Vector2.Colwith identical SI data and display unit - Throws:
IllegalStateException- if shape is not 2 x 1
-
asVector3Col
Return this matrix as a 3-element column vector. Shape must be 3 x 1.- Returns:
- a
Vector3.Colwith identical SI data and display unit - Throws:
IllegalStateException- if shape is not 3 x 1
-
asVectorNCol
Return this matrix as an N-element column vector. Shape must be N x 1.- Returns:
- a
VectorN.Colwith identical SI data and display unit - Throws:
IllegalStateException- ifcols() != 1
-
asVector2Row
Return this matrix as a 2-element row vector. Shape must be 1 x 2.- Returns:
- a
Vector2.Rowwith identical SI data and display unit - Throws:
IllegalStateException- if shape is not 1 x 2
-
asVector3Row
Return this matrix as a 3-element row vector. Shape must be 1 x 3.- Returns:
- a
Vector3.Rowwith identical SI data and display unit - Throws:
IllegalStateException- if shape is not 1 x 3
-
asVectorNRow
Return this matrix as an N-element row vector. Shape must be 1 x N.- Returns:
- a
VectorN.Rowwith identical SI data and display unit - Throws:
IllegalStateException- ifrows() != 1
-