Skip to content

Commit 21ab97d

Browse files
Add regression test for issue #564
This test ensures that solve() does not throw `MethodError: no method matching zero(::Type{Any})` when sparse regression produces all-zero coefficients. This can happen when: 1. Input data has very small values that become zero after regularization 2. High regularization forces all coefficients to zero 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <[email protected]>
1 parent e3623fc commit 21ab97d

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

lib/DataDrivenSparse/test/sparse_linear_solve.jl

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,3 +70,40 @@ end
7070
@test vec(rescoeff)[0.25; 0.0; -1.0; 0.11; 0.0; -0.5] atol=5e-2
7171
end
7272
end
73+
74+
# Issue #564: Test that solve doesn't throw MethodError when coefficients are all zero
75+
# This can happen with very small data values or high regularization
76+
@testset "Zero coefficients handling (Issue #564)" begin
77+
rng = StableRNG(1111)
78+
79+
# Test case 1: Very small data values that lead to zero coefficients after regularization
80+
N = 3
81+
= randn(rng, N, 50) * 1e-10
82+
Ŷ = randn(rng, 1, 50) * 1e-10
83+
84+
@variables u[1:N]
85+
b = polynomial_basis(u, 2)
86+
basis = Basis(b, u)
87+
problem = DirectDataDrivenProblem(X̂, Ŷ)
88+
89+
λ = 1e-1
90+
opt = ADMM(λ)
91+
options = DataDrivenCommonOptions()
92+
93+
# This should not throw MethodError: no method matching zero(::Type{Any})
94+
result = @test_nowarn solve(problem, basis, opt, options = options)
95+
@test result isa DataDrivenSolution
96+
@test eltype(result.prob) == Float64
97+
98+
# Test case 2: High regularization that forces all coefficients to zero
99+
X̂2 = randn(rng, N, 50)
100+
Ŷ2 = randn(rng, 1, 50)
101+
problem2 = DirectDataDrivenProblem(X̂2, Ŷ2)
102+
103+
λ_high = 1e10 # Very high regularization
104+
opt_high = ADMM(λ_high)
105+
106+
result2 = @test_nowarn solve(problem2, basis, opt_high, options = options)
107+
@test result2 isa DataDrivenSolution
108+
@test eltype(result2.prob) == Float64
109+
end

0 commit comments

Comments
 (0)