Skip to content

Commit 3553533

Browse files
authored
[Bridges] wrap compute_sparse_sqrt in a try-catch (#2961)
1 parent b632fb6 commit 3553533

1 file changed

Lines changed: 24 additions & 14 deletions

File tree

src/Bridges/Constraint/bridges/QuadtoSOCBridge.jl

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -80,22 +80,32 @@ function compute_sparse_sqrt_fallback(Q, ::F, ::S) where {F,S}
8080
end
8181

8282
function compute_sparse_sqrt(Q, func, set)
83-
factor = try
84-
LinearAlgebra.cholesky(Q; check = false)
85-
catch
86-
msg = "There was an error computing a Cholesky decomposition"
83+
# There's a big try-catch here because Cholesky can fail even if
84+
# `check = false`. As one example, it currently (v1.12) fails with
85+
# `BigFloat`. Similarly, we want to guard against errors in
86+
# `compute_sparse_sqrt_fallback`.
87+
#
88+
# The try-catch isn't a performance concern because the alternative is not
89+
# being able to reformulate the problem.
90+
try
91+
factor = LinearAlgebra.cholesky(Q; check = false)
92+
if !LinearAlgebra.issuccess(factor)
93+
return compute_sparse_sqrt_fallback(Q, func, set)
94+
end
95+
L, p = SparseArrays.sparse(factor.L), factor.p
96+
# We have Q = P' * L * L' * P. We want to find Q = U' * U, so U = L' * P
97+
# First, compute L'. Note I and J are reversed
98+
J, I, V = SparseArrays.findnz(L)
99+
# Then, we want to permute the columns of L'. The rows stay in the same
100+
# order.
101+
return I, p[J], V
102+
catch err
103+
if err isa MOI.AddConstraintNotAllowed
104+
rethrow(err)
105+
end
106+
msg = "There was an error computing a matrix square root"
87107
throw(MOI.UnsupportedConstraint{typeof(func),typeof(set)}(msg))
88108
end
89-
if !LinearAlgebra.issuccess(factor)
90-
return compute_sparse_sqrt_fallback(Q, func, set)
91-
end
92-
L, p = SparseArrays.sparse(factor.L), factor.p
93-
# We have Q = P' * L * L' * P. We want to find Q = U' * U, so U = L' * P
94-
# First, compute L'. Note I and J are reversed
95-
J, I, V = SparseArrays.findnz(L)
96-
# Then, we want to permute the columns of L'. The rows stay in the same
97-
# order.
98-
return I, p[J], V
99109
end
100110

101111
function bridge_constraint(

0 commit comments

Comments
 (0)