/*complex.c - data structure for, and operations on, complex numbers*/ #include "complex.h" complex ComplexMultiply(a, b) complex a, b; { complex product; Re(product) = Re(a)*Re(b) - Im(a)*Im(b); Im(product) = Re(a)*Im(b) + Im(a)*Re(b); return product; } complex ComplexDivide(a, b) complex a, b; { complex quotient; double magnitude_sq; magnitude_sq = Re(b)*Re(b) + Im(b)*Im(b); Re(b) /= magnitude_sq; Im(b) = -Im(b)/magnitude_sq; /*construct normalised conjugate*/ quotient = ComplexMultiply(a, b); return quotient; } complex ComplexAdd(a, b) complex a, b; { complex sum; Re(sum) = Re(a)+Re(b); Im(sum) = Im(a)+Im(b); return sum; } complex ComplexSubtract(a, b) complex a, b; { complex difference; Re(difference) = Re(a)-Re(b); Im(difference) = Im(a)-Im(b); return difference; }