# Stack overflow error with @SDconstraint

**URL:** https://discourse.julialang.org/t/stack-overflow-error-with-sdconstraint/41557
**Category:** Optimization (Mathematical)
**Tags:** jump
**Created:** [June 16, 2020, 10:55pm UTC](https://discourse.julialang.org/t/stack-overflow-error-with-sdconstraint/41557 "2020-06-16T22:55:30Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![stephentu](https://avatars.discourse-cdn.com/v4/letter/s/ed655f/32.png) [@stephentu](https://discourse.julialang.org/u/stephentu)
#### Post date: [June 16, 2020, 10:55pm UTC](https://discourse.julialang.org/t/stack-overflow-error-with-sdconstraint/41557/1 "2020-06-16T22:55:30Z")

</div>

I have the following simple code snippet which is causing me issues:

```julia
function find_common_lyapunov(system::PiecewiseLinearSystem, gamma::Float64)
    @assert(gamma > 0.0)
    dim = system_dim(system)
    model = Model(Mosek.Optimizer)
    @variable(model, P[1:dim, 1:dim], PSD)
    @SDconstraint(model, P >= I)

    @show(length(system.matrices))
    for Ai in system.matrices
        @show(Ai)
        @SDconstraint(model, Ai' * P + P * Ai + gamma * P <= 0)
    end 
                                                                                                                                       
    @objective(model, Min, sum(P .^ 2)) 
                                                                                                                                       
    optimize!(model)
    status = termination_status(model)
    if status != MOI.OPTIMAL
        println("TERMINATION WAS NOT OPTIMAL: $status")
        return nothing
    end 
                                                                                                                                       
    value.(P)
end

```

When I run this, I get the following:

```julia
julia> P_star = find_common_lyapunov(system, 1e-3)
length(system.matrices) = 4
Ai = [-0.3216088732749837 0.2456225325321437 0.0 0.0 0.0 0.0 0.0 0.0; -4.550258232817038 -1.0264957875870517 0.0 0.0 0.0 0.0 0.0 0.0; 0.0 0.0 -0.01 0.0 0.0 0.0 0.0 0.0; 0.0 0.0 0.0 -0.01 0.0 0.0 0.0 0.0; 0.0 0.0 0.0 0.0 -0.01 0.0 0.0 0.0; 0.0 0.0 0.0 0.0 0.0 -0.01 0.0 0.0; 0.0 0.0 0.0 0.0 0.0 0.0 -0.01 0.0; 0.0 0.0 0.0 0.0 0.0 0.0 0.0 -0.01]
ERROR: StackOverflowError:
Stacktrace:
 [1] mutable_operate!(::typeof(MutableArithmetics.sub_mul), ::Array{GenericAffExpr{Float64,VariableRef},2}, ::Symmetric{VariableRef,Array{VariableRef,2}}, ::Array{Float64,2}) at /Users/stephentu/.julia/packages/MutableArithmetics/ZGFsK/src/linear_algebra.jl:69 (repeats 79984 times)

```

This seems like too small of an SDP to run into memory issues. Any ideas?

---

<div class="post-metadata">

### Author: ![odow](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/odow/32/28685_2.png) [@odow](https://discourse.julialang.org/u/odow)
#### Post date: [June 17, 2020, 12:26am UTC](https://discourse.julialang.org/t/stack-overflow-error-with-sdconstraint/41557/2 "2020-06-17T00:26:06Z")

</div>

This looks like a bug. Here is a simpler reproducer:

```julia
using JuMP
model = Model()
@variable(model, P[1:2, 1:2], PSD)
A = [1 0; 0 1]
@SDconstraint(model, A' * P .+ P * A <= 0) # Works
@SDconstraint(model, A' * P + P * A <= 0) # Bus 10 error

```

~~I’ll open an issue.~~ Already reported: [https://github.com/jump-dev/MutableArithmetics.jl/issues/48](https://github.com/jump-dev/MutableArithmetics.jl/issues/48)

---

<div class="post-metadata">

### Author: ![stephentu](https://avatars.discourse-cdn.com/v4/letter/s/ed655f/32.png) [@stephentu](https://discourse.julialang.org/u/stephentu)
#### Post date: [June 17, 2020, 12:50am UTC](https://discourse.julialang.org/t/stack-overflow-error-with-sdconstraint/41557/3 "2020-06-17T00:50:11Z")

</div>

Thanks @odow. Does the `.+` line you showed have the same semantics as the `+` (can I use it as a proper workaround for now?)

---

<div class="post-metadata">

### Author: ![odow](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/odow/32/28685_2.png) [@odow](https://discourse.julialang.org/u/odow)
#### Post date: [June 17, 2020, 12:59am UTC](https://discourse.julialang.org/t/stack-overflow-error-with-sdconstraint/41557/4 "2020-06-17T00:59:30Z")

</div>

It seemed to work for me, but you’ll have to try it out to make sure.

In this case, `.+` is equivalent to `+`. It is just native Julia broadcasting: [Mathematical Operations and Elementary Functions · The Julia Language](https://docs.julialang.org/en/v1/manual/mathematical-operations/#man-dot-operators-1)

(So note that `P .* A` is \_not) the same as `P * A`.)
