"Testing complex numbers"

clear

check(prefixform((-1)^( 0/8)) == "1")
check(prefixform((-1)^( 1/8)) == "(^ -1 1/8)")
check(prefixform((-1)^( 2/8)) == "(^ -1 1/4)")
check(prefixform((-1)^( 3/8)) == "(^ -1 3/8)")
check(prefixform((-1)^( 4/8)) == "(^ -1 1/2)")
check(prefixform((-1)^( 5/8)) == "(* -1 (^ -1 -3/8))")
check(prefixform((-1)^( 6/8)) == "(* -1 (^ -1 -1/4))")
check(prefixform((-1)^( 7/8)) == "(* -1 (^ -1 -1/8))")
check(prefixform((-1)^( 8/8)) == "-1")
check(prefixform((-1)^( 9/8)) == "(* -1 (^ -1 1/8))")
check(prefixform((-1)^(10/8)) == "(* -1 (^ -1 1/4))")
check(prefixform((-1)^(11/8)) == "(* -1 (^ -1 3/8))")
check(prefixform((-1)^(12/8)) == "(* -1 (^ -1 1/2))")
check(prefixform((-1)^(13/8)) == "(^ -1 -3/8)")
check(prefixform((-1)^(14/8)) == "(^ -1 -1/4)")
check(prefixform((-1)^(15/8)) == "(^ -1 -1/8)")
check(prefixform((-1)^(16/8)) == "1")
check(prefixform((-1)^(17/8)) == "(^ -1 1/8)")
check(prefixform((-1)^(18/8)) == "(^ -1 1/4)")
check(prefixform((-1)^(19/8)) == "(^ -1 3/8)")
check(prefixform((-1)^(20/8)) == "(^ -1 1/2)")
check(prefixform((-1)^(21/8)) == "(* -1 (^ -1 -3/8))")
check(prefixform((-1)^(22/8)) == "(* -1 (^ -1 -1/4))")
check(prefixform((-1)^(23/8)) == "(* -1 (^ -1 -1/8))")
check(prefixform((-1)^(24/8)) == "-1")
check(prefixform((-1)^(25/8)) == "(* -1 (^ -1 1/8))")
check(prefixform((-1)^(26/8)) == "(* -1 (^ -1 1/4))")
check(prefixform((-1)^(27/8)) == "(* -1 (^ -1 3/8))")
check(prefixform((-1)^(28/8)) == "(* -1 (^ -1 1/2))")
check(prefixform((-1)^(29/8)) == "(^ -1 -3/8)")
check(prefixform((-1)^(30/8)) == "(^ -1 -1/4)")
check(prefixform((-1)^(31/8)) == "(^ -1 -1/8)")
check(prefixform((-1)^(32/8)) == "1")

check(prefixform((-1)^(- 0/8)) == "1")
check(prefixform((-1)^(- 1/8)) == "(^ -1 -1/8)")
check(prefixform((-1)^(- 2/8)) == "(^ -1 -1/4)")
check(prefixform((-1)^(- 3/8)) == "(^ -1 -3/8)")
check(prefixform((-1)^(- 4/8)) == "(* -1 (^ -1 1/2))")
check(prefixform((-1)^(- 5/8)) == "(* -1 (^ -1 3/8))")
check(prefixform((-1)^(- 6/8)) == "(* -1 (^ -1 1/4))")
check(prefixform((-1)^(- 7/8)) == "(* -1 (^ -1 1/8))")
check(prefixform((-1)^(- 8/8)) == "-1")
check(prefixform((-1)^(- 9/8)) == "(* -1 (^ -1 -1/8))")
check(prefixform((-1)^(-10/8)) == "(* -1 (^ -1 -1/4))")
check(prefixform((-1)^(-11/8)) == "(* -1 (^ -1 -3/8))")
check(prefixform((-1)^(-12/8)) == "(^ -1 1/2)")
check(prefixform((-1)^(-13/8)) == "(^ -1 3/8)")
check(prefixform((-1)^(-14/8)) == "(^ -1 1/4)")
check(prefixform((-1)^(-15/8)) == "(^ -1 1/8)")
check(prefixform((-1)^(-16/8)) == "1")
check(prefixform((-1)^(-17/8)) == "(^ -1 -1/8)")
check(prefixform((-1)^(-18/8)) == "(^ -1 -1/4)")
check(prefixform((-1)^(-19/8)) == "(^ -1 -3/8)")
check(prefixform((-1)^(-20/8)) == "(* -1 (^ -1 1/2))")
check(prefixform((-1)^(-21/8)) == "(* -1 (^ -1 3/8))")
check(prefixform((-1)^(-22/8)) == "(* -1 (^ -1 1/4))")
check(prefixform((-1)^(-23/8)) == "(* -1 (^ -1 1/8))")
check(prefixform((-1)^(-24/8)) == "-1")
check(prefixform((-1)^(-25/8)) == "(* -1 (^ -1 -1/8))")
check(prefixform((-1)^(-26/8)) == "(* -1 (^ -1 -1/4))")
check(prefixform((-1)^(-27/8)) == "(* -1 (^ -1 -3/8))")
check(prefixform((-1)^(-28/8)) == "(^ -1 1/2)")
check(prefixform((-1)^(-29/8)) == "(^ -1 3/8)")
check(prefixform((-1)^(-30/8)) == "(^ -1 1/4)")
check(prefixform((-1)^(-31/8)) == "(^ -1 1/8)")
check(prefixform((-1)^(-32/8)) == "1")

