I am trying to understand complex numbers and I thought that if I could get a source code, it would help me. Like many people, I don't get what $i$ does (Square root of $-1$). I got some code on the net which I posted below. As you can see, there is no $i$. Does that mean that we can ignore it?
I am also trying to understand the Discrete Fourier Transform and I don't get this expression:
e^(ix) = cos(x) + i sin(x)
Since $i = \sqrt{-1}$ and it's impossible, I tried to replace it by $1$ and it doesn't work. I did this since the source code below doesn't include $i$ anywhere so I though that maybe I could just replace it by $1$.
struct complex
{
float real;
float imag;
};
struct complex sum(struct complex t1, struct complex t2)
{
struct complex t;
t.real = t1.real + t2.real;
t.imag = t1.imag + t2.imag;
return t;
}
struct complex product(struct complex t1, struct complex t2)
{
struct complex t;
t.real = t1.real * t2.real - t1.imag * t2.imag;
t.imag = t1.real * t2.imag + t1.imag * t2.real;
return t;
}
From an abstract algebra point of view, a complex number can be defined as a cartesian product of two real numbers.
$$ \mathbb{C} = \mathbb{R} \times \mathbb{R} = ((a,b) | a \in \mathbb{R},b \in \mathbb{R}) $$
In yours struct, which represents a general complex number, a is 'real' and b is 'imag', referring to the real and imaginary part of a complex number. Addition and multiplication in the field $\mathbb{C}$:
$$ (a,b) + (c,d) = (a+c,b+d) \\ (a,b) * (c,d) = (ac-bd, ad+bc) $$
So basically, a complex number can be considered a combination of two real numbers. This i is just a notation to discern between the real and imaginary part.