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.




import math
 
class Complex
        declare real
        declare imag
 
        def Complex()
                real = 0.0
                imag = 0.0
        end
 
        def Complex(r, i)
                real = double(r)
                imag = double(i)
        end
 
        def operator-(b)
                return new(Complex, this.real - b.real, this.imag - b.imag)
        end
 
        def operator+(b)
                return new(Complex, this.real + b.real, this.imag + b.imag)
        end
 
        def operator*(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)
        end
 
        def inv()
                // 1/(a+bi) * (a-bi)/(a-bi) = 1/(a+bi) but it's more workable
                denom = this.real * this.real + this.imag * this.imag
                return new(Complex, real/denom, -imag/denom)
        end
 
        def neg()
                return new(Complex, -this.real, -this.imag)
        end
 
        def conj()
                return new(Complex, this.real, -this.imag)
        end
 
        def toString()
                return this.real + " + " + this.imag + " * i"
        end
end
 
a = new(Complex, math.pi, -5)
b = new(Complex, -1, 2.5)
println a.neg()
println a + b
println a.inv()
println a * b
println a.conj()