z1 = 1+i     -- z1 does not have arctan in polar form
z2 = 2+3*i   -- z2 does have arctan in polar form

check(arg(z1)=pi/4)
check(mag(z1)=sqrt(2))

check(arg(z2)=arctan(3,2))
check(mag(z2)=sqrt(13))

check(arg(polar(z1))=pi/4)
check(arg(clock(z1))=pi/4)

check(arg(polar(z2))=arctan(3,2))
check(arg(clock(z2))=arctan(3,2))

check(mag(polar(z1))=sqrt(2))
check(mag(clock(z1))=sqrt(2))

check(mag(polar(z2))=sqrt(13))
check(mag(clock(z2))=sqrt(13))

check(z1*z2=rect(clock(z1)*clock(z2)))
check(z1*z2=rect(clock(z1)*polar(z2)))
check(z1*z2=rect(polar(z1)*clock(z2)))
check(z1*z2=rect(polar(z1)*polar(z2)))

z = sqrt(1+i)
check(rect(z^2)=1+i)
z = sqrt(1-i)
check(rect(z^2)=1-i)
z = sqrt(-1+i)
check(rect(z^2)=-1+i)
z = sqrt(-1-i)
check(rect(z^2)=-1-i)

z = sqrt(2+3*i)
check(rect(z^2)=2+3*i)
z = sqrt(2-3*i)
check(rect(z^2)=2-3*i)
z = sqrt(-2+3*i)
check(rect(z^2)=-2+3*i)
z = sqrt(-2-3*i)
check(rect(z^2)=-2-3*i)

z1 = sqrt(2+3*i)
z2 = sqrt(4+5*i)
check(sqrt(z1*z2)=sqrt(z1)*sqrt(z2))
check(sqrt(z1/z2)=sqrt(z1)/sqrt(z2))

z1 = 1+i     -- z1 does not have arctan
z2 = 2+3*i   -- z2 does have arctan
check(z1*z2=rect(clock(z1)*clock(z2)))
check(z1*z2=rect(polar(z1)*polar(z2)))

check(arg(z1)=pi/4)
check(arg(polar(z1))=pi/4)
check(arg(clock(z1))=pi/4)

check(arg(z2)=arctan(3,2))
check(arg(polar(z2))=arctan(3,2))
check(arg(clock(z2))=arctan(3,2))

check(mag(z1)=sqrt(2))
check(mag(polar(z1))=sqrt(2))
check(mag(clock(z1))=sqrt(2))

check(mag(z2)=sqrt(13))
check(mag(polar(z2))=sqrt(13))
check(mag(clock(z2))=sqrt(13))

T = clock(2+3*i) - clock(2+3*i)
check(T=0)

T = mag(1.0 + 3 i)
check(infixform(T)="3.16228")
T = real(1.0 + 3 i)
check(infixform(T)="1")
T = imag(1.0 i)
check(infixform(T)="1")
T = imag(-1.0 i)
check(infixform(T)="-1")

clear

T = (2+3*i)^c
check(infixform(T)="(2 + 3 i)^c")

T = (1+i)^2
check(prefixform(T)="(* 2 (^ -1 1/2))")

T = (2*i)^2
check(T=-4)

T = (2+3*i)^2
check(prefixform(T)="(+ -5 (* 12 (^ -1 1/2)))")

T = (2+3*i)^(-2)
check(prefixform(T)"(+ -5/169 (* -12/169 (^ -1 1/2)))")

T = (2+3*i)^(1/2)
check(infixform(T)="13^(1/4) (-1)^(arctan(3,2) / (2 pi))")

T = (2+3*i)^(-1/2)
check(infixform(T)="(-1)^(-arctan(3,2) / (2 pi)) / 13^(1/4)")

T = i^0
check(T=1)

T = i^1
check(prefixform(T)="(^ -1 1/2)")

T = i^2
check(T=-1)

T = i^3
check(prefixform(T)="(* -1 (^ -1 1/2))")

T = i^4
check(T=1)

T = i^5
check(prefixform(T)="(^ -1 1/2)")

T = i^6
check(T=-1)

T = i^7
check(prefixform(T)="(* -1 (^ -1 1/2))")

T = i^(-1)
check(prefixform(T)="(* -1 (^ -1 1/2))")

T = i^(-2)
check(T=-1)

T = i^(-3)
check(prefixform(T)="(^ -1 1/2)")

T = i^(-4)
check(T=1)

T = i^(-5)
check(prefixform(T)="(* -1 (^ -1 1/2))")

T = i^(-6)
check(T=-1)

T = i^(-7)
check(prefixform(T)="(^ -1 1/2)")
