Class MatrixMath

java.lang.Object
org.djunits.util.MatrixMath

public final class MatrixMath extends Object
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 Classes
    Modifier and Type
    Class
    Description
    protected static final class 
    Helper class for LU decomposition with partial pivoting.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    protected static final double
    The default tolerance for operations when no tolerance is given.
  • Method Summary

    Modifier and Type
    Method
    Description
    static double[]
    adjugate(double[] aSi, int n)
    Calculate the adjugate.
    static double
    determinant(double[] aSi, int n)
    Calculate the determinant, based on the role of Sarrus.
    protected static double
    detFromLU(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 boolean
    isSingularFromLU(MatrixMath.LU luRes, int n, double relTol)
    Determine whether the matrix is singular, based on the LU decomposition.
    static boolean
    isSkewSymmetric(double[] aSi, int n)
    Return whether the matrix is skew-symmetric, using a default tolerance.
    static boolean
    isSkewSymmetric(double[] aSi, int n, double tol)
    Return whether the matrix is skew-symmetric, within the given tolerance.
    static boolean
    isSymmetric(double[] aSi, int n)
    Return whether the matrix is symmetric, using a default tolerance.
    static boolean
    isSymmetric(double[] aSi, int n, double tol)
    Return whether the matrix is symmetric, within the given tolerance.
    protected static MatrixMath.LU
    luDecompose(double[] a, int n)
    Decompose.
    protected static void
    luSolveInPlace(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 double
    trace(double[] aSi, int n)
    Calculate the trace of the matrix.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • DEFAULT_TOL

      protected static final double DEFAULT_TOL
      The 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 B
      p - 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 matrix
      n - 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 matrix
      n - 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 matrix
      n - the order of the matrix
      tol - 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 matrix
      n - 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 matrix
      n - the order of the matrix
      tol - the tolerance in SI units
      Returns:
      whether the matrix is symmetric
    • luDecompose

      protected static MatrixMath.LU luDecompose(double[] a, int n)
      Decompose.
      Parameters:
      a - the row-major storage of the matrix
      n - the order of the square matrix
      Returns:
      an LU object containing L and U in one array
    • isSingularFromLU

      protected static boolean isSingularFromLU(MatrixMath.LU luRes, int n, double relTol)
      Determine whether the matrix is singular, based on the LU decomposition.
      Parameters:
      luRes - The LU result
      n - the order of the matrix
      relTol - the relative tolerance
      Returns:
      whether the matrix is singular
    • detFromLU

      protected static double detFromLU(MatrixMath.LU luRes, int n)
      Return the determinant, based on the LU decomposition.
      Parameters:
      luRes - The LU result
      n - the order of the matrix
      Returns:
      the determinant of the matrix
    • luSolveInPlace

      protected static void luSolveInPlace(MatrixMath.LU luRes, int n, double[] b)
      Solve LU x = b for one right-hand side vector b (vector solve).
      Parameters:
      luRes - The LU result
      n - the order of the matrix
      b - 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 matrix
      n - the order of the matrix
      Returns:
      the determinant
    • inverse

      public static double[] inverse(double[] aSi, int n) throws NonInvertibleMatrixException
      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 matrix
      n - 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 matrix
      n - the order of the matrix
      Returns:
      the adjugate of the matrix