Class MatrixNxNTest

java.lang.Object
org.djunits.vecmat.dn.MatrixNxNTest

public class MatrixNxNTest extends Object
Unit tests for MatrixNxN (using n=4) with concrete quantity Length and unit Length.Unit.

The tests encode intended behavior (display → SI conversion, unit algebra via SIUnit.plus(SIUnit) / SIUnit.add(SIUnit, SIUnit), etc.). They fail if the implementation deviates—by design.

Coverage:

  • Factories (of(double[],U), of(double[][],U))
  • instantiateSi(double[])
  • Matrix basics: rows(), cols(), value(r,c), setDisplayUnit, toString
  • Ops defaults: add/sub (Q & VM), negate, abs, scaleBy, stats
  • SquareMatrixOps: transpose, determinantScalar, determinant, trace, normFrobenius, isSymmetric (± tol), isSkewSymmetric (± tol)
  • Inverse/adjugate (success + singular), matrixxmatrix, matrixxvector
  • Hadamard: invertElements, multiplyElements, divideElements (+ unit composition)
  • as(targetUnit) success/failure
  • Scalar extraction helpers: getScalars, getRowScalars, getColumnScalars, getDiagonalScalars
  • equals/hashCode
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

    • MatrixNxNTest

      public MatrixNxNTest()
  • Method Details

    • testFactoryArray

      @Test @DisplayName("of(double[],U): nulls, length, SI conversion") public void testFactoryArray()
      Verify MatrixNxN.of(double[], UnitInterface) rejects nulls, wrong lengths, and converts to SI.
    • testFactoryGrid

      @Test @DisplayName("of(double[][],U): shape & SI conversion") public void testFactoryGrid()
      Verify MatrixNxN.of(double[][], UnitInterface) checks 4x4 shape and converts to SI.
    • testInstantiateSi

      @Test @DisplayName("instantiateSi(double[]): display unit preserved, SI adopted") public void testInstantiateSi()
      Verify MatrixNxN.instantiateSi(double[]) preserves display unit and adopts SI values.
    • testConstructor

      @Test @DisplayName("MatrixNxN constructor") public void testConstructor()
      Verify constructor.
    • testShapeAndValue

      @Test @DisplayName("rows/cols/value/isRelative") public void testShapeAndValue()
    • testSetDisplayUnit

      @Test @DisplayName("setDisplayUnit() only changes presentation") public void testSetDisplayUnit()
      Verify that VectorMatrix.setDisplayUnit(UnitInterface) only affects presentation (not SI storage).
    • testToString

      @Test @DisplayName("toString()/toString(unit) include unit") public void testToString()
    • testAlgebraAndStats

      @Test @DisplayName("add/sub (Q & VM), negate/abs/scaleBy, stats") public void testAlgebraAndStats()
      Verify vector/matrix algebra defaults and statistics.
    • testSquareOps

      @Test @DisplayName("transpose, determinant/trace/Frobenius, symmetry") public void testSquareOps()
      Verify transpose, determinant/trace/normFrobenius, symmetry checks.
    • testInverseAdjugate

      @Test @DisplayName("inverse(): singular throws; adjugate() exists") public void testInverseAdjugate()
      Verify MatrixNxN.inverse() throws on singular and MatrixNxN.adjugate() shape.
    • testMultiply

      @Test @DisplayName("matrix x matrix and matrix x vector") public void testMultiply()
      Verify matrixxmatrix and matrixxvector multiplication with unit composition.
    • testHadamard

      @Test @DisplayName("Hadamard: invert/multiply/divide + unit composition") public void testHadamard()
      Verify element-wise Hadamard operations and unit composition.
    • testAs

      @Test @DisplayName("as(targetUnit) success/failure") public void testAs()
      Verify MatrixNxN.as(UnitInterface) success (m↔km) and failure (dimension mismatch).
    • testScalarExtraction

      @Test @DisplayName("getScalars / getRowScalars / getColumnScalars / getDiagonalScalars") public void testScalarExtraction()
      Verify scalar extraction helpers: getScalars, getRowScalars, getColumnScalars, and getDiagonalScalars.
    • testSiArrayExtraction

      @Test @DisplayName("getRowSi / getColumnSi / getDiagonalSi") public void testSiArrayExtraction()
      Verify SI extraction helpers: getRowSi, getColumnSi, and getDiagonalSi.
    • testVectorExtraction

      @Test @DisplayName("getRowVector / getColumnVector / getDiagonalVector") public void testVectorExtraction()
      Verify scalar extraction helpers: getRowVector, getColumnVector, and getDiagonalVector.
    • testEqualsHash

      @Test @DisplayName("equals / hashCode") public void testEqualsHash()
      Verify equals/hashCode for equal and different data.
    • testMultiplyScalarAs

      @Test public void testMultiplyScalarAs()
      Test multiply/divide by scalar and as() method.
    • testAsMatrix1x1

      @Test @DisplayName("MatrixNxN: asMatrix1x1 preserves SI & unit for 1x1; throws for N\u22601") public void testAsMatrix1x1()
      Verify that asMatrix1x1() preserves SI and display unit for a 1x1 matrix, and throws IllegalStateException for other N (N=2 and N=3).

      Note: The current implementation appears to have a bug: it checks cols() != 2 instead of cols() != 1, which will make this test fail until fixed.

    • testAsMatrix2x2

      @Test @DisplayName("MatrixNxN: asMatrix2x2 preserves SI & unit for 2x2; throws for N\u22602") public void testAsMatrix2x2()
      Verify that asMatrix2x2() preserves SI and display unit for a 2x2 matrix, and throws IllegalStateException for other N (tested with N=1 and N=3).
    • testAsMatrix3x3

      @Test @DisplayName("MatrixNxN: asMatrix3x3 preserves SI & unit for 3x3; throws for N\u22603") public void testAsMatrix3x3()
      Verify that asMatrix3x3() preserves SI and display unit for a 3x3 matrix, and throws IllegalStateException for other N (tested with N=2 and N=4).