DimensionMismatch error in creating semidefinite constraint in JuMP.jl

Dear all,

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

# 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:

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:

@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.

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

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

I see, thanks @odow !