# Jump using \`max(x...)\` where x is a matrix?

**URL:** <https://discourse.julialang.org/t/jump-using-max-x-where-x-is-a-matrix/78451>\
**Category:** Optimization (Mathematical)\
**Tags:** question, jump\
**Created:** [March 25, 2022, 10:06am UTC](https://discourse.julialang.org/t/jump-using-max-x-where-x-is-a-matrix/78451 "2022-03-25T10:06:19Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![floswald](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/floswald/32/195_2.png) [@floswald](https://discourse.julialang.org/u/floswald)\
**Post date:** [March 25, 2022, 10:06am UTC](https://discourse.julialang.org/t/jump-using-max-x-where-x-is-a-matrix/78451/1 "2022-03-25T10:06:19Z")

</div>

I need to revive a previously [asked question](https://discourse.julialang.org/t/using-max-in-nonlinear-constraints/21606/10). I have a problem that involves evaluating the `exp` function over an array of variables and I want to avoid huge values by centering those variables before appplying `exp`. I get a bounds error:

```julia
using JuMP
using Ipopt

m = Model(Ipopt.Optimizer)
fmax(y...) = max(y...)
JuMP.register(m, :fmax, N, fmax, autodiff=true)
@variable(m,x[i=1:2, j=1:3])
@NLexpression(m, xbar[i=1:2, j=1:3], x[i,j] - fmax(x...)) # normalize by largest value in x
@NLexpression(m, p[i=1:2, j=1:3], exp(xbar[i,j]) / sum(xbar[k,l] for k=1:2, l=1:3))
@NLobjective(m, Max, sum(p[i,j] for i=1:2, j=1:3))
optimize!(m)

ERROR: BoundsError: attempt to access 2-element Vector{Float64} at index [1:6]

```

it is strange because it seems the subexpression is built correctly:

```julia
julia> m[:xbar]
2×3 Matrix{NonlinearExpression}:
 subexpression[1]: x[1,1] - fmax(x[1,1], x[2,1], x[1,2], x[2,2], x[1,3], x[2,3]) … subexpression[5]: x[1,3] - fmax(x[1,1], x[2,1], x[1,2], x[2,2], x[1,3], x[2,3])
 subexpression[2]: x[2,1] - fmax(x[1,1], x[2,1], x[1,2], x[2,2], x[1,3], x[2,3]) subexpression[6]: x[2,3] - fmax(x[1,1], x[2,1], x[1,2], x[2,2], x[1,3], x[2,3])

```

or, at least to me that looks like exactly what I wanted to achieve. What 2-element vector are we talking abou here?  
thanks!

---

<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:** [March 26, 2022, 4:15am UTC](https://discourse.julialang.org/t/jump-using-max-x-where-x-is-a-matrix/78451/2 "2022-03-26T04:15:34Z")

</div>

When I tried to run this, it became clear that the value of `N` is not defined.  
I think you have `N = 2` set somewhere.  
Setting `N = 3` gives 3 in place of 2 here:  
`ERROR: BoundsError: attempt to access 3-element Vector{Float64} at index [1:6]`  
while setting `N = 6` misses this error but ultimately returns from IPOPT:  
`EXIT: Invalid number in NLP function or derivative detected.`

---

<div class="post-metadata">

**Author:** ![gustaphe](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gustaphe/32/18174_2.png) [@gustaphe](https://discourse.julialang.org/u/gustaphe)\
**Post date:** [March 26, 2022, 5:46am UTC](https://discourse.julialang.org/t/jump-using-max-x-where-x-is-a-matrix/78451/3 "2022-03-26T05:46:10Z")

</div>

Not the answer to your question but

I don’t have my laptop right now, but it seems to me `maximum(x)` is more appropriate than `max(x...)`

---

<div class="post-metadata">

**Author:** ![floswald](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/floswald/32/195_2.png) [@floswald](https://discourse.julialang.org/u/floswald)\
**Post date:** [March 26, 2022, 7:41am UTC](https://discourse.julialang.org/t/jump-using-max-x-where-x-is-a-matrix/78451/4 "2022-03-26T07:41:46Z")

</div>

Oh! How dumb, N = 6 in my case!  
And no, it’s precisely impossible to use `maximum` that’s why we have to splat the vector into `max`

---

<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:** [March 26, 2022, 10:23am UTC](https://discourse.julialang.org/t/jump-using-max-x-where-x-is-a-matrix/78451/5 "2022-03-26T10:23:07Z")

</div>

Are you sure that fmax(y…) = maximum(y) won’t work?

---

<div class="post-metadata">

**Author:** ![floswald](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/floswald/32/195_2.png) [@floswald](https://discourse.julialang.org/u/floswald)\
**Post date:** [March 26, 2022, 11:28am UTC](https://discourse.julialang.org/t/jump-using-max-x-where-x-is-a-matrix/78451/6 "2022-03-26T11:28:53Z")

</div>

sure.

```julia
using JuMP
using Ipopt

m = Model(Ipopt.Optimizer)
N = 6
fmax(y...) = maximum(y...)
JuMP.register(m, :fmax, N, fmax, autodiff=true)
@variable(m,x[i=1:2, j=1:3])
@NLexpression(m, xbar[i=1:2, j=1:3], x[i,j] - fmax(x...)) # normalize by largest value in x
@NLexpression(m, p[i=1:2, j=1:3], exp(xbar[i,j]) / sum(xbar[k,l] for k=1:2, l=1:3))
@NLobjective(m, Max, sum(p[i,j] for i=1:2, j=1:3))
optimize!(m)

ERROR: Unable to register the function :fmax because it does not support differentiation via ForwardDiff.

Common reasons for this include:

 * the function assumes `Float64` will be passed as input, it must work for any
   generic `Real` type.
 * the function allocates temporary storage using `zeros(3)` or similar. This
   defaults to `Float64`, so use `zeros(T, 3)` instead.

```

---

<div class="post-metadata">

**Author:** ![ccoffrin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ccoffrin/32/400_2.png) [@ccoffrin](https://discourse.julialang.org/u/ccoffrin)\
**Post date:** [March 26, 2022, 4:35pm UTC](https://discourse.julialang.org/t/jump-using-max-x-where-x-is-a-matrix/78451/7 "2022-03-26T16:35:01Z")

</div>

I am wondering is it possible to use the max relaxation in this context?

Something like this,

```julia
@variable(m, xmax)
@variable(m, x[i=1:2, j=1:3])
for x_var in x
    @constraint(m, x_var <= xmax)
end

```
