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.




' FB 1.05.0 Win64
 
Type Complex
  As Double real, imag 
  Declare Constructor(real As Double, imag As Double)
  Declare Function invert() As Complex
  Declare Function conjugate() As Complex
  Declare Operator cast() As String  
End Type
 
Constructor Complex(real As Double, imag As Double)
  This.real = real
  This.imag = imag
End Constructor
 
Function Complex.invert() As Complex
  Dim denom As Double = real * real + imag * imag
  Return Complex(real / denom, -imag / denom)
End Function 
 
Function Complex.conjugate() As Complex
  Return Complex(real, -imag)
End Function
 
Operator Complex.Cast() As String
  If imag >= 0 Then 
    Return Str(real) + "+" + Str(imag) + "j"
  End If
  Return Str(real) + Str(imag) + "j" 
End Operator
 
Operator - (c As Complex) As Complex 
  Return Complex(-c.real, -c.imag)
End Operator
 
Operator + (c1 As Complex, c2 As Complex) As Complex
  Return Complex(c1.real + c2.real, c1.imag + c2.imag)
End Operator
 
Operator - (c1 As Complex, c2 As Complex) As Complex
  Return c1 + (-c2)
End Operator
 
Operator * (c1 As Complex, c2 As Complex) As Complex
  Return Complex(c1.real * c2.real - c1.imag * c2.imag, c1.real * c2.imag + c2.real * c1.imag)
End Operator
 
Operator / (c1 As Complex, c2 As Complex) As Complex
  Return c1 * c2.invert 
End Operator
 
Var x = Complex(1, 3)
Var y = Complex(5, 2)
Print "x     = "; x
Print "y     = "; y
Print "x + y = "; x + y
Print "x - y = "; x - y
Print "x * y = "; x * y
Print "x / y = "; x / y
Print "-x    = "; -x
Print "1 / x = "; x.invert
Print "x*    = "; x.conjugate
Print
Print "Press any key to quit"
Sleep