'************************************************* '* Test elementary operations on complex numbers * '* --------------------------------------------- * '* * '* SAMPLE RUN: * '* * '* Z1=( 1.0000,-2.0000) * '* Z2=( 2.5000, 5.7500) * '* Z1+Z2=( 3.5000, 3.7500) * '* Z1+Z2 with modulus and phase: * '* ( 5.1296, 0.8199) * '* Z1 with modulus and phase: * '* ( 2.2361,-1.1071) * '* Z2 with modulus and phase: * '* ( 6.2700, 1.1607) * '* Z1*Z2=(14.0000, 0.7500) * '* Z1*Z2 with modulus and phase: * '* (14.0201, 0.0535) * '* Z1/Z2 with modulus and phase: * '* ( 0.3566,-2.2678) * '* Exp(Z1)=(-1.1312,-2.4717) * '* Exp(Z1) with modulus and phase: * '* ( 2.7183,-2.0000) * '* ArgSh(Z2)=( 2.5246, 1.1560) * '* * '* BASIC version by J-P Moreau, Paris. * '* (www.jpmoreau.fr) * '************************************************* DEFINT I-N DEFDBL A-H, O-Z 'a complex number z is represented as a table with 4 locations: ' z(1), z(2) for algebraic form x+iy ' z(3), z(4) for polar form r*exp(i*t) DIM ZZ(4), Z(4), Z1(4), Z2(4) 'Complex numbers DIM temp(4), u(4) 'Complex numbers XINF = 1.2E+16 'big number TINY = 1E-16 'small number PI = 4# * ATN(1) F$ = "##.####" CLS x = 1#: y = -2# GOSUB 500 'AssignXY(ZZ,x,y) FOR i = 1 TO 4 Z1(i) = ZZ(i) NEXT x = 2.5#: y = 5.75# GOSUB 500 'AssignXY(ZZ,x,y) FOR i = 1 TO 4 Z2(i) = ZZ(i) NEXT GOSUB 1000 'AddComplex(ZZ,Z1,Z2) FOR i = 1 TO 4 Z(i) = ZZ(i) NEXT PRINT PRINT " Z1="; FOR i = 1 TO 4 ZZ(i) = Z1(i) NEXT GOSUB 800 'DisplayComplex(ZZ) PRINT " Z2="; FOR i = 1 TO 4 ZZ(i) = Z2(i) NEXT GOSUB 800 'DisplayComplex(ZZ) PRINT " Z1+Z2="; FOR i = 1 TO 4 ZZ(i) = Z(i) NEXT GOSUB 800 'DisplayComplex(ZZ) PRINT " Z1+Z2 with modulus and phase:" GOSUB 900 'DisplayComplexR(ZZ) PRINT " Z1 with modulus and phase:" FOR i = 1 TO 4 ZZ(i) = Z1(i) NEXT GOSUB 900 'DisplayComplexR(ZZ) PRINT " Z2 with modulus and phase:" FOR i = 1 TO 4 ZZ(i) = Z2(i) NEXT GOSUB 900 'DisplayComplexR(ZZ) GOSUB 1300 'MulComplex(ZZ,Z1,Z2) PRINT " Z1*Z2="; GOSUB 800 'DisplayComplex(ZZ) PRINT " Z1*Z2 with modulus and phase:" GOSUB 900 'DisplayComplexR(ZZ) GOSUB 1400 'DivComplex(ZZ,Z1,Z2) PRINT " Z1/Z2 with modulus and Phase:" GOSUB 900 'DisplayComplexR(ZZ) GOSUB 1500 'ExpComplex(ZZ,Z1) PRINT " Exp(Z1)="; GOSUB 800 'DisplayComplex(ZZ) PRINT " Exp(Z1) with modulus and Phase:" GOSUB 900 'DisplayComplexR(ZZ) 'put Z2 in Z1 x = 2.5#: y = 5.75# GOSUB 500 'AssignXY(ZZ,x,y) FOR i = 1 TO 4 Z1(i) = ZZ(i) NEXT GOSUB 2800 'ArgShComplex(ZZ,Z1) PRINT " ArgSh(Z2)="; GOSUB 800 'DisplayComplex(ZZ) PRINT END 'define complex number ZZ by x and y 500 ZZ(1) = x: ZZ(2) = y ZZ(3) = SQR(x * x + y * y) IF x = 0 THEN IF y > 0 THEN ZZ(4) = PI / 2 ELSEIF y = 0 THEN ZZ(4) = -PI / 2 ELSE ZZ(4) = 0# END IF ELSE ZZ(4) = ATN(y / x) IF x < 0 THEN IF y >= 0 THEN ZZ(4) = ZZ(4) + PI ELSE ZZ(4) = ZZ(4) - PI END IF END IF IF ZZ(4) > PI THEN ZZ(4) = ZZ(4) - 2 * PI IF ZZ(4) < -PI THEN ZZ(4) = ZZ(4) + 2 * PI END IF RETURN 'define complex number by r and t in radians 600 ZZ(3) = r: ZZ(4) = t IF ZZ(4) > PI THEN ZZ(4) = ZZ(4) - 2 * PI IF ZZ(4) < -PI THEN ZZ(4) = ZZ(4) + 2 * PI ZZ(1) = r * COS(t): ZZ(2) = r * SIN(t) RETURN 'display complex number with x and y 800 PRINT "("; : PRINT USING F$; ZZ(1); PRINT ","; : PRINT USING F$; ZZ(2); PRINT ")" RETURN 'display complex number with radius and phase in radians 900 PRINT " ("; : PRINT USING F$; ZZ(3); PRINT ","; : PRINT USING F$; ZZ(4); PRINT ")" RETURN 'add two complex numbers: ZZ=Z1+Z2 1000 ZZ(1) = Z1(1) + Z2(1): ZZ(2) = Z1(2) + Z2(2) x = ZZ(1): y = ZZ(2) GOSUB 500 'AssignXY(ZZ,x,y) RETURN 'substract two complex numbers: ZZ=Z1-Z2 1100 ZZ(1) = Z1(1) - Z2(1): ZZ(2) = Z1(2) - Z2(2) x = ZZ(1): y = ZZ(2) GOSUB 500 'AssignXY(ZZ,x,y) RETURN 'change sign of a complex number: ZZ=-ZZ 1200 ZZ(1) = -ZZ(1): ZZ(2) = -ZZ(2) x = ZZ(1): y = ZZ(2) GOSUB 500 'AssignXY(ZZ,x,y) RETURN 'multiply two complex numbers: ZZ=Z1*Z2 1300 ZZ(3) = Z1(3) * Z2(3): ZZ(4) = Z1(4) + Z2(4) r = ZZ(3): t = ZZ(4) GOSUB 600 'AssignRT(ZZ,r,t) RETURN 'divide two complex numbers: ZZ=Z1/Z2 1400 IF Z2(3) < TINY THEN ZZ(3) = XINF ELSE ZZ(3) = Z1(3) / Z2(3) END IF ZZ(4) = Z1(4) - Z2(4) r = ZZ(3): t = ZZ(4) GOSUB 600 'AssignRT(ZZ,r,t) RETURN 'exponential complex function: ZZ=Exp(Z1) 1500 IF EXP(Z1(1)) > XINF THEN tmp = XINF ELSE tmp = EXP(Z1(1)) END IF ZZ(1) = tmp * COS(Z1(2)): ZZ(2) = tmp * SIN(Z1(2)) x = ZZ(1): y = ZZ(2) GOSUB 500 'AssignXY(ZZ,x,y) RETURN ' ZZ=LN(Z1) 1600 IF Z1(3) <= 0 THEN ZZ(1) = -XINF ELSE ZZ(1) = LOG(Z1(3)) END IF ZZ(2) = Z1(4) x = ZZ(1): y = ZZ(2) GOSUB 500 'AssignXY(ZZ,x,y) RETURN 'Complex power ZZ=Z1^Z2 1700 GOSUB 1600 'LnComplex(temp,Z1) FOR i = 1 TO 4 Z1(i) = Z2(i) Z2(i) = ZZ(i) NEXT GOSUB 1300 'MulComplex(temp,Z1,temp) FOR i = 1 TO 4 Z1(i) = ZZ(i) NEXT GOSUB 1500 'ExpComplex(ZZ,Z1) RETURN ' ZZ=COS(Z1) 1800 ZZ(1) = (EXP(-Z1(2)) * COS(Z1(1)) + EXP(Z1(2)) * COS(-Z1(1))) / 2# ZZ(2) = (EXP(-Z1(2)) * SIN(Z1(1)) + EXP(Z1(2)) * SIN(-Z1(1))) / 2# x = ZZ(1): y = ZZ(2) GOSUB 500 'AssignXY(ZZ,x,y) RETURN ' ZZ=SIN(Z1) 1900 ZZ(1) = (EXP(-Z1(2)) * SIN(Z1(1)) - EXP(Z1(2)) * SIN(-Z(1))) / 2# ZZ(2) = -(EXP(-Z1(2)) * COS(Z1(1)) - EXP(Z1(2)) * COS(-Z(1))) / 2# x = ZZ(1): y = ZZ(2) GOSUB 500 'AssignXY(ZZ,x,y) RETURN ' ZZ=TAN(Z1) 2000 GOSUB 1900 'SinComplex(ZZ,Z1) FOR i = 1 TO 4 temp(i) = ZZ(i) NEXT GOSUB 1800 'CosComplex(ZZ,Z1) FOR i = 1 TO 4 Z1(i) = temp(i) Z2(i) = ZZ(i) NEXT GOSUB 1400 'DivComplex(ZZ,Z1,Z2) RETURN ' ZZ=CH(Z1) 2100 ZZ(1) = (EXP(Z1(1)) * COS(Z1(2)) + EXP(-Z1(1)) * COS(-Z1(2))) / 2# ZZ(2) = (EXP(Z1(1)) * SIN(Z1(2)) + EXP(-Z1(1)) * SIN(-Z1(2))) / 2# x = ZZ(1): y = ZZ(2) GOSUB 500 'AssignXY(ZZ,x,y) RETURN ' ZZ=SH(Z1) 2200 ZZ(1) = (EXP(Z1(1)) * COS(Z1(2)) - EXP(-Z1(1)) * COS(-Z1(2))) / 2# ZZ(2) = -(EXP(Z1(1)) * SIN(Z1(2)) - EXP(-Z1(1)) * SIN(-Z1(2))) / 2# x = ZZ(1): y = ZZ(2) GOSUB 500 'AssignXY(ZZ,x,y) RETURN ' ZZ=TH(Z1) 2300 GOSUB 2200 'ShComplex(ZZ,Z1) FOR i = 1 TO 4 temp(i) = ZZ(i) NEXT GOSUB 2100 'ChComplex(ZZ,Z1) FOR i = 1 TO 4 Z1(i) = temp(i) Z2(i) = ZZ(i) NEXT GOSUB 1400 'DivComplex(ZZ,Z1,Z2) RETURN ' ZZ=ARCCOS(Z1) 2400 FOR i = 1 TO 4 Z(i) = Z1(i) NEXT temp(1) = 1# - Z1(1) * Z1(1) + Z1(2) * Z1(2) temp(2) = -2# * Z1(1) * Z1(2) x = temp(1): y = temp(2) GOSUB 500 'AssignXY(ZZ,x,y) FOR i = 1 TO 4 Z1(i) = ZZ(i) NEXT x = .5#: y = 0# GOSUB 500 'AssignXY(ZZ,x,y) FOR i = 1 TO 4 Z2(i) = ZZ(i) NEXT GOSUB 1700 'PowerComplex(ZZ,Z1,Z2) tmp = ZZ(1) ZZ(1) = Z(1) - ZZ(2) ZZ(2) = Z(2) + tmp x = ZZ(1): y = ZZ(2) GOSUB 500 'AssignXY(ZZ,x,y) FOR i = 1 TO 4 Z1(i) = ZZ(i) NEXT GOSUB 1600 'LnComplex(ZZ,Z1) tmp = ZZ(1) ZZ(1) = ZZ(2): ZZ(2) = -tmp x = ZZ(1): y = ZZ(2) GOSUB 500 'AssignXY(ZZ,x,y) RETURN ' ZZ=ARCSIN(Z1) 2500 FOR i = 1 TO 4 Z(i) = Z1(i) NEXT temp(1) = 1# - Z1(1) * Z1(1) + Z1(2) * Z1(2) temp.b = -2# * Z1(1) * Z1(2) x = temp(1): y = temp(2) GOSUB 500 'AssignXY(ZZ,x,y) FOR i = 1 TO 4 Z1(i) = ZZ(i) NEXT x = .5#: y = 0# GOSUB 500 'AssignXY(ZZ,x,y) FOR i = 1 TO 4 Z2(i) = ZZ(i) NEXT GOSUB 1700 'PowerComplex(ZZ,Z1,Z2) ZZ(1) = ZZ(1) - Z(2) ZZ(2) = ZZ(2) + Z(1) x = ZZ(1): y = ZZ(2) GOSUB 500 'AssignXY(ZZ,x,y) FOR i = 1 TO 4 Z1(i) = ZZ(i) NEXT GOSUB 1600 'LnComplex(ZZ,Z1) tmp = ZZ(1) ZZ(1) = ZZ(2): ZZ(2) = -tmp x = ZZ(1): y = ZZ(2) GOSUB 500 'AssignXY(ZZ,x,y) RETURN ' ZZ=ARCTAN(Z1) 2600 temp(1) = -Z1(1): temp(2) = 1# - Z1(2) u(1) = Z1(1): u(2) = 1# + Z1(2) x = temp(1): y = temp(2) GOSUB 500 'AssignXY(ZZ,x,y) FOR i = 1 TO 4 Z1(i) = ZZ(i) NEXT x = u(1): y = u(2) GOSUB 500 'AssignXY(ZZ,x,y) FOR i = 1 TO 4 Z2(i) = ZZ(i) NEXT GOSUB 1400 'DivComplex(ZZ,Z1,Z2) FOR i = 1 TO 4 Z1(i) = ZZ(i) NEXT GOSUB 1600 'LnComplex(ZZ,Z1) tmp = ZZ(1) ZZ(1) = ZZ(2) / 2#: ZZ(2) = -tmp / 2# x = ZZ(1): y = ZZ(2) GOSUB 500 'AssignXY(ZZ,x,y) RETURN ' ZZ=ARGCH(Z1) 2700 FOR i = 1 TO 4 Z(i) = Z1(i) NEXT temp(1) = -1# + Z1(1) * Z1(1) + Z1(2) * Z1(2) temp(2) = 2# * Z1(1) * Z1(2) x = temp(1): y = temp(2) GOSUB 500 'AssignXY(ZZ,x,y) FOR i = 1 TO 4 Z1(i) = ZZ(i) NEXT x = .5#: y = 0# GOSUB 500 'AssignXY(ZZ,x,y) FOR i = 1 TO 4 Z2(i) = ZZ(i) NEXT GOSUB 1700 'PowerComplex(ZZ,Z1,Z2) ZZ(1) = ZZ(1) + Z(1) ZZ(2) = ZZ(2) + Z(2) x = ZZ(1): y = ZZ(2) GOSUB 500 'AssignXY(ZZ,x,y) FOR i = 1 TO 4 Z1(i) = ZZ(i) NEXT GOSUB 1600 'LnComplex(ZZ,Z1) RETURN ' ZZ=ARGSH(Z1) 2800 FOR i = 1 TO 4 Z(i) = Z1(i) NEXT temp(1) = 1# + Z1(1) * Z1(1) - Z1(2) * Z1(2) temp(2) = 2# * Z1(1) * Z1(2) x = temp(1): y = temp(2) GOSUB 500 'AssignXY(ZZ,x,y) FOR i = 1 TO 4 Z1(i) = ZZ(i) NEXT x = .5#: y = 0# GOSUB 500 'AssignXY(ZZ,x,y) FOR i = 1 TO 4 Z2(i) = ZZ(i) NEXT GOSUB 1700 'PowerComplex(ZZ,Z1,Z2) x = ZZ(1) + Z(1) y = ZZ(2) + Z(2) GOSUB 500 'AssignXY(ZZ,x,y) FOR i = 1 TO 4 Z1(i) = ZZ(i) NEXT GOSUB 1600 'LnComplex(ZZ,Z1) RETURN ' ZZ=ARGTH(Z1) 2900 FOR i = 1 TO 4 Z(i) = Z1(i) NEXT Z1(1) = 1# + Z(1): Z1(2) = Z(2) Z2(1) = 1# - Z(1): Z2(2) = -Z(2) x = Z1(1): y = Z1(2) GOSUB 500 'AssignXY(ZZ,x,y) FOR i = 1 TO 4 Z1(i) = ZZ(i) NEXT x = Z2(1): y = Z2(2) GOSUB 500 'AssignXY(ZZ,x,y) FOR i = 1 TO 4 Z2(i) = ZZ(i) NEXT GOSUB 1400 'DivComplex(ZZ,Z1,Z2) FOR i = 1 TO 4 Z1(i) = ZZ(i) NEXT GOSUB 1600 'LnComplex(ZZ,Z1) ZZ(1) = ZZ(1) / 2#: ZZ(2) = ZZ(2) / 2# x = ZZ(1): y = ZZ(2) GOSUB 500 'AssignXY(ZZ,x,y) RETURN 'end of file tcomplex.bas