# DimensionMismatch error in creating semidefinite constraint in JuMP.jl

**URL:** https://discourse.julialang.org/t/dimensionmismatch-error-in-creating-semidefinite-constraint-in-jump-jl/111969
**Category:** Optimization (Mathematical)
**Tags:** jump
**Created:** [March 22, 2024, 1:03pm UTC](https://discourse.julialang.org/t/dimensionmismatch-error-in-creating-semidefinite-constraint-in-jump-jl/111969 "2024-03-22T13:03:13Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![Shuvomoy\_Das\_Gupta](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/shuvomoy_das_gupta/32/10069_2.png) [@Shuvomoy\_Das\_Gupta](https://discourse.julialang.org/u/Shuvomoy_Das_Gupta)
#### Post date: [March 22, 2024, 1:03pm UTC](https://discourse.julialang.org/t/dimensionmismatch-error-in-creating-semidefinite-constraint-in-jump-jl/111969/1 "2024-03-22T13:03:13Z")

</div>

Dear all,

I have encountered a somewhat unintuitive error in creating the following semidefinte constraint in JuMP.jl. Sample code is as follows:

```julia
# Create a JuMP model

model = Model(optimizer_with_attributes(Mosek.Optimizer))

# Add the variables

@variable(model, G[1:13, 1:13], PSD)

A = [0.0 0.0 0.0 0.0 0.0; 0.0 0.0 0.0 0.0 0.0; 0.0 0.0 0.0 0.0 0.0; 0.0 0.0 0.0 0.0 0.0; 0.0 0.0 0.0 0.0 0.0; 0.0 0.0 0.0 0.0 0.0; 0.0 0.0 0.0 0.0 0.0; 0.0 0.0 0.0 0.0 0.0; 1.0 0.0 0.0 0.0 0.0; 0.0 1.0 0.0 0.0 0.0; 0.0 0.0 1.0 0.0 0.0; 0.0 0.0 0.0 1.0 0.0; 0.0 0.0 0.0 0.0 1.0]

# Want to enforce the constraint -A'GA ⪰ 0

@constraint(model, -A' * G * A >= 0, PSDCone())

```

The last line results in the following error:

```julia
ERROR: DimensionMismatch: array could not be broadcast to match destination
Stacktrace:
  [1] check_broadcast_shape
    @ .\broadcast.jl:579 [inlined]
  [2] check_broadcast_shape
    @ .\broadcast.jl:580 [inlined]
  [3] check_broadcast_axes
    @ .\broadcast.jl:582 [inlined]
  [4] instantiate
    @ .\broadcast.jl:309 [inlined]
  [5] materialize!
    @ .\broadcast.jl:914 [inlined]
  [6] materialize!
    @ .\broadcast.jl:911 [inlined]
  [7] operate!(::typeof(*), x::Matrix{AffExpr}, y::Matrix{Float64})
    @ MutableArithmetics C:\Users\shuvo\.julia\packages\MutableArithmetics\2vhhw\src\rewrite_generic.jl:9
  [8] operate_fallback!!
    @ MutableArithmetics C:\Users\username\.julia\packages\MutableArithmetics\2vhhw\src\interface.jl:601 [inlined]
  [9] operate!!(::typeof(*), ::Matrix{AffExpr}, ::Matrix{Float64})
    @ MutableArithmetics C:\Users\username\.julia\packages\MutableArithmetics\2vhhw\src\interface.jl:585
 [10] macro expansion
    @ C:\Users\username\.julia\packages\MutableArithmetics\2vhhw\src\rewrite.jl:321 [inlined]
 [11] macro expansion
    @ C:\Users\username\.julia\packages\JuMP\kSaGf\src\macros.jl:257 [inlined]
 [12] macro expansion
    @ C:\Users\username\.julia\packages\JuMP\kSaGf\src\macros\@constraint.jl:131 [inlined]
 [13] macro expansion
    @ C:\Users\username\.julia\packages\JuMP\kSaGf\src\macros.jl:393 [inlined]
 [14] top-level scope
    @ c:\Users\username\test.jl:147

```

The error goes away if I explicitly describe what `-A` means by rewriting the constraint:

```julia
@constraint(model, ((-1) .* A' * G * A) >= 0, PSDCone())

```

My question is if this `DimensionMismatch` error is expected behavior or not. I would assume that intuitively `JuMP` or `Julia` should interpret `-A` as `(-1).*A`. Please let me know if I am missing something.

---

<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: [March 23, 2024, 1:33am UTC](https://discourse.julialang.org/t/dimensionmismatch-error-in-creating-semidefinite-constraint-in-jump-jl/111969/2 "2024-03-23T01:33:23Z")

</div>

Hmm there’s a quite serious bug: [Bug in operate!!(\*, A, B) · Issue #271 · jump-dev/MutableArithmetics.jl · GitHub](https://github.com/jump-dev/MutableArithmetics.jl/issues/271)

```Julia
julia> A = [1; 2;;]
2×1 Matrix{Int64}:
 1
 2

julia> B = [1 2]
1×2 Matrix{Int64}:
 1 2

julia> A * B
2×2 Matrix{Int64}:
 1 2
 2 4

julia> B * A
1×1 Matrix{Int64}:
 5

julia> MutableArithmetics.operate!!(*, A, B)
ERROR: DimensionMismatch: array could not be broadcast to match destination
Stacktrace:
  [1] check_broadcast_shape
    @ ./broadcast.jl:579 [inlined]
  [2] check_broadcast_shape
    @ ./broadcast.jl:580 [inlined]
  [3] check_broadcast_axes
    @ ./broadcast.jl:582 [inlined]
  [4] instantiate
    @ ./broadcast.jl:309 [inlined]
  [5] materialize!
    @ ./broadcast.jl:914 [inlined]
  [6] materialize!
    @ ./broadcast.jl:911 [inlined]
  [7] operate!(::typeof(*), x::Matrix{Int64}, y::Matrix{Int64})
    @ MutableArithmetics ~/.julia/dev/MutableArithmetics/src/rewrite_generic.jl:9
  [8] operate_fallback!!
    @ ~/.julia/dev/MutableArithmetics/src/interface.jl:601 [inlined]
  [9] operate!!(::typeof(*), ::Matrix{Int64}, ::Matrix{Int64})
    @ MutableArithmetics ~/.julia/dev/MutableArithmetics/src/interface.jl:585
 [10] top-level scope
    @ REPL[70]:1

julia> A
2×1 Matrix{Int64}:
 1
 2

julia> B
1×2 Matrix{Int64}:
 1 2

julia> MutableArithmetics.operate!!(*, B, A)
1×2 Matrix{Int64}:
 5 5

julia> A
2×1 Matrix{Int64}:
 1
 2

julia> B
1×2 Matrix{Int64}:
 5 5

```

---

<div class="post-metadata">

### Author: ![Shuvomoy\_Das\_Gupta](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/shuvomoy_das_gupta/32/10069_2.png) [@Shuvomoy\_Das\_Gupta](https://discourse.julialang.org/u/Shuvomoy_Das_Gupta)
#### Post date: [March 23, 2024, 8:46pm UTC](https://discourse.julialang.org/t/dimensionmismatch-error-in-creating-semidefinite-constraint-in-jump-jl/111969/3 "2024-03-23T20:46:19Z")

</div>

I see, thanks @odow !
