Engee documentation
Notebook

Complex numbers in Engee and MATLAB

This demonstration aims to analyze the differences between complex numbers in MATLAB and Engee and the possibility of transferring numbers from one medium to another.

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 of forming complex numbers themselves is identical for both environments. There are two possible tasks.:

  1. using the + sign and the imaginary part symbol;
  2. using 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 a script from the MATLAB workspace to Engee, then there are no problems with the transfer: the transfer from one environment to another is performed automatically.
But for the reverse action, you need to use the real, imag and complex commands.

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 using the example of TXT.

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

Let's write a complex number in 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 a string containing a complex number in MATLAB. Let's write a function for parsing it.

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

Conclusion

In this demo, we have figured out how to interact with complex numbers in two development environments, namely MATLAB and Engee. We also explored the possibilities of transferring complex numbers between these two media.