//  File:  Cmplx\Cmplx.java

import java.io.*;

/**
 * Implementation of an ADT for ordinary algebraic complex numbers
 * 
 * @author Bary W Pollack
 * @version Oct. 1, 1999
 * @see Cmplx
 * @See RunCmplx
 */

public class Cmplx {

	/**
	 * the "real" portion of an imaginary number
	 * 
	 * @see Cmplx
	 */
	
	private double re;

	/**
	 * the "imaginary" portion of an imaginary number
	 * 
	 * @see Cmplx
	 */
	private double im;

	/**
	 * the square root of -1
	 * 
	 * @see Cmplx
	 */
	public static final Cmplx i = new Cmplx(0, 1);

	/**
	 * default constructor; creats  0+0i
	 * 
	 * @see Cmplx
	 */

	Cmplx () { re = im = 0; }

	/**
	 * creates the complex number c+di
	 * 
	 * @param re the real portion of the complex number to be created
	 * @param im the imaginary portion of the complex number to be created
	 */
	
	Cmplx(double re, double im) {
		this.re = re;  this.im = im;
	}

	/**
	 * adds two complex numbers
	 * 
	 * @param c a complex number
	 * @param d another complex number
	 * @return the sum of two complex numbers
	 * @see sub
	 */
	
	public static Cmplx add(Cmplx c, Cmplx d) {
		return new Cmplx(c.re + d.re, c.im + d.im);
	}

	/**
	 * subtracts one complex number from another
	 * 
	 * @param c the complex number from which the second is to be subtracted
	 * @param d the complex number to be subtracted
	 * @return difference of two complex numbers
	 * @see add
	 */
	
	public static Cmplx sub(Cmplx c, Cmplx d) {
		return new Cmplx(c.re - d.re, c.im - d.im);
	}

	/**
	 * conjugates a complex number
	 * 
	 * @param c a complex number
	 * @return the complex conjugate of the complex number provided
	 */
	
	public static Cmplx conj(Cmplx c) {
		return new Cmplx(c.re, -c.im);
	}

	/**
	 * multiplies two complex numbers
	 * 
	 * @param c one complex number
	 * @param d another complex number
	 * @return product of two complex numbers
	 * @see div
	 */
	
	public static Cmplx mpy(Cmplx c, Cmplx d) {
		return new Cmplx(c.re * d.re - c.im * d.im,
					     c.re * d.im + c.im * d.re);
	}

	/**
	 * multiplies a scalar times a complex number
	 * 
	 * @param d a scalar double multiplier
	 * @param c a complex number
	 * @return product of a scalar and a complex number
	 * @see div
	 */
	
	public static Cmplx mpy(double d, Cmplx c) {
		return new Cmplx(d * c.re, d * c.im);
	}

	/**
	 * divides two complex numbers
	 * 
	 * @param c the numerator complex number
	 * @param d the denominator complex number
	 * @return a complex number that is the quotient of the two inputs
	 * @see mpy
	 */
	
	public static Cmplx div(Cmplx c, Cmplx d) { 
		double den = c.re * c.re + d.im * d.im;
		return div(mpy(c, conj(d)), den);
	}

	/**
	 * divides a complex number by a scalar
	 * 
	 * @param c numerator complex number
	 * @param d double denominator
	 * @return the quotient a complex numbers when divided by a scalar
	 * @see mpy
	 */
	
	public static Cmplx div(Cmplx c, double d) {
		return new Cmplx(c.re / d, c.im / d);
	}

	/**
	 * calculates the magnitude of a complex number
	 * 
	 * @param c the complex number
	 * @return the magnitude (a double) of the complex number provided as input
	 * @see Cmplx
	 */
	
	public static double mag(Cmplx c) {
		return Math.sqrt(c.re * c.re + c.im * c.im);
	}

	/**
	 * converts a complex number into its String representation
	 * 
	 * @return a String representation of the complex number in the form:  ( a b )
	 * @see Cmplx
	 */
	
	public String toString() { return "( " + re + " " + im + " )"; }
	    
}

