/*=====================================================================
Filename:	ComplexMath.c
Language:	Think C

Source code for complex math functions

Modifications:
	02/23/90	Gerry Beauregard	Created this file
	02/27/91	Gerry Beauregard	Switch to floats from doubles
==========================================================================*/

/*=======================================================================
Includes
=========================================================================*/
 
#include 	<math.h>
#include	"ComplexMath.h"

/*-- CAvg--------------------------------------------------------------
Complex average
----------------------------------------------------------------------*/		
Complex CAvg(Complex a, Complex b)
{
	Complex	Avg;
	
	Avg.Re = (a.Re + b.Re)/2.0;
	Avg.Im = (a.Im + b.Im)/2.0;
	return Avg;
}

/*-- CAdd--------------------------------------------------------------
Complex addition
----------------------------------------------------------------------*/		
Complex CAdd(Complex a, Complex b)
{
	Complex	Sum;
	
	Sum.Re = a.Re + b.Re;
	Sum.Im = a.Im + b.Im;
	return Sum;
}

/*--CSub--------------------------------------------------------------
Complex subtraction
----------------------------------------------------------------------*/		
Complex CSub(Complex a, Complex b)
{
	Complex	Difference;
	
	Difference.Re = a.Re - b.Re;
	Difference.Im = a.Im - b.Im;
	return Difference;
}

/*--CMult--------------------------------------------------------------
Complex multiplication
----------------------------------------------------------------------*/		
Complex CMult(Complex a, Complex b)
{
	Complex	Product;
	
	Product.Re = a.Re*b.Re - a.Im*b.Im;
	Product.Im = a.Re*b.Im + a.Im*b.Re;
	return Product;
}
	
/*--CDiv--------------------------------------------------------------
Complex division
----------------------------------------------------------------------*/		
Complex CDiv(Complex a, Complex b)
{
	Polar	PolarQuotient;
	Complex	Quotient;
	
	PolarQuotient.Mag = ComplexMag(a)/ComplexMag(b);
	PolarQuotient.Phase = ComplexPhase(a) - ComplexPhase(b);
	Quotient = PolarToComplex(PolarQuotient);
	return Quotient;
}		

/*--Conj--------------------------------------------------------------
Complex conjugate
----------------------------------------------------------------------*/		
Complex Conj(Complex a)
{
	Complex	Conjugate;
	
	Conjugate = a;
	Conjugate.Im *= -1.0;
	
	return Conjugate;
}		

/*--ComplexToPolar----------------------------------------------------
Conversion from rectangular to polar form
----------------------------------------------------------------------*/		
Polar ComplexToPolar(Complex	x)
{
	Polar	Result;
	
	Result.Mag   = ComplexMag(x);
	Result.Phase = ComplexPhase(x);
	return Result;
}

/*--PolarToComplex----------------------------------------------------
Conversion from polar to rectangular form
----------------------------------------------------------------------*/		
Complex PolarToComplex(Polar x)
{
	Complex	Result;
	
	Result.Re = x.Mag * cos(x.Phase);
	Result.Im = x.Mag * sin(x.Phase);
	return Result;
}

/*--ComplexMag----------------------------------------------------
Compute magnitude of complex number
----------------------------------------------------------------------*/		
float ComplexMag(Complex x)
{
	float	Magnitude;

	Magnitude = sqrt(x.Re*x.Re + x.Im*x.Im);
	return Magnitude;
}

/*--ComplexPhase----------------------------------------------------
Compute the phase of a complex number
----------------------------------------------------------------------*/		
float ComplexPhase(Complex x)
{
	float	Phase;

	Phase = atan2(x.Im, x.Re);
	return Phase;
}

/*--RadToDeg----------------------------------------------------
Convert radians to degrees
----------------------------------------------------------------------*/		
float RadToDeg(float RadianPhase)
{
	float	DegreePhase;

	DegreePhase = 360.0 * RadianPhase / (2.0 * MY_PI);
	return DegreePhase;
}

/*--DegToRad----------------------------------------------------
Convert degrees to radians
----------------------------------------------------------------------*/		
float DegToRad(float DegreePhase)
{
	float	RadianPhase;

	RadianPhase = 2.0 * MY_PI * DegreePhase / 360.0;
	return RadianPhase;
}


	
