# How to formulate a complex JuMP variable array

**URL:** https://discourse.julialang.org/t/how-to-formulate-a-complex-jump-variable-array/24222
**Category:** General Usage
**Tags:** jump, optimization
**Created:** [May 14, 2019, 11:01pm UTC](https://discourse.julialang.org/t/how-to-formulate-a-complex-jump-variable-array/24222 "2019-05-14T23:01:00Z")
**Posts on this page:** 4
**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: [May 14, 2019, 11:01pm UTC](https://discourse.julialang.org/t/how-to-formulate-a-complex-jump-variable-array/24222/1 "2019-05-14T23:01:00Z")

</div>

I would like to create a complex jump array variable and I have been looking through the documentation for awhile and things are still not clear.

Lets say for a trivial example I want to minimize the trace of a DxD complex array. I might create something like:

```julia
using Ipopt
using LinearAlgebra
using JuMP

model = Model(with_optimizer(Ipopt.Optimizer))

function E(x::Vector)

     F = reshape(Complex.(x[1:2:2*D^2-1],x[2:2:2*D^2]),D,D)

     return e = tr(F)
end

F = @variable(model)
@objective(model, Min, e)

optimize!(model)

```

How would I reformat this to be correct?

---

<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: [May 15, 2019, 1:39am UTC](https://discourse.julialang.org/t/how-to-formulate-a-complex-jump-variable-array/24222/2 "2019-05-15T01:39:09Z")

</div>

Please search discourse for existing issues. This question has been asked.

> [@How Define a Complex JuMP Variable](https://discourse.julialang.org/t/how-define-a-complex-jump-variable/14362):
>
> How can this be done? I remember there was a Complex128 syntax or something, but i couldn’t find how to use it in a JuMP variable. Here’s my problem, assume i already have the values of Rf, Xlf and Xcf: Nt = 2; hmax = 5; @variable(m, y5[i = 1:Nt,h = 3:2:hmax]) @NLconstraint(m , con8[i = 1:Nt, h = 3:2:hmax], y5[i,h] == 1/(Rf[i]+im\*(h\*Xlf[i]-Xcf[i]/h))) I got this error: ERROR: LoadError: InexactError() Stacktrace: [1] convert at .\complex.jl:31 [inlined] [2] push!(::Array{Float64,1}, ::Comp…

JuMP cannot handle complex variables. You need to add two variables: one for the real and one for the imaginary part.

---

<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: [May 15, 2019, 3:47pm UTC](https://discourse.julialang.org/t/how-to-formulate-a-complex-jump-variable-array/24222/3 "2019-05-15T15:47:11Z")

</div>

Okay thank you, I’m sorry I didn’t see that.

---

<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: [May 15, 2019, 3:53pm UTC](https://discourse.julialang.org/t/how-to-formulate-a-complex-jump-variable-array/24222/4 "2019-05-15T15:53:10Z")

</div>

No worries! Good luck.
