Abt. Komplexe Funktionen für Komplexe Zahlen
==============================
Nachstehend eine in Profan-11 zum Laufen gebrachte Studie, wie man Komplexe Zahlen als Vier-Tupel darstellen kann und dadurch manche Funktionen (z.B. trigonometrische) leicht auf Komplexe Zahlen erweitern könnte (2 Teile, bitte zusammensetzen).
Gruss
WindowTitle "PGM TComplex: Operationen mit Komplexen Zahlen (Re+j*Im)"
' Q:http://jean-pierre.moreau.pagesperso-orange.fr/Basic/tcomplex_bas.txt
' (D) Demo für eine Übertragung nach XProfan-11.2a. Ohne jede Gewähr!
' 2017-03 by P.Specht, Vienna/Austria. Mögliche Rechte Dritter ungeprüft.
'
' A complex number Z is represented as an array with 4 locations:
' z(1), z(2) for algebraic form x+%j y = (x,y)
' z(3), z(4) for polar form r*exp(%i*t) = r versor t = (r \ t)
WindowStyle 24
Window %maxx*0.2,%maxy*0.1 - %maxx*0.6,%maxy*0.6
font 2
Declare ZZ![4],Z![4],Z1![4],Z2![4],temp![4],u![4] 'Complex numbers
Declare tmp!,x!,y!,r!,t!,i&
var XINF!=1.2E16 'big number
var TINY!=val("1E-16")'small
var PI!=4*ArcTan(1)
var F$="#0.0000"
x!=1 : y!=-2 : AssignXY(ZZ![],x!,y!):Z1![]=ZZ![&index]
x!=2.5: y!=5.75 : AssignXY(ZZ![],x!,y!):Z2![]=ZZ![&index]
AddCpx(ZZ![],Z1![],Z2![]):Z![]=ZZ![&index]
PRINT "\n Z1 = ";:ZZ![]=Z1![&index]:DsplCpx(ZZ![])
PRINT "\n Z2 = ";:ZZ![]=Z2![&index]:DsplCpx(ZZ![])
PRINT "\n Z1+Z2 = ";:ZZ![]=Z![&index]:DsplCpx(ZZ![])
PRINT "\n\n Z1+Z2 polar = ";:DsplCpxR(ZZ![])
PRINT "\n Z1 polar = ";:ZZ![]=Z1![&index]:DsplCpxR(ZZ![])
PRINT "\n Z2 polar = ";:ZZ![]=Z2![&index]:DsplCpxR(ZZ![])
MulCpx(ZZ![],Z1![],Z2![])
PRINT "\n\n Z1*Z2 = ";:DsplCpx(ZZ![])
PRINT "\n Z1*Z2 polar = ";:DsplCpxR(ZZ![])
DivCpx(ZZ![],Z1![],Z2![])
PRINT "\n\n Z1 / Z2 polar = ";:DsplCpxR(ZZ![])
ExpCpx(ZZ![],Z1![])
PRINT "\n\n Exp(Z1) = ";:DsplCpx(ZZ![])
PRINT "\n Exp(Z1) polar = ";:DsplCpxR(ZZ![])
x!=2.5:y!=5.75 :AssignXY(ZZ![],x!,y!)
Z1![]=ZZ![&index]
AreaSinHypCpx(ZZ![],Z1![])
PRINT "\n\n AreaSinHypCpx(Z2) = ";:DsplCpx(ZZ![])
x!=1 : y!=-2 : AssignXY(ZZ![],x!,y!):Z1![]=ZZ![&index]
x!=2.5: y!=5.75 : AssignXY(ZZ![],x!,y!):Z2![]=ZZ![&index]
SubtrCpx
PRINT "\n\n SubtrCpx(zz=z1-z2) = ";:DsplCpx(ZZ![])
ChgSgnCpx
PRINT "\n ChgSgnCpx(zz) = ";:DsplCpx(ZZ![])
ConjugCpx
PRINT "\n ConjugCpx(zz) = ";:DsplCpx(ZZ![])
LnCpx
PRINT "\n LnCpx(zz) = ";:DsplCpx(ZZ![])
ExpCpx
PRINT "\n ExpCpx(zz) = ";:DsplCpx(ZZ![])
x!=1 : y!=2 :AssignXY(ZZ![],x!,y!):Z1![]=ZZ![&index]
x!=0 : y!=1 :AssignXY(ZZ![],x!,y!):Z2![]=ZZ![&index]
PowerCpx
PRINT "\n PowerCpx(zz=z1^z2) = ";:DsplCpx(ZZ![])
waitinput
cls
x!=1 : y!=2 :AssignXY(ZZ![],x!,y!):Z1![]=ZZ![&index]
CosCpx(zz![],Z1![])
PRINT "\n\n CosCpx(zz=cos Z1) = ";:DsplCpx(ZZ![])
x!=1 : y!=2 :AssignXY(ZZ![],x!,y!):Z1![]=ZZ![&index]
SinCpx(zz![],Z1![])
PRINT "\n SinCpx(zz=sin Z1) = ";:DsplCpx(ZZ![])
x!=1 : y!=2 :AssignXY(ZZ![],x!,y!):Z1![]=ZZ![&index]
TanCpx(zz![],Z1![])
PRINT "\n TanCpx(zz=tan Z1) = ";:DsplCpx(ZZ![])
x!=1 : y!=2 :AssignXY(ZZ![],x!,y!):Z1![]=ZZ![&index]
ArcCosCpx
PRINT "\n\n ArcCosCpx (zz=acos Z1) = ";:DsplCpx(ZZ![])
x!=1 : y!=2 :AssignXY(ZZ![],x!,y!):Z1![]=ZZ![&index]
ArcSinCpx(zz![],Z1![])
PRINT "\n ArcSinCpx(zz=asin Z1) = ";:DsplCpx(ZZ![])
x!=1 : y!=2 :AssignXY(ZZ![],x!,y!):Z1![]=ZZ![&index]
ArcTanCpx(zz![],Z1![])
PRINT "\n ArcTanCpx(zz=atan Z1) = ";:DsplCpx(ZZ![])
x!=1 : y!=2 :AssignXY(ZZ![],x!,y!):Z1![]=ZZ![&index]
CoshypCpx
PRINT "\n\n\n CoshypCpx(zz=cosh z1) = ";:DsplCpx(ZZ![])
x!=1 : y!=2 :AssignXY(ZZ![],x!,y!):Z1![]=ZZ![&index]
SinhypCpx
PRINT "\n SinhypCpx(zz=sinh z1) = ";:DsplCpx(ZZ![])
x!=1 : y!=2 :AssignXY(ZZ![],x!,y!):Z1![]=ZZ![&index]
TanhypCpx
PRINT "\n TanhypCpx(zz=sinh z1) = ";:DsplCpx(ZZ![])
x!=1 : y!=2 :AssignXY(ZZ![],x!,y!):Z1![]=ZZ![&index]
AreaCoshypCpx
PRINT "\n\n AreaCoshypCpx(zz=acosh z1) = ";:DsplCpx(ZZ![])
x!=1 : y!=2 :AssignXY(ZZ![],x!,y!):Z1![]=ZZ![&index]
AreaSinhypCpx
PRINT "\n AreaSinhypCpx(zz=asinh z1) = ";:DsplCpx(ZZ![])
x!=1 : y!=2 :AssignXY(ZZ![],x!,y!):Z1![]=ZZ![&index]
AreaTanhypCpx
PRINT "\n AreaTanhypCpx(zz=atanh z1) = ";:DsplCpx(ZZ![])
PRINT:beep:Waitinput
END
Proc AssignXY 'def Complex ZZ by values x (=Re) and y (=Im)
ZZ![1]=x!:ZZ![2]=y!
ZZ![3]=SQRT(sqr(x!)+sqr(y!))
IF x!=0
IF y! > 0
ZZ![4]=PI! / 2
ELSEIF y!=0
ZZ![4]=-PI! / 2
ELSE
ZZ![4]=0
ENDIF
ELSE
ZZ![4]=ArcTan(y!/x!)
IF x! < 0
IF y! >= 0
ZZ![4]=ZZ![4] + PI!
ELSE
ZZ![4]=ZZ![4] - PI!
ENDIF
ENDIF
case ZZ![4] > PI!:ZZ![4]=ZZ![4] - 2 * PI!
case ZZ![4] < (-PI!):ZZ![4]=ZZ![4] + 2 * PI!
ENDIF
EndProc
Proc AssignRT 'define Cpx number by r and t in radians
ZZ![3]=r!:ZZ![4]=t!
case ZZ![4] > PI!:ZZ![4]=ZZ![4] - 2 * PI!
case ZZ![4]<(-PI!):ZZ![4]= ZZ![4] + 2 * PI!
ZZ![1]=r! * COS(t!):ZZ![2]=r! * SIN(t!)
endproc
Proc DsplCpx 'Dspl Cpx number with x and y
PRINT "("+format$(F$,ZZ![1])+" +j* "+format$(F$,ZZ![2])+")";
Endproc
Proc DsplCpxR 'Dspl Cpx number with radius and phase in radians
PRINT "("+format$(F$,ZZ![3])+" \ "+format$(F$,ZZ![4])+")";
Endproc
Proc AddCpx 'add two Cpx numbers:ZZ=Z1+Z2
ZZ![1]=Z1![1] + Z2![1]:ZZ![2]=Z1![2] + Z2![2]
x!=ZZ![1]:y!=ZZ![2]
AssignXY(ZZ![],x!,y!)
EndProc
Proc SubtrCpx 'subtract two Cpx numbers:ZZ=Z1-Z2
ZZ![1]=Z1![1] - Z2![1]:ZZ![2]=Z1![2] - Z2![2]
x!=ZZ![1]:y!=ZZ![2]:AssignXY(ZZ![],x!,y!)
EndProc
Proc ChgSgnCpx 'change sign of a Cpx number:ZZ=-ZZ
ZZ![1]=-1*ZZ![1]:ZZ![2]=-1*ZZ![2]
x!=ZZ![1]:y!=ZZ![2]:AssignXY(ZZ![],x!,y!)
EndProc
Proc ConjugCpx 'change sign of Im of a Cpx number
ZZ![2]=-1*ZZ![2]
x!=ZZ![1]:y!=ZZ![2]:AssignXY(ZZ![],x!,y!)
EndProc
proc MulCpx 'multiply two Cpx numbers:ZZ=Z1*Z2
ZZ![3]=Z1![3] * Z2![3]:ZZ![4]=Z1![4] + Z2![4]
r!=ZZ![3]:t!=ZZ![4]
AssignRT(ZZ![],r!,t!)
endproc
Proc DivCpx 'divide two Cpx numbers:ZZ=Z1/Z2
IF Z2![3] < TINY!
ZZ![3]=XINF!
ELSE
ZZ![3]=Z1![3] / Z2![3]
ENDIF
ZZ![4]=Z1![4] - Z2![4]
r!=ZZ![3]:t!=ZZ![4]
AssignRT(ZZ![],r!,t!)
endproc
Proc ExpCpx 'exponential Cpx function:ZZ=Exp(Z1)
IF EXP(Z1![1]) > XINF!
tmp!=XINF!
ELSE
tmp!=EXP(Z1![1])
ENDIF
ZZ![1]=tmp! * COS(Z1![2]):ZZ![2]=tmp! * SIN(Z1![2])
x!=ZZ![1]:y!=ZZ![2]
AssignXY(ZZ![],x!,y!)
EndProc
Proc LnCpx 'ZZ=LN(Z1)
IF Z1![3] <= 0
ZZ![1]=-1*XINF!
ELSE
ZZ![1]=LN(Z1![3])
ENDIF
ZZ![2]=Z1![4]
x!=ZZ![1]:y!=ZZ![2]
AssignXY(ZZ![],x!,y!)
EndProc
Proc PowerCpx 'Cpx power ZZ=Z1^Z2
LnCpx(temp![],Z1![])
Whileloop 4:i&=&Loop
Z1![i&]=Z2![i&]
Z2![i&]=ZZ![i&]
endwhile
MulCpx(temp![],Z1![],temp![])
Z1![]=ZZ![&index]
ExpCpx(ZZ![],Z1![])
EndProc
Alles anzeigen