Engee documentation
Notebook

Complex numbers in Engee and MATLAB

This demonstration aims to analyse the differences between complex numbers in MATLAB and Engee and the possibility of transferring numbers from one environment to the other.

In [ ]:
Pkg.add(["CSV"])
In [ ]:
using MATLAB
using CSV
using DataFrames

The main difference lies in the designation of the imaginary part: in MATLAB it is i, and in Engee it is im.

The syntax for generating the complex numbers themselves is identical for both environments. There are two options for assignment:

  1. through the + sign and the imaginary part symbol;
  2. through the complex command.
In [ ]:
1+1im
Out[0]:
1 + 1im
In [ ]:
mat"1+1i"
Out[0]:
1.0 + 1.0im
In [ ]:
Complex_eng = complex(2,2)
Out[0]:
2 + 2im
In [ ]:
Complex_mat = mat"complex(2,2)"
Out[0]:
2.0 + 2.0im

If you transfer complex numbers inside the script from the MATLAB workspace to Engee, there are no problems during the transfer: the transfer from one environment to another is performed automatically. But for the reverse action it is necessary to use the commands real, imag and complex.

In [ ]:
r = real(Complex_eng)
i = imag(Complex_eng)
print("real: ",r,", imag: ",i)
real: 2, imag: 2
In [ ]:
mat"complex($r,$i)"
Out[0]:
2 + 2im

There are also nuances when reading from a file. Let's consider parsing a file with imaginary numbers on the example of TXT.

In [ ]:
path = @__DIR__ # каталог, где лежит текущий скрипт
Out[0]:
"/user/start/examples/base_simulation/complex_numbers"

Let's write a complex number into a text document from MATLAB.

In [ ]:
mat"csvwrite($path + string('/Data.txt'),2+3i)"

Let's calculate this value in Engee.

In [ ]:
txt = open(io->read(io, String), path * "/Data.txt")
Out[0]:
"2+3i\n"

Thus, we have calculated the string containing a complex number in MATLAB. Let's write a function of its parsing.

In [ ]:
parse(ComplexF64,txt)
Out[0]:
2.0 + 3.0im

Conclusion

In this demonstration, we have learnt how to interact with complex numbers in two development environments, namely MATLAB and Engee. We have also explored the possibilities of forwarding complex numbers between these two environments.