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 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 getSiArray() 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 getSiArray() 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 getSiArray() 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 getSiArray() 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>.
    • testTransposeDenseDouble

      @Test @DisplayName("transpose(): DenseDoubleDataSi 2x3 \u2192 3x2, unit preserved, T(T(A))=A") public void testTransposeDenseDouble()
      Verify transpose() for a DenseDoubleDataSi-backed rectangular matrix (2x3 → 3x2): - Row-major mapping: [a11,a12,a13,a21,a22,a23] → [a11,a21,a12,a22,a13,a23]. - Shape swap rows↔cols. - Display unit preserved. - Double transpose returns original SI values.
    • testTransposeDenseFloat

      @Test @DisplayName("transpose(): DenseFloatDataSi 2x3 \u2192 3x2, unit preserved") public void testTransposeDenseFloat()
      Verify transpose() for a DenseFloatDataSi-backed rectangular matrix (2x3 → 3x2): - Correct element remapping in row-major order. - Shape swap rows↔cols. - Display unit preserved.
    • testTransposeSparseDouble

      @Test @DisplayName("transpose(): SparseDoubleDataSi with zeros, 2x3 \u2192 3x2, nnz preserved") public void testTransposeSparseDouble()
      Verify transpose() for a SparseDoubleDataSi-backed rectangular matrix (2x3 → 3x2) containing zeros: - Correct element remapping in row-major order. - Shape swap rows↔cols. - nnz (non-zero count) preserved (zeros remain zeros after transpose).
    • testTransposeSparseFloat

      @Test @DisplayName("transpose(): SparseFloatDataSi with zeros, 2x3 \u2192 3x2, nnz preserved") public void testTransposeSparseFloat()
      Verify transpose() for a SparseFloatDataSi-backed rectangular matrix (2x3 → 3x2) containing zeros: - Correct element remapping in row-major order. - Shape swap rows↔cols. - nnz preserved (zeros remain zeros).
    • testOfDoubleArray

      @Test @DisplayName("of(double[], rows, cols, Unit): nulls, size checks, SI conversion (cm/km)") public void testOfDoubleArray()
      Test MatrixNxM.of(double[], int, int, Unit) for nulls, invalid sizes, and SI conversion using Length.
    • testOfSiDoubleArray

      @Test @DisplayName("ofSi(double[], rows, cols, Unit): nulls, size checks, display unit") public void testOfSiDoubleArray()
      Test MatrixNxM.ofSi(double[], int, int, Unit) for nulls, size errors, and display-unit handling.
    • testOfQuantityArray

      @Test @DisplayName("of(Q[], rows, cols): nulls, size checks, SI conversion") public void testOfQuantityArray()
      Test MatrixNxM.of(Quantity[], int, int) for nulls, size errors, and SI conversion.
    • testOfSiDoubleGrid

      @Test @DisplayName("ofSi(double[][], Unit): nulls, empty/ragged grids, SI values") public void testOfSiDoubleGrid()
      Test MatrixNxM.ofSi(double[][], Unit) for nulls, empty grids, ragged grids, and SI usage.
    • testOfDoubleGridWithUnit

      @Test @DisplayName("of(double[][], Unit): rectangular grid and unit conversion (ms/h)") public void testOfDoubleGridWithUnit()
      Test MatrixNxM.of(double[][], Unit) for unit conversion using Duration.
    • testOfQuantityGrid

      @Test @DisplayName("of(Q[][]): nulls, empty grid, SI conversion") public void testOfQuantityGrid()
      Test MatrixNxM.of(Quantity[][]) for nulls, empty grids, and SI conversion.