Class MatrixMathTest

java.lang.Object
org.djunits.util.MatrixMathTest

public class MatrixMathTest extends Object
Unit tests for MatrixMath. The suite aims for 100% line and branch coverage by exercising:
  • Multiplication (rectangular), including length-mismatch error paths
  • Trace, symmetry and skew-symmetry with default and explicit tolerances
  • Determinant: closed-form (n=1,2,3) and LU-based (n≥4)
  • Inverse: closed-form (n=1,2,3) and LU-based (n≥4), including singular matrix exceptions
  • Adjugate: closed-form (n=1,2,3) and general (n≥4), validated via A·adj(A)=det(A)·I
  • Coverage of the private constructor

Notes:

  • All matrices are provided in row-major layout, matching MatrixMath contracts.
  • Floating-point comparisons use a tight tolerance; identities from inversion are validated numerically.
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 (specifications); Test implementation by Copilot.
  • Constructor Details

    • MatrixMathTest

      public MatrixMathTest()
  • Method Details

    • privateConstructorCoverage

      @Test void privateConstructorCoverage() throws Exception
      Covers the private constructor of MatrixMath via reflection for complete coverage.
      Throws:
      Exception - if reflective construction fails
    • multiplyRectangularAndErrors

      @Test void multiplyRectangularAndErrors()
      Verifies rectangular matrix multiplication and length-mismatch precondition errors.
    • traceOfMatrix

      @Test void traceOfMatrix()
      Verifies MatrixMath.trace(double[], int) on simple matrices.
    • symmetryChecks

      @Test void symmetryChecks()
      Verifies symmetry checks with default and explicit tolerances.
    • determinantVariants

      @Test void determinantVariants()
      Verifies determinant for n=1,2,3 (closed-form) and n≥4 (LU path).
    • inverseVariants

      @Test void inverseVariants() throws Exception
      Verifies inverse for n=1,2,3 and n≥4, including singular matrix exceptions.
      Throws:
      Exception - if inversion unexpectedly fails
    • adjugateVariants

      @Test void adjugateVariants()
      Verifies adjugate for n=1,2,3 and n≥4 via the identity A·adj(A) = det(A)·I.
    • testAdjugateAllBranches

      @Test public void testAdjugateAllBranches()
      Test the adjugate(double[], int) method for correctness and full branch coverage. Covers fast paths for n=1, n=2, n=3 as well as the general case for larger n, ensuring that minors of size m=1, m=2, m=3 and m>3 are exercised. For m>3, LU-based determinant computation is invoked indirectly to complete coverage.

      Correctness is evaluated by using the mathematical identity: adj(A) = det(A) * inverse(A). For small matrices (1x1, 2x2, 3x3), expected adjugates are computed analytically. For larger matrices (4x4, 5x5), correctness is verified by checking that A * adj(A) equals det(A) times the identity matrix.