# Matrix Constraints in JuMP

**URL:** https://discourse.julialang.org/t/matrix-constraints-in-jump/116663
**Category:** Optimization (Mathematical)
**Tags:** jump
**Created:** [July 5, 2024, 1:42pm UTC](https://discourse.julialang.org/t/matrix-constraints-in-jump/116663 "2024-07-05T13:42:10Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![Gagan\_Meena](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gagan_meena/32/49621_2.png) [@Gagan\_Meena](https://discourse.julialang.org/u/Gagan_Meena)
#### Post date: [July 5, 2024, 1:42pm UTC](https://discourse.julialang.org/t/matrix-constraints-in-jump/116663/1 "2024-07-05T13:42:10Z")

</div>

Can I write matrix constraints in JUMP using @constraints macro?  
While writing this flow constraint `Bd*A*δ .== f` as one of the constraints in the @constraints macro, I am getting the following error.

ERROR: MethodError: Cannot `convert` an object of type  
Vector{Int64} to an object of type  
Base.OneTo  
Closest candidates are:  
convert(::Type{T}, ::AbstractRange) where T\<:AbstractRange at range.jl:256  
convert(::Type{T}, ::HDF5.BlockRange) where T\<:AbstractRange at C:\Users\GAGAN MEENA.julia\packages\HDF5\Z859u\src\dataspaces.jl:204  
convert(::Type{SL}, ::FillArrays.AbstractFill{T, 1} where T) where SL\<:AbstractRange at C:\Users\GAGAN MEENA.julia\packages\FillArrays\eOEVm\src\FillArrays.jl:544  
…  
Stacktrace:  
[1] \_broadcast\_getindex\_evalf  
@ .\broadcast.jl:670 [inlined]  
[2] \_broadcast\_getindex  
@ .\broadcast.jl:653 [inlined]  
[3] (::Base.Broadcast.var"#29#30"{Base.Broadcast.Broadcasted{Base.Broadcast.Style{Tuple}, Nothing, typeof(convert), Tuple{Base.RefValue{Type{Base.OneTo}}, Tuple{Base.OneTo{Int64}, Vector{Int64}}}}})(k::Int64)  
@ Base.Broadcast .\broadcast.jl:1075  
[4] ntuple  
@ .\ntuple.jl:49 [inlined]  
[5] copy  
@ .\broadcast.jl:1075 [inlined]  
[6] materialize  
@ .\broadcast.jl:860 [inlined]  
[7] undef\_array(::Type{Matrix{AffExpr}}, ::Base.OneTo{Int64}, ::Vector{Int64})  
@ MutableArithmetics C:\Users\GAGAN MEENA.julia\packages\MutableArithmetics\iovKe\src\implementations\LinearAlgebra.jl:387  
[8] operate(#unused#::typeof(\*), A::Matrix{Float64}, B::JuMP.Containers.DenseAxisArray{VariableRef, 2, Tuple{Vector{Int64}, Vector{Int64}}, Tuple{JuMP.Containers.\_AxisLookup{Dict{Int64, Int64}}, JuMP.Containers.\_AxisLookup{Dict{Int64, Int64}}}})  
@ MutableArithmetics C:\Users\GAGAN MEENA.julia\packages\MutableArithmetics\iovKe\src\implementations\LinearAlgebra.jl:419  
[9] macro expansion  
@ C:\Users\GAGAN MEENA.julia\packages\MutableArithmetics\iovKe\src\rewrite.jl:325 [inlined]  
[10] macro expansion  
@ C:\Users\GAGAN MEENA.julia\packages\JuMP\Gwn88\src\macros.jl:257 [inlined]  
[11] macro expansion  
@ C:\Users\GAGAN MEENA.julia\packages\JuMP\Gwn88\src\macros@constraint.jl:119 [inlined]  
[12] macro expansion  
@ C:\Users\GAGAN MEENA.julia\packages\JuMP\Gwn88\src\macros.jl:393 [inlined]  
[13] Scenario\_Reconciliation(orig::result\_DACCPO, Prob::Chance\_Probability, scen::Int64, fmax::Float64)  
@ Main e:\IITB\Sem7\Research\Code\DACCPO\Reconciliation.jl:178  
[14] top-level scope  
@ e:\IITB\Sem7\Research\Code\DACCPO\Main.jl:13

---

<div class="post-metadata">

### Author: ![raman\_kumar](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/raman_kumar/32/26782_2.png) [@raman\_kumar](https://discourse.julialang.org/u/raman_kumar)
#### Post date: [July 5, 2024, 2:45pm UTC](https://discourse.julialang.org/t/matrix-constraints-in-jump/116663/2 "2024-07-05T14:45:21Z")

</div>

> [@Gagan\_Meena](#):
>
> DACCPO\Main.jl:13

Look in your \DACCPO\Main.jl file in 13th line. You are getting error `type` conversion.

> [@Gagan\_Meena](#):
>
> ERROR: MethodError: Cannot `convert` an object of type  
> Vector{Int64} to an object of type  
> Base.OneTo

and learn `quoted code` posting method to post your code. [Please read: make it easier to help you](https://discourse.julialang.org/t/please-read-make-it-easier-to-help-you/14757)

---

<div class="post-metadata">

### Author: ![blegat](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/blegat/32/217090_2.png) [@blegat](https://discourse.julialang.org/u/blegat)
#### Post date: [July 8, 2024, 8:00am UTC](https://discourse.julialang.org/t/matrix-constraints-in-jump/116663/3 "2024-07-08T08:00:14Z")

</div>

It seems that you are multiplying a matrix with a JuMP `DenseAxisArray`.  
A `DenseAxisArray` is created in the `@variable` macro when the indices are not `1:n`. This also means that multiplying a matrix with a `DenseAxisArray` is not well defined since they don’t have the same indices. Note that the field `.data` of the `DenseAxisArray` is a matrix so you could use that to access the container of variables as a `Matrix`.

---

<div class="post-metadata">

### Author: ![Gagan\_Meena](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gagan_meena/32/49621_2.png) [@Gagan\_Meena](https://discourse.julialang.org/u/Gagan_Meena)
#### Post date: [July 10, 2024, 12:13pm UTC](https://discourse.julialang.org/t/matrix-constraints-in-jump/116663/4 "2024-07-10T12:13:08Z")

</div>

Thank you
