Package org.djunits.util
Class MatrixMath
java.lang.Object
org.djunits.util.MatrixMath
MatrixMath implements a number of methods for linear algebra operations on square matrices, such as LU decomposition,
inverse, trace, etc.
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
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprotected static final classHelper class for LU decomposition with partial pivoting. -
Field Summary
FieldsModifier and TypeFieldDescriptionprotected static final doubleThe default tolerance for operations when no tolerance is given. -
Method Summary
Modifier and TypeMethodDescriptionstatic double[]adjugate(double[] aSi, int n) Calculate the adjugate.static doubledeterminant(double[] aSi, int n) Calculate the determinant, based on the role of Sarrus.protected static doubledetFromLU(MatrixMath.LU luRes, int n) Return the determinant, based on the LU decomposition.static double[]inverse(double[] aSi, int n) Calculate the inverse.protected static booleanisSingularFromLU(MatrixMath.LU luRes, int n, double relTol) Determine whether the matrix is singular, based on the LU decomposition.static booleanisSkewSymmetric(double[] aSi, int n) Return whether the matrix is skew-symmetric, using a default tolerance.static booleanisSkewSymmetric(double[] aSi, int n, double tol) Return whether the matrix is skew-symmetric, within the given tolerance.static booleanisSymmetric(double[] aSi, int n) Return whether the matrix is symmetric, using a default tolerance.static booleanisSymmetric(double[] aSi, int n, double tol) Return whether the matrix is symmetric, within the given tolerance.protected static MatrixMath.LUluDecompose(double[] a, int n) Decompose.protected static voidluSolveInPlace(MatrixMath.LU luRes, int n, double[] b) Solve LU x = b for one right-hand side vector b (vector solve).static double[]multiply(double[] aSi, double[] bSi, int m, int n, int p) Multiply A (m x n, row-major) with B (n x p, row-major) to produce C (m x p, row-major).static doubletrace(double[] aSi, int n) Calculate the trace of the matrix.
-
Field Details
-
DEFAULT_TOL
protected static final double DEFAULT_TOLThe default tolerance for operations when no tolerance is given.- See Also:
-
-
Method Details
-
multiply
public static double[] multiply(double[] aSi, double[] bSi, int m, int n, int p) Multiply A (m x n, row-major) with B (n x p, row-major) to produce C (m x p, row-major). Storage: row-major means A[i,k] is at aSi[i * n + k], B[k,j] at bSi[k * p + j], and C[i,j] at result[i * p + j].- Parameters:
aSi- matrix A, length must be m * n, stored as row-major double[]bSi- matrix B, length must be n * p, stored as row-major double[]m- rows of A (and C)n- columns of A == rows of Bp- columns of B (and C)- Returns:
- C = A * B, as row-major double[] (length m * p)
- Throws:
IllegalArgumentException- if input lengths are inconsistent
-
trace
public static double trace(double[] aSi, int n) Calculate the trace of the matrix.- Parameters:
aSi- the row-major storage of the matrixn- the order of the matrix- Returns:
- the trace of the matrix
-
isSymmetric
public static boolean isSymmetric(double[] aSi, int n) Return whether the matrix is symmetric, using a default tolerance.- Parameters:
aSi- the row-major storage of the matrixn- the order of the matrix- Returns:
- whether the matrix is symmetric
-
isSymmetric
public static boolean isSymmetric(double[] aSi, int n, double tol) Return whether the matrix is symmetric, within the given tolerance.- Parameters:
aSi- the row-major storage of the matrixn- the order of the matrixtol- the tolerance in SI units- Returns:
- whether the matrix is symmetric
-
isSkewSymmetric
public static boolean isSkewSymmetric(double[] aSi, int n) Return whether the matrix is skew-symmetric, using a default tolerance.- Parameters:
aSi- the row-major storage of the matrixn- the order of the matrix- Returns:
- whether the matrix is symmetric
-
isSkewSymmetric
public static boolean isSkewSymmetric(double[] aSi, int n, double tol) Return whether the matrix is skew-symmetric, within the given tolerance.- Parameters:
aSi- the row-major storage of the matrixn- the order of the matrixtol- the tolerance in SI units- Returns:
- whether the matrix is symmetric
-
luDecompose
Decompose.- Parameters:
a- the row-major storage of the matrixn- the order of the square matrix- Returns:
- an LU object containing L and U in one array
-
isSingularFromLU
Determine whether the matrix is singular, based on the LU decomposition.- Parameters:
luRes- The LU resultn- the order of the matrixrelTol- the relative tolerance- Returns:
- whether the matrix is singular
-
detFromLU
Return the determinant, based on the LU decomposition.- Parameters:
luRes- The LU resultn- the order of the matrix- Returns:
- the determinant of the matrix
-
luSolveInPlace
Solve LU x = b for one right-hand side vector b (vector solve).- Parameters:
luRes- The LU resultn- the order of the matrixb- the right-hand side
-
determinant
public static double determinant(double[] aSi, int n) Calculate the determinant, based on the role of Sarrus. See https://en.wikipedia.org/wiki/Rule_of_Sarrus.- Parameters:
aSi- the row-major storage of the matrixn- the order of the matrix- Returns:
- the determinant
-
inverse
Calculate the inverse. Fast methods for n=1, 2, 3. For higher order matrices, the calculation is based on the LU decomposition.- Parameters:
aSi- the row-major storage of the matrixn- the order of the matrix- Returns:
- the inverse of the matrix
- Throws:
NonInvertibleMatrixException- when the matrix cannot be inverted
-
adjugate
public static double[] adjugate(double[] aSi, int n) Calculate the adjugate. Fast methods for n=1, 2, 3.- Parameters:
aSi- the row-major storage of the matrixn- the order of the matrix- Returns:
- the adjugate of the matrix
-