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.
Pkg.add(["CSV"])
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.:
- using the + sign and the imaginary part symbol;
- using the complex command.
1+1im
mat"1+1i"
Complex_eng = complex(2,2)
Complex_mat = mat"complex(2,2)"
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.
r = real(Complex_eng)
i = imag(Complex_eng)
print("real: ",r,", imag: ",i)
mat"complex($r,$i)"
There are also nuances when reading from a file. Let's consider parsing a file with imaginary numbers using the example of TXT.
path = @__DIR__ # каталог, где лежит текущий скрипт
Let's write a complex number in a text document from MATLAB.
mat"csvwrite($path + string('/Data.txt'),2+3i)"
Let's calculate this value in Engee.
txt = open(io->read(io, String), path * "/Data.txt")
Thus, we have calculated a string containing a complex number in MATLAB. Let's write a function for parsing it.
parse(ComplexF64,txt)
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.