Package org.djunits.vecmat.dnxm
Class MatrixNxM<Q extends Quantity<Q>>
java.lang.Object
org.djunits.vecmat.def.VectorMatrix<Q,T,SI,H,TT>
org.djunits.vecmat.def.Table<Q,M,SI,H,MT>
org.djunits.vecmat.def.Matrix<Q,MatrixNxM<Q>,MatrixNxM<SIQuantity>,MatrixNxM<?>,MatrixNxM<Q>>
org.djunits.vecmat.dnxm.MatrixNxM<Q>
- Type Parameters:
Q- the quantity type
- All Implemented Interfaces:
Serializable,Additive<MatrixNxM<Q>>,Scalable<MatrixNxM<Q>>,Value<MatrixNxM<Q>,,Q> Hadamard<MatrixNxM<?>,MatrixNxM<SIQuantity>>
public class MatrixNxM<Q extends Quantity<Q>>
extends Matrix<Q,MatrixNxM<Q>,MatrixNxM<SIQuantity>,MatrixNxM<?>,MatrixNxM<Q>>
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<?> dataGridSi, Unit<?, Q> displayUnit) Create a new NxM Matrix with a unit, based on a DataGrid storage object that contains SI data. -
Method Summary
Modifier and TypeMethodDescriptionReturn the matrix 'as' a matrix with a known quantity, using a unit to express the result in.intcols()Return the number of columns.booleandouble[]getColumnSi(int col) Return an array with SI-values for the given column (0-based) from the table or matrix.getColumnVector(int col) Return a quantity column (0-based) from the table 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 table or matrix.getRowVector(int row) Return a quantity row (0-based) from the table or matrix.double[]Return a row-major array of SI-values for this matrix or vector.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 table or matrix.mgetRowVector(int mRow) Return a quantity row (1-based) from the table 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.intReturn the number of non-zero entries in the vector, matrix or table.Create a new MatrixNxM with a unit, based on a 2-dimensional grid with values in the given unit.Create a new MatrixNxM with a unit, based on a row-major array with values in the given unit.of(Q[][] grid) Create a MatrixNxM without needing generics, based on a 2-dimensional grid of quantities.of(Q[] data, int rows, int cols) Create a MatrixNxM without needing generics, based on a row-major array of quantities.Create a new MatrixNxM with a unit, based on a 2-dimensional grid with SI-values.Create a MatrixNxM without needing generics, based on a row-major array with SI-values.introws()Return the number of rows.doublesi(int row, int col) Return the si-value at position (row, col), where both row and col are 0-based values.Return the transposed matrix.double[]Return a row-major possibly UNSAFE array of SI-values for this matrix or vector.Methods inherited from class org.djunits.vecmat.def.Table
checkCol, checkRow, get, getColumnScalars, getRowScalars, getScalarArray, getScalarGrid, getSiGrid, mcheckCol, mcheckRow, mget, mgetColumnScalars, mgetColumnSi, mgetRowScalars, mgetRowSi, msiMethods inherited from class org.djunits.vecmat.def.VectorMatrix
abs, add, add, asMatrix1x1, asMatrix2x2, asMatrix3x3, asMatrixNxM, asMatrixNxN, asQuantityTable, asVector1, asVector2Col, asVector2Row, asVector3Col, asVector3Row, asVectorNCol, asVectorNRow, checkMultiply, checkMultiply, divideElements, divideEntries, getDisplayUnit, invertEntries, isRelative, max, mean, median, min, multiplyElements, multiplyEntries, multiplyEntries, negate, nnz, scaleBy, setDisplayUnit, subtract, subtract, sum, toStringMethods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, waitMethods inherited from interface org.djunits.vecmat.operations.Hadamard
divideEntriesMethods 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:
dataGridSi- 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
-
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>,MatrixNxM<Q extends Quantity<Q>>, MatrixNxM<SIQuantity>, MatrixNxM<?>, MatrixNxM<Q extends Quantity<Q>>> - 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>,MatrixNxM<Q extends Quantity<Q>>, MatrixNxM<SIQuantity>, MatrixNxM<?>, MatrixNxM<Q extends Quantity<Q>>> - 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
-
getSiArray
public double[] getSiArray()Description copied from class:VectorMatrixReturn a row-major array of SI-values for this matrix or vector. This is guaranteed to be a safe copy. -
unsafeSiArray
public double[] unsafeSiArray()Description copied from class:VectorMatrixReturn a row-major possibly UNSAFE array of SI-values for this matrix or vector. The method might give access to the underlying data structure, so treat the data carefully. -
si
Description copied from class:TableReturn the si-value at position (row, col), where both row and col are 0-based values.- Specified by:
siin classTable<Q extends Quantity<Q>,MatrixNxM<Q extends Quantity<Q>>, MatrixNxM<SIQuantity>, MatrixNxM<?>, MatrixNxM<Q extends Quantity<Q>>> - 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:TableReturn a quantity row (0-based) from the table or matrix. Note that the specific vector to return can be tightened by the implementing class. -
mgetRowVector
Description copied from class:TableReturn a quantity row (1-based) from the table or matrix. Note that the specific vector to return can be tightened by the implementing class. -
getColumnVector
Description copied from class:TableReturn a quantity column (0-based) from the table or matrix. Note that the specific vector to return can be tightened by the implementing class. -
mgetColumnVector
Description copied from class:TableReturn a quantity column (1-based) from the table or matrix. Note that the specific vector to return can be tightened by the implementing class. -
getRowSi
public double[] getRowSi(int row) Description copied from class:TableReturn an array with SI-values for the given row (0-based) from the table or matrix. -
getColumnSi
public double[] getColumnSi(int col) Description copied from class:TableReturn an array with SI-values for the given column (0-based) from the table or matrix. -
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. -
nonZeroCount
public int nonZeroCount()Description copied from class:VectorMatrixReturn the number of non-zero entries in the vector, matrix or table. Note that NaN and Infinity count as a non-zero element. The value -0.0 counts as 0.0. -
transpose
Return the transposed matrix. A transposed matrix has the same unit as the original one. -
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
-
of
public static <Q extends Quantity<Q>> MatrixNxM<Q> of(double[] dataInUnit, int rows, int cols, Unit<?, Q> unit) Create a new MatrixNxM with a unit, based on a row-major array with values in the given unit.- Type Parameters:
Q- the quantity type- Parameters:
dataInUnit- the matrix values {a11, a12, ..., A1M, ..., aN1, aN2, ..., aNM} expressed in the unitrows- the number of rowscols- the number of columnsunit- the unit of the data, also used as the display unit- Returns:
- a new MatrixNxM with a unit
- Throws:
IllegalArgumentException- when dataInUnit does not contain a square number of values
-
ofSi
public static <Q extends Quantity<Q>> MatrixNxM<Q> ofSi(double[] dataSi, int rows, int cols, Unit<?, Q> displayUnit) Create a MatrixNxM without needing generics, based on a row-major array with SI-values.- Type Parameters:
Q- the quantity type- Parameters:
dataSi- the matrix values {a11, a12, ..., A1M, ..., aN1, aN2, ..., aNM} as an array using SI unitsrows- the number of rowscols- the number of columnsdisplayUnit- the display unit to use- Returns:
- a new MatrixNxM with a unit
- Throws:
IllegalArgumentException- when dataSi does not contain a square number of values
-
of
Create a MatrixNxM without needing generics, based on a row-major array of quantities. The unit is taken from the first quantity in the array.- Type Parameters:
Q- the quantity type- Parameters:
data- the matrix values {a11, a12, ..., A1M, ..., aN1, aN2, ..., aNM} expressed as an array of quantitiesrows- the number of rowscols- the number of columns- Returns:
- a new MatrixNxM with a unit
- Throws:
IllegalArgumentException- when data does not contain a square number of quantities
-
ofSi
Create a new MatrixNxM with a unit, based on a 2-dimensional grid with SI-values.- Type Parameters:
Q- the quantity type- Parameters:
gridSi- the matrix values {{a11, a12, ..., A1M}, ..., {aN1, aN2, ..., aNM}} expressed in the SI or base unitdisplayUnit- the unit of the data, which will also be used as the display unit- Returns:
- a new MatrixNxM with a unit
- Throws:
IllegalArgumentException- when dataInUnit does not contain a square number of values
-
of
Create a new MatrixNxM with a unit, based on a 2-dimensional grid with values in the given unit.- Type Parameters:
Q- the quantity type- Parameters:
gridInUnit- the matrix values {{a11, a12, ..., A1M}, ..., {aN1, aN2, ..., aNM}} expressed in the unitunit- the unit of the values, also used as the display unit- Returns:
- a new MatrixNxM with a unit
- Throws:
IllegalArgumentException- when dataInUnit does not contain a square number of values
-
of
Create a MatrixNxM without needing generics, based on a 2-dimensional grid of quantities. The unit is taken from the first quantity in the grid.- Type Parameters:
Q- the quantity type- Parameters:
grid- the matrix values {{a11, a12, ..., A1M}, ..., {aN1, aN2, ..., aNM}} expressed as a 2-dimensional array of quantities- Returns:
- a new MatrixNxM with a unit
- Throws:
IllegalArgumentException- when dataInUnit does not contain a square number of quantities
-
as
public <TQ extends Quantity<TQ>> MatrixNxM<TQ> as(Unit<?, TQ> targetUnit) throws IllegalArgumentExceptionReturn 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- 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
-