A   complex number   is a number which can be written as:




a
+
b
×
i


{\displaystyle a+b\times i}


(sometimes shown as:




b
+
a
×
i


{\displaystyle b+a\times i}


where   



a


{\displaystyle a}

   and   



b


{\displaystyle b}

  are real numbers,   and   



i


{\displaystyle i}

   is   √ -1 



Typically, complex numbers are represented as a pair of real numbers called the "imaginary part" and "real part",   where the imaginary part is the number to be multiplied by 



i


{\displaystyle i}

.



Task

 Show addition, multiplication, negation, and inversion of complex numbers in separate functions. (Subtraction and division operations can be made with pairs of these operations.) 
 Print the results for each operation tested.
 Optional: Show complex conjugation.


By definition, the   complex conjugate   of




a
+
b
i


{\displaystyle a+bi}


is




a
−
b
i


{\displaystyle a-bi}





Some languages have complex number libraries available.   If your language does, show the operations.   If your language does not, also show the definition of this type.




public class Complex {
    public final double real;
    public final double imag;
 
    public Complex() {
        this(0, 0);
    }
 
    public Complex(double r, double i) {
        real = r;
        imag = i;
    }
 
    public Complex add(Complex b) {
        return new Complex(this.real + b.real, this.imag + b.imag);
    }
 
    public Complex mult(Complex b) {
        // FOIL of (a+bi)(c+di) with i*i = -1
        return new Complex(this.real * b.real - this.imag * b.imag,
                this.real * b.imag + this.imag * b.real);
    }
 
    public Complex inv() {
        // 1/(a+bi) * (a-bi)/(a-bi) = 1/(a+bi) but it's more workable
        double denom = real * real + imag * imag;
        return new Complex(real / denom, -imag / denom);
    }
 
    public Complex neg() {
        return new Complex(-real, -imag);
    }
 
    public Complex conj() {
        return new Complex(real, -imag);
    }
 
    @Override
    public String toString() {
        return real + " + " + imag + " * i";
    }
 
    public static void main(String[] args) {
        Complex a = new Complex(Math.PI, -5); //just some numbers
        Complex b = new Complex(-1, 2.5);
        System.out.println(a.neg());
        System.out.println(a.add(b));
        System.out.println(a.inv());
        System.out.println(a.mult(b));
        System.out.println(a.conj());
    }
}