@@ -29,4 +29,46 @@ include("carpanzano_tearing.jl")
2929 @test mm2. ncols == 2
3030 aliases = Dict (1 => sparse ([0 , 0 , 2 , 1 ]))
3131 @test_throws SSel. BadMMAliasError SSel. get_new_mm (aliases, old_to_new_eq, old_to_new_var, mm)
32+
33+ # Three entries collapse onto the same column, and the first two cancel to
34+ # zero. The remaining duplicate must survive as a fresh entry rather than
35+ # being folded into the (now popped) accumulator.
36+ # Row `[2 1 1]`: var1 retained, var2 -> -2*var1, var3 -> 3*var1.
37+ # Contributions to new col 1: 2 (direct) + 1*(-2) + 1*3 = 3.
38+ mm = SSel. CLIL. SparseMatrixCLIL ([2 1 1 ])
39+ old_to_new_eq = [1 ]
40+ old_to_new_var = [1 , 0 , 0 ]
41+ aliases = Dict (2 => sparse ([- 2 , 0 , 0 ]), 3 => sparse ([3 , 0 , 0 ]))
42+ mm2 = SSel. get_new_mm (aliases, old_to_new_eq, old_to_new_var, mm)
43+ @test mm2. nzrows == [1 ]
44+ @test mm2. row_cols == [[1 ]]
45+ @test mm2. row_vals == [[3 ]]
46+ @test mm2. ncols == 1
47+
48+ # Same cancellation, but with a distinct retained column already present
49+ # before the cancelling group. The surviving duplicate must not leak into
50+ # the earlier column.
51+ # Row `[7 2 1 1]`: var1 -> col1, var2 -> col2, var3 -> -2*var2, var4 -> 5*var2.
52+ # col1 = 7, col2 = 2 + 1*(-2) + 1*5 = 5.
53+ mm = SSel. CLIL. SparseMatrixCLIL ([7 2 1 1 ])
54+ old_to_new_eq = [1 ]
55+ old_to_new_var = [1 , 2 , 0 , 0 ]
56+ aliases = Dict (3 => sparse ([0 , - 2 , 0 , 0 ]), 4 => sparse ([0 , 5 , 0 , 0 ]))
57+ mm2 = SSel. get_new_mm (aliases, old_to_new_eq, old_to_new_var, mm)
58+ @test mm2. nzrows == [1 ]
59+ @test mm2. row_cols == [[1 , 2 ]]
60+ @test mm2. row_vals == [[7 , 5 ]]
61+ @test mm2. ncols == 2
62+
63+ # An alias carrying an explicit stored zero (as `sparsevec` produces when
64+ # duplicate indices cancel) must not leak in as a phantom structural nonzero.
65+ # Row references var2 (removed, aliased to 0*var1) and var3 (retained); there
66+ # is no direct var1 term, so the only contribution to col 1 is the zero.
67+ mm = SSel. CLIL. SparseMatrixCLIL ([0 1 1 ])
68+ old_to_new_eq = [1 ]
69+ old_to_new_var = [1 , 0 , 2 ]
70+ aliases = Dict (2 => sparsevec ([1 , 1 ], [2 , - 2 ], 3 ))
71+ mm2 = SSel. get_new_mm (aliases, old_to_new_eq, old_to_new_var, mm)
72+ @test mm2. row_cols == [[2 ]]
73+ @test mm2. row_vals == [[1 ]]
3274end
0 commit comments