Class MatrixNxMTest

java.lang.Object
org.djunits.vecmat.dnxm.MatrixNxMTest

public class MatrixNxMTest extends Object
Unit tests for MatrixNxM with concrete quantity Length and unit Length.Unit.

Tests cover factories, instantiateSi, algebra/stats (defaults), Hadamard ops, matrixxmatrix, matrixxvector, “as” conversions to square matrices and vectors, scalar extraction helpers, equals/hashCode, and display-unit behavior. 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 Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    Verify add/sub (Q & VM), negate/abs/scaleBy, stats on 2x3 matrix.
    void
    Verify as* matrix conversions and shape checks.
    void
    Verify that asMatrixNxN() preserves SI data and display unit for square matrices (tested with 1x1 and 4x4), and throws IllegalStateException for non-square shapes (tested with 2x3 and 3x2).
    void
    Verify as(targetUnit) success (m↔km) and failure (dimension mismatch).
    void
    Verify as* vector conversions for 2/3/N Col and Row, plus shape checks.
    void
    Verify that asVectorNCol(): Preserves SI data and display unit for N x 1 matrices (tested with N=4 and N=1), Throws IllegalStateException when the matrix has more than one column (e.g., N x 2). SI correctness is validated via both the row-major si() array and element access on the returned vector.
    void
    Verify that asVectorNRow(): Preserves SI data and display unit for 1 x N matrices (tested with N=4 and N=1), Throws IllegalStateException when the matrix has more than one row (e.g., 2 x N). SI correctness is validated via both the row-major si() array and element access on the returned vector.
    void
    Verify equals/hashCode.
    void
    Verify of(double[], rows, cols, U) rejects nulls/bad sizes and converts display→SI.
    void
    Verify of(double[][],U) checks rectangular shape and converts display→SI.
    void
    Verify of(Q[][],U) accepts per-cell units via DenseDoubleData and sets display unit.
    void
    Verify Hadamard invert/multiply/divide with unit composition.
    void
    Verify instantiateSi preserves display unit and adopts provided SI.
    void
    Verify MatrixNxM conversions to fixed-size matrices preserve SI data and display unit, and that shape checks throw IllegalStateException for the mismatch branches covered.
    void
    Verify MatrixNxM conversions to quantity table preserve SI data and display unit, and that shape checks throw IllegalStateException for the mismatch branches covered.
    void
    Verify MatrixNxM conversions to fixed-size vectors and matrices preserve SI data and display unit, and that shape checks throw IllegalStateException with both row- and column-mismatch branches covered.
    void
    Verify Nx2 x 2x2 and Nx3 x 3x3 multiplication variants.
    void
    Verify general (NxM)x(MxP) multiplication and unit composition.
    void
    (Mx1) x (1x1) -> (Mx1).
    void
    (Mx2) x (2x2) -> (Mx2).
    void
    (Mx3) x (3x3) -> (Mx3).
    void
    (NxM) x (MxP) where rhs is NxN: verify both branches of getDataGrid().isDouble() by constructing a double-backed and a float-backed MatrixNxN.
    void
    Verify (2x3) x (3x2) multiplication with a double-backed RHS matrix: Numeric correctness via si() and si(r,c). Unit composition via as(Area.Unit.*). Result shape (2x2).
    void
    Verify (2x3) x (3x2) multiplication with a float-backed RHS matrix to cover the DenseFloatDataSi branch.
    void
    Test multiply/divide by scalar and as() method.
    void
    Verify A(Nx2)·v2 and A(Nx3)·v3 and A(NxM)·vN return VectorN.Col with correct unit.
    void
    (Mx1) x (1x1) -> (Mx1) MatrixNxM<SIQuantity, SIUni>.
    void
    (Mx2) x (2x1) -> (Mx1) VectorN.Col<SIQuantity, SIUnit>.
    void
    (Mx3) x (3x1) -> (Mx1) VectorN.Col<SIQuantity, SIUnit>.
    void
    (MxN) x (Nx1) -> (Mx1) VectorN.Col<SIQuantity, SIUnit>.
    void
    Verify scalar extraction helpers.
    void
    setDisplayUnit only affects presentation; SI unchanged.
    void
    Verify rows/cols/value/isRelative on a 3x2 matrix.
    void
    Verify double extraction helpers.
    void
    toString and toString(unit) include unit abbreviation.
    void
    Verify vector extraction helpers.

    Methods inherited from class java.lang.Object

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

    • MatrixNxMTest

      public MatrixNxMTest()
  • Method Details

    • testFactoryArray

      @Test @DisplayName("of(double[],rows,cols,U): nulls/bad sizes/SI convert") public void testFactoryArray()
      Verify of(double[], rows, cols, U) rejects nulls/bad sizes and converts display→SI.
    • testFactoryGrid

      @Test @DisplayName("of(double[][],U): rectangular shape & SI convert") public void testFactoryGrid()
      Verify of(double[][],U) checks rectangular shape and converts display→SI.
    • testFactoryQuantityGrid

      @Test @DisplayName("of(Q[][],U): quantity grid accepted") public void testFactoryQuantityGrid()
      Verify of(Q[][],U) accepts per-cell units via DenseDoubleData and sets display unit.
    • testInstantiateSi

      @Test @DisplayName("instantiateSi: preserve display unit, adopt SI") public void testInstantiateSi()
      Verify instantiateSi preserves display unit and adopts provided SI.
    • testShapeAndValue

      @Test @DisplayName("rows/cols/value/isRelative") public void testShapeAndValue()
      Verify rows/cols/value/isRelative on a 3x2 matrix.
    • testSetDisplayUnit

      @Test @DisplayName("setDisplayUnit() only changes presentation") public void testSetDisplayUnit()
      setDisplayUnit only affects presentation; SI unchanged.
    • testToString

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

      @Test @DisplayName("add/sub (Q & VM), negate/abs/scaleBy, stats") public void testAlgebraAndStats()
      Verify add/sub (Q & VM), negate/abs/scaleBy, stats on 2x3 matrix.
    • testHadamard

      @Test @DisplayName("Hadamard: invert/multiply/divide + unit composition") public void testHadamard()
      Verify Hadamard invert/multiply/divide with unit composition.
    • testMultiplyGeneral

      @Test @DisplayName("A(NxM) x B(MxP) \u2192 C(NxP)") public void testMultiplyGeneral()
      Verify general (NxM)x(MxP) multiplication and unit composition.
    • testMultiply2x2And3x3

      @Test @DisplayName("A(Nx2)xB(2x2), A(Nx3)xB(3x3)") public void testMultiply2x2And3x3()
      Verify Nx2 x 2x2 and Nx3 x 3x3 multiplication variants.
    • testMultiplyVector

      @Test @DisplayName("A\u00b7v: size 2, size 3, and size N") public void testMultiplyVector()
      Verify A(Nx2)·v2 and A(Nx3)·v3 and A(NxM)·vN return VectorN.Col with correct unit.
    • testAsTargetUnit

      @Test @DisplayName("as(targetUnit) success/failure") public void testAsTargetUnit()
      Verify as(targetUnit) success (m↔km) and failure (dimension mismatch).
    • testAsMatrixConversions

      @Test @DisplayName("asMatrix2x2 / asMatrix3x3 / asMatrixNxN") public void testAsMatrixConversions()
      Verify as* matrix conversions and shape checks.
    • testAsVectorConversions

      @Test @DisplayName("asVector2/3/N (Col/Row) conversions") public void testAsVectorConversions()
      Verify as* vector conversions for 2/3/N Col and Row, plus shape checks.
    • testScalarExtraction

      @Test @DisplayName("getScalars / getRowScalars / getColumnScalars") public void testScalarExtraction()
      Verify scalar extraction helpers.
    • testVectorExtraction

      @Test @DisplayName("getRowVector / getColumnVector") public void testVectorExtraction()
      Verify vector extraction helpers.
    • testSiArrayExtraction

      @Test @DisplayName("getRowSi / getColumnSi") public void testSiArrayExtraction()
      Verify double extraction helpers.
    • testEqualsHash

      @Test @DisplayName("equals / hashCode") public void testEqualsHash()
      Verify equals/hashCode.
    • testMultiplyScalarAs

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

      @Test @DisplayName("MatrixNxM: asVector preserve SI and unit; row/col mismatch branches throw") public void testMatrixNxMAsVectorConversions()
      Verify MatrixNxM conversions to fixed-size vectors and matrices preserve SI data and display unit, and that shape checks throw IllegalStateException with both row- and column-mismatch branches covered.

      Uses kilometers for column-shaped tests and centimeters for row-shaped tests to exercise SI conversion.

    • testMatrixNxMAsMatrixConversions

      @Test @DisplayName("MatrixNxM: asMatrix preserve SI and unit") public void testMatrixNxMAsMatrixConversions()
      Verify MatrixNxM conversions to fixed-size matrices preserve SI data and display unit, and that shape checks throw IllegalStateException for the mismatch branches covered.
    • testMatrixNxMAsQuantityTable

      @Test @DisplayName("MatrixNxM: asQuantityTable preserve SI and unit") public void testMatrixNxMAsQuantityTable()
      Verify MatrixNxM conversions to quantity table preserve SI data and display unit, and that shape checks throw IllegalStateException for the mismatch branches covered.
    • testAsMatrixNxN

      @Test @DisplayName("MatrixNxM: asMatrixNxN preserves SI & unit (square) and throws for non-square shapes") public void testAsMatrixNxN()
      Verify that asMatrixNxN() preserves SI data and display unit for square matrices (tested with 1x1 and 4x4), and throws IllegalStateException for non-square shapes (tested with 2x3 and 3x2). SI correctness is checked via both the row-major si() array and si(row, col) with 0-based indices.
    • testAsVectorNCol

      @Test @DisplayName("MatrixNxM: asVectorNCol preserves SI & unit (Nx1) and throws for Nx2") public void testAsVectorNCol()
      Verify that asVectorNCol():
      • Preserves SI data and display unit for N x 1 matrices (tested with N=4 and N=1),
      • Throws IllegalStateException when the matrix has more than one column (e.g., N x 2).
      SI correctness is validated via both the row-major si() array and element access on the returned vector.
    • testAsVectorNRow

      @Test @DisplayName("MatrixNxM: asVectorNRow preserves SI & unit (1xN) and throws for 2xN") public void testAsVectorNRow()
      Verify that asVectorNRow():
      • Preserves SI data and display unit for 1 x N matrices (tested with N=4 and N=1),
      • Throws IllegalStateException when the matrix has more than one row (e.g., 2 x N).
      SI correctness is validated via both the row-major si() array and element access on the returned vector.
    • testMultiplyMatrix1x1

      @Test @DisplayName("MatrixNxM.multiply(Matrix1x1): happy path and bad dimension mismatch") public void testMultiplyMatrix1x1()
      (Mx1) x (1x1) -> (Mx1). Verify numeric correctness, unit composition (Length×Length→Area) via as(Area.Unit.*), and bad path when cols(this) != rows(rhs).
    • testMultiplyMatrix2x2

      @Test @DisplayName("MatrixNxM.multiply(Matrix2x2): happy path and bad dimension mismatch") public void testMultiplyMatrix2x2()
      (Mx2) x (2x2) -> (Mx2).
    • testMultiplyMatrix3x3

      @Test @DisplayName("MatrixNxM.multiply(Matrix3x3): happy path and bad dimension mismatch") public void testMultiplyMatrix3x3()
      (Mx3) x (3x3) -> (Mx3).
    • testMultiplyMatrixNxNDoubleAndFloatBranches

      @Test @DisplayName("MatrixNxM.multiply(MatrixNxN): double and float storage branches + bad dimension") public void testMultiplyMatrixNxNDoubleAndFloatBranches()
      (NxM) x (MxP) where rhs is NxN: verify both branches of getDataGrid().isDouble() by constructing a double-backed and a float-backed MatrixNxN.
    • testMultiplyNxMWithNxMDoubleRhs

      @Test @DisplayName("MatrixNxM.multiply(MatrixNxM): 2x3 x 3x2 with double-backed RHS") public void testMultiplyNxMWithNxMDoubleRhs()
      Verify (2x3) x (3x2) multiplication with a double-backed RHS matrix:
      • Numeric correctness via si() and si(r,c).
      • Unit composition via as(Area.Unit.*).
      • Result shape (2x2).
    • testMultiplyNxMWithNxMFloatRhs

      @Test @DisplayName("MatrixNxM.multiply(MatrixNxM): 2x3 x 3x2 with float-backed RHS") public void testMultiplyNxMWithNxMFloatRhs()
      Verify (2x3) x (3x2) multiplication with a float-backed RHS matrix to cover the DenseFloatDataSi branch.
    • testMultiplyVector1

      @Test @DisplayName("MatrixNxM.multiply(Vector1): happy path and bad dimension mismatch") public void testMultiplyVector1()
      (Mx1) x (1x1) -> (Mx1) MatrixNxM<SIQuantity, SIUni>.
    • testMultiplyVector2Col

      @Test @DisplayName("MatrixNxM.multiply(Vector2.Col): happy path and bad dimension mismatch") public void testMultiplyVector2Col()
      (Mx2) x (2x1) -> (Mx1) VectorN.Col<SIQuantity, SIUnit>.
    • testMultiplyVector3Col

      @Test @DisplayName("MatrixNxM.multiply(Vector3.Col): happy path and bad dimension mismatch") public void testMultiplyVector3Col()
      (Mx3) x (3x1) -> (Mx1) VectorN.Col<SIQuantity, SIUnit>.
    • testMultiplyVectorNCol

      @Test @DisplayName("MatrixNxM.multiply(VectorN.Col): happy path and bad dimension mismatch") public void testMultiplyVectorNCol()
      (MxN) x (Nx1) -> (Mx1) VectorN.Col<SIQuantity, SIUnit>.