/** * A class that represents complex numbers. * * @author EECS1030_W15 * */ public class Complex { private double real; private double imag; /** * Create a complex number equal to 0 + 0i. * */ public Complex() { this(0.0, 0.0); } /** * Create a complex number with a zero imaginary part and a given real part. * * @param re * The real part of the complex number. */ public Complex(double re) { this(re, 0.0); } /** * Create a complex number that has the same real and imaginary parts as * another complex number. * * @param other * The complex number to copy. */ public Complex(Complex other) { this(other.getReal(), other.getImag()); } /** * Create a complex number with the given real and imaginary components. * * @param re * The real part of the complex number. * @param im * The imaginary part of the complex number. */ public Complex(double re, double im) { this.real = re; this.imag = im; } /** * Get the real part of this complex number. * * @return The real part of this complex number. */ public double getReal() { return this.real; } /** * Get the imaginary part of this complex number. * * @return The imaginary part of this complex number. */ public double getImag() { return this.imag; } /** * Set the real part of this complex number. * * @param real the new value for the real part of this complex number */ public void setReal(double real) { this.real = real; } /** * Set the imaginary part of this complex number. * * @param imag the new value for the imaginary part of this complex number */ public void setImag(double imag) { this.imag = imag; } /** * Add this complex number and another complex number to obtain a new complex * number. * * @param other * The complex number to add to this complex number. * @return A new Complex number equal to the sum of this complex number and c. */ public Complex add(Complex other) { double a = this.getReal(); double b = this.getImag(); double c = other.getReal(); double d = other.getImag(); return new Complex(a + c, b + d); } /** * Multiply this complex number with another complex number to obtain a new * complex number. * *

* This is not an industrial strength implementation of complex number * multiplication. In particular, issues related to the differences between * -0.0 and 0.0, infinite real or imaginary parts, * and underflow and overflow are not taken into account. * * @param c * The complex number to multiply by. * @return A new Complex number equal to this complex number * multiplied by c. */ public Complex multiply(Complex c) { return new Complex(this.getReal() * c.getReal() - this.getImag() * c.getImag(), this.getReal() * c.getImag() + this.getImag() * c.getReal()); } /** * Compute the magnitude of this complex number. * * @return the magnitude of this complex number. */ public double abs() { return Math.hypot(this.getReal(), this.getImag()); } /** * Compute the complex conjugate of this complex number. For a complex * number (a + bi) the complex conjugate is (a + (-b)i) * * @return a new complex number equal to the complex conjugate of this number */ public Complex conj() { return new Complex(this.getReal(), -this.getImag()); } /** * Return a hash code for this complex number. * * @return A hash code value for this complex number. */ @Override public int hashCode() { final int prime = 31; int result = 1; long temp; temp = Double.doubleToLongBits(imag); result = prime * result + (int) (temp ^ (temp >>> 32)); temp = Double.doubleToLongBits(real); result = prime * result + (int) (temp ^ (temp >>> 32)); return result; } /** * Compares this complex number with the specifed object. The result is * true if and only if the argument is a Complex * number with the same real and imaginary parts as this complex number. * *

* The comparisons of the real and imaginary parts are performed in a manner * consistent with Double.compare(double, double)). * * @param obj * The object to compare this Complex number against. * @return true if the given object is a Complex * number equal to this complex number, false otherwise. */ @Override public boolean equals(Object obj) { if (this == obj) { return true; } if (obj == null) { return false; } if (getClass() != obj.getClass()) { return false; } Complex other = (Complex) obj; if (Double.compare(this.getReal(), other.getReal()) != 0) { return false; } if (Double.compare(this.getImag(), other.getImag()) != 0) { return false; } return true; } /** * Returns a string representation of this complex number. * *

* The returned string is the real part of the complex number, followed by a * space, followed by a + or - sign, followed by a * space, followed by the absolute value of the imaginary part of the complex * number, followed by an i. The sign is + if the * imaginary part of the complex number is positive, and - if the * imaginary part of the complex number is negative. * * For example the complex number with real and imaginary parts equal to zero * has the string representation 0.0 + 0.0i. The complex number * with real part equal to zero and imaginary part equal to -1 * has the string representation 0.0 - 1.0i. * * @return A string representation of this complex number. * */ @Override public String toString() { StringBuilder b = new StringBuilder(); b.append(this.getReal()); double imag = this.getImag(); if (imag < 0) { b.append(" - "); } else { b.append(" + "); } b.append(Math.abs(imag)); b.append('i'); return b.toString(); } /** * Returns a reference to a new Complex number given its * polar form. * *

* The polar form of a complex number can be drawn in a two-dimensional * Cartesian coordinate system as a vector from the origin to the * coordinates (re, im) where re is the * real part of the complex number and im is the imaginary * part of the complex number. * *

*
* *

* The polar form is given as a non-negative magnitude and an angle θ from * the positive real axis. The real and imaginary components can be * computed from the polar form as: * *

* re = magnitude * cos(θ)
* im = magnitude * sin(θ) * * @param magnitude the magnitude of the complex number * @param theta the angle in radians from the real axis * @return a new Complex number equal to the given polar form */ public static Complex fromPolar(double magnitude, double theta) { if (magnitude < 0.) { throw new IllegalArgumentException("magnitude must not be negative"); } if (magnitude == 0.) { return new Complex(); } return new Complex(magnitude * Math.cos(theta), magnitude * Math.sin(theta)); } }