/* * complex.c * * This file contains C functions implementing a partial complex * arithmetic package. The functions operate on abstract arrays of * complex numbers stored as floats, with the real parts in the even * indices and the imaginary parts in the odd indices. */ #include #include "complex.h" /* * Return the real part of the n'th complex number in the array */ float get_real_part (float cv[], int n) { return cv[n*2]; } /* * Return the imaginary part of the n'th complex number in the array */ float get_imag_part (float cv[], int n) { return cv[n*2+1]; } /* * Set the real part of the n'th complex number equal to 'val' */ void set_real_part (float cv[], int n, float val) { cv[n*2] = val; } /* * Set the imaginary part of the n'th complex number equal to 'val' */ void set_imag_part (float cv[], int n, float val) { cv[n*2+1] = val; } /* * Return the magnitude (i.e., distance from the origin in the * complex plane) of the n'th complex number. */ float magnitude (float cv[], int n) { float rp, ip, sq; rp = get_real_part (cv, n); ip = get_imag_part (cv, n); sq = rp * rp + ip * ip; return (float) sqrt (sq); } /* * Scalar multiply the n'th number by a float, placing the value * back into the array */ float scalar_mult (float cv[], int n, float r) { cv[n*2] *= r; cv[n*2+1] *= r; }