# Help creating a JuMP variable array

**URL:** https://discourse.julialang.org/t/help-creating-a-jump-variable-array/11515
**Category:** New to Julia
**Tags:** question, jump, optimization
**Created:** [June 7, 2018, 6:17pm UTC](https://discourse.julialang.org/t/help-creating-a-jump-variable-array/11515 "2018-06-07T18:17:43Z")
**Posts on this page:** 16
**Page:** 1

<div class="post-metadata">

### Author: ![CPPhysics](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cpphysics/32/4372_2.png) [@CPPhysics](https://discourse.julialang.org/u/CPPhysics)
#### Post date: [June 7, 2018, 6:17pm UTC](https://discourse.julialang.org/t/help-creating-a-jump-variable-array/11515/1 "2018-06-07T18:17:43Z")

</div>

I am interested if anyone can help me create a JuMP variable which is a 2X2 array. I want it to look like:

```julia
X=diag([X0,X1]) 

```

Where X0 and X1 are complex and just numbers varied in the optimization problem, however I need help formatting this specifically in the form of a JuMP variable.

Any help is greatly appreciated

---

<div class="post-metadata">

### Author: ![ExpandingMan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/expandingman/32/866_2.png) [@ExpandingMan](https://discourse.julialang.org/u/ExpandingMan)
#### Post date: [June 7, 2018, 6:19pm UTC](https://discourse.julialang.org/t/help-creating-a-jump-variable-array/11515/2 "2018-06-07T18:19:42Z")

</div>

You should be able to fill any type of `AbstractArray` with JuMP variables. You can do, for example

```julia
@variable(m, xdiag[i=1:N]) # first create the variables that will fill the array
x = Diagonal(xdiag) # create the array with those variables along its diagonal

```

---

<div class="post-metadata">

### Author: ![CPPhysics](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cpphysics/32/4372_2.png) [@CPPhysics](https://discourse.julialang.org/u/CPPhysics)
#### Post date: [June 7, 2018, 8:54pm UTC](https://discourse.julialang.org/t/help-creating-a-jump-variable-array/11515/3 "2018-06-07T20:54:41Z")

</div>

Thank you! I used:

```julia
N=2
@variable(m, Qdiag[i=1:N])  
Q = Diagonal(Qdiag)

```

However I get an error that my dimensions are mismatched.  
`DimensionMismatch("dimensions must match")`

`Stacktrace: [1] promote_shape(::Tuple{Base.OneTo{Int64},Base.OneTo{Int64}}, ::Tuple{Base.OneTo{Int64},Base.OneTo{Int64}}) at .\indices.jl:79 [2] +(::Array{JuMP.GenericAffExpr{Float64,JuMP.Variable},2}, ::Array{JuMP.GenericQuadExpr{Float64,JuMP.Variable},2}) at .\arraymath.jl:38 [3] +(::Array{JuMP.GenericAffExpr{Float64,JuMP.Variable},2}, ::Array{JuMP.GenericAffExpr{Float64,JuMP.Variable},2}, ::Array{JuMP.GenericQuadExpr{Float64,JuMP.Variable},2}) at .\operators.jl:424`

Do you know why this would be? I assume it is something to do with my N=2 statement

---

<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: [June 7, 2018, 10:07pm UTC](https://discourse.julialang.org/t/help-creating-a-jump-variable-array/11515/5 "2018-06-07T22:07:32Z")

</div>

What you have works for me. It looks like the error is coming from a different line. Try posting a [minimum working example](https://stackoverflow.com/help/mcve) of your problem.

---

<div class="post-metadata">

### Author: ![CPPhysics](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cpphysics/32/4372_2.png) [@CPPhysics](https://discourse.julialang.org/u/CPPhysics)
#### Post date: [June 8, 2018, 2:41pm UTC](https://discourse.julialang.org/t/help-creating-a-jump-variable-array/11515/6 "2018-06-08T14:41:49Z")

</div>

Thanks, it was a different error and restarting the kernal helped. Now I have this new very related error. Q has worked for me but now I would like to make an identical variable X except it will be a flipped diagonal rather than a normal diagonal matrix. Can you help with what’s wrong with this code?:

```julia
@variable(m, Rdiag[i=1:N]) 
R = flipdim(Diagonal(Rdiag),2)

```

`MethodError: Cannot `convert` an object of type JuMP.GenericAffExpr{Float64,JuMP.Variable} to an object of type JuMP.Variable This may have arisen from a call to the constructor JuMP.Variable(...), since type constructors fall back to convert methods.`

---

<div class="post-metadata">

### Author: ![CPPhysics](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cpphysics/32/4372_2.png) [@CPPhysics](https://discourse.julialang.org/u/CPPhysics)
#### Post date: [June 8, 2018, 2:42pm UTC](https://discourse.julialang.org/t/help-creating-a-jump-variable-array/11515/7 "2018-06-08T14:42:46Z")

</div>

I can post a minimum working example:

```julia
using NLopt
using JuMP

m=Model(solver=NLoptSolver(algorithm=:DIRECT))

N=2
L=100000

@variable(m, Rdiag[i=1:N]) 
R = flipdim(Diagonal(Rdiag),2)

```

---

<div class="post-metadata">

### Author: ![tkoolen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tkoolen/32/1603_2.png) [@tkoolen](https://discourse.julialang.org/u/tkoolen)
#### Post date: [June 8, 2018, 3:11pm UTC](https://discourse.julialang.org/t/help-creating-a-jump-variable-array/11515/8 "2018-06-08T15:11:12Z")

</div>

This is because `Diagonal` only stores the diagonal elements, and uses the `zero` function to get the off-diagonal elements when needed for generic methods. Since

```julia
julia> typeof(zero(Rdiag[1]))
JuMP.GenericAffExpr{Float64,JuMP.Variable}

```

we have `typeof(Diagonal(Rdiag)[1, 2]) != typeof(Diagonal(Rdiag)[1, 1])`, and since `flipdim` uses the element type of its input matrix as the element type of its output, you get an error saying that you can’t put a `GenericAffExpr` in a matrix of `Variable`s.

You can work around this using

```julia
flipdim(Matrix(Diagonal(Rdiag)), 2)

```

Edit: opened this issue: [https://github.com/JuliaLang/julia/issues/27494](https://github.com/JuliaLang/julia/issues/27494).

---

<div class="post-metadata">

### Author: ![tkoolen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tkoolen/32/1603_2.png) [@tkoolen](https://discourse.julialang.org/u/tkoolen)
#### Post date: [June 10, 2018, 9:53pm UTC](https://discourse.julialang.org/t/help-creating-a-jump-variable-array/11515/10 "2018-06-10T21:53:13Z")

</div>

Have you read [Nonlinear Modeling — JuMP -- Julia for Mathematical Optimization 0.18 documentation](http://www.juliaopt.org/JuMP.jl/0.18/nlp.html#syntax-notes)?

---

<div class="post-metadata">

### Author: ![CPPhysics](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cpphysics/32/4372_2.png) [@CPPhysics](https://discourse.julialang.org/u/CPPhysics)
#### Post date: [June 12, 2018, 1:34am UTC](https://discourse.julialang.org/t/help-creating-a-jump-variable-array/11515/11 "2018-06-12T01:34:52Z")

</div>

No, thank you that is just what I needed.

---

<div class="post-metadata">

### Author: ![CPPhysics](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cpphysics/32/4372_2.png) [@CPPhysics](https://discourse.julialang.org/u/CPPhysics)
#### Post date: [June 12, 2018, 2:18am UTC](https://discourse.julialang.org/t/help-creating-a-jump-variable-array/11515/12 "2018-06-12T02:18:07Z")

</div>

So I have been cleaning it up and now I have the same error with a Generic AffExpr rather than a Quad expression. I am thinking that I need to simplify e\_c and create expressions for each of its components, like a particular JuMP constraint for `e.^(*(T,L))`, and for `(kron(C(Q,R),C(conj(Q),conj(R))))`, etc? I am sorry I am understanding more and more thanks to your help and the syntax notes but I am missing some part of the big picture here

---

<div class="post-metadata">

### Author: ![tkoolen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tkoolen/32/1603_2.png) [@tkoolen](https://discourse.julialang.org/u/tkoolen)
#### Post date: [June 12, 2018, 2:50am UTC](https://discourse.julialang.org/t/help-creating-a-jump-variable-array/11515/13 "2018-06-12T02:50:02Z")

</div>

Just to clarify first, do you want a matrix exponential or an elementwise scalar exponential?

Also, what are `C` and `c`?

---

<div class="post-metadata">

### Author: ![tkoolen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tkoolen/32/1603_2.png) [@tkoolen](https://discourse.julialang.org/u/tkoolen)
#### Post date: [June 12, 2018, 4:24am UTC](https://discourse.julialang.org/t/help-creating-a-jump-variable-array/11515/15 "2018-06-12T04:24:13Z")

</div>

Alright, getting a little bit closer, but still quite a few big problems. First, `@variable(m,T)` creates a _scalar_ variable called `T`, and you’re currently trying to constrain each element of the matrix `T_` to be equal to `T`. That’s probably not what you want.

Second, are you aware that JuMP’s variables are assumed to be real? `conj` applied to a real matrix `A` just returns `A`. Are you looking for `adjoint`/`transpose` instead?

> lets go for a matrix exponential for now.

OK, in Julia, the `.` in `e.^A` is used to signify a [broadcasted](https://docs.julialang.org/en/stable/manual/arrays/#Broadcasting-1) operation (in this case, an elementwise-applied exponential function). If you were operating on a numeric array, you’d want `expm(A)` for a matrix exponential (or just `exp(A)` in the upcoming version of Julia).

Next, it’s important to note the difference between linear/quadratic modeling and nonlinear modeling. All of the expressions you construct up to `@constraint(m,Exp1.==e.^(*(T,L)))` are linear or quadratic in the decision variables you create using `@variable`. That code works because various operators (like `+`) and functions are overloaded for e.g. `JuMP`’s `Variable` and `GenericAffExpr` types. But JuMP’s nonlinear interface unfortunately doesn’t work quite the same way as its linear/quadratic interface. It’s quite a bit harder to use.

The following nonlinear syntax notes are especially relevant to your problem:

> All expressions must be simple scalar operations.

This means that creating an expression that represents the matrix exponential of `T * L` unfortunately can’t work, as it is not a scalar expression.

> There is no operator overloading provided to build up nonlinear expressions

and

> All nonlinear expressions must be inside of macros.

Before, you were trying to construct a nonlinear expression outside of a JuMP macro, which is not supported, so the code in your latest post is better in that sense, but still not quite right. `@expression` is only for linear/quadratic modeling, so you’d need `@NLexpression`, but again, `@NLexpression` can only be used to construct scalar nonlinear expressions.

Here’s a slightly updated version of the first part of your problem formulation:

```julia
using Ipopt # just because that's what I have installed right now
using JuMP

C(A, B) = A * B - B * A
m = Model(solver=IpoptSolver())

N = 2
L = 100000

@variable(m, Rdiag[i=1:N]) # first create the variables that will fill the array
R = flipdim(Matrix(Diagonal(Rdiag)),2) # create the array with those variables along its diagonal
@variable(m, Qdiag[i=1:N]) 
Q = Diagonal(Qdiag)
T = kron(Q, Diagonal(ones(2))) + kron(Diagonal(ones(2)), conj(Q)) + kron(R, conj(R))
LT = L * T

# since syntax notes state that
# > 'AffExpr and QuadExpr objects cannot currently be used inside nonlinear expressions'
# need to create matrix of slack variables `LT_` and set them equal to `LT`
@variable(m, LT_[1:size(LT, 1), 1:size(LT, 2)])
for i in eachindex(T)
    @constraint(m, LT_[i] == LT[i])
end

```

The next part, involving the nonlinear expressions for `expm` and for e.g. `kron(C(Q,R),C(conj(Q),conj(R)))` (a matrix of quartic expressions) is the tricky bit, but unfortunately this is all I have time for right now. Some hints for you or others who want to help out:

1. if you use the same slack variable trick as above for `C(Q, R)` and `C(conj(Q),conj(R))`, you can get `kron(C(Q,R),C(conj(Q),conj(R)))` as a matrix of quadratic expressions, and then you can use the slack variable trick again to reduce this to a matrix of plain variables.
2. since you can only use scalar nonlinear expressions, you’ll have to do the whole computation of the objective given `LT`, `C(Q, R)` and `C(conj(Q),conj(R))` in one function that you then register with JuMP. Nonlinear functions can’t take matrix or vector arguments though; search Discourse for some solutions to this problem.
3. there’s currently no generic `expm` function, so automatic differentiation won’t work; you’ll have to supply derivatives of the manually registered function from 2. yourself (not too hard in this case)

---

<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: [June 12, 2018, 4:31am UTC](https://discourse.julialang.org/t/help-creating-a-jump-variable-array/11515/16 "2018-06-12T04:31:51Z")

</div>

> since I am new to coding, julia, and the science of the project itself so I am sorry for my ignorance.

Your model looks very difficult to formulate even for people who are experienced in JuMP. It might be helpful to play around with some simpler models completely unrelated to this in order to learn some of the basics of JuMP:  
[https://github.com/JuliaOpt/JuMP.jl/tree/master/examples](https://github.com/JuliaOpt/JuMP.jl/tree/master/examples)

> **[GitHub - JuliaOpt/juliaopt-notebooks: A collection of IJulia notebooks...](https://github.com/JuliaOpt/juliaopt-notebooks)**
>
> A collection of IJulia notebooks related to optimization - GitHub - JuliaOpt/juliaopt-notebooks: A collection of IJulia notebooks related to optimization

Also, as far as it looks, you are only doing unconstrained optimization. If so, JuMP is probably the wrong choice. You may want to look at [GitHub - JuliaNLSolvers/Optim.jl: Optimization functions for Julia](https://github.com/JuliaNLSolvers/Optim.jl) instead.

---

<div class="post-metadata">

### Author: ![CPPhysics](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cpphysics/32/4372_2.png) [@CPPhysics](https://discourse.julialang.org/u/CPPhysics)
#### Post date: [June 12, 2018, 12:12pm UTC](https://discourse.julialang.org/t/help-creating-a-jump-variable-array/11515/17 "2018-06-12T12:12:00Z")

</div>

Thank you so much, that was very helpful. You’ve laid everything out in a very understandable way. For the conj(matrix) I was looking for a complex transpose. I really cannot thank you enough for the time you’ve been willing to put in you have helped me learn a lot. I will work off of what you have given me thus far.

---

<div class="post-metadata">

### Author: ![CPPhysics](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cpphysics/32/4372_2.png) [@CPPhysics](https://discourse.julialang.org/u/CPPhysics)
#### Post date: [June 12, 2018, 12:12pm UTC](https://discourse.julialang.org/t/help-creating-a-jump-variable-array/11515/18 "2018-06-12T12:12:47Z")

</div>

I will also look into optim to see if it is better suited to my needs because I do believe I want unconstrained optimization and thank you for those references I will check them out.

---

<div class="post-metadata">

### Author: ![tkoolen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tkoolen/32/1603_2.png) [@tkoolen](https://discourse.julialang.org/u/tkoolen)
#### Post date: [June 12, 2018, 1:13pm UTC](https://discourse.julialang.org/t/help-creating-a-jump-variable-array/11515/19 "2018-06-12T13:13:17Z")

</div>

Yeah, Optim may be a better choice at this point in time. Hopefully JuMP’s nonlinear interface will become a little easier to use for cases like this in the future.
