Complex numbers in programming

77 Views Asked by At

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;
}
4

There are 4 best solutions below

3
On BEST ANSWER

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.

0
On

The code above does not contain the definition of $i = \sqrt{-1}$ as a separate entity. What is does is storing a complex number in the structure complex as two floating numbers. For example, the number $z = 1 + 2i$ is going to be stored as

 struct complex z;
 z.real = 1;
 z.imag = 2;

In this sense, you can define $i$ as

struct complex i;
i.real = 0;
i.imag = 1;
1
On

There is no explicit "i" constant in the code above because it's purpose is to only simulate operations on complex numbers. You should always think that a struct complex has an "i" but is not placed explicitly. You can think about a program that does operations using fractions:

struct Fraction {
    int numerator;
    int denominator;
};

And you can do this:

struct Fraction x;
x.numerator = 10;
x.denominator = 20;

This is conceptually equivalent to: $\frac{10}{20}$ but there is no fraction sign in the code (the little bar between the numerator and the denominator).

0
On

Mathematically, the code gives an implementation of a structure that stores a complex number, and defines the addition and multiplication operators on two complex numbers.

In symbols,

$$C_1 + C_2 = (a_1+b_1i) + (a_2 + b_2i) = (a_1 + a_2) + (b_1 + b_2)i$$

$$C_1 \cdot C_2 = (a_1+b_1i) \cdot (a_2 + b_2i) = (a_1a_2 - b_1b_2) + (a_1b_2 + b_1a_2)i$$

For the addition case, you add up all of the real parts (the $a$'s) and add up all of the imaginary parts (the $b$'s) separately.

For the multiplication case, it's very similar to the FOIL (first, outside, inside, last) method. The main surprise that comes out is $b_1b_2i^2 = -b_1b_2$, because $i^2 = -1$. (Multiplying two purely imaginary numbers gives you a real number!)

As for the identity $e^{ix} = \cos x + i \sin x$, you can write down the Taylor expansion of each term to see that they're equal.

Have fun!