Package org.djunits.vecmat.dn
Class MatrixNxN<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,M,SI,H,M>
org.djunits.vecmat.def.SquareMatrix<Q,MatrixNxN<Q>,MatrixNxN<SIQuantity>,MatrixNxN<?>>
org.djunits.vecmat.dn.MatrixNxN<Q>
- Type Parameters:
Q- the quantity type
- All Implemented Interfaces:
Serializable,Additive<MatrixNxN<Q>>,Scalable<MatrixNxN<Q>>,Value<MatrixNxN<Q>,,Q> Hadamard<MatrixNxN<?>,MatrixNxN<SIQuantity>>
public class MatrixNxN<Q extends Quantity<Q>>
extends SquareMatrix<Q,MatrixNxN<Q>,MatrixNxN<SIQuantity>,MatrixNxN<?>>
MatrixNxN implements a square matrix with NxN 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.
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
ConstructorsConstructorDescriptionMatrixNxN(DataGridSi<?> dataGridSi, Unit<?, Q> displayUnit) Create a new NxN Matrix with a unit, based on a DataGrid storage object that contains SI data. -
Method Summary
Modifier and TypeMethodDescriptionadjugate()Return the adjugate (classical adjoint) matrix for this matrix, often denoted as adj(M).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.protected static intcheckSquare(int length) Check if the length if the row-major matrix is a square.intcols()Return the number of columns.divideEntries(MatrixNxN<?> other) Divide the entries of this vector, matrix or table on an entry-by-entry basis with those of another collection of the same type (but possibly representing another quantity).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 data grid in SI units.Retrieve the main diagonal of the matrix as a column vector.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.inverse()Return the inverse of the square matrix, if the matrix is non-singular.Invert the vector, matrix or table on an entry-by-entry basis.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 matrix with another matrix using matrix multiplication and return the result.multiply(VectorN.Col<?> otherVec) Multiply this matrix with a column vector, resulting in a column vector.multiplyEntries(Quantity<?> quantity) Multiply the entries of this vector, matrix or table by the given quantity.multiplyEntries(MatrixNxN<?> other) Multiply the entries of this vector, matrix or table on an entry-by-entry basis with those of another collection of the same type (but possibly representing another quantity).intReturn the number of non-zero entries in the vector, matrix or table.Create a new MatrixNxN with a unit, based on a 2-dimensional grid with values in the given unit.Create a new MatrixNxN with a unit, based on a row-major array with values in the given unit.of(Q[] data) Create a MatrixNxN without needing generics, based on a row-major array of quantities.of(Q[][] grid) Create a MatrixNxN without needing generics, based on a 2-dimensional grid of quantities.Create a new MatrixNxN with a unit, based on a 2-dimensional grid with SI-values.Create a MatrixNxN 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.double[]Return a row-major possibly UNSAFE array of SI-values for this matrix or vector.Methods inherited from class org.djunits.vecmat.def.SquareMatrix
determinant, determinantSi, getDiagonalScalars, getDiagonalSi, isSkewSymmetric, isSkewSymmetric, isSymmetric, isSymmetric, normFrobenius, order, trace, transposeMethods 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, asMatrixNxM, asMatrixNxN, asQuantityTable, asVector1, asVector2Col, asVector2Row, asVector3Col, asVector3Row, asVectorNCol, asVectorNRow, checkMultiply, checkMultiply, divideElements, getDisplayUnit, isRelative, max, mean, median, min, multiplyElements, 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
-
MatrixNxN
Create a new NxN 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>,MatrixNxN<Q extends Quantity<Q>>, MatrixNxN<SIQuantity>, MatrixNxN<?>, MatrixNxN<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
-
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. -
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. -
si
public double si(int row, int col) Description copied from class:TableReturn the si-value at position (row, col), where both row and col are 0-based values. -
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. -
getDataGrid
Return the data grid in SI units.- Returns:
- the data grid in SI units
-
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>,MatrixNxN<Q extends Quantity<Q>>, MatrixNxN<SIQuantity>, MatrixNxN<?>, MatrixNxN<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
-
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. -
getDiagonalVector
Description copied from class:SquareMatrixRetrieve the main diagonal of the matrix as a column vector.- Specified by:
getDiagonalVectorin classSquareMatrix<Q extends Quantity<Q>,MatrixNxN<Q extends Quantity<Q>>, MatrixNxN<SIQuantity>, MatrixNxN<?>> - Returns:
- the main diagonal as a Vector
- Throws:
IllegalStateException
-
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. -
inverse
Description copied from class:SquareMatrixReturn the inverse of the square matrix, if the matrix is non-singular. The unit of the matrix is U^(-1).- Specified by:
inversein classSquareMatrix<Q extends Quantity<Q>,MatrixNxN<Q extends Quantity<Q>>, MatrixNxN<SIQuantity>, MatrixNxN<?>> - Returns:
- the inverse of the square matrix, if the matrix is non-singular
- Throws:
NonInvertibleMatrixException- when the matrix is singular or cannot be inverted
-
adjugate
Description copied from class:SquareMatrixReturn the adjugate (classical adjoint) matrix for this matrix, often denoted as adj(M). The unit of adj(M) is U^(n-1) where n is the order of the matrix. -
invertEntries
Description copied from interface:HadamardInvert the vector, matrix or table on an entry-by-entry basis.- Specified by:
invertEntriesin interfaceHadamard<MatrixNxN<?>,MatrixNxN<SIQuantity>> - Overrides:
invertEntriesin classVectorMatrix<Q extends Quantity<Q>,MatrixNxN<Q extends Quantity<Q>>, MatrixNxN<SIQuantity>, MatrixNxN<?>, MatrixNxN<Q extends Quantity<Q>>> - Returns:
- a new vector, matrix or table with an entry-by-entry inversion (1/value) of the entries
-
multiplyEntries
Description copied from interface:HadamardMultiply the entries of this vector, matrix or table on an entry-by-entry basis with those of another collection of the same type (but possibly representing another quantity).- Specified by:
multiplyEntriesin interfaceHadamard<MatrixNxN<?>,MatrixNxN<SIQuantity>> - Overrides:
multiplyEntriesin classVectorMatrix<Q extends Quantity<Q>,MatrixNxN<Q extends Quantity<Q>>, MatrixNxN<SIQuantity>, MatrixNxN<?>, MatrixNxN<Q extends Quantity<Q>>> - Parameters:
other- the other collection of the same type- Returns:
- a new vector, matrix or table with an entry-by-entry multiplication of its entries
-
divideEntries
Description copied from interface:HadamardDivide the entries of this vector, matrix or table on an entry-by-entry basis with those of another collection of the same type (but possibly representing another quantity).- Specified by:
divideEntriesin interfaceHadamard<MatrixNxN<?>,MatrixNxN<SIQuantity>> - Overrides:
divideEntriesin classVectorMatrix<Q extends Quantity<Q>,MatrixNxN<Q extends Quantity<Q>>, MatrixNxN<SIQuantity>, MatrixNxN<?>, MatrixNxN<Q extends Quantity<Q>>> - Parameters:
other- the other collection of the same type- Returns:
- a new vector, matrix or table with an entry-by-entry division of this collection's entries and other's entries
-
multiplyEntries
Description copied from interface:HadamardMultiply the entries of this vector, matrix or table by the given quantity.- Specified by:
multiplyEntriesin interfaceHadamard<MatrixNxN<?>,MatrixNxN<SIQuantity>> - Overrides:
multiplyEntriesin classVectorMatrix<Q extends Quantity<Q>,MatrixNxN<Q extends Quantity<Q>>, MatrixNxN<SIQuantity>, MatrixNxN<?>, MatrixNxN<Q extends Quantity<Q>>> - Parameters:
quantity- the scalar quantity to multiply by- Returns:
- a new vector, matrix or table where the entries have been multiplied by the given quantity
-
hashCode
public int hashCode() -
equals
-
multiply
Multiply this matrix with another matrix using matrix multiplication and return the result.The unit of the result is the SI-unit “sum” of this matrix and the other matrix (i.e.,
U.plus(V)on the underlyingSIUnits).- Parameters:
otherMat- the right-hand matrix to multiply with- Returns:
- the product matrix with the correct SI unit
-
multiply
Multiply this matrix with a column vector, resulting in a column vector.The unit of the result is the SI-unit “sum” of this matrix and the vector (i.e.,
U.plus(V)on the underlyingSIUnits).- Parameters:
otherVec- the column vector to multiply with (sizeN)- Returns:
- the resulting column vector from the multiplication
- Throws:
IllegalArgumentException- if the vector size does not equalorder()
-
checkSquare
protected static int checkSquare(int length) Check if the length if the row-major matrix is a square.- Parameters:
length- of the array to check- Returns:
- square root of the length
- Throws:
IllegalArgumentException- when length is not a square
-
of
Create a new MatrixNxN 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, 13, ..., aN1, aN2, ..., aNN} expressed in the unitunit- the unit of the data, also used as the display unit- Returns:
- a new MatrixNxN with a unit
- Throws:
IllegalArgumentException- when dataInUnit does not contain a square number of values
-
ofSi
Create a MatrixNxN 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, 13, ..., aN1, aN2, ..., aNN} as an array using SI unitsdisplayUnit- the display unit to use- Returns:
- a new MatrixNxN with a unit
- Throws:
IllegalArgumentException- when dataSi does not contain a square number of values
-
of
Create a MatrixNxN 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, 13, ..., aN1, aN2, ..., aNN} expressed as an array of quantities- Returns:
- a new MatrixNxN with a unit
- Throws:
IllegalArgumentException- when data does not contain a square number of quantities
-
ofSi
Create a new MatrixNxN 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, ..., a1N}, ..., {aN1, aN2, ..., aNN}} expressed in the SI or base unitdisplayUnit- the unit of the data, which will also be used as the display unit- Returns:
- a new MatrixNxN with a unit
- Throws:
IllegalArgumentException- when dataInUnit does not contain a square number of values
-
of
Create a new MatrixNxN 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, ..., a1N}, ..., {aN1, aN2, ..., aNN}} expressed in the unitunit- the unit of the values, also used as the display unit- Returns:
- a new MatrixNxN with a unit
- Throws:
IllegalArgumentException- when dataInUnit does not contain a square number of values
-
of
Create a MatrixNxN 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, ..., a1N}, ..., {aN1, aN2, ..., aNN}} expressed as a 2-dimensional array of quantities- Returns:
- a new MatrixNxN with a unit
- Throws:
IllegalArgumentException- when dataInUnit does not contain a square number of quantities
-
as
Return the matrix "as" a matrix with a known quantity, using a unit to express the result in.The SI units of this matrix and the target unit must match; otherwise an
IllegalArgumentExceptionis thrown. The returned matrix shares the SI values but has the specified display unit.- Type Parameters:
TQ- target quantity type- Parameters:
targetUnit- the unit to convert the matrix to- Returns:
- a matrix typed in the target quantity with the specified display unit
- Throws:
IllegalArgumentException- when the units do not match
-
asMatrix1x1
Convert this matrix to aMatrix1x1. The shape must be 1 x 1.- Overrides:
asMatrix1x1in classVectorMatrix<Q extends Quantity<Q>,MatrixNxN<Q extends Quantity<Q>>, MatrixNxN<SIQuantity>, MatrixNxN<?>, MatrixNxN<Q extends Quantity<Q>>> - 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.- Overrides:
asMatrix2x2in classVectorMatrix<Q extends Quantity<Q>,MatrixNxN<Q extends Quantity<Q>>, MatrixNxN<SIQuantity>, MatrixNxN<?>, MatrixNxN<Q extends Quantity<Q>>> - 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.- Overrides:
asMatrix3x3in classVectorMatrix<Q extends Quantity<Q>,MatrixNxN<Q extends Quantity<Q>>, MatrixNxN<SIQuantity>, MatrixNxN<?>, MatrixNxN<Q extends Quantity<Q>>> - Returns:
- a
Matrix3x3with identical SI data and display unit - Throws:
IllegalStateException- if this matrix is not 3 x 3
-