Skip to content

Commit 1393aff

Browse files
authored
Fix Cholesky decompoosition error when given different element type (#2959)
1 parent 6dec535 commit 1393aff

2 files changed

Lines changed: 18 additions & 6 deletions

File tree

src/Bridges/Constraint/bridges/QuadtoSOCBridge.jl

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,12 @@ function compute_sparse_sqrt_fallback(Q, ::F, ::S) where {F,S}
8080
end
8181

8282
function compute_sparse_sqrt(Q, func, set)
83-
factor = LinearAlgebra.cholesky(Q; check = false)
83+
factor = try
84+
LinearAlgebra.cholesky(Q; check = false)
85+
catch
86+
msg = "There was an error computing a Cholesky decomposition"
87+
throw(MOI.UnsupportedConstraint{typeof(func),typeof(set)}(msg))
88+
end
8489
if !LinearAlgebra.issuccess(factor)
8590
return compute_sparse_sqrt_fallback(Q, func, set)
8691
end

test/Bridges/Constraint/test_QuadtoSOCBridge.jl

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -356,9 +356,7 @@ function test_semidefinite_cholesky_fail()
356356
end
357357

358358
function test_compute_sparse_sqrt_edge_cases()
359-
f = zero(MOI.ScalarQuadraticFunction{Float64})
360-
s = MOI.GreaterThan(0.0)
361-
for A in [
359+
for A in Any[
362360
# Trivial Cholesky
363361
[1.0 0.0; 0.0 2.0],
364362
# Cholesky works, with pivoting
@@ -371,20 +369,29 @@ function test_compute_sparse_sqrt_edge_cases()
371369
[2.0 0.0; 0.0 0.0],
372370
]
373371
B = SparseArrays.sparse(A)
372+
f = zero(MOI.ScalarQuadraticFunction{eltype(A)})
373+
s = MOI.GreaterThan(zero(eltype(A)))
374374
I, J, V = MOI.Bridges.Constraint.compute_sparse_sqrt(B, f, s)
375-
U = zeros(size(A))
375+
U = zeros(eltype(A), size(A))
376376
for (i, j, v) in zip(I, J, V)
377377
U[i, j] += v
378378
end
379379
@test isapprox(A, U' * U; atol = 1e-10)
380380
end
381381
# Test failures
382-
for A in [
382+
for A in Any[
383383
[-1.0 0.0; 0.0 1.0],
384384
# Found from test_quadratic_nonconvex_constraint_basic
385385
[0.0 -1.0; -1.0 0.0],
386+
# Different element type. We could potentially make this work in future,
387+
# but it first requires https://github.com/JuliaSmoothOptimizers/LDLFactorizations.jl/pull/142
388+
BigFloat[-1.0 0.0; 0.0 1.0],
389+
BigFloat[1.0 0.0; 0.0 2.0],
390+
BigFloat[1.0 1.0; 1.0 1.0],
386391
]
387392
B = SparseArrays.sparse(A)
393+
f = zero(MOI.ScalarQuadraticFunction{eltype(A)})
394+
s = MOI.GreaterThan(zero(eltype(A)))
388395
@test_throws(
389396
MOI.UnsupportedConstraint{typeof(f),typeof(s)},
390397
MOI.Bridges.Constraint.compute_sparse_sqrt(B, f, s),

0 commit comments

Comments
 (0)