I have a text file which contains a 64×64 matrix values and they are complex numbers. I want to read them from a file but I’m having difficulties. Either using the complex library of C or creating a new data type for complex numbers is okay for me, I just need them read correctly.
What I mean is, whether using:
#include <complex.h>
int complex matrix[64][64];
or creating a data type for it:
typedef struct {
int real, imag;
} Complex;
Complex matrix[64][64];
is okay for me as long as they are read correctly.
Below you can find 2×3 matrix, just to demonstrate how the numbers are in my file:
{{-32767, 12532 + 5341I, -3415 - 51331I}
{32767I, 32609 + 3211I, 32137 + 6392I}}
So as you can see some parts have both the real and imaginary part, some just the imaginary and some just the real part, and all the imaginary numbers have upper case ‘i’ letter at the end. If you could help me with that, I would be glad.
Two common design patterns apply:
1) Recursive descent parser that accepts a ‘language’
2) State Machine
Both cases can benefit from a read_char() function, that exits with error if it encounters
anything else than ‘{‘, ‘}’, 0-9, i, +, – and which skips all white spaces (ch<=32).
A state machine can be a bit more versatile: if at any point there is ‘+’ or ‘-‘, one can just add or subtract the next value (ending with ‘i’ or non ‘i’) to the currently accumulated value. (Then the state machine is able to also calculate 1+2-1+1i while it goes…)