''' complex_calc_1.f90 z1: convert from real/imag to and from mag/phase Z=z1*z2 or z=z1/z2 ''' import cmath, math def polrec(iform,x,y): if iform==1: z=complex(x,y) if iform==2: z=x*cmath.exp(complex(0.,y*3.141593/180)) return z def recpol(iform,z): if iform==2: return if iform==1: zm=abs(z); zp=cmath.phase(z)*180/3.141593; return zm,zp while True: print(' ') print('[ SIMPLE COMPLEX CALCULATOR ]') if1,x1,y1=map(float,input('z1: iform(1:real/imag., 2:mag./phase),x1,y1=').split()) if1=int(if1) z1=polrec(if1,x1,y1) print(' z1=',z1) print('Operation on z1:') print(' iop=1: real/imag. to mag./phase') print(' iop=2: mag./phase to real/imag.') print(' iop=3: multiply z1 with the next number z2') print(' iop=4: divide z1 by the next number z2') iop=int(input('iop=')) if iop==1: zm,zp=recpol(if1,z1); print(' mag.(z1),phase(z1)(deg.)=',zm,',',zp) if iop==2: z1=polrec(if1,x1,y1); print(' z1=',z1) if iop==3 or iop==4: if2,x2,y2=map(float,input('z2: iform(1:real/imag., 2:mag./phase),x2,y2=').split()) z2=polrec(if2,x2,y2) print(' z2=',z2) if iop==3: z=z1*z2 if iop==4: z=z1/z2 zm,zp=recpol(1,z) print(' z=',z,'; mag(z),phase(z)(deg)=',zm,',',zp) ''' [ SIMPLE COMPLEX CALCULATOR ] z1: iform(1:real/imag., 2:mag./phase),x1,y1= 1 1 2 z1= (1+2j) Operation on z1: iop=1: real/imag. to mag./phase iop=2: mag./phase to real/imag. iop=3: multiply z1 with the next number z2 iop=4: divide z1 by the next number z2 iop= 3 z2: iform(1:real/imag., 2:mag./phase),x2,y2= 1 3 4 z2= (3+4j) z= (-5+10j) ; mag(z),phase(z)(deg)= 11.180339887498949 , 116.56503832394155 [ SIMPLE COMPLEX CALCULATOR ] z1: iform(1:real/imag., 2:mag./phase),x1,y1= '''