# JuMP @constraint with sparse matrix

**URL:** <https://discourse.julialang.org/t/jump-constraint-with-sparse-matrix/71940>\
**Category:** Optimization (Mathematical)\
**Tags:** jump\
**Created:** [November 23, 2021, 12:51pm UTC](https://discourse.julialang.org/t/jump-constraint-with-sparse-matrix/71940 "2021-11-23T12:51:15Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Thomas](https://avatars.discourse-cdn.com/v4/letter/t/e36b37/32.png) [@Thomas](https://discourse.julialang.org/u/Thomas)\
**Post date:** [November 23, 2021, 12:51pm UTC](https://discourse.julialang.org/t/jump-constraint-with-sparse-matrix/71940/1 "2021-11-23T12:51:15Z")

</div>

I want to create PSD constraints with a sparse matrix of AffExpr as in the snippet below (rawdata is a Vector of tuples of the form (I, J, Va, Vs) containing row-col-values information)

```julia
model = let
    println("Constructing SDP model...")
    model = JuMP.Model()
    numvars = maximum([maximum(m[3]) for m in rawdata])
    println("Number of variables = $numvars")
    x = @variable(model, x[1:numvars])
    for m in rawdata
        mat = sparse(m[1], m[2], m[4].*x[m[3]])
        @constraint(model, mat >=0, PSDCone())
    end
    model
end

```

Is this error expected?

```julia
MethodError: no method matching -(::SparseMatrixCSC{AffExpr, Int64}, ::Int64)
Closest candidates are:
  -(!Matched::T, ::T) where T<:Union{Int128, Int16, Int32, Int64, Int8, UInt128, UInt16, UInt32, UInt64, UInt8} at int.jl:86
  -(!Matched::Union{MathOptInterface.ScalarAffineFunction{T}, MathOptInterface.ScalarQuadraticFunction{T}}, ::T) where T at C:\Users\ ****** \.julia\packages\MathOptInterface\YDdD3\src\Utilities\functions.jl:1725
  -(!Matched::LinearAlgebra.UniformScaling, ::Number) at C:\buildbot\worker\package_win64\build\usr\share\julia\stdlib\v1.6\LinearAlgebra\src\uniformscaling.jl:147
  ...
sub_mul(a::SparseMatrixCSC{AffExpr, Int64}, b::Int64) at MutableArithmetics.jl:33
operate(::typeof(MutableArithmetics.sub_mul), ::SparseMatrixCSC{AffExpr, Int64}, ::Int64) at interface.jl:131
operate_fallback!(::MutableArithmetics.NotMutable, ::Function, ::SparseMatrixCSC{AffExpr, Int64}, ::Int64) at interface.jl:428
operate!(op::typeof(MutableArithmetics.sub_mul), x::SparseMatrixCSC{AffExpr, Int64}, args::Int64) at rewrite.jl:83
macro expansion at rewrite.jl:279 [inlined]
macro expansion at macros.jl:676 [inlined]
top-level scope at readDavid.jl:25
eval at boot.jl:360 [inlined]

```

---

<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:** [November 23, 2021, 7:55pm UTC](https://discourse.julialang.org/t/jump-constraint-with-sparse-matrix/71940/2 "2021-11-23T19:55:34Z")

</div>

I can’t reproduce this. Please provide a reproducible example:

```nohighlight
julia> using JuMP, SparseArrays

julia> model = Model();

julia> @variable(model, x[1:2]);

julia> X = sparse([1, 2], [1, 2], x .+ 1)
2×2 SparseMatrixCSC{AffExpr, Int64} with 2 stored entries:
 x[1] + 1 ⋅
 ⋅ x[2] + 1

julia> @constraint(model, X >= 0, PSDCone())
[x[1] + 1 0;
 0 x[2] + 1] ∈ PSDCone()

```

Also, ensure that you have updated your packages. JuMP should be version 0.22

```nohighlight
julia> import Pkg

julia> Pkg.status("JuMP")
      Status `/private/tmp/Project.toml`
  [4076af6c] JuMP v0.22.0

```

---

<div class="post-metadata">

**Author:** ![jd-foster](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jd-foster/32/35824_2.png) [@jd-foster](https://discourse.julialang.org/u/jd-foster)\
**Post date:** [November 24, 2021, 6:23am UTC](https://discourse.julialang.org/t/jump-constraint-with-sparse-matrix/71940/3 "2021-11-24T06:23:08Z")

</div>

I _can_ reproduce this error with JuMP v0.21.10 using your MWE, and confirm that it is solved under version 0.22.

---

<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:** [November 24, 2021, 6:38am UTC](https://discourse.julialang.org/t/jump-constraint-with-sparse-matrix/71940/4 "2021-11-24T06:38:35Z")

</div>

Note that this syntax didn’t exist in 0.21, so the error message is somewhat arbitrary.

---

<div class="post-metadata">

**Author:** ![Thomas](https://avatars.discourse-cdn.com/v4/letter/t/e36b37/32.png) [@Thomas](https://discourse.julialang.org/u/Thomas)\
**Post date:** [November 24, 2021, 9:49am UTC](https://discourse.julialang.org/t/jump-constraint-with-sparse-matrix/71940/5 "2021-11-24T09:49:46Z")

</div>

Sorry guys, I can’t reproduce it too! But the problem is fixed after upgrading my packages (and have a clean session).
