|
| 1 | +"""Tests for combinatorial-matrix operations.""" |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +import pytest |
| 6 | +from pydantic import ValidationError |
| 7 | + |
| 8 | +from jacobian.math.combinatorial_matrices import HadamardMatrix, SignMatrix |
| 9 | +from jacobian.math.combinatorial_matrices._models import ( |
| 10 | + DeterminantProfileRequest, |
| 11 | + GramProfileRequest, |
| 12 | + NormalizeRequest, |
| 13 | + SignProfileRequest, |
| 14 | + SylvesterRequest, |
| 15 | +) |
| 16 | +from jacobian.math.combinatorial_matrices._operations import ( |
| 17 | + compute_determinant_profile, |
| 18 | + compute_gram_profile, |
| 19 | + compute_normalize, |
| 20 | + compute_sign_profile, |
| 21 | + compute_sylvester, |
| 22 | +) |
| 23 | +from jacobian.math.combinatorial_matrices._tools import TOOLS |
| 24 | + |
| 25 | +# --------------------------------------------------------------------------- |
| 26 | +# Helpers |
| 27 | +# --------------------------------------------------------------------------- |
| 28 | + |
| 29 | + |
| 30 | +def _h2() -> SignMatrix: |
| 31 | + return SignMatrix(rows=((1, 1), (1, -1))) |
| 32 | + |
| 33 | + |
| 34 | +def _non_hadamard() -> SignMatrix: |
| 35 | + """A sign matrix that is NOT Hadamard: all +1 2x2.""" |
| 36 | + return SignMatrix(rows=((1, 1), (1, 1))) |
| 37 | + |
| 38 | + |
| 39 | +# --------------------------------------------------------------------------- |
| 40 | +# Catalog |
| 41 | +# --------------------------------------------------------------------------- |
| 42 | + |
| 43 | + |
| 44 | +def test_catalog_contains_only_audited_agent_outcomes() -> None: |
| 45 | + assert {tool.operation_id for tool in TOOLS} == { |
| 46 | + "matrix.sign.profile.compute", |
| 47 | + "matrix.hadamard.gram_profile.compute", |
| 48 | + "matrix.hadamard.normalize.compute", |
| 49 | + "matrix.hadamard.determinant_profile.compute", |
| 50 | + "matrix.hadamard.sylvester.compute", |
| 51 | + } |
| 52 | + |
| 53 | + |
| 54 | +# --------------------------------------------------------------------------- |
| 55 | +# Sign profile |
| 56 | +# --------------------------------------------------------------------------- |
| 57 | + |
| 58 | + |
| 59 | +class TestSignProfile: |
| 60 | + def test_h2_profile(self) -> None: |
| 61 | + result = compute_sign_profile(SignProfileRequest(matrix=_h2())) |
| 62 | + assert result.row_count == 2 |
| 63 | + assert result.column_count == 2 |
| 64 | + assert result.plus_one_count == 3 |
| 65 | + assert result.minus_one_count == 1 |
| 66 | + assert result.is_square is True |
| 67 | + assert result.row_sums == (2, 0) |
| 68 | + |
| 69 | + |
| 70 | +# --------------------------------------------------------------------------- |
| 71 | +# Gram profile |
| 72 | +# --------------------------------------------------------------------------- |
| 73 | + |
| 74 | + |
| 75 | +class TestGramProfile: |
| 76 | + def test_h2_is_hadamard(self) -> None: |
| 77 | + result = compute_gram_profile(GramProfileRequest(matrix=_h2())) |
| 78 | + assert result.order == 2 |
| 79 | + assert result.is_hadamard is True |
| 80 | + assert result.gram == ((2, 0), (0, 2)) |
| 81 | + assert result.diagonal_residuals == (0, 0) |
| 82 | + assert result.nonzero_off_diagonal == () |
| 83 | + |
| 84 | + def test_non_hadamard(self) -> None: |
| 85 | + result = compute_gram_profile( |
| 86 | + GramProfileRequest(matrix=_non_hadamard()) |
| 87 | + ) |
| 88 | + assert result.is_hadamard is False |
| 89 | + assert result.gram == ((2, 2), (2, 2)) |
| 90 | + |
| 91 | + |
| 92 | +# --------------------------------------------------------------------------- |
| 93 | +# Normalize |
| 94 | +# --------------------------------------------------------------------------- |
| 95 | + |
| 96 | + |
| 97 | +class TestNormalize: |
| 98 | + def test_h2_normalize_idempotent(self) -> None: |
| 99 | + result = compute_normalize(NormalizeRequest(matrix=_h2())) |
| 100 | + # H2 already has first row/column all +1. |
| 101 | + assert result.normalized == ((1, 1), (1, -1)) |
| 102 | + assert result.row_switches == (0, 0) |
| 103 | + assert result.column_switches == (0, 0) |
| 104 | + |
| 105 | + def test_normalize_flips(self) -> None: |
| 106 | + matrix = SignMatrix(rows=((-1, -1), (-1, 1))) |
| 107 | + result = compute_normalize(NormalizeRequest(matrix=matrix)) |
| 108 | + assert result.normalized == ((1, 1), (1, -1)) |
| 109 | + assert result.column_switches == (1, 1) |
| 110 | + assert result.row_switches == (0, 0) |
| 111 | + |
| 112 | + |
| 113 | +# --------------------------------------------------------------------------- |
| 114 | +# Determinant profile |
| 115 | +# --------------------------------------------------------------------------- |
| 116 | + |
| 117 | + |
| 118 | +class TestDeterminantProfile: |
| 119 | + def test_h2_determinant(self) -> None: |
| 120 | + h = HadamardMatrix(rows=((1, 1), (1, -1))) |
| 121 | + result = compute_determinant_profile(DeterminantProfileRequest(matrix=h)) |
| 122 | + assert result.order == 2 |
| 123 | + assert result.determinant_magnitude == 2 # 2^(2/2) = 2 |
| 124 | + assert result.gram_determinant == 4 # 2^2 |
| 125 | + |
| 126 | + |
| 127 | +# --------------------------------------------------------------------------- |
| 128 | +# Sylvester |
| 129 | +# --------------------------------------------------------------------------- |
| 130 | + |
| 131 | + |
| 132 | +class TestSylvester: |
| 133 | + def test_sylvester_k0(self) -> None: |
| 134 | + result = compute_sylvester(SylvesterRequest(k=0)) |
| 135 | + assert result.order == 1 |
| 136 | + assert result.matrix == ((1,),) |
| 137 | + |
| 138 | + def test_sylvester_k1(self) -> None: |
| 139 | + result = compute_sylvester(SylvesterRequest(k=1)) |
| 140 | + assert result.order == 2 |
| 141 | + assert result.matrix == ((1, 1), (1, -1)) |
| 142 | + |
| 143 | + def test_sylvester_k2_is_hadamard(self) -> None: |
| 144 | + result = compute_sylvester(SylvesterRequest(k=2)) |
| 145 | + assert result.order == 4 |
| 146 | + h = HadamardMatrix(rows=result.matrix) |
| 147 | + assert len(h.rows) == 4 |
| 148 | + |
| 149 | + |
| 150 | +# --------------------------------------------------------------------------- |
| 151 | +# Validation |
| 152 | +# --------------------------------------------------------------------------- |
| 153 | + |
| 154 | + |
| 155 | +class TestValidation: |
| 156 | + def test_non_sign_entry_rejected(self) -> None: |
| 157 | + with pytest.raises(ValidationError, match="sign matrix entries"): |
| 158 | + SignMatrix(rows=((1, 0), (0, 1))) |
| 159 | + |
| 160 | + def test_non_hadamard_rejected(self) -> None: |
| 161 | + with pytest.raises(ValidationError, match="orthogonality"): |
| 162 | + HadamardMatrix(rows=((1, 1), (1, 1))) |
| 163 | + |
| 164 | + def test_non_square_hadamard_rejected(self) -> None: |
| 165 | + with pytest.raises(ValidationError, match="square"): |
| 166 | + HadamardMatrix(rows=((1, 1, 1), (1, -1, 1))) |
| 167 | + |
| 168 | + def test_unequal_row_lengths_rejected(self) -> None: |
| 169 | + with pytest.raises(ValidationError, match="equal length"): |
| 170 | + SignMatrix(rows=((1, 1, 1), (1, -1))) |
0 commit comments