# How Define a Complex JuMP Variable

**URL:** <https://discourse.julialang.org/t/how-define-a-complex-jump-variable/14362>\
**Category:** Optimization (Mathematical)\
**Created:** [August 31, 2018, 1:00pm UTC](https://discourse.julialang.org/t/how-define-a-complex-jump-variable/14362 "2018-08-31T13:00:51Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![muriloasouza](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/muriloasouza/32/4650_2.png) [@muriloasouza](https://discourse.julialang.org/u/muriloasouza)\
**Post date:** [August 31, 2018, 1:00pm UTC](https://discourse.julialang.org/t/how-define-a-complex-jump-variable/14362/1 "2018-08-31T13:00:51Z")

</div>

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`:

```julia
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:

```julia
ERROR: LoadError: InexactError()
Stacktrace:
 [1] convert at .\complex.jl:31 [inlined]
 [2] push!(::Array{Float64,1}, ::Complex{Bool}) at .\array.jl:646
 [3] parseNLExpr_runtime(::JuMP.Model, ::Complex{Bool}, ::Array{ReverseDiffSparse.NodeData,1}, ::Int64, ::Array{Float64,1}) at C:\Users\Muril\.julia\v0.6\JuMP\src\parsenlp.jl:196
 [4] macro expansion at C:\Users\Muril\.julia\v0.6\JuMP\src\parsenlp.jl:226 [inlined]
 [5] macro expansion at C:\Users\Muril\.julia\v0.6\JuMP\src\macros.jl:1201 [inlined]
 [6] anonymous at .\<missing>:?
 [7] include_string(::String, ::String) at .\loading.jl:522
 [8] include_string(::Module, ::String, ::String) at C:\Users\Muril\.julia\v0.6\Compat\src\Compat.jl:88
 [9] (::Atom.##112#116{String,String})() at C:\Users\Muril\.julia\v0.6\Atom\src\eval.jl:109
 [10] withpath(::Atom.##112#116{String,String}, ::String) at C:\Users\Muril\.julia\v0.6\CodeTools\src\utils.jl:30
 [11] withpath(::Function, ::String) at C:\Users\Muril\.julia\v0.6\Atom\src\eval.jl:38
 [12] hideprompt(::Atom.##111#115{String,String}) at C:\Users\Muril\.julia\v0.6\Atom\src\repl.jl:67
 [13] macro expansion at C:\Users\Muril\.julia\v0.6\Atom\src\eval.jl:106 [inlined]
 [14] (::Atom.##110#114{Dict{String,Any}})() at .\task.jl:80
while loading C:\Users\Muril\Desktop\Murilo\UFPE\Dissertação\Julia\Arquivos Feitos\teste_otimização_matriz_3_dimensões.jl, in expression starting on line 69

```

Is this related to `y5` not being defined as a complex variable?

---

<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:** [September 1, 2018, 5:58am UTC](https://discourse.julialang.org/t/how-define-a-complex-jump-variable/14362/2 "2018-09-01T05:58:27Z")

</div>

JuMP can only solve problems with real variables. You should reformulate the problem in polar form, or add two JuMP variables, one for the real part and one for the imaginary part.

---

<div class="post-metadata">

**Author:** ![muriloasouza](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/muriloasouza/32/4650_2.png) [@muriloasouza](https://discourse.julialang.org/u/muriloasouza)\
**Post date:** [September 3, 2018, 6:27pm UTC](https://discourse.julialang.org/t/how-define-a-complex-jump-variable/14362/3 "2018-09-03T18:27:53Z")

</div>

There are any plans to make complex variables work on JuMP?

I don’t know if i understood correctly what you said, but i guess my problem can’t be solved?

I have a restriction in the form of:

`A*x = b`

Where A and b are a matrix and a vector, respectively, full of complex numbers inside of them (for example, that variable `y5` is inside the matrix `A`).

---

<div class="post-metadata">

**Author:** ![joaquimg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/joaquimg/32/223_2.png) [@joaquimg](https://discourse.julialang.org/u/joaquimg)\
**Post date:** [September 3, 2018, 8:16pm UTC](https://discourse.julialang.org/t/how-define-a-complex-jump-variable/14362/4 "2018-09-03T20:16:06Z")

</div>

> There are any plans to make complex variables work on JuMP?

There are no plans from the main JuMP developers to add support complex variables AFAIK.

> I don’t know if i understood correctly what you said, but i guess my problem can’t be solved?

What Oscar said is that you can still model your problem, but you will need to decompose the complex variables in some combination of real numbers with something like a polar or rectangular decomposition. This is not only doable but it has already been done in some packages like [PowerModels.jl](https://github.com/lanl-ansi/PowerModels.jl)

Going back to:

> There are any plans to make complex variables work on JuMP?

In this work [[slides](http://www.juliaopt.org/meetings/bordeaux2018/sliwak.pdf)/[video](https://www.youtube.com/watch?v=NYxBJ1YSXO8&index=10&list=PLP8iPy9hna6RJUxzYlWENcs9yf-CRoDvD)] presented at [JuMP-dev workshop](http://www.juliaopt.org/meetings/bordeaux2018/), the author did something along thoses lines. It is not officially supported by JuMP though.

Creating JuMP extensions to support complex variables would certainly be appreciated.
