# ModelingToolkit complex system of equations

**URL:** <https://discourse.julialang.org/t/modelingtoolkit-complex-system-of-equations/113370>\
**Category:** Modelling & Simulations\
**Created:** [April 23, 2024, 3:55am UTC](https://discourse.julialang.org/t/modelingtoolkit-complex-system-of-equations/113370 "2024-04-23T03:55:24Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![ograsdijk](https://avatars.discourse-cdn.com/v4/letter/o/d26b3c/32.png) [@ograsdijk](https://discourse.julialang.org/u/ograsdijk)\
**Post date:** [April 23, 2024, 3:55am UTC](https://discourse.julialang.org/t/modelingtoolkit-complex-system-of-equations/113370/1 "2024-04-23T03:55:24Z")

</div>

A few years ago I had a similar problem regarding a system of complex equations and I had found a workaround: [ModelingToolkit matrix form system of equations and complex values](https://discourse.julialang.org/t/modelingtoolkit-matrix-form-system-of-equations-and-complex-values/64431).  
As far as I can tell complex symbolics are still not supported in ModelingToolkit, but now unfortunately my workaround is no longer functioning either. Some of the ModelingToolkit syntax has changed in the almost 3 years since, so here is an updated MWE:

```julia
@parameters icomplex Γ32 δl δμ Ωl Ωμ
@variables t (ρ(t))[1:3,1:3]
D = Differential(t)

H = [
    -δl-δμ -Ωμ/2 0
    -Ωμ/2 -δl -Ωl/2
    0 -Ωl/2 0
]

L = [
    0 0 -Γ32*ρ[1,3]/2
    0 Γ32*ρ[3,3] -Γ32*ρ[2,3]/2
    -Γ32*ρ[3,1]/2 -Γ32*ρ[3,2]/2 -Γ32*ρ[3,3]
]

eqns = D(ρ) ~ -icomplex*(H*ρ-ρ*H) + L

@mtkbuild bloch = ODESystem(Symbolics.scalarize(eqns), t)

ρᵢ = zeros(ComplexF64,3,3)
ρᵢ[2,2] = 1
u0 = [ρ[idx,idy] => ρᵢ[idx,idy] for idx in 1:3 for idy in 1:3]

p = [Ωl => 1, Ωμ=> 0, δl => 0., δμ => 0., Γ32 => 1., icomplex => 1.0im]

prob = ODEProblem(bloch, u0, (0., 20.), p, jac = true)
sol = solve(prob, Tsit5())

```

The issue occurs when constructing the ODEProblem, throwing the an `InexactError: Float64(0.0 + 1.0im)`. It seems like internally it tries to convert `icomplex` to a `Float64`.

Is there a different workaround possible?

---

<div class="post-metadata">

**Author:** ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)\
**Post date:** [April 23, 2024, 10:58am UTC](https://discourse.julialang.org/t/modelingtoolkit-complex-system-of-equations/113370/2 "2024-04-23T10:58:16Z")

</div>

> [@ograsdijk](#):
>
> `@variables t (ρ(t))[1:3,1:3]`

`@variables t (ρ(t))[1:3,1:3]::Complex`? I haven’t tried complex arrays but complex variables should work if the type is appropriately declared.

---

<div class="post-metadata">

**Author:** ![ograsdijk](https://avatars.discourse-cdn.com/v4/letter/o/d26b3c/32.png) [@ograsdijk](https://discourse.julialang.org/u/ograsdijk)\
**Post date:** [April 23, 2024, 2:31pm UTC](https://discourse.julialang.org/t/modelingtoolkit-complex-system-of-equations/113370/3 "2024-04-23T14:31:39Z")

</div>

I tried this but unfortunately it fails with the following error, which happens in the `Symbolics.scalarize(eqns)` step

```julia
TypeError: in Complex, in T, expected T<:Real, got Type{Complex{Real}}

```

---

<div class="post-metadata">

**Author:** ![ograsdijk](https://avatars.discourse-cdn.com/v4/letter/o/d26b3c/32.png) [@ograsdijk](https://discourse.julialang.org/u/ograsdijk)\
**Post date:** [April 23, 2024, 5:38pm UTC](https://discourse.julialang.org/t/modelingtoolkit-complex-system-of-equations/113370/4 "2024-04-23T17:38:44Z")

</div>

It still isn’t functioning, but I’ve narrowed down the issue, but also found a new one.

The dissipator term `L` has explicit `ρ` terms, when these are removed from the equation the `TypeError` dissapears. Constructing the problem then throws the following error: `ERROR: Initialization expression (ρ(t))[1, 1] is currently not supported.`

---

<div class="post-metadata">

**Author:** ![pmc4](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pmc4/32/211593_2.png) [@pmc4](https://discourse.julialang.org/u/pmc4)\
**Post date:** [January 14, 2026, 2:47pm UTC](https://discourse.julialang.org/t/modelingtoolkit-complex-system-of-equations/113370/5 "2026-01-14T14:47:30Z")

</div>

Hi, I stumbled across this post when trying to solve a complex differential equations that also involves a density matrix and a hamiltonian. The following MWE compiles under ModelingToolkit:

```julia-auto
using ModelingToolkit, Symbolics, LinearAlgebra, DifferentialEquations

@independent_variables x
D = Differential(x)

ρ0 = ComplexF64[exp(-0.5) 1im; 0 exp(-0.7)]
U = [0.88 0.27; -0.35 + 0.1im 0.66]

vars = @variables begin
    ρ(x)[1:2, 1:2] = ρ0
    H(x)[1:2, 1:2]
end

eqs = [
    H ~ x^2 * U
    D(ρ) ~ -im * (H * ρ - ρ * H)
]

sys = System(eqs, x, vars, []; name = :foo)
foo = mtkcompile(sys)

```

Here, `ρ0` is the initial value of the matrix variable `ρ` and `U` is a complex matrix used inside the variable `H`. The problem arises when tyring to build a `ODEProblem` and give it to DifferentialEquations. The first line of

```julia-auto
prob = ODEProblem(foo, [], (0.0, 1.0))
sol = solve(prob)

```

produces `InexactError: Real(0.0 + 1.0im)`. Note that I wrote a complex number inside `ρ0`. If I change that line so we have a complex matrix with real values, i.e.,

```julia-auto
ρ0 = ComplexF64[exp(-0.5) 0; 0 exp(-0.7)]

```

the ODE problem is built with output:

```julia-auto
ODEProblem with uType Vector{Float64} and tType Float64. In-place: true
Initialization status: FULLY_DETERMINED
Non-trivial mass matrix: false
timespan: (0.0, 1.0)
u0: 4-element Vector{Float64}:
 0.4965853037914095
 0.0
 0.0
 0.6065306597126334

```

However it says that u0 is a 4-element real vector, not a complex one. I try passing the keyword argument `u0_eltype = ComplexFloat` to the ODEProblem constructor but the output is exactly the same. If I execute the `solve(prob)` line, then the following error arises

```julia-auto
ERROR: InexactError: Float64(0.0 + 2.9685246098730456e-14im)

```

I think it has to due with the fact that treats `u0` as a real vector, as per the previous output.

I thought, okay, let’s declare that explicitly inside the `@variables` call. So I modify the previous code to

```julia-auto
vars = @variables begin
    ρ(x)[1:2, 1:2]::Complex = ρ0
    H(x)[1:2, 1:2]::Complex
end

eqs = [
    H ~ x^2 * U
    D(ρ) ~ -im * (H * ρ - ρ * H)
]

```

But now, the second equation inside `eqs`, the one with `D(ρ) ` arises the following error:

```julia-auto
ERROR: TypeError: in typeassert, expected DataType, got Type{Complex}

```

It looks like the most robust option for now is to separate everything into a real and a complex part, even though is more of a hassle.
