# Identical diagonal elements

**URL:** <https://discourse.julialang.org/t/identical-diagonal-elements/83969>\
**Category:** Optimization (Mathematical)\
**Tags:** jump\
**Created:** [July 8, 2022, 7:51pm UTC](https://discourse.julialang.org/t/identical-diagonal-elements/83969 "2022-07-08T19:51:40Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![ivzktch99](https://avatars.discourse-cdn.com/v4/letter/i/b19c9b/32.png) [@ivzktch99](https://discourse.julialang.org/u/ivzktch99)\
**Post date:** [July 8, 2022, 7:51pm UTC](https://discourse.julialang.org/t/identical-diagonal-elements/83969/1 "2022-07-08T19:51:40Z")

</div>

Suppose you have

```julia
using JuMP,Ipopt
model = Model(Ipopt.Optimizer)
n = 5
@variable(model, X[1:n, 1:n])

```

What is the easiest way to impose that the elements of main diagonal of X are identical. No restriction on what they are, they just need to be identical. That is X[1,1] = X[2,2] = … = X[n,n]

---

<div class="post-metadata">

**Author:** ![gdalle](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gdalle/32/27854_2.png) [@gdalle](https://discourse.julialang.org/u/gdalle)\
**Post date:** [July 8, 2022, 8:29pm UTC](https://discourse.julialang.org/t/identical-diagonal-elements/83969/2 "2022-07-08T20:29:46Z")

</div>

Add n - 1 constraints of the form `X[1, 1] == X[i, i]`?

---

<div class="post-metadata">

**Author:** ![ivzktch99](https://avatars.discourse-cdn.com/v4/letter/i/b19c9b/32.png) [@ivzktch99](https://discourse.julialang.org/u/ivzktch99)\
**Post date:** [July 8, 2022, 8:30pm UTC](https://discourse.julialang.org/t/identical-diagonal-elements/83969/3 "2022-07-08T20:30:17Z")

</div>

What if n =1000?

---

<div class="post-metadata">

**Author:** ![gdalle](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gdalle/32/27854_2.png) [@gdalle](https://discourse.julialang.org/u/gdalle)\
**Post date:** [July 8, 2022, 8:30pm UTC](https://discourse.julialang.org/t/identical-diagonal-elements/83969/4 "2022-07-08T20:30:50Z")

</div>

Of course you’d write the constraints in a loop

```julia
for i in 1:n
    @constraint(model, X[1, 1] == X[i, i])
end

```

---

<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:** [July 9, 2022, 8:04pm UTC](https://discourse.julialang.org/t/identical-diagonal-elements/83969/5 "2022-07-09T20:04:41Z")

</div>

Another option is to just replace the diagonal elements of `X` with `X[1, 1]`:

```julia
julia> using JuMP

julia> model = Model()
A JuMP Model
Feasibility problem with:
Variables: 0
Model mode: AUTOMATIC
CachingOptimizer state: NO_OPTIMIZER
Solver name: No optimizer attached.

julia> n = 5
5

julia> @variable(model, X[1:n, 1:n])
5×5 Matrix{VariableRef}:
 X[1,1] X[1,2] X[1,3] X[1,4] X[1,5]
 X[2,1] X[2,2] X[2,3] X[2,4] X[2,5]
 X[3,1] X[3,2] X[3,3] X[3,4] X[3,5]
 X[4,1] X[4,2] X[4,3] X[4,4] X[4,5]
 X[5,1] X[5,2] X[5,3] X[5,4] X[5,5]

julia> for i in 2:n
           delete(model, X[i, i]) # We don't need this variable
           X[i, i] = X[1, 1] # Replace the diagonals with X[1, 1]
       end

julia> X
5×5 Matrix{VariableRef}:
 X[1,1] X[1,2] X[1,3] X[1,4] X[1,5]
 X[2,1] X[1,1] X[2,3] X[2,4] X[2,5]
 X[3,1] X[3,2] X[1,1] X[3,4] X[3,5]
 X[4,1] X[4,2] X[4,3] X[1,1] X[4,5]
 X[5,1] X[5,2] X[5,3] X[5,4] X[1,1]

```

---

<div class="post-metadata">

**Author:** ![horvetz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/horvetz/32/31993_2.png) [@horvetz](https://discourse.julialang.org/u/horvetz)\
**Post date:** [July 7, 2023, 12:41am UTC](https://discourse.julialang.org/t/identical-diagonal-elements/83969/6 "2023-07-07T00:41:57Z")

</div>

I came across this and want to use it. But I got the following error. Are you sure that it is working? Thanks

`@constraint(model, X[i, i] = X[1, 1])`: Not enough positional arguments  
Stacktrace:

---

<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:** [July 7, 2023, 2:01am UTC](https://discourse.julialang.org/t/identical-diagonal-elements/83969/7 "2023-07-07T02:01:56Z")

</div>

You need `==` for an equality constraint, not `=`.
