Skip to content

Commit 73aec9c

Browse files
lrnvericphanson
andauthored
More efficient and less intrusive quadform (#444)
* small change to make it work * Update src/atoms/second_order_cone/quadform.jl Co-authored-by: Eric Hanson <[email protected]> * Update src/atoms/second_order_cone/quadform.jl Co-authored-by: Eric Hanson <[email protected]> * Adding a test * Update src/problem_depot/problems/socp.jl Co-authored-by: Eric Hanson <[email protected]> Co-authored-by: Eric Hanson <[email protected]>
1 parent feacb66 commit 73aec9c

File tree

2 files changed

+18
-7
lines changed

2 files changed

+18
-7
lines changed

src/atoms/second_order_cone/quadform.jl

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,18 @@ function quadform(x::AbstractExpr, A::Value)
66
if length(size(A)) != 2 || size(A, 1) != size(A, 2)
77
error("Quadratic form only takes square matrices")
88
end
9-
if !issymmetric(A)
10-
error("Quadratic form only defined for symmetric matrices")
9+
if !ishermitian(A)
10+
error("Quadratic form only defined for Hermitian matrices")
1111
end
12-
V = eigvals(Symmetric(Matrix(A)))
13-
14-
if all(V .>= 0)
12+
if isposdef(A)
1513
factor = 1
16-
elseif all(V .<= 0)
14+
elseif isposdef(.-A)
1715
factor = -1
1816
else
1917
error("Quadratic forms supported only for semidefinite matrices")
2018
end
2119

22-
P = real(sqrt(Matrix(factor * A)))
20+
P = sqrt(Hermitian(factor * A))
2321
return factor * square(norm2(P * x))
2422
end
2523

src/problem_depot/problems/socp.jl

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,19 @@ end
267267
@test evaluate(H) Hval atol=atol rtol=rtol
268268
end
269269

270+
# https://github.com/jump-dev/Convex.jl/pull/444
271+
x = Variable(3)
272+
a,b,c,d,e,f = rand(6)
273+
M = [2 a-b*im c-d*im;
274+
a+b*im 2 e-f*im
275+
c+d*im e+f*im 2]
276+
y = rand(3)
277+
p = minimize(quadform(x-y,M); numeric_type = T)
278+
handle_problem!(p)
279+
if test
280+
@test evaluate(x) y atol=atol rtol=rtol
281+
end
282+
270283
end
271284

272285
@add_problem socp function socp_huber_atom(handle_problem!, ::Val{test}, atol, rtol, ::Type{T}) where {T, test}

0 commit comments

Comments
 (0)