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.




 
'COMPLEX OPERATIONS
'=================
 
type tcomplex double x,y
 
class Complex
'============
 
  has tcomplex
  static sys i,pp
  static tcomplex accum[32]
 
def operands
  tcomplex*a,*b
  @a=@accum+i
  if pp then
    @b=@a+sizeof accum
    pp=0
  else
    @b=@this
  end if
end def
 
method "load"()
  operands
  a.x=b.x
  a.y=b.y
end method
 
method "push"()
  i+=sizeof accum
end method
 
method "pop"()
  pp=1
  i-=sizeof accum
end method
 
method "="()
  operands
  b.x=a.x
  b.y=a.y
end method
 
method "+"()
  operands
  a.x+=b.x
  a.y+=b.y
end method
 
method "-"()
  operands
  a.x-=b.x
  a.y-=b.y
end method
 
method "*"()
  operands
  double d
  d=a.x
  a.x = a.x * b.x - a.y * b.y
  a.y = a.y * b.x + d   * b.y
end method
 
method "/"()
  operands
  double d,v
  v=1/(b.x * b.x + b.y * b.y)
  d=a.x
  a.x = (a.x * b.x + a.y * b.y) * v
  a.y = (a.y * b.x - d   * b.y) * v
end method
 
method power(double n)
  operands
  'Using DeMoivre theorem
  double r,an,mg
  r = hypot(b.x,b.y)
  mg = r^n
  if b.x=0 then
    ay=.5*pi
    if b.y<0 then ay=-ay
  else
    an = atan(b.y,b.x)
  end if
  an  *= n 
  a.x  = mg * cos(an)
  a.y  = mg * sin(an)
end method
 
method show() as string
  return str(x,14) ", " str(y,14)
end method
 
end class
 
'#recordof complexop  
 
'====
'TEST
'====
 
complex z1,z2,z3,z4,z5
 
'ENTER VALUES
 
z1 <=  0, 0
z2 <=  2, 1
z3 <= -2, 1
z4 <=  2, 4
z5 <=  1, 1
 
'EVALUATE COMPLEX EXPRESSIONS
 
z1 =  z2 * z3
print "Z1 = "+z1.show 'RESULT  -5.0, 0
 
z1 = z3+(z2.power(2))
print "Z1 = "+z1.show  'RESULT  1.0, 5.0
 
 
z1 = z5/z4
print "Z1 = "+z1.show  'RESULT 0.3, 0.1
 
z1 = z5/z1
print "Z1 = "+z1.show  'RESULT 2.0, 4.0
 
z1 = z2/z4
print "Z1 = "+z1.show  'RESULT  -0.4, -0.3
 
z1 = z1*z4
print "Z1 = "+z1.show  'RESULT  2.0, 1.0
 